Skip to content

Commit 2daaf82

Browse files
swissspidyclaude
andauthored
Add support for the new meta.public flag (#10)
Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent d8dbe7f commit 2daaf82

2 files changed

Lines changed: 213 additions & 9 deletions

File tree

‎features/ability.feature‎

Lines changed: 155 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,18 +384,171 @@ Feature: Manage abilities registered via the WordPress Abilities API.
384384
} );
385385
"""
386386

387-
When I run `wp ability list --namespace=test-plugin --show-in-rest=true --field=name`
387+
When I run `wp ability list --namespace=test-plugin --show-in-rest --field=name`
388388
Then STDOUT should be:
389389
"""
390390
test-plugin/public-ability
391391
"""
392392

393-
When I run `wp ability list --namespace=test-plugin --show-in-rest=false --field=name`
393+
When I run `wp ability list --namespace=test-plugin --no-show-in-rest --field=name`
394394
Then STDOUT should be:
395395
"""
396396
test-plugin/private-ability
397397
"""
398398

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

‎src/Ability_Command.php‎

Lines changed: 58 additions & 7 deletions
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,8 +108,11 @@ class Ability_Command extends WP_CLI_Command {
106108
* [--namespace=<prefix>]
107109
* : Filter abilities by namespace prefix (e.g., 'core' for 'core/*' abilities).
108110
*
109-
* [--show-in-rest=<bool>]
110-
* : Filter abilities by REST API exposure.
111+
* [--public]
112+
* : Only list abilities flagged for client exposure. Pass --no-public to invert.
113+
*
114+
* [--show-in-rest]
115+
* : Only list abilities exposed to the REST API. Pass --no-show-in-rest to invert.
111116
*
112117
* [--field=<field>]
113118
* : Prints the value of a single field for each ability.
@@ -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.
@@ -162,7 +174,13 @@ class Ability_Command extends WP_CLI_Command {
162174
* $ wp ability list --namespace=core
163175
*
164176
* # List abilities exposed to REST API.
165-
* $ wp ability list --show-in-rest=true
177+
* $ wp ability list --show-in-rest
178+
*
179+
* # List abilities meant to be available to clients.
180+
* $ wp ability list --public
181+
*
182+
* # Find abilities that opt out of REST despite being public.
183+
* $ wp ability list --public --no-show-in-rest
166184
*
167185
* # List abilities as JSON.
168186
* $ wp ability list --format=json
@@ -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 = Utils\get_flag_value( $assoc_args, 'public' );
185204
$show_in_rest = Utils\get_flag_value( $assoc_args, 'show-in-rest' );
186205

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

225+
// Filter by public if specified.
226+
if ( null !== $public ) {
227+
$ability_public = '1' === $ability_data['public'];
228+
if ( $public !== $ability_public ) {
229+
continue;
230+
}
231+
}
232+
206233
// Filter by show_in_rest if specified.
207234
if ( null !== $show_in_rest ) {
208-
$show_in_rest_bool = filter_var( $show_in_rest, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
209-
$ability_rest = '1' === $ability_data['show_in_rest'];
210-
if ( null !== $show_in_rest_bool && $show_in_rest_bool !== $ability_rest ) {
235+
$ability_rest = '1' === $ability_data['show_in_rest'];
236+
if ( $show_in_rest !== $ability_rest ) {
211237
continue;
212238
}
213239
}
@@ -246,6 +272,8 @@ public function list_( $args, $assoc_args ): void {
246272
*
247273
* ## AVAILABLE FIELDS
248274
*
275+
* These fields will be displayed by default:
276+
*
249277
* * name
250278
* * label
251279
* * category
@@ -255,8 +283,22 @@ public function list_( $args, $assoc_args ): void {
255283
* * readonly
256284
* * destructive
257285
* * idempotent
286+
* * public
258287
* * show_in_rest
259288
*
289+
* These fields are optionally available:
290+
*
291+
* * meta
292+
*
293+
* The `public` field reports the high-level client exposure flag as declared
294+
* by the ability. As of WordPress 7.1 it seeds the default for channel-specific
295+
* flags such as `show_in_rest`, which take precedence when set explicitly.
296+
* On earlier versions the flag is stored but has no effect, so `public` and
297+
* `show_in_rest` may disagree.
298+
*
299+
* The `meta` field renders the raw metadata as JSON. It is the only way to
300+
* inspect channel-specific settings registered by plugins, such as `mcp`.
301+
*
260302
* ## EXAMPLES
261303
*
262304
* # Get details of a specific ability.
@@ -273,9 +315,14 @@ public function list_( $args, $assoc_args ): void {
273315
* | readonly | 1 |
274316
* | destructive | 0 |
275317
* | idempotent | 1 |
318+
* | public | 1 |
276319
* | show_in_rest | 1 |
277320
* +---------------+----------------------+
278321
*
322+
* # Inspect channel-specific settings that have no field of their own.
323+
* $ wp ability get my-plugin/my-ability --field=meta
324+
* {"annotations":{"readonly":true,"destructive":false,"idempotent":null},"mcp":{"public":false},"show_in_rest":true,"public":true}
325+
*
279326
* # Get ability as JSON.
280327
* $ wp ability get core/get-site-info --format=json
281328
*
@@ -650,6 +697,7 @@ private function format_ability_for_list( $ability ) {
650697
'readonly' => $this->format_annotation( $annotations['readonly'] ),
651698
'destructive' => $this->format_annotation( $annotations['destructive'] ),
652699
'idempotent' => $this->format_annotation( $annotations['idempotent'] ),
700+
'public' => $ability->get_meta_item( 'public', false ) ? '1' : '0',
653701
'show_in_rest' => $ability->get_meta_item( 'show_in_rest', false ) ? '1' : '0',
654702
];
655703
}
@@ -662,6 +710,7 @@ private function format_ability_for_list( $ability ) {
662710
*/
663711
private function format_ability_for_get( $ability ) {
664712
$annotations = $this->get_annotations( $ability );
713+
$meta = $ability->get_meta();
665714

666715
return [
667716
'name' => $ability->get_name(),
@@ -673,7 +722,9 @@ private function format_ability_for_get( $ability ) {
673722
'readonly' => $this->format_annotation( $annotations['readonly'] ),
674723
'destructive' => $this->format_annotation( $annotations['destructive'] ),
675724
'idempotent' => $this->format_annotation( $annotations['idempotent'] ),
725+
'public' => $ability->get_meta_item( 'public', false ) ? '1' : '0',
676726
'show_in_rest' => $ability->get_meta_item( 'show_in_rest', false ) ? '1' : '0',
727+
'meta' => ! empty( $meta ) ? wp_json_encode( $meta ) : '{}',
677728
];
678729
}
679730

0 commit comments

Comments
 (0)