WordPress.org

Make WordPress Core

Ticket #21412: hooks.php

File hooks.php, 19.7 KB (added by mikeschinkel, 3 years ago)

hooks.php which contains the files moved out of plugin.php.

Line 
1<?php
2
3/**
4 * Hooks a function or method to a specific filter action.
5 *
6 * Filters are the hooks that WordPress launches to modify text of various types
7 * before adding it to the database or sending it to the browser screen. Plugins
8 * can specify that one or more of its PHP functions is executed to
9 * modify specific types of text at these times, using the Filter API.
10 *
11 * To use the API, the following code should be used to bind a callback to the
12 * filter.
13 *
14 * <code>
15 * function example_hook($example) { echo $example; }
16 * add_filter('example_filter', 'example_hook');
17 * </code>
18 *
19 * In WordPress 1.5.1+, hooked functions can take extra arguments that are set
20 * when the matching do_action() or apply_filters() call is run. The
21 * $accepted_args allow for calling functions only when the number of args
22 * match. Hooked functions can take extra arguments that are set when the
23 * matching do_action() or apply_filters() call is run. For example, the action
24 * comment_id_not_found will pass any functions that hook onto it the ID of the
25 * requested comment.
26 *
27 * <strong>Note:</strong> the function will return true no matter if the
28 * function was hooked fails or not. There are no checks for whether the
29 * function exists beforehand and no checks to whether the <tt>$function_to_add
30 * is even a string. It is up to you to take care and this is done for
31 * optimization purposes, so everything is as quick as possible.
32 *
33 * @package WordPress
34 * @subpackage Plugin
35 * @since 0.71
36 * @global array $wp_filter Stores all of the filters added in the form of
37 *      wp_filter['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)']']
38 * @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.
39 *
40 * @param string $tag The name of the filter to hook the $function_to_add to.
41 * @param callback $function_to_add The name of the function to be called when the filter is applied.
42 * @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.
43 * @param int $accepted_args optional. The number of arguments the function accept (default 1).
44 * @return boolean true
45 */
46function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
47        global $wp_filter, $merged_filters;
48
49        $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
50        $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
51        unset( $merged_filters[ $tag ] );
52        return true;
53}
54
55/**
56 * Check if any filter has been registered for a hook.
57 *
58 * @package WordPress
59 * @subpackage Plugin
60 * @since 2.5
61 * @global array $wp_filter Stores all of the filters
62 *
63 * @param string $tag The name of the filter hook.
64 * @param callback $function_to_check optional.
65 * @return mixed If $function_to_check is omitted, returns boolean for whether the hook has anything registered.
66 *      When checking a specific function, the priority of that hook is returned, or false if the function is not attached.
67 *      When using the $function_to_check argument, this function may return a non-boolean value that evaluates to false
68 *      (e.g.) 0, so use the === operator for testing the return value.
69 */
70function has_filter($tag, $function_to_check = false) {
71        global $wp_filter;
72
73        $has = !empty($wp_filter[$tag]);
74        if ( false === $function_to_check || false == $has )
75                return $has;
76
77        if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) )
78                return false;
79
80        foreach ( (array) array_keys($wp_filter[$tag]) as $priority ) {
81                if ( isset($wp_filter[$tag][$priority][$idx]) )
82                        return $priority;
83        }
84
85        return false;
86}
87
88/**
89 * Call the functions added to a filter hook.
90 *
91 * The callback functions attached to filter hook $tag are invoked by calling
92 * this function. This function can be used to create a new filter hook by
93 * simply calling this function with the name of the new hook specified using
94 * the $tag parameter.
95 *
96 * The function allows for additional arguments to be added and passed to hooks.
97 * <code>
98 * function example_hook($string, $arg1, $arg2)
99 * {
100 *              //Do stuff
101 *              return $string;
102 * }
103 * $value = apply_filters('example_filter', 'filter me', 'arg1', 'arg2');
104 * </code>
105 *
106 * @package WordPress
107 * @subpackage Plugin
108 * @since 0.71
109 * @global array $wp_filter Stores all of the filters
110 * @global array $merged_filters Merges the filter hooks using this function.
111 * @global array $wp_current_filter stores the list of current filters with the current one last
112 *
113 * @param string $tag The name of the filter hook.
114 * @param mixed $value The value on which the filters hooked to <tt>$tag</tt> are applied on.
115 * @param mixed $var,... Additional variables passed to the functions hooked to <tt>$tag</tt>.
116 * @return mixed The filtered value after all hooked functions are applied to it.
117 */
118function apply_filters($tag, $value) {
119        global $wp_filter, $merged_filters, $wp_current_filter;
120
121        $args = array();
122
123        // Do 'all' actions first
124        if ( isset($wp_filter['all']) ) {
125                $wp_current_filter[] = $tag;
126                $args = func_get_args();
127                _wp_call_all_hook($args);
128        }
129
130        if ( !isset($wp_filter[$tag]) ) {
131                if ( isset($wp_filter['all']) )
132                        array_pop($wp_current_filter);
133                return $value;
134        }
135
136        if ( !isset($wp_filter['all']) )
137                $wp_current_filter[] = $tag;
138
139        // Sort
140        if ( !isset( $merged_filters[ $tag ] ) ) {
141                ksort($wp_filter[$tag]);
142                $merged_filters[ $tag ] = true;
143        }
144
145        reset( $wp_filter[ $tag ] );
146
147        if ( empty($args) )
148                $args = func_get_args();
149
150        do {
151                foreach( (array) current($wp_filter[$tag]) as $the_ )
152                        if ( !is_null($the_['function']) ){
153                                $args[1] = $value;
154                                $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
155                        }
156
157        } while ( next($wp_filter[$tag]) !== false );
158
159        array_pop( $wp_current_filter );
160
161        return $value;
162}
163
164/**
165 * Execute functions hooked on a specific filter hook, specifying arguments in an array.
166 *
167 * @see apply_filters() This function is identical, but the arguments passed to the
168 * functions hooked to <tt>$tag</tt> are supplied using an array.
169 *
170 * @package WordPress
171 * @subpackage Plugin
172 * @since 3.0.0
173 * @global array $wp_filter Stores all of the filters
174 * @global array $merged_filters Merges the filter hooks using this function.
175 * @global array $wp_current_filter stores the list of current filters with the current one last
176 *
177 * @param string $tag The name of the filter hook.
178 * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
179 * @return mixed The filtered value after all hooked functions are applied to it.
180 */
181function apply_filters_ref_array($tag, $args) {
182        global $wp_filter, $merged_filters, $wp_current_filter;
183
184        // Do 'all' actions first
185        if ( isset($wp_filter['all']) ) {
186                $wp_current_filter[] = $tag;
187                $all_args = func_get_args();
188                _wp_call_all_hook($all_args);
189        }
190
191        if ( !isset($wp_filter[$tag]) ) {
192                if ( isset($wp_filter['all']) )
193                        array_pop($wp_current_filter);
194                return $args[0];
195        }
196
197        if ( !isset($wp_filter['all']) )
198                $wp_current_filter[] = $tag;
199
200        // Sort
201        if ( !isset( $merged_filters[ $tag ] ) ) {
202                ksort($wp_filter[$tag]);
203                $merged_filters[ $tag ] = true;
204        }
205
206        reset( $wp_filter[ $tag ] );
207
208        do {
209                foreach( (array) current($wp_filter[$tag]) as $the_ )
210                        if ( !is_null($the_['function']) )
211                                $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
212
213        } while ( next($wp_filter[$tag]) !== false );
214
215        array_pop( $wp_current_filter );
216
217        return $args[0];
218}
219
220/**
221 * Removes a function from a specified filter hook.
222 *
223 * This function removes a function attached to a specified filter hook. This
224 * method can be used to remove default functions attached to a specific filter
225 * hook and possibly replace them with a substitute.
226 *
227 * To remove a hook, the $function_to_remove and $priority arguments must match
228 * when the hook was added. This goes for both filters and actions. No warning
229 * will be given on removal failure.
230 *
231 * @package WordPress
232 * @subpackage Plugin
233 * @since 1.2
234 *
235 * @param string $tag The filter hook to which the function to be removed is hooked.
236 * @param callback $function_to_remove The name of the function which should be removed.
237 * @param int $priority optional. The priority of the function (default: 10).
238 * @param int $accepted_args optional. The number of arguments the function accepts (default: 1).
239 * @return boolean Whether the function existed before it was removed.
240 */
241function remove_filter( $tag, $function_to_remove, $priority = 10 ) {
242        $function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority);
243
244        $r = isset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
245
246        if ( true === $r) {
247                unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
248                if ( empty($GLOBALS['wp_filter'][$tag][$priority]) )
249                        unset($GLOBALS['wp_filter'][$tag][$priority]);
250                unset($GLOBALS['merged_filters'][$tag]);
251        }
252
253        return $r;
254}
255
256/**
257 * Remove all of the hooks from a filter.
258 *
259 * @since 2.7
260 *
261 * @param string $tag The filter to remove hooks from.
262 * @param int $priority The priority number to remove.
263 * @return bool True when finished.
264 */
265function remove_all_filters($tag, $priority = false) {
266        global $wp_filter, $merged_filters;
267
268        if( isset($wp_filter[$tag]) ) {
269                if( false !== $priority && isset($wp_filter[$tag][$priority]) )
270                        unset($wp_filter[$tag][$priority]);
271                else
272                        unset($wp_filter[$tag]);
273        }
274
275        if( isset($merged_filters[$tag]) )
276                unset($merged_filters[$tag]);
277
278        return true;
279}
280
281/**
282 * Retrieve the name of the current filter or action.
283 *
284 * @package WordPress
285 * @subpackage Plugin
286 * @since 2.5
287 *
288 * @return string Hook name of the current filter or action.
289 */
290function current_filter() {
291        global $wp_current_filter;
292        return end( $wp_current_filter );
293}
294
295/**
296 * Hooks a function on to a specific action.
297 *
298 * Actions are the hooks that the WordPress core launches at specific points
299 * during execution, or when specific events occur. Plugins can specify that
300 * one or more of its PHP functions are executed at these points, using the
301 * Action API.
302 *
303 * @uses add_filter() Adds an action. Parameter list and functionality are the same.
304 *
305 * @package WordPress
306 * @subpackage Plugin
307 * @since 1.2
308 *
309 * @param string $tag The name of the action to which the $function_to_add is hooked.
310 * @param callback $function_to_add The name of the function you wish to be called.
311 * @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.
312 * @param int $accepted_args optional. The number of arguments the function accept (default 1).
313 */
314function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
315        return add_filter($tag, $function_to_add, $priority, $accepted_args);
316}
317
318/**
319 * Execute functions hooked on a specific action hook.
320 *
321 * This function invokes all functions attached to action hook $tag. It is
322 * possible to create new action hooks by simply calling this function,
323 * specifying the name of the new hook using the <tt>$tag</tt> parameter.
324 *
325 * You can pass extra arguments to the hooks, much like you can with
326 * apply_filters().
327 *
328 * @see apply_filters() This function works similar with the exception that
329 * nothing is returned and only the functions or methods are called.
330 *
331 * @package WordPress
332 * @subpackage Plugin
333 * @since 1.2
334 * @global array $wp_filter Stores all of the filters
335 * @global array $wp_actions Increments the amount of times action was triggered.
336 *
337 * @param string $tag The name of the action to be executed.
338 * @param mixed $arg,... Optional additional arguments which are passed on to the functions hooked to the action.
339 * @return null Will return null if $tag does not exist in $wp_filter array
340 */
341function do_action($tag, $arg = '') {
342        global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
343
344        if ( ! isset($wp_actions) )
345                $wp_actions = array();
346
347        if ( ! isset($wp_actions[$tag]) )
348                $wp_actions[$tag] = 1;
349        else
350                ++$wp_actions[$tag];
351
352        // Do 'all' actions first
353        if ( isset($wp_filter['all']) ) {
354                $wp_current_filter[] = $tag;
355                $all_args = func_get_args();
356                _wp_call_all_hook($all_args);
357        }
358
359        if ( !isset($wp_filter[$tag]) ) {
360                if ( isset($wp_filter['all']) )
361                        array_pop($wp_current_filter);
362                return;
363        }
364
365        if ( !isset($wp_filter['all']) )
366                $wp_current_filter[] = $tag;
367
368        $args = array();
369        if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
370                $args[] =& $arg[0];
371        else
372                $args[] = $arg;
373        for ( $a = 2; $a < func_num_args(); $a++ )
374                $args[] = func_get_arg($a);
375
376        // Sort
377        if ( !isset( $merged_filters[ $tag ] ) ) {
378                ksort($wp_filter[$tag]);
379                $merged_filters[ $tag ] = true;
380        }
381
382        reset( $wp_filter[ $tag ] );
383
384        do {
385                foreach ( (array) current($wp_filter[$tag]) as $the_ )
386                        if ( !is_null($the_['function']) )
387                                call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
388
389        } while ( next($wp_filter[$tag]) !== false );
390
391        array_pop($wp_current_filter);
392}
393
394/**
395 * Retrieve the number of times an action is fired.
396 *
397 * @package WordPress
398 * @subpackage Plugin
399 * @since 2.1
400 * @global array $wp_actions Increments the amount of times action was triggered.
401 *
402 * @param string $tag The name of the action hook.
403 * @return int The number of times action hook <tt>$tag</tt> is fired
404 */
405function did_action($tag) {
406        global $wp_actions;
407
408        if ( ! isset( $wp_actions ) || ! isset( $wp_actions[$tag] ) )
409                return 0;
410
411        return $wp_actions[$tag];
412}
413
414/**
415 * Execute functions hooked on a specific action hook, specifying arguments in an array.
416 *
417 * @see do_action() This function is identical, but the arguments passed to the
418 * functions hooked to <tt>$tag</tt> are supplied using an array.
419 *
420 * @package WordPress
421 * @subpackage Plugin
422 * @since 2.1
423 * @global array $wp_filter Stores all of the filters
424 * @global array $wp_actions Increments the amount of times action was triggered.
425 *
426 * @param string $tag The name of the action to be executed.
427 * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
428 * @return null Will return null if $tag does not exist in $wp_filter array
429 */
430function do_action_ref_array($tag, $args) {
431        global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
432
433        if ( ! isset($wp_actions) )
434                $wp_actions = array();
435
436        if ( ! isset($wp_actions[$tag]) )
437                $wp_actions[$tag] = 1;
438        else
439                ++$wp_actions[$tag];
440
441        // Do 'all' actions first
442        if ( isset($wp_filter['all']) ) {
443                $wp_current_filter[] = $tag;
444                $all_args = func_get_args();
445                _wp_call_all_hook($all_args);
446        }
447
448        if ( !isset($wp_filter[$tag]) ) {
449                if ( isset($wp_filter['all']) )
450                        array_pop($wp_current_filter);
451                return;
452        }
453
454        if ( !isset($wp_filter['all']) )
455                $wp_current_filter[] = $tag;
456
457        // Sort
458        if ( !isset( $merged_filters[ $tag ] ) ) {
459                ksort($wp_filter[$tag]);
460                $merged_filters[ $tag ] = true;
461        }
462
463        reset( $wp_filter[ $tag ] );
464
465        do {
466                foreach( (array) current($wp_filter[$tag]) as $the_ )
467                        if ( !is_null($the_['function']) )
468                                call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
469
470        } while ( next($wp_filter[$tag]) !== false );
471
472        array_pop($wp_current_filter);
473}
474
475/**
476 * Check if any action has been registered for a hook.
477 *
478 * @package WordPress
479 * @subpackage Plugin
480 * @since 2.5
481 * @see has_filter() has_action() is an alias of has_filter().
482 *
483 * @param string $tag The name of the action hook.
484 * @param callback $function_to_check optional.
485 * @return mixed If $function_to_check is omitted, returns boolean for whether the hook has anything registered.
486 *      When checking a specific function, the priority of that hook is returned, or false if the function is not attached.
487 *      When using the $function_to_check argument, this function may return a non-boolean value that evaluates to false
488 *      (e.g.) 0, so use the === operator for testing the return value.
489 */
490function has_action($tag, $function_to_check = false) {
491        return has_filter($tag, $function_to_check);
492}
493
494/**
495 * Removes a function from a specified action hook.
496 *
497 * This function removes a function attached to a specified action hook. This
498 * method can be used to remove default functions attached to a specific filter
499 * hook and possibly replace them with a substitute.
500 *
501 * @package WordPress
502 * @subpackage Plugin
503 * @since 1.2
504 *
505 * @param string $tag The action hook to which the function to be removed is hooked.
506 * @param callback $function_to_remove The name of the function which should be removed.
507 * @param int $priority optional The priority of the function (default: 10).
508 * @return boolean Whether the function is removed.
509 */
510function remove_action( $tag, $function_to_remove, $priority = 10 ) {
511        return remove_filter( $tag, $function_to_remove, $priority );
512}
513
514/**
515 * Remove all of the hooks from an action.
516 *
517 * @since 2.7
518 *
519 * @param string $tag The action to remove hooks from.
520 * @param int $priority The priority number to remove them from.
521 * @return bool True when finished.
522 */
523function remove_all_actions($tag, $priority = false) {
524        return remove_all_filters($tag, $priority);
525}
526
527/**
528 * Build Unique ID for storage and retrieval.
529 *
530 * The old way to serialize the callback caused issues and this function is the
531 * solution. It works by checking for objects and creating an a new property in
532 * the class to keep track of the object and new objects of the same class that
533 * need to be added.
534 *
535 * It also allows for the removal of actions and filters for objects after they
536 * change class properties. It is possible to include the property $wp_filter_id
537 * in your class and set it to "null" or a number to bypass the workaround.
538 * However this will prevent you from adding new classes and any new classes
539 * will overwrite the previous hook by the same class.
540 *
541 * Functions and static method callbacks are just returned as strings and
542 * shouldn't have any speed penalty.
543 *
544 * @package WordPress
545 * @subpackage Plugin
546 * @access private
547 * @since 2.2.3
548 * @link http://trac.wordpress.org/ticket/3875
549 *
550 * @global array $wp_filter Storage for all of the filters and actions
551 * @param string $tag Used in counting how many hooks were applied
552 * @param callback $function Used for creating unique id
553 * @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.
554 * @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.
555 */
556function _wp_filter_build_unique_id($tag, $function, $priority) {
557        global $wp_filter;
558        static $filter_id_count = 0;
559
560        if ( is_string($function) )
561                return $function;
562
563        if ( is_object($function) ) {
564                // Closures are currently implemented as objects
565                $function = array( $function, '' );
566        } else {
567                $function = (array) $function;
568        }
569
570        if (is_object($function[0]) ) {
571                // Object Class Calling
572                if ( function_exists('spl_object_hash') ) {
573                        return spl_object_hash($function[0]) . $function[1];
574                } else {
575                        $obj_idx = get_class($function[0]).$function[1];
576                        if ( !isset($function[0]->wp_filter_id) ) {
577                                if ( false === $priority )
578                                        return false;
579                                $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;
580                                $function[0]->wp_filter_id = $filter_id_count;
581                                ++$filter_id_count;
582                        } else {
583                                $obj_idx .= $function[0]->wp_filter_id;
584                        }
585
586                        return $obj_idx;
587                }
588        } else if ( is_string($function[0]) ) {
589                // Static Calling
590                return $function[0].$function[1];
591        }
592}