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 @@ -787,21 +787,7 @@ public function create_item( $request ) {
* reactions from then on. Hold requests from stale editor sessions
* to that too, for the root note and for every reply in its thread.
*/
$thread_root = $parent_comment;
$visited = array( (int) $thread_root->comment_ID => true );
while ( $thread_root->comment_parent ) {
$ancestor = get_comment( $thread_root->comment_parent );

// Stop at a missing ancestor or a corrupt, cyclic chain.
if ( ! $ancestor || isset( $visited[ (int) $ancestor->comment_ID ] ) ) {
break;
}

$visited[ (int) $ancestor->comment_ID ] = true;
$thread_root = $ancestor;
}

if ( '1' === $thread_root->comment_approved ) {
if ( $this->is_thread_resolved( $parent_comment ) ) {
return new WP_Error(
'rest_comment_invalid_parent',
__( 'A reaction cannot be added to a resolved note.' ),
Expand Down Expand Up @@ -1350,6 +1336,28 @@ public function delete_item_permissions_check( $request ) {
array( 'status' => rest_authorization_required_code() )
);
}

if ( 'reaction' === strtolower( $comment->comment_type ) ) {
// A reaction speaks for its author, so only they can remove it.
if ( get_current_user_id() !== (int) $comment->user_id ) {
return new WP_Error(
'rest_cannot_delete',
__( 'Sorry, you can only remove your own reactions.' ),
array( 'status' => rest_authorization_required_code() )
);
}

// Reactions on a resolved thread are frozen, like adding new ones.
$note = get_comment( $comment->comment_parent );
if ( $note instanceof WP_Comment && $this->is_thread_resolved( $note ) ) {
return new WP_Error(
'rest_cannot_delete',
__( 'A reaction cannot be removed from a resolved note.' ),
array( 'status' => 403 )
);
}
}

return true;
}

Expand Down Expand Up @@ -2652,6 +2660,34 @@ protected function check_target_post_permission( int $post_id, WP_REST_Request $
return true;
}

/**
* Checks whether a note belongs to a resolved thread.
*
* Resolving a thread approves its root note.
*
* @since 7.2.0
*
* @param WP_Comment $note A root note or a reply.
* @return bool True if the note's thread is resolved.
*/
private function is_thread_resolved( WP_Comment $note ): bool {
$thread_root = $note;
$visited = array( (int) $thread_root->comment_ID => true );
while ( $thread_root->comment_parent ) {
$ancestor = get_comment( $thread_root->comment_parent );

// Stop at a missing ancestor or a corrupt, cyclic chain.
if ( ! $ancestor || isset( $visited[ (int) $ancestor->comment_ID ] ) ) {
break;
}

$visited[ (int) $ancestor->comment_ID ] = true;
$thread_root = $ancestor;
}

return '1' === $thread_root->comment_approved;
}

/**
* Check if post type supports notes.
*
Expand Down
113 changes: 112 additions & 1 deletion tests/phpunit/tests/rest-api/rest-comments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -5996,6 +5996,116 @@ public function test_delete_own_reaction_on_another_users_note() {
$this->assertNull( get_comment( $reaction_id ) );
}

/**
* A reaction speaks for its author, so being able to edit the post is not
* enough to remove someone else's.
*
* @ticket 66447
*
* @dataProvider data_users_who_cannot_delete_another_users_reaction
*
* @param string $user Name of the static property holding the user ID.
*/
public function test_cannot_delete_another_users_reaction( string $user ): void {
$post_id = self::factory()->post->create( array( 'post_author' => self::$author_id ) );
$note_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_type' => 'note',
'comment_approved' => 0,
'user_id' => self::$editor_id,
'comment_content' => 'Test note',
)
);
$reaction_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_type' => 'reaction',
'comment_parent' => $note_id,
'comment_approved' => 1,
'user_id' => self::$editor_id,
'comment_content' => '2764',
)
);

wp_set_current_user( self::${$user} );

$request = new WP_REST_Request( 'DELETE', '/wp/v2/comments/' . $reaction_id );
$request->set_param( 'force', true );
$response = rest_get_server()->dispatch( $request );

$this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
$this->assertNotNull( get_comment( $reaction_id ), 'Reaction must still exist.' );
}

/**
* Data provider for test_cannot_delete_another_users_reaction().
*
* @return array<string, array{string}>
*/
public static function data_users_who_cannot_delete_another_users_reaction(): array {
return array(
'post author' => array( 'author_id' ),
'administrator' => array( 'admin_id' ),
);
}

/**
* Reactions on a resolved thread are frozen, like adding new ones, on the
* root note and on its replies.
*
* @ticket 66447
*
* @dataProvider data_resolved_thread_targets
*
* @param bool $on_reply Whether the reaction is on a reply rather than the root note.
*/
public function test_cannot_delete_reaction_on_resolved_thread( bool $on_reply ): void {
wp_set_current_user( self::$editor_id );

$post_id = self::factory()->post->create( array( 'post_author' => self::$editor_id ) );
$note_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_type' => 'note',
'comment_approved' => 0,
'user_id' => self::$editor_id,
'comment_content' => 'Test note',
)
);
$target = $note_id;
if ( $on_reply ) {
$target = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_parent' => $note_id,
'comment_type' => 'note',
'comment_approved' => 0,
'user_id' => self::$editor_id,
'comment_content' => 'Test reply',
)
);
}
$reaction_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_type' => 'reaction',
'comment_parent' => $target,
'comment_approved' => 1,
'user_id' => self::$editor_id,
'comment_content' => '2764',
)
);
wp_set_comment_status( $note_id, 'approve' );

$request = new WP_REST_Request( 'DELETE', '/wp/v2/comments/' . $reaction_id );
$request->set_param( 'force', true );
$response = rest_get_server()->dispatch( $request );

$this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
$this->assertNotNull( get_comment( $reaction_id ), 'Reaction must still exist.' );
}

/**
* A reaction cannot be added to a trashed or spammed note, where it would
* escape the trash cascade.
Expand Down Expand Up @@ -6108,7 +6218,8 @@ public function test_create_reaction_on_resolved_thread( $on_reply ) {
}

/**
* Data provider for test_create_reaction_on_resolved_thread().
* Data provider for test_create_reaction_on_resolved_thread() and
* test_cannot_delete_reaction_on_resolved_thread().
*
* @return array[]
*/
Expand Down
Loading