diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 6a1641cbae4d0..62bf7c29dd50d 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -3004,7 +3004,12 @@ function wp_update_comment( $commentarr, $wp_error = false ) { $filter_comment = false; if ( ! has_filter( 'pre_comment_content', 'wp_filter_kses' ) ) { - $filter_comment = ! user_can( $comment['user_id'] ?? 0, 'unfiltered_html' ); + /* + * Filter the comment content based on the capability of the user performing + * the update rather than the comment author, for consistency with the post + * editor. Programmatic updates without a logged-in user are filtered by default. + */ + $filter_comment = ! current_user_can( 'unfiltered_html' ); } if ( $filter_comment ) { diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index c5c40b9b34da6..342438168ebe2 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -131,6 +131,14 @@ public function test_update_comment_from_privileged_user_by_privileged_user() { $this->assertSame( $expected_content, $comment->comment_content ); } + /** + * A privileged user editing an unprivileged user's comment may save HTML + * beyond the comment author's allowed markup, consistent with the post + * editor. On multisite, where no user has the unfiltered_html capability, + * the content is still filtered. + * + * @ticket 57343 + */ public function test_update_comment_from_unprivileged_user_by_privileged_user() { wp_set_current_user( self::$user_id ); @@ -168,12 +176,22 @@ public function test_update_comment_from_unprivileged_user_by_privileged_user() wp_set_current_user( 0 ); $comment = get_comment( $comment_id ); - $this->assertEqualHTML( - 'click', - $comment->comment_content, - '', - 'Comment: ' . $comment->comment_content - ); + + if ( is_multisite() ) { + $this->assertEqualHTML( + 'click', + $comment->comment_content, + '', + 'Comment: ' . $comment->comment_content + ); + } else { + $this->assertEqualHTML( + 'click', + $comment->comment_content, + '', + 'Comment: ' . $comment->comment_content + ); + } } /** diff --git a/tests/phpunit/tests/comment/wpUpdateComment.php b/tests/phpunit/tests/comment/wpUpdateComment.php new file mode 100644 index 0000000000000..c8d24dac52cf2 --- /dev/null +++ b/tests/phpunit/tests/comment/wpUpdateComment.php @@ -0,0 +1,112 @@ +post->create(); + self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); + self::$author_id = $factory->user->create( array( 'role' => 'author' ) ); + } + + public function tear_down() { + wp_set_current_user( 0 ); + parent::tear_down(); + } + + /** + * Updating a comment with a user lacking the unfiltered_html capability + * should filter the comment content. + * + * @ticket 57343 + */ + public function test_wp_update_comment_filters_content_without_unfiltered_html() { + $comment_id = self::factory()->comment->create( + array( + 'comment_post_ID' => self::$post_id, + 'user_id' => self::$author_id, + ) + ); + + wp_update_comment( + array( + 'comment_ID' => $comment_id, + 'comment_content' => 'A comment with a tag.', + ) + ); + + $this->assertSame( 'A comment with a tag.', get_comment( $comment_id )->comment_content ); + } + + /** + * A user with the unfiltered_html capability editing another user's comment + * should be able to save HTML, consistent with the post editor. + * + * @ticket 57343 + */ + public function test_wp_update_comment_preserves_html_for_editing_user_with_unfiltered_html() { + $comment_id = self::factory()->comment->create( + array( + 'comment_post_ID' => self::$post_id, + 'user_id' => self::$author_id, + ) + ); + + if ( is_multisite() ) { + // In multisite, only Super Admins have the unfiltered_html capability. + grant_super_admin( self::$admin_id ); + } + + wp_set_current_user( self::$admin_id ); + + $this->assertTrue( current_user_can( 'unfiltered_html' ) ); + + wp_update_comment( + array( + 'comment_ID' => $comment_id, + 'comment_content' => 'A comment with a tag.', + ) + ); + + $this->assertSame( + 'A comment with a tag.', + get_comment( $comment_id )->comment_content + ); + } + + /** + * Filtering is based on the editing user, not the comment author: content in + * a comment authored by a privileged user is still filtered when the update + * is performed without the unfiltered_html capability. + * + * @ticket 57343 + */ + public function test_wp_update_comment_filters_content_despite_privileged_comment_author() { + $comment_id = self::factory()->comment->create( + array( + 'comment_post_ID' => self::$post_id, + 'user_id' => self::$admin_id, + ) + ); + + // No logged-in user: a programmatic update keeps filtering by default. + wp_update_comment( + array( + 'comment_ID' => $comment_id, + 'comment_content' => 'A comment with a tag.', + ) + ); + + $this->assertSame( 'A comment with a tag.', get_comment( $comment_id )->comment_content ); + } +}