From bc24364183799656146d151943123af365e0f80d Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Fri, 10 Oct 2025 16:00:39 -0700 Subject: [PATCH 1/2] fix: improve CAP GA logic --- .../class-cap-authors.php | 2 +- .../class-cap-guest-authors.php | 22 +++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/includes/content-distribution/class-cap-authors.php b/includes/content-distribution/class-cap-authors.php index 209a2786..7e5b3985 100644 --- a/includes/content-distribution/class-cap-authors.php +++ b/includes/content-distribution/class-cap-authors.php @@ -162,7 +162,7 @@ public static function ingest_incoming_for_post( $post_data, $post, $remote_url } } - do_action( 'newspack_network_incoming_cap_guest_authors', $post->ID, $guest_authors ); + do_action( 'newspack_network_incoming_cap_guest_authors', $post->ID, $guest_authors, $cap_authors_from_payload ); global $coauthors_plus; $coauthors_plus->add_coauthors( $post->ID, $guest_contributors ); diff --git a/includes/content-distribution/class-cap-guest-authors.php b/includes/content-distribution/class-cap-guest-authors.php index 12628780..b04e0b9d 100644 --- a/includes/content-distribution/class-cap-guest-authors.php +++ b/includes/content-distribution/class-cap-guest-authors.php @@ -19,6 +19,11 @@ class Cap_Guest_Authors { */ const GUEST_AUTHORS_META_KEY = 'newspack_network_guest_authors_distributed'; + /** + * Meta key to keep track of how many authors there are. + */ + const AUTHOR_COUNT_META_KEY = 'newspack_network_author_count'; + /** * Go! * @@ -35,7 +40,7 @@ public static function init(): void { add_filter( 'newspack_network_content_distribution_ignored_post_meta_keys', [ __CLASS__, 'filter_ignored_post_meta_keys' ], 10, 2 ); add_filter( 'newspack_network_outgoing_non_wp_user_author', [ __CLASS__, 'filter_outgoing_non_wp_user_author' ], 10, 2 ); - add_action( 'newspack_network_incoming_cap_guest_authors', [ __CLASS__, 'on_guest_authors_incoming' ], 10, 2 ); + add_action( 'newspack_network_incoming_cap_guest_authors', [ __CLASS__, 'on_guest_authors_incoming' ], 10, 3 ); if ( ! is_admin() ) { @@ -92,6 +97,7 @@ public static function filter_get_coauthors( $coauthors, $post_id ) { } $distributed_authors = get_post_meta( $post_id, self::GUEST_AUTHORS_META_KEY, true ); + $number_of_authors = get_post_meta( $post_id, self::AUTHOR_COUNT_META_KEY, true ); if ( ! $distributed_authors ) { return $coauthors; @@ -111,6 +117,11 @@ public static function filter_get_coauthors( $coauthors, $post_id ) { $guest_authors[] = (object) $distributed_author; } + // If a post has only guest authors, don't include the non-guest authors. + if ( $number_of_authors && (int) $number_of_authors === count( $guest_authors ) ) { + return [ ...$guest_authors ]; + } + return [ ...$coauthors, ...$guest_authors ]; } @@ -151,20 +162,22 @@ public static function filter_author_link( $link, $author_id, $author_nicename ) } /** - * Action callback for reacting to incoimng guest authors. + * Action callback for reacting to incoming guest authors. * * @param int $post_id The post ID. * @param array $guest_authors The guest authors. + * @param array $all_authors All synced authors. * * @return void */ - public static function on_guest_authors_incoming( $post_id, $guest_authors ): void { + public static function on_guest_authors_incoming( $post_id, $guest_authors, $all_authors ): void { if ( empty( $guest_authors ) ) { delete_post_meta( $post_id, self::GUEST_AUTHORS_META_KEY ); - + delete_post_meta( $post_id, self::AUTHOR_COUNT_META_KEY ); return; } + update_post_meta( $post_id, self::AUTHOR_COUNT_META_KEY, count( $all_authors ) ); update_post_meta( $post_id, self::GUEST_AUTHORS_META_KEY, $guest_authors ); } @@ -179,6 +192,7 @@ public static function on_guest_authors_incoming( $post_id, $guest_authors ): vo */ public static function filter_ignored_post_meta_keys( array $ignored_keys ): array { $ignored_keys[] = self::GUEST_AUTHORS_META_KEY; + $ignored_keys[] = self::AUTHOR_COUNT_META_KEY; return $ignored_keys; } From da7f88ea05c2f2be684adf5248664122f57a889a Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Mon, 13 Oct 2025 15:45:14 -0700 Subject: [PATCH 2/2] fix: improve guest author distribution --- .../class-cap-guest-authors.php | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/includes/content-distribution/class-cap-guest-authors.php b/includes/content-distribution/class-cap-guest-authors.php index b04e0b9d..d68956fc 100644 --- a/includes/content-distribution/class-cap-guest-authors.php +++ b/includes/content-distribution/class-cap-guest-authors.php @@ -20,9 +20,9 @@ class Cap_Guest_Authors { const GUEST_AUTHORS_META_KEY = 'newspack_network_guest_authors_distributed'; /** - * Meta key to keep track of how many authors there are. + * Meta key to keep track of which authors and guest authors are assigned to a post. */ - const AUTHOR_COUNT_META_KEY = 'newspack_network_author_count'; + const ASSIGNED_AUTHORS_META_KEY = 'newspack_network_assigned_authors'; /** * Go! @@ -96,33 +96,34 @@ public static function filter_get_coauthors( $coauthors, $post_id ) { return $coauthors; } - $distributed_authors = get_post_meta( $post_id, self::GUEST_AUTHORS_META_KEY, true ); - $number_of_authors = get_post_meta( $post_id, self::AUTHOR_COUNT_META_KEY, true ); + $distributed_authors = get_post_meta( $post_id, self::GUEST_AUTHORS_META_KEY, true ); + $assigned_author_names = get_post_meta( $post_id, self::ASSIGNED_AUTHORS_META_KEY, true ); if ( ! $distributed_authors ) { return $coauthors; } - $guest_authors = []; + $assigned_authors = []; + foreach ( $coauthors as $coauthor ) { + if ( empty( $assigned_author_names ) || in_array( $coauthor->display_name, $assigned_author_names ) ) { + $assigned_authors[] = $coauthor; + } + } foreach ( $distributed_authors as $distributed_author ) { - if ( 'guest_author' !== $distributed_author['type'] ) { continue; } - // This removes the author URL from the guest author. - $distributed_author['user_nicename'] = ''; - $distributed_author['ID'] = - 2; - $guest_authors[] = (object) $distributed_author; - } - - // If a post has only guest authors, don't include the non-guest authors. - if ( $number_of_authors && (int) $number_of_authors === count( $guest_authors ) ) { - return [ ...$guest_authors ]; + if ( empty( $assigned_author_names ) || in_array( $distributed_author['display_name'], $assigned_author_names ) ) { + // This removes the author URL from the guest author. + $distributed_author['user_nicename'] = ''; + $distributed_author['ID'] = - 2; + $assigned_authors[] = (object) $distributed_author; + } } - return [ ...$coauthors, ...$guest_authors ]; + return $assigned_authors; } /** @@ -173,11 +174,11 @@ public static function filter_author_link( $link, $author_id, $author_nicename ) public static function on_guest_authors_incoming( $post_id, $guest_authors, $all_authors ): void { if ( empty( $guest_authors ) ) { delete_post_meta( $post_id, self::GUEST_AUTHORS_META_KEY ); - delete_post_meta( $post_id, self::AUTHOR_COUNT_META_KEY ); + delete_post_meta( $post_id, self::ASSIGNED_AUTHORS_META_KEY ); return; } - update_post_meta( $post_id, self::AUTHOR_COUNT_META_KEY, count( $all_authors ) ); + update_post_meta( $post_id, self::ASSIGNED_AUTHORS_META_KEY, wp_list_pluck( $all_authors, 'display_name' ) ); update_post_meta( $post_id, self::GUEST_AUTHORS_META_KEY, $guest_authors ); } @@ -192,7 +193,7 @@ public static function on_guest_authors_incoming( $post_id, $guest_authors, $all */ public static function filter_ignored_post_meta_keys( array $ignored_keys ): array { $ignored_keys[] = self::GUEST_AUTHORS_META_KEY; - $ignored_keys[] = self::AUTHOR_COUNT_META_KEY; + $ignored_keys[] = self::ASSIGNED_AUTHORS_META_KEY; return $ignored_keys; }