[ Index ]

PHP Cross Reference of WordPress

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


Generated: Sun Dec 22 01:00:02 2024 Cross-referenced by PHPXref 0.7.1