REST API: Allow updating a comment without the content present.

For all resources in the REST API, sending partial updates is supported. This fixes needing to _always_ specify comment content.

Props jnylen.
Fixes #38720.


git-svn-id: https://develop.svn.wordpress.org/trunk@39196 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe Hoyle 2016-11-10 03:34:30 +00:00
parent 8b464c7140
commit 08c7dddc41
2 changed files with 31 additions and 9 deletions

View File

@ -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.
*

View File

@ -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 );