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 94d75cf42a..bd0c920c0f 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 @@ -664,6 +664,13 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { return $prepared_args; } + if ( ! empty( $prepared_args['comment_post_ID'] ) ) { + $post = get_post( $prepared_args['comment_post_ID'] ); + if ( empty( $post ) ) { + return new WP_Error( 'rest_comment_invalid_post_id', __( 'Invalid post ID.' ), array( 'status' => 403 ) ); + } + } + if ( empty( $prepared_args ) && isset( $request['status'] ) ) { // Only the comment status is being changed. $change = $this->handle_status_param( $request['status'], $id ); @@ -690,7 +697,7 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { $updated = wp_update_comment( wp_slash( (array) $prepared_args ) ); - if ( 0 === $updated ) { + if ( false === $updated ) { return new WP_Error( 'rest_comment_failed_edit', __( 'Updating comment failed.' ), array( 'status' => 500 ) ); } diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index 27e7e4e813..9de7d88308 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -1961,6 +1961,22 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 ); } + public function test_update_item_no_change() { + $comment = get_comment( self::$approved_id ); + + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); + $request->set_param( 'post', $comment->comment_post_ID ); + + // Run twice to make sure that the update still succeeds even if no DB + // rows are updated. + $response = $this->server->dispatch( $request ); + $this->assertEquals( 200, $response->get_status() ); + + $response = $this->server->dispatch( $request ); + $this->assertEquals( 200, $response->get_status() ); + } + public function test_update_comment_status() { wp_set_current_user( self::$admin_id ); @@ -2206,6 +2222,16 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase $this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 ); } + public function test_update_comment_invalid_post_id() { + wp_set_current_user( self::$admin_id ); + + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) ); + $request->set_param( 'post', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); + + $response = $this->server->dispatch( $request ); + $this->assertErrorResponse( 'rest_comment_invalid_post_id', $response, 403 ); + } + public function test_update_comment_invalid_permission() { add_filter( 'rest_allow_anonymous_comments', '__return_true' );