#42864 closed defect (bug) (fixed)
REST API: Fire an action after items are completely inserted/updated
Reported by: | pento | Owned by: | timothyblynjacobs |
---|---|---|---|
Milestone: | 5.0 | Priority: | normal |
Severity: | normal | Version: | |
Component: | REST API | Keywords: | has-patch fixed-5.0 |
Focuses: | rest-api | Cc: |
Description
In wp_insert_post()
and wp_insert_comment()
, the corresponding wp_insert_post
and wp_insert_comment
actions are run at the very end, after all meta and additional fields are inserted or update.
The equivalent REST API rest_insert_*
actions are fired before the meta and additional fields are updated - there isn't an action to hook into, which is triggered after.
I expect this to become more of an issue with Gutenberg, as the behaviour of the existing post editor is that the post and related data have been completely updated by the time wp_insert_post
fires.
Related: Gutenberg#3898, #38905.
Attachments (2)
Change History (22)
#2
@
7 years ago
I thought these were also inconsistent, but as it turns out, I think r39288 helped make them consistent, although in the process we lost a crucial ability. We should introduce common hooks across all of these for a final action:
rest_insert_attachment
is after attachment creation/update, but before meta + additional fieldsrest_insert_comment
is after comment creation/update, but before meta + additional fieldsrest_insert_{post}
is after post creation/update, but before meta + additional fields + sticky + terms + featured media ({post}
is the post type).rest_insert_{term}
is after term creation/update, but before meta + additional fields ({term}
is the taxonomy)rest_insert_user
is after user creation/update, but before roles + meta + additional fields
These actions are still useful if you want to do something when the object is updated, but not as useful as they could be.
I'm thinking a rest_did_insert_{type}
for all of these across the board, called with the same parameters, but fired at the end of the relevant method rather than after the object change.
This ticket was mentioned in Slack in #core-restapi by danielbachhuber. View the logs.
6 years ago
#6
@
6 years ago
- Owner set to timothyblynjacobs
@timothyblynjacobs said he'd pick this up at today's #core-restapi
chat.
#7
@
6 years ago
- Keywords has-patch added
Added a patch. I went with rest_after_*
because it seemed more inline with other hooks like wp_delete_post()
which fires after_delete_post
at the end. The other style I saw some examples of was something like rest_inserted_*
Thoughts?
When working on the attachments hook, I saw that the rest_insert_attachment
hook passes the request data as an array for the attachment instead of the actual WP_Post
object. Is it too late to fix that? Should it be a separate ticket?
#8
@
6 years ago
Patch looks pretty good, thanks @TimothyBlynJacobs.
As far as naming goes, I think rest_after_*
is fine.
For the sake of precision, should the hooks be called after $request->set_param( 'context', 'edit' );
instead of before? This would ensure that the context
value is always consistent.
When working on the attachments hook, I saw that the
rest_insert_attachment
hook passes the request data as an array for the attachment instead of the actualWP_Post
object. Is it too late to fix that? Should it be a separate ticket?
Probably too late :/ It would be a separate ticket though.
This ticket was mentioned in Slack in #core-restapi by desrosj. View the logs.
6 years ago
#12
@
6 years ago
- Keywords fixed-5.0 added
- Resolution fixed deleted
- Status changed from closed to reopened
Reopening for merge to trunk.
One side affect of this behavior is that if you're trying to support post meta in revisions, there is no hook that fires at the end of the process, meaning meta gets out of sync by one revision each time a new post + meta is saved.
Relevant code is in
WP_REST_Posts_Controller::update_item()
and it may be relevant that therest_insert_*
hooks were moved to higher up in the method in r39288.