Make WordPress Core

Ticket #41692: 41692.2.diff

File 41692.2.diff, 6.9 KB (added by TimothyBlynJacobs, 6 years ago)
  • src/wp-includes/post.php

    diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
    index e52ae57..fb17cc4 100644
    a b function create_initial_post_types() { 
    8282                'query_var' => false,
    8383                'show_in_nav_menus' => false,
    8484                'delete_with_user' => true,
    85                 'supports' => array( 'title', 'author', 'comments' ),
     85                'supports' => array( 'title', 'author', 'comments', 'thumbnail' ),
    8686                'show_in_rest' => true,
    8787                'rest_base' => 'media',
    8888                'rest_controller_class' => 'WP_REST_Attachments_Controller',
  • src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
    index 76802fe..d26545c 100644
    a b class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { 
    178178                        update_post_meta( $id, '_wp_attachment_image_alt', sanitize_text_field( $request['alt_text'] ) );
    179179                }
    180180
     181                if ( isset( $request['featured_media'] ) ) {
     182                        $this->handle_featured_media( $request['featured_media'], $id );
     183                }
     184
    181185                $fields_update = $this->update_additional_fields_for_object( $attachment, $request );
    182186
    183187                if ( is_wp_error( $fields_update ) ) {
  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
    index 018b66e..31c7d66 100644
    a b class WP_REST_Posts_Controller extends WP_REST_Controller { 
    18281828                                'comments',
    18291829                                'revisions',
    18301830                                'custom-fields',
     1831                                'thumbnail',
    18311832                        ),
    18321833                );
    18331834                foreach ( $post_type_attributes as $attribute ) {
  • tests/phpunit/tests/rest-api/rest-attachments-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php
    index 98a079b..1d79a7a 100644
    a b class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 
    697697                $this->assertEquals( '', $attachment['alt_text'] );
    698698        }
    699699
     700        public function test_create_update_post_with_featured_media() {
     701
     702                wp_set_current_user( self::$editor_id );
     703
     704                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     705                $request->set_file_params( array(
     706                        'file' => array(
     707                                'file'     => file_get_contents( $this->test_file ),
     708                                'name'     => 'canola.jpg',
     709                                'size'     => filesize( $this->test_file ),
     710                                'tmp_name' => $this->test_file,
     711                        ),
     712                ) );
     713                $request->set_header( 'Content-MD5', md5_file( $this->test_file ) );
     714
     715                $file = DIR_TESTDATA . '/images/canola.jpg';
     716                $this->attachment_id = $this->factory->attachment->create_object( $file, 0, array(
     717                        'post_mime_type' => 'image/jpeg',
     718                        'menu_order' => rand( 1, 100 ),
     719                ) );
     720
     721                $request->set_param( 'featured_media', $this->attachment_id );
     722
     723                $response = $this->server->dispatch( $request );
     724                $data = $response->get_data();
     725
     726                $this->assertEquals( 201, $response->get_status() );
     727
     728                $new_attachment = get_post( $data['id'] );
     729
     730                $this->assertEquals( $this->attachment_id, (int) get_post_thumbnail_id( $new_attachment->ID ) );
     731                $this->assertEquals( $this->attachment_id, $data['featured_media'] );
     732
     733                $request = new WP_REST_Request( 'PUT', '/wp/v2/media/' . $new_attachment->ID );
     734                $params = $this->set_post_data( array(
     735                        'featured_media' => 0,
     736                ) );
     737                $request->set_body_params( $params );
     738                $response = $this->server->dispatch( $request );
     739                $this->assertEquals( 200, $response->get_status() );
     740                $data = $response->get_data();
     741                $this->assertEquals( 0, $data['featured_media'] );
     742                $this->assertEquals( 0, (int) get_post_thumbnail_id( $new_attachment->ID ) );
     743        }
     744
    700745        public function test_update_item() {
    701746                wp_set_current_user( self::$editor_id );
    702747                $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
    class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 
    10791124                $response = $this->server->dispatch( $request );
    10801125                $data = $response->get_data();
    10811126                $properties = $data['schema']['properties'];
    1082                 $this->assertEquals( 24, count( $properties ) );
     1127                $this->assertEquals( 25, count( $properties ) );
    10831128                $this->assertArrayHasKey( 'author', $properties );
    10841129                $this->assertArrayHasKey( 'alt_text', $properties );
    10851130                $this->assertArrayHasKey( 'caption', $properties );
    class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 
    11101155                $this->assertArrayHasKey( 'raw', $properties['title']['properties'] );
    11111156                $this->assertArrayHasKey( 'rendered', $properties['title']['properties'] );
    11121157                $this->assertArrayHasKey( 'type', $properties );
     1158                $this->assertArrayHasKey( 'featured_media', $properties );
    11131159        }
    11141160
    11151161        public function test_get_additional_field_registration() {
  • tests/qunit/fixtures/wp-api-generated.js

    diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js
    index 72bcc5e..c0911dc 100644
    a b mockedApiResponse.Schema = { 
    15491549                            "description": "The ID for the author of the object.",
    15501550                            "type": "integer"
    15511551                        },
     1552                        "featured_media": {
     1553                            "required": false,
     1554                            "description": "The ID of the featured media for the object.",
     1555                            "type": "integer"
     1556                        },
    15521557                        "comment_status": {
    15531558                            "required": false,
    15541559                            "enum": [
    mockedApiResponse.Schema = { 
    16891694                            "description": "The ID for the author of the object.",
    16901695                            "type": "integer"
    16911696                        },
     1697                        "featured_media": {
     1698                            "required": false,
     1699                            "description": "The ID of the featured media for the object.",
     1700                            "type": "integer"
     1701                        },
    16921702                        "comment_status": {
    16931703                            "required": false,
    16941704                            "enum": [
    mockedApiResponse.MediaCollection = [ 
    39043914            "rendered": "REST API Client Fixture: Attachment"
    39053915        },
    39063916        "author": 0,
     3917        "featured_media": 0,
    39073918        "comment_status": "open",
    39083919        "ping_status": "closed",
    39093920        "template": "",
    mockedApiResponse.MediaModel = { 
    39653976        "rendered": "REST API Client Fixture: Attachment"
    39663977    },
    39673978    "author": 0,
     3979    "featured_media": 0,
    39683980    "comment_status": "open",
    39693981    "ping_status": "closed",
    39703982    "template": "",