From c59111945f286104c0f133d87f1210fa309e5c13 Mon Sep 17 00:00:00 2001 From: Adam Boro Date: Tue, 6 Feb 2024 09:48:47 +0100 Subject: [PATCH 1/5] fix(distributor): match yoast primary category --- includes/class-distributor-customizations.php | 2 - .../distributor-customizations/global.php | 90 ++++++++++++++----- 2 files changed, 68 insertions(+), 24 deletions(-) diff --git a/includes/class-distributor-customizations.php b/includes/class-distributor-customizations.php index e51c9d54..12d45f46 100644 --- a/includes/class-distributor-customizations.php +++ b/includes/class-distributor-customizations.php @@ -24,6 +24,4 @@ public static function init() { Distributor_Customizations\Author_Ingestion::init(); Distributor_Customizations\Authorship_Filters::init(); } - - } diff --git a/includes/distributor-customizations/global.php b/includes/distributor-customizations/global.php index b9367505..43159beb 100644 --- a/includes/distributor-customizations/global.php +++ b/includes/distributor-customizations/global.php @@ -1,23 +1,64 @@ ID ); + $category_id = $primary_term->get_primary_term(); + if ( $category_id ) { + $category = get_term( $category_id ); + return $category->slug; + } + } +} + +/** + * Fix primary category on the Node. + * + * @param WP_Post $post The post object. + * @param WP_REST_Request $request The request data. + */ +function newspack_network_fix_primary_category( $post, $request ) { + $primary_category_id = get_post_meta( $post->ID, '_yoast_wpseo_primary_category', true ); + $primary_category_slug = get_post_meta( $post->ID, 'yoast_primary_category_slug', true ); + $hub_primary_category = get_term( $primary_category_id ); + if ( ! $hub_primary_category ) { + // Attempt to find a matching category on the Hub site by slug. + $hub_primary_category = get_term_by( 'slug', $primary_category_slug, 'category' ); + } + if ( $hub_primary_category ) { + update_post_meta( $post->ID, '_yoast_wpseo_primary_category', $hub_primary_category->term_id ); + } elseif ( class_exists( '\Newspack\Logger' ) ) { + \Newspack\Logger::error( __( 'No matching category found on the Hub site.', 'newspack-network' ) ); + } +} + +/** + * This filter is used to filter the arguments sent to the remote server during a push. */ add_filter( 'dt_push_post_args', function( $post_body, $post ) { + // Pass the original published date to the new pushed post and set the same published date + // instead of setting it to the current time. $post_body['date'] = $post->post_date; $post_body['date_gmt'] = $post->post_date_gmt; + + $slug = newspack_network_get_primary_category_slug( $post_body, $post ); + if ( $slug ) { + $post_body['distributor_meta']['yoast_primary_category_slug'] = $slug; + } + return $post_body; }, 10, @@ -38,34 +79,19 @@ function( $post_array, $remote_id, $post ) { 10, 3 ); -/** - * ========================================= - * ===== End of Post publication date ====== - * ========================================= - */ /** - * ========================================= - * ===== Allow editors to pull content ===== - * ========================================= + * Allow editors to pull content. */ function newspack_network_filter_distributor_menu_cap() { return 'edit_others_posts'; } add_filter( 'dt_capabilities', 'newspack_network_filter_distributor_menu_cap' ); add_filter( 'dt_pull_capabilities', 'newspack_network_filter_distributor_menu_cap' ); -/** - * ========================================= - * ==== End of editors to pull content ===== - * ========================================= - */ /** - * ========================================= - * =========== Bug Workaround ============== * This is a workaround the bug fixed in https://github.com/10up/distributor/pull/1185 * Until that fix is released, we need to keep this workaround. - * ========================================= */ add_action( 'init', @@ -73,3 +99,23 @@ function() { wp_cache_delete( 'dt_media::{$post_id}', 'dt::post' ); } ); + +/** + * Send primary category slug to the Node when updating a post. + */ +add_filter( + 'dt_subscription_post_args', + function( $post_body, $post ) { + $slug = newspack_network_get_primary_category_slug( $post_body, $post ); + $post_body['post_data']['distributor_meta']['yoast_primary_category_slug'] = $slug; + return $post_body; + }, + 10, + 2 +); + +/** + * Map Hub primary category to the primary category on the Node. + */ +add_action( 'dt_process_subscription_attributes', 'newspack_network_fix_primary_category', 10, 2 ); +add_action( 'dt_process_distributor_attributes', 'newspack_network_fix_primary_category', 10, 2 ); From 1da862c0f84ac8401113f10699bcf47a709c0f58 Mon Sep 17 00:00:00 2001 From: Adam Boro Date: Tue, 6 Feb 2024 14:46:35 +0100 Subject: [PATCH 2/5] fix: account for ID clashes --- includes/distributor-customizations/global.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/includes/distributor-customizations/global.php b/includes/distributor-customizations/global.php index 43159beb..e61f924f 100644 --- a/includes/distributor-customizations/global.php +++ b/includes/distributor-customizations/global.php @@ -29,13 +29,9 @@ function newspack_network_get_primary_category_slug( $post_body, $post ) { * @param WP_REST_Request $request The request data. */ function newspack_network_fix_primary_category( $post, $request ) { - $primary_category_id = get_post_meta( $post->ID, '_yoast_wpseo_primary_category', true ); $primary_category_slug = get_post_meta( $post->ID, 'yoast_primary_category_slug', true ); - $hub_primary_category = get_term( $primary_category_id ); - if ( ! $hub_primary_category ) { - // Attempt to find a matching category on the Hub site by slug. - $hub_primary_category = get_term_by( 'slug', $primary_category_slug, 'category' ); - } + // Match the category by slug, the IDs might have a clash. + $hub_primary_category = get_term_by( 'slug', $primary_category_slug, 'category' ); if ( $hub_primary_category ) { update_post_meta( $post->ID, '_yoast_wpseo_primary_category', $hub_primary_category->term_id ); } elseif ( class_exists( '\Newspack\Logger' ) ) { From 94030533b66658265bb57ee2c63a59d86e3dd9d4 Mon Sep 17 00:00:00 2001 From: Adam Boro Date: Thu, 8 Feb 2024 14:28:32 +0100 Subject: [PATCH 3/5] feat: handle content pull --- .../distributor-customizations/global.php | 38 ++++++++++++++++--- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/includes/distributor-customizations/global.php b/includes/distributor-customizations/global.php index e61f924f..951d9e7c 100644 --- a/includes/distributor-customizations/global.php +++ b/includes/distributor-customizations/global.php @@ -8,11 +8,33 @@ /** * Send primary category slug to the Node along with post update data. * - * @param array $post_body The post data to be sent to the Node. * @param WP_Post $post The post object. + * @param bool $is_pulling Whether the post is being pulled from the remote site. */ -function newspack_network_get_primary_category_slug( $post_body, $post ) { - if ( class_exists( 'WPSEO_Primary_Term' ) ) { +function newspack_network_get_primary_category_slug( $post, $is_pulling = false ) { + if ( $is_pulling ) { + // When pulling content, the post will be the Node post. + // The category slug has to be read from the data on the post object. + if ( ! isset( $post->meta['_yoast_wpseo_primary_category'] ) ) { + return; + } + $primary_category_id = reset( $post->meta['_yoast_wpseo_primary_category'] ); + if ( ! $primary_category_id ) { + return; + } + $maybe_primary_categories = array_filter( + $post->terms['category'], + function( $category ) use ( $primary_category_id ) { + return (int) $category['term_id'] === (int) $primary_category_id; + } + ); + $maybe_primary_category = reset( $maybe_primary_categories ); + if ( $maybe_primary_category ) { + return $maybe_primary_category['slug']; + } + } elseif ( class_exists( 'WPSEO_Primary_Term' ) ) { + // When pushing, the post will be the Hub post. + // The category exists on the site which executes this code, so it can be retrieved via Yoast. $primary_term = new WPSEO_Primary_Term( 'category', $post->ID ); $category_id = $primary_term->get_primary_term(); if ( $category_id ) { @@ -50,7 +72,7 @@ function( $post_body, $post ) { $post_body['date'] = $post->post_date; $post_body['date_gmt'] = $post->post_date_gmt; - $slug = newspack_network_get_primary_category_slug( $post_body, $post ); + $slug = newspack_network_get_primary_category_slug( $post ); if ( $slug ) { $post_body['distributor_meta']['yoast_primary_category_slug'] = $slug; } @@ -70,6 +92,12 @@ function( $post_body, $post ) { function( $post_array, $remote_id, $post ) { $post_array['post_date'] = $post->post_date; $post_array['post_date_gmt'] = $post->post_date_gmt; + + $slug = newspack_network_get_primary_category_slug( $post, true ); + if ( $slug ) { + $post_array['meta']['yoast_primary_category_slug'] = $slug; + } + return $post_array; }, 10, @@ -102,7 +130,7 @@ function() { add_filter( 'dt_subscription_post_args', function( $post_body, $post ) { - $slug = newspack_network_get_primary_category_slug( $post_body, $post ); + $slug = newspack_network_get_primary_category_slug( $post ); $post_body['post_data']['distributor_meta']['yoast_primary_category_slug'] = $slug; return $post_body; }, From 3786e464abe370ea323f6ba7a943b238eeb82d68 Mon Sep 17 00:00:00 2001 From: Adam Boro Date: Thu, 8 Feb 2024 20:38:07 +0100 Subject: [PATCH 4/5] fix: on pulling --- .../distributor-customizations/global.php | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/includes/distributor-customizations/global.php b/includes/distributor-customizations/global.php index 951d9e7c..78d6f4ee 100644 --- a/includes/distributor-customizations/global.php +++ b/includes/distributor-customizations/global.php @@ -13,7 +13,7 @@ */ function newspack_network_get_primary_category_slug( $post, $is_pulling = false ) { if ( $is_pulling ) { - // When pulling content, the post will be the Node post. + // When pulling content, the post will be the remote site post (not on the WP instance that executes this code). // The category slug has to be read from the data on the post object. if ( ! isset( $post->meta['_yoast_wpseo_primary_category'] ) ) { return; @@ -33,7 +33,7 @@ function( $category ) use ( $primary_category_id ) { return $maybe_primary_category['slug']; } } elseif ( class_exists( 'WPSEO_Primary_Term' ) ) { - // When pushing, the post will be the Hub post. + // When pushing, the post will be the post on this site. // The category exists on the site which executes this code, so it can be retrieved via Yoast. $primary_term = new WPSEO_Primary_Term( 'category', $post->ID ); $category_id = $primary_term->get_primary_term(); @@ -47,10 +47,9 @@ function( $category ) use ( $primary_category_id ) { /** * Fix primary category on the Node. * - * @param WP_Post $post The post object. - * @param WP_REST_Request $request The request data. + * @param WP_Post $post The post object. */ -function newspack_network_fix_primary_category( $post, $request ) { +function newspack_network_fix_primary_category( $post ) { $primary_category_slug = get_post_meta( $post->ID, 'yoast_primary_category_slug', true ); // Match the category by slug, the IDs might have a clash. $hub_primary_category = get_term_by( 'slug', $primary_category_slug, 'category' ); @@ -95,7 +94,7 @@ function( $post_array, $remote_id, $post ) { $slug = newspack_network_get_primary_category_slug( $post, true ); if ( $slug ) { - $post_array['meta']['yoast_primary_category_slug'] = $slug; + $post_array['meta_input']['yoast_primary_category_slug'] = $slug; } return $post_array; @@ -141,5 +140,11 @@ function( $post_body, $post ) { /** * Map Hub primary category to the primary category on the Node. */ -add_action( 'dt_process_subscription_attributes', 'newspack_network_fix_primary_category', 10, 2 ); -add_action( 'dt_process_distributor_attributes', 'newspack_network_fix_primary_category', 10, 2 ); +add_action( 'dt_process_subscription_attributes', 'newspack_network_fix_primary_category' ); +add_action( 'dt_process_distributor_attributes', 'newspack_network_fix_primary_category' ); +add_action( + 'dt_pull_post', + function( $new_post_id ) { + newspack_network_fix_primary_category( get_post( $new_post_id ) ); + } +); From 29ee85fa914a5c0e8688d28787fbed8dbebdee0d Mon Sep 17 00:00:00 2001 From: Adam Boro Date: Fri, 9 Feb 2024 08:28:42 +0100 Subject: [PATCH 5/5] refactor: variable and option names --- includes/distributor-customizations/global.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/includes/distributor-customizations/global.php b/includes/distributor-customizations/global.php index 78d6f4ee..5d774c90 100644 --- a/includes/distributor-customizations/global.php +++ b/includes/distributor-customizations/global.php @@ -50,11 +50,11 @@ function( $category ) use ( $primary_category_id ) { * @param WP_Post $post The post object. */ function newspack_network_fix_primary_category( $post ) { - $primary_category_slug = get_post_meta( $post->ID, 'yoast_primary_category_slug', true ); + $primary_category_slug = get_post_meta( $post->ID, 'newspack_network_primary_cat_slug', true ); // Match the category by slug, the IDs might have a clash. - $hub_primary_category = get_term_by( 'slug', $primary_category_slug, 'category' ); - if ( $hub_primary_category ) { - update_post_meta( $post->ID, '_yoast_wpseo_primary_category', $hub_primary_category->term_id ); + $found_primary_category = get_term_by( 'slug', $primary_category_slug, 'category' ); + if ( $found_primary_category ) { + update_post_meta( $post->ID, '_yoast_wpseo_primary_category', $found_primary_category->term_id ); } elseif ( class_exists( '\Newspack\Logger' ) ) { \Newspack\Logger::error( __( 'No matching category found on the Hub site.', 'newspack-network' ) ); } @@ -73,7 +73,7 @@ function( $post_body, $post ) { $slug = newspack_network_get_primary_category_slug( $post ); if ( $slug ) { - $post_body['distributor_meta']['yoast_primary_category_slug'] = $slug; + $post_body['distributor_meta']['newspack_network_primary_cat_slug'] = $slug; } return $post_body; @@ -94,7 +94,7 @@ function( $post_array, $remote_id, $post ) { $slug = newspack_network_get_primary_category_slug( $post, true ); if ( $slug ) { - $post_array['meta_input']['yoast_primary_category_slug'] = $slug; + $post_array['meta_input']['newspack_network_primary_cat_slug'] = $slug; } return $post_array; @@ -130,7 +130,7 @@ function() { 'dt_subscription_post_args', function( $post_body, $post ) { $slug = newspack_network_get_primary_category_slug( $post ); - $post_body['post_data']['distributor_meta']['yoast_primary_category_slug'] = $slug; + $post_body['post_data']['distributor_meta']['newspack_network_primary_cat_slug'] = $slug; return $post_body; }, 10,