From bbdefa14822297fe1468c255ccd6963ed6c7b478 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Tue, 30 Apr 2019 18:46:44 -0600 Subject: [PATCH 1/9] from https://github.com/WordPress/packages/pull/106/files --- packages/hooks/src/createHasHook.js | 15 ++++++++--- packages/hooks/src/test/index.test.js | 38 +++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/packages/hooks/src/createHasHook.js b/packages/hooks/src/createHasHook.js index de4ee3c17a793c..f72edd6566ca32 100644 --- a/packages/hooks/src/createHasHook.js +++ b/packages/hooks/src/createHasHook.js @@ -5,17 +5,24 @@ * @param {Object} hooks Stored hooks, keyed by hook name. * * @return {Function} Function that returns whether any handlers are - * attached to a particular hook. + * attached to a particular hook and optional namespace. */ function createHasHook( hooks ) { /** - * Returns how many handlers are attached for the given hook. + * Returns whether any handlers are attached for the given hookName and optional namespace. * - * @param {string} hookName The name of the hook to check for. + * @param {string} hookName The name of the hook to check for. + * @param {string} namespace Optional. The unique namespace identifying the callback in the form `vendor/plugin/function`. * * @return {boolean} Whether there are handlers that are attached to the given hook. */ - return function hasHook( hookName ) { + return function hasHook( hookName, namespace ) { + // Use the namespace if provided. + if ( 'undefined' !== typeof namespace ) { + return hookName in hooks && + hooks[ hookName ].handlers.some( ( hook ) => hook.namespace === namespace ); + } + return hookName in hooks; }; } diff --git a/packages/hooks/src/test/index.test.js b/packages/hooks/src/test/index.test.js index a31a0d2d113ced..9f482dee4969aa 100644 --- a/packages/hooks/src/test/index.test.js +++ b/packages/hooks/src/test/index.test.js @@ -697,3 +697,41 @@ test( 'removing a filter triggers a hookRemoved action passing all callback deta 'my_callback3' ); } ); + +test( 'checking hasAction and hasFilter with named callbacks', () => { + // hasAction tests + addAction( 'test.action', 'my_callback', () => {} ); + expect( hasAction( 'test.action', 'not_my_callback' ) ).toBe( false ); + expect( hasAction( 'test.action', 'my_callback' ) ).toBe( true ); + + // test removing + removeAction( 'test.action', 'my_callback' ); + expect( hasAction( 'test.action', 'my_callback' ) ).toBe( false ); + + // test removeAll + addAction( 'test.action', 'my_callback', () => {} ); + addAction( 'test.action', 'my_second_callback', () => {} ); + expect( hasAction( 'test.action', 'my_callback' ) ).toBe( true ); + expect( hasAction( 'test.action', 'my_callback' ) ).toBe( true ); + removeAllActions( 'test.action' ); + expect( hasAction( 'test.action', 'my_callback' ) ).toBe( false ); + expect( hasAction( 'test.action', 'my_callback' ) ).toBe( false ); + + // hasFilter tests + addFilter( 'test.filter', 'my_callback', () => {} ); + expect( hasFilter( 'test.filter', 'not_my_callback' ) ).toBe( false ); + expect( hasFilter( 'test.filter', 'my_callback' ) ).toBe( true ); + + // test removing + removeFilter( 'test.filter', 'my_callback' ); + expect( hasFilter( 'test.filter', 'my_callback' ) ).toBe( false ); + + // test removeAll + addFilter( 'test.filter', 'my_callback', () => {} ); + addFilter( 'test.filter', 'my_second_callback', () => {} ); + expect( hasFilter( 'test.filter', 'my_callback' ) ).toBe( true ); + expect( hasFilter( 'test.filter', 'my_second_callback' ) ).toBe( true ); + removeAllFilters( 'test.filter' ); + expect( hasFilter( 'test.filter', 'my_callback' ) ).toBe( false ); + expect( hasFilter( 'test.filter', 'my_second_callback' ) ).toBe( false ); +} ); From cabd368b0592e31cafb493e30ba981235bbe72c7 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Tue, 25 Jun 2019 18:13:13 -0500 Subject: [PATCH 2/9] Update packages/hooks/src/createHasHook.js Co-Authored-By: Pascal Birchler --- packages/hooks/src/createHasHook.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/createHasHook.js b/packages/hooks/src/createHasHook.js index f72edd6566ca32..1629e5cc910ce1 100644 --- a/packages/hooks/src/createHasHook.js +++ b/packages/hooks/src/createHasHook.js @@ -12,7 +12,7 @@ function createHasHook( hooks ) { * Returns whether any handlers are attached for the given hookName and optional namespace. * * @param {string} hookName The name of the hook to check for. - * @param {string} namespace Optional. The unique namespace identifying the callback in the form `vendor/plugin/function`. + * @param {?string} namespace Optional. The unique namespace identifying the callback in the form `vendor/plugin/function`. * * @return {boolean} Whether there are handlers that are attached to the given hook. */ From 7168882b8d42a7c7d897a3066d08c72909b830b4 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Mon, 12 Aug 2019 13:41:49 -0600 Subject: [PATCH 3/9] clean up docblock spacing --- packages/hooks/src/createHasHook.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/hooks/src/createHasHook.js b/packages/hooks/src/createHasHook.js index 1629e5cc910ce1..7e2474ccf8ae0b 100644 --- a/packages/hooks/src/createHasHook.js +++ b/packages/hooks/src/createHasHook.js @@ -11,8 +11,9 @@ function createHasHook( hooks ) { /** * Returns whether any handlers are attached for the given hookName and optional namespace. * - * @param {string} hookName The name of the hook to check for. - * @param {?string} namespace Optional. The unique namespace identifying the callback in the form `vendor/plugin/function`. + * @param {string} hookName The name of the hook to check for. + * @param {?string} namespace Optional. The unique namespace identifying the callback + * in the form `vendor/plugin/function`. * * @return {boolean} Whether there are handlers that are attached to the given hook. */ From cf06b5fd2b85ef05f3ef3b2bfc491e5362c64fca Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Mon, 12 Aug 2019 13:48:22 -0600 Subject: [PATCH 4/9] add changelog entry for new namespace parameter for hasAction & hasFilter --- packages/hooks/CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/hooks/CHANGELOG.md b/packages/hooks/CHANGELOG.md index cb1625c912898f..13e71597373500 100644 --- a/packages/hooks/CHANGELOG.md +++ b/packages/hooks/CHANGELOG.md @@ -1,8 +1,12 @@ +## 2.5.0 (2019-08-12) + + - Enable an optional namespace parameter for `hasAction` & `hasFilter`. When checking if an action or filter exists, `hasAction` and `hasFilter` now accept an optional paramter to limit matches by namespace. + ## 2.4.0 (2019-06-12) ### New Feature -- Enable support for the 'all' hook in non production environments. +- Enable support for the 'all' hook in non production environments. ## 2.0.4 (2019-01-03) From bde71dad31877d3175bb4970ddd4fa2669f84f44 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Thu, 15 Aug 2019 16:41:01 -0600 Subject: [PATCH 5/9] Update packages/hooks/CHANGELOG.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Grzegorz (Greg) Ziółkowski --- packages/hooks/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/CHANGELOG.md b/packages/hooks/CHANGELOG.md index 13e71597373500..d65e84c23a5c5d 100644 --- a/packages/hooks/CHANGELOG.md +++ b/packages/hooks/CHANGELOG.md @@ -1,4 +1,4 @@ -## 2.5.0 (2019-08-12) +## Master - Enable an optional namespace parameter for `hasAction` & `hasFilter`. When checking if an action or filter exists, `hasAction` and `hasFilter` now accept an optional paramter to limit matches by namespace. From 1aae3f038d51f338efd829cf28943ea5ed6abc25 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Mon, 19 Aug 2019 14:25:30 -0600 Subject: [PATCH 6/9] break out tests --- packages/hooks/src/test/index.test.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/hooks/src/test/index.test.js b/packages/hooks/src/test/index.test.js index 3d5d0eeb9bb94f..26d04853ede06c 100644 --- a/packages/hooks/src/test/index.test.js +++ b/packages/hooks/src/test/index.test.js @@ -740,17 +740,15 @@ test( 'add multiple all actions and run it any hook to trigger them by priority' expect( window.actionValue ).toBe( 'ba' ); } ); -test( 'checking hasAction and hasFilter with named callbacks', () => { - // hasAction tests +test( 'checking hasAction with named callbacks and removing', () => { addAction( 'test.action', 'my_callback', () => {} ); expect( hasAction( 'test.action', 'not_my_callback' ) ).toBe( false ); expect( hasAction( 'test.action', 'my_callback' ) ).toBe( true ); - - // test removing removeAction( 'test.action', 'my_callback' ); expect( hasAction( 'test.action', 'my_callback' ) ).toBe( false ); +} ); - // test removeAll +test( 'checking hasAction with named callbacks and removeAllActions', () => { addAction( 'test.action', 'my_callback', () => {} ); addAction( 'test.action', 'my_second_callback', () => {} ); expect( hasAction( 'test.action', 'my_callback' ) ).toBe( true ); @@ -758,17 +756,17 @@ test( 'checking hasAction and hasFilter with named callbacks', () => { removeAllActions( 'test.action' ); expect( hasAction( 'test.action', 'my_callback' ) ).toBe( false ); expect( hasAction( 'test.action', 'my_callback' ) ).toBe( false ); +} ); - // hasFilter tests +test( 'checking hasFilter with named callbacks and removing', () => { addFilter( 'test.filter', 'my_callback', () => {} ); expect( hasFilter( 'test.filter', 'not_my_callback' ) ).toBe( false ); expect( hasFilter( 'test.filter', 'my_callback' ) ).toBe( true ); - - // test removing removeFilter( 'test.filter', 'my_callback' ); expect( hasFilter( 'test.filter', 'my_callback' ) ).toBe( false ); +} ); - // test removeAll +test( 'checking hasFilter with named callbacks and removeAllActions', () => { addFilter( 'test.filter', 'my_callback', () => {} ); addFilter( 'test.filter', 'my_second_callback', () => {} ); expect( hasFilter( 'test.filter', 'my_callback' ) ).toBe( true ); From 91bfe3c80de8cf6736dd0610241d54bb47be85f0 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Mon, 19 Aug 2019 14:27:28 -0600 Subject: [PATCH 7/9] update readme --- packages/hooks/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/hooks/README.md b/packages/hooks/README.md index 952931c393bd15..326093170d090a 100644 --- a/packages/hooks/README.md +++ b/packages/hooks/README.md @@ -39,8 +39,8 @@ In the WordPress context, API functions can be called via the global `wp.hooks` * `doingFilter( 'hookName' )` * `didAction( 'hookName' )` * `didFilter( 'hookName' )` -* `hasAction( 'hookName' )` -* `hasFilter( 'hookName' )` +* `hasAction( 'hookName', 'namespace )` +* `hasFilter( 'hookName' 'namespace' )` * `actions` * `filters` From 51321b4789d4a3325dd63d1151a259f71d5a0d31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=28Greg=29=20Zi=C3=B3=C5=82kowski?= Date: Mon, 26 Aug 2019 10:47:25 +0200 Subject: [PATCH 8/9] Apply suggestions from code review Co-Authored-By: Daniel Richards --- packages/hooks/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/hooks/README.md b/packages/hooks/README.md index 326093170d090a..4698580f4b4ed5 100644 --- a/packages/hooks/README.md +++ b/packages/hooks/README.md @@ -39,8 +39,8 @@ In the WordPress context, API functions can be called via the global `wp.hooks` * `doingFilter( 'hookName' )` * `didAction( 'hookName' )` * `didFilter( 'hookName' )` -* `hasAction( 'hookName', 'namespace )` -* `hasFilter( 'hookName' 'namespace' )` +* `hasAction( 'hookName', 'namespace' )` +* `hasFilter( 'hookName', 'namespace' )` * `actions` * `filters` From f3515b1c435556d46e7c155e29ed5602f35310ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=28Greg=29=20Zi=C3=B3=C5=82kowski?= Date: Mon, 26 Aug 2019 11:49:57 +0200 Subject: [PATCH 9/9] Update CHANGELOG.md --- packages/hooks/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/hooks/CHANGELOG.md b/packages/hooks/CHANGELOG.md index d65e84c23a5c5d..5e11d271d24415 100644 --- a/packages/hooks/CHANGELOG.md +++ b/packages/hooks/CHANGELOG.md @@ -1,5 +1,7 @@ ## Master +### New Feature + - Enable an optional namespace parameter for `hasAction` & `hasFilter`. When checking if an action or filter exists, `hasAction` and `hasFilter` now accept an optional paramter to limit matches by namespace. ## 2.4.0 (2019-06-12)