Ticket #26826: plugin.php.patch
File plugin.php.patch, 18.3 KB (added by , 11 years ago) |
---|
-
plugin.php
82 82 function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) { 83 83 global $wp_filter, $merged_filters; 84 84 85 $idx = _wp_filter_build_unique_id( $tag, $function_to_add, $priority);86 $wp_filter[ $tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);85 $idx = _wp_filter_build_unique_id( $tag, $function_to_add, $priority ); 86 $wp_filter[ $tag ][ $priority ][ $idx ] = array( 'function' => $function_to_add, 'accepted_args' => $accepted_args ); 87 87 unset( $merged_filters[ $tag ] ); 88 88 return true; 89 89 } … … 103 103 * When using the $function_to_check argument, this function may return a non-boolean value that evaluates to false 104 104 * (e.g.) 0, so use the === operator for testing the return value. 105 105 */ 106 function has_filter( $tag, $function_to_check = false) {106 function has_filter( $tag, $function_to_check = false ) { 107 107 global $wp_filter; 108 108 109 $has = !empty( $wp_filter[$tag]);109 $has = !empty( $wp_filter[ $tag ] ); 110 110 if ( false === $function_to_check || false == $has ) 111 111 return $has; 112 112 113 if ( ! $idx = _wp_filter_build_unique_id($tag, $function_to_check, false) )113 if ( ! $idx = _wp_filter_build_unique_id( $tag, $function_to_check, false ) ) 114 114 return false; 115 115 116 foreach ( (array) array_keys( $wp_filter[$tag]) as $priority ) {117 if ( isset( $wp_filter[$tag][$priority][$idx]) )116 foreach ( (array) array_keys( $wp_filter[ $tag ] ) as $priority ) { 117 if ( isset( $wp_filter[ $tag ][ $priority ][ $idx ] ) ) 118 118 return $priority; 119 119 } 120 120 … … 166 166 $args = array(); 167 167 168 168 // Do 'all' actions first 169 if ( isset( $wp_filter['all']) ) {169 if ( isset( $wp_filter['all'] ) ) { 170 170 $wp_current_filter[] = $tag; 171 171 $args = func_get_args(); 172 _wp_call_all_hook( $args);172 _wp_call_all_hook( $args ); 173 173 } 174 174 175 if ( ! isset($wp_filter[$tag]) ) {176 if ( isset( $wp_filter['all']) )177 array_pop( $wp_current_filter);175 if ( ! isset( $wp_filter[ $tag ] ) ) { 176 if ( isset( $wp_filter['all'] ) ) 177 array_pop( $wp_current_filter ); 178 178 return $value; 179 179 } 180 180 181 if ( ! isset($wp_filter['all']) )181 if ( ! isset( $wp_filter['all'] ) ) 182 182 $wp_current_filter[] = $tag; 183 183 184 184 // Sort 185 if ( ! isset( $merged_filters[ $tag ] ) ) {186 ksort( $wp_filter[$tag]);185 if ( ! isset( $merged_filters[ $tag ] ) ) { 186 ksort( $wp_filter[ $tag ] ); 187 187 $merged_filters[ $tag ] = true; 188 188 } 189 189 190 190 reset( $wp_filter[ $tag ] ); 191 191 192 if ( empty( $args) )192 if ( empty( $args ) ) 193 193 $args = func_get_args(); 194 194 195 195 do { 196 foreach ( (array) current($wp_filter[$tag]) as $the_ )197 if ( ! is_null($the_['function']) ){196 foreach ( (array) current( $wp_filter[ $tag ] ) as $the_ ) 197 if ( ! is_null( $the_['function'] ) ){ 198 198 $args[1] = $value; 199 $value = call_user_func_array( $the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));199 $value = call_user_func_array( $the_['function'], array_slice( $args, 1, (int) $the_['accepted_args'] ) ); 200 200 } 201 201 202 } while ( next( $wp_filter[$tag]) !== false );202 } while ( next( $wp_filter[$tag] ) !== false ); 203 203 204 204 array_pop( $wp_current_filter ); 205 205 … … 223 223 * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt> 224 224 * @return mixed The filtered value after all hooked functions are applied to it. 225 225 */ 226 function apply_filters_ref_array( $tag, $args) {226 function apply_filters_ref_array( $tag, $args ) { 227 227 global $wp_filter, $merged_filters, $wp_current_filter; 228 228 229 229 // Do 'all' actions first 230 if ( isset( $wp_filter['all']) ) {230 if ( isset( $wp_filter['all'] ) ) { 231 231 $wp_current_filter[] = $tag; 232 232 $all_args = func_get_args(); 233 _wp_call_all_hook( $all_args);233 _wp_call_all_hook( $all_args ); 234 234 } 235 235 236 if ( ! isset($wp_filter[$tag]) ) {237 if ( isset( $wp_filter['all']) )238 array_pop( $wp_current_filter);236 if ( ! isset( $wp_filter[ $tag ] ) ) { 237 if ( isset( $wp_filter['all'] ) ) 238 array_pop( $wp_current_filter ); 239 239 return $args[0]; 240 240 } 241 241 242 if ( ! isset($wp_filter['all']) )242 if ( ! isset( $wp_filter['all'] ) ) 243 243 $wp_current_filter[] = $tag; 244 244 245 245 // Sort 246 if ( ! isset( $merged_filters[ $tag ] ) ) {247 ksort( $wp_filter[$tag]);246 if ( ! isset( $merged_filters[ $tag ] ) ) { 247 ksort( $wp_filter[ $tag ] ); 248 248 $merged_filters[ $tag ] = true; 249 249 } 250 250 … … 251 251 reset( $wp_filter[ $tag ] ); 252 252 253 253 do { 254 foreach ( (array) current($wp_filter[$tag]) as $the_ )255 if ( ! is_null($the_['function']) )256 $args[0] = call_user_func_array( $the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));254 foreach ( (array) current( $wp_filter[ $tag ] ) as $the_ ) 255 if ( ! is_null( $the_['function'] ) ) 256 $args[0] = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) ); 257 257 258 } while ( next( $wp_filter[$tag]) !== false );258 } while ( next( $wp_filter[ $tag ] ) !== false ); 259 259 260 260 array_pop( $wp_current_filter ); 261 261 … … 284 284 * @return boolean Whether the function existed before it was removed. 285 285 */ 286 286 function remove_filter( $tag, $function_to_remove, $priority = 10 ) { 287 $function_to_remove = _wp_filter_build_unique_id( $tag, $function_to_remove, $priority);287 $function_to_remove = _wp_filter_build_unique_id( $tag, $function_to_remove, $priority ); 288 288 289 $r = isset( $GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);289 $r = isset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] ); 290 290 291 291 if ( true === $r) { 292 unset( $GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);293 if ( empty( $GLOBALS['wp_filter'][$tag][$priority]) )294 unset( $GLOBALS['wp_filter'][$tag][$priority]);295 unset( $GLOBALS['merged_filters'][$tag]);292 unset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] ); 293 if ( empty( $GLOBALS['wp_filter'][ $tag ][ $priority ] ) ) 294 unset( $GLOBALS['wp_filter'][ $tag ][ $priority ] ); 295 unset( $GLOBALS['merged_filters'][ $tag ] ); 296 296 } 297 297 298 298 return $r; … … 307 307 * @param int $priority The priority number to remove. 308 308 * @return bool True when finished. 309 309 */ 310 function remove_all_filters( $tag, $priority = false) {310 function remove_all_filters( $tag, $priority = false ) { 311 311 global $wp_filter, $merged_filters; 312 312 313 if( isset( $wp_filter[$tag]) ) {314 if( false !== $priority && isset( $wp_filter[$tag][$priority]) )315 unset( $wp_filter[$tag][$priority]);313 if( isset( $wp_filter[ $tag ] ) ) { 314 if( false !== $priority && isset( $wp_filter[ $tag ][ $priority ] ) ) 315 unset( $wp_filter[ $tag ][ $priority ] ); 316 316 else 317 unset( $wp_filter[$tag]);317 unset( $wp_filter[ $tag ] ); 318 318 } 319 319 320 if( isset( $merged_filters[$tag]) )321 unset( $merged_filters[$tag]);320 if( isset( $merged_filters[ $tag ] ) ) 321 unset( $merged_filters[ $tag ] ); 322 322 323 323 return true; 324 324 } … … 356 356 * @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. 357 357 * @param int $accepted_args optional. The number of arguments the function accept (default 1). 358 358 */ 359 function add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1) {360 return add_filter( $tag, $function_to_add, $priority, $accepted_args);359 function add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) { 360 return add_filter( $tag, $function_to_add, $priority, $accepted_args ); 361 361 } 362 362 363 363 /** … … 383 383 * @param mixed $arg,... Optional additional arguments which are passed on to the functions hooked to the action. 384 384 * @return null Will return null if $tag does not exist in $wp_filter array 385 385 */ 386 function do_action( $tag, $arg = '') {386 function do_action( $tag, $arg = '' ) { 387 387 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter; 388 388 389 if ( ! isset( $wp_actions[$tag]) )390 $wp_actions[ $tag] = 1;389 if ( ! isset( $wp_actions[ $tag ] ) ) 390 $wp_actions[ $tag ] = 1; 391 391 else 392 ++$wp_actions[ $tag];392 ++$wp_actions[ $tag ]; 393 393 394 394 // Do 'all' actions first 395 if ( isset( $wp_filter['all']) ) {395 if ( isset( $wp_filter['all'] ) ) { 396 396 $wp_current_filter[] = $tag; 397 397 $all_args = func_get_args(); 398 _wp_call_all_hook( $all_args);398 _wp_call_all_hook( $all_args ); 399 399 } 400 400 401 if ( ! isset($wp_filter[$tag]) ) {402 if ( isset( $wp_filter['all']) )403 array_pop( $wp_current_filter);401 if ( ! isset( $wp_filter[ $tag ] ) ) { 402 if ( isset( $wp_filter['all'] ) ) 403 array_pop( $wp_current_filter ); 404 404 return; 405 405 } 406 406 407 if ( ! isset($wp_filter['all']) )407 if ( ! isset( $wp_filter['all'] ) ) 408 408 $wp_current_filter[] = $tag; 409 409 410 410 $args = array(); 411 if ( is_array( $arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)411 if ( is_array( $arg ) && 1 == count( $arg ) && isset( $arg[0] ) && is_object( $arg[0] ) ) // array(&$this) 412 412 $args[] =& $arg[0]; 413 413 else 414 414 $args[] = $arg; 415 415 for ( $a = 2; $a < func_num_args(); $a++ ) 416 $args[] = func_get_arg( $a);416 $args[] = func_get_arg( $a ); 417 417 418 418 // Sort 419 419 if ( !isset( $merged_filters[ $tag ] ) ) { 420 ksort( $wp_filter[$tag]);420 ksort( $wp_filter[ $tag ] ); 421 421 $merged_filters[ $tag ] = true; 422 422 } 423 423 … … 424 424 reset( $wp_filter[ $tag ] ); 425 425 426 426 do { 427 foreach ( (array) current( $wp_filter[$tag]) as $the_ )428 if ( ! is_null($the_['function']) )429 call_user_func_array( $the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));427 foreach ( (array) current( $wp_filter[ $tag ] ) as $the_ ) 428 if ( ! is_null( $the_['function'] ) ) 429 call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) ); 430 430 431 } while ( next( $wp_filter[$tag]) !== false );431 } while ( next( $wp_filter[ $tag ] ) !== false ); 432 432 433 array_pop( $wp_current_filter);433 array_pop( $wp_current_filter ); 434 434 } 435 435 436 436 /** … … 444 444 * @param string $tag The name of the action hook. 445 445 * @return int The number of times action hook <tt>$tag</tt> is fired 446 446 */ 447 function did_action( $tag) {447 function did_action( $tag ) { 448 448 global $wp_actions; 449 449 450 450 if ( ! isset( $wp_actions[ $tag ] ) ) 451 451 return 0; 452 452 453 return $wp_actions[ $tag];453 return $wp_actions[ $tag ]; 454 454 } 455 455 456 456 /** … … 469 469 * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt> 470 470 * @return null Will return null if $tag does not exist in $wp_filter array 471 471 */ 472 function do_action_ref_array( $tag, $args) {472 function do_action_ref_array( $tag, $args ) { 473 473 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter; 474 474 475 if ( ! isset( $wp_actions[$tag]) )475 if ( ! isset( $wp_actions[ $tag ] ) ) 476 476 $wp_actions[$tag] = 1; 477 477 else 478 478 ++$wp_actions[$tag]; 479 479 480 480 // Do 'all' actions first 481 if ( isset( $wp_filter['all']) ) {481 if ( isset( $wp_filter['all'] ) ) { 482 482 $wp_current_filter[] = $tag; 483 483 $all_args = func_get_args(); 484 _wp_call_all_hook( $all_args);484 _wp_call_all_hook( $all_args ); 485 485 } 486 486 487 if ( ! isset($wp_filter[$tag]) ) {488 if ( isset( $wp_filter['all']) )489 array_pop( $wp_current_filter);487 if ( ! isset( $wp_filter[ $tag ] ) ) { 488 if ( isset( $wp_filter['all'] ) ) 489 array_pop( $wp_current_filter ); 490 490 return; 491 491 } 492 492 493 if ( ! isset($wp_filter['all']) )493 if ( ! isset( $wp_filter['all'] ) ) 494 494 $wp_current_filter[] = $tag; 495 495 496 496 // Sort 497 if ( ! isset( $merged_filters[ $tag ] ) ) {498 ksort( $wp_filter[$tag]);497 if ( ! isset( $merged_filters[ $tag ] ) ) { 498 ksort( $wp_filter[ $tag ] ); 499 499 $merged_filters[ $tag ] = true; 500 500 } 501 501 … … 502 502 reset( $wp_filter[ $tag ] ); 503 503 504 504 do { 505 foreach( (array) current( $wp_filter[$tag]) as $the_ )506 if ( ! is_null($the_['function']) )507 call_user_func_array( $the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));505 foreach( (array) current( $wp_filter[ $tag ] ) as $the_ ) 506 if ( ! is_null( $the_['function'] ) ) 507 call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) ); 508 508 509 } while ( next( $wp_filter[$tag]) !== false );509 } while ( next( $wp_filter[ $tag ] ) !== false ); 510 510 511 array_pop( $wp_current_filter);511 array_pop( $wp_current_filter ); 512 512 } 513 513 514 514 /** … … 526 526 * When using the $function_to_check argument, this function may return a non-boolean value that evaluates to false 527 527 * (e.g.) 0, so use the === operator for testing the return value. 528 528 */ 529 function has_action( $tag, $function_to_check = false) {530 return has_filter( $tag, $function_to_check);529 function has_action( $tag, $function_to_check = false ) { 530 return has_filter( $tag, $function_to_check ); 531 531 } 532 532 533 533 /** … … 559 559 * @param int $priority The priority number to remove them from. 560 560 * @return bool True when finished. 561 561 */ 562 function remove_all_actions( $tag, $priority = false) {563 return remove_all_filters( $tag, $priority);562 function remove_all_actions( $tag, $priority = false ) { 563 return remove_all_filters( $tag, $priority ); 564 564 } 565 565 566 566 // … … 582 582 * @return string The name of a plugin. 583 583 * @uses WP_PLUGIN_DIR 584 584 */ 585 function plugin_basename( $file) {586 $file = str_replace( '\\','/',$file); // sanitize for Win32 installs587 $file = preg_replace( '|/+|','/', $file); // remove any duplicate slash588 $plugin_dir = str_replace( '\\','/',WP_PLUGIN_DIR); // sanitize for Win32 installs589 $plugin_dir = preg_replace( '|/+|','/', $plugin_dir); // remove any duplicate slash590 $mu_plugin_dir = str_replace( '\\','/',WPMU_PLUGIN_DIR); // sanitize for Win32 installs591 $mu_plugin_dir = preg_replace( '|/+|','/', $mu_plugin_dir); // remove any duplicate slash592 $file = preg_replace( '#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir593 $file = trim( $file, '/');585 function plugin_basename( $file ) { 586 $file = str_replace( '\\', '/', $file ); // sanitize for Win32 installs 587 $file = preg_replace( '|/+|', '/', $file ); // remove any duplicate slash 588 $plugin_dir = str_replace( '\\', '/', WP_PLUGIN_DIR ); // sanitize for Win32 installs 589 $plugin_dir = preg_replace( '|/+|', '/', $plugin_dir ); // remove any duplicate slash 590 $mu_plugin_dir = str_replace( '\\', '/', WPMU_PLUGIN_DIR ); // sanitize for Win32 installs 591 $mu_plugin_dir = preg_replace( '|/+|', '/', $mu_plugin_dir ); // remove any duplicate slash 592 $file = preg_replace( '#^' . preg_quote( $plugin_dir, '#' ) . '/|^' . preg_quote( $mu_plugin_dir, '#' ) . '/#', '', $file); // get relative path from plugins dir 593 $file = trim( $file, '/' ); 594 594 return $file; 595 595 } 596 596 … … 640 640 * @param string $file The filename of the plugin including the path. 641 641 * @param callback $function the function hooked to the 'activate_PLUGIN' action. 642 642 */ 643 function register_activation_hook( $file, $function) {644 $file = plugin_basename( $file);645 add_action( 'activate_' . $file, $function);643 function register_activation_hook( $file, $function ) { 644 $file = plugin_basename( $file ); 645 add_action( 'activate_' . $file, $function ); 646 646 } 647 647 648 648 /** … … 665 665 * @param string $file The filename of the plugin including the path. 666 666 * @param callback $function the function hooked to the 'deactivate_PLUGIN' action. 667 667 */ 668 function register_deactivation_hook( $file, $function) {669 $file = plugin_basename( $file);670 add_action( 'deactivate_' . $file, $function);668 function register_deactivation_hook( $file, $function ) { 669 $file = plugin_basename( $file ); 670 add_action( 'deactivate_' . $file, $function ); 671 671 } 672 672 673 673 /** … … 704 704 // The option should not be autoloaded, because it is not needed in most 705 705 // cases. Emphasis should be put on using the 'uninstall.php' way of 706 706 // uninstalling the plugin. 707 $uninstallable_plugins = (array) get_option( 'uninstall_plugins');708 $uninstallable_plugins[ plugin_basename($file)] = $callback;709 update_option( 'uninstall_plugins', $uninstallable_plugins);707 $uninstallable_plugins = (array) get_option( 'uninstall_plugins' ); 708 $uninstallable_plugins[ plugin_basename( $file ) ] = $callback; 709 update_option( 'uninstall_plugins', $uninstallable_plugins ); 710 710 } 711 711 712 712 /** … … 729 729 * 730 730 * @param array $args The collected parameters from the hook that was called. 731 731 */ 732 function _wp_call_all_hook( $args) {732 function _wp_call_all_hook( $args ) { 733 733 global $wp_filter; 734 734 735 735 reset( $wp_filter['all'] ); 736 736 do { 737 foreach( (array) current( $wp_filter['all']) as $the_ )738 if ( ! is_null($the_['function']) )739 call_user_func_array( $the_['function'], $args);737 foreach( (array) current( $wp_filter['all'] ) as $the_ ) 738 if ( ! is_null( $the_['function'] ) ) 739 call_user_func_array( $the_['function'], $args ); 740 740 741 } while ( next( $wp_filter['all']) !== false );741 } while ( next( $wp_filter['all'] ) !== false ); 742 742 } 743 743 744 744 /** … … 770 770 * @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. 771 771 * @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. 772 772 */ 773 function _wp_filter_build_unique_id( $tag, $function, $priority) {773 function _wp_filter_build_unique_id( $tag, $function, $priority ) { 774 774 global $wp_filter; 775 775 static $filter_id_count = 0; 776 776 777 if ( is_string( $function) )777 if ( is_string( $function ) ) 778 778 return $function; 779 779 780 if ( is_object( $function) ) {780 if ( is_object( $function ) ) { 781 781 // Closures are currently implemented as objects 782 782 $function = array( $function, '' ); 783 783 } else { … … 784 784 $function = (array) $function; 785 785 } 786 786 787 if ( is_object($function[0]) ) {787 if ( is_object( $function[0] ) ) { 788 788 // Object Class Calling 789 if ( function_exists( 'spl_object_hash') ) {790 return spl_object_hash( $function[0]) . $function[1];789 if ( function_exists( 'spl_object_hash' ) ) { 790 return spl_object_hash( $function[0] ) . $function[1]; 791 791 } else { 792 $obj_idx = get_class( $function[0]).$function[1];793 if ( ! isset($function[0]->wp_filter_id) ) {792 $obj_idx = get_class( $function[0] ) . $function[1]; 793 if ( ! isset( $function[0]->wp_filter_id ) ) { 794 794 if ( false === $priority ) 795 795 return false; 796 $obj_idx .= isset( $wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;796 $obj_idx .= isset( $wp_filter[ $tag ][ $priority ] ) ? count( (array) $wp_filter[ $tag ][ $priority ] ) : $filter_id_count; 797 797 $function[0]->wp_filter_id = $filter_id_count; 798 798 ++$filter_id_count; 799 799 } else { … … 802 802 803 803 return $obj_idx; 804 804 } 805 } else if ( is_string( $function[0]) ) {805 } else if ( is_string( $function[0] ) ) { 806 806 // Static Calling 807 807 return $function[0] . '::' . $function[1]; 808 808 }