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 an action to signal processing co-author has finished #1053

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

oekeur
Copy link

@oekeur oekeur commented Jun 14, 2024

Description

Adds a (partly) solution for #1024. Which we also encounter at our company.
We sync posts to an external database secondary database, so we need to know when all data has been updated.

Because CAP saves co-authors with CPTs and metadata, the authors are updated only after save_post has finished.
It's counter-intuitive from a user perspective but logical from the architecture perspective.

This PR adds an action to explicitly signal when CAP has finished processing, allowing it to hook into that, for example, when syncing to external systems.

Deploy Notes

Steps to Test

  • Add a minimal plugin that hooks into the save_post action and var_dump (or use a tool like Xdebug to use step debugging)
  • Observe that the save_post hook doesn't have up-to-date data when updating an author
  • Check out this PR
  • Observe the previous functionality is unchanged
  • Observe that a new hook is now present that fires after the co-author has been updated, allowing for a direct hook into that.
Minimal reproduction
<?php  
  
add_action('save_post', 'savePost', 10, 2 );  
  
function savePost ($post_id, $post) {  
    var_dump($post);  
    return true;}  
  
add_action('coauthors_post_updated', 'coAuthorUpdate', 10, 2 );  
  
function coAuthorUpdate ($post_id, $coauthor_objects) {  
    var_dump($coauthor_objects);  
    return true;}  
  
?>

@oekeur
Copy link
Author

oekeur commented Jul 31, 2024

I am not sure who I should tag but @GaryJones, what would you think? :)

@GaryJones
Copy link
Contributor

@leogermani
Copy link
Contributor

This can easily be solved by adding your hook to a later priority:

add_action('save_post', 'savePost', 20, 2 );  

I don't see a strong reason for a hook.

But I'm also not strongly against it. But in that case, the hook should live in the coauthors_update_post method, and not on the add_coatuhros method. add_coauthors is a generic command to add authors that can be used in many other contexts, like CLI for example. coauthors_update_post is the method that is hooked to save_post and what would make sense to have a finished action to address the issue you describe

@oekeur
Copy link
Author

oekeur commented Sep 5, 2024

I'm sorry for not getting back to you sooner.

Well, adding at a lower priority is always an option. Still, you're then never really sure if cap has run already. Adding a specific hook would allow you to hook into the action itself.

Moving to coauthors_update_post seems logical as well.

How about df1c289?

@leogermani
Copy link
Contributor

Hi @oekeur

Still, you're then never really sure if cap has run already.

Why not? You are. If the plugin is enabled you know that the CAP filter has already been fired. There's no doubt about that.

I'm struggling to see a good justification for this new hook.

All the methods used in that callback are public, like is_post_type_enabled and current_user_can_set_authors, so you are able to know exactly what happened there and act accordingly.

If there was something that could only checked in that context, and we wanted to run something conditionally, it would make sense to add a hook inside that condition.

But not only all the conditions are easily to check from outside of that context, you added the hook after the if and else .. so it will just run anyways... it's the exact same thing as having another hook at a later priority

@leogermani
Copy link
Contributor

Maybe another approach, that would satisfy the inconviniences you're having on #1024 is to increase the priority of the save_post hook in CAP here. We could reduce it to say 8.

Then whenever someone hooks into the default priority of save_post, CAP will already has done its thing

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.

3 participants