#45269 closed defect (bug) (fixed)
REST API: `rest_insert_attachment` and `rest_after_insert_attachment` actions fire twice on update.
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 5.0 | Priority: | normal |
Severity: | normal | Version: | 5.0 |
Component: | REST API | Keywords: | has-patch has-unit-tests |
Focuses: | rest-api | Cc: |
Description
As \WP_REST_Attachments_Controller::update_item()
calls \WP_REST_Posts_Controller::update_item()
, this causes the rest_insert_attachment
and rest_after_insert_attachment
hooks to be fired twice on update requests, once in each controller class.
Creating as item isn't affected as the attachment controller does not call the parent class.
rest_after_insert_attachment
is new for version 5.0
rest_insert_attachment
is from a previous version of WP
Attachments (2)
Change History (12)
#3
@
6 years ago
- Owner set to danielbachhuber
- Resolution set to fixed
- Status changed from new to closed
In 43862:
#4
@
6 years ago
Thanks for catching this, @peterwilsoncc. Leaving the ticket closed; this can be backported with #42864.
#5
@
6 years ago
I had some late shower thoughts on this, this-morning. Is it worth allowing the if statement to be filterable to allow for custom endpoints making use of a similar pattern to the attachments?
<?php $subclass_endpoints = apply_filters( 'rest_api_subclass_endpoints', array( 'attachment' ) ); if ( in_array( $this->post_type, $subclass_endpoints, true ) ) { // etc
#6
@
6 years ago
@peterwilsoncc Hm, good question. It exposes the problem that if you're providing an endpoint implementation that subclasses one of the core controllers, then we run into the execution order problem again.
With this being said, I'm not sure how much subclassing is the formally supported extensibility pattern. I think it'd be much more common for developers to either use register_rest_field()
, or write completely custom controllers.
At this point, I don't have a strong feeling on the appropriate path forward. I'd love to know more about how serious of a real-world problem this is, vs. simply a theoretical concern.
More specifically, in the Jetpack and Yoast examples outlined in https://core.trac.wordpress.org/ticket/45114#comment:7, it would be really valuable to know:
- What is the specific bug that the issue causes.
- What's the level of effort and complexity to implement an alternative workaround.
With this information, we can better assess the severity of the issue.
In 45269.2.diff:
test_rest_insert_attachment_hooks_fire_once_on_create
test.$response = $this->prepare_item_for_response( $post, $request );
by duplicating the call insideif ( 'attachment' === $this->post_type ) {
.rest_after_insert_
action is fired.