Skip to content

Commit ae2ba63

Browse files
committed
Don't change 'type' schema
1 parent 10e1023 commit ae2ba63

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,12 +626,21 @@ public function create_item( $request ) {
626626
);
627627
}
628628

629+
// Do not allow comments to be created with a non-core type.
630+
if ( ! empty( $request['type'] ) && ! in_array( $request['type'], array( 'comment', 'note' ), true ) ) {
631+
return new WP_Error(
632+
'rest_invalid_comment_type',
633+
__( 'Cannot create a comment with that type.', 'gutenberg' ),
634+
array( 'status' => 400 )
635+
);
636+
}
637+
629638
$prepared_comment = $this->prepare_item_for_database( $request );
630639
if ( is_wp_error( $prepared_comment ) ) {
631640
return $prepared_comment;
632641
}
633642

634-
$prepared_comment['comment_type'] = $request['type'];
643+
$prepared_comment['comment_type'] = empty( $request['type'] ) ? 'comment' : $request['type'];
635644

636645
if ( ! isset( $prepared_comment['comment_content'] ) ) {
637646
$prepared_comment['comment_content'] = '';
@@ -1556,9 +1565,8 @@ public function get_item_schema() {
15561565
'type' => array(
15571566
'description' => __( 'Type of the comment.' ),
15581567
'type' => 'string',
1559-
'enum' => array( 'comment', 'note' ),
1560-
'default' => 'comment',
15611568
'context' => array( 'view', 'edit', 'embed' ),
1569+
'readonly' => true,
15621570
),
15631571
),
15641572
);

tests/phpunit/tests/rest-api/rest-comments-controller.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,7 +1699,7 @@ public function test_create_comment_with_invalid_type() {
16991699
$request->set_body( wp_json_encode( $params ) );
17001700

17011701
$response = rest_get_server()->dispatch( $request );
1702-
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
1702+
$this->assertErrorResponse( 'rest_invalid_comment_type', $response, 400 );
17031703
}
17041704

17051705
public function test_create_comment_invalid_email() {
@@ -2716,7 +2716,7 @@ public function test_update_comment_invalid_type() {
27162716
$request->set_body( wp_json_encode( $params ) );
27172717

27182718
$response = rest_get_server()->dispatch( $request );
2719-
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
2719+
$this->assertErrorResponse( 'rest_comment_invalid_type', $response, 404 );
27202720
}
27212721

27222722
public function test_update_comment_with_raw_property() {
@@ -3305,9 +3305,9 @@ public function test_get_item_schema() {
33053305

33063306
$this->assertSame( 0, $properties['parent']['default'] );
33073307
$this->assertSame( 0, $properties['post']['default'] );
3308-
$this->assertSame( array( 'comment', 'note' ), $properties['type']['enum'] );
33093308

33103309
$this->assertTrue( $properties['link']['readonly'] );
3310+
$this->assertTrue( $properties['type']['readonly'] );
33113311
}
33123312

33133313
public function test_get_item_schema_show_avatar() {
@@ -3792,8 +3792,10 @@ public function test_create_note_status() {
37923792
*/
37933793
public function test_cannot_create_with_non_valid_comment_type() {
37943794
wp_set_current_user( self::$admin_id );
3795+
$post_id = $this->factory->post->create();
37953796

37963797
$params = array(
3798+
'post' => $post_id,
37973799
'author_name' => 'Ishmael',
37983800
'author_email' => '[email protected]',
37993801
'author_url' => 'https://en.wikipedia.org/wiki/Herman_Melville',
@@ -3807,7 +3809,7 @@ public function test_cannot_create_with_non_valid_comment_type() {
38073809
$request->set_body( wp_json_encode( $params ) );
38083810
$response = rest_get_server()->dispatch( $request );
38093811

3810-
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
3812+
$this->assertErrorResponse( 'rest_invalid_comment_type', $response, 400 );
38113813
}
38123814

38133815
/**

0 commit comments

Comments
 (0)