[wp-trac] [WordPress Trac] #45114: Fire publishing related hooks after all data is saved via the REST API.

WordPress Trac noreply at wordpress.org
Fri Aug 14 21:36:24 UTC 2020


#45114: Fire publishing related hooks after all data is saved via the REST API.
---------------------------+------------------------------
 Reporter:  peterwilsoncc  |       Owner:  (none)
     Type:  defect (bug)   |      Status:  reopened
 Priority:  normal         |   Milestone:  Awaiting Review
Component:  REST API       |     Version:
 Severity:  normal         |  Resolution:
 Keywords:  dev-feedback   |     Focuses:
---------------------------+------------------------------

Comment (by TimothyBlynJacobs):

 Another thing is that if someone is updating things on the
 `rest_insert_post` or `rest_after_insert_post` hooks. Then `save_post`
 won't be accurate I don't think.

 > It was introduced as an accidental back-compat break with the REST API
 but now the new editor has been in core for a while, I'm wondering if
 doing this now would also break back-compat on a bunch of sites and
 plugins that have worked around it.

 I definitely think this is a risk. And to me it seems conceptually simpler
 that for block editor post types, use `rest_after_insert` and for others
 use `save_post` and call it a day.

 > I haven't been able to find an approach that doesn't involve firing the
 after post updates in a seperate function.

 Yeah I'm having a hard time seeing a solution other than this. But it also
 feels kinda fragile.

 ----

 I wonder if there is a more resilient way of doing these kinds of checks
 that rely on `save_post` in the first place? Because while the REST API is
 the primary way people are having this issue, it would happen with any
 code that does a `wp_insert_post()` and then updates meta fields or uses
 `wp_set_post_terms` afterwords, which is pretty common for plugins to do.

 My instinct is to write it something like this:

 {{{#!php
 <?php
 $queue = [];

 add_action( 'save_post', function ( $post_id ) use ( &$queue ) {
         $queue[] = $post_id;
 } );

 add_action( 'shutdown', function () use ( $queue ) {
         foreach ( $queue as $post_id ) {
                 // Get the latest copy of the post.
                 $post = get_post( $post_id );
         }
 } );
 }}}

 I think that'd work for both the REST API, the Classic Editor, or any code
 that creates post.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/45114#comment:27>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list