Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(content-distribution): Sync authors #194

Open
wants to merge 33 commits into
base: trunk
Choose a base branch
from

Conversation

naxoc
Copy link
Member

@naxoc naxoc commented Jan 20, 2025

This copies most of the existing code for distributed post authors.

I tried to make the naming of classes make a bit more sense (using outgoing/incoming) naming-wise, but I think I might have lost my way somewhere in the process. It's still pretty unwieldy. Suggestions very welcome!

One thing I still lack: when a post is new and I click "publish" and then distribute it (without having touched authors), the cap authors are not added to the payload. I think the best way to fix that is to save the post from JS before distributing the first time. What do you think @miguelpeixe ?

How to test

  • Try distributing a post without having CAP enabled. Check that the author is transferred in a way that makes sense
  • Try with an author that is a guest contributor. Also try mixing "normal" users and guest contributors.
  • Try with Guest Authors enabled. Try mixing here too.

Still remains:

@naxoc naxoc self-assigned this Jan 20, 2025
@naxoc naxoc marked this pull request as ready for review January 29, 2025 18:56
@naxoc naxoc requested a review from a team as a code owner January 29, 2025 18:56
Copy link
Member

@miguelpeixe miguelpeixe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for refactoring the approach to fit the new partial payload implementation!

I'm able to get multiple authors synced, but with the standard guest author approach from CAP I'm unable to get the guest authors distributed.

I'm activating it via define( 'NEWSPACK_ENABLE_CAP_GUEST_AUTHORS', true ); on all sites. Should I be doing something different?

@@ -47,7 +49,7 @@ public static function init() {
}

add_action( 'init', [ __CLASS__, 'register_data_event_actions' ] );
add_action( 'shutdown', [ __CLASS__, 'distribute_queued_posts' ] );
add_action( 'shutdown', [ __CLASS__, 'distribute_queued_posts' ], 20 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for the change in priority?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think that is a relic from when I was trying to game the timing of things. :) I removed it

includes/content-distribution/class-cap-authors.php Outdated Show resolved Hide resolved
Comment on lines 587 to 612
* @param int $post_id The post ID.
* @param bool $is_linked Whether the post is linked.
* @param array $payload The post payload.
* @param int $post_id The post ID.
* @param bool $is_linked Whether the post is linked.
* @param array $payload The post payload.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change in linting?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was probably just my editor when it autoformats. There are too many spaces I think it picked up?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It follows the standard we use for functions and methods so the parameters info is aligned. It could be that your IDE doesn't interpret the do_action() as a thing that can precede that type of docblock.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right! I had the editor set to not WP for some reason and it felt like I had to keep tweaking it :)

return [
'site_url' => get_bloginfo( 'url' ),
'post_id' => $this->post->ID,
'post_url' => get_permalink( $this->post->ID ),
'network_post_id' => $this->get_network_post_id(),
'sites' => $this->get_distribution(),
'status_on_create' => $status_on_create,
'multiple_authors' => is_wp_error( $multiple_authors ) ? [] : $multiple_authors,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partial payload filters through the contents of post_data. This should move there in order to work as designed.

As it is now, at the root-level, it'll be always part of the payload. It works, but not how we expect it to.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Moved it to post_data

*
* @return WP_Error|array
*/
public static function get_wp_user_for_distribution( $user ) {
Copy link
Member

@miguelpeixe miguelpeixe Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need a whole class for this. WDYT of moving this to the Outgoing_Post class?

It's similar to other methods we have there that are meant to build the payload, like get_processed_post_content(), get_post_taxonomy_terms(), and get_post_meta().

/**
* Class to handle author ingestion for content distribution.
*/
class Incoming_Author {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think this class could become methods inside Incoming_Post.

return new WP_Error( 'insert_error', __( 'Error inserting post.', 'newspack-network' ) );
}

// Update the object.
$this->ID = $post_id;
$this->post = get_post( $this->ID );

Incoming_Author::ingest_author_for_post( $this->ID, $this->get_original_site_url(), $post_data['author'] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method makes a second post update to the db. Can we dissolve it and move its logic upper in this method's $postarr arguments?

Regular WP author is a core aspect of the post and should fit well in the composition of our insert() logic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! thanks :)

@@ -202,15 +202,20 @@ public function get_payload_hash( $payload = null ) {
* @return array|WP_Error The post payload or WP_Error if the post is invalid.
*/
public function get_payload( $status_on_create = 'draft' ) {
$post_author = $this->post->post_author ? Outgoing_Author::get_wp_user_for_distribution( $this->post->post_author ) : [];
$multiple_authors = apply_filters( 'newspack_network_multiple_authors_for_post', [], $this->post );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you share more about the use case for this filter? It's missing a docblock.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was an attempt to make it so other implementations of multiple authors (like other than the CAP plugin) could chime in. I'm missing an equivalent on incoming – I'll add that and add those in-function docblocks

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also added a filter for incoming "mulitple_authors", so any implementation could hook in in both directions.

@naxoc naxoc requested a review from miguelpeixe January 30, 2025 14:23
@naxoc
Copy link
Member Author

naxoc commented Jan 30, 2025

Thanks for the review @miguelpeixe

I implemented your suggestions. If you think the whole hooks thing for multiple authors is over engineering things, let me know and I can change them to function calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants