Skip to content

Commit 6c2fe89

Browse files
committed
Add support for the meta.public flag
WordPress 7.1 introduces `meta.public` as the high-level client exposure setting, with channel-specific flags taking precedence. Core resolves the cascade at registration, so `show_in_rest` already reports effective REST exposure -- but `meta.public` itself was invisible, and there was no way to tell whether REST exposure came from the high-level flag or the channel flag. Add `public` as an optional field on `list` and a default field on `get`, ordered before `show_in_rest`, plus a `--public=` filter on `list`. Add `meta` as an optional field on `get`, rendering the raw metadata as JSON the way `wp ability category get` already does. It is the only way to inspect channel-specific settings such as `mcp.public`, which no dedicated field covers. Nothing here reimplements the precedence rules, and `list` keeps reporting every registered ability regardless of exposure. WP-CLI runs with full trust; these flags are display and filter concerns, not access control. Fixes #9 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0162D8GNP2TsQyHUQKNoQv3a
1 parent cf8c84f commit 6c2fe89

2 files changed

Lines changed: 240 additions & 1 deletion

File tree

‎features/ability.feature‎

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,12 +398,171 @@ Feature: Manage abilities registered via the WordPress Abilities API.
398398

399399
@require-wp-6.9
400400
Scenario: Reject invalid boolean filter values.
401+
When I try `wp ability list --public=bogus`
402+
Then STDERR should be:
403+
"""
404+
Error: Invalid boolean value for --public. Use 'true' or 'false'.
405+
"""
406+
401407
When I try `wp ability list --show-in-rest=bogus`
402408
Then STDERR should be:
403409
"""
404410
Error: Invalid boolean value for --show-in-rest. Use 'true' or 'false'.
405411
"""
406412

413+
@require-wp-7.1
414+
Scenario: Display the public flag and its precedence over channel flags.
415+
Given a wp-content/mu-plugins/test-ability.php file:
416+
"""
417+
<?php
418+
add_action( 'wp_abilities_api_categories_init', function() {
419+
wp_register_ability_category( 'test-category', array(
420+
'label' => 'Test Category',
421+
'description' => 'A test category.',
422+
) );
423+
} );
424+
425+
add_action( 'wp_abilities_api_init', function() {
426+
wp_register_ability( 'test-plugin/public-ability', array(
427+
'label' => 'Public Ability',
428+
'description' => 'A public ability.',
429+
'category' => 'test-category',
430+
'execute_callback' => function( $input ) {
431+
return array( 'result' => 'done' );
432+
},
433+
'permission_callback' => '__return_true',
434+
'meta' => array( 'public' => true ),
435+
) );
436+
437+
wp_register_ability( 'test-plugin/public-not-rest', array(
438+
'label' => 'Public But Not REST',
439+
'description' => 'A public ability hidden from REST.',
440+
'category' => 'test-category',
441+
'execute_callback' => function( $input ) {
442+
return array( 'result' => 'done' );
443+
},
444+
'permission_callback' => '__return_true',
445+
'meta' => array( 'public' => true, 'show_in_rest' => false ),
446+
) );
447+
448+
wp_register_ability( 'test-plugin/plain-ability', array(
449+
'label' => 'Plain Ability',
450+
'description' => 'An ability without exposure metadata.',
451+
'category' => 'test-category',
452+
'execute_callback' => function( $input ) {
453+
return array( 'result' => 'done' );
454+
},
455+
'permission_callback' => '__return_true',
456+
) );
457+
} );
458+
"""
459+
460+
When I run `wp ability list --namespace=test-plugin --fields=name,public,show_in_rest`
461+
Then STDOUT should be a table containing rows:
462+
| name | public | show_in_rest |
463+
| test-plugin/public-ability | 1 | 1 |
464+
| test-plugin/public-not-rest | 1 | 0 |
465+
| test-plugin/plain-ability | 0 | 0 |
466+
467+
When I run `wp ability get test-plugin/public-ability --format=json`
468+
Then STDOUT should be JSON containing:
469+
"""
470+
{"public":"1","show_in_rest":"1"}
471+
"""
472+
473+
When I run `wp ability get test-plugin/public-not-rest --format=json`
474+
Then STDOUT should be JSON containing:
475+
"""
476+
{"public":"1","show_in_rest":"0"}
477+
"""
478+
479+
@require-wp-7.1
480+
Scenario: Filter abilities by public.
481+
Given a wp-content/mu-plugins/test-ability.php file:
482+
"""
483+
<?php
484+
add_action( 'wp_abilities_api_categories_init', function() {
485+
wp_register_ability_category( 'test-category', array(
486+
'label' => 'Test Category',
487+
'description' => 'A test category.',
488+
) );
489+
} );
490+
491+
add_action( 'wp_abilities_api_init', function() {
492+
wp_register_ability( 'test-plugin/public-ability', array(
493+
'label' => 'Public Ability',
494+
'description' => 'A public ability.',
495+
'category' => 'test-category',
496+
'execute_callback' => function( $input ) {
497+
return array( 'result' => 'done' );
498+
},
499+
'permission_callback' => '__return_true',
500+
'meta' => array( 'public' => true ),
501+
) );
502+
503+
wp_register_ability( 'test-plugin/internal-ability', array(
504+
'label' => 'Internal Ability',
505+
'description' => 'An ability kept away from clients.',
506+
'category' => 'test-category',
507+
'execute_callback' => function( $input ) {
508+
return array( 'result' => 'done' );
509+
},
510+
'permission_callback' => '__return_true',
511+
'meta' => array( 'public' => false ),
512+
) );
513+
} );
514+
"""
515+
516+
When I run `wp ability list --namespace=test-plugin --public=true --field=name`
517+
Then STDOUT should be:
518+
"""
519+
test-plugin/public-ability
520+
"""
521+
522+
When I run `wp ability list --namespace=test-plugin --public=false --field=name`
523+
Then STDOUT should be:
524+
"""
525+
test-plugin/internal-ability
526+
"""
527+
528+
@require-wp-6.9
529+
Scenario: Inspect raw ability meta.
530+
Given a wp-content/mu-plugins/test-ability.php file:
531+
"""
532+
<?php
533+
add_action( 'wp_abilities_api_categories_init', function() {
534+
wp_register_ability_category( 'test-category', array(
535+
'label' => 'Test Category',
536+
'description' => 'A test category.',
537+
) );
538+
} );
539+
540+
add_action( 'wp_abilities_api_init', function() {
541+
wp_register_ability( 'test-plugin/channel-ability', array(
542+
'label' => 'Channel Ability',
543+
'description' => 'An ability with channel-specific metadata.',
544+
'category' => 'test-category',
545+
'execute_callback' => function( $input ) {
546+
return array( 'result' => 'done' );
547+
},
548+
'permission_callback' => '__return_true',
549+
'meta' => array( 'mcp' => array( 'public' => false ) ),
550+
) );
551+
} );
552+
"""
553+
554+
When I run `wp ability get test-plugin/channel-ability --field=meta`
555+
Then STDOUT should contain:
556+
"""
557+
"mcp":{"public":false}
558+
"""
559+
560+
When I run `wp ability get test-plugin/channel-ability`
561+
Then STDOUT should not contain:
562+
"""
563+
mcp
564+
"""
565+
407566
@require-wp-6.9
408567
Scenario: Display ability annotations.
409568
Given a wp-content/mu-plugins/test-ability.php file:

‎src/Ability_Command.php‎

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@
2828
* +---------------------------+----------------------+----------+------------------------------------------+
2929
*
3030
* # Get details of a specific ability.
31-
* $ wp ability get core/get-site-info --fields=name,label,category,readonly,show_in_rest
31+
* $ wp ability get core/get-site-info --fields=name,label,category,readonly,public,show_in_rest
3232
* +---------------+----------------------+
3333
* | Field | Value |
3434
* +---------------+----------------------+
3535
* | name | core/get-site-info |
3636
* | label | Get Site Information |
3737
* | category | site |
3838
* | readonly | 1 |
39+
* | public | 1 |
3940
* | show_in_rest | 1 |
4041
* +---------------+----------------------+
4142
*
@@ -92,6 +93,7 @@ class Ability_Command extends WP_CLI_Command {
9293
'readonly',
9394
'destructive',
9495
'idempotent',
96+
'public',
9597
'show_in_rest',
9698
];
9799

@@ -106,6 +108,9 @@ class Ability_Command extends WP_CLI_Command {
106108
* [--namespace=<prefix>]
107109
* : Filter abilities by namespace prefix (e.g., 'core' for 'core/*' abilities).
108110
*
111+
* [--public=<bool>]
112+
* : Filter abilities by the high-level client exposure flag.
113+
*
109114
* [--show-in-rest=<bool>]
110115
* : Filter abilities by REST API exposure.
111116
*
@@ -142,8 +147,15 @@ class Ability_Command extends WP_CLI_Command {
142147
* * readonly
143148
* * destructive
144149
* * idempotent
150+
* * public
145151
* * show_in_rest
146152
*
153+
* The `public` field reports the high-level client exposure flag as declared
154+
* by the ability. As of WordPress 7.1 it seeds the default for channel-specific
155+
* flags such as `show_in_rest`, which take precedence when set explicitly.
156+
* On earlier versions the flag is stored but has no effect, so `public` and
157+
* `show_in_rest` may disagree.
158+
*
147159
* ## EXAMPLES
148160
*
149161
* # List all abilities.
@@ -164,6 +176,12 @@ class Ability_Command extends WP_CLI_Command {
164176
* # List abilities exposed to REST API.
165177
* $ wp ability list --show-in-rest=true
166178
*
179+
* # List abilities meant to be available to clients.
180+
* $ wp ability list --public=true
181+
*
182+
* # Find abilities that opt out of REST despite being public.
183+
* $ wp ability list --public=true --show-in-rest=false
184+
*
167185
* # List abilities as JSON.
168186
* $ wp ability list --format=json
169187
*
@@ -182,6 +200,7 @@ public function list_( $args, $assoc_args ): void {
182200
$abilities = wp_get_abilities();
183201
$category_slug = Utils\get_flag_value( $assoc_args, 'category' );
184202
$namespace = Utils\get_flag_value( $assoc_args, 'namespace' );
203+
$public = $this->parse_bool_filter( $assoc_args, 'public' );
185204
$show_in_rest = $this->parse_bool_filter( $assoc_args, 'show-in-rest' );
186205

187206
$items = [];
@@ -203,6 +222,11 @@ public function list_( $args, $assoc_args ): void {
203222
}
204223
}
205224

225+
// Filter by public if specified.
226+
if ( null !== $public && $public !== ( '1' === $ability_data['public'] ) ) {
227+
continue;
228+
}
229+
206230
// Filter by show_in_rest if specified.
207231
if ( null !== $show_in_rest && $show_in_rest !== ( '1' === $ability_data['show_in_rest'] ) ) {
208232
continue;
@@ -211,6 +235,8 @@ public function list_( $args, $assoc_args ): void {
211235
$items[] = $ability_data;
212236
}
213237

238+
$this->maybe_debug_inert_public_flag( $items );
239+
214240
$formatter = $this->get_formatter( $assoc_args, $this->default_fields );
215241
$formatter->display_items( $items );
216242
}
@@ -242,6 +268,8 @@ public function list_( $args, $assoc_args ): void {
242268
*
243269
* ## AVAILABLE FIELDS
244270
*
271+
* These fields will be displayed by default:
272+
*
245273
* * name
246274
* * label
247275
* * category
@@ -251,8 +279,22 @@ public function list_( $args, $assoc_args ): void {
251279
* * readonly
252280
* * destructive
253281
* * idempotent
282+
* * public
254283
* * show_in_rest
255284
*
285+
* These fields are optionally available:
286+
*
287+
* * meta
288+
*
289+
* The `public` field reports the high-level client exposure flag as declared
290+
* by the ability. As of WordPress 7.1 it seeds the default for channel-specific
291+
* flags such as `show_in_rest`, which take precedence when set explicitly.
292+
* On earlier versions the flag is stored but has no effect, so `public` and
293+
* `show_in_rest` may disagree.
294+
*
295+
* The `meta` field renders the raw metadata as JSON. It is the only way to
296+
* inspect channel-specific settings registered by plugins, such as `mcp`.
297+
*
256298
* ## EXAMPLES
257299
*
258300
* # Get details of a specific ability.
@@ -269,9 +311,14 @@ public function list_( $args, $assoc_args ): void {
269311
* | readonly | 1 |
270312
* | destructive | 0 |
271313
* | idempotent | 1 |
314+
* | public | 1 |
272315
* | show_in_rest | 1 |
273316
* +---------------+----------------------+
274317
*
318+
* # Inspect channel-specific settings that have no field of their own.
319+
* $ wp ability get my-plugin/my-ability --field=meta
320+
* {"annotations":{"readonly":true,"destructive":false,"idempotent":null},"mcp":{"public":false},"show_in_rest":true,"public":true}
321+
*
275322
* # Get ability as JSON.
276323
* $ wp ability get core/get-site-info --format=json
277324
*
@@ -292,6 +339,8 @@ public function get( $args, $assoc_args ): void {
292339

293340
$ability_data = $this->format_ability_for_get( $ability );
294341

342+
$this->maybe_debug_inert_public_flag( [ $ability_data ] );
343+
295344
$formatter = $this->get_formatter( $assoc_args, $this->get_fields );
296345
$formatter->display_item( $ability_data );
297346
}
@@ -646,6 +695,7 @@ private function format_ability_for_list( $ability ) {
646695
'readonly' => $this->format_annotation( $annotations['readonly'] ),
647696
'destructive' => $this->format_annotation( $annotations['destructive'] ),
648697
'idempotent' => $this->format_annotation( $annotations['idempotent'] ),
698+
'public' => $ability->get_meta_item( 'public', false ) ? '1' : '0',
649699
'show_in_rest' => $ability->get_meta_item( 'show_in_rest', false ) ? '1' : '0',
650700
];
651701
}
@@ -658,6 +708,7 @@ private function format_ability_for_list( $ability ) {
658708
*/
659709
private function format_ability_for_get( $ability ) {
660710
$annotations = $this->get_annotations( $ability );
711+
$meta = $ability->get_meta();
661712

662713
return [
663714
'name' => $ability->get_name(),
@@ -669,7 +720,9 @@ private function format_ability_for_get( $ability ) {
669720
'readonly' => $this->format_annotation( $annotations['readonly'] ),
670721
'destructive' => $this->format_annotation( $annotations['destructive'] ),
671722
'idempotent' => $this->format_annotation( $annotations['idempotent'] ),
723+
'public' => $ability->get_meta_item( 'public', false ) ? '1' : '0',
672724
'show_in_rest' => $ability->get_meta_item( 'show_in_rest', false ) ? '1' : '0',
725+
'meta' => ! empty( $meta ) ? wp_json_encode( $meta ) : '{}',
673726
];
674727
}
675728

@@ -713,6 +766,33 @@ private function parse_bool_filter( $assoc_args, $flag ) {
713766
return $parsed;
714767
}
715768

769+
/**
770+
* Emits a debug note when the `public` meta flag is declared but inert.
771+
*
772+
* WordPress only resolves `public` into channel-specific flags such as
773+
* `show_in_rest` as of 7.1. On earlier versions the flag is stored as
774+
* declared but has no effect on client exposure, so `public` and
775+
* `show_in_rest` can disagree for no visible reason.
776+
*
777+
* @param array<int,array<string,mixed>> $items The formatted abilities.
778+
* @return void
779+
*/
780+
private function maybe_debug_inert_public_flag( $items ) {
781+
if ( ! Utils\wp_version_compare( '7.1', '<' ) ) {
782+
return;
783+
}
784+
785+
foreach ( $items as $item ) {
786+
if ( isset( $item['public'] ) && '1' === $item['public'] ) {
787+
WP_CLI::debug(
788+
'The `public` meta flag is only resolved into channel-specific flags such as `show_in_rest` as of WordPress 7.1. On this version it is reported as declared but has no effect on client exposure.',
789+
'ability'
790+
);
791+
return;
792+
}
793+
}
794+
}
795+
716796
/**
717797
* Formats an annotation value for output.
718798
*

0 commit comments

Comments
 (0)