Skip to content

Comments: Filter wp_update_comment() content by the editing user's unfiltered_html capability - #16141

Open
MoS2mir wants to merge 5 commits into
WordPress:trunkfrom
MoS2mir:trac-57343
Open

MoS2mir wants to merge 5 commits into
WordPress:trunkfrom
MoS2mir:trac-57343

Conversation

@MoS2mir

@MoS2mir MoS2mir commented Nov 21, 2026

Copy link
Copy Markdown

What

Trac ticket: https://core.trac.wordpress.org/ticket/57343

wp_update_comment() decides whether to run wp_filter_kses() on the comment content based on the comment author's unfiltered_html capability. As a result, when an administrator edits another user's comment, any HTML beyond the author's allowed markup (images, classes, scripts) is stripped on save.

This PR bases the decision on the capability of the user performing the update instead, consistent with the post editor behavior (as noted by @westonruter on the ticket): a non-admin's post edited by an admin keeps the admin's markup, and is filtered again if the non-admin edits it. Comments now behave the same way.

Behavior

  • Admin (or any user with unfiltered_html) editing another user's comment: HTML is preserved.
  • User without unfiltered_html editing any comment: content is filtered, regardless of who authored the comment.
  • Programmatic wp_update_comment() calls with no logged-in user continue to filter by default (current_user_can( 'unfiltered_html' ) is false), preserving existing behavior for cron jobs, importers, and other unattended code.
  • On multisite, where no user has unfiltered_html, content is still filtered, as before.

Tests

New tests/phpunit/tests/comment/wpUpdateComment.php with three tests:

  1. Filtering applies when the updating user lacks unfiltered_html (unchanged default behavior).
  2. HTML is preserved when a user with unfiltered_html edits another user's comment (the bug; fails on trunk).
  3. Filtering applies to a privileged author's comment when the update is performed without the capability (documents the intentional switch from author-based to editor-based filtering).

The existing Tests_Comment::test_update_comment_from_unprivileged_user_by_privileged_user encoded the old author-based behavior and is updated to expect the preserved markup on single site (filtered on multisite).

Test results

  • New tests: 3/3 pass with the patch; test 2 fails on trunk, proving the bug.
  • Tests_Comment group: 828 tests, 1900 assertions, OK.
  • WP_Test_REST_Comments_Controller: 291 tests, 1337 assertions, OK.

…filtered_html capability. See #57343.

Change comment filtering logic to use current user capability instead of comment author capability.
…tering. 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.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props mosamirdev.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

…site. See #57343.

Added a check for multisite to grant super admin capability before updating a comment.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant