[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> plugin.php (source)

   1  <?php
   2  /**
   3   * The plugin API is located in this file, which allows for creating actions
   4   * and filters and hooking functions, and methods. The functions or methods will
   5   * then be run when the action or filter is called.
   6   *
   7   * The API callback examples reference functions, but can be methods of classes.
   8   * To hook methods, you'll need to pass an array one of two ways.
   9   *
  10   * Any of the syntaxes explained in the PHP documentation for the
  11   * {@link https://www.php.net/manual/en/language.pseudo-types.php#language.types.callback 'callback'}
  12   * type are valid.
  13   *
  14   * Also see the {@link https://developer.wordpress.org/plugins/ Plugin API} for
  15   * more information and examples on how to use a lot of these functions.
  16   *
  17   * This file should have no external dependencies.
  18   *
  19   * @package WordPress
  20   * @subpackage Plugin
  21   * @since 1.5.0
  22   */
  23  
  24  // Initialize the filter globals.
  25  require  __DIR__ . '/class-wp-hook.php';
  26  require  __DIR__ . '/class-wp-filter-sentinel.php';
  27  
  28  /** @var WP_Hook[] $wp_filter */
  29  global $wp_filter;
  30  
  31  /** @var int[] $wp_actions */
  32  global $wp_actions;
  33  
  34  /** @var int[] $wp_filters */
  35  global $wp_filters;
  36  
  37  /** @var string[] $wp_current_filter */
  38  global $wp_current_filter;
  39  
  40  if ( $wp_filter ) {
  41      $wp_filter = WP_Hook::build_preinitialized_hooks( $wp_filter );
  42  } else {
  43      $wp_filter = array();
  44  }
  45  
  46  if ( ! isset( $wp_actions ) ) {
  47      $wp_actions = array();
  48  }
  49  
  50  if ( ! isset( $wp_filters ) ) {
  51      $wp_filters = array();
  52  }
  53  
  54  if ( ! isset( $wp_current_filter ) ) {
  55      $wp_current_filter = array();
  56  }
  57  
  58  /**
  59   * Adds a callback function to a filter hook.
  60   *
  61   * WordPress offers filter hooks to allow plugins to modify
  62   * various types of internal data at runtime.
  63   *
  64   * A plugin can modify data by binding a callback to a filter hook. When the filter
  65   * is later applied, each bound callback is run in order of priority, and given
  66   * the opportunity to modify a value by returning a new value.
  67   *
  68   * The following example shows how a callback function is bound to a filter hook.
  69   *
  70   * Note that `$example` is passed to the callback, (maybe) modified, then returned:
  71   *
  72   *     function example_callback( $example ) {
  73   *         // Maybe modify $example in some way.
  74   *         return $example;
  75   *     }
  76   *     add_filter( 'example_filter', 'example_callback' );
  77   *
  78   * Bound callbacks can accept from none to the total number of arguments passed as parameters
  79   * in the corresponding apply_filters() call.
  80   *
  81   * In other words, if an apply_filters() call passes four total arguments, callbacks bound to
  82   * it can accept none (the same as 1) of the arguments or up to four. The important part is that
  83   * the `$accepted_args` value must reflect the number of arguments the bound callback *actually*
  84   * opted to accept. If no arguments were accepted by the callback that is considered to be the
  85   * same as accepting 1 argument. For example:
  86   *
  87   *     // Filter call.
  88   *     $value = apply_filters( 'hook', $value, $arg2, $arg3 );
  89   *
  90   *     // Accepting zero/one arguments.
  91   *     function example_callback() {
  92   *         ...
  93   *         return 'some value';
  94   *     }
  95   *     add_filter( 'hook', 'example_callback' ); // Where $priority is default 10, $accepted_args is default 1.
  96   *
  97   *     // Accepting two arguments (three possible).
  98   *     function example_callback( $value, $arg2 ) {
  99   *         ...
 100   *         return $maybe_modified_value;
 101   *     }
 102   *     add_filter( 'hook', 'example_callback', 10, 2 ); // Where $priority is 10, $accepted_args is 2.
 103   *
 104   * *Note:* The function will return true whether or not the callback is valid.
 105   * It is up to you to take care. This is done for optimization purposes, so
 106   * everything is as quick as possible.
 107   *
 108   * @since 0.71
 109   *
 110   * @global WP_Hook[] $wp_filter A multidimensional array of all hooks and the callbacks hooked to them.
 111   *
 112   * @param string   $hook_name     The name of the filter to add the callback to.
 113   * @param callable $callback      The callback to be run when the filter is applied.
 114   * @param int      $priority      Optional. Used to specify the order in which the functions
 115   *                                associated with a particular filter are executed.
 116   *                                Lower numbers correspond with earlier execution,
 117   *                                and functions with the same priority are executed
 118   *                                in the order in which they were added to the filter. Default 10.
 119   * @param int      $accepted_args Optional. The number of arguments the function accepts. Default 1.
 120   * @return true Always returns true.
 121   */
 122  function add_filter( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) {
 123      global $wp_filter;
 124  
 125      if ( ! isset( $wp_filter[ $hook_name ] ) ) {
 126          $wp_filter[ $hook_name ] = new WP_Hook();
 127      }
 128  
 129      $wp_filter[ $hook_name ]->add_filter( $hook_name, $callback, $priority, $accepted_args );
 130  
 131      return true;
 132  }
 133  
 134  /**
 135   * Calls the callback functions that have been added to a filter hook.
 136   *
 137   * This function invokes all functions attached to filter hook `$hook_name`.
 138   * It is possible to create new filter hooks by simply calling this function,
 139   * specifying the name of the new hook using the `$hook_name` parameter.
 140   *
 141   * The function also allows for multiple additional arguments to be passed to hooks.
 142   *
 143   * Example usage:
 144   *
 145   *     // The filter callback function.
 146   *     function example_callback( $string, $arg1, $arg2 ) {
 147   *         // (maybe) modify $string.
 148   *         return $string;
 149   *     }
 150   *     add_filter( 'example_filter', 'example_callback', 10, 3 );
 151   *
 152   *     /*
 153   *      * Apply the filters by calling the 'example_callback()' function
 154   *      * that's hooked onto `example_filter` above.
 155   *      *
 156   *      * - 'example_filter' is the filter hook.
 157   *      * - 'filter me' is the value being filtered.
 158   *      * - $arg1 and $arg2 are the additional arguments passed to the callback.
 159   *     $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );
 160   *
 161   * @since 0.71
 162   * @since 6.0.0 Formalized the existing and already documented `...$args` parameter
 163   *              by adding it to the function signature.
 164   *
 165   * @global WP_Hook[] $wp_filter         Stores all of the filters and actions.
 166   * @global int[]     $wp_filters        Stores the number of times each filter was triggered.
 167   * @global string[]  $wp_current_filter Stores the list of current filters with the current one last.
 168   *
 169   * @param string $hook_name The name of the filter hook.
 170   * @param mixed  $value     The value to filter.
 171   * @param mixed  ...$args   Optional. Additional parameters to pass to the callback functions.
 172   * @no-named-arguments
 173   * @return mixed The filtered value after all hooked functions are applied to it.
 174   */
 175  function apply_filters( $hook_name, $value, ...$args ) {
 176      global $wp_filter, $wp_filters, $wp_current_filter;
 177  
 178      if ( ! isset( $wp_filters[ $hook_name ] ) ) {
 179          $wp_filters[ $hook_name ] = 1;
 180      } else {
 181          ++$wp_filters[ $hook_name ];
 182      }
 183  
 184      // Do 'all' actions first.
 185      if ( isset( $wp_filter['all'] ) ) {
 186          $wp_current_filter[] = $hook_name;
 187  
 188          $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
 189          _wp_call_all_hook( $all_args );
 190      }
 191  
 192      if ( ! isset( $wp_filter[ $hook_name ] ) ) {
 193          if ( isset( $wp_filter['all'] ) ) {
 194              array_pop( $wp_current_filter );
 195          }
 196  
 197          return $value;
 198      }
 199  
 200      if ( ! isset( $wp_filter['all'] ) ) {
 201          $wp_current_filter[] = $hook_name;
 202      }
 203  
 204      // Pass the value to WP_Hook.
 205      array_unshift( $args, $value );
 206  
 207      $filtered = $wp_filter[ $hook_name ]->apply_filters( $value, $args );
 208  
 209      array_pop( $wp_current_filter );
 210  
 211      return $filtered;
 212  }
 213  
 214  /**
 215   * Calls the callback functions that have been added to a filter hook, specifying arguments in an array.
 216   *
 217   * @since 3.0.0
 218   *
 219   * @see apply_filters() This function is identical, but the arguments passed to the
 220   *                      functions hooked to `$hook_name` are supplied using an array.
 221   *
 222   * @global WP_Hook[] $wp_filter         Stores all of the filters and actions.
 223   * @global int[]     $wp_filters        Stores the number of times each filter was triggered.
 224   * @global string[]  $wp_current_filter Stores the list of current filters with the current one last.
 225   *
 226   * @param string                $hook_name The name of the filter hook.
 227   * @param non-empty-list<mixed> $args      The arguments supplied to the functions hooked to `$hook_name`.
 228   * @return mixed The filtered value after all hooked functions are applied to it.
 229   */
 230  function apply_filters_ref_array( $hook_name, $args ) {
 231      global $wp_filter, $wp_filters, $wp_current_filter;
 232  
 233      if ( ! isset( $wp_filters[ $hook_name ] ) ) {
 234          $wp_filters[ $hook_name ] = 1;
 235      } else {
 236          ++$wp_filters[ $hook_name ];
 237      }
 238  
 239      // Do 'all' actions first.
 240      if ( isset( $wp_filter['all'] ) ) {
 241          $wp_current_filter[] = $hook_name;
 242          $all_args            = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
 243          _wp_call_all_hook( $all_args );
 244      }
 245  
 246      if ( ! isset( $wp_filter[ $hook_name ] ) ) {
 247          if ( isset( $wp_filter['all'] ) ) {
 248              array_pop( $wp_current_filter );
 249          }
 250  
 251          return $args[0];
 252      }
 253  
 254      if ( ! isset( $wp_filter['all'] ) ) {
 255          $wp_current_filter[] = $hook_name;
 256      }
 257  
 258      $filtered = $wp_filter[ $hook_name ]->apply_filters( $args[0], $args );
 259  
 260      array_pop( $wp_current_filter );
 261  
 262      return $filtered;
 263  }
 264  
 265  /**
 266   * Checks if any filter has been registered for a hook.
 267   *
 268   * When using the `$callback` argument, this function may return a non-boolean value
 269   * that evaluates to false (e.g. 0), so use the `===` operator for testing the return value.
 270   *
 271   * @since 2.5.0
 272   * @since 6.9.0 Added the `$priority` parameter.
 273   *
 274   * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
 275   *
 276   * @param string                      $hook_name The name of the filter hook.
 277   * @param callable|string|array|false $callback  Optional. The callback to check for.
 278   *                                               This function can be called unconditionally to speculatively check
 279   *                                               a callback that may or may not exist. Default false.
 280   * @param int|false                   $priority  Optional. The specific priority at which to check for the callback.
 281   *                                               Default false.
 282   * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has
 283   *                  anything registered. When checking a specific function, the priority
 284   *                  of that hook is returned, or false if the function is not attached.
 285   *                  If `$callback` and `$priority` are both provided, a boolean is returned
 286   *                  for whether the specific function is registered at that priority.
 287   * @phpstan-param Maybe_Callable|false $callback
 288   * @phpstan-return (
 289   *     $callback is false
 290   *         ? bool
 291   *         : ( $priority is int
 292   *             ? bool
 293   *             : false|int )
 294   * )
 295   */
 296  function has_filter( $hook_name, $callback = false, $priority = false ) {
 297      global $wp_filter;
 298  
 299      if ( ! isset( $wp_filter[ $hook_name ] ) ) {
 300          return false;
 301      }
 302  
 303      return $wp_filter[ $hook_name ]->has_filter( $hook_name, $callback, $priority );
 304  }
 305  
 306  /**
 307   * Removes a callback function from a filter hook.
 308   *
 309   * This can be used to remove default functions attached to a specific filter
 310   * hook and possibly replace them with a substitute.
 311   *
 312   * To remove a hook, the `$callback` and `$priority` arguments must match
 313   * when the hook was added. This goes for both filters and actions. No warning
 314   * will be given on removal failure.
 315   *
 316   * @since 1.2.0
 317   *
 318   * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
 319   *
 320   * @param string                $hook_name The filter hook to which the function to be removed is hooked.
 321   * @param callable|string|array $callback  The callback to be removed from running when the filter is applied.
 322   *                                         This function can be called unconditionally to speculatively remove
 323   *                                         a callback that may or may not exist.
 324   * @param int                   $priority  Optional. The exact priority used when adding the original
 325   *                                         filter callback. Default 10.
 326   * @return bool Whether the function existed before it was removed.
 327   * @phpstan-param Maybe_Callable $callback
 328   */
 329  function remove_filter( $hook_name, $callback, $priority = 10 ) {
 330      global $wp_filter;
 331  
 332      $r = false;
 333  
 334      if ( isset( $wp_filter[ $hook_name ] ) ) {
 335          $r = $wp_filter[ $hook_name ]->remove_filter( $hook_name, $callback, $priority );
 336  
 337          if ( ! $wp_filter[ $hook_name ]->callbacks ) {
 338              unset( $wp_filter[ $hook_name ] );
 339          }
 340      }
 341  
 342      return $r;
 343  }
 344  
 345  /**
 346   * Removes all of the callback functions from a filter hook.
 347   *
 348   * @since 2.7.0
 349   *
 350   * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
 351   *
 352   * @param string    $hook_name The filter to remove callbacks from.
 353   * @param int|false $priority  Optional. The priority number to remove them from.
 354   *                             Default false.
 355   * @return true Always returns true.
 356   */
 357  function remove_all_filters( $hook_name, $priority = false ) {
 358      global $wp_filter;
 359  
 360      if ( isset( $wp_filter[ $hook_name ] ) ) {
 361          $wp_filter[ $hook_name ]->remove_all_filters( $priority );
 362  
 363          if ( ! $wp_filter[ $hook_name ]->has_filters() ) {
 364              unset( $wp_filter[ $hook_name ] );
 365          }
 366      }
 367  
 368      return true;
 369  }
 370  
 371  /**
 372   * Retrieves the name of the current filter hook.
 373   *
 374   * @since 2.5.0
 375   *
 376   * @global string[] $wp_current_filter Stores the list of current filters with the current one last
 377   *
 378   * @return string|false Hook name of the current filter, false if no filter is running.
 379   */
 380  function current_filter() {
 381      global $wp_current_filter;
 382  
 383      return end( $wp_current_filter );
 384  }
 385  
 386  /**
 387   * Returns whether or not a filter hook is currently being processed.
 388   *
 389   * The function current_filter() only returns the most recent filter being executed.
 390   * did_filter() returns the number of times a filter has been applied during
 391   * the current request.
 392   *
 393   * This function allows detection for any filter currently being executed
 394   * (regardless of whether it's the most recent filter to fire, in the case of
 395   * hooks called from hook callbacks) to be verified.
 396   *
 397   * @since 3.9.0
 398   *
 399   * @see current_filter()
 400   * @see did_filter()
 401   * @global string[] $wp_current_filter Current filter.
 402   *
 403   * @param string|null $hook_name Optional. Filter hook to check. Defaults to null,
 404   *                               which checks if any filter is currently being run.
 405   * @return bool Whether the filter is currently in the stack.
 406   */
 407  function doing_filter( $hook_name = null ) {
 408      global $wp_current_filter;
 409  
 410      if ( null === $hook_name ) {
 411          return ! empty( $wp_current_filter );
 412      }
 413  
 414      return in_array( $hook_name, $wp_current_filter, true );
 415  }
 416  
 417  /**
 418   * Retrieves the number of times a filter has been applied during the current request.
 419   *
 420   * @since 6.1.0
 421   *
 422   * @global int[] $wp_filters Stores the number of times each filter was triggered.
 423   *
 424   * @param string $hook_name The name of the filter hook.
 425   * @return int The number of times the filter hook has been applied.
 426   */
 427  function did_filter( $hook_name ) {
 428      global $wp_filters;
 429  
 430      if ( ! isset( $wp_filters[ $hook_name ] ) ) {
 431          return 0;
 432      }
 433  
 434      return $wp_filters[ $hook_name ];
 435  }
 436  
 437  /**
 438   * Adds a callback function to an action hook.
 439   *
 440   * Actions are the hooks that the WordPress core launches at specific points
 441   * during execution, or when specific events occur. Plugins can specify that
 442   * one or more of its PHP functions are executed at these points, using the
 443   * Action API.
 444   *
 445   * @since 1.2.0
 446   *
 447   * @param string   $hook_name       The name of the action to add the callback to.
 448   * @param callable $callback        The callback to be run when the action is called.
 449   * @param int      $priority        Optional. Used to specify the order in which the functions
 450   *                                  associated with a particular action are executed.
 451   *                                  Lower numbers correspond with earlier execution,
 452   *                                  and functions with the same priority are executed
 453   *                                  in the order in which they were added to the action. Default 10.
 454   * @param int      $accepted_args   Optional. The number of arguments the function accepts. Default 1.
 455   * @return true Always returns true.
 456   */
 457  function add_action( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) {
 458      return add_filter( $hook_name, $callback, $priority, $accepted_args );
 459  }
 460  
 461  /**
 462   * Calls the callback functions that have been added to an action hook.
 463   *
 464   * This function invokes all functions attached to action hook `$hook_name`.
 465   * It is possible to create new action hooks by simply calling this function,
 466   * specifying the name of the new hook using the `$hook_name` parameter.
 467   *
 468   * You can pass extra arguments to the hooks, much like you can with `apply_filters()`.
 469   *
 470   * Example usage:
 471   *
 472   *     // The action callback function.
 473   *     function example_callback( $arg1, $arg2 ) {
 474   *         // (maybe) do something with the args.
 475   *     }
 476   *     add_action( 'example_action', 'example_callback', 10, 2 );
 477   *
 478   *     /*
 479   *      * Trigger the actions by calling the 'example_callback()' function
 480   *      * that's hooked onto `example_action` above.
 481   *      *
 482   *      * - 'example_action' is the action hook.
 483   *      * - $arg1 and $arg2 are the additional arguments passed to the callback.
 484   *     do_action( 'example_action', $arg1, $arg2 );
 485   *
 486   * @since 1.2.0
 487   * @since 5.3.0 Formalized the existing and already documented `...$arg` parameter
 488   *              by adding it to the function signature.
 489   *
 490   * @global WP_Hook[] $wp_filter         Stores all of the filters and actions.
 491   * @global int[]     $wp_actions        Stores the number of times each action was triggered.
 492   * @global string[]  $wp_current_filter Stores the list of current filters with the current one last.
 493   *
 494   * @param string $hook_name The name of the action to be executed.
 495   * @param mixed  ...$arg    Optional. Additional arguments which are passed on to the
 496   *                          functions hooked to the action. Default empty.
 497   * @no-named-arguments
 498   */
 499  function do_action( $hook_name, ...$arg ) {
 500      global $wp_filter, $wp_actions, $wp_current_filter;
 501  
 502      if ( ! isset( $wp_actions[ $hook_name ] ) ) {
 503          $wp_actions[ $hook_name ] = 1;
 504      } else {
 505          ++$wp_actions[ $hook_name ];
 506      }
 507  
 508      // Do 'all' actions first.
 509      if ( isset( $wp_filter['all'] ) ) {
 510          $wp_current_filter[] = $hook_name;
 511          $all_args            = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
 512          _wp_call_all_hook( $all_args );
 513      }
 514  
 515      if ( ! isset( $wp_filter[ $hook_name ] ) ) {
 516          if ( isset( $wp_filter['all'] ) ) {
 517              array_pop( $wp_current_filter );
 518          }
 519  
 520          return;
 521      }
 522  
 523      if ( ! isset( $wp_filter['all'] ) ) {
 524          $wp_current_filter[] = $hook_name;
 525      }
 526  
 527      if ( empty( $arg ) ) {
 528          $arg[] = '';
 529      } elseif ( is_array( $arg[0] ) && 1 === count( $arg[0] ) && isset( $arg[0][0] ) && is_object( $arg[0][0] ) ) {
 530          // Backward compatibility for PHP4-style passing of `array( &$this )` as action `$arg`.
 531          $arg[0] = $arg[0][0];
 532      }
 533  
 534      $wp_filter[ $hook_name ]->do_action( $arg );
 535  
 536      array_pop( $wp_current_filter );
 537  }
 538  
 539  /**
 540   * Calls the callback functions that have been added to an action hook, specifying arguments in an array.
 541   *
 542   * @since 2.1.0
 543   *
 544   * @see do_action() This function is identical, but the arguments passed to the
 545   *                  functions hooked to `$hook_name` are supplied using an array.
 546   *
 547   * @global WP_Hook[] $wp_filter         Stores all of the filters and actions.
 548   * @global int[]     $wp_actions        Stores the number of times each action was triggered.
 549   * @global string[]  $wp_current_filter Stores the list of current filters with the current one last.
 550   *
 551   * @param string      $hook_name The name of the action to be executed.
 552   * @param list<mixed> $args      The arguments supplied to the functions hooked to `$hook_name`.
 553   */
 554  function do_action_ref_array( $hook_name, $args ) {
 555      global $wp_filter, $wp_actions, $wp_current_filter;
 556  
 557      if ( ! isset( $wp_actions[ $hook_name ] ) ) {
 558          $wp_actions[ $hook_name ] = 1;
 559      } else {
 560          ++$wp_actions[ $hook_name ];
 561      }
 562  
 563      // Do 'all' actions first.
 564      if ( isset( $wp_filter['all'] ) ) {
 565          $wp_current_filter[] = $hook_name;
 566          $all_args            = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
 567          _wp_call_all_hook( $all_args );
 568      }
 569  
 570      if ( ! isset( $wp_filter[ $hook_name ] ) ) {
 571          if ( isset( $wp_filter['all'] ) ) {
 572              array_pop( $wp_current_filter );
 573          }
 574  
 575          return;
 576      }
 577  
 578      if ( ! isset( $wp_filter['all'] ) ) {
 579          $wp_current_filter[] = $hook_name;
 580      }
 581  
 582      $wp_filter[ $hook_name ]->do_action( $args );
 583  
 584      array_pop( $wp_current_filter );
 585  }
 586  
 587  /**
 588   * Checks if any action has been registered for a hook.
 589   *
 590   * When using the `$callback` argument, this function may return a non-boolean value
 591   * that evaluates to false (e.g. 0), so use the `===` operator for testing the return value.
 592   *
 593   * @since 2.5.0
 594   * @since 6.9.0 Added the `$priority` parameter.
 595   *
 596   * @see has_filter() This function is an alias of has_filter().
 597   *
 598   * @param string                      $hook_name The name of the action hook.
 599   * @param callable|string|array|false $callback  Optional. The callback to check for.
 600   *                                               This function can be called unconditionally to speculatively check
 601   *                                               a callback that may or may not exist. Default false.
 602   * @param int|false                   $priority  Optional. The specific priority at which to check for the callback.
 603   *                                               Default false.
 604   * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has
 605   *                  anything registered. When checking a specific function, the priority
 606   *                  of that hook is returned, or false if the function is not attached.
 607   *                  If `$callback` and `$priority` are both provided, a boolean is returned
 608   *                  for whether the specific function is registered at that priority.
 609   * @phpstan-param Maybe_Callable|false $callback
 610   * @phpstan-return (
 611   *     $callback is false
 612   *         ? bool
 613   *         : ( $priority is int
 614   *             ? bool
 615   *             : false|int )
 616   * )
 617   */
 618  function has_action( $hook_name, $callback = false, $priority = false ) {
 619      return has_filter( $hook_name, $callback, $priority );
 620  }
 621  
 622  /**
 623   * Removes a callback function from an action hook.
 624   *
 625   * This can be used to remove default functions attached to a specific action
 626   * hook and possibly replace them with a substitute.
 627   *
 628   * To remove a hook, the `$callback` and `$priority` arguments must match
 629   * when the hook was added. This goes for both filters and actions. No warning
 630   * will be given on removal failure.
 631   *
 632   * @since 1.2.0
 633   *
 634   * @param string                $hook_name The action hook to which the function to be removed is hooked.
 635   * @param callable|string|array $callback  The name of the function which should be removed.
 636   *                                         This function can be called unconditionally to speculatively remove
 637   *                                         a callback that may or may not exist.
 638   * @param int                   $priority  Optional. The exact priority used when adding the original
 639   *                                         action callback. Default 10.
 640   * @return bool Whether the function is removed.
 641   * @phpstan-param Maybe_Callable $callback
 642   */
 643  function remove_action( $hook_name, $callback, $priority = 10 ) {
 644      return remove_filter( $hook_name, $callback, $priority );
 645  }
 646  
 647  /**
 648   * Removes all of the callback functions from an action hook.
 649   *
 650   * @since 2.7.0
 651   *
 652   * @param string    $hook_name The action to remove callbacks from.
 653   * @param int|false $priority  Optional. The priority number to remove them from.
 654   *                             Default false.
 655   * @return true Always returns true.
 656   */
 657  function remove_all_actions( $hook_name, $priority = false ) {
 658      return remove_all_filters( $hook_name, $priority );
 659  }
 660  
 661  /**
 662   * Retrieves the name of the current action hook.
 663   *
 664   * @since 3.9.0
 665   *
 666   * @return string|false Hook name of the current action, false if no action is running.
 667   */
 668  function current_action() {
 669      return current_filter();
 670  }
 671  
 672  /**
 673   * Returns whether or not an action hook is currently being processed.
 674   *
 675   * The function current_action() only returns the most recent action being executed.
 676   * did_action() returns the number of times an action has been fired during
 677   * the current request.
 678   *
 679   * This function allows detection for any action currently being executed
 680   * (regardless of whether it's the most recent action to fire, in the case of
 681   * hooks called from hook callbacks) to be verified.
 682   *
 683   * @since 3.9.0
 684   *
 685   * @see current_action()
 686   * @see did_action()
 687   *
 688   * @param string|null $hook_name Optional. Action hook to check. Defaults to null,
 689   *                               which checks if any action is currently being run.
 690   * @return bool Whether the action is currently in the stack.
 691   */
 692  function doing_action( $hook_name = null ) {
 693      return doing_filter( $hook_name );
 694  }
 695  
 696  /**
 697   * Retrieves the number of times an action has been fired during the current request.
 698   *
 699   * @since 2.1.0
 700   *
 701   * @global int[] $wp_actions Stores the number of times each action was triggered.
 702   *
 703   * @param string $hook_name The name of the action hook.
 704   * @return int The number of times the action hook has been fired.
 705   */
 706  function did_action( $hook_name ) {
 707      global $wp_actions;
 708  
 709      if ( ! isset( $wp_actions[ $hook_name ] ) ) {
 710          return 0;
 711      }
 712  
 713      return $wp_actions[ $hook_name ];
 714  }
 715  
 716  /**
 717   * Fires functions attached to a deprecated filter hook.
 718   *
 719   * When a filter hook is deprecated, the apply_filters() call is replaced with
 720   * apply_filters_deprecated(), which triggers a deprecation notice and then fires
 721   * the original filter hook.
 722   *
 723   * Note: the value and extra arguments passed to the original apply_filters() call
 724   * must be passed here to `$args` as an array. For example:
 725   *
 726   *     // Old filter.
 727   *     return apply_filters( 'wpdocs_filter', $value, $extra_arg );
 728   *
 729   *     // Deprecated.
 730   *     return apply_filters_deprecated( 'wpdocs_filter', array( $value, $extra_arg ), '4.9.0', 'wpdocs_new_filter' );
 731   *
 732   * @since 4.6.0
 733   *
 734   * @see _deprecated_hook()
 735   *
 736   * @param string                $hook_name   The name of the filter hook.
 737   * @param non-empty-list<mixed> $args        Array of additional function arguments to be passed to apply_filters().
 738   * @param string                $version     The version of WordPress that deprecated the hook.
 739   * @param string                $replacement Optional. The hook that should have been used. Default empty.
 740   * @param string                $message     Optional. A message regarding the change. Default empty.
 741   * @return mixed The filtered value after all hooked functions are applied to it.
 742   */
 743  function apply_filters_deprecated( $hook_name, $args, $version, $replacement = '', $message = '' ) {
 744      if ( ! has_filter( $hook_name ) ) {
 745          return $args[0];
 746      }
 747  
 748      _deprecated_hook( $hook_name, $version, $replacement, $message );
 749  
 750      return apply_filters_ref_array( $hook_name, $args );
 751  }
 752  
 753  /**
 754   * Fires functions attached to a deprecated action hook.
 755   *
 756   * When an action hook is deprecated, the do_action() call is replaced with
 757   * do_action_deprecated(), which triggers a deprecation notice and then fires
 758   * the original hook.
 759   *
 760   * @since 4.6.0
 761   *
 762   * @see _deprecated_hook()
 763   *
 764   * @param string      $hook_name   The name of the action hook.
 765   * @param list<mixed> $args        Array of additional function arguments to be passed to do_action().
 766   * @param string      $version     The version of WordPress that deprecated the hook.
 767   * @param string      $replacement Optional. The hook that should have been used. Default empty.
 768   * @param string      $message     Optional. A message regarding the change. Default empty.
 769   */
 770  function do_action_deprecated( $hook_name, $args, $version, $replacement = '', $message = '' ) {
 771      if ( ! has_action( $hook_name ) ) {
 772          return;
 773      }
 774  
 775      _deprecated_hook( $hook_name, $version, $replacement, $message );
 776  
 777      do_action_ref_array( $hook_name, $args );
 778  }
 779  
 780  //
 781  // Functions for handling plugins.
 782  //
 783  
 784  /**
 785   * Gets the basename of a plugin.
 786   *
 787   * This method extracts the name of a plugin from its filename.
 788   *
 789   * @since 1.5.0
 790   *
 791   * @global array $wp_plugin_paths
 792   *
 793   * @param string $file The filename of plugin.
 794   * @return string The name of a plugin.
 795   */
 796  function plugin_basename( $file ) {
 797      global $wp_plugin_paths;
 798  
 799      // $wp_plugin_paths contains normalized paths.
 800      $file = wp_normalize_path( $file );
 801  
 802      arsort( $wp_plugin_paths );
 803  
 804      foreach ( $wp_plugin_paths as $dir => $realdir ) {
 805          if ( str_starts_with( $file, $realdir ) ) {
 806              $file = $dir . substr( $file, strlen( $realdir ) );
 807          }
 808      }
 809  
 810      $plugin_dir    = wp_normalize_path( WP_PLUGIN_DIR );
 811      $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
 812  
 813      // Get relative path from plugins directory.
 814      $file = preg_replace( '#^' . preg_quote( $plugin_dir, '#' ) . '/|^' . preg_quote( $mu_plugin_dir, '#' ) . '/#', '', $file );
 815      $file = trim( $file, '/' );
 816      return $file;
 817  }
 818  
 819  /**
 820   * Register a plugin's real path.
 821   *
 822   * This is used in plugin_basename() to resolve symlinked paths.
 823   *
 824   * @since 3.9.0
 825   *
 826   * @see wp_normalize_path()
 827   *
 828   * @global array $wp_plugin_paths
 829   *
 830   * @param string $file Known path to the file.
 831   * @return bool Whether the path was able to be registered.
 832   */
 833  function wp_register_plugin_realpath( $file ) {
 834      global $wp_plugin_paths;
 835  
 836      // Normalize, but store as static to avoid recalculation of a constant value.
 837      static $wp_plugin_path = null, $wpmu_plugin_path = null;
 838  
 839      if ( ! isset( $wp_plugin_path ) ) {
 840          $wp_plugin_path   = wp_normalize_path( WP_PLUGIN_DIR );
 841          $wpmu_plugin_path = wp_normalize_path( WPMU_PLUGIN_DIR );
 842      }
 843  
 844      $plugin_path     = wp_normalize_path( dirname( $file ) );
 845      $plugin_realpath = wp_normalize_path( dirname( realpath( $file ) ) );
 846  
 847      if ( $plugin_path === $wp_plugin_path || $plugin_path === $wpmu_plugin_path ) {
 848          return false;
 849      }
 850  
 851      if ( $plugin_path !== $plugin_realpath ) {
 852          $wp_plugin_paths[ $plugin_path ] = $plugin_realpath;
 853      }
 854  
 855      return true;
 856  }
 857  
 858  /**
 859   * Get the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in.
 860   *
 861   * @since 2.8.0
 862   *
 863   * @param string $file The filename of the plugin (__FILE__).
 864   * @return string the filesystem path of the directory that contains the plugin.
 865   */
 866  function plugin_dir_path( $file ) {
 867      return trailingslashit( dirname( $file ) );
 868  }
 869  
 870  /**
 871   * Get the URL directory path (with trailing slash) for the plugin __FILE__ passed in.
 872   *
 873   * @since 2.8.0
 874   *
 875   * @param string $file The filename of the plugin (__FILE__).
 876   * @return string the URL path of the directory that contains the plugin.
 877   */
 878  function plugin_dir_url( $file ) {
 879      return trailingslashit( plugins_url( '', $file ) );
 880  }
 881  
 882  /**
 883   * Set the activation hook for a plugin.
 884   *
 885   * When a plugin is activated, the action 'activate_PLUGINNAME' hook is
 886   * called. In the name of this hook, PLUGINNAME is replaced with the name
 887   * of the plugin, including the optional subdirectory. For example, when the
 888   * plugin is located in wp-content/plugins/sampleplugin/sample.php, then
 889   * the name of this hook will become 'activate_sampleplugin/sample.php'.
 890   *
 891   * When the plugin consists of only one file and is (as by default) located at
 892   * wp-content/plugins/sample.php the name of this hook will be
 893   * 'activate_sample.php'.
 894   *
 895   * @since 2.0.0
 896   *
 897   * @param string   $file     The filename of the plugin including the path.
 898   * @param callable $callback The function hooked to the 'activate_PLUGIN' action.
 899   */
 900  function register_activation_hook( $file, $callback ) {
 901      $file = plugin_basename( $file );
 902      add_action( 'activate_' . $file, $callback );
 903  }
 904  
 905  /**
 906   * Sets the deactivation hook for a plugin.
 907   *
 908   * When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is
 909   * called. In the name of this hook, PLUGINNAME is replaced with the name
 910   * of the plugin, including the optional subdirectory. For example, when the
 911   * plugin is located in wp-content/plugins/sampleplugin/sample.php, then
 912   * the name of this hook will become 'deactivate_sampleplugin/sample.php'.
 913   *
 914   * When the plugin consists of only one file and is (as by default) located at
 915   * wp-content/plugins/sample.php the name of this hook will be
 916   * 'deactivate_sample.php'.
 917   *
 918   * @since 2.0.0
 919   *
 920   * @param string   $file     The filename of the plugin including the path.
 921   * @param callable $callback The function hooked to the 'deactivate_PLUGIN' action.
 922   */
 923  function register_deactivation_hook( $file, $callback ) {
 924      $file = plugin_basename( $file );
 925      add_action( 'deactivate_' . $file, $callback );
 926  }
 927  
 928  /**
 929   * Sets the uninstallation hook for a plugin.
 930   *
 931   * Registers the uninstall hook that will be called when the user clicks on the
 932   * uninstall link that calls for the plugin to uninstall itself. The link won't
 933   * be active unless the plugin hooks into the action.
 934   *
 935   * The plugin should not run arbitrary code outside of functions, when
 936   * registering the uninstall hook. In order to run using the hook, the plugin
 937   * will have to be included, which means that any code laying outside of a
 938   * function will be run during the uninstallation process. The plugin should not
 939   * hinder the uninstallation process.
 940   *
 941   * If the plugin can not be written without running code within the plugin, then
 942   * the plugin should create a file named 'uninstall.php' in the base plugin
 943   * folder. This file will be called, if it exists, during the uninstallation process
 944   * bypassing the uninstall hook. The plugin, when using the 'uninstall.php'
 945   * should always check for the 'WP_UNINSTALL_PLUGIN' constant, before
 946   * executing.
 947   *
 948   * @since 2.7.0
 949   *
 950   * @param string   $file     Plugin file.
 951   * @param callable $callback The callback to run when the hook is called. Must be
 952   *                           a static method or function.
 953   */
 954  function register_uninstall_hook( $file, $callback ) {
 955      if ( is_array( $callback ) && is_object( $callback[0] ) ) {
 956          _doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1.0' );
 957          return;
 958      }
 959  
 960      /*
 961       * The option should not be autoloaded, because it is not needed in most
 962       * cases. Emphasis should be put on using the 'uninstall.php' way of
 963       * uninstalling the plugin.
 964       */
 965      $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
 966      $plugin_basename       = plugin_basename( $file );
 967  
 968      if ( ! isset( $uninstallable_plugins[ $plugin_basename ] ) || $uninstallable_plugins[ $plugin_basename ] !== $callback ) {
 969          $uninstallable_plugins[ $plugin_basename ] = $callback;
 970          update_option( 'uninstall_plugins', $uninstallable_plugins );
 971      }
 972  }
 973  
 974  /**
 975   * Calls the 'all' hook, which will process the functions hooked into it.
 976   *
 977   * The 'all' hook passes all of the arguments or parameters that were used for
 978   * the hook, which this function was called for.
 979   *
 980   * This function is used internally for apply_filters(), do_action(), and
 981   * do_action_ref_array() and is not meant to be used from outside those
 982   * functions. This function does not check for the existence of the all hook, so
 983   * it will fail unless the all hook exists prior to this function call.
 984   *
 985   * @since 2.5.0
 986   * @access private
 987   *
 988   * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
 989   *
 990   * @param list<mixed> $args The collected parameters from the hook that was called.
 991   */
 992  function _wp_call_all_hook( $args ) {
 993      global $wp_filter;
 994  
 995      $wp_filter['all']->do_all_hook( $args );
 996  }
 997  
 998  /**
 999   * Builds a unique string ID for a hook callback function.
1000   *
1001   * Functions and static method callbacks are just returned as strings and
1002   * shouldn't have any speed penalty.
1003   *
1004   * @link https://core.trac.wordpress.org/ticket/3875
1005   *
1006   * @since 2.2.3
1007   * @since 5.3.0 Removed workarounds for spl_object_hash().
1008   *              `$hook_name` and `$priority` are no longer used,
1009   *              and no longer returns false, but can still return void for invalid callbacks.
1010   * @since 6.9.0 Returns explicit null if an invalid callback is supplied.
1011   * @since 7.1.0 Uses spl_object_id() instead of spl_object_hash() for performance.
1012   * @since 7.1.1 The ID for an object callback is prefixed so that it is never cast to an integer array key.
1013   *
1014   * @access private
1015   *
1016   * @param string                $hook_name Unused. The name of the filter to build ID for.
1017   * @param callable|string|array $callback  The callback to generate ID for. The callback may
1018   *                                         or may not exist.
1019   * @param int                   $priority  Unused. The order in which the functions
1020   *                                         associated with a particular action are executed.
1021   * @return string|null Unique function ID for usage as array key, or null if it couldn't be determined.
1022   * @phpstan-param Maybe_Callable $callback
1023   * @phpstan-return non-decimal-int-string|null
1024   */
1025  function _wp_filter_build_unique_id( $hook_name, $callback, $priority ): ?string {
1026      if ( is_string( $callback ) ) {
1027          return $callback;
1028      }
1029  
1030      if ( is_object( $callback ) ) {
1031          /*
1032           * The prefix keeps the ID from being the decimal representation of an integer. PHP casts such a
1033           * string to int when it is used as an array key, which would change the type of the keys in
1034           * WP_Hook::$callbacks and break consumers that pass them to string functions.
1035           */
1036          return 'spl_object_id:' . spl_object_id( $callback );
1037      }
1038  
1039      if ( ! isset( $callback[1] ) || ! is_string( $callback[1] ) ) {
1040          return null;
1041      }
1042  
1043      if ( is_object( $callback[0] ) ) {
1044          // Object class calling.
1045          return ( (string) spl_object_id( $callback[0] ) ) . $callback[1];
1046      } elseif ( is_string( $callback[0] ) ) {
1047          // Static calling.
1048          return $callback[0] . '::' . $callback[1];
1049      }
1050  
1051      return null;
1052  }


Generated : Wed Oct 14 08:20:33 2026 Cross-referenced by PHPXref