<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[41375] trunk: Add `wp.hooks` - JavaScript actions and filters.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/41375">41375</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/41375","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>adamsilverstein</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-09-12 12:53:21 +0000 (Tue, 12 Sep 2017)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/21170">#21170</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkGruntfilejs">trunk/Gruntfile.js</a></li>
<li><a href="#trunksrcwpincludespluginphp">trunk/src/wp-includes/plugin.php</a></li>
<li><a href="#trunksrcwpincludesscriptloaderphp">trunk/src/wp-includes/script-loader.php</a></li>
<li><a href="#trunktestsqunitindexhtml">trunk/tests/qunit/index.html</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesjswphooksjs">trunk/src/wp-includes/js/wp-hooks.js</a></li>
<li><a href="#trunktestsqunitwpincludesjswphooksjs">trunk/tests/qunit/wp-includes/js/wp-hooks.js</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkGruntfilejs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/Gruntfile.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/Gruntfile.js 2017-09-12 07:02:49 UTC (rev 41374)
+++ trunk/Gruntfile.js 2017-09-12 12:53:21 UTC (rev 41375)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -369,7 +369,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> '!wp-includes/js/json2.js',
</span><span class="cx" style="display: block; padding: 0 10px"> '!wp-includes/js/tw-sack.js',
</span><span class="cx" style="display: block; padding: 0 10px"> '!wp-includes/js/twemoji.js',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- '!**/*.min.js'
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '!**/*.min.js',
+ '!wp-includes/js/wp-hooks.js'
</ins><span class="cx" style="display: block; padding: 0 10px"> ],
</span><span class="cx" style="display: block; padding: 0 10px"> // Remove once other JSHint errors are resolved
</span><span class="cx" style="display: block; padding: 0 10px"> options: {
</span></span></pre></div>
<a id="trunksrcwpincludesjswphooksjs"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/js/wp-hooks.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/js/wp-hooks.js (rev 0)
+++ trunk/src/wp-includes/js/wp-hooks.js 2017-09-12 12:53:21 UTC (rev 41375)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,698 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+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;
+
+/***/ })
+/******/ ]);
</ins></span></pre></div>
<a id="trunksrcwpincludespluginphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/plugin.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/plugin.php 2017-09-12 07:02:49 UTC (rev 41374)
+++ trunk/src/wp-includes/plugin.php 2017-09-12 12:53:21 UTC (rev 41375)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -363,7 +363,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Retrieve the name of an action currently being processed.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieve whether action currently being processed.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.9.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span></span></pre></div>
<a id="trunksrcwpincludesscriptloaderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/script-loader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/script-loader.php 2017-09-12 07:02:49 UTC (rev 41374)
+++ trunk/src/wp-includes/script-loader.php 2017-09-12 12:53:21 UTC (rev 41375)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -85,6 +85,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $scripts->add( 'wp-a11y', "/wp-includes/js/wp-a11y$suffix.js", array( 'jquery' ), false, 1 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $scripts->add( 'wp-hooks', "/wp-includes/js/wp-hooks$suffix.js", array(), false, 1 );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $scripts->add( 'sack', "/wp-includes/js/tw-sack$suffix.js", array(), '1.6.1', 1 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $scripts->add( 'quicktags', "/wp-includes/js/quicktags$suffix.js", array(), false, 1 );
</span></span></pre></div>
<a id="trunktestsqunitindexhtml"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/qunit/index.html</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/qunit/index.html 2017-09-12 07:02:49 UTC (rev 41374)
+++ trunk/tests/qunit/index.html 2017-09-12 12:53:21 UTC (rev 41375)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -77,6 +77,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <script src="../../src/wp-includes/js/customize-base.js"></script>
</span><span class="cx" style="display: block; padding: 0 10px"> <script src="../../src/wp-includes/js/customize-models.js"></script>
</span><span class="cx" style="display: block; padding: 0 10px"> <script src="../../src/wp-includes/js/shortcode.js"></script>
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ <script src="../../src/wp-includes/js/wp-hooks.js"></script>
</ins><span class="cx" style="display: block; padding: 0 10px"> <script src="../../src/wp-admin/js/customize-controls.js"></script>
</span><span class="cx" style="display: block; padding: 0 10px"> <script src="../../src/wp-includes/js/api-request.js"></script>
</span><span class="cx" style="display: block; padding: 0 10px"> <script src="../../src/wp-includes/js/wp-api.js"></script>
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -125,6 +126,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <script src="wp-admin/js/customize-header.js"></script>
</span><span class="cx" style="display: block; padding: 0 10px"> <script src="wp-includes/js/shortcode.js"></script>
</span><span class="cx" style="display: block; padding: 0 10px"> <script src="wp-includes/js/api-request.js"></script>
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ <script src="wp-includes/js/wp-hooks.js"></script>
</ins><span class="cx" style="display: block; padding: 0 10px"> <script src="wp-includes/js/wp-api.js"></script>
</span><span class="cx" style="display: block; padding: 0 10px"> <script src="wp-admin/js/customize-controls.js"></script>
</span><span class="cx" style="display: block; padding: 0 10px"> <script src="wp-admin/js/customize-controls-utils.js"></script>
</span></span></pre></div>
<a id="trunktestsqunitwpincludesjswphooksjs"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/qunit/wp-includes/js/wp-hooks.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/qunit/wp-includes/js/wp-hooks.js (rev 0)
+++ trunk/tests/qunit/wp-includes/js/wp-hooks.js 2017-09-12 12:53:21 UTC (rev 41375)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,235 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/* 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 );
</ins></span></pre>
</div>
</div>
</body>
</html>