Ticket #21412: hookable-dropins.diff

File hookable-dropins.diff, 21.6 KB (added by mikeschinkel, 10 months ago)

hookable-dropins.diff

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