Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
d21a29b
Merge pull request #2 from WordPress/trunk
faisalahammad Apr 1, 2023
dd727aa
Merge branch 'WordPress:trunk' into trunk
faisalahammad Apr 2, 2023
656240e
Merge branch 'WordPress:trunk' into trunk
faisalahammad Apr 4, 2023
580f43d
Merge branch 'WordPress:trunk' into trunk
faisalahammad Apr 5, 2023
608e3d5
Merge branch 'WordPress:trunk' into trunk
faisalahammad Apr 11, 2023
ffaee64
Merge branch 'WordPress:trunk' into trunk
faisalahammad Jun 24, 2023
df714f0
Merge branch 'WordPress:trunk' into trunk
faisalahammad Jul 22, 2023
69a3788
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 2, 2024
1fe9e36
Merge branch 'WordPress:trunk' into trunk
faisalahammad Aug 4, 2026
c66b88f
Merge branch 'WordPress:trunk' into trunk
faisalahammad Aug 5, 2026
ebb35ce
Merge branch 'WordPress:trunk' into trunk
faisalahammad Aug 11, 2026
5bb2147
Merge branch 'WordPress:trunk' into trunk
faisalahammad Aug 21, 2026
e7fd0e5
Merge branch 'WordPress:trunk' into trunk
faisalahammad Aug 24, 2026
184d199
Merge branch 'WordPress:trunk' into trunk
faisalahammad Aug 26, 2026
67bb6ca
Merge branch 'WordPress:trunk' into trunk
faisalahammad Aug 28, 2026
630a7d7
Merge branch 'WordPress:trunk' into trunk
faisalahammad Aug 30, 2026
6053ef4
Merge branch 'WordPress:trunk' into trunk
faisalahammad Sep 7, 2026
26433a6
Merge branch 'WordPress:trunk' into trunk
faisalahammad Sep 9, 2026
b02375c
Merge branch 'WordPress:trunk' into trunk
faisalahammad Sep 14, 2026
a283758
Merge branch 'WordPress:trunk' into trunk
faisalahammad Sep 18, 2026
19ce386
Merge branch 'WordPress:trunk' into trunk
faisalahammad Sep 20, 2026
26ec101
Merge branch 'WordPress:trunk' into trunk
faisalahammad Sep 21, 2026
aefd6ca
Merge branch 'WordPress:trunk' into trunk
faisalahammad Sep 21, 2026
dc9985d
Merge branch 'WordPress:trunk' into trunk
faisalahammad Sep 22, 2026
9dcb18b
Merge branch 'WordPress:trunk' into trunk
faisalahammad Sep 22, 2026
b8422cc
Merge branch 'WordPress:trunk' into trunk
faisalahammad Sep 23, 2026
d698505
Merge branch 'WordPress:trunk' into trunk
faisalahammad Sep 23, 2026
b1d9192
Merge branch 'WordPress:trunk' into trunk
faisalahammad Sep 24, 2026
d7991f7
Merge branch 'WordPress:trunk' into trunk
faisalahammad Sep 25, 2026
e0350e5
Merge branch 'WordPress:trunk' into trunk
faisalahammad Sep 28, 2026
46c302b
Merge branch 'WordPress:trunk' into trunk
faisalahammad Sep 29, 2026
fb5b857
Merge branch 'WordPress:trunk' into trunk
faisalahammad Sep 30, 2026
f586aef
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 1, 2026
916dc81
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 2, 2026
903b778
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 4, 2026
8e4b0bb
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 5, 2026
8ac8df6
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 6, 2026
097f90b
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 6, 2026
f3a4ed5
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 7, 2026
b46ef06
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 7, 2026
5d34771
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 8, 2026
27f38ed
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 9, 2026
f0086c6
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 11, 2026
707cfba
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 12, 2026
4ee3e5b
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 12, 2026
90c2c4e
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 13, 2026
2cc94e2
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 14, 2026
684662f
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 14, 2026
842229e
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 15, 2026
9716298
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 16, 2026
3b8875d
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 17, 2026
49d8ba6
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 18, 2026
655b84d
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 19, 2026
f16f738
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 21, 2026
6bf818c
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 21, 2026
8cdbc36
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 22, 2026
2380c4b
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 22, 2026
e76b97c
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 23, 2026
5e3b78e
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 24, 2026
6323b93
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 25, 2026
44205f3
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 26, 2026
cd6bc9a
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 27, 2026
839b240
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 28, 2026
90e5715
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 29, 2026
ddcef6e
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 31, 2026
07db7cb
Merge branch 'WordPress:trunk' into trunk
faisalahammad Oct 31, 2026
ec4557c
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 2, 2026
4b9276a
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 3, 2026
7019d13
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 4, 2026
c4c69d4
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 5, 2026
5830ebd
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 6, 2026
00446e1
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 7, 2026
33d5643
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 8, 2026
d75d6ea
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 9, 2026
f27421a
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 9, 2026
b802023
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 10, 2026
61bab52
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 11, 2026
6ee38c6
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 12, 2026
be7fe3f
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 13, 2026
0400531
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 15, 2026
3e69d0a
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 16, 2026
b5649f4
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 17, 2026
2434c55
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 18, 2026
12b84ad
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 19, 2026
ac42b10
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 19, 2026
a71ffac
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 20, 2026
f74163c
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 21, 2026
6d51f70
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 22, 2026
d39053d
Merge branch 'WordPress:trunk' into trunk
faisalahammad Nov 23, 2026
a372a87
Abilities API: Let plugins add fields and query filters to content ab…
faisalahammad Nov 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 193 additions & 0 deletions src/wp-includes/abilities-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -895,3 +895,196 @@

return $registry->get_all_registered();
}

/**
* Registers a field that content abilities add to the posts they return.
*
* The field is returned under `extensions.<namespace>.<name>` for each post, and the
* `fields` input of the abilities accepts its path, such as `extensions.my-plugin.language`.
* It is not a default field, so a caller must request it, directly or through the
* `extensions` or `extensions.<namespace>` path. Abilities that return posts, such as
* `core/content-query`, read the registered fields when they are registered, so post
* fields must be registered on the `wp_abilities_api_extensions_init` action hook.
*
* Example:
*
* function my_plugin_register_post_fields(): void {
* wp_register_ability_post_field(
* 'my-plugin',
* 'language',
* array(
* 'schema' => array(
* 'type' => array( 'string', 'null' ),
* 'description' => __( 'The language slug of the post, such as "fi".', 'my-plugin' ),
* ),
* 'get_callback' => static function ( WP_Post $post ) {
* return my_plugin_get_post_language( $post->ID );
* },
* )
* );
* }
* add_action( 'wp_abilities_api_extensions_init', 'my_plugin_register_post_fields' );
*
* @since 7.3.0
*
* @param string $extension_namespace The namespace of the plugin that registers the field,
* such as `my-plugin`. Lowercase letters, digits, and dashes.
* @param string $name The field name. Lowercase letters, digits, dashes, and underscores.
* @param array<string, mixed> $args {
* Arguments for the field.
*
* @type array<string, mixed> $schema The JSON Schema of the field value, used in the
* output schema of the abilities.
* @type callable $get_callback Returns the field value for a post. Receives the
* WP_Post object, which is set up as the global post.
* Only called for posts the current user can read.
* @type string[] $object_types Optional. The post types the field applies to. The field
* is left out of posts of other post types. Default empty
* array, which applies to every post type.
* }
* @return bool True if the field was registered, false otherwise.
*/
function wp_register_ability_post_field( string $extension_namespace, string $name, array $args ): bool {
return _wp_register_ability_post_extension( 'fields', 'get_callback', __FUNCTION__, $extension_namespace, $name, $args );
}

/**
* Registers a query filter that content abilities accept for the posts they query.
*
* The filter is read from `where.extensions.<namespace>.<name>` in the input of
* abilities that query posts, such as `core/content-query`. Its callback turns the
* value into a limited set of query arguments, which the ability joins with its own
* filters and with the other query filters, so a post must match all of them. The
* ability applies them to every query it runs, so the totals match the results.
* Query filters must be registered on the `wp_abilities_api_extensions_init` action hook.
*
* The callback receives the filter value, already validated against the schema, and the
* WP_Post_Type object of the queried post type. It returns an array with any of the
* following WP_Query arguments, or a WP_Error to refuse the query:
*
* - `meta_query` and `tax_query`: added as a nested clause, joined with AND.
* - `post__in`: intersected with the IDs the other filters allow. An empty list matches no post.
* - `post__not_in`: merged with the IDs the other filters exclude.
*
* Example:
*
* function my_plugin_register_post_query_filters(): void {
* wp_register_ability_post_query_filter(
* 'my-plugin',
* 'language',
* array(
* 'schema' => array(
* 'type' => 'string',
* 'minLength' => 1,
* 'description' => __( 'Only return posts in this language, such as "fi".', 'my-plugin' ),
* ),
* 'callback' => static function ( string $language ): array {
* return array(
* 'tax_query' => array(
* array(
* 'taxonomy' => 'language',
* 'field' => 'slug',
* 'terms' => $language,
* ),
* ),
* );
* },
* )
* );
* }
* add_action( 'wp_abilities_api_extensions_init', 'my_plugin_register_post_query_filters' );
*
* @since 7.3.0
*
* @param string $extension_namespace The namespace of the plugin that registers the filter,
* such as `my-plugin`. Lowercase letters, digits, and dashes.
* @param string $name The filter name. Lowercase letters, digits, dashes, and underscores.
* @param array<string, mixed> $args {
* Arguments for the query filter.
*
* @type array<string, mixed> $schema The JSON Schema of the filter value, used in the
* input schema of the abilities.
* @type callable $callback Returns the query arguments for a filter value, or a
* WP_Error to refuse the query.
* @type string[] $object_types Optional. The post types the filter applies to. A query
* of another post type that sets the filter is refused.
* Default empty array, which applies to every post type.
* }
* @return bool True if the query filter was registered, false otherwise.
*/
function wp_register_ability_post_query_filter( string $extension_namespace, string $name, array $args ): bool {
return _wp_register_ability_post_extension( 'query_filters', 'callback', __FUNCTION__, $extension_namespace, $name, $args );
}

/**
* Registers a post field or a post query filter for content abilities.
*
* @since 7.3.0
* @access private
*
* @global array $wp_ability_post_extensions Registered post fields and query filters, keyed by
* kind, then namespace, then name.
*
* @param string $kind The kind of extension: `fields` or `query_filters`.
* @param string $callback_key The argument that holds the callback.
* @param string $function_name The public function that was called, for notices.
* @param string $extension_namespace The extension namespace.
* @param string $name The extension name.
* @param array<string, mixed> $args The extension arguments.
* @return bool True if the extension was registered, false otherwise.
*/
function _wp_register_ability_post_extension( string $kind, string $callback_key, string $function_name, string $extension_namespace, string $name, array $args ): bool {
global $wp_ability_post_extensions;

Check warning on line 1037 in src/wp-includes/abilities-api.php

View workflow job for this annotation

GitHub Actions / PHP static analysis / Run PHP static analysis (strict paths)

PHPStan (strict, level 10): missingType.iterableValue

PHPDoc tag @var for variable $wp_ability_post_extensions has no value type specified in iterable type array. See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type

$error = '';
if ( ! doing_action( 'wp_abilities_api_extensions_init' ) ) {
/* translators: %s: wp_abilities_api_extensions_init */
$error = sprintf( __( 'Ability extensions must be registered on the %s action.' ), '<code>wp_abilities_api_extensions_init</code>' );
} elseif ( ! preg_match( '/^[a-z0-9-]+$/', $extension_namespace ) ) {
$error = __( 'The namespace must contain only lowercase letters, digits, and dashes.' );
} elseif ( ! preg_match( '/^[a-z0-9_-]+$/', $name ) ) {
$error = __( 'The name must contain only lowercase letters, digits, dashes, and underscores.' );
} elseif ( isset( $wp_ability_post_extensions[ $kind ][ $extension_namespace ][ $name ] ) ) {

Check warning on line 1047 in src/wp-includes/abilities-api.php

View workflow job for this annotation

GitHub Actions / PHP static analysis / Run PHP static analysis (strict paths)

PHPStan (strict, level 10): offsetAccess.nonOffsetAccessible

Cannot access offset non-empty-string on mixed.

Check warning on line 1047 in src/wp-includes/abilities-api.php

View workflow job for this annotation

GitHub Actions / PHP static analysis / Run PHP static analysis (strict paths)

PHPStan (strict, level 10): offsetAccess.nonOffsetAccessible

Cannot access offset non-empty-string on mixed.
$error = __( 'An extension with this namespace and name is already registered.' );
} elseif ( empty( $args['schema'] ) || ! is_array( $args['schema'] ) ) {
/* translators: %s: schema */
$error = sprintf( __( 'The %s argument must be a non-empty array.' ), '<code>schema</code>' );
} elseif ( ! isset( $args[ $callback_key ] ) || ! is_callable( $args[ $callback_key ] ) ) {
/* translators: %s: Argument name. */
$error = sprintf( __( 'The %s argument must be a valid callback.' ), '<code>' . $callback_key . '</code>' );
} elseif ( isset( $args['object_types'] ) && ( ! is_array( $args['object_types'] ) || array_filter( $args['object_types'], 'is_string' ) !== $args['object_types'] ) ) {
/* translators: %s: object_types */
$error = sprintf( __( 'The %s argument must be an array of post type names.' ), '<code>object_types</code>' );
}

if ( '' !== $error ) {
_doing_it_wrong( $function_name, $error, '7.3.0' );
return false;
}

$wp_ability_post_extensions[ $kind ][ $extension_namespace ][ $name ] = array(

Check warning on line 1065 in src/wp-includes/abilities-api.php

View workflow job for this annotation

GitHub Actions / PHP static analysis / Run PHP static analysis (strict paths)

PHPStan (strict, level 10): offsetAccess.nonOffsetAccessible

Cannot access offset string on mixed.

Check warning on line 1065 in src/wp-includes/abilities-api.php

View workflow job for this annotation

GitHub Actions / PHP static analysis / Run PHP static analysis (strict paths)

PHPStan (strict, level 10): offsetAccess.nonOffsetAccessible

Cannot access offset string on mixed.
'schema' => $args['schema'],
'callback' => $args[ $callback_key ],
'object_types' => array_values( $args['object_types'] ?? array() ),

Check warning on line 1068 in src/wp-includes/abilities-api.php

View workflow job for this annotation

GitHub Actions / PHP static analysis / Run PHP static analysis (strict paths)

PHPStan (strict, level 10): argument.type

Parameter #1 $input of function array_values expects array<mixed>, mixed given.
);

return true;
}

/**
* Returns the registered post fields or post query filters for content abilities.
*
* @since 7.3.0
* @access private
*
* @global array $wp_ability_post_extensions Registered post fields and query filters.
*
* @param string $kind The kind of extension: `fields` or `query_filters`.
* @return array<string, array<string, array{schema: array<string, mixed>, callback: callable, object_types: list<string>}>>
* The extensions, keyed by namespace, then name.
*/
function _wp_get_ability_post_extensions( string $kind ): array {
global $wp_ability_post_extensions;

Check warning on line 1087 in src/wp-includes/abilities-api.php

View workflow job for this annotation

GitHub Actions / PHP static analysis / Run PHP static analysis (strict paths)

PHPStan (strict, level 10): missingType.iterableValue

PHPDoc tag @var for variable $wp_ability_post_extensions has no value type specified in iterable type array. See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type

return $wp_ability_post_extensions[ $kind ] ?? array();

Check warning on line 1089 in src/wp-includes/abilities-api.php

View workflow job for this annotation

GitHub Actions / PHP static analysis / Run PHP static analysis (strict paths)

PHPStan (strict, level 10): return.type

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.
}
11 changes: 11 additions & 0 deletions src/wp-includes/abilities-api/class-wp-abilities-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,17 @@ public static function get_instance(): ?self {
// before abilities that depend on them.
WP_Ability_Categories_Registry::get_instance();

/**
* Fires before abilities are registered, so that extensions to them can be registered.
*
* Post fields and post query filters must be registered on this action with
* wp_register_ability_post_field() and wp_register_ability_post_query_filter(), since
* the abilities that return posts read them when they are registered.
*
* @since 7.3.0
*/
do_action( 'wp_abilities_api_extensions_init' );

/**
* Fires when preparing abilities registry.
*
Expand Down
Loading
Loading