Skip to content

Commit cf8c84f

Browse files
committed
Error on invalid boolean values for --show-in-rest
`filter_var()` with `FILTER_NULL_ON_FAILURE` returns null for an unparseable value such as `--show-in-rest=bogus`. The guard then skipped the comparison entirely, so the filter silently became a no-op and the command listed every registered ability. Asking to filter and receiving the full set back is worse than an error. Extract the parsing into `parse_bool_filter()` and fail with a clear message instead. `true`/`false`/`1`/`0`/`yes`/`no`, the bare flag, and `--no-show-in-rest` all keep working as before. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0162D8GNP2TsQyHUQKNoQv3a
1 parent d8dbe7f commit cf8c84f

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

‎features/ability.feature‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,14 @@ Feature: Manage abilities registered via the WordPress Abilities API.
396396
test-plugin/private-ability
397397
"""
398398

399+
@require-wp-6.9
400+
Scenario: Reject invalid boolean filter values.
401+
When I try `wp ability list --show-in-rest=bogus`
402+
Then STDERR should be:
403+
"""
404+
Error: Invalid boolean value for --show-in-rest. Use 'true' or 'false'.
405+
"""
406+
399407
@require-wp-6.9
400408
Scenario: Display ability annotations.
401409
Given a wp-content/mu-plugins/test-ability.php file:

‎src/Ability_Command.php‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public function list_( $args, $assoc_args ): void {
182182
$abilities = wp_get_abilities();
183183
$category_slug = Utils\get_flag_value( $assoc_args, 'category' );
184184
$namespace = Utils\get_flag_value( $assoc_args, 'namespace' );
185-
$show_in_rest = Utils\get_flag_value( $assoc_args, 'show-in-rest' );
185+
$show_in_rest = $this->parse_bool_filter( $assoc_args, 'show-in-rest' );
186186

187187
$items = [];
188188

@@ -204,12 +204,8 @@ public function list_( $args, $assoc_args ): void {
204204
}
205205

206206
// Filter by show_in_rest if specified.
207-
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 ) {
211-
continue;
212-
}
207+
if ( null !== $show_in_rest && $show_in_rest !== ( '1' === $ability_data['show_in_rest'] ) ) {
208+
continue;
213209
}
214210

215211
$items[] = $ability_data;
@@ -694,6 +690,29 @@ private function get_annotations( $ability ) {
694690
];
695691
}
696692

693+
/**
694+
* Parses a boolean filter flag.
695+
*
696+
* @param array $assoc_args Associative arguments.
697+
* @param string $flag The flag name.
698+
* @return bool|null The parsed value, or null when the flag was not provided.
699+
*/
700+
private function parse_bool_filter( $assoc_args, $flag ) {
701+
$value = Utils\get_flag_value( $assoc_args, $flag );
702+
703+
if ( null === $value ) {
704+
return null;
705+
}
706+
707+
$parsed = filter_var( $value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
708+
709+
if ( null === $parsed ) {
710+
WP_CLI::error( "Invalid boolean value for --{$flag}. Use 'true' or 'false'." );
711+
}
712+
713+
return $parsed;
714+
}
715+
697716
/**
698717
* Formats an annotation value for output.
699718
*

0 commit comments

Comments
 (0)