Skip to content

Restrict note editing capabilities to their authors or comment moderators#11191

Draft
mindctrl wants to merge 8 commits intoWordPress:trunkfrom
mindctrl:trac-64779/note-capabilities
Draft

Restrict note editing capabilities to their authors or comment moderators#11191
mindctrl wants to merge 8 commits intoWordPress:trunkfrom
mindctrl:trac-64779/note-capabilities

Conversation

@mindctrl
Copy link

@mindctrl mindctrl commented Mar 6, 2026

Notes (internal collaborative comments on posts) currently inherit the same capability mapping as regular comments, meaning anyone who can edit the parent post can edit or delete any note on it.

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

Use of AI Tools

Opus 4.6 helped with this, particularly the phpunit tests.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions
Copy link

github-actions bot commented Mar 6, 2026

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

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

Props mindctrl, westonruter.

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

@github-actions
Copy link

github-actions bot commented Mar 6, 2026

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.

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Just some notes nits

mindctrl and others added 2 commits March 9, 2026 10:06
Co-authored-by: Weston Ruter <westonruter@gmail.com>
Co-authored-by: Weston Ruter <westonruter@gmail.com>
@mindctrl mindctrl requested a review from westonruter March 9, 2026 14:07
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR changes WordPress note (internal comment) capability mapping so only the note author or users who can moderate comments can edit/delete a note, while ensuring authorized collaborators can still read notes via REST.

Changes:

  • Update map_meta_cap() to apply note-specific rules for edit_comment and delete_comment.
  • Adjust REST comments controller note read/access behavior for context=edit and note reading.
  • Add PHPUnit coverage for note edit/delete capability mapping and REST update/delete/read scenarios.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
tests/phpunit/tests/user/capabilities.php Adds meta-cap and role behavior tests for note edit/delete and regular comment behavior.
tests/phpunit/tests/rest-api/rest-comments-controller.php Adds REST API tests ensuring non-authors can’t edit/delete notes, while authors/moderators can (and reads remain possible for post editors).
src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php Adjusts note read permission logic and edit-context permission behavior in REST.
src/wp-includes/capabilities.php Introduces note-specific meta-cap mapping for editing/deleting notes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +580 to +594
if ( 'note' === $comment->comment_type ) {
if ( (int) $user_id === (int) $comment->user_id ) {
$post = get_post( $comment->comment_post_ID );
if ( $post ) {
$post_type = get_post_type_object( $post->post_type );
if ( $post_type ) {
$caps[] = $post_type->cap->edit_posts;
break;
}
}
$caps[] = 'edit_posts';
} else {
$caps[] = 'moderate_comments';
}
break;
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

For note authors, this maps edit_comment/delete_comment to the post type’s primitive edit_posts cap (or generic edit_posts). That bypasses the per-post rules enforced by edit_post mapping (e.g., edit_published_posts, edit_others_posts, status transitions), so a user could still edit/delete their note even when they can no longer edit the parent post (e.g., the post becomes publish). Consider delegating to the existing per-post mapping (similar to regular comments) so note authors require the same effective capabilities as editing the associated post, while non-authors still require moderate_comments.

Copilot uses AI. Check for mistakes.
@mindctrl mindctrl marked this pull request as draft March 9, 2026 17:44
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.

3 participants