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 a0b68759f9942..0714a9ebfb108 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 @@ -859,8 +859,7 @@ public function update_item( $request ) { if ( is_wp_error( $prepared_args ) ) { return $prepared_args; } - - if ( isset( $prepared_args['comment_content'] ) && empty( $prepared_args['comment_content'] ) ) { + if ( ! $this->check_is_comment_content_allowed( $prepared_args ) ) { return new WP_Error( 'rest_comment_content_invalid', __( 'Invalid comment content.' ), @@ -1903,6 +1902,10 @@ public function check_comment_author_email( $value, $request, $param ) { * @return bool True if the content is allowed, false otherwise. */ protected function check_is_comment_content_allowed( $prepared_comment ) { + if ( ! isset( $prepared_comment['comment_content'] ) ) { + return true; + } + $check = wp_parse_args( $prepared_comment, array( diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index 0bfe4e778d870..90192a2e247a0 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -2493,6 +2493,30 @@ public function test_update_item_no_content() { $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 ); } + /** + * @ticket 64049 + */ + public function test_update_item_no_content_allow_empty_comment_filter() { + $post_id = self::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 = rest_get_server()->dispatch( $request ); + $this->assertSame( 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', '' ); + add_filter( 'allow_empty_comment', '__return_true' ); + $response = rest_get_server()->dispatch( $request ); + remove_filter( 'allow_empty_comment', '__return_true' ); + $this->assertSame( 200, $response->get_status() ); + } + public function test_update_item_no_change() { $comment = get_comment( self::$approved_id );