Skip to content
Merged
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
30 changes: 20 additions & 10 deletions includes/abilities-api/class-wp-ability.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,24 @@ protected function validate_input( $input = null ) {
return true;
}

/**
* Invokes a callable, ensuring the input is passed through only if the input schema is defined.
*
* @since n.e.x.t
*
* @param callable $callback The callable to invoke.
* @param mixed $input Optional. The input data for the ability. Default `null`.
* @return mixed The result of the callable execution.
*/
protected function invoke_callback( callable $callback, $input = null ) {
$args = array();
if ( ! empty( $this->get_input_schema() ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're cleaning things up, what do you think about adding a $this->has_input_schema() method to make this sort of check more declarative?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good idea 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will play with this, and the other method idea #104 (comment) in a follow-up PR 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to have a similar method for the output schema? Given all the other tasks necessary for WP 6.9, this one is nice to have we can introduce later.

$args[] = $input;
}

return $callback( ...$args );
}

/**
* Checks whether the ability has the necessary permissions.
*
Expand All @@ -397,11 +415,7 @@ public function check_permissions( $input = null ) {
return $is_valid;
}

if ( empty( $this->get_input_schema() ) ) {
return call_user_func( $this->permission_callback );
}

return call_user_func( $this->permission_callback, $input );
return $this->invoke_callback( $this->permission_callback, $input );
}

/**
Expand Down Expand Up @@ -439,11 +453,7 @@ protected function do_execute( $input = null ) {
);
}

if ( empty( $this->get_input_schema() ) ) {
return call_user_func( $this->execute_callback );
}

return call_user_func( $this->execute_callback, $input );
return $this->invoke_callback( $this->execute_callback, $input );
}

/**
Expand Down
68 changes: 68 additions & 0 deletions tests/unit/abilities-api/wpAbility.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,74 @@ public function test_execute_input( $input_schema, $execute_callback, $input, $r
$this->assertSame( $result, $ability->execute( $input ) );
}

/**
* A static method to be used as a callback in tests.
*
* @param string $input An input string.
* @return int The length of the input string.
*/
public static function my_static_execute_callback( string $input ): int {
return strlen( $input );
}

/**
* An instance method to be used as a callback in tests.
*
* @param string $input An input string.
* @return int The length of the input string.
*/
public function my_instance_execute_callback( string $input ): int {
return strlen( $input );
}

/**
* Data provider for testing different types of execute callbacks.
*/
public function data_execute_callback() {
return array(
'function name string' => array(
'strlen',
),
'closure' => array(
static function ( string $input ): int {
return strlen( $input );
},
),
'static class method string' => array(
'Tests_Abilities_API_WpAbility::my_static_execute_callback',
),
'static class method array' => array(
array( 'Tests_Abilities_API_WpAbility', 'my_static_execute_callback' ),
),
'object method' => array(
array( $this, 'my_instance_execute_callback' ),
),
);
}

/**
* Tests the execution of the ability with different types of callbacks.
*
* @dataProvider data_execute_callback
*/
public function test_execute_with_different_callbacks( $execute_callback) {
$args = array_merge(
self::$test_ability_properties,
array(
'input_schema' => array(
'type' => 'string',
'description' => 'Test input string.',
'required' => true,
),
'execute_callback' => $execute_callback,
)
);

$ability = new WP_Ability( self::$test_ability_name, $args );

$this->assertSame( 6, $ability->execute( 'hello!' ) );
}

/**
* Tests the execution of the ability with no input.
*/
Expand Down