WordPress.org

Make WordPress Core

Changeset 39337


Ignore:
Timestamp:
11/21/2016 10:55:33 PM (18 months ago)
Author:
rachelbaker
Message:

REST API: Set the comment type to a readonly property in the schema.

Document the type property as readonly and remove the default value. After #38820 it is no longer possible to set the type property on a comment to anything a custom type.

Props jnylen0, rachelbaker.
Fixes #38886.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r39327 r39337  
    459459        }
    460460
    461         $prepared_comment = $this->prepare_item_for_database( $request );
    462 
    463         if ( is_wp_error( $prepared_comment ) ) {
    464             return $prepared_comment;
    465         }
    466 
    467461        // Do not allow comments to be created with a non-default type.
    468462        if ( ! empty( $request['type'] ) && 'comment' !== $request['type'] ) {
    469463            return new WP_Error( 'rest_invalid_comment_type', __( 'Cannot create a comment with that type.' ), array( 'status' => 400 ) );
    470464        }
     465
     466        $prepared_comment = $this->prepare_item_for_database( $request );
     467        if ( is_wp_error( $prepared_comment ) ) {
     468            return $prepared_comment;
     469        }
     470
     471        $prepared_comment['comment_type'] = '';
    471472
    472473        /*
     
    10881089        } elseif ( $request->get_header( 'user_agent' ) ) {
    10891090            $prepared_comment['comment_agent'] = $request->get_header( 'user_agent' );
    1090         }
    1091 
    1092         if ( isset( $request['type'] ) ) {
    1093             // Comment type "comment" needs to be created as an empty string.
    1094             $prepared_comment['comment_type'] = 'comment' === $request['type'] ? '' : $request['type'];
    10951091        }
    10961092
     
    12451241                    'type'         => 'string',
    12461242                    'context'      => array( 'view', 'edit', 'embed' ),
    1247                     'default'      => 'comment',
    1248                     'arg_options'  => array(
    1249                         'sanitize_callback' => 'sanitize_key',
    1250                     ),
     1243                    'readonly'     => true,
    12511244                ),
    12521245            ),
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r39327 r39337  
    23312331        $this->assertArrayHasKey( 'status', $properties );
    23322332        $this->assertArrayHasKey( 'type', $properties );
    2333         $this->assertEquals( 'comment', $properties['type']['default'] );
    23342333
    23352334        $this->assertEquals( 0, $properties['parent']['default'] );
    23362335        $this->assertEquals( 0, $properties['post']['default'] );
     2336
     2337        $this->assertEquals( true, $properties['link']['readonly'] );
     2338        $this->assertEquals( true, $properties['type']['readonly'] );
    23372339    }
    23382340
Note: See TracChangeset for help on using the changeset viewer.