Skip to content
Merged
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
3 changes: 3 additions & 0 deletions backport-changelog/7.2/15130.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/15130

* https://github.com/WordPress/gutenberg/pull/85883
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,39 @@ public function update_item_permissions_check( $request ) {
return parent::update_item_permissions_check( $request );
}

/**
* Checks if a comment can be edited or deleted.
*
* Copied from core, extended to the 'reaction' type. A reaction requires
* the same edit access to the note's post as the note it belongs to.
*
* @since 7.2.0
*
* @param WP_Comment $comment Comment object.
* @return bool Whether the comment can be edited or deleted.
*/
protected function check_edit_permission( $comment ) {
if ( 0 === (int) get_current_user_id() ) {
return false;
}

// START CHANGE: Check notes and reactions against the note's post.
/*
* Notes and their reactions are editorial content, so require edit access
* to the note's post rather than the moderate_comments capability.
*/
if ( in_array( strtolower( $comment->comment_type ), array( 'note', 'reaction' ), true ) ) {
Comment on lines +368 to +378

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor - could it use is_note_or_reaction?

Also, that method doesn't strtolower the comment_type. Bug, or is the code here over protective?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I saw is_note_or_reaction and noticed it was a Gutenberg only helper. Not sure what the reasoning behind that was.

We could use it, but I think matching Core might be less confusing in the end. I'm on the fence. Maybe @adamsilverstein has a preference?

strtolower was there for edge cases to a comment type of Note or Reaction. In the database it's case insensitive.

So where, for example, a comment stored as 'Note' is returned by note queries.

return current_user_can( 'edit_comment', $comment->comment_ID );
}
// END CHANGE.

if ( current_user_can( 'moderate_comments' ) ) {
return true;
}

return current_user_can( 'edit_comment', $comment->comment_ID );
Comment on lines +383 to +387

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realise this is the existing code, but maybe it could be:

Suggested change
if ( current_user_can( 'moderate_comments' ) ) {
return true;
}
return current_user_can( 'edit_comment', $comment->comment_ID );
return current_user_can( 'moderate_comments' ) || current_user_can( 'edit_comment', $comment->comment_ID );

}

/**
* Checks if a given request has access to create a comment.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ class WP_Test_REST_Comments_Controller_Gutenberg extends WP_Test_REST_TestCase {
protected static $contributor_id;
protected static $subscriber_id;
protected static $author_id;
protected static $moderator_id;
protected static $user_ids = array();
protected static $post_id;
protected static $num_notes = 10;

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
add_role(
'comment_moderator',
'Comment Moderator',
array(
'read' => true,
'moderate_comments' => true,
)
);

self::$admin_id = $factory->user->create(
array(
'role' => 'administrator',
Expand All @@ -40,6 +50,11 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
'role' => 'author',
)
);
self::$moderator_id = $factory->user->create(
array(
'role' => 'comment_moderator',
)
);

self::$user_ids = array(
'administrator' => self::$admin_id,
Expand All @@ -51,11 +66,14 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
}

public static function wpTearDownAfterClass() {
remove_role( 'comment_moderator' );

self::delete_user( self::$admin_id );
self::delete_user( self::$editor_id );
self::delete_user( self::$contributor_id );
self::delete_user( self::$subscriber_id );
self::delete_user( self::$author_id );
self::delete_user( self::$moderator_id );
}

/**
Expand Down Expand Up @@ -1212,6 +1230,24 @@ public function test_can_still_update_note() {
$this->assertSame( 200, $response->get_status() );
}

/**
* A user with moderate_comments but no editing capabilities cannot remove a
* reaction on a post they cannot edit.
*/
public function test_moderator_cannot_delete_reaction() {
$post_id = self::factory()->post->create( array( 'post_author' => self::$editor_id ) );
$note_id = $this->create_note( $post_id, self::$editor_id );
$reaction_id = $this->create_reaction( $post_id, $note_id, self::$editor_id );

wp_set_current_user( self::$moderator_id );
$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.' );
}

/**
* `wp_delete_comment()` reparents children rather than deleting them, so
* without a cascade the reactions would outlive their note as orphan rows.
Expand Down
Loading