hooks.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /******/ (function() { // webpackBootstrap
  2. /******/ "use strict";
  3. /******/ // The require scope
  4. /******/ var __webpack_require__ = {};
  5. /******/
  6. /************************************************************************/
  7. /******/ /* webpack/runtime/define property getters */
  8. /******/ !function() {
  9. /******/ // define getter functions for harmony exports
  10. /******/ __webpack_require__.d = function(exports, definition) {
  11. /******/ for(var key in definition) {
  12. /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  13. /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
  14. /******/ }
  15. /******/ }
  16. /******/ };
  17. /******/ }();
  18. /******/
  19. /******/ /* webpack/runtime/hasOwnProperty shorthand */
  20. /******/ !function() {
  21. /******/ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
  22. /******/ }();
  23. /******/
  24. /******/ /* webpack/runtime/make namespace object */
  25. /******/ !function() {
  26. /******/ // define __esModule on exports
  27. /******/ __webpack_require__.r = function(exports) {
  28. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  29. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  30. /******/ }
  31. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  32. /******/ };
  33. /******/ }();
  34. /******/
  35. /************************************************************************/
  36. var __webpack_exports__ = {};
  37. // ESM COMPAT FLAG
  38. __webpack_require__.r(__webpack_exports__);
  39. // EXPORTS
  40. __webpack_require__.d(__webpack_exports__, {
  41. "actions": function() { return /* binding */ actions; },
  42. "addAction": function() { return /* binding */ addAction; },
  43. "addFilter": function() { return /* binding */ addFilter; },
  44. "applyFilters": function() { return /* binding */ applyFilters; },
  45. "createHooks": function() { return /* reexport */ build_module_createHooks; },
  46. "currentAction": function() { return /* binding */ currentAction; },
  47. "currentFilter": function() { return /* binding */ currentFilter; },
  48. "defaultHooks": function() { return /* binding */ defaultHooks; },
  49. "didAction": function() { return /* binding */ didAction; },
  50. "didFilter": function() { return /* binding */ didFilter; },
  51. "doAction": function() { return /* binding */ doAction; },
  52. "doingAction": function() { return /* binding */ doingAction; },
  53. "doingFilter": function() { return /* binding */ doingFilter; },
  54. "filters": function() { return /* binding */ filters; },
  55. "hasAction": function() { return /* binding */ hasAction; },
  56. "hasFilter": function() { return /* binding */ hasFilter; },
  57. "removeAction": function() { return /* binding */ removeAction; },
  58. "removeAllActions": function() { return /* binding */ removeAllActions; },
  59. "removeAllFilters": function() { return /* binding */ removeAllFilters; },
  60. "removeFilter": function() { return /* binding */ removeFilter; }
  61. });
  62. ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/validateNamespace.js
  63. /**
  64. * Validate a namespace string.
  65. *
  66. * @param {string} namespace The namespace to validate - should take the form
  67. * `vendor/plugin/function`.
  68. *
  69. * @return {boolean} Whether the namespace is valid.
  70. */
  71. function validateNamespace(namespace) {
  72. if ('string' !== typeof namespace || '' === namespace) {
  73. // eslint-disable-next-line no-console
  74. console.error('The namespace must be a non-empty string.');
  75. return false;
  76. }
  77. if (!/^[a-zA-Z][a-zA-Z0-9_.\-\/]*$/.test(namespace)) {
  78. // eslint-disable-next-line no-console
  79. console.error('The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.');
  80. return false;
  81. }
  82. return true;
  83. }
  84. /* harmony default export */ var build_module_validateNamespace = (validateNamespace);
  85. ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/validateHookName.js
  86. /**
  87. * Validate a hookName string.
  88. *
  89. * @param {string} hookName The hook name to validate. Should be a non empty string containing
  90. * only numbers, letters, dashes, periods and underscores. Also,
  91. * the hook name cannot begin with `__`.
  92. *
  93. * @return {boolean} Whether the hook name is valid.
  94. */
  95. function validateHookName(hookName) {
  96. if ('string' !== typeof hookName || '' === hookName) {
  97. // eslint-disable-next-line no-console
  98. console.error('The hook name must be a non-empty string.');
  99. return false;
  100. }
  101. if (/^__/.test(hookName)) {
  102. // eslint-disable-next-line no-console
  103. console.error('The hook name cannot begin with `__`.');
  104. return false;
  105. }
  106. if (!/^[a-zA-Z][a-zA-Z0-9_.-]*$/.test(hookName)) {
  107. // eslint-disable-next-line no-console
  108. console.error('The hook name can only contain numbers, letters, dashes, periods and underscores.');
  109. return false;
  110. }
  111. return true;
  112. }
  113. /* harmony default export */ var build_module_validateHookName = (validateHookName);
  114. ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/createAddHook.js
  115. /**
  116. * Internal dependencies
  117. */
  118. /**
  119. * @callback AddHook
  120. *
  121. * Adds the hook to the appropriate hooks container.
  122. *
  123. * @param {string} hookName Name of hook to add
  124. * @param {string} namespace The unique namespace identifying the callback in the form `vendor/plugin/function`.
  125. * @param {import('.').Callback} callback Function to call when the hook is run
  126. * @param {number} [priority=10] Priority of this hook
  127. */
  128. /**
  129. * Returns a function which, when invoked, will add a hook.
  130. *
  131. * @param {import('.').Hooks} hooks Hooks instance.
  132. * @param {import('.').StoreKey} storeKey
  133. *
  134. * @return {AddHook} Function that adds a new hook.
  135. */
  136. function createAddHook(hooks, storeKey) {
  137. return function addHook(hookName, namespace, callback) {
  138. let priority = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 10;
  139. const hooksStore = hooks[storeKey];
  140. if (!build_module_validateHookName(hookName)) {
  141. return;
  142. }
  143. if (!build_module_validateNamespace(namespace)) {
  144. return;
  145. }
  146. if ('function' !== typeof callback) {
  147. // eslint-disable-next-line no-console
  148. console.error('The hook callback must be a function.');
  149. return;
  150. } // Validate numeric priority
  151. if ('number' !== typeof priority) {
  152. // eslint-disable-next-line no-console
  153. console.error('If specified, the hook priority must be a number.');
  154. return;
  155. }
  156. const handler = {
  157. callback,
  158. priority,
  159. namespace
  160. };
  161. if (hooksStore[hookName]) {
  162. // Find the correct insert index of the new hook.
  163. const handlers = hooksStore[hookName].handlers;
  164. /** @type {number} */
  165. let i;
  166. for (i = handlers.length; i > 0; i--) {
  167. if (priority >= handlers[i - 1].priority) {
  168. break;
  169. }
  170. }
  171. if (i === handlers.length) {
  172. // If append, operate via direct assignment.
  173. handlers[i] = handler;
  174. } else {
  175. // Otherwise, insert before index via splice.
  176. handlers.splice(i, 0, handler);
  177. } // We may also be currently executing this hook. If the callback
  178. // we're adding would come after the current callback, there's no
  179. // problem; otherwise we need to increase the execution index of
  180. // any other runs by 1 to account for the added element.
  181. hooksStore.__current.forEach(hookInfo => {
  182. if (hookInfo.name === hookName && hookInfo.currentIndex >= i) {
  183. hookInfo.currentIndex++;
  184. }
  185. });
  186. } else {
  187. // This is the first hook of its type.
  188. hooksStore[hookName] = {
  189. handlers: [handler],
  190. runs: 0
  191. };
  192. }
  193. if (hookName !== 'hookAdded') {
  194. hooks.doAction('hookAdded', hookName, namespace, callback, priority);
  195. }
  196. };
  197. }
  198. /* harmony default export */ var build_module_createAddHook = (createAddHook);
  199. ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/createRemoveHook.js
  200. /**
  201. * Internal dependencies
  202. */
  203. /**
  204. * @callback RemoveHook
  205. * Removes the specified callback (or all callbacks) from the hook with a given hookName
  206. * and namespace.
  207. *
  208. * @param {string} hookName The name of the hook to modify.
  209. * @param {string} namespace The unique namespace identifying the callback in the
  210. * form `vendor/plugin/function`.
  211. *
  212. * @return {number | undefined} The number of callbacks removed.
  213. */
  214. /**
  215. * Returns a function which, when invoked, will remove a specified hook or all
  216. * hooks by the given name.
  217. *
  218. * @param {import('.').Hooks} hooks Hooks instance.
  219. * @param {import('.').StoreKey} storeKey
  220. * @param {boolean} [removeAll=false] Whether to remove all callbacks for a hookName,
  221. * without regard to namespace. Used to create
  222. * `removeAll*` functions.
  223. *
  224. * @return {RemoveHook} Function that removes hooks.
  225. */
  226. function createRemoveHook(hooks, storeKey) {
  227. let removeAll = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  228. return function removeHook(hookName, namespace) {
  229. const hooksStore = hooks[storeKey];
  230. if (!build_module_validateHookName(hookName)) {
  231. return;
  232. }
  233. if (!removeAll && !build_module_validateNamespace(namespace)) {
  234. return;
  235. } // Bail if no hooks exist by this name.
  236. if (!hooksStore[hookName]) {
  237. return 0;
  238. }
  239. let handlersRemoved = 0;
  240. if (removeAll) {
  241. handlersRemoved = hooksStore[hookName].handlers.length;
  242. hooksStore[hookName] = {
  243. runs: hooksStore[hookName].runs,
  244. handlers: []
  245. };
  246. } else {
  247. // Try to find the specified callback to remove.
  248. const handlers = hooksStore[hookName].handlers;
  249. for (let i = handlers.length - 1; i >= 0; i--) {
  250. if (handlers[i].namespace === namespace) {
  251. handlers.splice(i, 1);
  252. handlersRemoved++; // This callback may also be part of a hook that is
  253. // currently executing. If the callback we're removing
  254. // comes after the current callback, there's no problem;
  255. // otherwise we need to decrease the execution index of any
  256. // other runs by 1 to account for the removed element.
  257. hooksStore.__current.forEach(hookInfo => {
  258. if (hookInfo.name === hookName && hookInfo.currentIndex >= i) {
  259. hookInfo.currentIndex--;
  260. }
  261. });
  262. }
  263. }
  264. }
  265. if (hookName !== 'hookRemoved') {
  266. hooks.doAction('hookRemoved', hookName, namespace);
  267. }
  268. return handlersRemoved;
  269. };
  270. }
  271. /* harmony default export */ var build_module_createRemoveHook = (createRemoveHook);
  272. ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/createHasHook.js
  273. /**
  274. * @callback HasHook
  275. *
  276. * Returns whether any handlers are attached for the given hookName and optional namespace.
  277. *
  278. * @param {string} hookName The name of the hook to check for.
  279. * @param {string} [namespace] Optional. The unique namespace identifying the callback
  280. * in the form `vendor/plugin/function`.
  281. *
  282. * @return {boolean} Whether there are handlers that are attached to the given hook.
  283. */
  284. /**
  285. * Returns a function which, when invoked, will return whether any handlers are
  286. * attached to a particular hook.
  287. *
  288. * @param {import('.').Hooks} hooks Hooks instance.
  289. * @param {import('.').StoreKey} storeKey
  290. *
  291. * @return {HasHook} Function that returns whether any handlers are
  292. * attached to a particular hook and optional namespace.
  293. */
  294. function createHasHook(hooks, storeKey) {
  295. return function hasHook(hookName, namespace) {
  296. const hooksStore = hooks[storeKey]; // Use the namespace if provided.
  297. if ('undefined' !== typeof namespace) {
  298. return hookName in hooksStore && hooksStore[hookName].handlers.some(hook => hook.namespace === namespace);
  299. }
  300. return hookName in hooksStore;
  301. };
  302. }
  303. /* harmony default export */ var build_module_createHasHook = (createHasHook);
  304. ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/createRunHook.js
  305. /**
  306. * Returns a function which, when invoked, will execute all callbacks
  307. * registered to a hook of the specified type, optionally returning the final
  308. * value of the call chain.
  309. *
  310. * @param {import('.').Hooks} hooks Hooks instance.
  311. * @param {import('.').StoreKey} storeKey
  312. * @param {boolean} [returnFirstArg=false] Whether each hook callback is expected to
  313. * return its first argument.
  314. *
  315. * @return {(hookName:string, ...args: unknown[]) => unknown} Function that runs hook callbacks.
  316. */
  317. function createRunHook(hooks, storeKey) {
  318. let returnFirstArg = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  319. return function runHooks(hookName) {
  320. const hooksStore = hooks[storeKey];
  321. if (!hooksStore[hookName]) {
  322. hooksStore[hookName] = {
  323. handlers: [],
  324. runs: 0
  325. };
  326. }
  327. hooksStore[hookName].runs++;
  328. const handlers = hooksStore[hookName].handlers; // The following code is stripped from production builds.
  329. if (false) {}
  330. for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  331. args[_key - 1] = arguments[_key];
  332. }
  333. if (!handlers || !handlers.length) {
  334. return returnFirstArg ? args[0] : undefined;
  335. }
  336. const hookInfo = {
  337. name: hookName,
  338. currentIndex: 0
  339. };
  340. hooksStore.__current.push(hookInfo);
  341. while (hookInfo.currentIndex < handlers.length) {
  342. const handler = handlers[hookInfo.currentIndex];
  343. const result = handler.callback.apply(null, args);
  344. if (returnFirstArg) {
  345. args[0] = result;
  346. }
  347. hookInfo.currentIndex++;
  348. }
  349. hooksStore.__current.pop();
  350. if (returnFirstArg) {
  351. return args[0];
  352. }
  353. };
  354. }
  355. /* harmony default export */ var build_module_createRunHook = (createRunHook);
  356. ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/createCurrentHook.js
  357. /**
  358. * Returns a function which, when invoked, will return the name of the
  359. * currently running hook, or `null` if no hook of the given type is currently
  360. * running.
  361. *
  362. * @param {import('.').Hooks} hooks Hooks instance.
  363. * @param {import('.').StoreKey} storeKey
  364. *
  365. * @return {() => string | null} Function that returns the current hook name or null.
  366. */
  367. function createCurrentHook(hooks, storeKey) {
  368. return function currentHook() {
  369. var _hooksStore$__current, _hooksStore$__current2;
  370. const hooksStore = hooks[storeKey];
  371. return (_hooksStore$__current = (_hooksStore$__current2 = hooksStore.__current[hooksStore.__current.length - 1]) === null || _hooksStore$__current2 === void 0 ? void 0 : _hooksStore$__current2.name) !== null && _hooksStore$__current !== void 0 ? _hooksStore$__current : null;
  372. };
  373. }
  374. /* harmony default export */ var build_module_createCurrentHook = (createCurrentHook);
  375. ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/createDoingHook.js
  376. /**
  377. * @callback DoingHook
  378. * Returns whether a hook is currently being executed.
  379. *
  380. * @param {string} [hookName] The name of the hook to check for. If
  381. * omitted, will check for any hook being executed.
  382. *
  383. * @return {boolean} Whether the hook is being executed.
  384. */
  385. /**
  386. * Returns a function which, when invoked, will return whether a hook is
  387. * currently being executed.
  388. *
  389. * @param {import('.').Hooks} hooks Hooks instance.
  390. * @param {import('.').StoreKey} storeKey
  391. *
  392. * @return {DoingHook} Function that returns whether a hook is currently
  393. * being executed.
  394. */
  395. function createDoingHook(hooks, storeKey) {
  396. return function doingHook(hookName) {
  397. const hooksStore = hooks[storeKey]; // If the hookName was not passed, check for any current hook.
  398. if ('undefined' === typeof hookName) {
  399. return 'undefined' !== typeof hooksStore.__current[0];
  400. } // Return the __current hook.
  401. return hooksStore.__current[0] ? hookName === hooksStore.__current[0].name : false;
  402. };
  403. }
  404. /* harmony default export */ var build_module_createDoingHook = (createDoingHook);
  405. ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/createDidHook.js
  406. /**
  407. * Internal dependencies
  408. */
  409. /**
  410. * @callback DidHook
  411. *
  412. * Returns the number of times an action has been fired.
  413. *
  414. * @param {string} hookName The hook name to check.
  415. *
  416. * @return {number | undefined} The number of times the hook has run.
  417. */
  418. /**
  419. * Returns a function which, when invoked, will return the number of times a
  420. * hook has been called.
  421. *
  422. * @param {import('.').Hooks} hooks Hooks instance.
  423. * @param {import('.').StoreKey} storeKey
  424. *
  425. * @return {DidHook} Function that returns a hook's call count.
  426. */
  427. function createDidHook(hooks, storeKey) {
  428. return function didHook(hookName) {
  429. const hooksStore = hooks[storeKey];
  430. if (!build_module_validateHookName(hookName)) {
  431. return;
  432. }
  433. return hooksStore[hookName] && hooksStore[hookName].runs ? hooksStore[hookName].runs : 0;
  434. };
  435. }
  436. /* harmony default export */ var build_module_createDidHook = (createDidHook);
  437. ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/createHooks.js
  438. /**
  439. * Internal dependencies
  440. */
  441. /**
  442. * Internal class for constructing hooks. Use `createHooks()` function
  443. *
  444. * Note, it is necessary to expose this class to make its type public.
  445. *
  446. * @private
  447. */
  448. class _Hooks {
  449. constructor() {
  450. /** @type {import('.').Store} actions */
  451. this.actions = Object.create(null);
  452. this.actions.__current = [];
  453. /** @type {import('.').Store} filters */
  454. this.filters = Object.create(null);
  455. this.filters.__current = [];
  456. this.addAction = build_module_createAddHook(this, 'actions');
  457. this.addFilter = build_module_createAddHook(this, 'filters');
  458. this.removeAction = build_module_createRemoveHook(this, 'actions');
  459. this.removeFilter = build_module_createRemoveHook(this, 'filters');
  460. this.hasAction = build_module_createHasHook(this, 'actions');
  461. this.hasFilter = build_module_createHasHook(this, 'filters');
  462. this.removeAllActions = build_module_createRemoveHook(this, 'actions', true);
  463. this.removeAllFilters = build_module_createRemoveHook(this, 'filters', true);
  464. this.doAction = build_module_createRunHook(this, 'actions');
  465. this.applyFilters = build_module_createRunHook(this, 'filters', true);
  466. this.currentAction = build_module_createCurrentHook(this, 'actions');
  467. this.currentFilter = build_module_createCurrentHook(this, 'filters');
  468. this.doingAction = build_module_createDoingHook(this, 'actions');
  469. this.doingFilter = build_module_createDoingHook(this, 'filters');
  470. this.didAction = build_module_createDidHook(this, 'actions');
  471. this.didFilter = build_module_createDidHook(this, 'filters');
  472. }
  473. }
  474. /** @typedef {_Hooks} Hooks */
  475. /**
  476. * Returns an instance of the hooks object.
  477. *
  478. * @return {Hooks} A Hooks instance.
  479. */
  480. function createHooks() {
  481. return new _Hooks();
  482. }
  483. /* harmony default export */ var build_module_createHooks = (createHooks);
  484. ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/index.js
  485. /**
  486. * Internal dependencies
  487. */
  488. /** @typedef {(...args: any[])=>any} Callback */
  489. /**
  490. * @typedef Handler
  491. * @property {Callback} callback The callback
  492. * @property {string} namespace The namespace
  493. * @property {number} priority The namespace
  494. */
  495. /**
  496. * @typedef Hook
  497. * @property {Handler[]} handlers Array of handlers
  498. * @property {number} runs Run counter
  499. */
  500. /**
  501. * @typedef Current
  502. * @property {string} name Hook name
  503. * @property {number} currentIndex The index
  504. */
  505. /**
  506. * @typedef {Record<string, Hook> & {__current: Current[]}} Store
  507. */
  508. /**
  509. * @typedef {'actions' | 'filters'} StoreKey
  510. */
  511. /**
  512. * @typedef {import('./createHooks').Hooks} Hooks
  513. */
  514. const defaultHooks = build_module_createHooks();
  515. const {
  516. addAction,
  517. addFilter,
  518. removeAction,
  519. removeFilter,
  520. hasAction,
  521. hasFilter,
  522. removeAllActions,
  523. removeAllFilters,
  524. doAction,
  525. applyFilters,
  526. currentAction,
  527. currentFilter,
  528. doingAction,
  529. doingFilter,
  530. didAction,
  531. didFilter,
  532. actions,
  533. filters
  534. } = defaultHooks;
  535. (window.wp = window.wp || {}).hooks = __webpack_exports__;
  536. /******/ })()
  537. ;