-
Notifications
You must be signed in to change notification settings - Fork 3.3k
REST API: Add finalize endpoint to WP_REST_Attachments_Controller #11168
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
77cc071
672fd49
3a82b96
4de3c1d
a136469
ba6d05d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -103,6 +103,26 @@ public function register_routes() { | |||||||
| 'schema' => array( $this, 'get_public_item_schema' ), | ||||||||
| ) | ||||||||
| ); | ||||||||
|
|
||||||||
| register_rest_route( | ||||||||
| $this->namespace, | ||||||||
| '/' . $this->rest_base . '/(?P<id>[\d]+)/finalize', | ||||||||
| array( | ||||||||
| array( | ||||||||
| 'methods' => WP_REST_Server::CREATABLE, | ||||||||
| 'callback' => array( $this, 'finalize_item' ), | ||||||||
| 'permission_callback' => array( $this, 'finalize_item_permissions_check' ), | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shell we use
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like the wrapper approach is pretty standard here? or maybe I'm not understanding your suggestion completely?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes but wordpress-develop/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php Lines 1987 to 1989 in ba6d05d
Can we use single method for both? |
||||||||
| 'args' => array( | ||||||||
| 'id' => array( | ||||||||
| 'description' => __( 'Unique identifier for the attachment.' ), | ||||||||
| 'type' => 'integer', | ||||||||
| ), | ||||||||
| ), | ||||||||
| ), | ||||||||
| 'allow_batch' => $this->allow_batch, | ||||||||
| 'schema' => array( $this, 'get_public_item_schema' ), | ||||||||
| ) | ||||||||
| ); | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -2176,4 +2196,60 @@ private static function filter_wp_unique_filename( $filename, $dir, $number, $at | |||||||
|
|
||||||||
| return $filename; | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Checks if a given request has access to finalize an attachment. | ||||||||
| * | ||||||||
| * @since 7.0.0 | ||||||||
| * | ||||||||
| * @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( WP_REST_Request $request ) { | ||||||||
| return $this->edit_media_item_permissions_check( $request ); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Finalizes an attachment after client-side media processing. | ||||||||
| * | ||||||||
| * Triggers the 'wp_generate_attachment_metadata' filter so that | ||||||||
| * server-side plugins can process the attachment after all client-side | ||||||||
| * operations (upload, thumbnail generation, sideloads) are complete. | ||||||||
| * | ||||||||
| * @since 7.0.0 | ||||||||
| * | ||||||||
| * @param WP_REST_Request $request Full details about the request. | ||||||||
| * @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure. | ||||||||
| */ | ||||||||
| public function finalize_item( WP_REST_Request $request ) { | ||||||||
| $attachment_id = $request['id']; | ||||||||
|
|
||||||||
| $post = $this->get_post( $attachment_id ); | ||||||||
| if ( is_wp_error( $post ) ) { | ||||||||
| return $post; | ||||||||
| } | ||||||||
|
|
||||||||
| $metadata = wp_get_attachment_metadata( $attachment_id ); | ||||||||
| if ( ! is_array( $metadata ) ) { | ||||||||
| $metadata = array(); | ||||||||
| } | ||||||||
|
|
||||||||
| /** 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 ); | ||||||||
adamsilverstein marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
|
||||||||
| $response_request = new WP_REST_Request( | ||||||||
| WP_REST_Server::READABLE, | ||||||||
| rest_get_route_for_post( $attachment_id ) | ||||||||
| ); | ||||||||
|
|
||||||||
| $response_request['context'] = 'edit'; | ||||||||
|
|
||||||||
| if ( isset( $request['_fields'] ) ) { | ||||||||
| $response_request['_fields'] = $request['_fields']; | ||||||||
| } | ||||||||
|
|
||||||||
| return $this->prepare_item_for_response( $post, $response_request ); | ||||||||
| } | ||||||||
| } | ||||||||
Uh oh!
There was an error while loading. Please reload this page.