Index: plugin.php
===================================================================
--- plugin.php	(revision 6312)
+++ plugin.php	(working copy)
@@ -59,8 +59,8 @@
  * @package WordPress
  * @subpackage Plugin
  * @since 1.5
- * @global array $wp_filter Stores all of the filters added in the form of
- *	wp_filter['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)]']
+ * @global array $wp_filter_hooks Stores all of the filters added in the form of
+ *	$wp_filter_hooks['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)]']
  * @global array $merged_filters Tracks the tags that need to be merged for later. If the hook is added, it doesn't need to run through that process.
  *
  * @param string $tag The name of the filter to hook the <tt>$function_to_add</tt> to.
@@ -70,10 +70,9 @@
  * @return boolean true
  */
 function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
-	global $wp_filter, $merged_filters;
+	global $wp_filter_hooks, $merged_filters;
 
-	$idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
-	$wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
+	$wp_filter_hooks[$tag][$priority][_wp_filter_build_unique_id($tag, $function_to_add, $priority)] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
 	unset( $merged_filters[ $tag ] );
 	return true;
 }
@@ -107,26 +106,26 @@
  * @return string The text in <tt>$string</tt> after all hooked functions are applied to it.
  */
 function apply_filters($tag, $value) {
-	global $wp_filter, $merged_filters;
+	global $wp_filter_hooks, $merged_filters;
 
 	if ( !isset( $merged_filters[ $tag ] ) )
-		merge_filters($tag);
+		merge_filters($tag, 'filter');
 
 	if ( !isset($wp_filter[$tag]) )
 		return $value;
 
-	reset( $wp_filter[ $tag ] );
+	reset( $wp_filter_hooks[ $tag ] );
 
 	$args = func_get_args();
 
 	do{
-		foreach( (array) current($wp_filter[$tag]) as $the_ )
+		foreach( (array) current($wp_filter_hooks[$tag]) as $the_ )
 			if ( !is_null($the_['function']) ){
 				$args[1] = $value;
 				$value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
 			}
 
-	} while ( next($wp_filter[$tag]) !== false );
+	} while ( next($wp_filter_hooks[$tag]) !== false );
 
 	return $value;
 }
@@ -149,16 +148,20 @@
  * @global array $merge_filters Merges the filter hooks using this function.
  *
  * @param string $tag The filter hook of which the functions should be merged.
+ * @param string $caller Whether the caller is a 'filter' or 'action'
  */
-function merge_filters($tag) {
-	global $wp_filter, $merged_filters;
+function merge_filters($tag, $caller='filter') {
+	global $merged_filters;
 
-	if ( isset($wp_filter['all']) && is_array($wp_filter['all']) )
-		$wp_filter[$tag] = array_merge($wp_filter['all'], (array) $wp_filter[$tag]);
+	$global_hook = 'wp_'.$caller.'_hooks';
+	
+	global $$global_hook; // Yeah, yeah, this is hackish. I know
+	if ( isset($$global_hook[$all]) && is_array($$global_hook[$all]) )
+		$$global_hook[$tag] = $$global_hook[$all] + (array) $$global_hook[$tag];
 
-	if ( isset($wp_filter[$tag]) ){
-		reset($wp_filter[$tag]);
-		uksort($wp_filter[$tag], "strnatcasecmp");
+	if ( isset($$global_hook[$tag]) ){
+		reset($$global_hook[$tag]);
+		uksort($$global_hook[$tag], "strnatcasecmp");
 	}
 	$merged_filters[ $tag ] = true;
 }
@@ -185,13 +188,13 @@
  * @return boolean Whether the function existed before it was removed.
  */
 function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
-	$function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority);
+	$r = isset($GLOBALS['wp_filter_hooks'][$tag][$priority][_wp_filter_build_unique_id($tag, $function_to_remove, $priority)]);
 
-	$r = isset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
+	if( true === $r) {
+		unset($GLOBALS['wp_filter_hooks'][$tag][$priority][$function_to_remove]);
+		unset($GLOBALS['merged_filters'][$tag]);
+	}
 
-	unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
-	unset($GLOBALS['merged_filters'][$tag]);
-
 	return $r;
 }
 
@@ -203,7 +206,8 @@
  * one or more of its PHP functions are executed at these points, using the
  * Action API.
  *
- * @uses add_filter() Adds an action. Parameter list and functionality are the same.
+ * @global array $wp_action_hooks
+ * @uses $merged_filters Resets the value that the tag has already been merged.
  *
  * @package WordPress
  * @subpackage Plugin
@@ -215,7 +219,11 @@
  * @param int $accepted_args optional. The number of arguments the function accept (default 1).
  */
 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
-	add_filter($tag, $function_to_add, $priority, $accepted_args);
+	global $wp_action_hooks, $merged_filters;
+
+	$wp_filter[$tag][$priority][_wp_filter_build_unique_id($tag, $function_to_add, $priority)] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
+	unset( $merged_filters[ $tag ] );
+	return true;
 }
 
 /**
@@ -241,12 +249,12 @@
  * @return null Will return null if $tag does not exist in $wp_filter array
  */
 function do_action($tag, $arg = '') {
-	global $wp_filter, $wp_actions;
+	global $wp_action_hooks, $wp_actions;
 
 	if ( is_array($wp_actions) )
-		$wp_actions[] = $tag;
+		$wp_action_hooks[] = $tag;
 	else
-		$wp_actions = array($tag);
+		$wp_action_hooks = array($tag);
 
 	$args = array();
 	if ( is_array($arg) && 1 == count($arg) && is_object($arg[0]) ) // array(&$this)
@@ -256,17 +264,18 @@
 	for ( $a = 2; $a < func_num_args(); $a++ )
 		$args[] = func_get_arg($a);
 
-	merge_filters($tag);
+	if ( !isset( $merged_filters[ $tag ] ) )
+		merge_filters($tag, 'action');
 
-	if ( !isset($wp_filter[$tag]) )
+	if ( !isset($wp_action_hooks[$tag]) )
 		return;
 
 	do{
-		foreach( (array) current($wp_filter[$tag]) as $the_ )
+		foreach( (array) current($wp_action_hooks[$tag]) as $the_ )
 			if ( !is_null($the_['function']) )
 				call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
 
-	} while ( next($wp_filter[$tag]) !== false );
+	} while ( next($wp_action_hooks[$tag]) !== false );
 
 }
 
@@ -309,20 +318,21 @@
  * @return null Will return null if $tag does not exist in $wp_filter array
  */
 function do_action_ref_array($tag, $args) {
-	global $wp_filter, $wp_actions;
+	global $wp_action_hooks, $wp_actions;
 
 	if ( !is_array($wp_actions) )
-		$wp_actions = array($tag);
+		$wp_action_hooks = array($tag);
 	else
-		$wp_actions[] = $tag;
+		$wp_action_hooks[] = $tag;
 
-	merge_filters($tag);
+	if ( !isset( $merged_filters[ $tag ] ) )
+		merge_filters($tag, 'action');
 
-	if ( !isset($wp_filter[$tag]) )
+	if ( !isset($wp_action_hooks[$tag]) )
 		return;
 
 	do{
-		foreach( (array) current($wp_filter[$tag]) as $the_ )
+		foreach( (array) current($wp_action_hooks[$tag]) as $the_ )
 			if ( !is_null($the_['function']) )
 				call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
 
@@ -350,7 +360,14 @@
  * @return boolean Whether the function is removed.
  */
 function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
-	return remove_filter($tag, $function_to_remove, $priority, $accepted_args);
+	$r = isset($GLOBALS['wp_action_hooks'][$tag][$priority][_wp_filter_build_unique_id($tag, $function_to_remove, $priority)]);
+
+	if( true === $r) {
+		unset($GLOBALS['wp_action_hooks'][$tag][$priority][$function_to_remove]);
+		unset($GLOBALS['merged_filters'][$tag]);
+	}
+
+	return $r;
 }
 
 //
@@ -461,18 +478,20 @@
  * @param int $priority Used in counting how many hooks were applied
  * @return string Unique ID for usage as array key
  */
-function _wp_filter_build_unique_id($tag, $function, $priority = 10)
-{
+function _wp_filter_build_unique_id($tag, $function, $priority = 10) {
 	global $wp_filter;
 
 	// If function then just skip all of the tests and not overwrite the following.
 	if( is_string($function) )
 		return $function;
+	// Static Calling
+	else if( is_string($function[0]) )
+		return $function[0].$function[1];
 	// Object Class Calling
 	else if(is_object($function[0]) )
 	{
 		$obj_idx = get_class($function[0]).$function[1];
-		if( is_null($function[0]->wp_filter_id) ) { // This should be instead of is_null() change to !isset() to fix notice
+		if( !isset($function[0]->wp_filter_id) ) {
 			$count = count((array)$wp_filter[$tag][$priority]);
 			$function[0]->wp_filter_id = $count;
 			$obj_idx .= $count;
@@ -481,9 +500,6 @@
 			$obj_idx .= $function[0]->wp_filter_id;
 		return $obj_idx;
 	}
-	// Static Calling
-	else if( is_string($function[0]) )
-		return $function[0].$function[1];
 }
 
-?>
\ No newline at end of file
+?>

