From 6bdfeda4bf6ddd72a8167593d875d81646d5b97a Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Sun, 15 Dec 2024 22:39:24 +0100 Subject: [PATCH 1/2] Fix multiple callbacks with same name --- packages/phpunit-wp-hooks/README.md | 2 +- packages/phpunit-wp-hooks/src/IsolateHookTrait.php | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/phpunit-wp-hooks/README.md b/packages/phpunit-wp-hooks/README.md index c8e52b4..a359db0 100644 --- a/packages/phpunit-wp-hooks/README.md +++ b/packages/phpunit-wp-hooks/README.md @@ -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. diff --git a/packages/phpunit-wp-hooks/src/IsolateHookTrait.php b/packages/phpunit-wp-hooks/src/IsolateHookTrait.php index f80a9a1..638055f 100644 --- a/packages/phpunit-wp-hooks/src/IsolateHookTrait.php +++ b/packages/phpunit-wp-hooks/src/IsolateHookTrait.php @@ -22,9 +22,17 @@ protected function unregisterAllCallbacksExcept( $event_name, $method_name, $pri 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 { From 28a599ffe9c524d3da6b3ab76d9f08da01f2c75e Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Sun, 15 Dec 2024 23:21:07 +0100 Subject: [PATCH 2/2] Fixed tests --- packages/phpunit-wp-hooks/src/IsolateHookTrait.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/phpunit-wp-hooks/src/IsolateHookTrait.php b/packages/phpunit-wp-hooks/src/IsolateHookTrait.php index 638055f..6c6dad6 100644 --- a/packages/phpunit-wp-hooks/src/IsolateHookTrait.php +++ b/packages/phpunit-wp-hooks/src/IsolateHookTrait.php @@ -15,7 +15,9 @@ 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 ) {