Comments: Make REST comment moderation comment-type aware (Trac #35214, Stage 3)#56
Conversation
`WP_REST_Comments_Controller::check_edit_permission()` short-circuited on the global `moderate_comments` primitive, so a moderator of a custom comment type could not edit or delete comments of that type via REST, while a global moderator could act on every type regardless of its capability model. Route the moderator shortcut through the `moderate_comment` meta capability instead. For comment types using the default capability model this resolves to `moderate_comments` (behavior unchanged), while a type with its own capabilities is gated by its own moderation primitive. Apply the same change to the orphaned-comment branch of `check_read_permission()`. This consumes the meta capabilities added in the previous commit; it makes no change for the built-in comment, pingback, trackback, or note types. See #35214.
Dispatch real update and delete requests through the comments controller to prove `check_edit_permission()` honors per-type capabilities: a moderator (or editor) of an independent `review` type can edit and delete review comments, a global `moderate_comments` moderator cannot touch them, and the default and built-in ping types behave exactly as before. See #35214.
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Stage 3 of capability enforcement (follow-up to PR #55): start rerouting real moderation call sites through the new
moderate_commentmeta capability, beginning with the REST API - the cleanest, most self-contained, most testable surface.WP_REST_Comments_Controller::check_edit_permission()(the gate for REST update and delete) short-circuited on the globalmoderate_commentsprimitive:That had two problems for a comment type with an independent capability model: a moderator of that type (holding e.g.
moderate_reviews) could not act on its comments, while any global moderator could act on every type regardless of its model.What changed
One-line semantic change in two spots: route the moderator shortcut through the per-comment
moderate_commentmeta cap (added in #55) instead of the global primitive.check_edit_permission()- the update/delete gate.check_read_permission()- the orphaned-comment branch.For the default capability model
moderate_commentresolves tomoderate_comments, so built-incomment/pingback/trackback/noteare completely unchanged. A type with its owncapability_typeis gated by its own moderation primitive.Why this is the whole REST surface (mostly)
Most per-comment REST gates already call
current_user_can( 'edit_comment', $id ), which #55 made type-aware - so they needed no change. The remaining baremoderate_commentschecks in the controller are cross-cutting / field-level (collection edit context, settingauthor/author_ip), where per-single-comment semantics don't apply; those are intentionally left as the global primitive.Testing
New
tests/phpunit/tests/rest-api/rest-comment-type-moderation.phpdispatches realPOST(update) andDELETErequests:reviewmoderator (moderate_reviews) and arevieweditor (edit_others_reviews) can update/delete review comments.moderate_commentsmoderator gets 403 on review comments (independence proven at the REST layer) - and a review moderator gets 403 on default comments (the inverse).pingbacktype, still update/delete fine for a global moderator (back-compat).Full
--group comment --group capabilities --group restapipasses (the only failure is a pre-existing environmental oEmbed flake unrelated to comments). PHPCS + PHPStan clean.Remaining Stage 3 families (future PRs)
Same pattern, one focused PR each so every surface gets its own security review: admin (
wp-admin/comment.phpis alreadyedit_comment-gated; list-table bulk/ajax-actions.phpmoderation fallback at line 1000), and XML-RPC (wp.editCommentstatus change). These reach the samemap_meta_cap()foundation.Stacking
Based on
feature/comment-type-cap-enforcement(#55). Retarget totrunkas the stack lands behind #12311.See #35214.