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


Generated: Sun Dec 5 01:00:03 2021 Cross-referenced by PHPXref 0.7.1