Make WordPress Core

Ticket #36199: 36199-foreach.patch

File 36199-foreach.patch, 4.9 KB (added by dougwollison, 9 years ago)

This patch uses the foreach loop method, iterating over the array the standard way.

  • src/wp-includes/plugin.php

     
    209209        }
    210210
    211211        if ( !isset($wp_filter[$tag]) ) {
    212                 if ( isset($wp_filter['all']) )
     212                if ( isset($wp_filter['all']) ) {
    213213                        array_pop($wp_current_filter);
     214                }
    214215                return $value;
    215216        }
    216217
    217         if ( !isset($wp_filter['all']) )
     218        if ( !isset($wp_filter['all']) ) {
    218219                $wp_current_filter[] = $tag;
     220        }
    219221
    220222        // Sort.
    221223        if ( !isset( $merged_filters[ $tag ] ) ) {
     
    223225                $merged_filters[ $tag ] = true;
    224226        }
    225227
    226         reset( $wp_filter[ $tag ] );
    227 
    228         if ( empty($args) )
     228        if ( empty($args) ) {
    229229                $args = func_get_args();
     230        }
    230231
    231         do {
    232                 foreach ( (array) current($wp_filter[$tag]) as $the_ )
     232        foreach ( $wp_filter[ $tag ] as $hooks ) {
     233                foreach ( (array) $hooks as $the_ ) {
    233234                        if ( !is_null($the_['function']) ){
    234235                                $args[1] = $value;
    235236                                $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
    236237                        }
     238                }
     239        }
    237240
    238         } while ( next($wp_filter[$tag]) !== false );
    239 
    240241        array_pop( $wp_current_filter );
    241242
    242243        return $value;
     
    269270        }
    270271
    271272        if ( !isset($wp_filter[$tag]) ) {
    272                 if ( isset($wp_filter['all']) )
     273                if ( isset($wp_filter['all']) ) {
    273274                        array_pop($wp_current_filter);
     275                }
    274276                return $args[0];
    275277        }
    276278
    277         if ( !isset($wp_filter['all']) )
     279        if ( !isset($wp_filter['all']) ) {
    278280                $wp_current_filter[] = $tag;
     281        }
    279282
    280283        // Sort
    281284        if ( !isset( $merged_filters[ $tag ] ) ) {
     
    283286                $merged_filters[ $tag ] = true;
    284287        }
    285288
    286         reset( $wp_filter[ $tag ] );
    287 
    288         do {
    289                 foreach ( (array) current($wp_filter[$tag]) as $the_ )
    290                         if ( !is_null($the_['function']) )
     289        foreach ( $wp_filter[ $tag ] as $hooks ) {
     290                foreach ( (array) $hooks as $the_ ) {
     291                        if ( !is_null($the_['function']) ){
    291292                                $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
     293                        }
     294                }
     295        }
    292296
    293         } while ( next($wp_filter[$tag]) !== false );
    294 
    295297        array_pop( $wp_current_filter );
    296298
    297299        return $args[0];
     
    482484function do_action($tag, $arg = '') {
    483485        global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
    484486
    485         if ( ! isset($wp_actions[$tag]) )
     487        if ( ! isset($wp_actions[$tag]) ) {
    486488                $wp_actions[$tag] = 1;
    487         else
     489        } else {
    488490                ++$wp_actions[$tag];
     491        }
    489492
    490493        // Do 'all' actions first
    491494        if ( isset($wp_filter['all']) ) {
     
    495498        }
    496499
    497500        if ( !isset($wp_filter[$tag]) ) {
    498                 if ( isset($wp_filter['all']) )
     501                if ( isset($wp_filter['all']) ) {
    499502                        array_pop($wp_current_filter);
     503                }
    500504                return;
    501505        }
    502506
    503         if ( !isset($wp_filter['all']) )
     507        if ( !isset($wp_filter['all']) ) {
    504508                $wp_current_filter[] = $tag;
     509        }
    505510
    506511        $args = array();
    507         if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
     512        if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) { // array(&$this)
    508513                $args[] =& $arg[0];
    509         else
     514        } else {
    510515                $args[] = $arg;
    511         for ( $a = 2, $num = func_num_args(); $a < $num; $a++ )
     516        }
     517
     518        for ( $a = 2, $num = func_num_args(); $a < $num; $a++ ) {
    512519                $args[] = func_get_arg($a);
     520        }
    513521
    514522        // Sort
    515523        if ( !isset( $merged_filters[ $tag ] ) ) {
     
    517525                $merged_filters[ $tag ] = true;
    518526        }
    519527
    520         reset( $wp_filter[ $tag ] );
    521 
    522         do {
    523                 foreach ( (array) current($wp_filter[$tag]) as $the_ )
    524                         if ( !is_null($the_['function']) )
     528        foreach ( $wp_filter[$tag] as $hooks ) {
     529                foreach ( (array) $hooks as $the_ ) {
     530                        if ( !is_null($the_['function']) ) {
    525531                                call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
     532                        }
     533                }
     534        }
    526535
    527         } while ( next($wp_filter[$tag]) !== false );
    528 
    529536        array_pop($wp_current_filter);
    530537}
    531538
     
    566573function do_action_ref_array($tag, $args) {
    567574        global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
    568575
    569         if ( ! isset($wp_actions[$tag]) )
     576        if ( ! isset($wp_actions[$tag]) ) {
    570577                $wp_actions[$tag] = 1;
    571         else
     578        } else {
    572579                ++$wp_actions[$tag];
     580        }
    573581
    574582        // Do 'all' actions first
    575583        if ( isset($wp_filter['all']) ) {
     
    579587        }
    580588
    581589        if ( !isset($wp_filter[$tag]) ) {
    582                 if ( isset($wp_filter['all']) )
     590                if ( isset($wp_filter['all']) ) {
    583591                        array_pop($wp_current_filter);
     592                }
    584593                return;
    585594        }
    586595
    587         if ( !isset($wp_filter['all']) )
     596        if ( !isset($wp_filter['all']) ) {
    588597                $wp_current_filter[] = $tag;
     598        }
    589599
    590600        // Sort
    591601        if ( !isset( $merged_filters[ $tag ] ) ) {
     
    593603                $merged_filters[ $tag ] = true;
    594604        }
    595605
    596         reset( $wp_filter[ $tag ] );
    597 
    598         do {
    599                 foreach ( (array) current($wp_filter[$tag]) as $the_ )
    600                         if ( !is_null($the_['function']) )
     606        foreach ( $wp_filter[$tag] as $hooks ) {
     607                foreach ( (array) $hooks as $the_ ) {
     608                        if ( !is_null($the_['function']) ) {
    601609                                call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
     610                        }
     611                }
     612        }
    602613
    603         } while ( next($wp_filter[$tag]) !== false );
    604 
    605614        array_pop($wp_current_filter);
    606615}
    607616