Make WordPress Core


Ignore:
Timestamp:
02/12/2024 10:15:45 PM (2 years ago)
Author:
swissspidy
Message:

REST API: Add featured_media field to attachments endpoint.

Audio and video attachments can have a featured image, also known as a poster image. This functionality is now properly exposed by the wp/v2/media endpoint.

Props swissspidy, timothyblynjacobs, wonderboymusic, dlh, spacedmonkey.
Fixes #41692.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r57524 r57603  
    172172        }
    173173
     174        if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) {
     175            $thumbnail_update = $this->handle_featured_media( $request['featured_media'], $attachment_id );
     176
     177            if ( is_wp_error( $thumbnail_update ) ) {
     178                return $thumbnail_update;
     179            }
     180        }
     181
    174182        if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
    175183            $meta_update = $this->meta->update_value( $request['meta'], $attachment_id );
     
    324332
    325333    /**
     334     * Determines the featured media based on a request param.
     335     *
     336     * @since 6.5.0
     337     *
     338     * @param int $featured_media Featured Media ID.
     339     * @param int $post_id        Post ID.
     340     * @return bool|WP_Error Whether the post thumbnail was successfully deleted, otherwise WP_Error.
     341     */
     342    protected function handle_featured_media( $featured_media, $post_id ) {
     343        $post_type         = get_post_type( $post_id );
     344        $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' );
     345
     346        // Similar check as in wp_insert_post().
     347        if ( ! $thumbnail_support && get_post_mime_type( $post_id ) ) {
     348            if ( wp_attachment_is( 'audio', $post_id ) ) {
     349                $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' );
     350            } elseif ( wp_attachment_is( 'video', $post_id ) ) {
     351                $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
     352            }
     353        }
     354
     355        if ( $thumbnail_support ) {
     356            return parent::handle_featured_media( $featured_media, $post_id );
     357        }
     358
     359        return new WP_Error(
     360            'rest_no_featured_media',
     361            sprintf(
     362                /* translators: %s: attachment mime type */
     363                __( 'This site does not support post thumbnails on attachments with MIME type %s.' ),
     364                get_post_mime_type( $post_id )
     365            ),
     366            array( 'status' => 400 )
     367        );
     368    }
     369
     370    /**
    326371     * Updates a single attachment.
    327372     *
     
    355400
    356401        $attachment = get_post( $request['id'] );
     402
     403        if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) {
     404            $thumbnail_update = $this->handle_featured_media( $request['featured_media'], $attachment->ID );
     405
     406            if ( is_wp_error( $thumbnail_update ) ) {
     407                return $thumbnail_update;
     408            }
     409        }
    357410
    358411        $fields_update = $this->update_additional_fields_for_object( $attachment, $request );
Note: See TracChangeset for help on using the changeset viewer.