Skip to content

Commit

Permalink
Merge pull request #52 from conduit-innovation/51-injection-does-not-…
Browse files Browse the repository at this point in the history
…bind-scope-correctly

fix(#51): injection does not bind scope correctly
  • Loading branch information
talss89 committed May 1, 2023
2 parents 4b892b4 + 544d8e3 commit e0dec45
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
20 changes: 15 additions & 5 deletions src/class/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,23 @@ public function inject($before, $after = false) {
$original_callback = &$this->original_callback;

$this->replace(function(...$args) use ($before, $after, $original_callback) {
if($before)
$args[0] = $before(...$args);

if($before) {
if(_is_callable_object($original_callback)) {
$args[0] = (new HookProxy($before, $original_callback[0]))->__cb(...$args);
} else {
$args[0] = $before(...$args);
}
}

$args[0] = call_user_func_array($original_callback, $args);

if($after)
$args[0] = $after(...$args);
if($after) {
if(_is_callable_object($original_callback)) {
$args[0] = (new HookProxy($after, $original_callback[0]))->__cb(...$args);
} else {
$args[0] = $after(...$args);
}
}

return $args[0];
});
Expand Down
12 changes: 12 additions & 0 deletions src/inc/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@ function add_filters(string | array $hook_names, $callback, $priority = 10, $acc
foreach($hook_names as $hook_name) {
add_filter($hook_name, $callback, $priority, $accepted_args);
}
}

/**
* _is_callable_object
*
* @param mixed $callable
* @return void
*/

function _is_callable_object(callable $callable) {
if(is_array($callable) && is_object($callable[0])) return true;
return false;
}
44 changes: 41 additions & 3 deletions unit/tests/HookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

use function GorillaClaw\find_filters;

function dummy_func($input) { return $input; }

final class HookTest extends WPFilterTestCase {

protected function setUp(): void {
Expand Down Expand Up @@ -131,12 +133,14 @@ public function testHookBeforeAfter() {
$hooks = find_filters('test_inject');

$hooks->inject(function($input) {
return $input . '-before';
$this->{'id'} = 1;
return $input . '-before-' . $this->{'get_private_id'}();
}, function($input) {
return $input . '-after';
$this->{'id'} = 2;
return $input . '-after-' . $this->{'get_id'}();
});

$this->assertEquals('test-before-obj-after', apply_filters('test_inject', 'test'));
$this->assertEquals('test-before-1-obj-after-2', apply_filters('test_inject', 'test'));

add_filter('test_inject_2', [$test_object, 'test'], 10);

Expand All @@ -159,6 +163,40 @@ public function testHookBeforeAfter() {
$this->assertEquals('test-before-obj', apply_filters('test_inject_3', 'test'));
}

public function testHookBeforeAfterNoBinding() {
$test_object = new MockClass();

add_filter('test_inject', function($input) { return $input; }, 10);

$hooks = find_filters('test_inject');

$hooks->inject(function($input) {
return $input . '-before';
}, function($input) {
return $input . '-after';
});

$this->assertEquals('test-before-after', apply_filters('test_inject', 'test'));

}

public function testHookBeforeAfterNoBindingPlain() {
$test_object = new MockClass();

add_filter('test_inject', __NAMESPACE__. '\dummy_func', 10);

$hooks = find_filters('test_inject');

$hooks->inject(function($input) {
return $input . '-before';
}, function($input) {
return $input . '-after';
});

$this->assertEquals('test-before-after', apply_filters('test_inject', 'test'));

}

public function testTolerateBrokenWpFilter() {
global $wp_filter;

Expand Down

0 comments on commit e0dec45

Please sign in to comment.