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