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/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
Note: See TracChangeset for help on using the changeset viewer.