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

add filter to DistributorPost::to_pull_list #1181

Merged
merged 1 commit into from
Jan 18, 2024
Merged

Conversation

leogermani
Copy link
Contributor

@leogermani leogermani commented Jan 17, 2024

Description of the Change

Adds a new hook to DistributorPost::to_pull_list method to allow third party plugins to add additional data to the post being pulled by another site.

This is a useful alternative to extend the post data when pulling posts. Apart from that, the only other way plugins can add data to posts is by writing data to the post meta table, which is not always convenient.

How to test the Change

In the site where the content is going to be pulled from, add a snippet to add custom data to the post:

add_filter( 'dt_post_to_pull', function( $post_data ) {
    $post_data['custom_field'] = 'custom_value';
} );

Now in the site where you are pulling the content from, add a snippet to inspect the contents of the pulled post and make sure the custom data is there:

add_action( 'dt_pull_post', function ( $new_post_id, $connection, $post_array ) {

    if ( isset( $post_array['custom_field']) && $post_array['custom_field'] == 'custom_value') {
        error_log( 'It worked!' );
    }

} );

Changelog Entry

Added - dt_post_to_pull filter that allows plugins to add custom data to a post being pulled by another site.

Credits

Props @leogermani

Checklist:

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests pass.

I suppose the documentation is automatically generated in this case. Doc block looks good.

Also suppose this does not require new tests.

@leogermani leogermani requested a review from a team as a code owner January 17, 2024 20:37
@leogermani leogermani requested review from faisal-alvi and removed request for a team January 17, 2024 20:37
@dkotter dkotter added this to the 2.0.3 milestone Jan 17, 2024
@peterwilsoncc
Copy link
Collaborator

@leogermani Thanks for the pull request.

Is the background to this request to ensure the custom distributor endpoints account for data added via the register_rest_field() function in WordPress?

I realise that pulling from the custom endpoint fails to consider that for both posts and terms so we may wish to include all such data in the distributor endpoint now we are using it to pull content.

@leogermani
Copy link
Contributor Author

@leogermani Thanks for the pull request.

Is the background to this request to ensure the custom distributor endpoints account for data added via the register_rest_field() function in WordPress?

I realise that pulling from the custom endpoint fails to consider that for both posts and terms so we may wish to include all such data in the distributor endpoint now we are using it to pull content.

Hi @peterwilsoncc,

No, not really.

It's just a way to be able to add custom data to a post that is being pulled. I realize I could have data sent if I added them as post meta, but that's not very convenient when you are running the plugin in a site with hundreds or thousands of existing posts. I want to be able to add data to the distribution on the fly, only when the post is distributed.

Another way to look at it is as this hook being the counterpart of dt_push_post_args and dt_subscription_post_args. Those hooks can be used to add data to pushed posts. I want to achieve the same thing, but for pulled posts.

@leogermani
Copy link
Contributor Author

Sharing some additional context:

Before 2.0, I was using core's rest_prepare_post and it took us way too long to realize that this integration broke in 2.0, since Distributor no longer pull posts via the core REST API endpoints, but uses the same endpoint to list posts to be pulled and to actually pull them.

@faisal-alvi faisal-alvi requested review from peterwilsoncc and removed request for faisal-alvi January 18, 2024 12:52
@dkotter
Copy link
Collaborator

dkotter commented Jan 18, 2024

I'm fine with including this new filter.

As @peterwilsoncc mentions, there may be a better way for us to handle this in Distributor that doesn't require others to utilize a filter. But for now, I think adding this is fine.

@dkotter dkotter merged commit f880331 into 10up:develop Jan 18, 2024
14 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants