git-svn-id: http://svn.automattic.com/wordpress/trunk@2398 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2005-03-02 15:28:32 +00:00
parent 26808caf61
commit e276f13b35
1 changed files with 71 additions and 28 deletions

View File

@ -871,42 +871,69 @@ function merge_filters($tag) {
function apply_filters($tag, $string) { function apply_filters($tag, $string) {
global $wp_filter; global $wp_filter;
$args = array_slice(func_get_args(), 3); $args = array_slice(func_get_args(), 2);
merge_filters($tag); merge_filters($tag);
if (isset($wp_filter[$tag])) { if (!isset($wp_filter[$tag])) {
foreach ($wp_filter[$tag] as $priority => $functions) { return $string;
if (!is_null($functions)) { }
foreach($functions as $function) { foreach ($wp_filter[$tag] as $priority => $functions) {
$string = call_user_func_array($function, array($string) + $args); if (!is_null($functions)) {
foreach($functions as $function) {
$all_args = array_merge(array($string), $args);
$function_name = $function['function'];
$accepted_args = $function['accepted_args'];
if($accepted_args == 1) {
$args = array($string);
} elseif ($accepted_args > 1) {
$args = array_slice($all_args, 0, $accepted_args);
} elseif($accepted_args == 0) {
$args = NULL;
} else {
$args = $all_args;
} }
$string = call_user_func_array($function_name, $args);
} }
} }
} }
return $string; return $string;
} }
function add_filter($tag, $function_to_add, $priority = 10) { function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
global $wp_filter; global $wp_filter;
// So the format is wp_filter['tag']['array of priorities']['array of functions']
if (!@in_array($function_to_add, $wp_filter[$tag]["$priority"])) { // check that we don't already have the same filter at the same priority
$wp_filter[$tag]["$priority"][] = $function_to_add; if (isset($wp_filter[$tag]["$priority"])) {
foreach($wp_filter[$tag]["$priority"] as $filter) {
// uncomment if we want to match function AND accepted_args
//if ($filter == array($function, $accepted_args)) {
if ($filter['function'] == $function_to_add) {
return true;
}
}
} }
// So the format is wp_filter['tag']['array of priorities']['array of ['array (functions, accepted_args)]']
$wp_filter[$tag]["$priority"][] = array('function'=>$function_to_add, 'accepted_args'=>$accepted_args);
return true; return true;
} }
function remove_filter($tag, $function_to_remove, $priority = 10) { function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
global $wp_filter; global $wp_filter;
if (@in_array($function_to_remove, $wp_filter[$tag]["$priority"])) {
foreach ($wp_filter[$tag]["$priority"] as $function) { // rebuild the list of filters
if ($function_to_remove != $function) { if (isset($wp_filter[$tag]["$priority"])) {
$new_function_list[] = $function; foreach($wp_filter[$tag]["$priority"] as $filter) {
if ($filter['function'] != $function_to_remove) {
$new_function_list[] = $filter;
} }
} }
$wp_filter[$tag]["$priority"] = $new_function_list; $wp_filter[$tag]["$priority"] = $new_function_list;
} }
//die(var_dump($wp_filter));
return true; return true;
} }
@ -914,31 +941,47 @@ function remove_filter($tag, $function_to_remove, $priority = 10) {
function do_action($tag, $arg = '') { function do_action($tag, $arg = '') {
global $wp_filter; global $wp_filter;
$extra_args = array_slice(func_get_args(), 2);
if ( is_array($arg) ) if ( is_array($arg) )
$args = $arg + array_slice(func_get_args(), 2); $args = array_merge($arg, $extra_args);
else else
$args = array($arg) + array_slice(func_get_args(), 2); $args = array_merge(array($arg), $extra_args);
merge_filters($tag); merge_filters($tag);
if (isset($wp_filter[$tag])) { if (!isset($wp_filter[$tag])) {
foreach ($wp_filter[$tag] as $priority => $functions) { return;
if (!is_null($functions)) { }
foreach($functions as $function) { foreach ($wp_filter[$tag] as $priority => $functions) {
$string = call_user_func_array($function, $args); if (!is_null($functions)) {
foreach($functions as $function) {
$all_args = array_merge(array($string), $args);
$function_name = $function['function'];
$accepted_args = $function['accepted_args'];
if($accepted_args == 1) {
$args = array($string);
} elseif ($accepted_args > 1) {
$args = array_slice($all_args, 0, $accepted_args);
} elseif($accepted_args == 0) {
$args = NULL;
} else {
$args = $all_args;
} }
$string = call_user_func_array($function_name, $args);
} }
} }
} }
} }
function add_action($tag, $function_to_add, $priority = 10) { function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
add_filter($tag, $function_to_add, $priority); add_filter($tag, $function_to_add, $priority, $accepted_args);
} }
function remove_action($tag, $function_to_remove, $priority = 10) { function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
remove_filter($tag, $function_to_remove, $priority); remove_filter($tag, $function_to_remove, $priority, $accepted_args);
} }
function get_page_uri($page_id) { function get_page_uri($page_id) {