Skip to content

Commit 4dc57b3

Browse files
gzioloJasonTheAdamsfelixarntz
authored
Introduce invoke_callback helper method in WP_Ability class (#88)
* Introduce `invoke_callback` helper method in `WP_Ability` class * Update tests/unit/abilities-api/wpAbility.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add callable type hint --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: gziolo <gziolo@git.wordpress.org> Co-authored-by: JasonTheAdams <jason_the_adams@git.wordpress.org> Co-authored-by: felixarntz <flixos90@git.wordpress.org>
1 parent 56616ba commit 4dc57b3

File tree

2 files changed

+88
-10
lines changed

2 files changed

+88
-10
lines changed

includes/abilities-api/class-wp-ability.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,24 @@ protected function validate_input( $input = null ) {
381381
return true;
382382
}
383383

384+
/**
385+
* Invokes a callable, ensuring the input is passed through only if the input schema is defined.
386+
*
387+
* @since n.e.x.t
388+
*
389+
* @param callable $callback The callable to invoke.
390+
* @param mixed $input Optional. The input data for the ability. Default `null`.
391+
* @return mixed The result of the callable execution.
392+
*/
393+
protected function invoke_callback( callable $callback, $input = null ) {
394+
$args = array();
395+
if ( ! empty( $this->get_input_schema() ) ) {
396+
$args[] = $input;
397+
}
398+
399+
return $callback( ...$args );
400+
}
401+
384402
/**
385403
* Checks whether the ability has the necessary permissions.
386404
*
@@ -397,11 +415,7 @@ public function check_permissions( $input = null ) {
397415
return $is_valid;
398416
}
399417

400-
if ( empty( $this->get_input_schema() ) ) {
401-
return call_user_func( $this->permission_callback );
402-
}
403-
404-
return call_user_func( $this->permission_callback, $input );
418+
return $this->invoke_callback( $this->permission_callback, $input );
405419
}
406420

407421
/**
@@ -439,11 +453,7 @@ protected function do_execute( $input = null ) {
439453
);
440454
}
441455

442-
if ( empty( $this->get_input_schema() ) ) {
443-
return call_user_func( $this->execute_callback );
444-
}
445-
446-
return call_user_func( $this->execute_callback, $input );
456+
return $this->invoke_callback( $this->execute_callback, $input );
447457
}
448458

449459
/**

tests/unit/abilities-api/wpAbility.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,74 @@ public function test_execute_input( $input_schema, $execute_callback, $input, $r
349349
$this->assertSame( $result, $ability->execute( $input ) );
350350
}
351351

352+
/**
353+
* A static method to be used as a callback in tests.
354+
*
355+
* @param string $input An input string.
356+
* @return int The length of the input string.
357+
*/
358+
public static function my_static_execute_callback( string $input ): int {
359+
return strlen( $input );
360+
}
361+
362+
/**
363+
* An instance method to be used as a callback in tests.
364+
*
365+
* @param string $input An input string.
366+
* @return int The length of the input string.
367+
*/
368+
public function my_instance_execute_callback( string $input ): int {
369+
return strlen( $input );
370+
}
371+
372+
/**
373+
* Data provider for testing different types of execute callbacks.
374+
*/
375+
public function data_execute_callback() {
376+
return array(
377+
'function name string' => array(
378+
'strlen',
379+
),
380+
'closure' => array(
381+
static function ( string $input ): int {
382+
return strlen( $input );
383+
},
384+
),
385+
'static class method string' => array(
386+
'Tests_Abilities_API_WpAbility::my_static_execute_callback',
387+
),
388+
'static class method array' => array(
389+
array( 'Tests_Abilities_API_WpAbility', 'my_static_execute_callback' ),
390+
),
391+
'object method' => array(
392+
array( $this, 'my_instance_execute_callback' ),
393+
),
394+
);
395+
}
396+
397+
/**
398+
* Tests the execution of the ability with different types of callbacks.
399+
*
400+
* @dataProvider data_execute_callback
401+
*/
402+
public function test_execute_with_different_callbacks( $execute_callback) {
403+
$args = array_merge(
404+
self::$test_ability_properties,
405+
array(
406+
'input_schema' => array(
407+
'type' => 'string',
408+
'description' => 'Test input string.',
409+
'required' => true,
410+
),
411+
'execute_callback' => $execute_callback,
412+
)
413+
);
414+
415+
$ability = new WP_Ability( self::$test_ability_name, $args );
416+
417+
$this->assertSame( 6, $ability->execute( 'hello!' ) );
418+
}
419+
352420
/**
353421
* Tests the execution of the ability with no input.
354422
*/

0 commit comments

Comments
 (0)