diff options
Diffstat (limited to 'wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php')
-rw-r--r-- | wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php | 65 |
1 files changed, 62 insertions, 3 deletions
diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 7367c0f..804d22a 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -171,6 +171,14 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { update_post_meta( $attachment_id, '_wp_attachment_image_alt', sanitize_text_field( $request['alt_text'] ) ); } + if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) { + $thumbnail_update = $this->handle_featured_media( $request['featured_media'], $attachment_id ); + + if ( is_wp_error( $thumbnail_update ) ) { + return $thumbnail_update; + } + } + if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { $meta_update = $this->meta->update_value( $request['meta'], $attachment_id ); @@ -186,6 +194,12 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { return $fields_update; } + $terms_update = $this->handle_terms( $attachment_id, $request ); + + if ( is_wp_error( $terms_update ) ) { + return $terms_update; + } + $request->set_param( 'context', 'edit' ); /** @@ -201,7 +215,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { wp_after_insert_post( $attachment, false, null ); - if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { + if ( wp_is_serving_rest_request() ) { /* * Set a custom header with the attachment_id. * Used by the browser/client to resume creating image sub-sizes after a PHP fatal error. @@ -317,6 +331,43 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { } /** + * Determines the featured media based on a request param. + * + * @since 6.5.0 + * + * @param int $featured_media Featured Media ID. + * @param int $post_id Post ID. + * @return bool|WP_Error Whether the post thumbnail was successfully deleted, otherwise WP_Error. + */ + protected function handle_featured_media( $featured_media, $post_id ) { + $post_type = get_post_type( $post_id ); + $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ); + + // Similar check as in wp_insert_post(). + if ( ! $thumbnail_support && get_post_mime_type( $post_id ) ) { + if ( wp_attachment_is( 'audio', $post_id ) ) { + $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); + } elseif ( wp_attachment_is( 'video', $post_id ) ) { + $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); + } + } + + if ( $thumbnail_support ) { + return parent::handle_featured_media( $featured_media, $post_id ); + } + + return new WP_Error( + 'rest_no_featured_media', + sprintf( + /* translators: %s: attachment mime type */ + __( 'This site does not support post thumbnails on attachments with MIME type %s.' ), + get_post_mime_type( $post_id ) + ), + array( 'status' => 400 ) + ); + } + + /** * Updates a single attachment. * * @since 4.7.0 @@ -349,6 +400,14 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { $attachment = get_post( $request['id'] ); + if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) { + $thumbnail_update = $this->handle_featured_media( $request['featured_media'], $attachment->ID ); + + if ( is_wp_error( $thumbnail_update ) ) { + return $thumbnail_update; + } + } + $fields_update = $this->update_additional_fields_for_object( $attachment, $request ); if ( is_wp_error( $fields_update ) ) { @@ -450,7 +509,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { ); } - $supported_types = array( 'image/jpeg', 'image/png', 'image/gif', 'image/webp' ); + $supported_types = array( 'image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif' ); $mime_type = get_post_mime_type( $attachment_id ); if ( ! in_array( $mime_type, $supported_types, true ) ) { return new WP_Error( @@ -630,7 +689,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { update_post_meta( $new_attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) ); } - if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { + if ( wp_is_serving_rest_request() ) { /* * Set a custom header with the attachment_id. * Used by the browser/client to resume creating image sub-sizes after a PHP fatal error. |