]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/plugin.php
Wordpress 4.5.3-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 http://us2.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
469  * {@see apply_filters()}.
470  *
471  * @since 1.2.0
472  *
473  * @global array $wp_filter         Stores all of the filters
474  * @global array $wp_actions        Increments the amount of times action was triggered.
475  * @global array $merged_filters    Merges the filter hooks using this function.
476  * @global array $wp_current_filter Stores the list of current filters with the current one last
477  *
478  * @param string $tag     The name of the action to be executed.
479  * @param mixed  $arg,... Optional. Additional arguments which are passed on to the
480  *                        functions hooked to the action. Default empty.
481  */
482 function do_action($tag, $arg = '') {
483         global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
484
485         if ( ! isset($wp_actions[$tag]) )
486                 $wp_actions[$tag] = 1;
487         else
488                 ++$wp_actions[$tag];
489
490         // Do 'all' actions first
491         if ( isset($wp_filter['all']) ) {
492                 $wp_current_filter[] = $tag;
493                 $all_args = func_get_args();
494                 _wp_call_all_hook($all_args);
495         }
496
497         if ( !isset($wp_filter[$tag]) ) {
498                 if ( isset($wp_filter['all']) )
499                         array_pop($wp_current_filter);
500                 return;
501         }
502
503         if ( !isset($wp_filter['all']) )
504                 $wp_current_filter[] = $tag;
505
506         $args = array();
507         if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
508                 $args[] =& $arg[0];
509         else
510                 $args[] = $arg;
511         for ( $a = 2, $num = func_num_args(); $a < $num; $a++ )
512                 $args[] = func_get_arg($a);
513
514         // Sort
515         if ( !isset( $merged_filters[ $tag ] ) ) {
516                 ksort($wp_filter[$tag]);
517                 $merged_filters[ $tag ] = true;
518         }
519
520         reset( $wp_filter[ $tag ] );
521
522         do {
523                 foreach ( (array) current($wp_filter[$tag]) as $the_ )
524                         if ( !is_null($the_['function']) )
525                                 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
526
527         } while ( next($wp_filter[$tag]) !== false );
528
529         array_pop($wp_current_filter);
530 }
531
532 /**
533  * Retrieve the number of times an action is fired.
534  *
535  * @since 2.1.0
536  *
537  * @global array $wp_actions Increments the amount of times action was triggered.
538  *
539  * @param string $tag The name of the action hook.
540  * @return int The number of times action hook $tag is fired.
541  */
542 function did_action($tag) {
543         global $wp_actions;
544
545         if ( ! isset( $wp_actions[ $tag ] ) )
546                 return 0;
547
548         return $wp_actions[$tag];
549 }
550
551 /**
552  * Execute functions hooked on a specific action hook, specifying arguments in an array.
553  *
554  * @since 2.1.0
555  *
556  * @see do_action() This function is identical, but the arguments passed to the
557  *                  functions hooked to $tag< are supplied using an array.
558  * @global array $wp_filter         Stores all of the filters
559  * @global array $wp_actions        Increments the amount of times action was triggered.
560  * @global array $merged_filters    Merges the filter hooks using this function.
561  * @global array $wp_current_filter Stores the list of current filters with the current one last
562  *
563  * @param string $tag  The name of the action to be executed.
564  * @param array  $args The arguments supplied to the functions hooked to `$tag`.
565  */
566 function do_action_ref_array($tag, $args) {
567         global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
568
569         if ( ! isset($wp_actions[$tag]) )
570                 $wp_actions[$tag] = 1;
571         else
572                 ++$wp_actions[$tag];
573
574         // Do 'all' actions first
575         if ( isset($wp_filter['all']) ) {
576                 $wp_current_filter[] = $tag;
577                 $all_args = func_get_args();
578                 _wp_call_all_hook($all_args);
579         }
580
581         if ( !isset($wp_filter[$tag]) ) {
582                 if ( isset($wp_filter['all']) )
583                         array_pop($wp_current_filter);
584                 return;
585         }
586
587         if ( !isset($wp_filter['all']) )
588                 $wp_current_filter[] = $tag;
589
590         // Sort
591         if ( !isset( $merged_filters[ $tag ] ) ) {
592                 ksort($wp_filter[$tag]);
593                 $merged_filters[ $tag ] = true;
594         }
595
596         reset( $wp_filter[ $tag ] );
597
598         do {
599                 foreach ( (array) current($wp_filter[$tag]) as $the_ )
600                         if ( !is_null($the_['function']) )
601                                 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
602
603         } while ( next($wp_filter[$tag]) !== false );
604
605         array_pop($wp_current_filter);
606 }
607
608 /**
609  * Check if any action has been registered for a hook.
610  *
611  * @since 2.5.0
612  *
613  * @see has_filter() has_action() is an alias of has_filter().
614  *
615  * @param string        $tag               The name of the action hook.
616  * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
617  * @return bool|int If $function_to_check is omitted, returns boolean for whether the hook has
618  *                  anything registered. When checking a specific function, the priority of that
619  *                  hook is returned, or false if the function is not attached. When using the
620  *                  $function_to_check argument, this function may return a non-boolean value
621  *                  that evaluates to false (e.g.) 0, so use the === operator for testing the
622  *                  return value.
623  */
624 function has_action($tag, $function_to_check = false) {
625         return has_filter($tag, $function_to_check);
626 }
627
628 /**
629  * Removes a function from a specified action hook.
630  *
631  * This function removes a function attached to a specified action hook. This
632  * method can be used to remove default functions attached to a specific filter
633  * hook and possibly replace them with a substitute.
634  *
635  * @since 1.2.0
636  *
637  * @param string   $tag                The action hook to which the function to be removed is hooked.
638  * @param callable $function_to_remove The name of the function which should be removed.
639  * @param int      $priority           Optional. The priority of the function. Default 10.
640  * @return bool Whether the function is removed.
641  */
642 function remove_action( $tag, $function_to_remove, $priority = 10 ) {
643         return remove_filter( $tag, $function_to_remove, $priority );
644 }
645
646 /**
647  * Remove all of the hooks from an action.
648  *
649  * @since 2.7.0
650  *
651  * @param string   $tag      The action to remove hooks from.
652  * @param int|bool $priority The priority number to remove them from. Default false.
653  * @return true True when finished.
654  */
655 function remove_all_actions($tag, $priority = false) {
656         return remove_all_filters($tag, $priority);
657 }
658
659 //
660 // Functions for handling plugins.
661 //
662
663 /**
664  * Gets the basename of a plugin.
665  *
666  * This method extracts the name of a plugin from its filename.
667  *
668  * @since 1.5.0
669  *
670  * @global array $wp_plugin_paths
671  *
672  * @param string $file The filename of plugin.
673  * @return string The name of a plugin.
674  */
675 function plugin_basename( $file ) {
676         global $wp_plugin_paths;
677
678         foreach ( $wp_plugin_paths as $dir => $realdir ) {
679                 if ( strpos( $file, $realdir ) === 0 ) {
680                         $file = $dir . substr( $file, strlen( $realdir ) );
681                 }
682         }
683
684         $file = wp_normalize_path( $file );
685         $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
686         $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
687
688         $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
689         $file = trim($file, '/');
690         return $file;
691 }
692
693 /**
694  * Register a plugin's real path.
695  *
696  * This is used in plugin_basename() to resolve symlinked paths.
697  *
698  * @since 3.9.0
699  *
700  * @see plugin_basename()
701  *
702  * @global array $wp_plugin_paths
703  *
704  * @staticvar string $wp_plugin_path
705  * @staticvar string $wpmu_plugin_path
706  *
707  * @param string $file Known path to the file.
708  * @return bool Whether the path was able to be registered.
709  */
710 function wp_register_plugin_realpath( $file ) {
711         global $wp_plugin_paths;
712
713         // Normalize, but store as static to avoid recalculation of a constant value
714         static $wp_plugin_path = null, $wpmu_plugin_path = null;
715         if ( ! isset( $wp_plugin_path ) ) {
716                 $wp_plugin_path   = wp_normalize_path( WP_PLUGIN_DIR   );
717                 $wpmu_plugin_path = wp_normalize_path( WPMU_PLUGIN_DIR );
718         }
719
720         $plugin_path = wp_normalize_path( dirname( $file ) );
721         $plugin_realpath = wp_normalize_path( dirname( realpath( $file ) ) );
722
723         if ( $plugin_path === $wp_plugin_path || $plugin_path === $wpmu_plugin_path ) {
724                 return false;
725         }
726
727         if ( $plugin_path !== $plugin_realpath ) {
728                 $wp_plugin_paths[ $plugin_path ] = $plugin_realpath;
729         }
730
731         return true;
732 }
733
734 /**
735  * Get the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in.
736  *
737  * @since 2.8.0
738  *
739  * @param string $file The filename of the plugin (__FILE__).
740  * @return string the filesystem path of the directory that contains the plugin.
741  */
742 function plugin_dir_path( $file ) {
743         return trailingslashit( dirname( $file ) );
744 }
745
746 /**
747  * Get the URL directory path (with trailing slash) for the plugin __FILE__ passed in.
748  *
749  * @since 2.8.0
750  *
751  * @param string $file The filename of the plugin (__FILE__).
752  * @return string the URL path of the directory that contains the plugin.
753  */
754 function plugin_dir_url( $file ) {
755         return trailingslashit( plugins_url( '', $file ) );
756 }
757
758 /**
759  * Set the activation hook for a plugin.
760  *
761  * When a plugin is activated, the action 'activate_PLUGINNAME' hook is
762  * called. In the name of this hook, PLUGINNAME is replaced with the name
763  * of the plugin, including the optional subdirectory. For example, when the
764  * plugin is located in wp-content/plugins/sampleplugin/sample.php, then
765  * the name of this hook will become 'activate_sampleplugin/sample.php'.
766  *
767  * When the plugin consists of only one file and is (as by default) located at
768  * wp-content/plugins/sample.php the name of this hook will be
769  * 'activate_sample.php'.
770  *
771  * @since 2.0.0
772  *
773  * @param string   $file     The filename of the plugin including the path.
774  * @param callable $function The function hooked to the 'activate_PLUGIN' action.
775  */
776 function register_activation_hook($file, $function) {
777         $file = plugin_basename($file);
778         add_action('activate_' . $file, $function);
779 }
780
781 /**
782  * Set the deactivation hook for a plugin.
783  *
784  * When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is
785  * called. In the name of this hook, PLUGINNAME is replaced with the name
786  * of the plugin, including the optional subdirectory. For example, when the
787  * plugin is located in wp-content/plugins/sampleplugin/sample.php, then
788  * the name of this hook will become 'deactivate_sampleplugin/sample.php'.
789  *
790  * When the plugin consists of only one file and is (as by default) located at
791  * wp-content/plugins/sample.php the name of this hook will be
792  * 'deactivate_sample.php'.
793  *
794  * @since 2.0.0
795  *
796  * @param string   $file     The filename of the plugin including the path.
797  * @param callable $function The function hooked to the 'deactivate_PLUGIN' action.
798  */
799 function register_deactivation_hook($file, $function) {
800         $file = plugin_basename($file);
801         add_action('deactivate_' . $file, $function);
802 }
803
804 /**
805  * Set the uninstallation hook for a plugin.
806  *
807  * Registers the uninstall hook that will be called when the user clicks on the
808  * uninstall link that calls for the plugin to uninstall itself. The link won't
809  * be active unless the plugin hooks into the action.
810  *
811  * The plugin should not run arbitrary code outside of functions, when
812  * registering the uninstall hook. In order to run using the hook, the plugin
813  * will have to be included, which means that any code laying outside of a
814  * function will be run during the uninstall process. The plugin should not
815  * hinder the uninstall process.
816  *
817  * If the plugin can not be written without running code within the plugin, then
818  * the plugin should create a file named 'uninstall.php' in the base plugin
819  * folder. This file will be called, if it exists, during the uninstall process
820  * bypassing the uninstall hook. The plugin, when using the 'uninstall.php'
821  * should always check for the 'WP_UNINSTALL_PLUGIN' constant, before
822  * executing.
823  *
824  * @since 2.7.0
825  *
826  * @param string   $file     Plugin file.
827  * @param callable $callback The callback to run when the hook is called. Must be
828  *                           a static method or function.
829  */
830 function register_uninstall_hook( $file, $callback ) {
831         if ( is_array( $callback ) && is_object( $callback[0] ) ) {
832                 _doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1' );
833                 return;
834         }
835
836         /*
837          * The option should not be autoloaded, because it is not needed in most
838          * cases. Emphasis should be put on using the 'uninstall.php' way of
839          * uninstalling the plugin.
840          */
841         $uninstallable_plugins = (array) get_option('uninstall_plugins');
842         $uninstallable_plugins[plugin_basename($file)] = $callback;
843
844         update_option('uninstall_plugins', $uninstallable_plugins);
845 }
846
847 /**
848  * Call the 'all' hook, which will process the functions hooked into it.
849  *
850  * The 'all' hook passes all of the arguments or parameters that were used for
851  * the hook, which this function was called for.
852  *
853  * This function is used internally for apply_filters(), do_action(), and
854  * do_action_ref_array() and is not meant to be used from outside those
855  * functions. This function does not check for the existence of the all hook, so
856  * it will fail unless the all hook exists prior to this function call.
857  *
858  * @since 2.5.0
859  * @access private
860  *
861  * @global array $wp_filter  Stores all of the filters
862  *
863  * @param array $args The collected parameters from the hook that was called.
864  */
865 function _wp_call_all_hook($args) {
866         global $wp_filter;
867
868         reset( $wp_filter['all'] );
869         do {
870                 foreach ( (array) current($wp_filter['all']) as $the_ )
871                         if ( !is_null($the_['function']) )
872                                 call_user_func_array($the_['function'], $args);
873
874         } while ( next($wp_filter['all']) !== false );
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         if ( is_object($function) ) {
919                 // Closures are currently implemented as objects
920                 $function = array( $function, '' );
921         } else {
922                 $function = (array) $function;
923         }
924
925         if (is_object($function[0]) ) {
926                 // Object Class Calling
927                 if ( function_exists('spl_object_hash') ) {
928                         return spl_object_hash($function[0]) . $function[1];
929                 } else {
930                         $obj_idx = get_class($function[0]).$function[1];
931                         if ( !isset($function[0]->wp_filter_id) ) {
932                                 if ( false === $priority )
933                                         return false;
934                                 $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;
935                                 $function[0]->wp_filter_id = $filter_id_count;
936                                 ++$filter_id_count;
937                         } else {
938                                 $obj_idx .= $function[0]->wp_filter_id;
939                         }
940
941                         return $obj_idx;
942                 }
943         } elseif ( is_string( $function[0] ) ) {
944                 // Static Calling
945                 return $function[0] . '::' . $function[1];
946         }
947 }