Skip to content

Commit

Permalink
Merge pull request #54 from passle/develop
Browse files Browse the repository at this point in the history
Bug fixes and SEO data addition to posts. Removed tweets from wordpress plugin
  • Loading branch information
klikas authored Apr 10, 2024
2 parents beff7ca + e0da477 commit 02dda5e
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 178 deletions.
45 changes: 15 additions & 30 deletions class/Models/PasslePost.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ class PasslePost
* @var PassleShareViewsNetwork[]|null
*/
public ?array $share_views;
/**
* A list of tweets that have been chosen to be shown alongside this post.
* @var PassleTweet[]
*/
public ?array $tweets;
/** An integer showing how many times this post has been shared. */
public int $total_shares;
/** An integer showing how many times the post has been liked. */
public int $total_likes;
Expand Down Expand Up @@ -84,6 +78,10 @@ class PasslePost
public string $quote_text;
/** The URL for the post's quote. */
public string $quote_url;
/** The metadata title of the original post. */
public string $metadata_title;
/** The metadata description of the original post. */
public string $metadata_description;

/* Options */
private bool $load_authors;
Expand Down Expand Up @@ -135,7 +133,6 @@ public function __construct($wp_post, array $options = [])
}

$this->initialize_share_views();
$this->initialize_tweets();
}

/**
Expand Down Expand Up @@ -200,6 +197,8 @@ private function initialize_wp_post()
$this->opens_in_new_tab = $this->meta["post_opens_in_new_tab"][0] ?? false;
$this->quote_text = $this->meta["post_quote_text"][0] ?? "";
$this->quote_url = $this->meta["post_quote_url"][0] ?? "";
$this->metadata_title = $this->meta["post_metadata_title"][0] ?? "";
$this->metadata_description = $this->meta["post_metadata_description"][0] ?? "";
}

/** @internal */
Expand Down Expand Up @@ -228,6 +227,8 @@ private function initialize_passle_post()
$this->opens_in_new_tab = $this->passle_post["OpensInNewTab"] ?? false;
$this->quote_text = $this->passle_post["QuoteText"] ?? "";
$this->quote_url = $this->passle_post["QuoteUrl"] ?? "";
$this->metadata_title = $this->passle_post["MetaData"]['Title'] ?? "";
$this->metadata_description = $this->passle_post["MetaData"]['Description'] ?? "";
}

/** @internal */
Expand Down Expand Up @@ -277,7 +278,7 @@ private function initialize_authors()
private function initialize_tags()
{
if (isset($this->meta)) {
$tags = $this->meta["post_tags"];
$tags = isset($this->meta["post_tags"]) ? $this->meta["post_tags"] : [];
$wp_tags = get_the_tags();
} else {
$tags = $this->passle_post["Tags"];
Expand All @@ -294,25 +295,14 @@ private function initialize_tags()
private function initialize_share_views()
{
if (isset($this->meta)) {
$share_views = $this->meta["post_share_views"];
$share_views = isset($this->meta["post_share_views"]) ? $this->meta["post_share_views"] : [];
} else {
$share_views = $this->passle_post["ShareViews"] ?? [];
}

$this->share_views = $this->map_share_views($share_views ?? []);
}

private function initialize_tweets()
{
if (isset($this->meta)) {
$tweets = $this->meta["post_tweets"] ?? [];
} else {
$tweets = $this->passle_post["Tweets"] ?? [];
}

$this->tweets = $this->map_tweets($tweets);
}

/*
* Mapping methods
*/
Expand All @@ -321,7 +311,11 @@ private function map_authors(string $shortcode_meta_key, string $author_meta_key
{
if (isset($this->meta)) {
$post_authors = array_map(fn ($author) => unserialize($author), $this->meta[$author_meta_key] ?? []);
$author_shortcodes = $this->meta[$shortcode_meta_key];
if (isset($this->meta[$shortcode_meta_key])) {
$author_shortcodes = $this->meta[$shortcode_meta_key];
} else {
$author_shortcodes = Utils::array_select($post_authors, "shortcode");
}
} else {
$post_authors = PostHandler::map_authors($this->passle_post[$author_post_key]);
$author_shortcodes = Utils::array_select($post_authors, "shortcode");
Expand Down Expand Up @@ -355,13 +349,4 @@ private function map_share_views(array $share_views)
return array_map(fn ($network) => new PassleShareViewsNetwork($network), PostHandler::map_share_views($share_views));
}
}

private function map_tweets(array $tweets)
{
if (isset($this->meta)) {
return array_map(fn ($tweet) => new PassleTweet(unserialize($tweet)), $tweets);
} else {
return array_map(fn ($tweet) => new PassleTweet($tweet), PostHandler::map_tweets($tweets));
}
}
}
33 changes: 0 additions & 33 deletions class/Models/PassleTweet.php

This file was deleted.

4 changes: 2 additions & 2 deletions class/Services/Content/Passle/PassleContentServiceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public static function get_all_paginated(string $url, int $page_number = 1)
return $result;
}

private static function get_next_url(string $url, int $page_number)
public static function get_next_url(string $url, int $page_number)
{
$parsed_url = wp_parse_url($url);
wp_parse_str($parsed_url['query'], $query);
Expand All @@ -191,7 +191,7 @@ private static function get_next_url(string $url, int $page_number)
return $next_url;
}

protected static function get(string $url)
public static function get(string $url)
{
$options = OptionsService::get();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static function fetch_entities(array $shortcodes = [])

$query = [
"numberposts" => -1,
"post_status" => array('publish', 'future'),
"post_type" => [$resource->get_post_type()],
];

Expand Down
2 changes: 1 addition & 1 deletion class/Services/TaxonomyRegistryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function create_taxonomies()
);

if (is_wp_error($term)) {
error_log("Error creating term: $term " . $term->get_error_message() . PHP_EOL);
error_log("Error creating term " . $tag . ": " . $term->get_error_message() . PHP_EOL);
}
}
}
Expand Down
11 changes: 2 additions & 9 deletions class/SyncHandlers/PostHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ protected static function map_data(array $data, int $entity_id)
"post_coauthors" => static::map_authors($data["CoAuthors"]),
"post_coauthors_shortcodes" => static::map_author_shortcodes($data["CoAuthors"]),
"post_share_views" => static::map_share_views($data["ShareViews"]),
"post_tweets" => static::map_tweets($data["Tweets"]),
"post_total_shares" => $data["TotalShares"],
"post_total_likes" => $data["TotalLikes"],
"post_is_repost" => $data["IsRepost"],
Expand All @@ -57,6 +56,8 @@ protected static function map_data(array $data, int $entity_id)
"post_opens_in_new_tab" => $data["OpensInNewTab"],
"post_quote_text" => $data["QuoteText"],
"post_quote_url" => $data["QuoteUrl"],
"post_metadata_title" => $data["MetaData"]['Title'] ?? "",
"post_metadata_description" => $data["MetaData"]['Description'] ?? "",
],
];

Expand Down Expand Up @@ -96,14 +97,6 @@ public static function map_share_views(array $share_views)
], $share_views);
}

public static function map_tweets(array $tweets)
{
return array_map(fn ($tweet) => [
"embed_code" => $tweet["EmbedCode"],
"tweet_id" => $tweet["TweetId"],
"screen_name" => $tweet["ScreenName"],
], $tweets);
}
public static function map_tags_and_aliases(array $tag_mappings)
{
$tags_to_return = array();
Expand Down
109 changes: 87 additions & 22 deletions class/SyncHandlers/SyncHandlerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Passle\PassleSync\SyncHandlers;

use Exception;
use Passle\PassleSync\Utils\ResourceClassBase;
use Passle\PassleSync\Utils\Utils;
use Passle\PassleSync\Utils\UrlFactory;
use Passle\PassleSync\Services\OptionsService;

abstract class SyncHandlerBase extends ResourceClassBase
Expand All @@ -15,14 +17,12 @@ public static function sync_all()
if (method_exists(static::class, "pre_sync_all_hook")) {
call_user_func([static::class, "pre_sync_all_hook"]);
}

$resource = static::get_resource_instance();

$wp_entities = call_user_func([$resource->wordpress_content_service_name, "fetch_entities"]);

$api_entities = call_user_func([$resource->passle_content_service_name, "get_cache"]);

static::compare_items($wp_entities, $api_entities);
static::batch_sync_all($wp_entities);
}

public static function sync_many(array $shortcodes)
Expand Down Expand Up @@ -79,23 +79,27 @@ private static function compare_items(array $wp_entities, array $api_entities)
$meta_shortcode_name = "{$resource->name_singular}_shortcode";

$passle_shortcodes = Utils::array_select($api_entities, $resource_shortcode_name);
$existing_shortcodes = array_map(fn ($item) => $item->{$meta_shortcode_name}, $wp_entities);
$existing_shortcodes = array_map(function ($item) use ($meta_shortcode_name) {
// post_shortcode can be an array. We need to return the string value
return is_array($item->{$meta_shortcode_name}) ? $item->{$meta_shortcode_name}[0] : $item->{$meta_shortcode_name};
}, $wp_entities);
$all_shortcodes = array_unique(array_merge($passle_shortcodes, $existing_shortcodes));

$shortcodes_to_delete = array_filter($existing_shortcodes, fn ($shortcode) => !in_array($shortcode, $passle_shortcodes));
$shortcodes_to_sync = array_filter($all_shortcodes, fn ($shortcode) => !in_array($shortcode, $shortcodes_to_delete));

// Delete
$items_to_delete = array_filter($wp_entities, fn ($item) => in_array($item->{$meta_shortcode_name}, $shortcodes_to_delete));
foreach ($items_to_delete as $item) {
static::delete($item->ID);
}
// Items
$shortcodes_pending = array_filter($existing_shortcodes, fn ($shortcode) => !in_array($shortcode, $passle_shortcodes));
$shortcodes_to_sync = array_filter($all_shortcodes, fn ($shortcode) => !in_array($shortcode, $shortcodes_pending));

// Add/update
$items_to_sync = array_filter($api_entities, fn ($item) => in_array($item[$resource_shortcode_name], $shortcodes_to_sync));
foreach ($items_to_sync as $item) {
static::create_or_update($item);
}

return array_filter($wp_entities, function ($item) use ($meta_shortcode_name, $shortcodes_pending) {
// post_shortcode can be an array. We need to use the string value to check if it is inside $shortcodes_pending
$shortcode = is_array($item->{$meta_shortcode_name}) ? $item->{$meta_shortcode_name}[0] : $item->{$meta_shortcode_name};
return in_array($shortcode, $shortcodes_pending);
});
}

protected static function delete(int $id)
Expand Down Expand Up @@ -144,15 +148,15 @@ protected static function insert_post(array $postarr, $wp_error = \false, $fire_

// Set post taxonomy terms based on tags
if (!empty($postarr_arrays["post_tag_group_tags"]) && $options->include_passle_tag_groups) {
$taxonomies = get_taxonomies(array("object_type" => array(PASSLESYNC_POST_TYPE), "public" => true, "_builtin" => false));
foreach ($taxonomies as $taxonomy) {
foreach($postarr_arrays["post_tag_group_tags"] as $tag) {
$term = get_term_by("name", $tag, $taxonomy);
if($term != null && $term->name && $term->taxonomy) {
wp_set_object_terms($post_id, $term->name, $term->taxonomy, true);
}
}
$taxonomies = get_taxonomies(array("object_type" => array(PASSLESYNC_POST_TYPE), "public" => true, "_builtin" => false));
foreach ($taxonomies as $taxonomy) {
foreach ($postarr_arrays["post_tag_group_tags"] as $tag) {
$term = get_term_by("name", $tag, $taxonomy);
if ($term != null && $term->name && $term->taxonomy) {
wp_set_object_terms($post_id, $term->name, $term->taxonomy, true);
}
}
}
}
unset($postarr_arrays["post_tag_group_tags"]);

Expand All @@ -174,4 +178,65 @@ protected static function extract_slug_from_url(string $url)
{
return basename($url);
}
}

protected static function batch_sync_all(array $wp_entities)
{
$passle_shortcodes = OptionsService::get()->passle_shortcodes;

foreach ($passle_shortcodes as &$passle_shortcode) {
static::sync_all_by_passle($passle_shortcode, $wp_entities);
}
}


public static function sync_all_by_passle(string $passle_shortcode, array $wp_entities)
{
$resource = static::get_resource_instance();

$url = (new UrlFactory())
->path("passlesync/{$resource->name_plural}")
->parameters([
"PassleShortcode" => $passle_shortcode,
"ItemsPerPage" => "100"
])
->build();

static::sync_all_paginated($url, 1, $wp_entities);
}


protected static function sync_all_paginated(string $url, int $page_number = 1, array $wp_entities)
{
$resource = static::get_resource_instance();
$max_pages = 1000; // Maximum number of pages to process

while ($page_number <= $max_pages) {

$next_url = call_user_func([$resource->passle_content_service_name, "get_next_url"], $url, $page_number);
$response = call_user_func([$resource->passle_content_service_name, "get"], $next_url);

// Validate the API response
if (!isset($response[ucfirst($resource->name_plural)])) {
throw new Exception("Failed to get data from the API", 500);
}

$response = $response[ucfirst($resource->name_plural)];

if (empty($response)) {
break; // No more items
}

// Compare and process the items, update pending entities array
$wp_entities = static::compare_items($wp_entities, $response);

$page_number += 1;
}

// Delete unused entities
foreach ($wp_entities as $item) {
static::delete($item->ID);
}

return;
}
}
Loading

0 comments on commit 02dda5e

Please sign in to comment.