Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions features/ability.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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',
) );
} );
"""

Expand Down Expand Up @@ -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:
"""
Expand Down
8 changes: 6 additions & 2 deletions src/Ability_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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<string,mixed> The input data.
* @return array<string,mixed>|null The input data, or null if none provided.
*/
private function build_input_with_stdin( $assoc_args ) {
$input = [];
Expand Down Expand Up @@ -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;
}

/**
Expand Down
Loading