Make WordPress Core

Changeset 6324


Ignore:
Timestamp:
11/08/2007 01:05:43 AM (16 years ago)
Author:
ryan
Message:

Change 'all' hook calling. Bring filters and actions back into one array for back-compat. see #5231

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/plugin.php

    r6320 r6324  
    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 ] );
     
    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
     
    139139    global $wp_filter, $merged_filters, $wp_current_filter;
    140140
     141    $args = array();
    141142    @$wp_current_filter[] = $tag;
    142143
     
    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                    call_user_func_array($the_['function'], $args);
    150152
    151153        } while ( next($wp_filter['all']) !== false );
     
    166168    reset( $wp_filter[ $tag ] );
    167169
    168     $args = func_get_args();
     170    if ( empty($args) )
     171        $args = func_get_args();
    169172
    170173    do {
     
    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]);
     
    248251 */
    249252function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
    250     global $wp_action, $merged_actions;
    251 
    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 ] );
     253    global $wp_filter, $merged_filters;
     254
     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}
     
    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) )
     
    286289    else
    287290        $wp_actions = array($tag);
     291
     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    }
    288310
    289311    $args = array();
     
    295317        $args[] = func_get_arg($a);
    296318
    297     @$wp_current_filter[] = $tag;
    298 
    299     // Do 'all' actions first
    300     if ( isset($wp_action['all']) ) {
    301         do {
    302             foreach( (array) current($wp_action['all']) as $the_ )
    303                 if ( !is_null($the_['function']) )
    304                     call_user_func_array($the_['function'], $args[0]);
    305 
    306         } while ( next($wp_action['all']) !== false );
    307     }
    308 
    309     if ( !isset($wp_action[$tag]) ) {
    310         array_pop($wp_current_filter);
    311         return;
    312     }
    313        
    314319    // Sort
    315     if ( !isset( $merged_actions[ $tag ] ) ) {
    316         reset($wp_action[$tag]);
    317         uksort($wp_action[$tag], "strnatcasecmp");
    318         $merged_actions[ $tag ] = true;
    319     }
    320 
    321     reset( $wp_action[ $tag ] );
     320    if ( !isset( $merged_filters[ $tag ] ) ) {
     321        reset($wp_filter[$tag]);
     322        uksort($wp_filter[$tag], "strnatcasecmp");
     323        $merged_filters[ $tag ] = true;
     324    }
     325
     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);
     
    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) )
     
    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]);
    383 
    384         } while ( next($wp_action['all']) !== false );
    385     }
    386 
    387     if ( !isset($wp_action[$tag]) )
     389                    call_user_func_array($the_['function'], $all_args);
     390
     391        } while ( next($wp_filter['all']) !== false );
     392    }
     393
     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;
    395     }
    396 
    397     reset( $wp_action[ $tag ] );
     398    if ( !isset( $merged_filters[ $tag ] ) ) {
     399        reset($wp_filter[$tag]);
     400        uksort($wp_filter[$tag], "strnatcasecmp");
     401        $merged_filters[ $tag ] = true;
     402    }
     403
     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}
     
    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.
     
    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
     
    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
     
    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.
     
    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;
Note: See TracChangeset for help on using the changeset viewer.