2929 * wp_register_ability(
3030 * 'my-plugin/export-users',
3131 * array(
32- * 'label' => __( 'Export Users', 'my-plugin' ),
33- * 'description' => __( 'Exports user data to CSV format.', 'my-plugin' ),
34- * 'category' => 'data-export',
35- * 'execute_callback' => 'my_plugin_export_users',
36- * 'permission_callback' => function(): bool {
37- * return current_user_can( 'export' );
38- * },
39- * 'input_schema' => array(
32+ * 'label' => __( 'Export Users', 'my-plugin' ),
33+ * 'description' => __( 'Exports user data to CSV format.', 'my-plugin' ),
34+ * 'category' => 'data-export',
35+ * 'execute_callback' => 'my_plugin_export_users',
36+ * 'required_capabilities' => array( 'export' ),
37+ * 'input_schema' => array(
4038 * 'type' => 'string',
4139 * 'enum' => array( 'subscriber', 'contributor', 'author', 'editor', 'administrator' ),
4240 * 'description' => __( 'Limits the export to users with this role.', 'my-plugin' ),
4341 * 'required' => false,
4442 * ),
45- * 'output_schema' => array(
43+ * 'output_schema' => array(
4644 * 'type' => 'string',
4745 * 'description' => __( 'User data in CSV format.', 'my-plugin' ),
4846 * 'required' => true,
4947 * ),
50- * 'meta' => array(
48+ * 'meta' => array(
5149 * 'public' => true,
5250 * ),
5351 * )
234232 * @since 6.9.0
235233 * @since 7.1.0 Added the `public` meta argument.
236234 * @since 7.2.0 The `category` argument is now optional and defaults to `uncategorized`.
235+ * @since 7.2.0 Added the `required_capabilities` argument.
237236 *
238237 * @see WP_Abilities_Registry::register()
239238 * @see wp_register_ability_category()
245244 * @param array<string, mixed> $args {
246245 * An associative array of arguments for configuring the ability.
247246 *
248- * @type string $label Required. The human-readable label for the ability.
249- * @type string $description Required. A detailed description of what the ability does
250- * and when it should be used.
251- * @type string $category Optional. The ability category slug this ability belongs to.
252- * Defaults to `uncategorized`. Custom categories must be registered
253- * via `wp_register_ability_category()` before registering the ability.
254- * @type callable $execute_callback Required. A callback function to execute when the ability is invoked.
255- * Receives optional mixed input data and must return either a result
256- * value (any type) or a `WP_Error` object on failure.
257- * @type callable $permission_callback Required. A callback function to check permissions before execution.
258- * Receives optional mixed input data (same as `execute_callback`) and
259- * must return `true`/`false` for simple checks, or `WP_Error` for
260- * detailed error responses.
261- * @type array<string, mixed> $input_schema Optional. JSON Schema definition for validating the ability's input.
262- * Must be a valid JSON Schema object defining the structure and
263- * constraints for input data. Used for automatic validation and
264- * API documentation.
265- * @type array<string, mixed> $output_schema Optional. JSON Schema definition for the ability's output.
266- * Describes the structure of successful return values from
267- * `execute_callback`. Used for documentation and validation.
268- * @type array<string, mixed> $meta {
247+ * @type string $label Required. The human-readable label for the ability.
248+ * @type string $description Required. A detailed description of what the ability does
249+ * and when it should be used.
250+ * @type string $category Optional. The ability category slug this ability belongs to.
251+ * Defaults to `uncategorized`. Custom categories must be registered
252+ * via `wp_register_ability_category()` before registering the ability.
253+ * @type callable $execute_callback Required. A callback function to execute when the ability is invoked.
254+ * Receives optional mixed input data and must return either a result
255+ * value (any type) or a `WP_Error` object on failure.
256+ * @type callable $permission_callback Required unless `$required_capabilities` lists at least one capability.
257+ * A callback function to check permissions before execution.
258+ * Receives optional mixed input data (same as `execute_callback`) and
259+ * must return `true`/`false` for simple checks, or `WP_Error` for
260+ * detailed error responses. To let everyone run the ability,
261+ * use `__return_true`. The `public` meta flag doesn't affect
262+ * this. It only controls where the ability is shown.
263+ * @type list<string> $required_capabilities Optional. Capabilities the current user must have to run the
264+ * ability. Each one is checked with `current_user_can()` before
265+ * `permission_callback` runs. Use it for checks that don't depend
266+ * on the input. Default empty array.
267+ * @type array<string, mixed> $input_schema Optional. JSON Schema definition for validating the ability's input.
268+ * Must be a valid JSON Schema object defining the structure and
269+ * constraints for input data. Used for automatic validation and
270+ * API documentation.
271+ * @type array<string, mixed> $output_schema Optional. JSON Schema definition for the ability's output.
272+ * Describes the structure of successful return values from
273+ * `execute_callback`. Used for documentation and validation.
274+ * @type array<string, mixed> $meta {
269275 * Optional. Additional metadata for the ability.
270276 *
271277 * @type array<string, bool|null> $annotations {
@@ -477,26 +483,32 @@ function wp_get_ability( string $name ): ?WP_Ability {
477483 *
478484 * @since 6.9.0
479485 * @since 7.1.0 Added the `$args` parameter for filtering support.
486+ * @since 7.2.0 Added the `has_required_capabilities` argument.
480487 *
481488 * @see WP_Abilities_Registry::get_all_registered()
482489 *
483490 * @param array $args {
484491 * Optional. Arguments to filter the returned abilities. Default empty array (returns all).
485492 *
486- * @type string $category Filter by category slug. Only abilities whose category
487- * exactly matches the given slug are included.
488- * @type string $namespace Filter by ability namespace prefix. Pass the namespace
489- * without a trailing slash, e.g. `'woocommerce'` matches
490- * `'woocommerce/create-order'`.
491- * @type array $meta Filter by meta key/value pairs. All conditions must
492- * match (AND logic). Supports nested arrays for structured
493- * meta, e.g. `array( 'mcp' => array( 'public' => true ) )`.
494- * @type callable $item_include_callback Optional. A callback invoked per ability after declarative
495- * filters. Receives a WP_Ability instance, returns bool.
496- * Return true to include, false to exclude.
497- * @type callable $result_callback Optional. A callback invoked once on the full matched
498- * array. Receives WP_Ability[], must return WP_Ability[].
499- * Use for sorting, slicing, or reshaping the result.
493+ * @type string $category Filter by category slug. Only abilities whose category
494+ * exactly matches the given slug are included.
495+ * @type string $namespace Filter by ability namespace prefix. Pass the namespace
496+ * without a trailing slash, e.g. `'woocommerce'` matches
497+ * `'woocommerce/create-order'`.
498+ * @type array $meta Filter by meta key/value pairs. All conditions must
499+ * match (AND logic). Supports nested arrays for structured
500+ * meta, e.g. `array( 'mcp' => array( 'public' => true ) )`.
501+ * @type bool $has_required_capabilities Whether to leave out abilities whose required capabilities
502+ * the current user doesn't have. Abilities without required
503+ * capabilities are always included. Permission callbacks
504+ * and filters don't run, so an included ability can still
505+ * deny execution. Default false.
506+ * @type callable $item_include_callback Optional. A callback invoked per ability after declarative
507+ * filters. Receives a WP_Ability instance, returns bool.
508+ * Return true to include, false to exclude.
509+ * @type callable $result_callback Optional. A callback invoked once on the full matched
510+ * array. Receives WP_Ability[], must return WP_Ability[].
511+ * Use for sorting, slicing, or reshaping the result.
500512 * }
501513 * @return WP_Ability[] An array of registered WP_Ability instances matching the given args,
502514 * keyed by ability name. Returns an empty array if no abilities are
@@ -511,11 +523,12 @@ function wp_get_abilities( array $args = array() ): array {
511523
512524 $ abilities = $ registry ->get_all_registered ();
513525
514- $ category = isset ( $ args ['category ' ] ) && is_string ( $ args ['category ' ] ) ? $ args ['category ' ] : '' ;
515- $ namespace = isset ( $ args ['namespace ' ] ) && is_string ( $ args ['namespace ' ] ) ? rtrim ( $ args ['namespace ' ], '/ ' ) . '/ ' : '' ;
516- $ meta = isset ( $ args ['meta ' ] ) && is_array ( $ args ['meta ' ] ) ? $ args ['meta ' ] : array ();
517- $ item_include_callback = isset ( $ args ['item_include_callback ' ] ) && is_callable ( $ args ['item_include_callback ' ] ) ? $ args ['item_include_callback ' ] : null ;
518- $ result_callback = isset ( $ args ['result_callback ' ] ) && is_callable ( $ args ['result_callback ' ] ) ? $ args ['result_callback ' ] : null ;
526+ $ category = isset ( $ args ['category ' ] ) && is_string ( $ args ['category ' ] ) ? $ args ['category ' ] : '' ;
527+ $ namespace = isset ( $ args ['namespace ' ] ) && is_string ( $ args ['namespace ' ] ) ? rtrim ( $ args ['namespace ' ], '/ ' ) . '/ ' : '' ;
528+ $ meta = isset ( $ args ['meta ' ] ) && is_array ( $ args ['meta ' ] ) ? $ args ['meta ' ] : array ();
529+ $ has_required_capabilities = ! empty ( $ args ['has_required_capabilities ' ] );
530+ $ item_include_callback = isset ( $ args ['item_include_callback ' ] ) && is_callable ( $ args ['item_include_callback ' ] ) ? $ args ['item_include_callback ' ] : null ;
531+ $ result_callback = isset ( $ args ['result_callback ' ] ) && is_callable ( $ args ['result_callback ' ] ) ? $ args ['result_callback ' ] : null ;
519532
520533 $ matched = array ();
521534
@@ -535,6 +548,15 @@ function wp_get_abilities( array $args = array() ): array {
535548 continue ;
536549 }
537550
551+ // Step 1d: Leave out abilities whose required capabilities the current user doesn't have.
552+ if ( $ has_required_capabilities ) {
553+ foreach ( $ ability ->get_required_capabilities () as $ capability ) {
554+ if ( ! current_user_can ( $ capability ) ) {
555+ continue 2 ;
556+ }
557+ }
558+ }
559+
538560 // Step 2: Caller-scoped per-item callback.
539561 $ include = true ;
540562 if ( null !== $ item_include_callback ) {
0 commit comments