diff --git src/wp-includes/js/wp-hooks.js src/wp-includes/js/wp-hooks.js
new file mode 100644
index 0000000000..6ae8963a63
--- /dev/null
+++ src/wp-includes/js/wp-hooks.js
@@ -0,0 +1,482 @@
+/******/ (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 = 1);
+/******/ })
+/************************************************************************/
+/******/ ([
+/* 0 */
+/***/ (function(module, __webpack_exports__, __webpack_require__) {
+
+"use strict";
+/**
+ * Contains the registered hooks, keyed by hook type. Each hook type is an
+ * array of objects with priority and callback of each registered hook.
+ */
+const HOOKS = {
+ actions: {},
+ filters: {},
+};
+
+/* harmony default export */ __webpack_exports__["a"] = (HOOKS);
+
+
+/***/ }),
+/* 1 */
+/***/ (function(module, __webpack_exports__, __webpack_require__) {
+
+"use strict";
+Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
+/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__hooks__ = __webpack_require__(0);
+/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1____ = __webpack_require__(2);
+
+
+
+const hooks = {
+ addAction: __WEBPACK_IMPORTED_MODULE_1____["a" /* addAction */],
+ addFilter: __WEBPACK_IMPORTED_MODULE_1____["b" /* addFilter */],
+ removeAction: __WEBPACK_IMPORTED_MODULE_1____["m" /* removeAction */],
+ removeFilter: __WEBPACK_IMPORTED_MODULE_1____["p" /* removeFilter */],
+ removeAllActions: __WEBPACK_IMPORTED_MODULE_1____["n" /* removeAllActions */],
+ removeAllFilters: __WEBPACK_IMPORTED_MODULE_1____["o" /* removeAllFilters */],
+ hasAction: __WEBPACK_IMPORTED_MODULE_1____["k" /* hasAction */],
+ hasFilter: __WEBPACK_IMPORTED_MODULE_1____["l" /* hasFilter */],
+ doAction: __WEBPACK_IMPORTED_MODULE_1____["h" /* doAction */],
+ applyFilters: __WEBPACK_IMPORTED_MODULE_1____["c" /* applyFilters */],
+ currentAction: __WEBPACK_IMPORTED_MODULE_1____["d" /* currentAction */],
+ currentFilter: __WEBPACK_IMPORTED_MODULE_1____["e" /* currentFilter */],
+ doingAction: __WEBPACK_IMPORTED_MODULE_1____["i" /* doingAction */],
+ doingFilter: __WEBPACK_IMPORTED_MODULE_1____["j" /* doingFilter */],
+ didAction: __WEBPACK_IMPORTED_MODULE_1____["f" /* didAction */],
+ didFilter: __WEBPACK_IMPORTED_MODULE_1____["g" /* didFilter */]
+}
+
+window.wp = window.wp || {};
+window.wp.hooks = hooks;
+
+
+/***/ }),
+/* 2 */
+/***/ (function(module, __webpack_exports__, __webpack_require__) {
+
+"use strict";
+/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__hooks__ = __webpack_require__(0);
+/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__createAddHook__ = __webpack_require__(3);
+/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__createRemoveHook__ = __webpack_require__(4);
+/* harmony import */ var __WEBPACK_IMPORTED_MODULE_3__createHasHook__ = __webpack_require__(5);
+/* harmony import */ var __WEBPACK_IMPORTED_MODULE_4__createRunHook__ = __webpack_require__(6);
+/* harmony import */ var __WEBPACK_IMPORTED_MODULE_5__createDoingHook__ = __webpack_require__(7);
+/* harmony import */ var __WEBPACK_IMPORTED_MODULE_6__createDidHook__ = __webpack_require__(8);
+
+
+
+
+
+
+
+
+// Add action/filter functions.
+const addAction = Object(__WEBPACK_IMPORTED_MODULE_1__createAddHook__["a" /* default */])( __WEBPACK_IMPORTED_MODULE_0__hooks__["a" /* default */].actions );
+/* harmony export (immutable) */ __webpack_exports__["a"] = addAction;
+
+const addFilter = Object(__WEBPACK_IMPORTED_MODULE_1__createAddHook__["a" /* default */])( __WEBPACK_IMPORTED_MODULE_0__hooks__["a" /* default */].filters );
+/* harmony export (immutable) */ __webpack_exports__["b"] = addFilter;
+
+
+// Remove action/filter functions.
+const removeAction = Object(__WEBPACK_IMPORTED_MODULE_2__createRemoveHook__["a" /* default */])( __WEBPACK_IMPORTED_MODULE_0__hooks__["a" /* default */].actions );
+/* harmony export (immutable) */ __webpack_exports__["m"] = removeAction;
+
+const removeFilter = Object(__WEBPACK_IMPORTED_MODULE_2__createRemoveHook__["a" /* default */])( __WEBPACK_IMPORTED_MODULE_0__hooks__["a" /* default */].filters );
+/* harmony export (immutable) */ __webpack_exports__["p"] = removeFilter;
+
+
+// Has action/filter functions.
+const hasAction = Object(__WEBPACK_IMPORTED_MODULE_3__createHasHook__["a" /* default */])( __WEBPACK_IMPORTED_MODULE_0__hooks__["a" /* default */].actions );
+/* harmony export (immutable) */ __webpack_exports__["k"] = hasAction;
+
+const hasFilter = Object(__WEBPACK_IMPORTED_MODULE_3__createHasHook__["a" /* default */])( __WEBPACK_IMPORTED_MODULE_0__hooks__["a" /* default */].filters );
+/* harmony export (immutable) */ __webpack_exports__["l"] = hasFilter;
+
+
+// Remove all actions/filters functions.
+const removeAllActions = Object(__WEBPACK_IMPORTED_MODULE_2__createRemoveHook__["a" /* default */])( __WEBPACK_IMPORTED_MODULE_0__hooks__["a" /* default */].actions, true );
+/* harmony export (immutable) */ __webpack_exports__["n"] = removeAllActions;
+
+const removeAllFilters = Object(__WEBPACK_IMPORTED_MODULE_2__createRemoveHook__["a" /* default */])( __WEBPACK_IMPORTED_MODULE_0__hooks__["a" /* default */].filters, true );
+/* harmony export (immutable) */ __webpack_exports__["o"] = removeAllFilters;
+
+
+// Do action/apply filters functions.
+const doAction = Object(__WEBPACK_IMPORTED_MODULE_4__createRunHook__["a" /* default */])( __WEBPACK_IMPORTED_MODULE_0__hooks__["a" /* default */].actions );
+/* harmony export (immutable) */ __webpack_exports__["h"] = doAction;
+
+const applyFilters = Object(__WEBPACK_IMPORTED_MODULE_4__createRunHook__["a" /* default */])( __WEBPACK_IMPORTED_MODULE_0__hooks__["a" /* default */].filters, true );
+/* harmony export (immutable) */ __webpack_exports__["c"] = applyFilters;
+
+
+// Current action/filter functions.
+const currentAction = () => __WEBPACK_IMPORTED_MODULE_0__hooks__["a" /* default */].actions.current || null;
+/* harmony export (immutable) */ __webpack_exports__["d"] = currentAction;
+
+const currentFilter = () => __WEBPACK_IMPORTED_MODULE_0__hooks__["a" /* default */].filters.current || null;
+/* harmony export (immutable) */ __webpack_exports__["e"] = currentFilter;
+
+
+// Doing action/filter: true while a hook is being run.
+const doingAction = Object(__WEBPACK_IMPORTED_MODULE_5__createDoingHook__["a" /* default */])( __WEBPACK_IMPORTED_MODULE_0__hooks__["a" /* default */].actions );
+/* harmony export (immutable) */ __webpack_exports__["i"] = doingAction;
+
+const doingFilter = Object(__WEBPACK_IMPORTED_MODULE_5__createDoingHook__["a" /* default */])( __WEBPACK_IMPORTED_MODULE_0__hooks__["a" /* default */].filters );
+/* harmony export (immutable) */ __webpack_exports__["j"] = doingFilter;
+
+
+// Did action/filter functions.
+const didAction = Object(__WEBPACK_IMPORTED_MODULE_6__createDidHook__["a" /* default */])( __WEBPACK_IMPORTED_MODULE_0__hooks__["a" /* default */].actions );
+/* harmony export (immutable) */ __webpack_exports__["f"] = didAction;
+
+const didFilter = Object(__WEBPACK_IMPORTED_MODULE_6__createDidHook__["a" /* default */])( __WEBPACK_IMPORTED_MODULE_0__hooks__["a" /* default */].filters );
+/* harmony export (immutable) */ __webpack_exports__["g"] = didFilter;
+
+
+
+/***/ }),
+/* 3 */
+/***/ (function(module, __webpack_exports__, __webpack_require__) {
+
+"use strict";
+/**
+ * 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 {Function} callback Function to call when the hook is run
+ * @param {?number} priority Priority of this hook (default=10)
+ */
+ return function addHook( hookName, callback, priority ) {
+ if ( typeof hookName !== 'string' ) {
+ console.error( 'The hook name must be a string.' );
+ return;
+ }
+
+ if ( typeof callback !== 'function' ) {
+ console.error( 'The hook callback must be a function.' );
+ return;
+ }
+
+ // Assign default priority
+ if ( 'undefined' === typeof priority ) {
+ priority = 10;
+ } else {
+ priority = parseInt( priority, 10 );
+ }
+
+ // Validate numeric priority
+ if ( isNaN( priority ) ) {
+ console.error( 'The hook priority must be omitted or a number.' );
+ return;
+ }
+
+ const handler = { callback, priority };
+ let handlers;
+
+ if ( hooks.hasOwnProperty( hookName ) ) {
+ // Find the correct insert index of the new hook.
+ handlers = hooks[ hookName ];
+ let i = 0;
+ while ( i < handlers.length ) {
+ if ( handlers[ i ].priority > priority ) {
+ break;
+ }
+ i++;
+ }
+ // Insert (or append) the new hook.
+ handlers.splice( i, 0, handler );
+ } else {
+ // This is the first hook of its type.
+ handlers = [ handler ];
+ }
+
+ hooks[ hookName ] = handlers;
+ };
+}
+
+/* harmony default export */ __webpack_exports__["a"] = (createAddHook);
+
+
+/***/ }),
+/* 4 */
+/***/ (function(module, __webpack_exports__, __webpack_require__) {
+
+"use strict";
+/**
+ * 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 hooked callbacks.
+ *
+ * @return {Function} Function that removes hooks.
+ */
+function createRemoveHook( hooks, removeAll ) {
+ /**
+ * Removes the specified callback (or all callbacks) from the hook with a
+ * given name.
+ *
+ * @param {string} hookName The name of the hook to modify.
+ * @param {?Function} callback The specific callback to be removed. If
+ * omitted (and `removeAll` is truthy), clears
+ * all callbacks.
+ */
+ return function removeHook( hookName, callback ) {
+ // Bail if no hooks exist by this name
+ if ( ! hooks.hasOwnProperty( hookName ) ) {
+ return;
+ }
+
+ if ( removeAll ) {
+ const runs = hooks[ hookName ].runs;
+ hooks[ hookName ] = [];
+ if ( runs ) {
+ hooks[ hookName ].runs = runs;
+ }
+ } else if ( callback ) {
+ // Try to find the specified callback to remove.
+ const handlers = hooks[ hookName ];
+ for ( let i = handlers.length - 1; i >= 0; i-- ) {
+ if ( handlers[ i ].callback === callback ) {
+ handlers.splice( i, 1 );
+ }
+ }
+ }
+ };
+}
+
+/* harmony default export */ __webpack_exports__["a"] = (createRemoveHook);
+
+
+/***/ }),
+/* 5 */
+/***/ (function(module, __webpack_exports__, __webpack_require__) {
+
+"use strict";
+/**
+ * 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 ].length
+ : 0;
+ };
+}
+
+/* harmony default export */ __webpack_exports__["a"] = (createHasHook);
+
+
+/***/ }),
+/* 6 */
+/***/ (function(module, __webpack_exports__, __webpack_require__) {
+
+"use strict";
+/**
+ * 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, ...args ) {
+ const handlers = hooks[ hookName ];
+ let maybeReturnValue = args[ 0 ];
+
+ if ( ! handlers ) {
+ return ( returnFirstArg ? maybeReturnValue : undefined );
+ }
+
+ hooks.current = hookName;
+ handlers.runs = ( handlers.runs || 0 ) + 1;
+
+ handlers.forEach( handler => {
+ maybeReturnValue = handler.callback.apply( null, args );
+ if ( returnFirstArg ) {
+ args[ 0 ] = maybeReturnValue;
+ }
+ } );
+
+ delete hooks.current;
+
+ if ( returnFirstArg ) {
+ return maybeReturnValue;
+ }
+ };
+}
+
+/* harmony default export */ __webpack_exports__["a"] = (createRunHook);
+
+
+/***/ }),
+/* 7 */
+/***/ (function(module, __webpack_exports__, __webpack_require__) {
+
+"use strict";
+/**
+ * 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;
+ }
+
+ // Return the current hook.
+ return hooks.current
+ ? hookName === hooks.current
+ : false;
+ };
+}
+
+/* harmony default export */ __webpack_exports__["a"] = (createDoingHook);
+
+
+/***/ }),
+/* 8 */
+/***/ (function(module, __webpack_exports__, __webpack_require__) {
+
+"use strict";
+/**
+ * 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 ) {
+ return hooks.hasOwnProperty( hookName ) && hooks[ hookName ].runs
+ ? hooks[ hookName ].runs
+ : 0;
+ };
+}
+
+/* harmony default export */ __webpack_exports__["a"] = (createDidHook);
+
+
+/***/ })
+/******/ ]);
diff --git src/wp-includes/plugin.php src/wp-includes/plugin.php
index 86f1c3b319..86f9db8964 100644
--- src/wp-includes/plugin.php
+++ 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 src/wp-includes/script-loader.php src/wp-includes/script-loader.php
index 0058c5e956..790272188e 100644
--- src/wp-includes/script-loader.php
+++ 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 tests/qunit/index.html tests/qunit/index.html
index c41fffe63a..183c492d85 100644
--- tests/qunit/index.html
+++ tests/qunit/index.html
@@ -76,6 +76,7 @@
+
@@ -122,6 +123,7 @@
+
diff --git tests/qunit/wp-includes/js/wp-hooks.js tests/qunit/wp-includes/js/wp-hooks.js
new file mode 100644
index 0000000000..43c8200c66
--- /dev/null
+++ tests/qunit/wp-includes/js/wp-hooks.js
@@ -0,0 +1,227 @@
+/* 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() {
+ ok( wp.hooks.doingFilter( 'runtest.filter' ), 'The runtest.filter is running.' );
+ }
+ window.actionValue = '';
+
+ QUnit.test( 'add and remove a filter', function() {
+ expect( 1 );
+ wp.hooks.addFilter( 'test.filter', filter_a );
+ wp.hooks.removeFilter( 'test.filter', filter_a );
+ equal( wp.hooks.applyFilters( 'test.filter', 'test' ), 'test' );
+ } );
+
+ QUnit.test( 'add a filter and run it', function() {
+ expect( 1 );
+ wp.hooks.addFilter( 'test.filter', 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', filter_a );
+ wp.hooks.addFilter( 'test.filter', 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', filter_a );
+ wp.hooks.addFilter( 'test.filter', filter_b, 2 );
+ wp.hooks.addFilter( 'test.filter', 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', action_a );
+ wp.hooks.removeAction( 'test.action', action_a );
+ 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', 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', action_a );
+ wp.hooks.addAction( 'test.action', 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', action_a );
+ wp.hooks.addAction( 'test.action', action_b, 2 );
+ wp.hooks.addAction( 'test.action', 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', 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', 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', action_a );
+ wp.hooks.addAction( 'test.action', action_b, 2 );
+ wp.hooks.addAction( 'test.action', action_c, 8 );
+
+ wp.hooks.removeAction( 'test.action', 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', action_a );
+ wp.hooks.addAction( 'test.action', action_b, 2 );
+ wp.hooks.addAction( 'test.action', 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', filter_a );
+ wp.hooks.addFilter( 'test.filter', filter_b, 2 );
+ wp.hooks.addFilter( 'test.filter', filter_c, 8 );
+
+ wp.hooks.removeFilter( 'test.filter', 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', filter_a );
+ wp.hooks.addFilter( 'test.filter', filter_b, 2 );
+ wp.hooks.addFilter( 'test.filter', 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', function(){} );
+ wp.hooks.doAction( 'another.action' );
+
+ // Verify no action is running yet.
+ ok( ! wp.hooks.doingAction( 'test.action' ), 'The test.action is not running.' );
+ equal( wp.hooks.didAction( 'new.test.action' ), 0, 'The test.action has not run.' );
+ ok( ! wp.hooks.hasAction( 'test.action' ), 'The test.action is not registered.' );
+
+ wp.hooks.addAction( 'test.action', action_a );
+
+ // Verify action added, not running yet.
+ ok( ! wp.hooks.doingAction( 'test.action' ), 'The test.action is not running.' );
+ equal( wp.hooks.didAction( 'test.action' ), 0, 'The test.action has not run.' );
+ ok( wp.hooks.hasAction( 'test.action' ), 'The test.action is registered.' );
+
+ wp.hooks.doAction( 'test.action' );
+
+ // Verify action added and running.
+ equal( wp.hooks.didAction( 'test.action' ), 1, 'The test.action has run once.' );
+ ok( wp.hooks.hasAction( 'test.action' ), 'The test.action is registered.' );
+
+ wp.hooks.doAction( 'test.action' );
+ equal( wp.hooks.didAction( 'test.action' ), 2, 'The test.action has run twice.' );
+
+ wp.hooks.removeAllActions( 'test.action' );
+
+ // Verify state is reset appropriately.
+ equal( wp.hooks.didAction( 'test.action' ), 2, 'The test.action has run twice.' );
+ ok( ! wp.hooks.hasAction( 'test.action' ), 'The test.action is not registered.' );
+
+ wp.hooks.doAction( 'another.action' );
+ ok( ! wp.hooks.doingAction( 'test.action' ), 'The test.action is running.' );
+
+ // Verify hasAction returns false when no matching action.
+ ok( ! wp.hooks.hasAction( 'notatest.action' ), 'The notatest.action is registered.' );
+
+ } );
+
+ QUnit.test( 'Verify doingFilter, didFilter and hasFilter.', function() {
+ expect( 4 );
+ wp.hooks.addFilter( 'runtest.filter', filter_check );
+
+ // Verify filter added and running.
+ var test = wp.hooks.applyFilters( 'runtest.filter', true );
+ 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 );