public/blog/wp-includes/plugin.php line 482

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