Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -574,21 +574,12 @@ public function create_item( $request ) {
);
}

// Do not allow comments to be created with a non-default type.
if ( ! empty( $request['type'] ) && 'comment' !== $request['type'] ) {
return new WP_Error(
'rest_invalid_comment_type',
__( 'Cannot create a comment with that type.' ),
array( 'status' => 400 )
);
}

Comment on lines -577 to -585
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be removed.

As the change to the unit tests show, it allows the addition of comment types to the database that WordPress is unable to support. This is problematic for a number of reasons, primarily because it fills the databse with junk, secondly because plugins may not expect the data and throw a fatal error as a result.

$prepared_comment = $this->prepare_item_for_database( $request );
if ( is_wp_error( $prepared_comment ) ) {
return $prepared_comment;
}

$prepared_comment['comment_type'] = 'comment';
$prepared_comment['comment_type'] = isset( $request['type'] ) ? $request['type'] : 'comment';

if ( ! isset( $prepared_comment['comment_content'] ) ) {
$prepared_comment['comment_content'] = '';
Expand Down Expand Up @@ -657,6 +648,7 @@ public function create_item( $request ) {
}

$prepared_comment['comment_approved'] = wp_allow_comment( $prepared_comment, true );
$prepared_comment['comment_approved'] = isset( $request['comment_approved'] ) ? $request['comment_approved'] : $prepared_comment['comment_approved'];

if ( is_wp_error( $prepared_comment['comment_approved'] ) ) {
$error_code = $prepared_comment['comment_approved']->get_error_code();
Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/tests/rest-api/rest-comments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1592,7 +1592,8 @@ public function test_create_comment_with_invalid_type() {
$request->set_body( wp_json_encode( $params ) );

$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_comment_type', $response, 400 );
$this->assertFalse( is_wp_error( $response ) );
$this->assertSame( 201, $response->get_status() );
}

public function test_create_comment_invalid_email() {
Expand Down