Skip to content

Commit 53233b4

Browse files
committed
Introduce invoke_callback helper method in WP_Ability class
1 parent 124d8be commit 53233b4

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
@@ -399,6 +399,24 @@ protected function validate_input( $input = null ) {
399399
return true;
400400
}
401401

402+
/**
403+
* Invokes a callable, ensuring the input is passed through only if the input schema is defined.
404+
*
405+
* @since n.e.x.t
406+
*
407+
* @param callable $callback The callable to invoke.
408+
* @param mixed $input Optional. The input data for the ability. Default `null`.
409+
* @return mixed The result of the callable execution.
410+
*/
411+
protected function invoke_callback( $callback, $input = null ) {
412+
$args = array();
413+
if ( ! empty( $this->get_input_schema() ) ) {
414+
$args[] = $input;
415+
}
416+
417+
return $callback( ...$args );
418+
}
419+
402420
/**
403421
* Checks whether the ability has the necessary permissions.
404422
*
@@ -415,11 +433,7 @@ public function check_permissions( $input = null ) {
415433
return $is_valid;
416434
}
417435

418-
if ( empty( $this->get_input_schema() ) ) {
419-
return call_user_func( $this->permission_callback );
420-
}
421-
422-
return call_user_func( $this->permission_callback, $input );
436+
return $this->invoke_callback( $this->permission_callback, $input );
423437
}
424438

425439
/**
@@ -457,11 +471,7 @@ protected function do_execute( $input = null ) {
457471
);
458472
}
459473

460-
if ( empty( $this->get_input_schema() ) ) {
461-
return call_user_func( $this->execute_callback );
462-
}
463-
464-
return call_user_func( $this->execute_callback, $input );
474+
return $this->invoke_callback( $this->execute_callback, $input );
465475
}
466476

467477
/**

tests/unit/abilities-api/wpAbility.php

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

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

0 commit comments

Comments
 (0)