Ticket #3875: plugin.php.diff
File plugin.php.diff, 5.3 KB (added by , 17 years ago) |
---|
-
plugin.php
7 7 function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) { 8 8 global $wp_filter; 9 9 10 // check that we don't already have the same filter at the same priority 11 if ( isset($wp_filter[$tag]["$priority"]) ) { 12 foreach ( $wp_filter[$tag]["$priority"] as $filter ) { 13 // uncomment if we want to match function AND accepted_args 14 // if ( $filter == array($function, $accepted_args) ) { 15 if ( $filter['function'] == $function_to_add ) 16 return true; 17 } 18 } 19 20 // So the format is wp_filter['tag']['array of priorities']['array of ['array (functions, accepted_args)]'] 21 $wp_filter[$tag]["$priority"][] = array('function'=>$function_to_add, 'accepted_args'=>$accepted_args); 10 // So the format is wp_filter['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)]'] 11 $wp_filter[$tag][$priority][serialize($function_to_add)] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); 22 12 return true; 23 13 } 24 14 25 15 function apply_filters($tag, $string) { 26 16 global $wp_filter; 27 17 28 $args = array();29 for ( $a = 2; $a < func_num_args(); $a++ )30 $args[] = func_get_arg($a);31 32 18 merge_filters($tag); 33 19 34 20 if ( !isset($wp_filter[$tag]) ) 35 21 return $string; 36 22 37 foreach ( (array) $wp_filter[$tag] as $priority => $functions ) { 38 if ( !is_null($functions) ) { 39 foreach ( (array) $functions as $function ) { 40 $function_name = $function['function']; 41 $accepted_args = $function['accepted_args']; 42 $the_args = $args; 43 array_unshift($the_args, $string); 44 if ( $accepted_args > 0 ) 45 $the_args = array_slice($the_args, 0, $accepted_args); 46 elseif ( 0 == $accepted_args ) 47 $the_args = NULL; 48 $string = call_user_func_array($function_name, $the_args); 23 $args = func_get_args(); 24 25 do{ 26 foreach( (array) current($wp_filter[$tag]) as $the_ ) 27 if ( !is_null($the_['function']) ){ 28 $args[1] = $string; 29 $string = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args'])); 49 30 } 50 } 51 } 31 32 } while ( next($wp_filter[$tag]) ); 33 52 34 return $string; 53 35 } 54 36 55 37 function merge_filters($tag) { 56 38 global $wp_filter; 57 if ( isset($wp_filter['all']) ) {58 foreach ( (array) $wp_filter['all'] as $priority => $functions ) {59 if ( isset($wp_filter[$tag][$priority]) )60 $wp_filter[$tag][$priority] = array_merge($wp_filter['all'][$priority], $wp_filter[$tag][$priority]);61 else62 $wp_filter[$tag][$priority] = array_merge($wp_filter['all'][$priority], array());63 $wp_filter[$tag][$priority] = array_unique($wp_filter[$tag][$priority]);64 }65 }66 39 67 if ( isset($wp_filter[$tag]) ) 68 uksort( $wp_filter[$tag], "strnatcasecmp" ); 40 if ( isset($wp_filter['all']) ) 41 $wp_filter[$tag] = array_merge($wp_filter['all'], (array) $wp_filter[$tag]); 42 43 if ( isset($wp_filter[$tag]) ){ 44 reset($wp_filter[$tag]); 45 uksort($wp_filter[$tag], "strnatcasecmp"); 46 } 69 47 } 70 48 71 49 function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) { 72 50 global $wp_filter; 73 51 74 // rebuild the list of filters 75 if ( isset($wp_filter[$tag]["$priority"]) ) { 76 $new_function_list = array(); 77 foreach ( (array) $wp_filter[$tag]["$priority"] as $filter ) { 78 if ( $filter['function'] != $function_to_remove ) 79 $new_function_list[] = $filter; 80 } 81 $wp_filter[$tag]["$priority"] = $new_function_list; 82 } 52 unset($GLOBALS['wp_filter'][$tag][$priority][serialize($function_to_remove)]); 53 83 54 return true; 84 55 } 85 56 … … 107 78 if ( !isset($wp_filter[$tag]) ) 108 79 return; 109 80 110 foreach ( (array) $wp_filter[$tag] as $priority => $functions ) { 111 if ( !is_null($functions) ) { 112 foreach ( (array) $functions as $function ) { 113 $function_name = $function['function']; 114 $accepted_args = $function['accepted_args']; 81 do{ 82 foreach( (array) current($wp_filter[$tag]) as $the_ ) 83 if ( !is_null($the_['function']) ) 84 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); 115 85 116 if ( $accepted_args > 0 ) 117 $the_args = array_slice($args, 0, $accepted_args); 118 elseif ( $accepted_args == 0 ) 119 $the_args = NULL; 120 else 121 $the_args = $args; 86 } while ( next($wp_filter[$tag]) ); 122 87 123 call_user_func_array($function_name, $the_args);124 }125 }126 }127 128 88 if ( is_array($wp_actions) ) 129 89 $wp_actions[] = $tag; 130 90 else … … 151 111 if ( !isset($wp_filter[$tag]) ) 152 112 return; 153 113 154 foreach ( (array) $wp_filter[$tag] as $priority => $functions ) { 155 if ( !is_null($functions) ) { 156 foreach ( (array) $functions as $function ) { 157 $function_name = $function['function']; 158 $accepted_args = $function['accepted_args']; 159 if ( $accepted_args > 0 ) 160 $the_args = array_slice($args, 0, $accepted_args); 161 elseif ( 0 == $accepted_args ) 162 $the_args = NULL; 163 else 164 $the_args = $args; 114 do{ 115 foreach( (array) current($wp_filter[$tag]) as $the_ ) 116 if ( !is_null($the_['function']) ) 117 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); 165 118 166 call_user_func_array($function_name, $the_args); 167 } 168 } 169 } 119 } while ( next($wp_filter[$tag]) ); 120 170 121 } 171 122 172 123 function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {