diff --git a/features/ability.feature b/features/ability.feature index 94a014c..4ccaae4 100644 --- a/features/ability.feature +++ b/features/ability.feature @@ -191,6 +191,19 @@ Feature: Manage abilities registered via the WordPress Abilities API. }, 'permission_callback' => '__return_true', ) ); + + wp_register_ability( 'test-plugin/simple-no-input', array( + 'label' => 'Simple No Input', + 'description' => 'Returns an empty array.', + 'category' => 'test-category', + 'output_schema' => array( + 'type' => 'array', + ), + 'execute_callback' => function( $input = null ) { + return array(); + }, + 'permission_callback' => '__return_true', + ) ); } ); """ @@ -224,6 +237,12 @@ Feature: Manage abilities registered via the WordPress Abilities API. {"sum":30} """ + When I run `wp ability run test-plugin/simple-no-input` + Then STDOUT should be: + """ + [] + """ + When I run `wp ability run test-plugin/get-site-title --format=yaml` Then STDOUT should contain: """ diff --git a/src/Ability_Command.php b/src/Ability_Command.php index 837b61a..48fc9a4 100644 --- a/src/Ability_Command.php +++ b/src/Ability_Command.php @@ -369,6 +369,9 @@ public function run( $args, $assoc_args ): void { // Build input data (with stdin support). $input = $this->build_input_with_stdin( $assoc_args ); + if ( null === $input && [] !== $ability->get_input_schema() ) { + $input = []; + } // Execute the ability. $result = $ability->execute( $input ); @@ -593,7 +596,7 @@ private function build_input( $assoc_args ) { * Builds input data from associative arguments with stdin support. * * @param array $assoc_args Associative arguments. - * @return array The input data. + * @return array|null The input data, or null if none provided. */ private function build_input_with_stdin( $assoc_args ) { $input = []; @@ -626,7 +629,8 @@ private function build_input_with_stdin( $assoc_args ) { $input[ $key ] = $value; } - return $input; + // Return null only when no input was provided (no --input flag and no field args). + return ( empty( $input ) && null === $json_input ) ? null : $input; } /**