diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 365c4f61ff..03d30b0e7a 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -431,10 +431,10 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { } /* - * Do not allow a comment to be created with an empty string for + * Do not allow a comment to be created with missing or empty * comment_content. See wp_handle_comment_submission(). */ - if ( '' === $prepared_comment['comment_content'] ) { + if ( empty( $prepared_comment['comment_content'] ) ) { return new WP_Error( 'rest_comment_content_invalid', __( 'Comment content is invalid.' ), array( 'status' => 400 ) ); } @@ -636,6 +636,10 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { return $prepared_args; } + if ( isset( $prepared_args['comment_content'] ) && empty( $prepared_args['comment_content'] ) ) { + return new WP_Error( 'rest_comment_content_invalid', __( 'Comment content is invalid.' ), array( 'status' => 400 ) ); + } + $prepared_args['comment_ID'] = $id; $check_comment_lengths = wp_check_comment_data_max_lengths( $prepared_args ); @@ -1064,11 +1068,6 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { } } - // Require 'comment_content' unless only the 'comment_status' is being updated. - if ( ! empty( $prepared_comment ) && ! isset( $prepared_comment['comment_content'] ) ) { - return new WP_Error( 'rest_comment_content_required', __( 'Missing comment content.' ), array( 'status' => 400 ) ); - } - /** * Filters a comment after it is prepared for the database. * diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index e225cbcd86..a0d87ffad5 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -929,7 +929,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->assertArrayHasKey( 'author_email', $data['data']['params'] ); } - public function test_create_item_invalid_blank_content() { + public function test_create_item_invalid_no_content() { wp_set_current_user( 0 ); $params = array( @@ -937,7 +937,6 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 'author_name' => 'Reverend Lovejoy', 'author_email' => 'lovejoy@example.com', 'author_url' => 'http://timothylovejoy.jr', - 'content' => '', ); $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); @@ -946,6 +945,11 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 ); + + $params['content'] = ''; + $request->set_body( wp_json_encode( $params ) ); + $response = $this->server->dispatch( $request ); + $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 ); } public function test_create_item_invalid_date() { @@ -1618,6 +1622,25 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->assertEquals( '2014-11-07T10:14:25', $comment['date'] ); } + public function test_update_item_no_content() { + $post_id = $this->factory->post->create(); + + wp_set_current_user( self::$admin_id ); + + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); + $request->set_param( 'author_email', 'another@email.com' ); + + // Sending a request without content is fine. + $response = $this->server->dispatch( $request ); + $this->assertEquals( 200, $response->get_status() ); + + // Sending a request with empty comment is not fine. + $request->set_param( 'author_email', 'yetanother@email.com' ); + $request->set_param( 'content', '' ); + $response = $this->server->dispatch( $request ); + $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 ); + } + public function test_update_comment_status() { wp_set_current_user( self::$admin_id );