Abilities API: Add an eligibility callback for situational relevance - #13356
Draft
gziolo wants to merge 2 commits into
Draft
Abilities API: Add an eligibility callback for situational relevance#13356gziolo wants to merge 2 commits into
gziolo wants to merge 2 commits into
Conversation
An ability can declare an eligibility_callback at registration. The callback receives an associative array describing the caller's usage context and returns whether the ability applies there. wp_get_abilities() accepts an eligibility_context argument and drops abilities whose callback returns false. The abilities REST collection accepts the same context through an eligibility_context query parameter. Eligibility is consulted only when listing abilities, never on execute, and it is not a security boundary. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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. |
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
This is a draft to explore the design in the open.
An ability can declare an
eligibility_callbackwhen it is registered. The callback receives an array that describes where the caller is. It returns true or false to say if the ability is useful there.wp_get_abilities()accepts a neweligibility_contextargument. Abilities whose callback returns false are left out. The REST collection accepts the same data through aneligibility_contextquery parameter.The first user of this feature is a WebMCP adapter (WordPress/ai#448). It registers tools for each admin screen and each frontend page. A site with hundreds of abilities can then expose only the few that matter on the current page.
Contract
Three things stay separate:
permission_callbackwhen the ability runs.Eligibility is not a security check. The callback runs only when listing abilities. It never runs when an ability executes. It never runs on the single ability REST route. The context comes from the caller and is not verified. Nothing security related may depend on it.
The result without a context is the upper bound. With no context, callbacks are not called and every ability is included. Context keys can only remove abilities from a result. They can never add more. This keeps the MCP adapter's
tools/listcomplete, because it passes no context.An ability without a callback is always included. A callback that returns something that is not a boolean is treated as true.
Core does not define the context keys. Callers and ability authors agree on them. Plugins should prefix their own keys, like
plugin-slug/key. The REST parameter declares no properties, so values arrive as strings. A plugin can declare typed keys with the existingrest_abilities_collection_paramsfilter.New API
eligibility_callbackregistration argument. Must be callable.WP_Ability::is_eligible( array $eligibility_context = array() ): bool, with awp_ability_eligibility_resultfilter. It follows the same pattern as the other lifecycle filters.eligibility_contextargument forwp_get_abilities(). It runs in the same loop, after thecategory,namespace, andmetafilters and beforeitem_include_callback. Thewp_get_abilities_item_includefilter can see it through$args.eligibility_contextobject parameter on thewp-abilities/v1/abilitiesroute. Bracket syntax works:eligibility_context[post_type]=product.Open questions
@ticketannotations once it does.@wordpress/abilitiesand@wordpress/core-abilities) lives in the Gutenberg repository and will follow separately.🤖 Generated with Claude Code