Make WordPress Core

Ticket #5231: filtaction.diff

File filtaction.diff, 9.2 KB (added by ryan, 16 years ago)
  • wp-includes/plugin.php

     
    7272function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
    7373        global $wp_filter, $merged_filters;
    7474
    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);
    7676        $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
    7777        unset( $merged_filters[ $tag ] );
    7878        return true;
     
    9696        if ( false === $function_to_check || false == $has )
    9797                return $has;
    9898
    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) )
    100100                return false;
    101101
    102102        foreach ( array_keys($wp_filter[$tag]) as $priority ) {
     
    138138function apply_filters($tag, $value) {
    139139        global $wp_filter, $merged_filters, $wp_current_filter;
    140140
     141        $args = array();
    141142        @$wp_current_filter[] = $tag;
    142143
    143144        // Do 'all' actions first
    144145        if ( isset($wp_filter['all']) ) {
    145146                reset( $wp_filter['all'] );
     147                $args = func_get_args();
    146148                do {
    147149                        foreach ( (array) current($wp_filter['all']) as $the_ )
    148150                                if ( !is_null($the_['function']) )
    149                                         $value = call_user_func_array($the_['function'], $value);
     151                                        $value = call_user_func_array($the_['function'], $args);
    150152
    151153                } while ( next($wp_filter['all']) !== false );
    152154        }
     
    165167
    166168        reset( $wp_filter[ $tag ] );
    167169
    168         $args = func_get_args();
     170        if ( empty($args) )
     171                $args = func_get_args();
    169172
    170173        do {
    171174                foreach( (array) current($wp_filter[$tag]) as $the_ )
     
    203206 * @return boolean Whether the function existed before it was removed.
    204207 */
    205208function 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);
    207210
    208211        $r = isset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
    209212
     
    247250 * @param int $accepted_args optional. The number of arguments the function accept (default 1).
    248251 */
    249252function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
    250         global $wp_action, $merged_actions;
     253        global $wp_filter, $merged_filters;
    251254
    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 ] );
    255258        return true;
    256259}
    257260
     
    279282 * @return null Will return null if $tag does not exist in $wp_filter array
    280283 */
    281284function do_action($tag, $arg = '') {
    282         global $wp_action, $wp_actions, $wp_current_filter;
     285        global $wp_filter, $wp_actions, $wp_current_filter;
    283286
    284287        if ( is_array($wp_actions) )
    285288                $wp_actions[] = $tag;
    286289        else
    287290                $wp_actions = array($tag);
    288291
    289         $args = array();
    290         if ( is_array($arg) && 1 == count($arg) && is_object($arg[0]) ) // array(&$this)
    291                 $args[] =& $arg[0];
    292         else
    293                 $args[] = $arg;
    294         for ( $a = 2; $a < func_num_args(); $a++ )
    295                 $args[] = func_get_arg($a);
    296 
    297292        @$wp_current_filter[] = $tag;
    298293
    299294        // 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();
    301298                do {
    302                         foreach( (array) current($wp_action['all']) as $the_ )
     299                        foreach( (array) current($wp_filter['all']) as $the_ )
    303300                                if ( !is_null($the_['function']) )
    304                                         call_user_func_array($the_['function'], $args[0]);
     301                                        call_user_func_array($the_['function'], $all_args);
    305302
    306                 } while ( next($wp_action['all']) !== false );
     303                } while ( next($wp_filter['all']) !== false );
    307304        }
    308305
    309         if ( !isset($wp_action[$tag]) ) {
     306        if ( !isset($wp_filter[$tag]) ) {
    310307                array_pop($wp_current_filter);
    311308                return;
    312309        }
    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
    314319        // 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;
    319324        }
    320325
    321         reset( $wp_action[ $tag ] );
     326        reset( $wp_filter[ $tag ] );
    322327
    323328        do {
    324                 foreach ( (array) current($wp_action[$tag]) as $the_ )
     329                foreach ( (array) current($wp_filter[$tag]) as $the_ )
    325330                        if ( !is_null($the_['function']) )
    326331                                call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
    327332
    328         } while ( next($wp_action[$tag]) !== false );
     333        } while ( next($wp_filter[$tag]) !== false );
    329334
    330335                array_pop($wp_current_filter);
    331336}
     
    367372 * @return null Will return null if $tag does not exist in $wp_filter array
    368373 */
    369374function do_action_ref_array($tag, $args) {
    370         global $wp_action, $wp_actions;
     375        global $wp_filter, $wp_actions;
    371376
    372377        if ( !is_array($wp_actions) )
    373378                $wp_actions = array($tag);
     
    375380                $wp_actions[] = $tag;
    376381
    377382        // 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();
    379386                do {
    380                         foreach( (array) current($wp_action['all']) as $the_ )
     387                        foreach( (array) current($wp_filter['all']) as $the_ )
    381388                                if ( !is_null($the_['function']) )
    382                                         call_user_func_array($the_['function'], $args[0]);
     389                                        call_user_func_array($the_['function'], $all_args);
    383390
    384                 } while ( next($wp_action['all']) !== false );
     391                } while ( next($wp_filter['all']) !== false );
    385392        }
    386393
    387         if ( !isset($wp_action[$tag]) )
     394        if ( !isset($wp_filter[$tag]) )
    388395                return;
    389396
    390397        // 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;
    395402        }
    396403
    397         reset( $wp_action[ $tag ] );
     404        reset( $wp_filter[ $tag ] );
    398405
    399406        do {
    400                 foreach( (array) current($wp_action[$tag]) as $the_ )
     407                foreach( (array) current($wp_filter[$tag]) as $the_ )
    401408                        if ( !is_null($the_['function']) )
    402409                                call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
    403410
    404         } while ( next($wp_action[$tag]) !== false );
     411        } while ( next($wp_filter[$tag]) !== false );
    405412
    406413}
    407414
     
    410417 * @package WordPress
    411418 * @subpackage Plugin
    412419 * @since 2.4
    413  * @global array $wp_action Stores all of the actions
     420 * @global array $wp_filter Stores all of the actions
    414421 *
    415422 * @param string $tag The name of the action hook.
    416423 * @param callback $function_to_check optional.  If specified, return the priority of that function on this hook or false if not attached.
    417424 * @return int|boolean
    418425 */
    419426function 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);
    435428}
    436429
    437430/**
     
    452445 * @return boolean Whether the function is removed.
    453446 */
    454447function 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);
    467449}
    468450
    469451//
     
    575557 * @param string $type filter or action
    576558 * @return string Unique ID for usage as array key
    577559 */
    578 function _wp_filter_build_unique_id($tag, $function, $priority, $type)
     560function _wp_filter_build_unique_id($tag, $function, $priority)
    579561{
    580         global $wp_filter, $wp_action;
     562        global $wp_filter;
    581563
    582564        // If function then just skip all of the tests and not overwrite the following.
    583565        if ( is_string($function) )
     
    588570                if ( !isset($function[0]->wp_filter_id) ) {
    589571                        if ( false === $priority )
    590572                                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]);
    595574                        $function[0]->wp_filter_id = $count;
    596575                        $obj_idx .= $count;
    597576                        unset($count);