Skip to content

Commit 621cd5c

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 621cd5c

2 files changed

Lines changed: 34 additions & 4 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 & 4 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

@@ -205,9 +205,8 @@ public function list_( $args, $assoc_args ): void {
205205

206206
// Filter by show_in_rest if specified.
207207
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 ) {
208+
$ability_rest = '1' === $ability_data['show_in_rest'];
209+
if ( $show_in_rest !== $ability_rest ) {
211210
continue;
212211
}
213212
}
@@ -694,6 +693,29 @@ private function get_annotations( $ability ) {
694693
];
695694
}
696695

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

0 commit comments

Comments
 (0)