Repository navigation
Abilities API: Let plugins add fields and query filters to content abilities - #16174
faisalahammad wants to merge 90 commits into
Conversation
…ilities. Adds `wp_register_ability_post_field()` and `wp_register_ability_post_query_filter()`, registered on the new `wp_abilities_api_extensions_init` action, which fires before abilities are registered. `core/content-query` adds registered fields to its output under `extensions.<namespace>.<name>`, and accepts registered filters under `where.extensions.<namespace>.<name>`. The schemas stay closed, so unregistered keys still fail validation, and they do not change when nothing is registered. A query filter returns only `meta_query`, `tax_query`, `post__in` or `post__not_in`. These are joined with AND with the core filters and the other query filters before the query runs, so the totals match the results. A filter that does not apply to the queried post type is refused. See #66550.
|
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: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
|
| Location | Error | Identifier | Level | First reported |
|---|---|---|---|---|
src/wp-includes/abilities-api.php:1037 |
PHPDoc tag @var for variable $wp_ability_post_extensions has no value type specified in iterable type array. |
missingType.iterableValue |
10 | a372a87 |
src/wp-includes/abilities-api.php:1047 |
Cannot access offset non-empty-string on mixed. |
offsetAccess.nonOffsetAccessible |
10 | a372a87 |
src/wp-includes/abilities-api.php:1047 |
Cannot access offset non-empty-string on mixed. |
offsetAccess.nonOffsetAccessible |
10 | a372a87 |
src/wp-includes/abilities-api.php:1065 |
Cannot access offset string on mixed. |
offsetAccess.nonOffsetAccessible |
10 | a372a87 |
src/wp-includes/abilities-api.php:1065 |
Cannot access offset string on mixed. |
offsetAccess.nonOffsetAccessible |
10 | a372a87 |
src/wp-includes/abilities-api.php:1068 |
Parameter #1 $input of function array_values expects array<mixed>, mixed given. |
argument.type |
10 | a372a87 |
src/wp-includes/abilities-api.php:1087 |
PHPDoc tag @var for variable $wp_ability_post_extensions has no value type specified in iterable type array. |
missingType.iterableValue |
10 | a372a87 |
src/wp-includes/abilities-api.php:1089 |
Function _wp_get_ability_post_extensions() should return array<string, array<string, array{schema: array<string, mixed>, callback: callable(): mixed, object_types: list<string>}>> but returns mixed. |
return.type |
10 | a372a87 |
Running PHPStan locally
To run the analysis on your own machine, use npm run typecheck:php, or composer phpstan outside the Docker environment.
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. |
Prototype for discussion. It explores one shape for letting plugins extend
core/content-query, using the multilingual case from the ticket as the test.What it adds
wp_register_ability_post_field( $namespace, $name, $args ): adds a field to each returned post underextensions.<namespace>.<name>. It is returned only when requested infields(extensions,extensions.<namespace>, or the full path).wp_register_ability_post_query_filter( $namespace, $name, $args ): adds a filter underwhere.extensions.<namespace>.<name>. The callback receives the value (already validated and converted to its schema type) and the post type object. It returns onlymeta_query,tax_query,post__inorpost__not_in, or aWP_Errorto refuse the query.wp_abilities_api_extensions_initaction, fired right beforewp_abilities_api_init, so the extensions exist whencore/content-querybuilds its schemas.How core applies them
additionalProperties: false). Registered extensions are listed in them, so unknown keys still fail validation. With nothing registered, the schemas don't change.WP_Queryruns. The count query starts from the same args, sototalandtotal_pagesstay correct.post__inlists are intersected, an empty result matches no post, andpost__not_inis applied topost__insinceWP_Queryignores it when both are set.object_typeslimits a field or filter to some post types. A field is left out for other types; a filter is refused, so it can't silently widen the query.Example (Polylang-style)
Input:
{ "where": { "type": "page", "parent": 0, "extensions": { "polylang": { "language": "en" } } }, "fields": [ "title", "extensions.polylang.language" ] }Not included
set_callback, validators, syncs) for future create/update abilities.Testing
npm run test:php -- --filter Tests_Abilities_API_WpRegisterAbilityPostExtensions(25 tests) and--group abilities-api.Trac ticket: https://core.trac.wordpress.org/ticket/66550
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Code and tests drafted with AI assistance; I reviewed, ran and edited the final implementation 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.