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