Make WordPress Core

Ticket #14789: 14789.2.patch

File 14789.2.patch, 6.4 KB (added by hakre, 15 years ago)

Basically filters and actions are hooks and the same.

  • wp-includes/plugin.php

     
    132132 * @return mixed The filtered value after all hooked functions are applied to it.
    133133 */
    134134function apply_filters($tag, $value) {
    135         global $wp_filter, $merged_filters, $wp_current_filter;
    136 
    137         $args = array();
    138         $wp_current_filter[] = $tag;
    139 
    140         // Do 'all' actions first
    141         if ( isset($wp_filter['all']) ) {
    142                 $args = func_get_args();
    143                 _wp_call_all_hook($args);
    144         }
    145 
    146         if ( !isset($wp_filter[$tag]) ) {
    147                 array_pop($wp_current_filter);
    148                 return $value;
    149         }
    150 
    151         // Sort
    152         if ( !isset( $merged_filters[ $tag ] ) ) {
    153                 ksort($wp_filter[$tag]);
    154                 $merged_filters[ $tag ] = true;
    155         }
    156 
    157         reset( $wp_filter[ $tag ] );
    158 
    159         if ( empty($args) )
    160                 $args = func_get_args();
    161 
    162         do {
    163                 foreach( (array) current($wp_filter[$tag]) as $the_ )
    164                         if ( !is_null($the_['function']) ){
    165                                 $args[1] = $value;
    166                                 $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
    167                         }
    168 
    169         } while ( next($wp_filter[$tag]) !== false );
    170 
    171         array_pop( $wp_current_filter );
    172 
    173         return $value;
     135        $args = func_get_args();
     136        $tag  = array_shift( $args );
     137        return _wp_call_hook(true, $tag, $args);
    174138}
    175139
    176140/**
     
    191155 * @return mixed The filtered value after all hooked functions are applied to it.
    192156 */
    193157function apply_filters_ref_array($tag, $args) {
    194         global $wp_filter, $merged_filters, $wp_current_filter;
    195 
    196         $wp_current_filter[] = $tag;
    197 
    198         // Do 'all' actions first
    199         if ( isset($wp_filter['all']) ) {
    200                 $all_args = func_get_args();
    201                 _wp_call_all_hook($all_args);
    202         }
    203 
    204         if ( !isset($wp_filter[$tag]) ) {
    205                 array_pop($wp_current_filter);
    206                 return $args[0];
    207         }
    208 
    209         // Sort
    210         if ( !isset( $merged_filters[ $tag ] ) ) {
    211                 ksort($wp_filter[$tag]);
    212                 $merged_filters[ $tag ] = true;
    213         }
    214 
    215         reset( $wp_filter[ $tag ] );
    216 
    217         do {
    218                 foreach( (array) current($wp_filter[$tag]) as $the_ )
    219                         if ( !is_null($the_['function']) )
    220                                 $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
    221 
    222         } while ( next($wp_filter[$tag]) !== false );
    223 
    224         array_pop( $wp_current_filter );
    225 
    226         return $args[0];
     158        return _wp_call_hook( true, $tag, $args );
    227159}
    228160
    229161/**
     
    350282 * @return null Will return null if $tag does not exist in $wp_filter array
    351283 */
    352284function do_action($tag, $arg = '') {
    353         global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
    354 
    355         if ( ! isset($wp_actions) )
    356                 $wp_actions = array();
    357 
    358         if ( ! isset($wp_actions[$tag]) )
    359                 $wp_actions[$tag] = 1;
    360         else
    361                 ++$wp_actions[$tag];
    362 
    363         $wp_current_filter[] = $tag;
    364 
    365         // Do 'all' actions first
    366         if ( isset($wp_filter['all']) ) {
    367                 $all_args = func_get_args();
    368                 _wp_call_all_hook($all_args);
    369         }
    370 
    371         if ( !isset($wp_filter[$tag]) ) {
    372                 array_pop($wp_current_filter);
    373                 return;
    374         }
    375 
    376         $args = array();
    377         if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
    378                 $args[] =& $arg[0];
    379         else
    380                 $args[] = $arg;
    381         for ( $a = 2; $a < func_num_args(); $a++ )
    382                 $args[] = func_get_arg($a);
    383 
    384         // Sort
    385         if ( !isset( $merged_filters[ $tag ] ) ) {
    386                 ksort($wp_filter[$tag]);
    387                 $merged_filters[ $tag ] = true;
    388         }
    389 
    390         reset( $wp_filter[ $tag ] );
    391 
    392         do {
    393                 foreach ( (array) current($wp_filter[$tag]) as $the_ )
    394                         if ( !is_null($the_['function']) )
    395                                 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
    396 
    397         } while ( next($wp_filter[$tag]) !== false );
    398 
    399         array_pop($wp_current_filter);
     285        $args = func_get_args();
     286        $tag  = array_shift( $args );
     287        _wp_call_hook(false, $tag, $args);
    400288}
    401289
    402290/**
     
    436324 * @return null Will return null if $tag does not exist in $wp_filter array
    437325 */
    438326function do_action_ref_array($tag, $args) {
    439         global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
    440 
    441         if ( ! isset($wp_actions) )
    442                 $wp_actions = array();
    443 
    444         if ( ! isset($wp_actions[$tag]) )
    445                 $wp_actions[$tag] = 1;
    446         else
    447                 ++$wp_actions[$tag];
    448 
    449         $wp_current_filter[] = $tag;
    450 
    451         // Do 'all' actions first
    452         if ( isset($wp_filter['all']) ) {
    453                 $all_args = func_get_args();
    454                 _wp_call_all_hook($all_args);
    455         }
    456 
    457         if ( !isset($wp_filter[$tag]) ) {
    458                 array_pop($wp_current_filter);
    459                 return;
    460         }
    461 
    462         // Sort
    463         if ( !isset( $merged_filters[ $tag ] ) ) {
    464                 ksort($wp_filter[$tag]);
    465                 $merged_filters[ $tag ] = true;
    466         }
    467 
    468         reset( $wp_filter[ $tag ] );
    469 
    470         do {
    471                 foreach( (array) current($wp_filter[$tag]) as $the_ )
    472                         if ( !is_null($the_['function']) )
    473                                 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
    474 
    475         } while ( next($wp_filter[$tag]) !== false );
    476 
    477         array_pop($wp_current_filter);
     327        _wp_call_hook( false, $tag, $args );
    478328}
    479329
    480330/**
     
    701551}
    702552
    703553/**
     554 * Call a hook
     555 *
     556 * @since 3.1
     557 * @access private
     558 * @param bool   $filter True: apply_filter, False: do_action
     559 * @param string $tag The name of the filter hook.
     560 * @param array  $args The arguments supplied to the functions hooked on <tt>$tag</tt>
     561 * @return mixed first element of $args, filtered if applicable
     562 */
     563function _wp_call_hook($filter, $tag, $args) {
     564        global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
     565       
     566        $filter || isset( $wp_actions[$tag] ) && $wp_actions[$tag]++ || $wp_actions[$tag] = 1;
     567
     568        $wp_current_filter[] = $tag; // hook stack
     569
     570        // Do 'all' actions first
     571        isset( $wp_filter['all'] ) && _wp_call_all_hook( array_merge( array($tag), $args ) );
     572
     573        if ( isset( $wp_filter[$tag] ) ) {
     574                // Sort
     575                isset( $merged_filters[$tag] ) || $merged_filters[$tag] = ksort( $wp_filter[$tag] );
     576
     577                for ( $go = reset( $wp_filter[$tag] ); false !== $go ; $go = next( $wp_filter[$tag] ) ) {
     578                        foreach( (array) current( $wp_filter[$tag] ) as $the_ ) {
     579                                if ( is_null( $the_['function'] ) )
     580                                        continue;
     581                               
     582                                $callback  = $the_['function'];
     583                                $count     = max( 0, (int) $the_['accepted_args'] );
     584                                $parameter = array_slice( $args, 0, $count );
     585
     586                                $filter
     587                                ? $args[0] = call_user_func_array( $callback, $parameter)
     588                                : call_user_func_array( $callback, $parameter);
     589                        }
     590                }
     591        }
     592
     593        array_pop( $wp_current_filter ); // hook stack
     594        return isset( $args[0] ) ? $args[0] : null;
     595}
     596
     597/**
    704598 * Build Unique ID for storage and retrieval.
    705599 *
    706600 * The old way to serialize the callback caused issues and this function is the