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
64 changes: 42 additions & 22 deletions src/psr4/Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,11 @@ public function on_admin()
* Adds a WordPress action hook.
* @since 1.0.3
*
* @param string $hook WordPress hook name.
* @param string $mvc_call Lightweight MVC call. (i.e. 'Controller@method')
* @param mixed $priority Execution priority or MVC params.
* @param mixed $accepted_args Accepted args or priority.
* @param int $args Accepted args.
* @param string $hook WordPress hook name.
* @param string|array $mvc_call Lightweight MVC call. (i.e. 'Controller@method')
* @param mixed $priority Execution priority or MVC params.
* @param mixed $accepted_args Accepted args or priority.
* @param int $args Accepted args.
*/
public function add_action( $hook, $mvc_call, $priority = 10, $accepted_args = 1, $args = 1 )
{
Expand All @@ -354,11 +354,11 @@ public function add_action( $hook, $mvc_call, $priority = 10, $accepted_args = 1
* Adds a WordPress filter hook.
* @since 1.0.3
*
* @param string $hook WordPress hook name.
* @param string $mvc_call Lightweight MVC call. (i.e. 'Controller@method')
* @param mixed $priority Execution priority or MVC params.
* @param mixed $accepted_args Accepted args or priority.
* @param int $args Accepted args.
* @param string $hook WordPress hook name.
* @param string|array $mvc_call Lightweight MVC call. (i.e. 'Controller@method')
* @param mixed $priority Execution priority or MVC params.
* @param mixed $accepted_args Accepted args or priority.
* @param int $args Accepted args.
*/
public function add_filter( $hook, $mvc_call, $priority = 10, $accepted_args = 1, $args = 1 )
{
Expand All @@ -375,8 +375,8 @@ public function add_filter( $hook, $mvc_call, $priority = 10, $accepted_args = 1
* Adds a WordPress shortcode.
* @since 1.0.3
*
* @param string $tag WordPress tag name.
* @param string $mvc_call Lightweight MVC call. (i.e. 'Controller@method')
* @param string $tag WordPress tag name.
* @param string|array $mvc_call Lightweight MVC call. (i.e. 'Controller@method')
*/
public function add_shortcode( $tag, $mvc_call, $mvc_args = null )
{
Expand Down Expand Up @@ -721,13 +721,13 @@ public function _admin_assets()
* @since 3.1.15
*
* @param string $hook
* @param string $mvc_handler
* @param string|array $mvc_handler
* @param int $priority
*/
public function remove_action( $hook, $mvc_handler, $priority = 10 )
{
remove_action(
$hook,
$hook,
[ &$this, $this->get_mapped_mvc_call( $mvc_handler ) ],
$priority
);
Expand All @@ -738,26 +738,46 @@ public function remove_action( $hook, $mvc_handler, $priority = 10 )
* @since 3.1.15
*
* @param string $hook
* @param string $mvc_handler
* @param string|array $mvc_handler
* @param int $priority
*/
public function remove_filter( $hook, $mvc_handler, $priority = 10 )
{
remove_filter(
$hook,
$hook,
[ &$this, $this->get_mapped_mvc_call( $mvc_handler, true ) ],
$priority
);
}

/**
* Cast MVC call from PHP array Callbacks / Callables to typical string representation
* @since 3.1.18
*
* @param string|array $mvc_call Lightweight MVC call. (i.e. 'Controller@method', or [Controller::class, 'method'])
*
* @return string
*/
private function cast_mvc_call_to_string( $mvc_call )
{
if ( is_array( $mvc_call ) && count( $mvc_call ) === 2 ) {
$class_path = explode( '\\', $mvc_call[0] );
return end( $class_path ) . '@' . $mvc_call[1];
}
return $mvc_call;
}

/**
* Returns class method call mapped to a mvc engine method.
* @since 1.0.3
*
* @param string|array $call
*
* @return string
*/
private function get_mapped_mvc_call( $call, $return = false )
{
$call = $this->cast_mvc_call_to_string( $call );
return ( preg_match( '/[vV]iew\@/', $call ) ? '_v_' : '_c_' )
. ( $return ? 'return_' : 'void_' )
. $call;
Expand All @@ -767,19 +787,19 @@ private function get_mapped_mvc_call( $call, $return = false )
* Returns valid action filter item.
* @since 1.0.3
*
* @param string $hook WordPress hook name.
* @param string $mvc_call Lightweight MVC call. (i.e. 'Controller@method')
* @param mixed $priority Execution priority or MVC params.
* @param mixed $accepted_args Accepted args or priority.
* @param int $args Accepted args.
* @param string $hook WordPress hook name.
* @param string|array $mvc_call Lightweight MVC call. (i.e. 'Controller@method')
* @param mixed $priority Execution priority or MVC params.
* @param mixed $accepted_args Accepted args or priority.
* @param int $args Accepted args.
*
* @return array
*/
private function get_hook( $hook, $mvc_call, $priority = 10, $accepted_args = 1, $args = null )
{
return [
'hook' => $hook,
'mvc' => $mvc_call,
'mvc' => $this->cast_mvc_call_to_string( $mvc_call ),
'priority' => is_array( $priority ) ? $accepted_args : $priority,
'args' => is_array( $priority ) ? ( $args ? $args : count( $priority ) ) : $accepted_args,
'mvc_args' => is_array( $priority ) ? $priority : null,
Expand Down
35 changes: 27 additions & 8 deletions tests/cases/BridgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,14 @@ function testControllerParameterOverride()
// Prepare
global $config;
$main = new Main($config);
$main->add_action( 'test', 'TestController@filter', ['override'] );
$main->add_action( 'test1', 'TestController@filter', ['override'] );
$main->add_action( 'test2', [\UnitTesting\Controllers\TestController::class, 'filter'], ['override'] );
// Exec
$return = $main->{'_c_return_TestController@filter'}(123);
$return1 = $main->{'_c_return_TestController@filter'}(123);
$return2 = $main->{'_c_return_TestController@filter'}(123);
// Assert
$this->assertEquals('override', $return);
$this->assertEquals('override', $return1);
$this->assertEquals('override', $return2);
}
/**
* Tests view parameter override.
Expand Down Expand Up @@ -169,13 +172,16 @@ function testAddAction()
$main = new Main($config);
// Run
$main->add_action( 'test', 'TestController@testing' );
$main->add_action( 'test2', 'view@test2' );
$main->add_action( 'test2', [\UnitTesting\Controllers\TestController::class, 'testing'] );
$main->add_action( 'test3', 'view@test2' );
$main->add_hooks();
// Assert
$this->assertArrayHasKey('test', $hooks['actions']);
$this->assertArrayHasKey('test2', $hooks['actions']);
$this->assertArrayHasKey('test3', $hooks['actions']);
$this->assertEquals('[{},"_c_void_TestController@testing"]', json_encode($hooks['actions']['test']));
$this->assertEquals('[{},"_v_void_view@test2"]', json_encode($hooks['actions']['test2']));
$this->assertEquals('[{},"_c_void_TestController@testing"]', json_encode($hooks['actions']['test2']));
$this->assertEquals('[{},"_v_void_view@test2"]', json_encode($hooks['actions']['test3']));
}
/**
* Test method.
Expand All @@ -191,11 +197,14 @@ function testAddFilter()
global $hooks;
$main = new Main($config);
// Run
$main->add_filter( 'controller', 'FilterController@filtering' );
$main->add_filter( 'controller1', 'FilterController@filtering' );
$main->add_filter( 'controller2', [\UnitTesting\Controllers\TestController::class, 'filter'] );
$main->add_hooks();
// Assert
$this->assertArrayHasKey('controller', $hooks['filters']);
$this->assertEquals('[{},"_c_return_FilterController@filtering"]', json_encode($hooks['filters']['controller']));
$this->assertArrayHasKey('controller1', $hooks['filters']);
$this->assertArrayHasKey('controller2', $hooks['filters']);
$this->assertEquals('[{},"_c_return_FilterController@filtering"]', json_encode($hooks['filters']['controller1']));
$this->assertEquals('[{},"_c_return_TestController@filter"]', json_encode($hooks['filters']['controller2']));
}
/**
* Test method.
Expand All @@ -212,12 +221,17 @@ function testRemoveAction()
$main = new Main($config);
// Run
$main->add_action( 'action1', 'ActionController@to_remove' );
$main->add_action( 'action2', [\UnitTesting\Controllers\TestController::class, 'action'] );
$main->add_hooks();
$main->remove_action( 'action1', 'ActionController@to_remove' );
$main->remove_action( 'action2', [\UnitTesting\Controllers\TestController::class, 'action'] );
// Assert
$this->assertArrayNotHasKey('action1', $hooks['actions']);
$this->assertArrayHasKey('action1', $hooks['removed']);
$this->assertArrayNotHasKey('action2', $hooks['actions']);
$this->assertArrayHasKey('action2', $hooks['removed']);
$this->assertEquals('[{},"_c_void_ActionController@to_remove"]', json_encode($hooks['removed']['action1']));
$this->assertEquals('[{},"_c_void_TestController@action"]', json_encode($hooks['removed']['action2']));
}
/**
* Test method.
Expand All @@ -234,11 +248,16 @@ function testRemoveFilter()
$main = new Main($config);
// Run
$main->add_filter( 'filter1', 'FilterController@to_remove' );
$main->add_filter( 'filter2', [\UnitTesting\Controllers\TestController::class, 'filter'] );
$main->add_hooks();
$main->remove_filter( 'filter1', 'FilterController@to_remove' );
$main->remove_filter( 'filter2', [\UnitTesting\Controllers\TestController::class, 'filter'] );
// Assert
$this->assertArrayNotHasKey('filter1', $hooks['filters']);
$this->assertArrayHasKey('filter1', $hooks['removed']);
$this->assertArrayNotHasKey('filter2', $hooks['filters']);
$this->assertArrayHasKey('filter2', $hooks['removed']);
$this->assertEquals('[{},"_c_return_FilterController@to_remove"]', json_encode($hooks['removed']['filter1']));
$this->assertEquals('[{},"_c_return_TestController@filter"]', json_encode($hooks['removed']['filter2']));
}
}