Skip to content
Open
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 @@ -568,6 +568,14 @@ public function create_item_permissions_check( $request ) {
);
}

if ( empty( $request['post'] ) ) {
Comment thread
ramonjd marked this conversation as resolved.
return new WP_Error(
'rest_comment_invalid_post_id',
__( 'Sorry, you are not allowed to create this comment without a post.' ),
array( 'status' => 403 )
);
}
Comment thread
ramonjd marked this conversation as resolved.

$edit_cap = $is_note ? array( 'edit_post', (int) $request['post'] ) : array( 'moderate_comments' );
if ( isset( $request['status'] ) && ! current_user_can( ...$edit_cap ) ) {
return new WP_Error(
Expand All @@ -578,14 +586,6 @@ public function create_item_permissions_check( $request ) {
);
}

if ( empty( $request['post'] ) ) {
return new WP_Error(
'rest_comment_invalid_post_id',
__( 'Sorry, you are not allowed to create this comment without a post.' ),
array( 'status' => 403 )
);
}

$post = get_post( (int) $request['post'] );

if ( ! $post ) {
Expand Down
55 changes: 55 additions & 0 deletions tests/phpunit/tests/rest-api/rest-comments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2024,6 +2024,31 @@ public function test_create_comment_no_post_id_no_permission() {
$this->assertErrorResponse( 'rest_comment_invalid_post_id', $response, 403 );
}

/**
* A missing post should be reported as a missing post, even when the request
* also sets `status` and the user is not allowed to set it.
*
* @ticket 65761
*/
public function test_create_comment_status_and_no_post_id_no_permission() {
wp_set_current_user( self::$author_id );

$params = array(
'author_name' => 'Homer Jay Simpson',
'author_email' => 'chunkylover53@aol.com',
'author_url' => 'http://compuglobalhypermeganet.com',
'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
'status' => 'approved',
);

$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
$request->add_header( 'Content-Type', 'application/json' );
$request->set_body( wp_json_encode( $params ) );

$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_comment_invalid_post_id', $response, 403 );
}

public function test_create_comment_invalid_post_id() {
wp_set_current_user( self::$admin_id );

Expand Down Expand Up @@ -3813,6 +3838,36 @@ public function test_create_note_status() {
$this->assertSame( 'note', $new_comment->comment_type );
}

/**
* A missing post should be reported as a missing post for notes too.
*
* Without a post, the `status` capability check falls back to
* `current_user_can( 'edit_post', 0 )`, which no role can satisfy. An
* administrator is used here to show the missing post is reported even for a
* user holding every capability.
*
* @ticket 65761
*/
public function test_create_note_status_and_no_post_id() {
wp_set_current_user( self::$admin_id );

$params = array(
'author_name' => 'Ishmael',
'author_email' => 'herman-melville@earthlink.net',
'author_url' => 'https://en.wikipedia.org/wiki/Herman_Melville',
'content' => 'Comic Book Guy',
'type' => 'note',
'status' => 'hold',
);

$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
$request->add_header( 'Content-Type', 'application/json' );
$request->set_body( wp_json_encode( $params ) );

$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_comment_invalid_post_id', $response, 403 );
}

/**
* @ticket 64096
*/
Expand Down
Loading