Repository navigation
Conversation
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
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. |
19e7314 to
d8e0ffe
Compare
Adds a `required_capabilities` argument to `wp_register_ability()`. It is a list of capabilities the current user must have to run the ability. `WP_Ability::check_permissions()` checks these capabilities first. The `permission_callback` only runs when they all pass. The callback is optional when the list is not empty. `wp_get_abilities()` accepts a new `current_user_can` argument. It leaves out abilities that declare capabilities the current user doesn't have, without calling any permission callbacks. The REST list route gets a matching query parameter and includes `required_capabilities` in the response. Core abilities that only check `manage_options` now use the new argument. See #66325. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lity filter. See #66325. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Uses shorter and more consistent wording for `permission_callback`, `required_capabilities`, and the `has_required_capabilities` filter. Removes reasoning that was repeated in several places. See #66325. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merges similar tests into data providers and groups the helpers. Keeps the ticket reference only on newly added tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
d8e0ffe to
8f7d992
Compare
Add `required_capabilities` to ability registration and expose it in REST responses. Check these capabilities before permission callbacks, allowing the callback to be omitted when capabilities are declared. Add `has_required_capabilities` filtering to `wp_get_abilities()` and the REST collection endpoint without running permission callbacks. Developed in: #14335 Props ovidiu-galatan. Fixes #66325. git-svn-id: https://develop.svn.wordpress.org/trunk@64500 602fd350-edb4-49c9-b593-d223f7449a82
Add `required_capabilities` to ability registration and expose it in REST responses. Check these capabilities before permission callbacks, allowing the callback to be omitted when capabilities are declared. Add `has_required_capabilities` filtering to `wp_get_abilities()` and the REST collection endpoint without running permission callbacks. Developed in: WordPress/wordpress-develop#14335 Props ovidiu-galatan. Fixes #66325. Built from https://develop.svn.wordpress.org/trunk@64500 git-svn-id: http://core.svn.wordpress.org/trunk@63650 1a063a9b-81f0-0310-95a4-ce76da25c4cd
What
Abilities can now declare the capabilities a user needs to run them:
Today
wp_get_abilities()lists every ability, even the ones the current user can't run. With this change, callers like AI agents and MCP adapters can ask for only the abilities the current user can use. No permission callbacks need to run for that.How it works
Registration
required_capabilitiesis a top-level argument, likepermission_callback. It must be a list of non-empty strings.permission_callbackis required unlessrequired_capabilitieslists at least one capability.permission_callback. To let everyone run an ability, use'permission_callback' => '__return_true'. Thepublicmeta flag doesn't affect this. It only controls where the ability is shown.permission_callbackthat is set must be callable, even when capabilities are listed.Execution
check_permissions( $input )combines both checks with AND:current_user_can(). If one is missing, the check fails and the callback doesn't run.permission_callback, it runs with$input, as it does today.wp_ability_permission_resultfilter runs on the final result, including a failure from step 1.If only one of the two is set, that one decides. Custom
ability_classsubclasses skip the registration checks. For them,check_permissions()returns theability_invalid_permission_callbackerror when the callback is set but not callable, or when both the callback and the capabilities are missing.Listing
wp_get_abilities()accepts'has_required_capabilities' => true. It leaves out abilities whose required capabilities the current user doesn't have.required_capabilitiesstay in the result, because their permission may depend on the input.category,namespace, andmetafilters and beforeitem_include_callback.wp-abilities/v1/abilitiesroute accepts a matchinghas_required_capabilitiesquery parameter.required_capabilities, so JavaScript clients can hide UI for abilities the user can't run.Core abilities
core/get-site-info,core/get-environment-info, andcore/settings-getonly checkedmanage_options. They now userequired_capabilitiesinstead of a closure.New public API
required_capabilitiesregistration argument.WP_Ability::get_required_capabilities(): array.has_required_capabilitiesargument forwp_get_abilities()and the REST list route.required_capabilitiesfield in the REST response.Testing
Follow-ups
@wordpress/abilitiesand@wordpress/core-abilities) live in the Gutenberg repository and will follow separately.Trac ticket: https://core.trac.wordpress.org/ticket/66325
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Implementation and tests, based on the design in the Trac ticket. I reviewed and adjusted the code and 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.
🤖 Generated with Claude Code