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


Generated: Thu Jul 18 01:00:03 2019 Cross-referenced by PHPXref 0.7.1