Skip to content

Abilities API: Let plugins add fields and query filters to content abilities - #16174

Open
faisalahammad wants to merge 90 commits into
WordPress:trunkfrom
faisalahammad:ticket/66550-content-query-extensions
Open

faisalahammad wants to merge 90 commits into
WordPress:trunkfrom
faisalahammad:ticket/66550-content-query-extensions

Conversation

@faisalahammad

Copy link
Copy Markdown

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 under extensions.<namespace>.<name>. It is returned only when requested in fields (extensions, extensions.<namespace>, or the full path).
  • wp_register_ability_post_query_filter( $namespace, $name, $args ): adds a filter under where.extensions.<namespace>.<name>. The callback receives the value (already validated and converted to its schema type) and the post type object. It returns only meta_query, tax_query, post__in or post__not_in, or a WP_Error to refuse the query.
  • A new wp_abilities_api_extensions_init action, fired right before wp_abilities_api_init, so the extensions exist when core/content-query builds its schemas.

How core applies them

  • The schemas stay closed (additionalProperties: false). Registered extensions are listed in them, so unknown keys still fail validation. With nothing registered, the schemas don't change.
  • Query filter results are joined with AND with the core filters and with each other, before WP_Query runs. The count query starts from the same args, so total and total_pages stay correct. post__in lists are intersected, an empty result matches no post, and post__not_in is applied to post__in since WP_Query ignores it when both are set.
  • object_types limits 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.
  • Extension fields prime the meta and term caches, since they usually read them.

Example (Polylang-style)

add_action( 'wp_abilities_api_extensions_init', function () {
	wp_register_ability_post_field( 'polylang', 'language', array(
		'schema'       => array( 'type' => array( 'string', 'null' ) ),
		'get_callback' => fn( WP_Post $post ) => pll_get_post_language( $post->ID ) ?: null,
	) );
	wp_register_ability_post_query_filter( 'polylang', 'language', array(
		'schema'   => array( 'type' => 'string', 'minLength' => 1 ),
		'callback' => fn( string $lang ) => array(
			'tax_query' => array( array( 'taxonomy' => 'language', 'field' => 'slug', 'terms' => $lang ) ),
		),
	) );
} );

Input: { "where": { "type": "page", "parent": 0, "extensions": { "polylang": { "language": "en" } } }, "fields": [ "title", "extensions.polylang.language" ] }

Not included

  • Write support (set_callback, validators, syncs) for future create/update abilities.
  • A general resource model for plugin-defined abilities. The registry is posts only for now.

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.

…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.
@github-actions

Copy link
Copy Markdown

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:

Props faisalahammad.

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

⚠️ PHPStan Static Analysis

PHPStan reported 8 errors when analyzing a372a87 (workflow run). This comment is updated as new commits are pushed.

An error at level 10 is in a path that phpstan-strict.neon.dist holds to rule level 10, which the rest of the codebase is not yet held to. See Stricter analysis of selected paths.

Errors

These are also shown as annotations on the changed files. They do not fail the check, but they should still be fixed before the pull request is committed. An error that is a false positive may instead warrant being added to a baseline.

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.

@github-actions

Copy link
Copy Markdown

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.

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.

1 participant