@wordpress/hooks
Advanced tools
Comparing version 2.5.0 to 2.6.0
@@ -8,13 +8,22 @@ /** | ||
* @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(function (hook) { | ||
return hook.namespace === namespace; | ||
}); | ||
} | ||
return hookName in hooks; | ||
@@ -21,0 +30,0 @@ }; |
@@ -15,13 +15,22 @@ "use strict"; | ||
* @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(function (hook) { | ||
return hook.namespace === namespace; | ||
}); | ||
} | ||
return hookName in hooks; | ||
@@ -28,0 +37,0 @@ }; |
@@ -0,1 +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) | ||
@@ -5,3 +11,3 @@ | ||
- Enable support for the 'all' hook in non production environments. | ||
- Enable support for the 'all' hook in non production environments. | ||
@@ -8,0 +14,0 @@ ## 2.0.4 (2019-01-03) |
{ | ||
"name": "@wordpress/hooks", | ||
"version": "2.5.0", | ||
"version": "2.6.0", | ||
"description": "WordPress hooks library.", | ||
@@ -29,3 +29,3 @@ "author": "The WordPress Contributors", | ||
}, | ||
"gitHead": "2080b50d4a41c9f746485519d1dc368dd9d9354d" | ||
"gitHead": "989502eccaadee1ea7666d6d9fb9f4d08b274546" | ||
} |
@@ -42,4 +42,4 @@ # Hooks | ||
* `didFilter( 'hookName' )` | ||
* `hasAction( 'hookName' )` | ||
* `hasFilter( 'hookName' )` | ||
* `hasAction( 'hookName', 'namespace' )` | ||
* `hasFilter( 'hookName', 'namespace' )` | ||
* `actions` | ||
@@ -46,0 +46,0 @@ * `filters` |
@@ -8,13 +8,21 @@ /** | ||
* @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; | ||
@@ -21,0 +29,0 @@ }; |
@@ -1,3 +0,1 @@ | ||
/* eslint-disable no-console */ | ||
/** | ||
@@ -745,1 +743,36 @@ * Internal dependencies | ||
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 ); | ||
removeAction( 'test.action', 'my_callback' ); | ||
expect( hasAction( 'test.action', 'my_callback' ) ).toBe( false ); | ||
} ); | ||
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 ); | ||
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 ); | ||
} ); | ||
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 ); | ||
removeFilter( 'test.filter', 'my_callback' ); | ||
expect( hasFilter( 'test.filter', 'my_callback' ) ).toBe( false ); | ||
} ); | ||
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 ); | ||
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 ); | ||
} ); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
154657
2108