Socket
Socket
Sign inDemoInstall

@wordpress/hooks

Package Overview
Dependencies
Maintainers
4
Versions
158
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wordpress/hooks - npm Package Compare versions

Comparing version 0.1.0-beta.4 to 0.1.0-beta.5

README.md.orig

56

build-module/index.js

@@ -1,2 +0,1 @@

import HOOKS from './hooks';
import createAddHook from './createAddHook';

@@ -10,32 +9,27 @@ import createRemoveHook from './createRemoveHook';

// Add action/filter functions.
export var addAction = createAddHook(HOOKS.actions);
export var addFilter = createAddHook(HOOKS.filters);
function createHooks() {
var actions = {};
var filters = {};
// Remove action/filter functions.
export var removeAction = createRemoveHook(HOOKS.actions);
export var removeFilter = createRemoveHook(HOOKS.filters);
// Has action/filter functions.
export var hasAction = createHasHook(HOOKS.actions);
export var hasFilter = createHasHook(HOOKS.filters);
// Remove all actions/filters functions.
export var removeAllActions = createRemoveHook(HOOKS.actions, true);
export var removeAllFilters = createRemoveHook(HOOKS.filters, true);
// Do action/apply filters functions.
export var doAction = createRunHook(HOOKS.actions);
export var applyFilters = createRunHook(HOOKS.filters, true);
// Current action/filter functions.
export var currentAction = createCurrentHook(HOOKS.actions);
export var currentFilter = createCurrentHook(HOOKS.filters);
// Doing action/filter: true while a hook is being run.
export var doingAction = createDoingHook(HOOKS.actions);
export var doingFilter = createDoingHook(HOOKS.filters);
// Did action/filter functions.
export var didAction = createDidHook(HOOKS.actions);
export var didFilter = createDidHook(HOOKS.filters);
return {
addAction: createAddHook(actions),
addFilter: createAddHook(filters),
removeAction: createRemoveHook(actions),
removeFilter: createRemoveHook(filters),
hasAction: createHasHook(actions),
hasFilter: createHasHook(filters),
removeAllActions: createRemoveHook(actions, true),
removeAllFilters: createRemoveHook(filters, true),
doAction: createRunHook(actions),
applyFilters: createRunHook(filters, true),
currentAction: createCurrentHook(actions),
currentFilter: createCurrentHook(filters),
doingAction: createDoingHook(actions),
doingFilter: createDoingHook(filters),
didAction: createDidHook(actions),
didFilter: createDidHook(filters),
actions: actions,
filters: filters
};
};
export default createHooks;

@@ -16,12 +16,7 @@ /**

if (!/^[a-zA-Z][a-zA-Z0-9_.\-/]*$/.test(namespace)) {
console.error('The namespace can only contain numbers, letters, dashes, periods and underscores, plus the forward slash dividing slug and description in the namespace.');
if (!/^[a-zA-Z][a-zA-Z0-9_.\-]*$/.test(namespace)) {
console.error('The namespace can only contain numbers, letters, dashes, periods and underscores.');
return false;
}
if (!/^[a-zA-Z][a-zA-Z0-9_.\-]*\/[a-zA-Z][a-zA-Z0-9_.\-]*\/[a-zA-Z][a-zA-Z0-9_.\-]*$/.test(namespace)) {
console.error('The namespace must take the form `vendor/plugin/function`.');
return false;
}
return true;

@@ -28,0 +23,0 @@ }

'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports.didFilter = exports.didAction = exports.doingFilter = exports.doingAction = exports.currentFilter = exports.currentAction = exports.applyFilters = exports.doAction = exports.removeAllFilters = exports.removeAllActions = exports.hasFilter = exports.hasAction = exports.removeFilter = exports.removeAction = exports.addFilter = exports.addAction = undefined;
var _hooks = require('./hooks');
var _hooks2 = _interopRequireDefault(_hooks);
var _createAddHook = require('./createAddHook');

@@ -42,32 +37,27 @@

// Add action/filter functions.
var addAction = exports.addAction = (0, _createAddHook2.default)(_hooks2.default.actions);
var addFilter = exports.addFilter = (0, _createAddHook2.default)(_hooks2.default.filters);
function createHooks() {
var actions = {};
var filters = {};
// Remove action/filter functions.
var removeAction = exports.removeAction = (0, _createRemoveHook2.default)(_hooks2.default.actions);
var removeFilter = exports.removeFilter = (0, _createRemoveHook2.default)(_hooks2.default.filters);
// Has action/filter functions.
var hasAction = exports.hasAction = (0, _createHasHook2.default)(_hooks2.default.actions);
var hasFilter = exports.hasFilter = (0, _createHasHook2.default)(_hooks2.default.filters);
// Remove all actions/filters functions.
var removeAllActions = exports.removeAllActions = (0, _createRemoveHook2.default)(_hooks2.default.actions, true);
var removeAllFilters = exports.removeAllFilters = (0, _createRemoveHook2.default)(_hooks2.default.filters, true);
// Do action/apply filters functions.
var doAction = exports.doAction = (0, _createRunHook2.default)(_hooks2.default.actions);
var applyFilters = exports.applyFilters = (0, _createRunHook2.default)(_hooks2.default.filters, true);
// Current action/filter functions.
var currentAction = exports.currentAction = (0, _createCurrentHook2.default)(_hooks2.default.actions);
var currentFilter = exports.currentFilter = (0, _createCurrentHook2.default)(_hooks2.default.filters);
// Doing action/filter: true while a hook is being run.
var doingAction = exports.doingAction = (0, _createDoingHook2.default)(_hooks2.default.actions);
var doingFilter = exports.doingFilter = (0, _createDoingHook2.default)(_hooks2.default.filters);
// Did action/filter functions.
var didAction = exports.didAction = (0, _createDidHook2.default)(_hooks2.default.actions);
var didFilter = exports.didFilter = (0, _createDidHook2.default)(_hooks2.default.filters);
return {
addAction: (0, _createAddHook2.default)(actions),
addFilter: (0, _createAddHook2.default)(filters),
removeAction: (0, _createRemoveHook2.default)(actions),
removeFilter: (0, _createRemoveHook2.default)(filters),
hasAction: (0, _createHasHook2.default)(actions),
hasFilter: (0, _createHasHook2.default)(filters),
removeAllActions: (0, _createRemoveHook2.default)(actions, true),
removeAllFilters: (0, _createRemoveHook2.default)(filters, true),
doAction: (0, _createRunHook2.default)(actions),
applyFilters: (0, _createRunHook2.default)(filters, true),
currentAction: (0, _createCurrentHook2.default)(actions),
currentFilter: (0, _createCurrentHook2.default)(filters),
doingAction: (0, _createDoingHook2.default)(actions),
doingFilter: (0, _createDoingHook2.default)(filters),
didAction: (0, _createDidHook2.default)(actions),
didFilter: (0, _createDidHook2.default)(filters),
actions: actions,
filters: filters
};
};
exports.default = createHooks;

@@ -21,12 +21,7 @@ 'use strict';

if (!/^[a-zA-Z][a-zA-Z0-9_.\-/]*$/.test(namespace)) {
console.error('The namespace can only contain numbers, letters, dashes, periods and underscores, plus the forward slash dividing slug and description in the namespace.');
if (!/^[a-zA-Z][a-zA-Z0-9_.\-]*$/.test(namespace)) {
console.error('The namespace can only contain numbers, letters, dashes, periods and underscores.');
return false;
}
if (!/^[a-zA-Z][a-zA-Z0-9_.\-]*\/[a-zA-Z][a-zA-Z0-9_.\-]*\/[a-zA-Z][a-zA-Z0-9_.\-]*$/.test(namespace)) {
console.error('The namespace must take the form `vendor/plugin/function`.');
return false;
}
return true;

@@ -33,0 +28,0 @@ }

{
"name": "@wordpress/hooks",
"version": "0.1.0-beta.4",
"version": "0.1.0-beta.5",
"repository": {

@@ -5,0 +5,0 @@ "type": "git",

@@ -17,27 +17,26 @@ # @wordpress/hooks

* `addAction( 'hook', 'vendor/plugin/function', callback, priority )`
* `addFilter( 'hook', 'vendor/plugin/function', callback, priority )`
* `removeAction( 'hook', 'vendor/plugin/function' )`
* `removeFilter( 'hook', 'vendor/plugin/function' )`
* `removeAllActions( 'hook' )`
* `removeAllFilters( 'hook' )`
* `doAction( 'hook', arg1, arg2, moreArgs, finalArg )`
* `applyFilters( 'hook', content, arg1, arg2, moreArgs, finalArg )`
* `doingAction( 'hook' )`
* `doingFilter( 'hook' )`
* `didAction( 'hook' )`
* `didFilter( 'hook' )`
* `hasAction( 'hook' )`
* `hasFilter( 'hook' )`
A lightweight & efficient filter and action manager.
### API Usage
### Background
See ticket [#21170](http://core.trac.wordpress.org/ticket/21170) for more information.
* `addAction( 'hookName', 'functionName', callback, priority )`
* `addFilter( 'hookName', 'functionName', callback, priority )`
* `removeAction( 'hookName', 'functionName' )`
* `removeFilter( 'hookName', 'functionName' )`
* `removeAllActions( 'hookName' )`
* `removeAllFilters( 'hookName' )`
* `doAction( 'hookName', arg1, arg2, moreArgs, finalArg )`
* `applyFilters( 'hookName', content, arg1, arg2, moreArgs, finalArg )`
* `doingAction( 'hookName' )`
* `doingFilter( 'hookName' )`
* `didAction( 'hookName' )`
* `didFilter( 'hookName' )`
* `hasAction( 'hookName' )`
* `hasFilter( 'hookName' )`
Hooks can be added to an object via composition:
`import createHooks from '../';`
### Features
`myObject.hooks = createHooks();`
* Fast and lightweight.
* Priorities system ensures hooks with lower integer priority are fired first.
* Uses native object hash lookup for finding hook callbacks.
* Utilizes insertion sort for keeping priorities correct. Best Case: O(n), worst case: O(n^2)
API functions are then be called: `myObject.hooks.addAction()`...

@@ -1,2 +0,1 @@

import HOOKS from './hooks';
import createAddHook from './createAddHook';

@@ -10,32 +9,27 @@ import createRemoveHook from './createRemoveHook';

// Add action/filter functions.
export const addAction = createAddHook( HOOKS.actions );
export const addFilter = createAddHook( HOOKS.filters );
function createHooks() {
const actions = {};
const filters = {};
// Remove action/filter functions.
export const removeAction = createRemoveHook( HOOKS.actions );
export const removeFilter = createRemoveHook( HOOKS.filters );
// Has action/filter functions.
export const hasAction = createHasHook( HOOKS.actions );
export const hasFilter = createHasHook( HOOKS.filters );
// Remove all actions/filters functions.
export const removeAllActions = createRemoveHook( HOOKS.actions, true );
export const removeAllFilters = createRemoveHook( HOOKS.filters, true );
// Do action/apply filters functions.
export const doAction = createRunHook( HOOKS.actions );
export const applyFilters = createRunHook( HOOKS.filters, true );
// Current action/filter functions.
export const currentAction = createCurrentHook( HOOKS.actions );
export const currentFilter = createCurrentHook( HOOKS.filters );
// Doing action/filter: true while a hook is being run.
export const doingAction = createDoingHook( HOOKS.actions );
export const doingFilter = createDoingHook( HOOKS.filters );
// Did action/filter functions.
export const didAction = createDidHook( HOOKS.actions );
export const didFilter = createDidHook( HOOKS.filters );
return {
addAction: createAddHook( actions ),
addFilter: createAddHook( filters ),
removeAction: createRemoveHook( actions ),
removeFilter: createRemoveHook( filters ),
hasAction: createHasHook( actions ),
hasFilter: createHasHook( filters ),
removeAllActions: createRemoveHook( actions, true ),
removeAllFilters: createRemoveHook( filters, true ),
doAction: createRunHook( actions ),
applyFilters: createRunHook( filters, true ),
currentAction: createCurrentHook( actions ),
currentFilter: createCurrentHook( filters ),
doingAction: createDoingHook( actions ),
doingFilter: createDoingHook( filters ),
didAction: createDidHook( actions ),
didFilter: createDidHook( filters ),
actions: actions,
filters: filters,
};
};
export default createHooks;

@@ -6,22 +6,7 @@ /* eslint-disable no-console */

*/
import HOOKS from '../hooks';
import {
addAction,
addFilter,
removeAction,
removeFilter,
removeAllActions,
removeAllFilters,
hasAction,
hasFilter,
doAction,
applyFilters,
currentAction,
currentFilter,
doingAction,
doingFilter,
didAction,
didFilter,
} from '../';
import createHooks from '../';
const testObject = {};
testObject.hooks = createHooks();
function filter_a( str ) {

@@ -40,3 +25,3 @@ return str + 'a';

function filter_b_removes_self( str ) {
removeFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_b_removes_self' );
testObject.hooks.removeFilter( 'test.filter', 'my_callback_filter_b_removes_self' );
return str + 'b';

@@ -46,3 +31,3 @@ }

function filter_removes_b( str ) {
removeFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_b' );
testObject.hooks.removeFilter( 'test.filter', 'my_callback_filter_b' );
return str;

@@ -52,3 +37,3 @@ }

function filter_removes_c( str ) {
removeFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_c' );
testObject.hooks.removeFilter( 'test.filter', 'my_callback_filter_c' );
return str;

@@ -73,6 +58,6 @@ }

window.actionValue = '';
// Reset state in between tests (clear all callbacks, `didAction` counts,
// etc.) Just reseting HOOKS.actions and HOOKS.filters is not enough
// Reset state in between tests (clear all callbacks, `testObject.hooks.didAction` counts,
// etc.) Just reseting actions and filters is not enough
// because the internal functions have references to the original objects.
[ HOOKS.actions, HOOKS.filters ].forEach( hooks => {
[ testObject.hooks.actions, testObject.hooks.filters ].forEach( hooks => {
for ( const k in hooks ) {

@@ -89,31 +74,35 @@ delete hooks[ k ];

test( 'hooks can be instantiated', () => {
expect( typeof testObject.hooks ).toEqual( 'object' );
} );
test( 'run a filter with no callbacks', () => {
expect( applyFilters( 'test.filter', 42 ) ).toEqual( 42 );
expect( testObject.hooks.applyFilters( 'test.filter', 42 ) ).toEqual( 42 );
} );
test( 'add and remove a filter', () => {
addFilter( 'test.filter', 'my_name/my_plugin/my_callback', filter_a );
expect( removeAllFilters( 'test.filter' ) ).toEqual( 1 );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'test' );
expect( removeAllFilters( 'test.filter' ) ).toEqual( 0 );
testObject.hooks.addFilter( 'test.filter', 'my_callback', filter_a );
expect( testObject.hooks.removeAllFilters( 'test.filter' ) ).toEqual( 1 );
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'test' );
expect( testObject.hooks.removeAllFilters( 'test.filter' ) ).toEqual( 0 );
} );
test( 'add a filter and run it', () => {
addFilter( 'test.filter', 'my_name/my_plugin/my_callback', filter_a );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testa' );
testObject.hooks.addFilter( 'test.filter', 'my_callback', filter_a );
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testa' );
} );
test( 'add 2 filters in a row and run them', () => {
addFilter( 'test.filter', 'my_name/my_plugin/my_callback', filter_a );
addFilter( 'test.filter', 'my_name/my_plugin/my_callback', filter_b );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testab' );
testObject.hooks.addFilter( 'test.filter', 'my_callback', filter_a );
testObject.hooks.addFilter( 'test.filter', 'my_callback', filter_b );
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testab' );
} );
test( 'remove a non-existent filter', () => {
expect( removeFilter( 'test.filter', 'my_name/my_plugin/my_callback', filter_a ) ).toEqual( 0 );
expect( removeAllFilters( 'test.filter' ) ).toEqual( 0 );
expect( testObject.hooks.removeFilter( 'test.filter', 'my_callback', filter_a ) ).toEqual( 0 );
expect( testObject.hooks.removeAllFilters( 'test.filter' ) ).toEqual( 0 );
} );
test( 'remove an invalid namespace from a filter', () => {
expect( removeFilter( 'test.filter', 42 ) ).toEqual( undefined );
expect( testObject.hooks.removeFilter( 'test.filter', 42 ) ).toEqual( undefined );
expect( console.error ).toHaveBeenCalledWith(

@@ -125,3 +114,3 @@ 'The namespace must be a non-empty string.'

test( 'cannot add filters with non-string hook names', () => {
addFilter( 42, 'my_name/my_plugin/my_callback', () => null );
testObject.hooks.addFilter( 42, 'my_callback', () => null );
expect( console.error ).toHaveBeenCalledWith(

@@ -133,3 +122,3 @@ 'The hook name must be a non-empty string.'

test( 'cannot add filters with empty-string hook names', () => {
addFilter( '', 'my_name/my_plugin/my_callback', () => null );
testObject.hooks.addFilter( '', 'my_callback', () => null );
expect( console.error ).toHaveBeenCalledWith(

@@ -141,3 +130,3 @@ 'The hook name must be a non-empty string.'

test( 'cannot add filters with empty-string namespaces', () => {
addFilter( 'hook_name', '', () => null );
testObject.hooks.addFilter( 'hook_name', '', () => null );
expect( console.error ).toHaveBeenCalledWith(

@@ -149,5 +138,5 @@ 'The namespace must be a non-empty string.'

test( 'cannot add filters with invalid namespaces', () => {
addFilter( 'hook_name', 'invalid_name', () => null );
testObject.hooks.addFilter( 'hook_name', 'invalid_%&name', () => null );
expect( console.error ).toHaveBeenCalledWith(
'The namespace must take the form `vendor/plugin/function`.'
'The namespace can only contain numbers, letters, dashes, periods and underscores.'
);

@@ -157,5 +146,5 @@ } );

test( 'cannot add filters with namespaces missing a functionDescription', () => {
addFilter( 'hook_name', 'invalid_name/', () => null );
testObject.hooks.addFilter( 'hook_name', 'invalid_name/', () => null );
expect( console.error ).toHaveBeenCalledWith(
'The namespace must take the form `vendor/plugin/function`.'
'The namespace can only contain numbers, letters, dashes, periods and underscores.'
);

@@ -165,3 +154,3 @@ } );

test( 'Can add filters with dashes in namespaces', () => {
addFilter( 'hook_name', 'my_name/with-dashes/action', () => null );
testObject.hooks.addFilter( 'hook_name', 'with-dashes', () => null );
expect( console.error ).toHaveBeenCalledTimes( 0 );

@@ -171,3 +160,3 @@ } );

test( 'Can add filters with capitals in namespaces', () => {
addFilter( 'hook_name', 'my_name/OhNo/action', () => null );
testObject.hooks.addFilter( 'hook_name', 'my_name-OhNoaction', () => null );
expect( console.error ).toHaveBeenCalledTimes( 0 );

@@ -177,3 +166,3 @@ } );

test( 'Can add filters with capitals in hookName', () => {
addFilter( 'hookName', 'my_name/plugin/action', () => null );
testObject.hooks.addFilter( 'hookName', 'action', () => null );
expect( console.error ).toHaveBeenCalledTimes( 0 );

@@ -183,3 +172,3 @@ } );

test( 'Can add filters with periods in namespaces', () => {
addFilter( 'hook_name', 'my_name/plugin.name/ok.action', () => null );
testObject.hooks.addFilter( 'hook_name', 'ok.action', () => null );
expect( console.error ).toHaveBeenCalledTimes( 0 );

@@ -189,3 +178,3 @@ } );

test( 'Can add filters with periods in hookName', () => {
addFilter( 'hook.name', 'my_name/plugin/action', () => null );
testObject.hooks.addFilter( 'hook.name', 'action', () => null );
expect( console.error ).toHaveBeenCalledTimes( 0 );

@@ -195,5 +184,5 @@ } );

test( 'cannot add filters with invalid namespaces', () => {
addFilter( 'hook_name', '/invalid_name', () => null );
testObject.hooks.addFilter( 'hook_name', '/invalid_name', () => null );
expect( console.error ).toHaveBeenCalledWith(
'The namespace can only contain numbers, letters, dashes, periods and underscores, plus the forward slash dividing slug and description in the namespace.'
'The namespace can only contain numbers, letters, dashes, periods and underscores.'
);

@@ -203,3 +192,3 @@ } );

test( 'cannot add filters named with __ prefix', () => {
addFilter( '__test', 'my_name/my_plugin/my_callback', () => null );
testObject.hooks.addFilter( '__test', 'my_callback', () => null );
expect( console.error ).toHaveBeenCalledWith(

@@ -211,3 +200,3 @@ 'The hook name cannot begin with `__`.'

test( 'cannot add filters with non-function callbacks', () => {
addFilter( 'test', 'my_name/my_plugin/my_callback', '42' );
testObject.hooks.addFilter( 'test', 'my_callback', '42' );
expect( console.error ).toHaveBeenCalledWith(

@@ -219,3 +208,3 @@ 'The hook callback must be a function.'

test( 'cannot add filters with non-numeric priorities', () => {
addFilter( 'test', 'my_name/my_plugin/my_callback', () => null, '42' );
testObject.hooks.addFilter( 'test', 'my_callback', () => null, '42' );
expect( console.error ).toHaveBeenCalledWith(

@@ -227,3 +216,3 @@ 'If specified, the hook priority must be a number.'

test( 'cannot run filters with non-string names', () => {
expect( applyFilters( () => {}, 42 ) ).toBe( undefined );
expect( testObject.hooks.applyFilters( () => {}, 42 ) ).toBe( undefined );
expect( console.error ).toHaveBeenCalledWith(

@@ -235,3 +224,3 @@ 'The hook name must be a non-empty string.'

test( 'cannot run filters named with __ prefix', () => {
expect( applyFilters( '__test', 42 ) ).toBe( undefined );
expect( testObject.hooks.applyFilters( '__test', 42 ) ).toBe( undefined );
expect( console.error ).toHaveBeenCalledWith(

@@ -243,6 +232,6 @@ 'The hook name cannot begin with `__`.'

test( 'add 3 filters with different priorities and run them', () => {
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_a', filter_a );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_b', filter_b, 2 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_c', filter_c, 8 );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testbca' );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 8 );
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testbca' );
} );

@@ -261,32 +250,32 @@

addFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_3a', callbacks.fn_3a, 3 );
addFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_3b', callbacks.fn_3b, 3 );
addFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_3c', callbacks.fn_3c, 3 );
addFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_2a', callbacks.fn_2a, 2 );
addFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_2b', callbacks.fn_2b, 2 );
addFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_2c', callbacks.fn_2c, 2 );
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_3a', callbacks.fn_3a, 3 );
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_3b', callbacks.fn_3b, 3 );
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_3c', callbacks.fn_3c, 3 );
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_2a', callbacks.fn_2a, 2 );
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_2b', callbacks.fn_2b, 2 );
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_2c', callbacks.fn_2c, 2 );
expect( applyFilters( 'test_order', [] ) ).toEqual(
expect( testObject.hooks.applyFilters( 'test_order', [] ) ).toEqual(
[ '2a', '2b', '2c', '3a', '3b', '3c' ]
);
removeFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_2b', callbacks.fn_2b );
removeFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_3a', callbacks.fn_3a );
testObject.hooks.removeFilter( 'test_order', 'my_callback_fn_2b', callbacks.fn_2b );
testObject.hooks.removeFilter( 'test_order', 'my_callback_fn_3a', callbacks.fn_3a );
expect( applyFilters( 'test_order', [] ) ).toEqual(
expect( testObject.hooks.applyFilters( 'test_order', [] ) ).toEqual(
[ '2a', '2c', '3b', '3c' ]
);
addFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_4a', callbacks.fn_4a, 4 );
addFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_4b', callbacks.fn_4b, 4 );
addFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_1a', callbacks.fn_1a, 1 );
addFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_4c', callbacks.fn_4c, 4 );
addFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_1b', callbacks.fn_1b, 1 );
addFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_3d', callbacks.fn_3d, 3 );
addFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_4d', callbacks.fn_4d, 4 );
addFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_1c', callbacks.fn_1c, 1 );
addFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_2d', callbacks.fn_2d, 2 );
addFilter( 'test_order', 'my_name/my_plugin/my_callback_fn_1d', callbacks.fn_1d, 1 );
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_4a', callbacks.fn_4a, 4 );
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_4b', callbacks.fn_4b, 4 );
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_1a', callbacks.fn_1a, 1 );
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_4c', callbacks.fn_4c, 4 );
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_1b', callbacks.fn_1b, 1 );
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_3d', callbacks.fn_3d, 3 );
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_4d', callbacks.fn_4d, 4 );
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_1c', callbacks.fn_1c, 1 );
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_2d', callbacks.fn_2d, 2 );
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_1d', callbacks.fn_1d, 1 );
expect( applyFilters( 'test_order', [] ) ).toEqual( [
expect( testObject.hooks.applyFilters( 'test_order', [] ) ).toEqual( [
// all except 2b and 3a, which we removed earlier

@@ -301,5 +290,5 @@ '1a', '1b', '1c', '1d',

test( 'add and remove an action', () => {
addAction( 'test.action', 'my_name/my_plugin/my_callback', action_a );
expect( removeAllActions( 'test.action' ) ).toEqual( 1 );
expect( doAction( 'test.action' ) ).toBe( undefined );
testObject.hooks.addAction( 'test.action', 'my_callback', action_a );
expect( testObject.hooks.removeAllActions( 'test.action' ) ).toEqual( 1 );
expect( testObject.hooks.doAction( 'test.action' ) ).toBe( undefined );
expect( window.actionValue ).toBe( '' );

@@ -309,4 +298,4 @@ } );

test( 'add an action and run it', () => {
addAction( 'test.action', 'my_name/my_plugin/my_callback', action_a );
doAction( 'test.action' );
testObject.hooks.addAction( 'test.action', 'my_callback', action_a );
testObject.hooks.doAction( 'test.action' );
expect( window.actionValue ).toBe( 'a' );

@@ -316,5 +305,5 @@ } );

test( 'add 2 actions in a row and then run them', () => {
addAction( 'test.action', 'my_name/my_plugin/my_callback', action_a );
addAction( 'test.action', 'my_name/my_plugin/my_callback', action_b );
doAction( 'test.action' );
testObject.hooks.addAction( 'test.action', 'my_callback', action_a );
testObject.hooks.addAction( 'test.action', 'my_callback', action_b );
testObject.hooks.doAction( 'test.action' );
expect( window.actionValue ).toBe( 'ab' );

@@ -324,6 +313,6 @@ } );

test( 'add 3 actions with different priorities and run them', () => {
addAction( 'test.action', 'my_name/my_plugin/my_callback', action_a );
addAction( 'test.action', 'my_name/my_plugin/my_callback', action_b, 2 );
addAction( 'test.action', 'my_name/my_plugin/my_callback', action_c, 8 );
doAction( 'test.action' );
testObject.hooks.addAction( 'test.action', 'my_callback', action_a );
testObject.hooks.addAction( 'test.action', 'my_callback', action_b, 2 );
testObject.hooks.addAction( 'test.action', 'my_callback', action_c, 8 );
testObject.hooks.doAction( 'test.action' );
expect( window.actionValue ).toBe( 'bca' );

@@ -336,7 +325,7 @@ } );

addAction( 'test.action', 'my_name/my_plugin/my_callback', ( a, b ) => {
testObject.hooks.addAction( 'test.action', 'my_callback', ( a, b ) => {
expect( a ).toBe( arg1 );
expect( b ).toBe( arg2 );
} );
doAction( 'test.action', arg1, arg2 );
testObject.hooks.doAction( 'test.action', arg1, arg2 );
} );

@@ -351,41 +340,41 @@

addAction( 'test.action', 'my_name/my_plugin/my_callback', func );
doAction( 'test.action' );
doAction( 'test.action' );
testObject.hooks.addAction( 'test.action', 'my_callback', func );
testObject.hooks.doAction( 'test.action' );
testObject.hooks.doAction( 'test.action' );
} );
test( 'add a filter before the one currently executing', () => {
addFilter( 'test.filter', 'my_name/my_plugin/my_callback', val => {
addFilter( 'test.filter', 'my_name/my_plugin/my_callback', val => val + 'a', 1 );
testObject.hooks.addFilter( 'test.filter', 'my_callback', val => {
testObject.hooks.addFilter( 'test.filter', 'my_callback', val => val + 'a', 1 );
return val + 'b';
}, 2 );
expect( applyFilters( 'test.filter', 'test_' ) ).toEqual( 'test_b' );
expect( testObject.hooks.applyFilters( 'test.filter', 'test_' ) ).toEqual( 'test_b' );
} );
test( 'add a filter after the one currently executing', () => {
addFilter( 'test.filter', 'my_name/my_plugin/my_callback', val => {
addFilter( 'test.filter', 'my_name/my_plugin/my_callback', val => val + 'b', 2 );
testObject.hooks.addFilter( 'test.filter', 'my_callback', val => {
testObject.hooks.addFilter( 'test.filter', 'my_callback', val => val + 'b', 2 );
return val + 'a';
}, 1 );
expect( applyFilters( 'test.filter', 'test_' ) ).toEqual( 'test_ab' );
expect( testObject.hooks.applyFilters( 'test.filter', 'test_' ) ).toEqual( 'test_ab' );
} );
test( 'add a filter immediately after the one currently executing', () => {
addFilter( 'test.filter', 'my_name/my_plugin/my_callback', val => {
addFilter( 'test.filter', 'my_name/my_plugin/my_callback', val => val + 'b', 1 );
testObject.hooks.addFilter( 'test.filter', 'my_callback', val => {
testObject.hooks.addFilter( 'test.filter', 'my_callback', val => val + 'b', 1 );
return val + 'a';
}, 1 );
expect( applyFilters( 'test.filter', 'test_' ) ).toEqual( 'test_ab' );
expect( testObject.hooks.applyFilters( 'test.filter', 'test_' ) ).toEqual( 'test_ab' );
} );
test( 'remove specific action callback', () => {
addAction( 'test.action', 'my_name/my_plugin/my_callback_action_a', action_a );
addAction( 'test.action', 'my_name/my_plugin/my_callback_action_b', action_b, 2 );
addAction( 'test.action', 'my_name/my_plugin/my_callback_action_c', action_c, 8 );
testObject.hooks.addAction( 'test.action', 'my_callback_action_a', action_a );
testObject.hooks.addAction( 'test.action', 'my_callback_action_b', action_b, 2 );
testObject.hooks.addAction( 'test.action', 'my_callback_action_c', action_c, 8 );
expect( removeAction( 'test.action', 'my_name/my_plugin/my_callback_action_b' ) ).toEqual( 1 );
doAction( 'test.action' );
expect( testObject.hooks.removeAction( 'test.action', 'my_callback_action_b' ) ).toEqual( 1 );
testObject.hooks.doAction( 'test.action' );
expect( window.actionValue ).toBe( 'ca' );

@@ -395,8 +384,8 @@ } );

test( 'remove all action callbacks', () => {
addAction( 'test.action', 'my_name/my_plugin/my_callback_action_a', action_a );
addAction( 'test.action', 'my_name/my_plugin/my_callback_action_b', action_b, 2 );
addAction( 'test.action', 'my_name/my_plugin/my_callback_action_c', action_c, 8 );
testObject.hooks.addAction( 'test.action', 'my_callback_action_a', action_a );
testObject.hooks.addAction( 'test.action', 'my_callback_action_b', action_b, 2 );
testObject.hooks.addAction( 'test.action', 'my_callback_action_c', action_c, 8 );
expect( removeAllActions( 'test.action' ) ).toEqual( 3 );
doAction( 'test.action' );
expect( testObject.hooks.removeAllActions( 'test.action' ) ).toEqual( 3 );
testObject.hooks.doAction( 'test.action' );
expect( window.actionValue ).toBe( '' );

@@ -406,135 +395,135 @@ } );

test( 'remove specific filter callback', () => {
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_a', filter_a );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_b', filter_b, 2 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_c', filter_c, 8 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 8 );
expect( removeFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_b' ) ).toEqual( 1 );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testca' );
expect( testObject.hooks.removeFilter( 'test.filter', 'my_callback_filter_b' ) ).toEqual( 1 );
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testca' );
} );
test( 'filter removes a callback that has already executed', () => {
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_a', filter_a, 1 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_b', filter_b, 3 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_c', filter_c, 5 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_removes_b', filter_removes_b, 4 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 3 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 5 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_removes_b', filter_removes_b, 4 );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' );
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' );
} );
test( 'filter removes a callback that has already executed (same priority)', () => {
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_a', filter_a, 1 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_b', filter_b, 2 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_removes_b', filter_removes_b, 2 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_c', filter_c, 4 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_removes_b', filter_removes_b, 2 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 4 );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' );
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' );
} );
test( 'filter removes the current callback', () => {
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_a', filter_a, 1 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_b_removes_self', filter_b_removes_self, 3 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_c', filter_c, 5 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b_removes_self', filter_b_removes_self, 3 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 5 );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' );
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' );
} );
test( 'filter removes a callback that has not yet executed (last)', () => {
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_a', filter_a, 1 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_b', filter_b, 3 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_c', filter_c, 5 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_removes_c', filter_removes_c, 4 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 3 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 5 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_removes_c', filter_removes_c, 4 );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testab' );
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testab' );
} );
test( 'filter removes a callback that has not yet executed (middle)', () => {
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_a', filter_a, 1 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_b', filter_b, 3 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_c', filter_c, 4 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_removes_b', filter_removes_b, 2 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 3 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 4 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_removes_b', filter_removes_b, 2 );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testac' );
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testac' );
} );
test( 'filter removes a callback that has not yet executed (same priority)', () => {
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_a', filter_a, 1 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_removes_b', filter_removes_b, 2 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_b', filter_b, 2 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_c', filter_c, 4 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_removes_b', filter_removes_b, 2 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 4 );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testac' );
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testac' );
} );
test( 'remove all filter callbacks', () => {
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_a', filter_a );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_b', filter_b, 2 );
addFilter( 'test.filter', 'me_name/my_plugin/my_callback_filter_c', filter_c, 8 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 );
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 8 );
expect( removeAllFilters( 'test.filter' ) ).toEqual( 3 );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'test' );
expect( testObject.hooks.removeAllFilters( 'test.filter' ) ).toEqual( 3 );
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'test' );
} );
// Test doingAction, didAction, hasAction.
test( 'Test doingAction, didAction and hasAction.', () => {
// Test testObject.hooks.doingAction, testObject.hooks.didAction, testObject.hooks.hasAction.
test( 'Test testObject.hooks.doingAction, testObject.hooks.didAction and testObject.hooks.hasAction.', () => {
let actionCalls = 0;
addAction( 'another.action', 'my_name/my_plugin/my_callback', () => {} );
doAction( 'another.action' );
testObject.hooks.addAction( 'another.action', 'my_callback', () => {} );
testObject.hooks.doAction( 'another.action' );
// Verify no action is running yet.
expect( doingAction( 'test.action' ) ).toBe( false );
expect( testObject.hooks.doingAction( 'test.action' ) ).toBe( false );
expect( didAction( 'test.action' ) ).toBe( 0 );
expect( hasAction( 'test.action' ) ).toBe( 0 );
expect( testObject.hooks.didAction( 'test.action' ) ).toBe( 0 );
expect( testObject.hooks.hasAction( 'test.action' ) ).toBe( 0 );
addAction( 'test.action', 'my_name/my_plugin/my_callback', () => {
testObject.hooks.addAction( 'test.action', 'my_callback', () => {
actionCalls++;
expect( currentAction() ).toBe( 'test.action' );
expect( doingAction() ).toBe( true );
expect( doingAction( 'test.action' ) ).toBe( true );
expect( testObject.hooks.currentAction() ).toBe( 'test.action' );
expect( testObject.hooks.doingAction() ).toBe( true );
expect( testObject.hooks.doingAction( 'test.action' ) ).toBe( true );
} );
// Verify action added, not running yet.
expect( doingAction( 'test.action' ) ).toBe( false );
expect( didAction( 'test.action' ) ).toBe( 0 );
expect( hasAction( 'test.action' ) ).toBe( 1 );
expect( testObject.hooks.doingAction( 'test.action' ) ).toBe( false );
expect( testObject.hooks.didAction( 'test.action' ) ).toBe( 0 );
expect( testObject.hooks.hasAction( 'test.action' ) ).toBe( 1 );
doAction( 'test.action' );
testObject.hooks.doAction( 'test.action' );
// Verify action added and running.
expect( actionCalls ).toBe( 1 );
expect( doingAction( 'test.action' ) ).toBe( false );
expect( didAction( 'test.action' ) ).toBe( 1 );
expect( hasAction( 'test.action' ) ).toBe( 1 );
expect( doingAction() ).toBe( false );
expect( doingAction( 'test.action' ) ).toBe( false );
expect( doingAction( 'notatest.action' ) ).toBe( false );
expect( currentAction() ).toBe( null );
expect( testObject.hooks.doingAction( 'test.action' ) ).toBe( false );
expect( testObject.hooks.didAction( 'test.action' ) ).toBe( 1 );
expect( testObject.hooks.hasAction( 'test.action' ) ).toBe( 1 );
expect( testObject.hooks.doingAction() ).toBe( false );
expect( testObject.hooks.doingAction( 'test.action' ) ).toBe( false );
expect( testObject.hooks.doingAction( 'notatest.action' ) ).toBe( false );
expect( testObject.hooks.currentAction() ).toBe( null );
doAction( 'test.action' );
testObject.hooks.doAction( 'test.action' );
expect( actionCalls ).toBe( 2 );
expect( didAction( 'test.action' ) ).toBe( 2 );
expect( testObject.hooks.didAction( 'test.action' ) ).toBe( 2 );
expect( removeAllActions( 'test.action' ) ).toEqual( 1 );
expect( testObject.hooks.removeAllActions( 'test.action' ) ).toEqual( 1 );
// Verify state is reset appropriately.
expect( doingAction( 'test.action' ) ).toBe( false );
expect( didAction( 'test.action' ) ).toBe( 2 );
expect( hasAction( 'test.action' ) ).toBe( 0 );
expect( testObject.hooks.doingAction( 'test.action' ) ).toBe( false );
expect( testObject.hooks.didAction( 'test.action' ) ).toBe( 2 );
expect( testObject.hooks.hasAction( 'test.action' ) ).toBe( 0 );
doAction( 'another.action' );
expect( doingAction( 'test.action' ) ).toBe( false );
testObject.hooks.doAction( 'another.action' );
expect( testObject.hooks.doingAction( 'test.action' ) ).toBe( false );
// Verify hasAction returns 0 when no matching action.
expect( hasAction( 'notatest.action' ) ).toBe( 0 );
// Verify testObject.hooks.hasAction returns 0 when no matching action.
expect( testObject.hooks.hasAction( 'notatest.action' ) ).toBe( 0 );
} );
test( 'Verify doingFilter, didFilter and hasFilter.', () => {
test( 'Verify testObject.hooks.doingFilter, testObject.hooks.didFilter and testObject.hooks.hasFilter.', () => {
let filterCalls = 0;
addFilter( 'runtest.filter', 'my_name/my_plugin/my_callback', arg => {
testObject.hooks.addFilter( 'runtest.filter', 'my_callback', arg => {
filterCalls++;
expect( currentFilter() ).toBe( 'runtest.filter' );
expect( doingFilter() ).toBe( true );
expect( doingFilter( 'runtest.filter' ) ).toBe( true );
expect( testObject.hooks.currentFilter() ).toBe( 'runtest.filter' );
expect( testObject.hooks.doingFilter() ).toBe( true );
expect( testObject.hooks.doingFilter( 'runtest.filter' ) ).toBe( true );
return arg;

@@ -544,46 +533,46 @@ } );

// Verify filter added and running.
const test = applyFilters( 'runtest.filter', 'someValue' );
const test = testObject.hooks.applyFilters( 'runtest.filter', 'someValue' );
expect( test ).toBe( 'someValue' );
expect( filterCalls ).toBe( 1 );
expect( didFilter( 'runtest.filter' ) ).toBe( 1 );
expect( hasFilter( 'runtest.filter' ) ).toBe( 1 );
expect( hasFilter( 'notatest.filter' ) ).toBe( 0 );
expect( doingFilter() ).toBe( false );
expect( doingFilter( 'runtest.filter' ) ).toBe( false );
expect( doingFilter( 'notatest.filter' ) ).toBe( false );
expect( currentFilter() ).toBe( null );
expect( testObject.hooks.didFilter( 'runtest.filter' ) ).toBe( 1 );
expect( testObject.hooks.hasFilter( 'runtest.filter' ) ).toBe( 1 );
expect( testObject.hooks.hasFilter( 'notatest.filter' ) ).toBe( 0 );
expect( testObject.hooks.doingFilter() ).toBe( false );
expect( testObject.hooks.doingFilter( 'runtest.filter' ) ).toBe( false );
expect( testObject.hooks.doingFilter( 'notatest.filter' ) ).toBe( false );
expect( testObject.hooks.currentFilter() ).toBe( null );
expect( removeAllFilters( 'runtest.filter' ) ).toEqual( 1 );
expect( testObject.hooks.removeAllFilters( 'runtest.filter' ) ).toEqual( 1 );
expect( hasFilter( 'runtest.filter' ) ).toBe( 0 );
expect( didFilter( 'runtest.filter' ) ).toBe( 1 );
expect( testObject.hooks.hasFilter( 'runtest.filter' ) ).toBe( 0 );
expect( testObject.hooks.didFilter( 'runtest.filter' ) ).toBe( 1 );
} );
test( 'recursively calling a filter', () => {
addFilter( 'test.filter', 'my_name/my_plugin/my_callback', value => {
testObject.hooks.addFilter( 'test.filter', 'my_callback', value => {
if ( value.length === 7 ) {
return value;
}
return applyFilters( 'test.filter', value + 'X' );
return testObject.hooks.applyFilters( 'test.filter', value + 'X' );
} );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testXXX' );
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testXXX' );
} );
test( 'current filter when multiple filters are running', () => {
addFilter( 'test.filter1', 'my_name/my_plugin/my_callback', value => {
return applyFilters( 'test.filter2', value.concat( currentFilter() ) );
testObject.hooks.addFilter( 'test.filter1', 'my_callback', value => {
return testObject.hooks.applyFilters( 'test.filter2', value.concat( testObject.hooks.currentFilter() ) );
} );
addFilter( 'test.filter2', 'my_name/my_plugin/my_callback', value => {
return value.concat( currentFilter() );
testObject.hooks.addFilter( 'test.filter2', 'my_callback', value => {
return value.concat( testObject.hooks.currentFilter() );
} );
expect( currentFilter() ).toBe( null );
expect( testObject.hooks.currentFilter() ).toBe( null );
expect( applyFilters( 'test.filter1', [ 'test' ] ) ).toEqual(
expect( testObject.hooks.applyFilters( 'test.filter1', [ 'test' ] ) ).toEqual(
[ 'test', 'test.filter1', 'test.filter2' ]
);
expect( currentFilter() ).toBe( null );
expect( testObject.hooks.currentFilter() ).toBe( null );
} );

@@ -593,14 +582,53 @@

function removeRecurseAndAdd2( val ) {
expect( removeFilter( 'remove_and_add', 'my_name/my_plugin/my_callback_recurse' ) ).toEqual( 1 );
val += '-' + applyFilters( 'remove_and_add', '' ) + '-';
addFilter( 'remove_and_add', 'my_name/my_plugin/my_callback_recurse', 10 );
expect( testObject.hooks.removeFilter( 'remove_and_add', 'my_callback_recurse' ) ).toEqual( 1 );
val += '-' + testObject.hooks.applyFilters( 'remove_and_add', '' ) + '-';
testObject.hooks.addFilter( 'remove_and_add', 'my_callback_recurse', 10 );
return val + '2';
}
addFilter( 'remove_and_add', 'my_name/my_plugin/my_callback', val => val + '1', 11 );
addFilter( 'remove_and_add', 'my_name/my_plugin/my_callback_recurse', removeRecurseAndAdd2, 12 );
addFilter( 'remove_and_add', 'my_name/my_plugin/my_callback', val => val + '3', 13 );
addFilter( 'remove_and_add', 'my_name/my_plugin/my_callback', val => val + '4', 14 );
testObject.hooks.addFilter( 'remove_and_add', 'my_callback', val => val + '1', 11 );
testObject.hooks.addFilter( 'remove_and_add', 'my_callback_recurse', removeRecurseAndAdd2, 12 );
testObject.hooks.addFilter( 'remove_and_add', 'my_callback', val => val + '3', 13 );
testObject.hooks.addFilter( 'remove_and_add', 'my_callback', val => val + '4', 14 );
expect( applyFilters( 'remove_and_add', '' ) ).toEqual( '1-134-234' );
expect( testObject.hooks.applyFilters( 'remove_and_add', '' ) ).toEqual( '1-134-234' );
} );
// Test adding via composition.
test( 'adding hooks via composition', () => {
var testObject2 = {};
testObject2.hooks = createHooks();
expect( typeof testObject2.hooks.applyFilters ).toEqual( 'function' );
} );
// Test adding as a mixin.
test( 'adding hooks as a mixin', () => {
var testObject3 = {};
Object.assign( testObject3, createHooks() );
expect( typeof testObject3.applyFilters ).toEqual( 'function' );
} );
// Test context.
test( 'Test `this` context via composition', () => {
var testObject2 = { test: 'test this' };
testObject2.hooks = createHooks();
var theCallback = function() {
expect( this.test ).toEqual( 'test this' );
};
testObject.hooks.addAction( 'test.action', 'my_callback', theCallback.apply( testObject2 ) );
testObject.hooks.doAction( 'test.action' );
var testObject3 = {};
Object.assign( testObject3, createHooks() );
} );

@@ -16,12 +16,7 @@ /**

if ( ! /^[a-zA-Z][a-zA-Z0-9_.\-/]*$/.test( namespace ) ) {
console.error( 'The namespace can only contain numbers, letters, dashes, periods and underscores, plus the forward slash dividing slug and description in the namespace.' );
if ( ! /^[a-zA-Z][a-zA-Z0-9_.\-]*$/.test( namespace ) ) {
console.error( 'The namespace can only contain numbers, letters, dashes, periods and underscores.' );
return false;
}
if ( ! /^[a-zA-Z][a-zA-Z0-9_.\-]*\/[a-zA-Z][a-zA-Z0-9_.\-]*\/[a-zA-Z][a-zA-Z0-9_.\-]*$/.test( namespace ) ) {
console.error( 'The namespace must take the form `vendor/plugin/function`.' );
return false;
}
return true;

@@ -28,0 +23,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc