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
2 changes: 1 addition & 1 deletion packages/phpunit-wp-hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Where 15 is the priority from the filter. If you forget this part the default pr
To isolate a hook to reduce it to a single callback it is possible by adding `@hook-isolated` annotation on the test method:
```php
/**
* @hook-isolate my-event myCallback 15
* @hook-isolated my-event myCallback 15
*/
```
Where 15 is the priority from the filter. If you forget this part the default priority will be 10.
18 changes: 14 additions & 4 deletions packages/phpunit-wp-hooks/src/IsolateHookTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,26 @@ protected function unregisterAllCallbacksExcept( $event_name, $method_name, $pri
global $wp_filter;
$this->original_wp_filter[ $event_name ] = $wp_filter[ $event_name ]->callbacks;

foreach ( $this->original_wp_filter[ $event_name ][ $priority ] as $key => $config ) {
$callbacks = [];

foreach ( $this->original_wp_filter[ $event_name ][ $priority ] as $key => $config ) {

// Skip if not this tests callback.
if ( substr( $key, - strlen( $method_name ) ) !== $method_name ) {
continue;
}

$wp_filter[ $event_name ]->callbacks = [
$priority => [ $key => $config ],
];
if(key_exists($priority, $callbacks)) {
$callbacks = [
$priority => array_merge_recursive($callbacks[$priority], [ $key => $config ])
];
}else {
$callbacks = [
$priority => [ $key => $config ],
];
}

$wp_filter[ $event_name ]->callbacks = $callbacks;
}

try {
Expand Down
Loading