Ticket #5231: plugin.#5231.r6312.take3.diff
File plugin.#5231.r6312.take3.diff, 8.7 KB (added by , 17 years ago) |
---|
-
plugin.php
59 59 * @package WordPress 60 60 * @subpackage Plugin 61 61 * @since 1.5 62 * @global array $wp_filter Stores all of the filters added in the form of63 * 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)]'] 64 64 * @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. 65 65 * 66 66 * @param string $tag The name of the filter to hook the <tt>$function_to_add</tt> to. … … 70 70 * @return boolean true 71 71 */ 72 72 function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) { 73 global $wp_filter , $merged_filters;73 global $wp_filter_hooks, $merged_filters; 74 74 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); 77 76 unset( $merged_filters[ $tag ] ); 78 77 return true; 79 78 } … … 107 106 * @return string The text in <tt>$string</tt> after all hooked functions are applied to it. 108 107 */ 109 108 function apply_filters($tag, $value) { 110 global $wp_filter , $merged_filters;109 global $wp_filter_hooks, $merged_filters; 111 110 112 111 if ( !isset( $merged_filters[ $tag ] ) ) 113 merge_filters($tag );112 merge_filters($tag, 'filter'); 114 113 115 114 if ( !isset($wp_filter[$tag]) ) 116 115 return $value; 117 116 118 reset( $wp_filter [ $tag ] );117 reset( $wp_filter_hooks[ $tag ] ); 119 118 120 119 $args = func_get_args(); 121 120 122 121 do{ 123 foreach( (array) current($wp_filter [$tag]) as $the_ )122 foreach( (array) current($wp_filter_hooks[$tag]) as $the_ ) 124 123 if ( !is_null($the_['function']) ){ 125 124 $args[1] = $value; 126 125 $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args'])); 127 126 } 128 127 129 } while ( next($wp_filter [$tag]) !== false );128 } while ( next($wp_filter_hooks[$tag]) !== false ); 130 129 131 130 return $value; 132 131 } … … 149 148 * @global array $merge_filters Merges the filter hooks using this function. 150 149 * 151 150 * @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' 152 152 */ 153 function merge_filters($tag ) {154 global $ wp_filter, $merged_filters;153 function merge_filters($tag, $caller='filter') { 154 global $merged_filters; 155 155 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]; 158 161 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"); 162 165 } 163 166 $merged_filters[ $tag ] = true; 164 167 } … … 185 188 * @return boolean Whether the function existed before it was removed. 186 189 */ 187 190 function 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)]); 189 192 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 } 191 197 192 unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);193 unset($GLOBALS['merged_filters'][$tag]);194 195 198 return $r; 196 199 } 197 200 … … 203 206 * one or more of its PHP functions are executed at these points, using the 204 207 * Action API. 205 208 * 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. 207 211 * 208 212 * @package WordPress 209 213 * @subpackage Plugin … … 215 219 * @param int $accepted_args optional. The number of arguments the function accept (default 1). 216 220 */ 217 221 function 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; 219 227 } 220 228 221 229 /** … … 241 249 * @return null Will return null if $tag does not exist in $wp_filter array 242 250 */ 243 251 function do_action($tag, $arg = '') { 244 global $wp_ filter, $wp_actions;252 global $wp_action_hooks, $wp_actions; 245 253 246 254 if ( is_array($wp_actions) ) 247 $wp_action s[] = $tag;255 $wp_action_hooks[] = $tag; 248 256 else 249 $wp_action s = array($tag);257 $wp_action_hooks = array($tag); 250 258 251 259 $args = array(); 252 260 if ( is_array($arg) && 1 == count($arg) && is_object($arg[0]) ) // array(&$this) … … 256 264 for ( $a = 2; $a < func_num_args(); $a++ ) 257 265 $args[] = func_get_arg($a); 258 266 259 merge_filters($tag); 267 if ( !isset( $merged_filters[ $tag ] ) ) 268 merge_filters($tag, 'action'); 260 269 261 if ( !isset($wp_ filter[$tag]) )270 if ( !isset($wp_action_hooks[$tag]) ) 262 271 return; 263 272 264 273 do{ 265 foreach( (array) current($wp_ filter[$tag]) as $the_ )274 foreach( (array) current($wp_action_hooks[$tag]) as $the_ ) 266 275 if ( !is_null($the_['function']) ) 267 276 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); 268 277 269 } while ( next($wp_ filter[$tag]) !== false );278 } while ( next($wp_action_hooks[$tag]) !== false ); 270 279 271 280 } 272 281 … … 309 318 * @return null Will return null if $tag does not exist in $wp_filter array 310 319 */ 311 320 function do_action_ref_array($tag, $args) { 312 global $wp_ filter, $wp_actions;321 global $wp_action_hooks, $wp_actions; 313 322 314 323 if ( !is_array($wp_actions) ) 315 $wp_action s = array($tag);324 $wp_action_hooks = array($tag); 316 325 else 317 $wp_action s[] = $tag;326 $wp_action_hooks[] = $tag; 318 327 319 merge_filters($tag); 328 if ( !isset( $merged_filters[ $tag ] ) ) 329 merge_filters($tag, 'action'); 320 330 321 if ( !isset($wp_ filter[$tag]) )331 if ( !isset($wp_action_hooks[$tag]) ) 322 332 return; 323 333 324 334 do{ 325 foreach( (array) current($wp_ filter[$tag]) as $the_ )335 foreach( (array) current($wp_action_hooks[$tag]) as $the_ ) 326 336 if ( !is_null($the_['function']) ) 327 337 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); 328 338 … … 350 360 * @return boolean Whether the function is removed. 351 361 */ 352 362 function 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; 354 371 } 355 372 356 373 // … … 461 478 * @param int $priority Used in counting how many hooks were applied 462 479 * @return string Unique ID for usage as array key 463 480 */ 464 function _wp_filter_build_unique_id($tag, $function, $priority = 10) 465 { 481 function _wp_filter_build_unique_id($tag, $function, $priority = 10) { 466 482 global $wp_filter; 467 483 468 484 // If function then just skip all of the tests and not overwrite the following. 469 485 if( is_string($function) ) 470 486 return $function; 487 // Static Calling 488 else if( is_string($function[0]) ) 489 return $function[0].$function[1]; 471 490 // Object Class Calling 472 491 else if(is_object($function[0]) ) 473 492 { 474 493 $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 notice494 if( !isset($function[0]->wp_filter_id) ) { 476 495 $count = count((array)$wp_filter[$tag][$priority]); 477 496 $function[0]->wp_filter_id = $count; 478 497 $obj_idx .= $count; … … 481 500 $obj_idx .= $function[0]->wp_filter_id; 482 501 return $obj_idx; 483 502 } 484 // Static Calling485 else if( is_string($function[0]) )486 return $function[0].$function[1];487 503 } 488 504 489 ?> 490 No newline at end of file 505 ?>