mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-03-30 18:24:31 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@57603 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -77,7 +77,16 @@ abstract class WP_Test_REST_Post_Type_Controller_Testcase extends WP_Test_REST_C
|
||||
$this->assertSame( get_page_template_slug( $post->ID ), $data['template'] );
|
||||
}
|
||||
|
||||
if ( post_type_supports( $post->post_type, 'thumbnail' ) ) {
|
||||
if (
|
||||
post_type_supports( $post->post_type, 'thumbnail' ) ||
|
||||
(
|
||||
'attachment' === $post->post_type &&
|
||||
(
|
||||
post_type_supports( 'attachment:audio', 'thumbnail' ) ||
|
||||
post_type_supports( 'attachment:video', 'thumbnail' )
|
||||
)
|
||||
)
|
||||
) {
|
||||
$this->assertSame( (int) get_post_thumbnail_id( $post->ID ), $data['featured_media'] );
|
||||
} else {
|
||||
$this->assertArrayNotHasKey( 'featured_media', $data );
|
||||
|
||||
@@ -1067,6 +1067,77 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$this->assertSame( $category['term_id'], $term[0]->term_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 41692
|
||||
*/
|
||||
public function test_create_update_post_with_featured_media() {
|
||||
// Add support for thumbnails on all attachment types to avoid incorrect-usage notice.
|
||||
add_post_type_support( 'attachment', 'thumbnail' );
|
||||
|
||||
wp_set_current_user( self::$editor_id );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
|
||||
$request->set_file_params(
|
||||
array(
|
||||
'file' => array(
|
||||
'file' => file_get_contents( self::$test_file ),
|
||||
'name' => 'canola.jpg',
|
||||
'size' => filesize( self::$test_file ),
|
||||
'tmp_name' => self::$test_file,
|
||||
),
|
||||
)
|
||||
);
|
||||
$request->set_header( 'Content-MD5', md5_file( self::$test_file ) );
|
||||
|
||||
$file = DIR_TESTDATA . '/images/canola.jpg';
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
'menu_order' => rand( 1, 100 ),
|
||||
)
|
||||
);
|
||||
|
||||
$request->set_param( 'featured_media', $attachment_id );
|
||||
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
|
||||
$new_attachment = get_post( $data['id'] );
|
||||
|
||||
$this->assertEquals( $attachment_id, (int) get_post_thumbnail_id( $new_attachment->ID ) );
|
||||
$this->assertEquals( $attachment_id, $data['featured_media'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wp/v2/media/' . $new_attachment->ID );
|
||||
$params = $this->set_post_data(
|
||||
array(
|
||||
'featured_media' => 0,
|
||||
)
|
||||
);
|
||||
$request->set_body_params( $params );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 0, $data['featured_media'] );
|
||||
$this->assertEquals( 0, (int) get_post_thumbnail_id( $new_attachment->ID ) );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wp/v2/media/' . $new_attachment->ID );
|
||||
$params = $this->set_post_data(
|
||||
array(
|
||||
'featured_media' => $attachment_id,
|
||||
)
|
||||
);
|
||||
$request->set_body_params( $params );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( $attachment_id, $data['featured_media'] );
|
||||
$this->assertEquals( $attachment_id, (int) get_post_thumbnail_id( $new_attachment->ID ) );
|
||||
}
|
||||
|
||||
public function test_update_item() {
|
||||
wp_set_current_user( self::$editor_id );
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
@@ -1576,7 +1647,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertCount( 27, $properties );
|
||||
$this->assertCount( 28, $properties );
|
||||
$this->assertArrayHasKey( 'author', $properties );
|
||||
$this->assertArrayHasKey( 'alt_text', $properties );
|
||||
$this->assertArrayHasKey( 'caption', $properties );
|
||||
@@ -1610,6 +1681,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$this->assertArrayHasKey( 'rendered', $properties['title']['properties'] );
|
||||
$this->assertArrayHasKey( 'type', $properties );
|
||||
$this->assertArrayHasKey( 'missing_image_sizes', $properties );
|
||||
$this->assertArrayHasKey( 'featured_media', $properties );
|
||||
}
|
||||
|
||||
public function test_get_additional_field_registration() {
|
||||
|
||||
Reference in New Issue
Block a user