Skip to content
122 changes: 72 additions & 50 deletions src/wp-includes/abilities-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,23 @@
* wp_register_ability(
* 'my-plugin/export-users',
* array(
* 'label' => __( 'Export Users', 'my-plugin' ),
* 'description' => __( 'Exports user data to CSV format.', 'my-plugin' ),
* 'category' => 'data-export',
* 'execute_callback' => 'my_plugin_export_users',
* 'permission_callback' => function(): bool {
* return current_user_can( 'export' );
* },
* 'input_schema' => array(
* 'label' => __( 'Export Users', 'my-plugin' ),
* 'description' => __( 'Exports user data to CSV format.', 'my-plugin' ),
* 'category' => 'data-export',
* 'execute_callback' => 'my_plugin_export_users',
* 'required_capabilities' => array( 'export' ),
* 'input_schema' => array(
* 'type' => 'string',
* 'enum' => array( 'subscriber', 'contributor', 'author', 'editor', 'administrator' ),
* 'description' => __( 'Limits the export to users with this role.', 'my-plugin' ),
* 'required' => false,
* ),
* 'output_schema' => array(
* 'output_schema' => array(
* 'type' => 'string',
* 'description' => __( 'User data in CSV format.', 'my-plugin' ),
* 'required' => true,
* ),
* 'meta' => array(
* 'meta' => array(
* 'public' => true,
* ),
* )
Expand Down Expand Up @@ -234,6 +232,7 @@
* @since 6.9.0
* @since 7.1.0 Added the `public` meta argument.
* @since 7.2.0 The `category` argument is now optional and defaults to `uncategorized`.
* @since 7.2.0 Added the `required_capabilities` argument.
*
* @see WP_Abilities_Registry::register()
* @see wp_register_ability_category()
Expand All @@ -245,27 +244,34 @@
* @param array<string, mixed> $args {
* An associative array of arguments for configuring the ability.
*
* @type string $label Required. The human-readable label for the ability.
* @type string $description Required. A detailed description of what the ability does
* and when it should be used.
* @type string $category Optional. The ability category slug this ability belongs to.
* Defaults to `uncategorized`. Custom categories must be registered
* via `wp_register_ability_category()` before registering the ability.
* @type callable $execute_callback Required. A callback function to execute when the ability is invoked.
* Receives optional mixed input data and must return either a result
* value (any type) or a `WP_Error` object on failure.
* @type callable $permission_callback Required. A callback function to check permissions before execution.
* Receives optional mixed input data (same as `execute_callback`) and
* must return `true`/`false` for simple checks, or `WP_Error` for
* detailed error responses.
* @type array<string, mixed> $input_schema Optional. JSON Schema definition for validating the ability's input.
* Must be a valid JSON Schema object defining the structure and
* constraints for input data. Used for automatic validation and
* API documentation.
* @type array<string, mixed> $output_schema Optional. JSON Schema definition for the ability's output.
* Describes the structure of successful return values from
* `execute_callback`. Used for documentation and validation.
* @type array<string, mixed> $meta {
* @type string $label Required. The human-readable label for the ability.
* @type string $description Required. A detailed description of what the ability does
* and when it should be used.
* @type string $category Optional. The ability category slug this ability belongs to.
* Defaults to `uncategorized`. Custom categories must be registered
* via `wp_register_ability_category()` before registering the ability.
* @type callable $execute_callback Required. A callback function to execute when the ability is invoked.
* Receives optional mixed input data and must return either a result
* value (any type) or a `WP_Error` object on failure.
* @type callable $permission_callback Required unless `$required_capabilities` lists at least one capability.
* A callback function to check permissions before execution.
* Receives optional mixed input data (same as `execute_callback`) and
* must return `true`/`false` for simple checks, or `WP_Error` for
* detailed error responses. To let everyone run the ability,
* use `__return_true`. The `public` meta flag doesn't affect
* this. It only controls where the ability is shown.
* @type list<string> $required_capabilities Optional. Capabilities the current user must have to run the
* ability. Each one is checked with `current_user_can()` before
* `permission_callback` runs. Use it for checks that don't depend
* on the input. Default empty array.
* @type array<string, mixed> $input_schema Optional. JSON Schema definition for validating the ability's input.
* Must be a valid JSON Schema object defining the structure and
* constraints for input data. Used for automatic validation and
* API documentation.
* @type array<string, mixed> $output_schema Optional. JSON Schema definition for the ability's output.
* Describes the structure of successful return values from
* `execute_callback`. Used for documentation and validation.
* @type array<string, mixed> $meta {
* Optional. Additional metadata for the ability.
*
* @type array<string, bool|null> $annotations {
Expand Down Expand Up @@ -477,26 +483,32 @@ function wp_get_ability( string $name ): ?WP_Ability {
*
* @since 6.9.0
* @since 7.1.0 Added the `$args` parameter for filtering support.
* @since 7.2.0 Added the `has_required_capabilities` argument.
*
* @see WP_Abilities_Registry::get_all_registered()
*
* @param array $args {
* Optional. Arguments to filter the returned abilities. Default empty array (returns all).
*
* @type string $category Filter by category slug. Only abilities whose category
* exactly matches the given slug are included.
* @type string $namespace Filter by ability namespace prefix. Pass the namespace
* without a trailing slash, e.g. `'woocommerce'` matches
* `'woocommerce/create-order'`.
* @type array $meta Filter by meta key/value pairs. All conditions must
* match (AND logic). Supports nested arrays for structured
* meta, e.g. `array( 'mcp' => array( 'public' => true ) )`.
* @type callable $item_include_callback Optional. A callback invoked per ability after declarative
* filters. Receives a WP_Ability instance, returns bool.
* Return true to include, false to exclude.
* @type callable $result_callback Optional. A callback invoked once on the full matched
* array. Receives WP_Ability[], must return WP_Ability[].
* Use for sorting, slicing, or reshaping the result.
* @type string $category Filter by category slug. Only abilities whose category
* exactly matches the given slug are included.
* @type string $namespace Filter by ability namespace prefix. Pass the namespace
* without a trailing slash, e.g. `'woocommerce'` matches
* `'woocommerce/create-order'`.
* @type array $meta Filter by meta key/value pairs. All conditions must
* match (AND logic). Supports nested arrays for structured
* meta, e.g. `array( 'mcp' => array( 'public' => true ) )`.
* @type bool $has_required_capabilities Whether to leave out abilities whose required capabilities
* the current user doesn't have. Abilities without required
* capabilities are always included. Permission callbacks
* and filters don't run, so an included ability can still
* deny execution. Default false.
* @type callable $item_include_callback Optional. A callback invoked per ability after declarative
* filters. Receives a WP_Ability instance, returns bool.
* Return true to include, false to exclude.
* @type callable $result_callback Optional. A callback invoked once on the full matched
* array. Receives WP_Ability[], must return WP_Ability[].
* Use for sorting, slicing, or reshaping the result.
* }
* @return WP_Ability[] An array of registered WP_Ability instances matching the given args,
* keyed by ability name. Returns an empty array if no abilities are
Expand All @@ -511,11 +523,12 @@ function wp_get_abilities( array $args = array() ): array {

$abilities = $registry->get_all_registered();

$category = isset( $args['category'] ) && is_string( $args['category'] ) ? $args['category'] : '';
$namespace = isset( $args['namespace'] ) && is_string( $args['namespace'] ) ? rtrim( $args['namespace'], '/' ) . '/' : '';
$meta = isset( $args['meta'] ) && is_array( $args['meta'] ) ? $args['meta'] : array();
$item_include_callback = isset( $args['item_include_callback'] ) && is_callable( $args['item_include_callback'] ) ? $args['item_include_callback'] : null;
$result_callback = isset( $args['result_callback'] ) && is_callable( $args['result_callback'] ) ? $args['result_callback'] : null;
$category = isset( $args['category'] ) && is_string( $args['category'] ) ? $args['category'] : '';
$namespace = isset( $args['namespace'] ) && is_string( $args['namespace'] ) ? rtrim( $args['namespace'], '/' ) . '/' : '';
$meta = isset( $args['meta'] ) && is_array( $args['meta'] ) ? $args['meta'] : array();
$has_required_capabilities = ! empty( $args['has_required_capabilities'] );
$item_include_callback = isset( $args['item_include_callback'] ) && is_callable( $args['item_include_callback'] ) ? $args['item_include_callback'] : null;
$result_callback = isset( $args['result_callback'] ) && is_callable( $args['result_callback'] ) ? $args['result_callback'] : null;

$matched = array();

Expand All @@ -535,6 +548,15 @@ function wp_get_abilities( array $args = array() ): array {
continue;
}

// Step 1d: Leave out abilities whose required capabilities the current user doesn't have.
if ( $has_required_capabilities ) {
foreach ( $ability->get_required_capabilities() as $capability ) {
if ( ! current_user_can( $capability ) ) {
continue 2;
}
}
}

// Step 2: Caller-scoped per-item callback.
$include = true;
if ( null !== $item_include_callback ) {
Expand Down
8 changes: 8 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 @@ -42,6 +42,7 @@ final class WP_Abilities_Registry {
* @since 6.9.0
* @since 7.1.0 Added the `public` meta argument.
* @since 7.2.0 The `category` argument is now optional and defaults to `uncategorized`.
* @since 7.2.0 Added the `required_capabilities` argument.
*
* @see wp_register_ability()
*
Expand All @@ -59,6 +60,9 @@ final class WP_Abilities_Registry {
* Receives optional mixed input and returns mixed result or WP_Error.
* @type callable $permission_callback A callback function to check permissions before execution.
* Receives optional mixed input and returns bool or WP_Error.
* Required unless `$required_capabilities` lists at least one capability.
* @type list<string> $required_capabilities Optional. Capabilities the current user must have to run the
* ability. They are checked before `$permission_callback`.
* @type array<string, mixed> $input_schema Optional. JSON Schema definition for the ability's input.
* @type array<string, mixed> $output_schema Optional. JSON Schema definition for the ability's output.
* @type array<string, mixed> $meta {
Expand Down Expand Up @@ -115,6 +119,7 @@ public function register( string $name, array $args ): ?WP_Ability {
* @since 6.9.0
* @since 7.1.0 Added the `public` meta argument.
* @since 7.2.0 The `category` argument is now optional and defaults to `uncategorized`.
* @since 7.2.0 Added the `required_capabilities` argument.
*
* @param array<string, mixed> $args {
* An associative array of arguments for the ability.
Expand All @@ -127,6 +132,9 @@ public function register( string $name, array $args ): ?WP_Ability {
* Receives optional mixed input and returns mixed result or WP_Error.
* @type callable $permission_callback A callback function to check permissions before execution.
* Receives optional mixed input and returns bool or WP_Error.
* Required unless `$required_capabilities` lists at least one capability.
* @type list<string> $required_capabilities Optional. Capabilities the current user must have to run the
* ability. They are checked before `$permission_callback`.
* @type array<string, mixed> $input_schema Optional. JSON Schema definition for the ability's input.
* @type array<string, mixed> $output_schema Optional. JSON Schema definition for the ability's output.
* @type array<string, mixed> $meta {
Expand Down
Loading
Loading