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