WordPress.org

Make WordPress Core

Ticket #5338: plugin.r6328.patch

File plugin.r6328.patch, 7.8 KB (added by darkdragon, 11 years ago)

Based off of r6328 for the Plugin API

  • plugin.php

     
    7070 * @return boolean true
    7171 */
    7272function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
    73         global $wp_filter, $merged_filters;
     73        global $wp_filter;
    7474
    7575        $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);
    77         unset( $merged_filters[ $tag ] );
    7877        return true;
    7978}
    8079
     
    131130 * @global array $merge_filters Merges the filter hooks using this function.
    132131 *
    133132 * @param string $tag The name of the filter hook.
    134  * @param string $value The value on which the filters hooked to <tt>$tag</tt> are applied on.
     133 * @param mixed $value The value on which the filters hooked to <tt>$tag</tt> are applied on.
    135134 * @param mixed $var,... Additional variables passed to the functions hooked to <tt>$tag</tt>.
    136  * @return string The text in <tt>$string</tt> after all hooked functions are applied to it.
     135 * @return mixed The value in $value after all hooked functions are applied to it.
    137136 */
    138137function apply_filters($tag, $value) {
    139         global $wp_filter, $merged_filters, $wp_current_filter;
     138        global $wp_filter, $wp_current_filter;
    140139
    141140        $args = array();
    142         @$wp_current_filter[] = $tag;
     141        $args = func_get_args();
    143142
    144143        // Do 'all' actions first
    145144        if ( isset($wp_filter['all']) ) {
    146                 reset( $wp_filter['all'] );
    147                 $args = func_get_args();
    148                 do {
    149                         foreach ( (array) current($wp_filter['all']) as $the_ )
    150                                 if ( !is_null($the_['function']) )
    151                                         call_user_func_array($the_['function'], $args);
    152 
    153                 } while ( next($wp_filter['all']) !== false );
     145                _wp_call_all_hook($args, $tag);
    154146        }
    155147
    156         if ( !isset($wp_filter[$tag]) ) {
    157                 array_pop($wp_current_filter);
     148        if ( !isset($wp_filter[$tag]) )
    158149                return $value;
    159         }
    160150
    161         // Sort
    162         if ( !isset( $merged_filters[ $tag ] ) ) {
    163                 reset($wp_filter[$tag]);
    164                 uksort($wp_filter[$tag], "strnatcasecmp");
    165                 $merged_filters[ $tag ] = true;
    166         }
     151        $wp_current_filter[] = $tag;
    167152
    168         reset( $wp_filter[ $tag ] );
    169 
    170         if ( empty($args) )
    171                 $args = func_get_args();
    172 
    173         do {
    174                 foreach( (array) current($wp_filter[$tag]) as $the_ )
    175                         if ( !is_null($the_['function']) ){
     153        foreach( (array) $wp_filter[$tag] as $priority => $unused) {
     154                foreach( (array) $wp_filter[$tag][$priority] as $the_ ) {
     155                        if ( !is_null($the_['function']) ) {
    176156                                $args[1] = $value;
    177157                                $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
    178158                        }
     159                }
     160        }
    179161
    180         } while ( next($wp_filter[$tag]) !== false );
    181        
    182162        array_pop( $wp_current_filter );
    183163
    184164        return $value;
     
    214194                unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
    215195                if ( empty($GLOBALS['wp_filter'][$tag][$priority]) )
    216196                        unset($GLOBALS['wp_filter'][$tag][$priority]);
    217                 unset($GLOBALS['merged_filters'][$tag]);
    218197        }
    219198
    220199        return $r;
     
    250229 * @param int $accepted_args optional. The number of arguments the function accept (default 1).
    251230 */
    252231function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
    253         global $wp_filter, $merged_filters;
     232        global $wp_filter;
    254233
    255234        $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
    256235        $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
    257         unset( $merged_filters[ $tag ] );
    258236        return true;
    259237}
    260238
     
    289267        else
    290268                $wp_actions = array($tag);
    291269
    292         @$wp_current_filter[] = $tag;
    293 
    294         // Do 'all' actions first
    295         if ( isset($wp_filter['all']) ) {
    296                 reset( $wp_filter['all'] );
    297                 $all_args = func_get_args();
    298                 do {
    299                         foreach( (array) current($wp_filter['all']) as $the_ )
    300                                 if ( !is_null($the_['function']) )
    301                                         call_user_func_array($the_['function'], $all_args);
    302 
    303                 } while ( next($wp_filter['all']) !== false );
    304         }
    305 
    306         if ( !isset($wp_filter[$tag]) ) {
    307                 array_pop($wp_current_filter);
    308                 return;
    309         }
    310 
    311270        $args = array();
    312271        if ( is_array($arg) && 1 == count($arg) && is_object($arg[0]) ) // array(&$this)
    313272                $args[] =& $arg[0];
     
    316275        for ( $a = 2; $a < func_num_args(); $a++ )
    317276                $args[] = func_get_arg($a);
    318277
    319         // Sort
    320         if ( !isset( $merged_filters[ $tag ] ) ) {
    321                 reset($wp_filter[$tag]);
    322                 uksort($wp_filter[$tag], "strnatcasecmp");
    323                 $merged_filters[ $tag ] = true;
     278        // Do 'all' actions first
     279        if ( isset($wp_filter['all']) ) {
     280                _wp_call_all_hook($args, $tag);
    324281        }
    325282
    326         reset( $wp_filter[ $tag ] );
     283        if ( !isset($wp_filter[$tag]) )
     284                return;
    327285
    328         do {
    329                 foreach ( (array) current($wp_filter[$tag]) as $the_ )
     286        $wp_current_filter[] = $tag;
     287
     288        foreach( (array) $wp_filter[$tag] as $priority => $unused )
     289                foreach ( (array) $wp_filter[$tag][$priority] as $the_ )
    330290                        if ( !is_null($the_['function']) )
    331291                                call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
    332292
    333         } while ( next($wp_filter[$tag]) !== false );
    334 
    335                 array_pop($wp_current_filter);
     293        array_pop($wp_current_filter);
    336294}
    337295
    338296/**
     
    372330 * @return null Will return null if $tag does not exist in $wp_filter array
    373331 */
    374332function do_action_ref_array($tag, $args) {
    375         global $wp_filter, $wp_actions;
     333        global $wp_filter, $wp_actions, $wp_current_filter;
    376334
    377335        if ( !is_array($wp_actions) )
    378336                $wp_actions = array($tag);
     
    381339
    382340        // Do 'all' actions first
    383341        if ( isset($wp_filter['all']) ) {
    384                 reset( $wp_filter['all'] );
    385                 $all_args = func_get_args();
    386                 do {
    387                         foreach( (array) current($wp_filter['all']) as $the_ )
    388                                 if ( !is_null($the_['function']) )
    389                                         call_user_func_array($the_['function'], $all_args);
    390 
    391                 } while ( next($wp_filter['all']) !== false );
     342                _wp_call_all_hook($args, $tag);
    392343        }
    393344
    394345        if ( !isset($wp_filter[$tag]) )
    395346                return;
    396347
    397         // Sort
    398         if ( !isset( $merged_filters[ $tag ] ) ) {
    399                 reset($wp_filter[$tag]);
    400                 uksort($wp_filter[$tag], "strnatcasecmp");
    401                 $merged_filters[ $tag ] = true;
    402         }
     348        $wp_current_filter[] = $tag;
    403349
    404         reset( $wp_filter[ $tag ] );
    405 
    406         do {
    407                 foreach( (array) current($wp_filter[$tag]) as $the_ )
     350        foreach( (array) $wp_filter[$tag] as $priority => $unused)
     351                foreach( (array) $wp_filter[$tag][$priority] as $the_ )
    408352                        if ( !is_null($the_['function']) )
    409353                                call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
    410354
    411         } while ( next($wp_filter[$tag]) !== false );
    412 
     355        array_pop($wp_current_filter);
    413356}
    414357
    415358/**
     
    526469}
    527470
    528471/**
     472 * Calls the 'all' hook, which will process the functions hooked into it.
     473 *
     474 * The 'all' hook passes all of the arguments or parameters that were used for the
     475 * hook, which this function was called for. The first parameter will have to be tested
     476 * as it will be the hook name.
     477 *
     478 * This function is used internally for apply_filters(), do_action(), and do_action_ref_array()
     479 * and is not meant to be used from outside those functions. This function does not check for the
     480 * existent of the all hook, so it will fail unless the all hook exists prior to this function call.
     481 *
     482 * @package WordPress
     483 * @subpackage Plugin
     484 * @since 2.4
     485 * @access private
     486 *
     487 * @uses $wp_filter Used to process all of the functions in the 'all' hook
     488 *
     489 * @param array $args The collected parameters from the hook that was called.
     490 * @param string $hook Optional. The hook name that was used to call the 'all' hook.
     491 */
     492function _wp_call_all_hook($args, $hook=null)
     493{
     494        global $wp_filter;
     495       
     496        if(isset($hook))
     497                array_unshift($args, $hook);
     498               
     499        foreach((array) $wp_filter['all'] as $priority => $unused)
     500                foreach ( (array) $wp_filter['all'][$priority] as $the_ )
     501                        if ( !is_null($the_['function']) )
     502                                call_user_func_array($the_['function'], $args);
     503}
     504
     505/**
    529506 * Build Unique ID for storage and retrieval
    530507 *
    531508 * The old way to serialize the callback caused issues and this function is the