Make WordPress Core

Ticket #36199: 36199-copyarray.patch

File 36199-copyarray.patch, 5.2 KB (added by dougwollison, 9 years ago)

This patch uses the $hooks = $wp_filters[$tag] method, copying the array before manipulating it via iteration.

  • 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 ] );
     228        // Copy for iteration
     229        $hooks = $wp_filter[ $tag ];
    227230
    228         if ( empty($args) )
     231        reset( $hooks );
     232
     233        if ( empty($args) ) {
    229234                $args = func_get_args();
     235        }
    230236
    231237        do {
    232                 foreach ( (array) current($wp_filter[$tag]) as $the_ )
     238                foreach ( (array) current($hooks) as $the_ ) {
    233239                        if ( !is_null($the_['function']) ){
    234240                                $args[1] = $value;
    235241                                $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
    236242                        }
     243                }
    237244
    238         } while ( next($wp_filter[$tag]) !== false );
     245        } while ( next($hooks) !== false );
    239246
    240247        array_pop( $wp_current_filter );
    241248
     
    269276        }
    270277
    271278        if ( !isset($wp_filter[$tag]) ) {
    272                 if ( isset($wp_filter['all']) )
     279                if ( isset($wp_filter['all']) ) {
    273280                        array_pop($wp_current_filter);
     281                }
    274282                return $args[0];
    275283        }
    276284
    277         if ( !isset($wp_filter['all']) )
     285        if ( !isset($wp_filter['all']) ) {
    278286                $wp_current_filter[] = $tag;
     287        }
    279288
    280289        // Sort
    281290        if ( !isset( $merged_filters[ $tag ] ) ) {
     
    283292                $merged_filters[ $tag ] = true;
    284293        }
    285294
    286         reset( $wp_filter[ $tag ] );
     295        // Copy for iteration
     296        $hooks = $wp_filter[ $tag ];
    287297
     298        reset( $hooks );
     299
    288300        do {
    289                 foreach ( (array) current($wp_filter[$tag]) as $the_ )
    290                         if ( !is_null($the_['function']) )
     301                foreach ( (array) current($hooks) as $the_ ) {
     302                        if ( !is_null($the_['function']) ) {
    291303                                $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
     304                        }
     305                }
     306        } while ( next($hooks) !== false );
    292307
    293         } while ( next($wp_filter[$tag]) !== false );
    294 
    295308        array_pop( $wp_current_filter );
    296309
    297310        return $args[0];
     
    482495function do_action($tag, $arg = '') {
    483496        global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
    484497
    485         if ( ! isset($wp_actions[$tag]) )
     498        if ( ! isset($wp_actions[$tag]) ) {
    486499                $wp_actions[$tag] = 1;
    487         else
     500        } else {
    488501                ++$wp_actions[$tag];
     502        }
    489503
    490504        // Do 'all' actions first
    491505        if ( isset($wp_filter['all']) ) {
     
    495509        }
    496510
    497511        if ( !isset($wp_filter[$tag]) ) {
    498                 if ( isset($wp_filter['all']) )
     512                if ( isset($wp_filter['all']) ) {
    499513                        array_pop($wp_current_filter);
     514                }
    500515                return;
    501516        }
    502517
    503         if ( !isset($wp_filter['all']) )
     518        if ( !isset($wp_filter['all']) ) {
    504519                $wp_current_filter[] = $tag;
     520        }
    505521
    506522        $args = array();
    507         if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
     523        if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) { // array(&$this)
    508524                $args[] =& $arg[0];
    509         else
     525        } else {
    510526                $args[] = $arg;
    511         for ( $a = 2, $num = func_num_args(); $a < $num; $a++ )
     527        }
     528
     529        for ( $a = 2, $num = func_num_args(); $a < $num; $a++ ) {
    512530                $args[] = func_get_arg($a);
     531        }
    513532
    514533        // Sort
    515534        if ( !isset( $merged_filters[ $tag ] ) ) {
     
    517536                $merged_filters[ $tag ] = true;
    518537        }
    519538
    520         reset( $wp_filter[ $tag ] );
     539        // Copy for iteration
     540        $hooks = $wp_filter[ $tag ];
    521541
     542        reset( $hooks );
     543
    522544        do {
    523                 foreach ( (array) current($wp_filter[$tag]) as $the_ )
    524                         if ( !is_null($the_['function']) )
     545                foreach ( (array) current($hooks) as $the_ ) {
     546                        if ( !is_null($the_['function']) ) {
    525547                                call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
     548                        }
     549                }
     550        } while ( next($hooks) !== false );
    526551
    527         } while ( next($wp_filter[$tag]) !== false );
    528 
    529552        array_pop($wp_current_filter);
    530553}
    531554
     
    566589function do_action_ref_array($tag, $args) {
    567590        global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
    568591
    569         if ( ! isset($wp_actions[$tag]) )
     592        if ( ! isset($wp_actions[$tag]) ) {
    570593                $wp_actions[$tag] = 1;
    571         else
     594        } else {
    572595                ++$wp_actions[$tag];
     596        }
    573597
    574598        // Do 'all' actions first
    575599        if ( isset($wp_filter['all']) ) {
     
    579603        }
    580604
    581605        if ( !isset($wp_filter[$tag]) ) {
    582                 if ( isset($wp_filter['all']) )
     606                if ( isset($wp_filter['all']) ) {
    583607                        array_pop($wp_current_filter);
     608                }
    584609                return;
    585610        }
    586611
    587         if ( !isset($wp_filter['all']) )
     612        if ( !isset($wp_filter['all']) ) {
    588613                $wp_current_filter[] = $tag;
     614        }
    589615
    590616        // Sort
    591617        if ( !isset( $merged_filters[ $tag ] ) ) {
     
    593619                $merged_filters[ $tag ] = true;
    594620        }
    595621
    596         reset( $wp_filter[ $tag ] );
     622        // Copy for iteration
     623        $hooks = $wp_filter[ $tag ];
    597624
     625        reset( $hooks );
     626
    598627        do {
    599                 foreach ( (array) current($wp_filter[$tag]) as $the_ )
    600                         if ( !is_null($the_['function']) )
     628                foreach ( (array) current($hooks) as $the_ ) {
     629                        if ( !is_null($the_['function']) ) {
    601630                                call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
     631                        }
     632                }
     633        } while ( next($hooks) !== false );
    602634
    603         } while ( next($wp_filter[$tag]) !== false );
    604 
    605635        array_pop($wp_current_filter);
    606636}
    607637