Skip to content

Commit 1c9e7ef

Browse files
committed
REST API: Allow comment content to be updated to empty when allow_empty_comment filter returns true
This filter was already respected during comment creation, but not updates. This was inconsistent between POST and PUT, and prevented erasing content of moderated comments via the API without deleting the entire object. Props adamsilverstein, kadamwhite, davidbaumwald, mukesh27, timothyblynjacobs. Fixes #64049. git-svn-id: https://develop.svn.wordpress.org/trunk@60937 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a721cf9 commit 1c9e7ef

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -859,8 +859,7 @@ public function update_item( $request ) {
859859
if ( is_wp_error( $prepared_args ) ) {
860860
return $prepared_args;
861861
}
862-
863-
if ( isset( $prepared_args['comment_content'] ) && empty( $prepared_args['comment_content'] ) ) {
862+
if ( ! $this->check_is_comment_content_allowed( $prepared_args ) ) {
864863
return new WP_Error(
865864
'rest_comment_content_invalid',
866865
__( 'Invalid comment content.' ),
@@ -1903,6 +1902,10 @@ public function check_comment_author_email( $value, $request, $param ) {
19031902
* @return bool True if the content is allowed, false otherwise.
19041903
*/
19051904
protected function check_is_comment_content_allowed( $prepared_comment ) {
1905+
if ( ! isset( $prepared_comment['comment_content'] ) ) {
1906+
return true;
1907+
}
1908+
19061909
$check = wp_parse_args(
19071910
$prepared_comment,
19081911
array(

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,6 +2493,30 @@ public function test_update_item_no_content() {
24932493
$this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 );
24942494
}
24952495

2496+
/**
2497+
* @ticket 64049
2498+
*/
2499+
public function test_update_item_no_content_allow_empty_comment_filter() {
2500+
$post_id = self::factory()->post->create();
2501+
2502+
wp_set_current_user( self::$admin_id );
2503+
2504+
$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
2505+
$request->set_param( 'author_email', '[email protected]' );
2506+
2507+
// Sending a request without content is fine.
2508+
$response = rest_get_server()->dispatch( $request );
2509+
$this->assertSame( 200, $response->get_status() );
2510+
2511+
// Sending a request with empty comment content is also fine.
2512+
$request->set_param( 'author_email', '[email protected]' );
2513+
$request->set_param( 'content', '' );
2514+
add_filter( 'allow_empty_comment', '__return_true' );
2515+
$response = rest_get_server()->dispatch( $request );
2516+
remove_filter( 'allow_empty_comment', '__return_true' );
2517+
$this->assertSame( 200, $response->get_status() );
2518+
}
2519+
24962520
public function test_update_item_no_change() {
24972521
$comment = get_comment( self::$approved_id );
24982522

0 commit comments

Comments
 (0)