WordPress.org

Make WordPress Core

Changeset 2398


Ignore:
Timestamp:
03/02/05 15:28:32 (9 years ago)
Author:
ryan
Message:

apply_filter fixes from michel. http://mosquito.wordpress.org/view.php?id=901

File:
1 edited

Legend:

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

    r2396 r2398  
    872872    global $wp_filter; 
    873873     
    874     $args = array_slice(func_get_args(), 3); 
     874    $args = array_slice(func_get_args(), 2); 
    875875 
    876876    merge_filters($tag); 
    877877     
    878     if (isset($wp_filter[$tag])) { 
    879         foreach ($wp_filter[$tag] as $priority => $functions) { 
    880             if (!is_null($functions)) { 
    881                 foreach($functions as $function) { 
    882                     $string = call_user_func_array($function, array($string) + $args); 
     878    if (!isset($wp_filter[$tag])) { 
     879        return $string; 
     880    } 
     881    foreach ($wp_filter[$tag] as $priority => $functions) { 
     882        if (!is_null($functions)) { 
     883            foreach($functions as $function) { 
     884 
     885                $all_args = array_merge(array($string), $args); 
     886                $function_name = $function['function']; 
     887                $accepted_args = $function['accepted_args']; 
     888 
     889                if($accepted_args == 1) { 
     890                    $args = array($string); 
     891                } elseif ($accepted_args > 1) { 
     892                    $args = array_slice($all_args, 0, $accepted_args); 
     893                } elseif($accepted_args == 0) { 
     894                    $args = NULL; 
     895                } else { 
     896                    $args = $all_args; 
    883897                } 
     898 
     899                $string = call_user_func_array($function_name, $args); 
    884900            } 
    885901        } 
     
    888904} 
    889905 
    890 function add_filter($tag, $function_to_add, $priority = 10) { 
     906function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) { 
    891907    global $wp_filter; 
    892     // So the format is wp_filter['tag']['array of priorities']['array of functions'] 
    893     if (!@in_array($function_to_add, $wp_filter[$tag]["$priority"])) { 
    894         $wp_filter[$tag]["$priority"][] = $function_to_add; 
    895     } 
     908 
     909    // check that we don't already have the same filter at the same priority 
     910    if (isset($wp_filter[$tag]["$priority"])) { 
     911        foreach($wp_filter[$tag]["$priority"] as $filter) { 
     912            // uncomment if we want to match function AND accepted_args 
     913            //if ($filter == array($function, $accepted_args)) { 
     914            if ($filter['function'] == $function_to_add) { 
     915                return true; 
     916            } 
     917        } 
     918    } 
     919 
     920    // So the format is wp_filter['tag']['array of priorities']['array of ['array (functions, accepted_args)]'] 
     921    $wp_filter[$tag]["$priority"][] = array('function'=>$function_to_add, 'accepted_args'=>$accepted_args); 
    896922    return true; 
    897923} 
    898924 
    899 function remove_filter($tag, $function_to_remove, $priority = 10) { 
     925function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) { 
    900926    global $wp_filter; 
    901     if (@in_array($function_to_remove, $wp_filter[$tag]["$priority"])) { 
    902         foreach ($wp_filter[$tag]["$priority"] as $function) { 
    903             if ($function_to_remove != $function) { 
    904                 $new_function_list[] = $function; 
     927 
     928    // rebuild the list of filters 
     929    if (isset($wp_filter[$tag]["$priority"])) { 
     930        foreach($wp_filter[$tag]["$priority"] as $filter) { 
     931            if ($filter['function'] != $function_to_remove) { 
     932                $new_function_list[] = $filter; 
    905933            } 
    906934        } 
    907935        $wp_filter[$tag]["$priority"] = $new_function_list; 
    908936    } 
    909     //die(var_dump($wp_filter)); 
    910937    return true; 
    911938} 
     
    915942function do_action($tag, $arg = '') { 
    916943    global $wp_filter; 
    917  
     944    $extra_args = array_slice(func_get_args(), 2); 
    918945    if ( is_array($arg) ) 
    919         $args = $arg + array_slice(func_get_args(), 2); 
     946        $args = array_merge($arg, $extra_args); 
    920947    else 
    921         $args = array($arg) + array_slice(func_get_args(), 2); 
     948        $args = array_merge(array($arg), $extra_args); 
    922949     
    923950    merge_filters($tag); 
    924951     
    925     if (isset($wp_filter[$tag])) { 
    926         foreach ($wp_filter[$tag] as $priority => $functions) { 
    927             if (!is_null($functions)) { 
    928                 foreach($functions as $function) { 
    929                     $string = call_user_func_array($function, $args); 
     952    if (!isset($wp_filter[$tag])) { 
     953        return; 
     954    } 
     955    foreach ($wp_filter[$tag] as $priority => $functions) { 
     956        if (!is_null($functions)) { 
     957            foreach($functions as $function) { 
     958 
     959                $all_args = array_merge(array($string), $args); 
     960                $function_name = $function['function']; 
     961                $accepted_args = $function['accepted_args']; 
     962 
     963                if($accepted_args == 1) { 
     964                    $args = array($string); 
     965                } elseif ($accepted_args > 1) { 
     966                    $args = array_slice($all_args, 0, $accepted_args); 
     967                } elseif($accepted_args == 0) { 
     968                    $args = NULL; 
     969                } else { 
     970                    $args = $all_args; 
    930971                } 
     972 
     973                $string = call_user_func_array($function_name, $args); 
    931974            } 
    932975        } 
     
    934977} 
    935978 
    936 function add_action($tag, $function_to_add, $priority = 10) { 
    937     add_filter($tag, $function_to_add, $priority); 
    938 } 
    939  
    940 function remove_action($tag, $function_to_remove, $priority = 10) { 
    941     remove_filter($tag, $function_to_remove, $priority); 
     979function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) { 
     980    add_filter($tag, $function_to_add, $priority, $accepted_args); 
     981} 
     982 
     983function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) { 
     984    remove_filter($tag, $function_to_remove, $priority, $accepted_args); 
    942985} 
    943986 
Note: See TracChangeset for help on using the changeset viewer.