Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion includes/content-distribution/class-cap-authors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
39 changes: 27 additions & 12 deletions includes/content-distribution/class-cap-guest-authors.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class Cap_Guest_Authors {
*/
const GUEST_AUTHORS_META_KEY = 'newspack_network_guest_authors_distributed';

/**
* Meta key to keep track of which authors and guest authors are assigned to a post.
*/
const ASSIGNED_AUTHORS_META_KEY = 'newspack_network_assigned_authors';

/**
* Go!
*
Expand All @@ -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() ) {
Expand Down Expand Up @@ -91,27 +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 );
$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 ( 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;
}

/**
Expand Down Expand Up @@ -151,20 +163,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::ASSIGNED_AUTHORS_META_KEY );
return;
}

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 );
}

Expand All @@ -179,6 +193,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::ASSIGNED_AUTHORS_META_KEY;

return $ignored_keys;
}
Expand Down