Index: wp-admin/menu-header.php
===================================================================
--- wp-admin/menu-header.php	(revision 19215)
+++ wp-admin/menu-header.php	(working copy)
@@ -36,129 +36,158 @@
 function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) {
 	global $self, $parent_file, $submenu_file, $plugin_page, $pagenow, $typenow;
 
-	$first = true;
-	// 0 = name, 1 = capability, 2 = file, 3 = class, 4 = id, 5 = icon src
+	$first_key = key( $menu );
+
+	// 0 = title, 1 = capability, 2 = file, 3 = page title (ignore), 4 = class, 5 = id, 6 = icon src
 	foreach ( $menu as $key => $item ) {
-		$admin_is_parent = false;
-		$class = array();
 
-		if ( $first ) {
-			$class[] = 'wp-first-item';
-			$first = false;
-		}
+		$item = array_pad( $item, 7, '' );
+		list( $title, $capability, $slug, $ignore, $class, $id, $icon_src ) = $item;
 
-		$submenu_items = false;
-		if ( ! empty( $submenu[$item[2]] ) ) {
-			$class[] = 'wp-has-submenu';
-			$submenu_items = $submenu[$item[2]];
-		}
+		// @todo: Clarify whether we should texturize or not.
+		$title = wptexturize( $title );
 
-		if ( ( $parent_file && $item[2] == $parent_file ) || ( empty($typenow) && $self == $item[2] ) )
-			$class[] = ! empty( $submenu_items ) ? 'wp-has-current-submenu wp-menu-open' : 'current';
+		$has_submenu   = ! empty( $submenu[ $slug ] );
+		$submenu_items = ( $has_submenu ) ? $submenu[ $slug ] : false;
+
+		$parent = $slug;
+
+		if ( $key == $first_key )
+			$class .= ' wp-first-item';
+
+		if ( $has_submenu )
+			$class .= ' wp-has-submenu';
+
+		if ( ( $parent_file && $slug == $parent_file ) || ( empty( $typenow ) && $self == $slug ) )
+			$class .= ( $has_submenu ) ? ' wp-has-current-submenu wp-menu-open' : ' current';
 		else
-			$class[] = 'wp-not-current-submenu';
+			$class .= ' wp-not-current-submenu';
 
-		if ( ! empty( $item[4] ) )
-			$class[] = $item[4];
+		// GENERATE MENU ITEM
+		if ( $submenu_as_parent && $has_submenu ) {
+			$submenu_items = array_values( $submenu_items );  // Re-index.
+			$item_slug     = $submenu_items[0][2];
+			$item_hook     = get_plugin_page_hook( $item_slug, $slug );
 
-		$class = $class ? ' class="' . join( ' ', $class ) . '"' : '';
-		$tabindex = ' tabindex="1"';
-		$id = ! empty( $item[5] ) ? ' id="' . preg_replace( '|[^a-zA-Z0-9_:.]|', '-', $item[5] ) . '"' : '';
-		$img = '';
-		if ( ! empty( $item[6] ) )
-			$img = ( 'div' === $item[6] ) ? '<br />' : '<img src="' . $item[6] . '" alt="" />';
-		$arrow = '<div class="wp-menu-arrow"><div></div></div>';
+		} elseif ( ! empty( $slug ) && current_user_can( $capability ) ) {
+			$item_slug = $slug;
+			$item_hook = get_plugin_page_hook( $item_slug, 'admin.php' );
+		}
 
-		$title = wptexturize( $item[0] );
+		if ( isset( $item_slug ) ) {
+			$item_file = $item_slug;
+			if ( false !== ( $pos = strpos( $item_file, '?' ) ) )
+				$item_file = substr( $item_file, 0, $pos );
 
-		echo "\n\t<li$class$id>";
+			$item_url = $item_slug;
+			if ( ! empty( $item_hook ) || ( ('index.php' != $item_slug ) && file_exists( WP_PLUGIN_DIR . "/$item_file" ) ) ) {
+				$parent   = 'admin.php';
+				$item_url = "admin.php?page=$item_slug";
+			}
+		}
 
+		// RENDER MENU ITEM
+		echo '<li id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '">';
+
 		if ( false !== strpos( $class, 'wp-menu-separator' ) ) {
 			echo '<div class="separator"></div>';
-		} elseif ( $submenu_as_parent && ! empty( $submenu_items ) ) {
-			$submenu_items = array_values( $submenu_items );  // Re-index.
-			$menu_hook = get_plugin_page_hook( $submenu_items[0][2], $item[2] );
-			$menu_file = $submenu_items[0][2];
-			if ( false !== ( $pos = strpos( $menu_file, '?' ) ) )
-				$menu_file = substr( $menu_file, 0, $pos );
-			if ( ! empty( $menu_hook ) || ( ('index.php' != $submenu_items[0][2]) && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) ) ) {
-				$admin_is_parent = true;
-				echo "<div class='wp-menu-image'><a href='admin.php?page={$submenu_items[0][2]}'>$img</a></div>$arrow<a href='admin.php?page={$submenu_items[0][2]}'$class$tabindex>$title</a>";
-			} else {
-				echo "\n\t<div class='wp-menu-image'><a href='{$submenu_items[0][2]}'>$img</a></div>$arrow<a href='{$submenu_items[0][2]}'$class$tabindex>$title</a>";
+
+		} elseif ( isset( $item_slug ) ) {
+			// Menu image
+			echo '<div class="wp-menu-image">';
+			echo '<a href="' . esc_url( $item_url ) . '">';
+
+			if ( ! empty( $icon_src ) ) {
+				if ( 'div' === $icon_src )
+					echo '<br />';
+				else
+					echo '<img src="' . esc_url( $icon_src ) . '" alt="" />';
 			}
-		} elseif ( ! empty( $item[2] ) && current_user_can( $item[1] ) ) {
-			$menu_hook = get_plugin_page_hook( $item[2], 'admin.php' );
-			$menu_file = $item[2];
+
+			echo '</a></div>';
+
+			// Menu arrow
+			echo '<div class="wp-menu-arrow"><div></div></div>';
+
+			// Menu title
+			echo '<a href="' . esc_url( $item_url ) . '" class="' . esc_attr( $class ) . '" tabindex="1">';
+			echo $title;
+			echo '</a>';
+		}
+
+
+		// HANDLE SUBMENUS
+		if ( $has_submenu ) {
+
+			// Submenu wrapper.
+			echo "<div class='wp-submenu'><div class='wp-submenu-wrap'>";
+			echo "<div class='wp-submenu-head'>$title</div><ul>";
+
+			$menu_file = $slug;
 			if ( false !== ( $pos = strpos( $menu_file, '?' ) ) )
 				$menu_file = substr( $menu_file, 0, $pos );
-			if ( ! empty( $menu_hook ) || ( ('index.php' != $item[2]) && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) ) ) {
-				$admin_is_parent = true;
-				echo "\n\t<div class='wp-menu-image'><a href='admin.php?page={$item[2]}'>$img</a></div>$arrow<a href='admin.php?page={$item[2]}'$class$tabindex>{$item[0]}</a>";
-			} else {
-				echo "\n\t<div class='wp-menu-image'><a href='{$item[2]}'>$img</a></div>$arrow<a href='{$item[2]}'$class$tabindex>{$item[0]}</a>";
-			}
-		}
 
-		if ( ! empty( $submenu_items ) ) {
-			echo "\n\t<div class='wp-submenu'><div class='wp-submenu-wrap'>";
-			echo "<div class='wp-submenu-head'>{$item[0]}</div><ul>";
-			$first = true;
+			// Clarify the location of the parent.
+			 if ( ( 'admin.php' != $parent && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! is_dir( WP_PLUGIN_DIR . "/$slug" ) ) || file_exists( $menu_file ) )
+				$parent = $slug;
+			else
+				$parent = 'admin.php';
+
+			// Handle current for post_type=post|page|foo pages, which won't match $self.
+			$self_type = ! empty( $typenow ) ? $self . '?post_type=' . $typenow : 'nothing';
+
+			$first_sub_key = key( $submenu_items );
+
 			foreach ( $submenu_items as $sub_key => $sub_item ) {
-				if ( ! current_user_can( $sub_item[1] ) )
+				// 0 = title, 1 = cap, 2 = slug, 3 = page title
+				list( $sub_title, $sub_cap, $sub_slug ) = $sub_item;
+
+				if ( ! current_user_can( $sub_cap ) )
 					continue;
 
-				$class = array();
-				if ( $first ) {
-					$class[] = 'wp-first-item';
-					$first = false;
-				}
+				$sub_title = wptexturize( $sub_title );
+				$class = '';
 
-				$menu_file = $item[2];
+				if ( $sub_key == $first_sub_key )
+					$class .= ' wp-first-item';
 
-				if ( false !== ( $pos = strpos( $menu_file, '?' ) ) )
-					$menu_file = substr( $menu_file, 0, $pos );
+				// Determine if we are on the current page.
+				if ( isset( $submenu_file ) ) {
+					if ( $submenu_file == $sub_slug )
+						$class .= ' current';
 
-				// Handle current for post_type=post|page|foo pages, which won't match $self.
-				$self_type = ! empty( $typenow ) ? $self . '?post_type=' . $typenow : 'nothing';
-
-				if ( isset( $submenu_file ) ) {
-					if ( $submenu_file == $sub_item[2] )
-						$class[] = 'current';
 				// If plugin_page is set the parent must either match the current page or not physically exist.
 				// This allows plugin pages with the same hook to exist under different parents.
 				} else if (
-					( ! isset( $plugin_page ) && $self == $sub_item[2] ) ||
-					( isset( $plugin_page ) && $plugin_page == $sub_item[2] && ( $item[2] == $self_type || $item[2] == $self || file_exists($menu_file) === false ) )
+					( ! isset( $plugin_page ) && $self == $sub_slug ) ||
+					( isset( $plugin_page ) && $plugin_page == $sub_slug &&
+						( $slug == $self_type || $slug == $self || file_exists( $menu_file ) === false )
+					)
 				) {
-					$class[] = 'current';
+					$class .= ' current';
 				}
 
-				$class = $class ? ' class="' . join( ' ', $class ) . '"' : '';
-
-				$menu_hook = get_plugin_page_hook($sub_item[2], $item[2]);
-				$sub_file = $sub_item[2];
+				$sub_hook = get_plugin_page_hook( $sub_slug, $slug );
+				$sub_file  = $sub_slug;
 				if ( false !== ( $pos = strpos( $sub_file, '?' ) ) )
-					$sub_file = substr($sub_file, 0, $pos);
+					$sub_file = substr( $sub_file, 0, $pos );
 
-				$title = wptexturize($sub_item[0]);
 
-				if ( ! empty( $menu_hook ) || ( ('index.php' != $sub_item[2]) && file_exists( WP_PLUGIN_DIR . "/$sub_file" ) ) ) {
-					// If admin.php is the current page or if the parent exists as a file in the plugins or admin dir
-					if ( (!$admin_is_parent && file_exists(WP_PLUGIN_DIR . "/$menu_file") && !is_dir(WP_PLUGIN_DIR . "/{$item[2]}")) || file_exists($menu_file) )
-						$sub_item_url = add_query_arg( array('page' => $sub_item[2]), $item[2] );
-					else
-						$sub_item_url = add_query_arg( array('page' => $sub_item[2]), 'admin.php' );
+				$sub_url = $sub_slug;
+				if ( ! empty( $sub_hook ) || ( ( 'index.php' != $sub_slug ) && file_exists( WP_PLUGIN_DIR . "/$sub_file" ) ) ) {
+					$sub_url = add_query_arg( array( 'page' => $sub_slug ), $parent );
+				}
 
-					$sub_item_url = esc_url( $sub_item_url );
-					echo "<li$class><a href='$sub_item_url'$class$tabindex>$title</a></li>";
-				} else {
-					echo "<li$class><a href='{$sub_item[2]}'$class$tabindex>$title</a></li>";
-				}
+				// RENDER SUBMENU ITEM
+				echo '<li class="' . esc_attr( $class ) . '">';
+				echo '<a href="' . esc_url( $sub_url ) . '" class="' . esc_attr( $class ) . '" tabindex="1">';
+				echo $sub_title;
+				echo '</a></li>';
 			}
-			echo "</ul></div></div>";
+
+			echo '</ul></div></div>';
 		}
-		echo "</li>";
+		echo '</li>';
 	}
 
 	echo '<li id="collapse-menu" class="hide-if-no-js"><div id="collapse-button"><div></div></div>';
