[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/dist/ -> hooks.js (source)

   1  (function() {
   2  "use strict";
   3  var wp;
   4  (wp ||= {}).hooks = (() => {
   5    var __defProp = Object.defineProperty;
   6    var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
   7    var __getOwnPropNames = Object.getOwnPropertyNames;
   8    var __hasOwnProp = Object.prototype.hasOwnProperty;
   9    var __export = (target, all) => {
  10      for (var name in all)
  11        __defProp(target, name, { get: all[name], enumerable: true });
  12    };
  13    var __copyProps = (to, from, except, desc) => {
  14      if (from && typeof from === "object" || typeof from === "function") {
  15        for (let key of __getOwnPropNames(from))
  16          if (!__hasOwnProp.call(to, key) && key !== except)
  17            __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  18      }
  19      return to;
  20    };
  21    var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  22  
  23    // packages/hooks/build-module/index.mjs
  24    var index_exports = {};
  25    __export(index_exports, {
  26      actions: () => actions,
  27      addAction: () => addAction,
  28      addFilter: () => addFilter,
  29      applyFilters: () => applyFilters,
  30      applyFiltersAsync: () => applyFiltersAsync,
  31      createHooks: () => createHooks_default,
  32      currentAction: () => currentAction,
  33      currentFilter: () => currentFilter,
  34      defaultHooks: () => defaultHooks,
  35      didAction: () => didAction,
  36      didFilter: () => didFilter,
  37      doAction: () => doAction,
  38      doActionAsync: () => doActionAsync,
  39      doingAction: () => doingAction,
  40      doingFilter: () => doingFilter,
  41      filters: () => filters,
  42      hasAction: () => hasAction,
  43      hasFilter: () => hasFilter,
  44      removeAction: () => removeAction,
  45      removeAllActions: () => removeAllActions,
  46      removeAllFilters: () => removeAllFilters,
  47      removeFilter: () => removeFilter
  48    });
  49  
  50    // packages/hooks/build-module/validateNamespace.mjs
  51    function validateNamespace(namespace) {
  52      if ("string" !== typeof namespace || "" === namespace) {
  53        console.error("The namespace must be a non-empty string.");
  54        return false;
  55      }
  56      if (!/^[a-zA-Z][a-zA-Z0-9_.\-\/]*$/.test(namespace)) {
  57        console.error(
  58          "The namespace can only contain numbers, letters, dashes, periods, underscores and slashes."
  59        );
  60        return false;
  61      }
  62      return true;
  63    }
  64    var validateNamespace_default = validateNamespace;
  65  
  66    // packages/hooks/build-module/validateHookName.mjs
  67    function validateHookName(hookName) {
  68      if ("string" !== typeof hookName || "" === hookName) {
  69        console.error("The hook name must be a non-empty string.");
  70        return false;
  71      }
  72      if (/^__/.test(hookName)) {
  73        console.error("The hook name cannot begin with `__`.");
  74        return false;
  75      }
  76      if (!/^[a-zA-Z][a-zA-Z0-9_.-]*$/.test(hookName)) {
  77        console.error(
  78          "The hook name can only contain numbers, letters, dashes, periods and underscores."
  79        );
  80        return false;
  81      }
  82      return true;
  83    }
  84    var validateHookName_default = validateHookName;
  85  
  86    // packages/hooks/build-module/createAddHook.mjs
  87    function createAddHook(hooks, storeKey) {
  88      return function addHook(hookName, namespace, callback, priority = 10) {
  89        const hooksStore = hooks[storeKey];
  90        if (!validateHookName_default(hookName)) {
  91          return;
  92        }
  93        if (!validateNamespace_default(namespace)) {
  94          return;
  95        }
  96        if ("function" !== typeof callback) {
  97          console.error("The hook callback must be a function.");
  98          return;
  99        }
 100        if ("number" !== typeof priority) {
 101          console.error(
 102            "If specified, the hook priority must be a number."
 103          );
 104          return;
 105        }
 106        const handler = { callback, priority, namespace };
 107        if (hooksStore[hookName]) {
 108          const handlers = hooksStore[hookName].handlers;
 109          let i;
 110          for (i = handlers.length; i > 0; i--) {
 111            if (priority >= handlers[i - 1].priority) {
 112              break;
 113            }
 114          }
 115          if (i === handlers.length) {
 116            handlers[i] = handler;
 117          } else {
 118            handlers.splice(i, 0, handler);
 119          }
 120          hooksStore.__current.forEach((hookInfo) => {
 121            if (hookInfo.name === hookName && hookInfo.currentIndex >= i) {
 122              hookInfo.currentIndex++;
 123            }
 124          });
 125        } else {
 126          hooksStore[hookName] = {
 127            handlers: [handler],
 128            runs: 0
 129          };
 130        }
 131        if (hookName !== "hookAdded") {
 132          hooks.doAction(
 133            "hookAdded",
 134            hookName,
 135            namespace,
 136            callback,
 137            priority
 138          );
 139        }
 140      };
 141    }
 142    var createAddHook_default = createAddHook;
 143  
 144    // packages/hooks/build-module/createRemoveHook.mjs
 145    function createRemoveHook(hooks, storeKey, removeAll = false) {
 146      return function removeHook(hookName, namespace) {
 147        const hooksStore = hooks[storeKey];
 148        if (!validateHookName_default(hookName)) {
 149          return;
 150        }
 151        if (!removeAll && !validateNamespace_default(namespace)) {
 152          return;
 153        }
 154        if (!hooksStore[hookName]) {
 155          return 0;
 156        }
 157        let handlersRemoved = 0;
 158        if (removeAll) {
 159          handlersRemoved = hooksStore[hookName].handlers.length;
 160          hooksStore[hookName] = {
 161            runs: hooksStore[hookName].runs,
 162            handlers: []
 163          };
 164        } else {
 165          const handlers = hooksStore[hookName].handlers;
 166          for (let i = handlers.length - 1; i >= 0; i--) {
 167            if (handlers[i].namespace === namespace) {
 168              handlers.splice(i, 1);
 169              handlersRemoved++;
 170              hooksStore.__current.forEach((hookInfo) => {
 171                if (hookInfo.name === hookName && hookInfo.currentIndex >= i) {
 172                  hookInfo.currentIndex--;
 173                }
 174              });
 175            }
 176          }
 177        }
 178        if (hookName !== "hookRemoved") {
 179          hooks.doAction("hookRemoved", hookName, namespace);
 180        }
 181        return handlersRemoved;
 182      };
 183    }
 184    var createRemoveHook_default = createRemoveHook;
 185  
 186    // packages/hooks/build-module/createHasHook.mjs
 187    function createHasHook(hooks, storeKey) {
 188      return function hasHook(hookName, namespace) {
 189        const hooksStore = hooks[storeKey];
 190        if ("undefined" !== typeof namespace) {
 191          return hookName in hooksStore && hooksStore[hookName].handlers.some(
 192            (hook) => hook.namespace === namespace
 193          );
 194        }
 195        return hookName in hooksStore;
 196      };
 197    }
 198    var createHasHook_default = createHasHook;
 199  
 200    // packages/hooks/build-module/createRunHook.mjs
 201    function createRunHook(hooks, storeKey, returnFirstArg, async) {
 202      return function runHook(hookName, ...args) {
 203        const hooksStore = hooks[storeKey];
 204        if (!hooksStore[hookName]) {
 205          hooksStore[hookName] = {
 206            handlers: [],
 207            runs: 0
 208          };
 209        }
 210        hooksStore[hookName].runs++;
 211        const handlers = hooksStore[hookName].handlers;
 212        if (true) {
 213          if ("hookAdded" !== hookName && hooksStore.all) {
 214            handlers.push(...hooksStore.all.handlers);
 215          }
 216        }
 217        if (!handlers || !handlers.length) {
 218          return returnFirstArg ? args[0] : void 0;
 219        }
 220        const hookInfo = {
 221          name: hookName,
 222          currentIndex: 0
 223        };
 224        async function asyncRunner() {
 225          try {
 226            hooksStore.__current.add(hookInfo);
 227            let result = returnFirstArg ? args[0] : void 0;
 228            while (hookInfo.currentIndex < handlers.length) {
 229              const handler = handlers[hookInfo.currentIndex];
 230              result = await handler.callback.apply(null, args);
 231              if (returnFirstArg) {
 232                args[0] = result;
 233              }
 234              hookInfo.currentIndex++;
 235            }
 236            return returnFirstArg ? result : void 0;
 237          } finally {
 238            hooksStore.__current.delete(hookInfo);
 239          }
 240        }
 241        function syncRunner() {
 242          try {
 243            hooksStore.__current.add(hookInfo);
 244            let result = returnFirstArg ? args[0] : void 0;
 245            while (hookInfo.currentIndex < handlers.length) {
 246              const handler = handlers[hookInfo.currentIndex];
 247              result = handler.callback.apply(null, args);
 248              if (returnFirstArg) {
 249                args[0] = result;
 250              }
 251              hookInfo.currentIndex++;
 252            }
 253            return returnFirstArg ? result : void 0;
 254          } finally {
 255            hooksStore.__current.delete(hookInfo);
 256          }
 257        }
 258        return (async ? asyncRunner : syncRunner)();
 259      };
 260    }
 261    var createRunHook_default = createRunHook;
 262  
 263    // packages/hooks/build-module/createCurrentHook.mjs
 264    function createCurrentHook(hooks, storeKey) {
 265      return function currentHook() {
 266        const hooksStore = hooks[storeKey];
 267        const currentArray = Array.from(hooksStore.__current);
 268        return currentArray.at(-1)?.name ?? null;
 269      };
 270    }
 271    var createCurrentHook_default = createCurrentHook;
 272  
 273    // packages/hooks/build-module/createDoingHook.mjs
 274    function createDoingHook(hooks, storeKey) {
 275      return function doingHook(hookName) {
 276        const hooksStore = hooks[storeKey];
 277        if ("undefined" === typeof hookName) {
 278          return hooksStore.__current.size > 0;
 279        }
 280        return Array.from(hooksStore.__current).some(
 281          (hook) => hook.name === hookName
 282        );
 283      };
 284    }
 285    var createDoingHook_default = createDoingHook;
 286  
 287    // packages/hooks/build-module/createDidHook.mjs
 288    function createDidHook(hooks, storeKey) {
 289      return function didHook(hookName) {
 290        const hooksStore = hooks[storeKey];
 291        if (!validateHookName_default(hookName)) {
 292          return;
 293        }
 294        return hooksStore[hookName] && hooksStore[hookName].runs ? hooksStore[hookName].runs : 0;
 295      };
 296    }
 297    var createDidHook_default = createDidHook;
 298  
 299    // packages/hooks/build-module/createHooks.mjs
 300    var _Hooks = class {
 301      actions;
 302      filters;
 303      addAction;
 304      addFilter;
 305      removeAction;
 306      removeFilter;
 307      hasAction;
 308      hasFilter;
 309      removeAllActions;
 310      removeAllFilters;
 311      doAction;
 312      doActionAsync;
 313      applyFilters;
 314      applyFiltersAsync;
 315      currentAction;
 316      currentFilter;
 317      doingAction;
 318      doingFilter;
 319      didAction;
 320      didFilter;
 321      constructor() {
 322        this.actions = /* @__PURE__ */ Object.create(null);
 323        this.actions.__current = /* @__PURE__ */ new Set();
 324        this.filters = /* @__PURE__ */ Object.create(null);
 325        this.filters.__current = /* @__PURE__ */ new Set();
 326        this.addAction = createAddHook_default(this, "actions");
 327        this.addFilter = createAddHook_default(this, "filters");
 328        this.removeAction = createRemoveHook_default(this, "actions");
 329        this.removeFilter = createRemoveHook_default(this, "filters");
 330        this.hasAction = createHasHook_default(this, "actions");
 331        this.hasFilter = createHasHook_default(this, "filters");
 332        this.removeAllActions = createRemoveHook_default(this, "actions", true);
 333        this.removeAllFilters = createRemoveHook_default(this, "filters", true);
 334        this.doAction = createRunHook_default(this, "actions", false, false);
 335        this.doActionAsync = createRunHook_default(this, "actions", false, true);
 336        this.applyFilters = createRunHook_default(this, "filters", true, false);
 337        this.applyFiltersAsync = createRunHook_default(this, "filters", true, true);
 338        this.currentAction = createCurrentHook_default(this, "actions");
 339        this.currentFilter = createCurrentHook_default(this, "filters");
 340        this.doingAction = createDoingHook_default(this, "actions");
 341        this.doingFilter = createDoingHook_default(this, "filters");
 342        this.didAction = createDidHook_default(this, "actions");
 343        this.didFilter = createDidHook_default(this, "filters");
 344      }
 345    };
 346    function createHooks() {
 347      return new _Hooks();
 348    }
 349    var createHooks_default = createHooks;
 350  
 351    // packages/hooks/build-module/index.mjs
 352    var defaultHooks = createHooks_default();
 353    var {
 354      addAction,
 355      addFilter,
 356      removeAction,
 357      removeFilter,
 358      hasAction,
 359      hasFilter,
 360      removeAllActions,
 361      removeAllFilters,
 362      doAction,
 363      doActionAsync,
 364      applyFilters,
 365      applyFiltersAsync,
 366      currentAction,
 367      currentFilter,
 368      doingAction,
 369      doingFilter,
 370      didAction,
 371      didFilter,
 372      actions,
 373      filters
 374    } = defaultHooks;
 375    return __toCommonJS(index_exports);
 376  })();
 377  (window.wp ||= {}).hooks = wp.hooks;
 378  })();


Generated : Tue Aug 18 08:20:25 2026 Cross-referenced by PHPXref