Make WordPress Core

Changeset 57603


Ignore:
Timestamp:
02/12/2024 10:15:45 PM (11 days 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.

Location:
trunk
Files:
5 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 );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r57230 r57603  
    23782378                'revisions',
    23792379                'custom-fields',
     2380                'thumbnail',
    23802381            ),
    23812382        );
  • trunk/tests/phpunit/includes/testcase-rest-post-type-controller.php

    r56549 r57603  
    7878        }
    7979
    80         if ( post_type_supports( $post->post_type, 'thumbnail' ) ) {
     80        if (
     81            post_type_supports( $post->post_type, 'thumbnail' ) ||
     82            (
     83                'attachment' === $post->post_type &&
     84                (
     85                    post_type_supports( 'attachment:audio', 'thumbnail' ) ||
     86                    post_type_supports( 'attachment:video', 'thumbnail' )
     87                )
     88            )
     89        ) {
    8190            $this->assertSame( (int) get_post_thumbnail_id( $post->ID ), $data['featured_media'] );
    8291        } else {
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r57380 r57603  
    10661066        $term = wp_get_post_terms( $attachment['id'], 'category' );
    10671067        $this->assertSame( $category['term_id'], $term[0]->term_id );
     1068    }
     1069
     1070    /**
     1071     * @ticket 41692
     1072     */
     1073    public function test_create_update_post_with_featured_media() {
     1074        // Add support for thumbnails on all attachment types to avoid incorrect-usage notice.
     1075        add_post_type_support( 'attachment', 'thumbnail' );
     1076
     1077        wp_set_current_user( self::$editor_id );
     1078
     1079        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     1080        $request->set_file_params(
     1081            array(
     1082                'file' => array(
     1083                    'file'     => file_get_contents( self::$test_file ),
     1084                    'name'     => 'canola.jpg',
     1085                    'size'     => filesize( self::$test_file ),
     1086                    'tmp_name' => self::$test_file,
     1087                ),
     1088            )
     1089        );
     1090        $request->set_header( 'Content-MD5', md5_file( self::$test_file ) );
     1091
     1092        $file          = DIR_TESTDATA . '/images/canola.jpg';
     1093        $attachment_id = self::factory()->attachment->create_object(
     1094            $file,
     1095            0,
     1096            array(
     1097                'post_mime_type' => 'image/jpeg',
     1098                'menu_order'     => rand( 1, 100 ),
     1099            )
     1100        );
     1101
     1102        $request->set_param( 'featured_media', $attachment_id );
     1103
     1104        $response = rest_get_server()->dispatch( $request );
     1105        $data     = $response->get_data();
     1106
     1107        $this->assertEquals( 201, $response->get_status() );
     1108
     1109        $new_attachment = get_post( $data['id'] );
     1110
     1111        $this->assertEquals( $attachment_id, (int) get_post_thumbnail_id( $new_attachment->ID ) );
     1112        $this->assertEquals( $attachment_id, $data['featured_media'] );
     1113
     1114        $request = new WP_REST_Request( 'PUT', '/wp/v2/media/' . $new_attachment->ID );
     1115        $params  = $this->set_post_data(
     1116            array(
     1117                'featured_media' => 0,
     1118            )
     1119        );
     1120        $request->set_body_params( $params );
     1121        $response = rest_get_server()->dispatch( $request );
     1122        $this->assertEquals( 200, $response->get_status() );
     1123        $data = $response->get_data();
     1124        $this->assertEquals( 0, $data['featured_media'] );
     1125        $this->assertEquals( 0, (int) get_post_thumbnail_id( $new_attachment->ID ) );
     1126
     1127        $request = new WP_REST_Request( 'PUT', '/wp/v2/media/' . $new_attachment->ID );
     1128        $params  = $this->set_post_data(
     1129            array(
     1130                'featured_media' => $attachment_id,
     1131            )
     1132        );
     1133        $request->set_body_params( $params );
     1134        $response = rest_get_server()->dispatch( $request );
     1135        $this->assertEquals( 200, $response->get_status() );
     1136        $data = $response->get_data();
     1137        $this->assertEquals( $attachment_id, $data['featured_media'] );
     1138        $this->assertEquals( $attachment_id, (int) get_post_thumbnail_id( $new_attachment->ID ) );
    10681139    }
    10691140
     
    15771648        $data       = $response->get_data();
    15781649        $properties = $data['schema']['properties'];
    1579         $this->assertCount( 27, $properties );
     1650        $this->assertCount( 28, $properties );
    15801651        $this->assertArrayHasKey( 'author', $properties );
    15811652        $this->assertArrayHasKey( 'alt_text', $properties );
     
    16111682        $this->assertArrayHasKey( 'type', $properties );
    16121683        $this->assertArrayHasKey( 'missing_image_sizes', $properties );
     1684        $this->assertArrayHasKey( 'featured_media', $properties );
    16131685    }
    16141686
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r57548 r57603  
    73167316                            "required": false
    73177317                        },
     7318                        "featured_media": {
     7319                            "description": "The ID of the featured media for the post.",
     7320                            "type": "integer",
     7321                            "required": false
     7322                        },
    73187323                        "comment_status": {
    73197324                            "description": "Whether or not comments are open on the post.",
     
    75157520                        "author": {
    75167521                            "description": "The ID for the author of the post.",
     7522                            "type": "integer",
     7523                            "required": false
     7524                        },
     7525                        "featured_media": {
     7526                            "description": "The ID of the featured media for the post.",
    75177527                            "type": "integer",
    75187528                            "required": false
     
    1269412704        },
    1269512705        "author": 0,
     12706        "featured_media": 0,
    1269612707        "comment_status": "open",
    1269712708        "ping_status": "closed",
     
    1275512766    },
    1275612767    "author": 0,
     12768    "featured_media": 0,
    1275712769    "comment_status": "open",
    1275812770    "ping_status": "closed",
Note: See TracChangeset for help on using the changeset viewer.