REST API: Add finalize endpoint to WP_REST_Attachments_Controller#11168
REST API: Add finalize endpoint to WP_REST_Attachments_Controller#11168adamsilverstein wants to merge 1 commit intoWordPress:trunkfrom
Conversation
Add a `POST /wp/v2/media/{id}/finalize` endpoint that triggers the
`wp_generate_attachment_metadata` filter after client-side media
processing completes. This ensures server-side plugins (e.g., for
watermarking, CDN sync, custom sizes) can post-process attachments
when client-side processing is active.
The endpoint:
- Reuses `edit_media_item_permissions_check` for authorization
- Re-applies `wp_generate_attachment_metadata` with context 'update'
- Updates attachment metadata and returns the updated attachment
- Is only registered when client-side media processing is enabled
Props adamsilverstein.
See #62243.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
|
||
| register_rest_route( | ||
| $this->namespace, | ||
| '/' . $this->rest_base . '/(?P<id>[\d]+)/finalize', |
There was a problem hiding this comment.
The brackets aren't necessary.
| '/' . $this->rest_base . '/(?P<id>[\d]+)/finalize', | |
| '/' . $this->rest_base . '/(?P<id>\d+)/finalize', |
There was a problem hiding this comment.
But I guess the other endpoints have the similar pattern.
| * @param WP_REST_Request $request Full details about the request. | ||
| * @return true|WP_Error True if the request has access, WP_Error object otherwise. | ||
| */ | ||
| public function finalize_item_permissions_check( $request ) { |
There was a problem hiding this comment.
| public function finalize_item_permissions_check( $request ) { | |
| public function finalize_item_permissions_check( WP_REST_Request $request ) { |
|
|
||
| return $this->prepare_item_for_response( get_post( $attachment_id ), $response_request ); | ||
| } | ||
|
|
| $response_request['_fields'] = $request['_fields']; | ||
| } | ||
|
|
||
| return $this->prepare_item_for_response( get_post( $attachment_id ), $response_request ); |
There was a problem hiding this comment.
Can this re-use the $post which was obtained above?
| return $this->prepare_item_for_response( get_post( $attachment_id ), $response_request ); | |
| return $this->prepare_item_for_response( $post, $response_request ); |
|
|
||
| $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 ); | ||
| } | ||
|
|
| $attachment_id = $request['id']; | ||
|
|
||
| $post = $this->get_post( $attachment_id ); | ||
|
|
| } | ||
|
|
||
| $metadata = wp_get_attachment_metadata( $attachment_id ); | ||
|
|
| /** This filter is documented in wp-admin/includes/image.php */ | ||
| $metadata = apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id, 'update' ); | ||
|
|
||
| wp_update_attachment_metadata( $attachment_id, $metadata ); |
There was a problem hiding this comment.
I guess it doesn't make sense to check the return value here for failure since it returns false if the updated post meta is the same.
| array( | ||
| 'methods' => WP_REST_Server::CREATABLE, | ||
| 'callback' => array( $this, 'finalize_item' ), | ||
| 'permission_callback' => array( $this, 'finalize_item_permissions_check' ), |
There was a problem hiding this comment.
Shell we use edit_media_item_permissions_check directly as the new method class that function and return the value?
Summary
POST /wp/v2/media/{id}/finalizeREST API endpoint toWP_REST_Attachments_Controllerwp_generate_attachment_metadatafilter with context'update'after client-side media processing completeswp_is_client_side_media_processing_enabled()returns trueContext
When client-side media processing handles image uploads (resizing, thumbnail generation, format conversion), server-side WordPress hooks like
wp_generate_attachment_metadataare bypassed. This endpoint is called after all client-side operations (upload, thumbnail generation, sideloads) are complete, re-triggering the filter so plugins continue to work.Hook usage example
Changes
class-wp-rest-attachments-controller.phpfinalizeroute registration,finalize_item_permissions_check(), andfinalize_item()methodsrest-attachments-controller.php(tests)rest-schema-setup.phpwp-api-generated.jsTest plan
wp_generate_attachment_metadatafilter fires with context'update'Related
Trac ticket: https://core.trac.wordpress.org/ticket/64804