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


Generated: Sat Sep 21 01:00:03 2019 Cross-referenced by PHPXref 0.7.1