From 1b7475645ab9e3bf426ae90f80d29678c3b324d9 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Tue, 12 Sep 2017 12:53:21 +0000 Subject: [PATCH] Add `wp.hooks` - JavaScript actions and filters. Add a JavaScript hooks library with an API that mirrors the WordPress Plugin API; provides similar functionality and API to PHP hooks. Called via the global `wp.hooks`, eg: `wp.hooks.addAction()`, etc. Adds: * `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' )` Props adamsilverstein, jnylen0, aduth, kadamwhite, youknowriad, schlessera, mikeschinkel, azaozz, vhauri, CaptainN, scribu, carldanley, chetanchauhan, mgibbs189, stephenharris, justnorris, koopersmith, gcorne, TV productions, atimmer. Fixes #21170. git-svn-id: https://develop.svn.wordpress.org/trunk@41375 602fd350-edb4-49c9-b593-d223f7449a82 --- Gruntfile.js | 3 +- src/wp-includes/js/wp-hooks.js | 698 +++++++++++++++++++++++++ src/wp-includes/plugin.php | 2 +- src/wp-includes/script-loader.php | 2 + tests/qunit/index.html | 2 + tests/qunit/wp-includes/js/wp-hooks.js | 235 +++++++++ 6 files changed, 940 insertions(+), 2 deletions(-) create mode 100644 src/wp-includes/js/wp-hooks.js create mode 100644 tests/qunit/wp-includes/js/wp-hooks.js diff --git a/Gruntfile.js b/Gruntfile.js index 5a73e695d5..a8ad7d00c4 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -369,7 +369,8 @@ module.exports = function(grunt) { '!wp-includes/js/json2.js', '!wp-includes/js/tw-sack.js', '!wp-includes/js/twemoji.js', - '!**/*.min.js' + '!**/*.min.js', + '!wp-includes/js/wp-hooks.js' ], // Remove once other JSHint errors are resolved options: { diff --git a/src/wp-includes/js/wp-hooks.js b/src/wp-includes/js/wp-hooks.js new file mode 100644 index 0000000000..3018618c6c --- /dev/null +++ b/src/wp-includes/js/wp-hooks.js @@ -0,0 +1,698 @@ +this["wp"] = this["wp"] || {}; this["wp"]["hooks"] = +/******/ (function(modules) { // webpackBootstrap +/******/ // The module cache +/******/ var installedModules = {}; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ +/******/ // Check if module is in cache +/******/ if(installedModules[moduleId]) { +/******/ return installedModules[moduleId].exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = installedModules[moduleId] = { +/******/ i: moduleId, +/******/ l: false, +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); +/******/ +/******/ // Flag the module as loaded +/******/ module.l = true; +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/******/ +/******/ // expose the modules object (__webpack_modules__) +/******/ __webpack_require__.m = modules; +/******/ +/******/ // expose the module cache +/******/ __webpack_require__.c = installedModules; +/******/ +/******/ // define getter function for harmony exports +/******/ __webpack_require__.d = function(exports, name, getter) { +/******/ if(!__webpack_require__.o(exports, name)) { +/******/ Object.defineProperty(exports, name, { +/******/ configurable: false, +/******/ enumerable: true, +/******/ get: getter +/******/ }); +/******/ } +/******/ }; +/******/ +/******/ // getDefaultExport function for compatibility with non-harmony modules +/******/ __webpack_require__.n = function(module) { +/******/ var getter = module && module.__esModule ? +/******/ function getDefault() { return module['default']; } : +/******/ function getModuleExports() { return module; }; +/******/ __webpack_require__.d(getter, 'a', getter); +/******/ return getter; +/******/ }; +/******/ +/******/ // Object.prototype.hasOwnProperty.call +/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; +/******/ +/******/ // __webpack_public_path__ +/******/ __webpack_require__.p = ""; +/******/ +/******/ // Load entry module and return exports +/******/ return __webpack_require__(__webpack_require__.s = 13); +/******/ }) +/************************************************************************/ +/******/ ([ +/* 0 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); +/** + * Validate a hookName string. + * + * @param {string} hookName The hook name to validate. Should be a non empty string containing + * only numbers, letters, dashes, periods and underscores. Also, + * the hook name cannot begin with `__`. + * + * @return {bool} Whether the hook name is valid. + */ +function validateHookName(hookName) { + + if ('string' !== typeof hookName || '' === hookName) { + console.error('The hook name must be a non-empty string.'); + return false; + } + + if (/^__/.test(hookName)) { + console.error('The hook name cannot begin with `__`.'); + return false; + } + + if (!/^[a-zA-Z][a-zA-Z0-9_.-]*$/.test(hookName)) { + console.error('The hook name can only contain numbers, letters, dashes, periods and underscores.'); + return false; + } + + return true; +} + +exports.default = validateHookName; + +/***/ }), +/* 1 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); +/** + * Validate a namespace string. + * + * @param {string} namespace The namespace to validate - should take the form + * `vendorName/pluginName/functionName`. + * + * @return {bool} Whether the namespace is valid. + */ +function validateNamespace(namespace) { + + if ('string' !== typeof namespace || '' === namespace) { + console.error('The namespace must be a non-empty string.'); + return false; + } + + 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.'); + 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; +} + +exports.default = validateNamespace; + +/***/ }), +/* 2 */, +/* 3 */, +/* 4 */, +/* 5 */, +/* 6 */, +/* 7 */, +/* 8 */, +/* 9 */, +/* 10 */, +/* 11 */, +/* 12 */, +/* 13 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + 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 = __webpack_require__(14); + +var _hooks2 = _interopRequireDefault(_hooks); + +var _createAddHook = __webpack_require__(15); + +var _createAddHook2 = _interopRequireDefault(_createAddHook); + +var _createRemoveHook = __webpack_require__(16); + +var _createRemoveHook2 = _interopRequireDefault(_createRemoveHook); + +var _createHasHook = __webpack_require__(17); + +var _createHasHook2 = _interopRequireDefault(_createHasHook); + +var _createRunHook = __webpack_require__(18); + +var _createRunHook2 = _interopRequireDefault(_createRunHook); + +var _createCurrentHook = __webpack_require__(19); + +var _createCurrentHook2 = _interopRequireDefault(_createCurrentHook); + +var _createDoingHook = __webpack_require__(20); + +var _createDoingHook2 = _interopRequireDefault(_createDoingHook); + +var _createDidHook = __webpack_require__(21); + +var _createDidHook2 = _interopRequireDefault(_createDidHook); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +// Add action/filter functions. +var addAction = exports.addAction = (0, _createAddHook2.default)(_hooks2.default.actions); +var addFilter = exports.addFilter = (0, _createAddHook2.default)(_hooks2.default.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); + +/***/ }), +/* 14 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); +/** + * Contains the registered hooks, keyed by hook type. Each hook type is an + * array of objects with priority and callback of each registered hook. + */ +var HOOKS = { + actions: {}, + filters: {} +}; + +exports.default = HOOKS; + +/***/ }), +/* 15 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _validateNamespace = __webpack_require__(1); + +var _validateNamespace2 = _interopRequireDefault(_validateNamespace); + +var _validateHookName = __webpack_require__(0); + +var _validateHookName2 = _interopRequireDefault(_validateHookName); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * Returns a function which, when invoked, will add a hook. + * + * @param {Object} hooks Stored hooks, keyed by hook name. + * + * @return {Function} Function that adds a new hook. + */ +function createAddHook(hooks) { + /** + * Adds the hook to the appropriate hooks container. + * + * @param {string} hookName Name of hook to add + * @param {string} namespace The unique namespace identifying the callback in the form `vendorName/pluginName/functionName`. + * @param {Function} callback Function to call when the hook is run + * @param {?number} priority Priority of this hook (default=10) + */ + return function addHook(hookName, namespace, callback) { + var priority = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 10; + + + if (!(0, _validateHookName2.default)(hookName)) { + return; + } + + if (!(0, _validateNamespace2.default)(namespace)) { + return; + } + + if ('function' !== typeof callback) { + console.error('The hook callback must be a function.'); + return; + } + + // Validate numeric priority + if ('number' !== typeof priority) { + console.error('If specified, the hook priority must be a number.'); + return; + } + + var handler = { callback: callback, priority: priority, namespace: namespace }; + + if (hooks.hasOwnProperty(hookName)) { + // Find the correct insert index of the new hook. + var handlers = hooks[hookName].handlers; + var i = 0; + while (i < handlers.length) { + if (handlers[i].priority > priority) { + break; + } + i++; + } + // Insert (or append) the new hook. + handlers.splice(i, 0, handler); + // We may also be currently executing this hook. If the callback + // we're adding would come after the current callback, there's no + // problem; otherwise we need to increase the execution index of + // any other runs by 1 to account for the added element. + (hooks.__current || []).forEach(function (hookInfo) { + if (hookInfo.name === hookName && hookInfo.currentIndex >= i) { + hookInfo.currentIndex++; + } + }); + } else { + // This is the first hook of its type. + hooks[hookName] = { + handlers: [handler], + runs: 0 + }; + } + }; +} + +exports.default = createAddHook; + +/***/ }), +/* 16 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _validateNamespace = __webpack_require__(1); + +var _validateNamespace2 = _interopRequireDefault(_validateNamespace); + +var _validateHookName = __webpack_require__(0); + +var _validateHookName2 = _interopRequireDefault(_validateHookName); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * Returns a function which, when invoked, will remove a specified hook or all + * hooks by the given name. + * + * @param {Object} hooks Stored hooks, keyed by hook name. + * @param {bool} removeAll Whether to remove all callbacks for a hookName, without regard to namespace. Used to create `removeAll*` functions. + * + * @return {Function} Function that removes hooks. + */ +function createRemoveHook(hooks, removeAll) { + /** + * Removes the specified callback (or all callbacks) from the hook with a + * given hookName and namespace. + * + * @param {string} hookName The name of the hook to modify. + * @param {string} namespace The unique namespace identifying the callback in the form `vendorName/pluginName/functionName`. + * + * @return {number} The number of callbacks removed. + */ + return function removeHook(hookName, namespace) { + + if (!(0, _validateHookName2.default)(hookName)) { + return; + } + + if (!removeAll && !(0, _validateNamespace2.default)(namespace)) { + return; + } + + // Bail if no hooks exist by this name + if (!hooks.hasOwnProperty(hookName)) { + return 0; + } + + var handlersRemoved = 0; + + if (removeAll) { + handlersRemoved = hooks[hookName].handlers.length; + hooks[hookName] = { + runs: hooks[hookName].runs, + handlers: [] + }; + } else { + // Try to find the specified callback to remove. + var handlers = hooks[hookName].handlers; + + var _loop = function _loop(i) { + if (handlers[i].namespace === namespace) { + handlers.splice(i, 1); + handlersRemoved++; + // This callback may also be part of a hook that is + // currently executing. If the callback we're removing + // comes after the current callback, there's no problem; + // otherwise we need to decrease the execution index of any + // other runs by 1 to account for the removed element. + (hooks.__current || []).forEach(function (hookInfo) { + if (hookInfo.name === hookName && hookInfo.currentIndex >= i) { + hookInfo.currentIndex--; + } + }); + } + }; + + for (var i = handlers.length - 1; i >= 0; i--) { + _loop(i); + } + } + + return handlersRemoved; + }; +} + +exports.default = createRemoveHook; + +/***/ }), +/* 17 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); +/** + * Returns a function which, when invoked, will return whether any handlers are + * attached to a particular hook. + * + * @param {Object} hooks Stored hooks, keyed by hook name. + * + * @return {Function} Function that returns whether any handlers are + * attached to a particular hook. + */ +function createHasHook(hooks) { + /** + * Returns how many handlers are attached for the given hook. + * + * @param {string} hookName The name of the hook to check for. + * + * @return {number} The number of handlers that are attached to + * the given hook. + */ + return function hasHook(hookName) { + return hooks.hasOwnProperty(hookName) ? hooks[hookName].handlers.length : 0; + }; +} + +exports.default = createHasHook; + +/***/ }), +/* 18 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _validateHookName = __webpack_require__(0); + +var _validateHookName2 = _interopRequireDefault(_validateHookName); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * Returns a function which, when invoked, will execute all callbacks + * registered to a hook of the specified type, optionally returning the final + * value of the call chain. + * + * @param {Object} hooks Stored hooks, keyed by hook name. + * @param {?bool} returnFirstArg Whether each hook callback is expected to + * return its first argument. + * + * @return {Function} Function that runs hook callbacks. + */ +function createRunHook(hooks, returnFirstArg) { + /** + * Runs all callbacks for the specified hook. + * + * @param {string} hookName The name of the hook to run. + * @param {...*} args Arguments to pass to the hook callbacks. + * + * @return {*} Return value of runner, if applicable. + */ + return function runHooks(hookName) { + + if (!(0, _validateHookName2.default)(hookName)) { + return; + } + + if (!hooks.hasOwnProperty(hookName)) { + hooks[hookName] = { + runs: 0, + handlers: [] + }; + } + + var handlers = hooks[hookName].handlers; + + for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { + args[_key - 1] = arguments[_key]; + } + + if (!handlers.length) { + return returnFirstArg ? args[0] : undefined; + } + + var hookInfo = { + name: hookName, + currentIndex: 0 + }; + + hooks.__current = hooks.__current || []; + hooks.__current.push(hookInfo); + hooks[hookName].runs++; + + var maybeReturnValue = args[0]; + + while (hookInfo.currentIndex < handlers.length) { + var handler = handlers[hookInfo.currentIndex]; + maybeReturnValue = handler.callback.apply(null, args); + if (returnFirstArg) { + args[0] = maybeReturnValue; + } + hookInfo.currentIndex++; + } + + hooks.__current.pop(); + + if (returnFirstArg) { + return maybeReturnValue; + } + }; +} + +exports.default = createRunHook; + +/***/ }), +/* 19 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); +/** + * Returns a function which, when invoked, will return the name of the + * currently running hook, or `null` if no hook of the given type is currently + * running. + * + * @param {Object} hooks Stored hooks, keyed by hook name. + * + * @return {Function} Function that returns the current hook. + */ +function createCurrentHook(hooks, returnFirstArg) { + /** + * Returns the name of the currently running hook, or `null` if no hook of + * the given type is currently running. + * + * @return {?string} The name of the currently running hook, or + * `null` if no hook is currently running. + */ + return function currentHook() { + if (!hooks.__current || !hooks.__current.length) { + return null; + } + + return hooks.__current[hooks.__current.length - 1].name; + }; +} + +exports.default = createCurrentHook; + +/***/ }), +/* 20 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); +/** + * Returns a function which, when invoked, will return whether a hook is + * currently being executed. + * + * @param {Object} hooks Stored hooks, keyed by hook name. + * + * @return {Function} Function that returns whether a hook is currently + * being executed. + */ +function createDoingHook(hooks) { + /** + * Returns whether a hook is currently being executed. + * + * @param {?string} hookName The name of the hook to check for. If + * omitted, will check for any hook being executed. + * + * @return {bool} Whether the hook is being executed. + */ + return function doingHook(hookName) { + // If the hookName was not passed, check for any current hook. + if ('undefined' === typeof hookName) { + return 'undefined' !== typeof hooks.__current[0]; + } + + // Return the __current hook. + return hooks.__current[0] ? hookName === hooks.__current[0].name : false; + }; +} + +exports.default = createDoingHook; + +/***/ }), +/* 21 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _validateHookName = __webpack_require__(0); + +var _validateHookName2 = _interopRequireDefault(_validateHookName); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * Returns a function which, when invoked, will return the number of times a + * hook has been called. + * + * @param {Object} hooks Stored hooks, keyed by hook name. + * + * @return {Function} Function that returns a hook's call count. + */ +function createDidHook(hooks) { + /** + * Returns the number of times an action has been fired. + * + * @param {string} hookName The hook name to check. + * + * @return {number} The number of times the hook has run. + */ + return function didHook(hookName) { + + if (!(0, _validateHookName2.default)(hookName)) { + return; + } + + return hooks.hasOwnProperty(hookName) && hooks[hookName].runs ? hooks[hookName].runs : 0; + }; +} + +exports.default = createDidHook; + +/***/ }) +/******/ ]); diff --git a/src/wp-includes/plugin.php b/src/wp-includes/plugin.php index 5a87e40635..c0c052a84a 100644 --- a/src/wp-includes/plugin.php +++ b/src/wp-includes/plugin.php @@ -363,7 +363,7 @@ function doing_filter( $filter = null ) { } /** - * Retrieve the name of an action currently being processed. + * Retrieve whether action currently being processed. * * @since 3.9.0 * diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 7a71cf624c..40b2559e50 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -85,6 +85,8 @@ function wp_default_scripts( &$scripts ) { $scripts->add( 'wp-a11y', "/wp-includes/js/wp-a11y$suffix.js", array( 'jquery' ), false, 1 ); + $scripts->add( 'wp-hooks', "/wp-includes/js/wp-hooks$suffix.js", array(), false, 1 ); + $scripts->add( 'sack', "/wp-includes/js/tw-sack$suffix.js", array(), '1.6.1', 1 ); $scripts->add( 'quicktags', "/wp-includes/js/quicktags$suffix.js", array(), false, 1 ); diff --git a/tests/qunit/index.html b/tests/qunit/index.html index 44447ee6b9..52f8f8d789 100644 --- a/tests/qunit/index.html +++ b/tests/qunit/index.html @@ -77,6 +77,7 @@ + @@ -125,6 +126,7 @@ + diff --git a/tests/qunit/wp-includes/js/wp-hooks.js b/tests/qunit/wp-includes/js/wp-hooks.js new file mode 100644 index 0000000000..3b54ea4177 --- /dev/null +++ b/tests/qunit/wp-includes/js/wp-hooks.js @@ -0,0 +1,235 @@ +/* global wp */ +( function( QUnit ) { + QUnit.module( 'wp-hooks' ); + + function filter_a( str ) { + return str + 'a'; + } + + function filter_b( str ) { + return str + 'b'; + } + + function filter_c( str ) { + return str + 'c'; + } + + function action_a() { + window.actionValue += 'a'; + } + + function action_b() { + window.actionValue += 'b'; + } + + function action_c() { + window.actionValue += 'c'; + } + + function filter_check( x ) { + ok( wp.hooks.doingFilter( 'runtest_filter' ), 'The runtest_filter is running.' ); + return x; + } + + window.actionValue = ''; + + QUnit.test( 'add and remove a filter', function() { + expect( 1 ); + wp.hooks.addFilter( 'test_filter', 'myPlugin/myNamespace/myCallback', filter_a ); + wp.hooks.removeFilter( 'test_filter', 'myPlugin/myNamespace/myCallback' ); + equal( wp.hooks.applyFilters( 'test_filter', 'test' ), 'test' ); + } ); + QUnit.test( 'add a filter and run it', function() { + expect( 1 ); + wp.hooks.addFilter( 'test_filter', 'myPlugin/myNamespace/myCallback_filter_a', filter_a ); + equal( wp.hooks.applyFilters( 'test_filter', 'test' ), 'testa' ); + wp.hooks.removeAllFilters( 'test_filter' ); + } ); + + QUnit.test( 'add 2 filters in a row and run them', function() { + expect( 1 ); + wp.hooks.addFilter( 'test_filter', 'myPlugin/myNamespace/myCallback_filter_a', filter_a ); + wp.hooks.addFilter( 'test_filter', 'myPlugin/myNamespace/myCallback_filter_b', filter_b ); + equal( wp.hooks.applyFilters( 'test_filter', 'test' ), 'testab' ); + wp.hooks.removeAllFilters( 'test_filter' ); + } ); + + QUnit.test( 'add 3 filters with different priorities and run them', function() { + expect( 1 ); + wp.hooks.addFilter( 'test_filter', 'myPlugin/myNamespace/myCallback_filter_a', filter_a ); + wp.hooks.addFilter( 'test_filter', 'myPlugin/myNamespace/myCallback_filter_b', filter_b, 2 ); + wp.hooks.addFilter( 'test_filter', 'myPlugin/myNamespace/myCallback_filter_c', filter_c, 8 ); + equal( wp.hooks.applyFilters( 'test_filter', 'test' ), 'testbca' ); + wp.hooks.removeAllFilters( 'test_filter' ); + } ); + + QUnit.test( 'add and remove an action', function() { + expect( 1 ); + window.actionValue = ''; + wp.hooks.addAction( 'test_action', 'myPlugin/myNamespace/myCallback', action_a ); + wp.hooks.removeAction( 'test_action', 'myPlugin/myNamespace/myCallback' ); + wp.hooks.doAction( 'test_action' ); + equal( window.actionValue, '' ); + } ); + + QUnit.test( 'add an action and run it', function() { + expect( 1 ); + window.actionValue = ''; + wp.hooks.addAction( 'test_action', 'myPlugin/myNamespace/myCallback', action_a ); + wp.hooks.doAction( 'test_action' ); + equal( window.actionValue, 'a' ); + wp.hooks.removeAllActions( 'test_action' ); + } ); + + QUnit.test( 'add 2 actions in a row and then run them', function() { + expect( 1 ); + window.actionValue = ''; + wp.hooks.addAction( 'test_action', 'myPlugin/myNamespace/myCallback', action_a ); + wp.hooks.addAction( 'test_action', 'myPlugin/myNamespace/myCallback', action_b ); + wp.hooks.doAction( 'test_action' ); + equal( window.actionValue, 'ab' ); + wp.hooks.removeAllActions( 'test_action' ); + } ); + + QUnit.test( 'add 3 actions with different priorities and run them', function() { + expect( 1 ); + window.actionValue = ''; + wp.hooks.addAction( 'test_action', 'myPlugin/myNamespace/myCallback', action_a ); + wp.hooks.addAction( 'test_action', 'myPlugin/myNamespace/myCallback', action_b, 2 ); + wp.hooks.addAction( 'test_action', 'myPlugin/myNamespace/myCallback', action_c, 8 ); + wp.hooks.doAction( 'test_action' ); + equal( window.actionValue, 'bca' ); + wp.hooks.removeAllActions( 'test_action' ); + } ); + + QUnit.test( 'pass in two arguments to an action', function() { + var arg1 = 10, + arg2 = 20; + + expect( 4 ); + + wp.hooks.addAction( 'test_action', 'myPlugin/myNamespace/myCallback', function( a, b ) { + equal( arg1, a ); + equal( arg2, b ); + } ); + wp.hooks.doAction( 'test_action', arg1, arg2 ); + wp.hooks.removeAllActions( 'test_action' ); + + equal( arg1, 10 ); + equal( arg2, 20 ); + } ); + + QUnit.test( 'fire action multiple times', function() { + var func; + expect( 2 ); + + func = function() { + ok( true ); + }; + + wp.hooks.addAction( 'test_action', 'myPlugin/myNamespace/myCallback', func ); + wp.hooks.doAction( 'test_action' ); + wp.hooks.doAction( 'test_action' ); + wp.hooks.removeAllActions( 'test_action' ); + } ); + + QUnit.test( 'remove specific action callback', function() { + window.actionValue = ''; + wp.hooks.addAction( 'test_action', 'myPlugin/myNamespace/myCallback_action_a', action_a ); + wp.hooks.addAction( 'test_action', 'myPlugin/myNamespace/myCallback_action_b', action_b, 2 ); + wp.hooks.addAction( 'test_action', 'myPlugin/myNamespace/myCallback_action_c', action_c, 8 ); + + wp.hooks.removeAction( 'test_action', 'myPlugin/myNamespace/myCallback_action_b' ); + wp.hooks.doAction( 'test_action' ); + equal( window.actionValue, 'ca' ); + wp.hooks.removeAllActions( 'test_action' ); + } ); + + QUnit.test( 'remove all action callbacks', function() { + window.actionValue = ''; + wp.hooks.addAction( 'test_action', 'myPlugin/myNamespace/myCallback_action_a', action_a ); + wp.hooks.addAction( 'test_action', 'myPlugin/myNamespace/myCallback_action_b', action_b, 2 ); + wp.hooks.addAction( 'test_action', 'myPlugin/myNamespace/myCallback_action_c', action_c, 8 ); + + wp.hooks.removeAllActions( 'test_action' ); + wp.hooks.doAction( 'test_action' ); + equal( window.actionValue, '' ); + } ); + + QUnit.test( 'remove specific filter callback', function() { + wp.hooks.addFilter( 'test_filter', 'myPlugin/myNamespace/myCallback_filter_a', filter_a ); + wp.hooks.addFilter( 'test_filter', 'myPlugin/myNamespace/myCallback_filter_b', filter_b, 2 ); + wp.hooks.addFilter( 'test_filter', 'myPlugin/myNamespace/myCallback_filter_c', filter_c, 8 ); + + wp.hooks.removeFilter( 'test_filter', 'myPlugin/myNamespace/myCallback_filter_b' ); + equal( wp.hooks.applyFilters( 'test_filter', 'test' ), 'testca' ); + wp.hooks.removeAllFilters( 'test_filter' ); + } ); + + QUnit.test( 'remove all filter callbacks', function() { + wp.hooks.addFilter( 'test_filter', 'myPlugin/myNamespace/myCallback_filter_a', filter_a ); + wp.hooks.addFilter( 'test_filter', 'myPlugin/myNamespace/myCallback_filter_b', filter_b, 2 ); + wp.hooks.addFilter( 'test_filter', 'myPlugin/myNamespace/myCallback_filter_c', filter_c, 8 ); + + wp.hooks.removeAllFilters( 'test_filter' ); + equal( wp.hooks.applyFilters( 'test_filter', 'test' ), 'test' ); + } ); + + // Test doingAction, didAction, hasAction. + QUnit.test( 'Test doingAction, didAction and hasAction.', function() { + + // Reset state for testing. + wp.hooks.removeAllActions( 'test_action' ); + wp.hooks.addAction( 'another_action', 'myPlugin/myNamespace/myCallback', function(){} ); + wp.hooks.doAction( 'another_action' ); + + // Verify no action is running yet. + ok( ! wp.hooks.doingAction( 'newtest_action' ), 'The newtest_action is not running.' ); + equal( wp.hooks.didAction( 'newtest_action' ), 0, 'The newtest_action has not run.' ); + ok( ! wp.hooks.hasAction( 'newtest_action' ), 'The newtest_action is not registered.' ); + + wp.hooks.addAction( 'newtest_action', 'myPlugin/myNamespace/myCallback', action_a ); + + // Verify action added, not running yet. + ok( ! wp.hooks.doingAction( 'newtest_action' ), 'The newtest_action is not running.' ); + equal( wp.hooks.didAction( 'newtest_action' ), 0, 'The newtest_action has not run.' ); + ok( wp.hooks.hasAction( 'newtest_action' ), 'The newtest_action is registered.' ); + + wp.hooks.doAction( 'newtest_action' ); + + // Verify action added and running. + equal( wp.hooks.didAction( 'newtest_action' ), 1, 'The newtest_action has run once.' ); + ok( wp.hooks.hasAction( 'newtest_action' ), 'The newtest_action is registered.' ); + + wp.hooks.doAction( 'newtest_action' ); + equal( wp.hooks.didAction( 'newtest_action' ), 2, 'The newtest_action has run twice.' ); + + wp.hooks.removeAllActions( 'newtest_action' ); + + // Verify state is reset appropriately. + equal( wp.hooks.didAction( 'newtest_action' ), 2, 'The newtest_action has run twice.' ); + ok( ! wp.hooks.hasAction( 'newtest_action' ), 'The newtest_action is not registered.' ); + + wp.hooks.doAction( 'another_action' ); + ok( ! wp.hooks.doingAction( 'newtest_action' ), 'The newtest_action is running.' ); + + // Verify hasAction returns false when no matching action. + ok( ! wp.hooks.hasAction( 'notanewtest_action' ), 'The notanewtest_action is registered.' ); + + } ); + + QUnit.test( 'Verify doingFilter, didFilter and hasFilter.', function() { + expect( 5 ); + wp.hooks.addFilter( 'runtest_filter', 'myPlugin/myNamespace/myCallback', filter_check ); + equal( wp.hooks.applyFilters( 'runtest_filter', 'test' ), 'test' ); + + // Verify filter added and running. + equal( wp.hooks.didFilter( 'runtest_filter' ), 1, 'The runtest_filter has run once.' ); + ok( wp.hooks.hasFilter( 'runtest_filter' ), 'The runtest_filter is registered.' ); + ok( ! wp.hooks.hasFilter( 'notatest_filter' ), 'The notatest_filter is not registered.' ); + + wp.hooks.removeAllFilters( 'runtest_filter' ); + } ); +/* +*/ +} )( window.QUnit );