Ticket #5231: filtaction.diff
File filtaction.diff, 9.2 KB (added by , 16 years ago) |
---|
-
wp-includes/plugin.php
72 72 function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) { 73 73 global $wp_filter, $merged_filters; 74 74 75 $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority , 'filter');75 $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority); 76 76 $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); 77 77 unset( $merged_filters[ $tag ] ); 78 78 return true; … … 96 96 if ( false === $function_to_check || false == $has ) 97 97 return $has; 98 98 99 if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false , 'filter') )99 if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) ) 100 100 return false; 101 101 102 102 foreach ( array_keys($wp_filter[$tag]) as $priority ) { … … 138 138 function apply_filters($tag, $value) { 139 139 global $wp_filter, $merged_filters, $wp_current_filter; 140 140 141 $args = array(); 141 142 @$wp_current_filter[] = $tag; 142 143 143 144 // Do 'all' actions first 144 145 if ( isset($wp_filter['all']) ) { 145 146 reset( $wp_filter['all'] ); 147 $args = func_get_args(); 146 148 do { 147 149 foreach ( (array) current($wp_filter['all']) as $the_ ) 148 150 if ( !is_null($the_['function']) ) 149 $value = call_user_func_array($the_['function'], $ value);151 $value = call_user_func_array($the_['function'], $args); 150 152 151 153 } while ( next($wp_filter['all']) !== false ); 152 154 } … … 165 167 166 168 reset( $wp_filter[ $tag ] ); 167 169 168 $args = func_get_args(); 170 if ( empty($args) ) 171 $args = func_get_args(); 169 172 170 173 do { 171 174 foreach( (array) current($wp_filter[$tag]) as $the_ ) … … 203 206 * @return boolean Whether the function existed before it was removed. 204 207 */ 205 208 function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) { 206 $function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority , 'filter');209 $function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority); 207 210 208 211 $r = isset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]); 209 212 … … 247 250 * @param int $accepted_args optional. The number of arguments the function accept (default 1). 248 251 */ 249 252 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) { 250 global $wp_ action, $merged_actions;253 global $wp_filter, $merged_filters; 251 254 252 $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority , 'action');253 $wp_ action[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);254 unset( $merged_ actions[ $tag ] );255 $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority); 256 $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); 257 unset( $merged_filters[ $tag ] ); 255 258 return true; 256 259 } 257 260 … … 279 282 * @return null Will return null if $tag does not exist in $wp_filter array 280 283 */ 281 284 function do_action($tag, $arg = '') { 282 global $wp_ action, $wp_actions, $wp_current_filter;285 global $wp_filter, $wp_actions, $wp_current_filter; 283 286 284 287 if ( is_array($wp_actions) ) 285 288 $wp_actions[] = $tag; 286 289 else 287 290 $wp_actions = array($tag); 288 291 289 $args = array();290 if ( is_array($arg) && 1 == count($arg) && is_object($arg[0]) ) // array(&$this)291 $args[] =& $arg[0];292 else293 $args[] = $arg;294 for ( $a = 2; $a < func_num_args(); $a++ )295 $args[] = func_get_arg($a);296 297 292 @$wp_current_filter[] = $tag; 298 293 299 294 // Do 'all' actions first 300 if ( isset($wp_action['all']) ) { 295 if ( isset($wp_filter['all']) ) { 296 reset( $wp_filter['all'] ); 297 $all_args = func_get_args(); 301 298 do { 302 foreach( (array) current($wp_ action['all']) as $the_ )299 foreach( (array) current($wp_filter['all']) as $the_ ) 303 300 if ( !is_null($the_['function']) ) 304 call_user_func_array($the_['function'], $a rgs[0]);301 call_user_func_array($the_['function'], $all_args); 305 302 306 } while ( next($wp_ action['all']) !== false );303 } while ( next($wp_filter['all']) !== false ); 307 304 } 308 305 309 if ( !isset($wp_ action[$tag]) ) {306 if ( !isset($wp_filter[$tag]) ) { 310 307 array_pop($wp_current_filter); 311 308 return; 312 309 } 313 310 311 $args = array(); 312 if ( is_array($arg) && 1 == count($arg) && is_object($arg[0]) ) // array(&$this) 313 $args[] =& $arg[0]; 314 else 315 $args[] = $arg; 316 for ( $a = 2; $a < func_num_args(); $a++ ) 317 $args[] = func_get_arg($a); 318 314 319 // Sort 315 if ( !isset( $merged_ actions[ $tag ] ) ) {316 reset($wp_ action[$tag]);317 uksort($wp_ action[$tag], "strnatcasecmp");318 $merged_ actions[ $tag ] = true;320 if ( !isset( $merged_filters[ $tag ] ) ) { 321 reset($wp_filter[$tag]); 322 uksort($wp_filter[$tag], "strnatcasecmp"); 323 $merged_filters[ $tag ] = true; 319 324 } 320 325 321 reset( $wp_ action[ $tag ] );326 reset( $wp_filter[ $tag ] ); 322 327 323 328 do { 324 foreach ( (array) current($wp_ action[$tag]) as $the_ )329 foreach ( (array) current($wp_filter[$tag]) as $the_ ) 325 330 if ( !is_null($the_['function']) ) 326 331 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); 327 332 328 } while ( next($wp_ action[$tag]) !== false );333 } while ( next($wp_filter[$tag]) !== false ); 329 334 330 335 array_pop($wp_current_filter); 331 336 } … … 367 372 * @return null Will return null if $tag does not exist in $wp_filter array 368 373 */ 369 374 function do_action_ref_array($tag, $args) { 370 global $wp_ action, $wp_actions;375 global $wp_filter, $wp_actions; 371 376 372 377 if ( !is_array($wp_actions) ) 373 378 $wp_actions = array($tag); … … 375 380 $wp_actions[] = $tag; 376 381 377 382 // Do 'all' actions first 378 if ( isset($wp_action['all']) ) { 383 if ( isset($wp_filter['all']) ) { 384 reset( $wp_filter['all'] ); 385 $all_args = func_get_args(); 379 386 do { 380 foreach( (array) current($wp_ action['all']) as $the_ )387 foreach( (array) current($wp_filter['all']) as $the_ ) 381 388 if ( !is_null($the_['function']) ) 382 call_user_func_array($the_['function'], $a rgs[0]);389 call_user_func_array($the_['function'], $all_args); 383 390 384 } while ( next($wp_ action['all']) !== false );391 } while ( next($wp_filter['all']) !== false ); 385 392 } 386 393 387 if ( !isset($wp_ action[$tag]) )394 if ( !isset($wp_filter[$tag]) ) 388 395 return; 389 396 390 397 // Sort 391 if ( !isset( $merged_ actions[ $tag ] ) ) {392 reset($wp_ action[$tag]);393 uksort($wp_ action[$tag], "strnatcasecmp");394 $merged_ actions[ $tag ] = true;398 if ( !isset( $merged_filters[ $tag ] ) ) { 399 reset($wp_filter[$tag]); 400 uksort($wp_filter[$tag], "strnatcasecmp"); 401 $merged_filters[ $tag ] = true; 395 402 } 396 403 397 reset( $wp_ action[ $tag ] );404 reset( $wp_filter[ $tag ] ); 398 405 399 406 do { 400 foreach( (array) current($wp_ action[$tag]) as $the_ )407 foreach( (array) current($wp_filter[$tag]) as $the_ ) 401 408 if ( !is_null($the_['function']) ) 402 409 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); 403 410 404 } while ( next($wp_ action[$tag]) !== false );411 } while ( next($wp_filter[$tag]) !== false ); 405 412 406 413 } 407 414 … … 410 417 * @package WordPress 411 418 * @subpackage Plugin 412 419 * @since 2.4 413 * @global array $wp_ actionStores all of the actions420 * @global array $wp_filter Stores all of the actions 414 421 * 415 422 * @param string $tag The name of the action hook. 416 423 * @param callback $function_to_check optional. If specified, return the priority of that function on this hook or false if not attached. 417 424 * @return int|boolean 418 425 */ 419 426 function has_action($tag, $function_to_check = false) { 420 global $wp_action; 421 422 $has = !empty($wp_action[$tag]); 423 if ( false === $function_to_check || false == $has ) 424 return $has; 425 426 if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false, 'action') ) 427 return false; 428 429 foreach ( array_keys($wp_action[$tag]) as $priority ) { 430 if ( isset($wp_action[$tag][$priority][$idx]) ) 431 return $priority; 432 } 433 434 return false; 427 return has_filter($tag, $function_to_check); 435 428 } 436 429 437 430 /** … … 452 445 * @return boolean Whether the function is removed. 453 446 */ 454 447 function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) { 455 $function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority, 'action'); 456 457 $r = isset($GLOBALS['wp_action'][$tag][$priority][$function_to_remove]); 458 459 if ( true === $r) { 460 unset($GLOBALS['wp_action'][$tag][$priority][$function_to_remove]); 461 if ( empty($GLOBALS['wp_action'][$tag][$priority]) ) 462 unset($GLOBALS['wp_action'][$tag][$priority]); 463 unset($GLOBALS['merged_actions'][$tag]); 464 } 465 466 return $r; 448 return remove_filter($tag, $function_to_remove, $priority, $accepted_args); 467 449 } 468 450 469 451 // … … 575 557 * @param string $type filter or action 576 558 * @return string Unique ID for usage as array key 577 559 */ 578 function _wp_filter_build_unique_id($tag, $function, $priority , $type)560 function _wp_filter_build_unique_id($tag, $function, $priority) 579 561 { 580 global $wp_filter , $wp_action;562 global $wp_filter; 581 563 582 564 // If function then just skip all of the tests and not overwrite the following. 583 565 if ( is_string($function) ) … … 588 570 if ( !isset($function[0]->wp_filter_id) ) { 589 571 if ( false === $priority ) 590 572 return false; 591 if ( 'filter' == $type ) 592 $count = count((array)$wp_filter[$tag][$priority]); 593 else 594 $count = count((array)$wp_action[$tag][$priority]); 573 $count = count((array)$wp_filter[$tag][$priority]); 595 574 $function[0]->wp_filter_id = $count; 596 575 $obj_idx .= $count; 597 576 unset($count);