Make WordPress Core

Ticket #38679: 38679.diff

File 38679.diff, 10.1 KB (added by jnylen0, 9 years ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
    index 9d8e7d4..bcbd524 100644
    class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { 
    247247        protected function prepare_item_for_database( $request ) {
    248248                $prepared_attachment = parent::prepare_item_for_database( $request );
    249249
     250                // Attachment caption (post_excerpt internally)
    250251                if ( isset( $request['caption'] ) ) {
    251                         $prepared_attachment->post_excerpt = $request['caption'];
     252                        if ( is_string( $request['caption'] ) ) {
     253                                $prepared_attachment->post_excerpt = wp_filter_post_kses( $request['caption'] );
     254                        } elseif ( isset( $request['caption']['raw'] ) ) {
     255                                $prepared_attachment->post_excerpt = wp_filter_post_kses( $request['caption']['raw'] );
     256                        }
    252257                }
    253258
     259                // Attachment description (post_content internally)
    254260                if ( isset( $request['description'] ) ) {
    255                         $prepared_attachment->post_content = $request['description'];
     261                        if ( is_string( $request['description'] ) ) {
     262                                $prepared_attachment->post_content = wp_filter_post_kses( $request['description'] );
     263                        } elseif ( isset( $request['description']['raw'] ) ) {
     264                                $prepared_attachment->post_content = wp_filter_post_kses( $request['description']['raw'] );
     265                        }
    256266                }
    257267
    258268                if ( isset( $request['post'] ) ) {
    class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { 
    276286                $response = parent::prepare_item_for_response( $post, $request );
    277287                $data = $response->get_data();
    278288
     289                $data['description'] = array(
     290                        'raw'       => $post->post_content,
     291                        /** This filter is documented in wp-includes/post-template.php */
     292                        'rendered'  => apply_filters( 'the_content', $post->post_content ),
     293                );
     294
     295                /** This filter is documented in wp-includes/post-template.php */
     296                $caption = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) );
     297                $data['caption'] = array(
     298                        'raw'       => $post->post_excerpt,
     299                        'rendered'  => $caption,
     300                );
     301
    279302                $data['alt_text']      = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
    280                 $data['caption']       = $post->post_excerpt;
    281                 $data['description']   = $post->post_content;
    282303                $data['media_type']    = wp_attachment_is_image( $post->ID ) ? 'image' : 'file';
    283304                $data['mime_type']     = $post->post_mime_type;
    284305                $data['media_details'] = wp_get_attachment_metadata( $post->ID );
    class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { 
    366387                );
    367388
    368389                $schema['properties']['caption'] = array(
    369                         'description'     => __( 'The caption for the resource.' ),
    370                         'type'            => 'string',
    371                         'context'         => array( 'view', 'edit' ),
    372                         'arg_options'     => array(
    373                                 'sanitize_callback' => 'wp_filter_post_kses',
     390                        'description' => __( 'The caption for the resource.' ),
     391                        'type'        => 'object',
     392                        'context'     => array( 'view', 'edit', 'embed' ),
     393                        'arg_options' => array(
     394                                'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database()
     395                        ),
     396                        'properties'  => array(
     397                                'raw' => array(
     398                                        'description' => __( 'Caption for the resource, as it exists in the database.' ),
     399                                        'type'        => 'string',
     400                                        'context'     => array( 'edit' ),
     401                                ),
     402                                'rendered' => array(
     403                                        'description' => __( 'HTML caption for the resource, transformed for display.' ),
     404                                        'type'        => 'string',
     405                                        'context'     => array( 'view', 'edit', 'embed' ),
     406                                        'readonly'    => true,
     407                                ),
    374408                        ),
    375409                );
    376410
    377411                $schema['properties']['description'] = array(
    378                         'description'     => __( 'The description for the resource.' ),
    379                         'type'            => 'string',
    380                         'context'         => array( 'view', 'edit' ),
    381                         'arg_options'     => array(
    382                                 'sanitize_callback' => 'wp_filter_post_kses',
     412                        'description' => __( 'The description for the resource.' ),
     413                        'type'        => 'object',
     414                        'context'     => array( 'view', 'edit' ),
     415                        'arg_options' => array(
     416                                'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database()
     417                        ),
     418                        'properties'  => array(
     419                                'raw' => array(
     420                                        'description' => __( 'Description for the object, as it exists in the database.' ),
     421                                        'type'        => 'string',
     422                                        'context'     => array( 'edit' ),
     423                                ),
     424                                'rendered' => array(
     425                                        'description' => __( 'HTML description for the object, transformed for display.' ),
     426                                        'type'        => 'string',
     427                                        'context'     => array( 'view', 'edit' ),
     428                                        'readonly'    => true,
     429                                ),
    383430                        ),
    384431                );
    385432
  • tests/phpunit/tests/rest-api/rest-attachments-controller.php

    diff --git tests/phpunit/tests/rest-api/rest-attachments-controller.php tests/phpunit/tests/rest-api/rest-attachments-controller.php
    index 3fceb44..b84b0cc 100644
    class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 
    460460
    461461        public function test_create_item() {
    462462                wp_set_current_user( self::$author_id );
     463
    463464                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    464465                $request->set_header( 'Content-Type', 'image/jpeg' );
    465466                $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
     467                $request->set_param( 'title', 'My title is very cool' );
     468                $request->set_param( 'caption', 'This is a better caption.' );
     469                $request->set_param( 'description', 'Without a description, my attachment is descriptionless.' );
     470                $request->set_param( 'alt_text', 'Alt text is stored outside post schema.' );
     471
    466472                $request->set_body( file_get_contents( $this->test_file ) );
    467473                $response = $this->server->dispatch( $request );
    468474                $data = $response->get_data();
     475
    469476                $this->assertEquals( 201, $response->get_status() );
    470477                $this->assertEquals( 'image', $data['media_type'] );
     478
     479                $attachment = get_post( $data['id'] );
     480                $this->assertEquals( 'My title is very cool', $data['title']['raw'] );
     481                $this->assertEquals( 'My title is very cool', $attachment->post_title );
     482                $this->assertEquals( 'This is a better caption.', $data['caption']['raw'] );
     483                $this->assertEquals( 'This is a better caption.', $attachment->post_excerpt );
     484                $this->assertEquals( 'Without a description, my attachment is descriptionless.', $data['description']['raw'] );
     485                $this->assertEquals( 'Without a description, my attachment is descriptionless.', $attachment->post_content );
     486                $this->assertEquals( 'Alt text is stored outside post schema.', $data['alt_text'] );
     487                $this->assertEquals( 'Alt text is stored outside post schema.', get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ) );
    471488        }
    472489
    473490        public function test_create_item_default_filename_title() {
    class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 
    650667                $attachment = get_post( $data['id'] );
    651668                $this->assertEquals( 'My title is very cool', $data['title']['raw'] );
    652669                $this->assertEquals( 'My title is very cool', $attachment->post_title );
    653                 $this->assertEquals( 'This is a better caption.', $data['caption'] );
     670                $this->assertEquals( 'This is a better caption.', $data['caption']['raw'] );
    654671                $this->assertEquals( 'This is a better caption.', $attachment->post_excerpt );
    655                 $this->assertEquals( 'Without a description, my attachment is descriptionless.', $data['description'] );
     672                $this->assertEquals( 'Without a description, my attachment is descriptionless.', $data['description']['raw'] );
    656673                $this->assertEquals( 'Without a description, my attachment is descriptionless.', $attachment->post_content );
    657674                $this->assertEquals( 'Alt text is stored outside post schema.', $data['alt_text'] );
    658675                $this->assertEquals( 'Alt text is stored outside post schema.', get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ) );
    class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 
    775792                $this->assertArrayHasKey( 'author', $properties );
    776793                $this->assertArrayHasKey( 'alt_text', $properties );
    777794                $this->assertArrayHasKey( 'caption', $properties );
     795                $this->assertArrayHasKey( 'raw', $properties['caption']['properties'] );
     796                $this->assertArrayHasKey( 'rendered', $properties['caption']['properties'] );
    778797                $this->assertArrayHasKey( 'description', $properties );
     798                $this->assertArrayHasKey( 'raw', $properties['description']['properties'] );
     799                $this->assertArrayHasKey( 'rendered', $properties['description']['properties'] );
    779800                $this->assertArrayHasKey( 'comment_status', $properties );
    780801                $this->assertArrayHasKey( 'date', $properties );
    781802                $this->assertArrayHasKey( 'date_gmt', $properties );
    class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 
    794815                $this->assertArrayHasKey( 'slug', $properties );
    795816                $this->assertArrayHasKey( 'source_url', $properties );
    796817                $this->assertArrayHasKey( 'title', $properties );
     818                $this->assertArrayHasKey( 'raw', $properties['title']['properties'] );
     819                $this->assertArrayHasKey( 'rendered', $properties['title']['properties'] );
    797820                $this->assertArrayHasKey( 'type', $properties );
    798821        }
    799822
    class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 
    891914        protected function check_post_data( $attachment, $data, $context = 'view', $links ) {
    892915                parent::check_post_data( $attachment, $data, $context, $links );
    893916
     917                $this->assertArrayNotHasKey( 'content', $data );
     918                $this->assertArrayNotHasKey( 'excerpt', $data );
     919
    894920                $this->assertEquals( get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), $data['alt_text'] );
    895                 $this->assertEquals( $attachment->post_excerpt, $data['caption'] );
    896                 $this->assertEquals( $attachment->post_content, $data['description'] );
     921                if ( 'edit' === $context ) {
     922                        $this->assertEquals( $attachment->post_excerpt, $data['caption']['raw'] );
     923                        $this->assertEquals( $attachment->post_content, $data['description']['raw'] );
     924                } else {
     925                        $this->assertFalse( isset( $data['caption']['raw'] ) );
     926                        $this->assertFalse( isset( $data['description']['raw'] ) );
     927                }
    897928                $this->assertTrue( isset( $data['media_details'] ) );
    898929
    899930                if ( $attachment->post_parent ) {