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 @@ -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.' ),
Expand Down Expand Up @@ -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(
Expand Down
24 changes: 24 additions & 0 deletions tests/phpunit/tests/rest-api/rest-comments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 content is also 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 );

Expand Down
Loading