From 5eaf997602d207574948e3e27ab5cb0bb54512cd Mon Sep 17 00:00:00 2001 From: Mohamed Samir <167022012+MoS2mir@users.noreply.github.com> Date: Sat, 21 Nov 2026 07:28:17 +0200 Subject: [PATCH 1/5] Add unit tests for wp_update_comment functionality --- .../phpunit/tests/comment/wpUpdateComment.php | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 tests/phpunit/tests/comment/wpUpdateComment.php diff --git a/tests/phpunit/tests/comment/wpUpdateComment.php b/tests/phpunit/tests/comment/wpUpdateComment.php new file mode 100644 index 0000000000000..cb3931bea4cd8 --- /dev/null +++ b/tests/phpunit/tests/comment/wpUpdateComment.php @@ -0,0 +1,105 @@ +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, + ) + ); + + wp_set_current_user( self::$admin_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 + ); + } + + /** + * 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 ); + } +} From f4fb4562cb7b42d615f45f4acda468c62cc9a561 Mon Sep 17 00:00:00 2001 From: Mohamed Samir <167022012+MoS2mir@users.noreply.github.com> Date: Sat, 21 Nov 2026 07:31:29 +0200 Subject: [PATCH 2/5] Comments: Filter wp_update_comment() content by the editing user's unfiltered_html capability. See #57343. Change comment filtering logic to use current user capability instead of comment author capability. --- src/wp-includes/comment.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 ) { From 0c6f939d643b08801f923a16749ff9f42ca53c6a Mon Sep 17 00:00:00 2001 From: Mohamed Samir <167022012+MoS2mir@users.noreply.github.com> Date: Sat, 21 Nov 2026 07:33:18 +0200 Subject: [PATCH 3/5] Comments: Update Tests_Comment expectations for editor-capability filtering. See #57343. Added a test to verify that a privileged user can edit an unprivileged user's comment with HTML content, ensuring proper filtering behavior on multisite. --- tests/phpunit/tests/comment.php | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) 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 + ); + } } /** From e7c62b06a2c006d7bd121862fdd3fc00c2940648 Mon Sep 17 00:00:00 2001 From: Mohamed Samir <167022012+MoS2mir@users.noreply.github.com> Date: Sat, 21 Nov 2026 07:45:30 +0200 Subject: [PATCH 4/5] Comments: Align assignment operators in Tests_Comment_wpUpdateComment. See #57343. --- tests/phpunit/tests/comment/wpUpdateComment.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/comment/wpUpdateComment.php b/tests/phpunit/tests/comment/wpUpdateComment.php index cb3931bea4cd8..c4b33b87c6845 100644 --- a/tests/phpunit/tests/comment/wpUpdateComment.php +++ b/tests/phpunit/tests/comment/wpUpdateComment.php @@ -14,8 +14,8 @@ class Tests_Comment_wpUpdateComment extends WP_UnitTestCase { protected static $author_id; public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { - self::$post_id = $factory->post->create(); - self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); + self::$post_id = $factory->post->create(); + self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); self::$author_id = $factory->user->create( array( 'role' => 'author' ) ); } From 24545b065baebc690769871d96a4f99a429b5565 Mon Sep 17 00:00:00 2001 From: Mohamed Samir <167022012+MoS2mir@users.noreply.github.com> Date: Sat, 21 Nov 2026 08:20:54 +0200 Subject: [PATCH 5/5] Comments: Grant Super Admin in Tests_Comment_wpUpdateComment on multisite. See #57343. Added a check for multisite to grant super admin capability before updating a comment. --- tests/phpunit/tests/comment/wpUpdateComment.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/phpunit/tests/comment/wpUpdateComment.php b/tests/phpunit/tests/comment/wpUpdateComment.php index c4b33b87c6845..c8d24dac52cf2 100644 --- a/tests/phpunit/tests/comment/wpUpdateComment.php +++ b/tests/phpunit/tests/comment/wpUpdateComment.php @@ -62,8 +62,15 @@ public function test_wp_update_comment_preserves_html_for_editing_user_with_unfi ) ); + 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,