Ticket #5231: plugin.#5231.r6312.take3.diff

File plugin.#5231.r6312.take3.diff, 8.7 KB (added by darkdragon, 5 years ago)

Separate action and filter hooks out into their own globals.

  • plugin.php

     
    5959 * @package WordPress 
    6060 * @subpackage Plugin 
    6161 * @since 1.5 
    62  * @global array $wp_filter Stores all of the filters added in the form of 
    63  *      wp_filter['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)]'] 
     62 * @global array $wp_filter_hooks Stores all of the filters added in the form of 
     63 *      $wp_filter_hooks['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)]'] 
    6464 * @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. 
    6565 * 
    6666 * @param string $tag The name of the filter to hook the <tt>$function_to_add</tt> to. 
     
    7070 * @return boolean true 
    7171 */ 
    7272function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) { 
    73         global $wp_filter, $merged_filters; 
     73        global $wp_filter_hooks, $merged_filters; 
    7474 
    75         $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority); 
    76         $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); 
     75        $wp_filter_hooks[$tag][$priority][_wp_filter_build_unique_id($tag, $function_to_add, $priority)] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); 
    7776        unset( $merged_filters[ $tag ] ); 
    7877        return true; 
    7978} 
     
    107106 * @return string The text in <tt>$string</tt> after all hooked functions are applied to it. 
    108107 */ 
    109108function apply_filters($tag, $value) { 
    110         global $wp_filter, $merged_filters; 
     109        global $wp_filter_hooks, $merged_filters; 
    111110 
    112111        if ( !isset( $merged_filters[ $tag ] ) ) 
    113                 merge_filters($tag); 
     112                merge_filters($tag, 'filter'); 
    114113 
    115114        if ( !isset($wp_filter[$tag]) ) 
    116115                return $value; 
    117116 
    118         reset( $wp_filter[ $tag ] ); 
     117        reset( $wp_filter_hooks[ $tag ] ); 
    119118 
    120119        $args = func_get_args(); 
    121120 
    122121        do{ 
    123                 foreach( (array) current($wp_filter[$tag]) as $the_ ) 
     122                foreach( (array) current($wp_filter_hooks[$tag]) as $the_ ) 
    124123                        if ( !is_null($the_['function']) ){ 
    125124                                $args[1] = $value; 
    126125                                $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args'])); 
    127126                        } 
    128127 
    129         } while ( next($wp_filter[$tag]) !== false ); 
     128        } while ( next($wp_filter_hooks[$tag]) !== false ); 
    130129 
    131130        return $value; 
    132131} 
     
    149148 * @global array $merge_filters Merges the filter hooks using this function. 
    150149 * 
    151150 * @param string $tag The filter hook of which the functions should be merged. 
     151 * @param string $caller Whether the caller is a 'filter' or 'action' 
    152152 */ 
    153 function merge_filters($tag) { 
    154         global $wp_filter, $merged_filters; 
     153function merge_filters($tag, $caller='filter') { 
     154        global $merged_filters; 
    155155 
    156         if ( isset($wp_filter['all']) && is_array($wp_filter['all']) ) 
    157                 $wp_filter[$tag] = array_merge($wp_filter['all'], (array) $wp_filter[$tag]); 
     156        $global_hook = 'wp_'.$caller.'_hooks'; 
     157         
     158        global $$global_hook; // Yeah, yeah, this is hackish. I know 
     159        if ( isset($$global_hook[$all]) && is_array($$global_hook[$all]) ) 
     160                $$global_hook[$tag] = $$global_hook[$all] + (array) $$global_hook[$tag]; 
    158161 
    159         if ( isset($wp_filter[$tag]) ){ 
    160                 reset($wp_filter[$tag]); 
    161                 uksort($wp_filter[$tag], "strnatcasecmp"); 
     162        if ( isset($$global_hook[$tag]) ){ 
     163                reset($$global_hook[$tag]); 
     164                uksort($$global_hook[$tag], "strnatcasecmp"); 
    162165        } 
    163166        $merged_filters[ $tag ] = true; 
    164167} 
     
    185188 * @return boolean Whether the function existed before it was removed. 
    186189 */ 
    187190function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) { 
    188         $function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority); 
     191        $r = isset($GLOBALS['wp_filter_hooks'][$tag][$priority][_wp_filter_build_unique_id($tag, $function_to_remove, $priority)]); 
    189192 
    190         $r = isset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]); 
     193        if( true === $r) { 
     194                unset($GLOBALS['wp_filter_hooks'][$tag][$priority][$function_to_remove]); 
     195                unset($GLOBALS['merged_filters'][$tag]); 
     196        } 
    191197 
    192         unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]); 
    193         unset($GLOBALS['merged_filters'][$tag]); 
    194  
    195198        return $r; 
    196199} 
    197200 
     
    203206 * one or more of its PHP functions are executed at these points, using the 
    204207 * Action API. 
    205208 * 
    206  * @uses add_filter() Adds an action. Parameter list and functionality are the same. 
     209 * @global array $wp_action_hooks 
     210 * @uses $merged_filters Resets the value that the tag has already been merged. 
    207211 * 
    208212 * @package WordPress 
    209213 * @subpackage Plugin 
     
    215219 * @param int $accepted_args optional. The number of arguments the function accept (default 1). 
    216220 */ 
    217221function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) { 
    218         add_filter($tag, $function_to_add, $priority, $accepted_args); 
     222        global $wp_action_hooks, $merged_filters; 
     223 
     224        $wp_filter[$tag][$priority][_wp_filter_build_unique_id($tag, $function_to_add, $priority)] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); 
     225        unset( $merged_filters[ $tag ] ); 
     226        return true; 
    219227} 
    220228 
    221229/** 
     
    241249 * @return null Will return null if $tag does not exist in $wp_filter array 
    242250 */ 
    243251function do_action($tag, $arg = '') { 
    244         global $wp_filter, $wp_actions; 
     252        global $wp_action_hooks, $wp_actions; 
    245253 
    246254        if ( is_array($wp_actions) ) 
    247                 $wp_actions[] = $tag; 
     255                $wp_action_hooks[] = $tag; 
    248256        else 
    249                 $wp_actions = array($tag); 
     257                $wp_action_hooks = array($tag); 
    250258 
    251259        $args = array(); 
    252260        if ( is_array($arg) && 1 == count($arg) && is_object($arg[0]) ) // array(&$this) 
     
    256264        for ( $a = 2; $a < func_num_args(); $a++ ) 
    257265                $args[] = func_get_arg($a); 
    258266 
    259         merge_filters($tag); 
     267        if ( !isset( $merged_filters[ $tag ] ) ) 
     268                merge_filters($tag, 'action'); 
    260269 
    261         if ( !isset($wp_filter[$tag]) ) 
     270        if ( !isset($wp_action_hooks[$tag]) ) 
    262271                return; 
    263272 
    264273        do{ 
    265                 foreach( (array) current($wp_filter[$tag]) as $the_ ) 
     274                foreach( (array) current($wp_action_hooks[$tag]) as $the_ ) 
    266275                        if ( !is_null($the_['function']) ) 
    267276                                call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); 
    268277 
    269         } while ( next($wp_filter[$tag]) !== false ); 
     278        } while ( next($wp_action_hooks[$tag]) !== false ); 
    270279 
    271280} 
    272281 
     
    309318 * @return null Will return null if $tag does not exist in $wp_filter array 
    310319 */ 
    311320function do_action_ref_array($tag, $args) { 
    312         global $wp_filter, $wp_actions; 
     321        global $wp_action_hooks, $wp_actions; 
    313322 
    314323        if ( !is_array($wp_actions) ) 
    315                 $wp_actions = array($tag); 
     324                $wp_action_hooks = array($tag); 
    316325        else 
    317                 $wp_actions[] = $tag; 
     326                $wp_action_hooks[] = $tag; 
    318327 
    319         merge_filters($tag); 
     328        if ( !isset( $merged_filters[ $tag ] ) ) 
     329                merge_filters($tag, 'action'); 
    320330 
    321         if ( !isset($wp_filter[$tag]) ) 
     331        if ( !isset($wp_action_hooks[$tag]) ) 
    322332                return; 
    323333 
    324334        do{ 
    325                 foreach( (array) current($wp_filter[$tag]) as $the_ ) 
     335                foreach( (array) current($wp_action_hooks[$tag]) as $the_ ) 
    326336                        if ( !is_null($the_['function']) ) 
    327337                                call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); 
    328338 
     
    350360 * @return boolean Whether the function is removed. 
    351361 */ 
    352362function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) { 
    353         return remove_filter($tag, $function_to_remove, $priority, $accepted_args); 
     363        $r = isset($GLOBALS['wp_action_hooks'][$tag][$priority][_wp_filter_build_unique_id($tag, $function_to_remove, $priority)]); 
     364 
     365        if( true === $r) { 
     366                unset($GLOBALS['wp_action_hooks'][$tag][$priority][$function_to_remove]); 
     367                unset($GLOBALS['merged_filters'][$tag]); 
     368        } 
     369 
     370        return $r; 
    354371} 
    355372 
    356373// 
     
    461478 * @param int $priority Used in counting how many hooks were applied 
    462479 * @return string Unique ID for usage as array key 
    463480 */ 
    464 function _wp_filter_build_unique_id($tag, $function, $priority = 10) 
    465 { 
     481function _wp_filter_build_unique_id($tag, $function, $priority = 10) { 
    466482        global $wp_filter; 
    467483 
    468484        // If function then just skip all of the tests and not overwrite the following. 
    469485        if( is_string($function) ) 
    470486                return $function; 
     487        // Static Calling 
     488        else if( is_string($function[0]) ) 
     489                return $function[0].$function[1]; 
    471490        // Object Class Calling 
    472491        else if(is_object($function[0]) ) 
    473492        { 
    474493                $obj_idx = get_class($function[0]).$function[1]; 
    475                 if( is_null($function[0]->wp_filter_id) ) { // This should be instead of is_null() change to !isset() to fix notice 
     494                if( !isset($function[0]->wp_filter_id) ) { 
    476495                        $count = count((array)$wp_filter[$tag][$priority]); 
    477496                        $function[0]->wp_filter_id = $count; 
    478497                        $obj_idx .= $count; 
     
    481500                        $obj_idx .= $function[0]->wp_filter_id; 
    482501                return $obj_idx; 
    483502        } 
    484         // Static Calling 
    485         else if( is_string($function[0]) ) 
    486                 return $function[0].$function[1]; 
    487503} 
    488504 
    489 ?> 
    490  No newline at end of file 
     505?>