web/wp-admin/menu-header.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
    11  *
    11  *
    12  * @global string $self
    12  * @global string $self
    13  * @name $self
    13  * @name $self
    14  * @var string
    14  * @var string
    15  */
    15  */
    16 $self = preg_replace('|^.*/wp-admin/|i', '', $_SERVER['PHP_SELF']);
    16 $self = preg_replace('|^.*/wp-admin/network/|i', '', $_SERVER['PHP_SELF']);
       
    17 $self = preg_replace('|^.*/wp-admin/|i', '', $self);
    17 $self = preg_replace('|^.*/plugins/|i', '', $self);
    18 $self = preg_replace('|^.*/plugins/|i', '', $self);
       
    19 $self = preg_replace('|^.*/mu-plugins/|i', '', $self);
    18 
    20 
    19 global $menu, $submenu, $parent_file; //For when admin-header is included from within a function.
    21 global $menu, $submenu, $parent_file; //For when admin-header is included from within a function.
       
    22 $parent_file = apply_filters("parent_file", $parent_file); // For plugins to move submenu tabs around.
    20 
    23 
    21 get_admin_page_parent();
    24 get_admin_page_parent();
    22 
    25 
    23 /**
    26 /**
    24  * Display menu.
    27  * Display menu.
    29  * @param array $menu
    32  * @param array $menu
    30  * @param array $submenu
    33  * @param array $submenu
    31  * @param bool $submenu_as_parent
    34  * @param bool $submenu_as_parent
    32  */
    35  */
    33 function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) {
    36 function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) {
    34 	global $self, $parent_file, $submenu_file, $plugin_page, $pagenow;
    37 	global $self, $parent_file, $submenu_file, $plugin_page, $pagenow, $typenow;
    35 
    38 
    36 	$first = true;
    39 	$first = true;
    37 	// 0 = name, 1 = capability, 2 = file, 3 = class, 4 = id, 5 = icon src
    40 	// 0 = name, 1 = capability, 2 = file, 3 = class, 4 = id, 5 = icon src
    38 	foreach ( $menu as $key => $item ) {
    41 	foreach ( $menu as $key => $item ) {
    39 		$admin_is_parent = false;
    42 		$admin_is_parent = false;
    40 		$class = array();
    43 		$class = array();
       
    44 		$aria_attributes = 'tabindex="1"';
       
    45 
    41 		if ( $first ) {
    46 		if ( $first ) {
    42 			$class[] = 'wp-first-item';
    47 			$class[] = 'wp-first-item';
    43 			$first = false;
    48 			$first = false;
    44 		}
    49 		}
    45 		if ( !empty($submenu[$item[2]]) )
    50 
       
    51 		$submenu_items = false;
       
    52 		if ( ! empty( $submenu[$item[2]] ) ) {
    46 			$class[] = 'wp-has-submenu';
    53 			$class[] = 'wp-has-submenu';
    47 
    54 			$submenu_items = $submenu[$item[2]];
    48 		if ( ( $parent_file && $item[2] == $parent_file ) || strcmp($self, $item[2]) == 0 ) {
       
    49 			if ( !empty($submenu[$item[2]]) )
       
    50 				$class[] = 'wp-has-current-submenu wp-menu-open';
       
    51 			else
       
    52 				$class[] = 'current';
       
    53 		}
    55 		}
    54 
    56 
    55 		if ( isset($item[4]) && ! empty($item[4]) )
    57 		if ( ( $parent_file && $item[2] == $parent_file ) || ( empty($typenow) && $self == $item[2] ) ) {
       
    58 			$class[] = ! empty( $submenu_items ) ? 'wp-has-current-submenu wp-menu-open' : 'current';
       
    59 		} else {
       
    60 			$class[] = 'wp-not-current-submenu';
       
    61 			if ( ! empty( $submenu_items ) )
       
    62 				$aria_attributes .= ' aria-haspopup="true"';
       
    63 		}
       
    64 
       
    65 		if ( ! empty( $item[4] ) )
    56 			$class[] = $item[4];
    66 			$class[] = $item[4];
    57 
    67 
    58 		$class = $class ? ' class="' . join( ' ', $class ) . '"' : '';
    68 		$class = $class ? ' class="' . join( ' ', $class ) . '"' : '';
    59 		$tabindex = ' tabindex="1"';
    69 		$id = ! empty( $item[5] ) ? ' id="' . preg_replace( '|[^a-zA-Z0-9_:.]|', '-', $item[5] ) . '"' : '';
    60 		$id = isset($item[5]) && ! empty($item[5]) ? ' id="' . preg_replace( '|[^a-zA-Z0-9_:.]|', '-', $item[5] ) . '"' : '';
       
    61 		$img = '';
    70 		$img = '';
    62 		if ( isset($item[6]) && ! empty($item[6]) ) {
    71 		if ( ! empty( $item[6] ) )
    63 			if ( 'div' === $item[6] )
    72 			$img = ( 'div' === $item[6] ) ? '<br />' : '<img src="' . $item[6] . '" alt="" />';
    64 				$img = '<br />';
    73 		$arrow = '<div class="wp-menu-arrow"><div></div></div>';
    65 			else
    74 
    66 				$img = '<img src="' . $item[6] . '" alt="" />';
    75 		$title = wptexturize( $item[0] );
    67 		}
    76 		$aria_label = esc_attr( strip_tags( $item[0] ) ); // strip the comment/plugins/updates bubbles spans but keep the pending number if any
    68 		$toggle = '<div class="wp-menu-toggle"><br /></div>';
       
    69 
    77 
    70 		echo "\n\t<li$class$id>";
    78 		echo "\n\t<li$class$id>";
    71 
    79 
    72 		if ( false !== strpos($class, 'wp-menu-separator') ) {
    80 		if ( false !== strpos( $class, 'wp-menu-separator' ) ) {
    73 			echo '<a class="separator" href="?unfoldmenu=1"><br /></a>';
    81 			echo '<div class="separator"></div>';
    74 		} elseif ( $submenu_as_parent && !empty($submenu[$item[2]]) ) {
    82 		} elseif ( $submenu_as_parent && ! empty( $submenu_items ) ) {
    75 			$submenu[$item[2]] = array_values($submenu[$item[2]]);  // Re-index.
    83 			$submenu_items = array_values( $submenu_items );  // Re-index.
    76 			$menu_hook = get_plugin_page_hook($submenu[$item[2]][0][2], $item[2]);
    84 			$menu_hook = get_plugin_page_hook( $submenu_items[0][2], $item[2] );
    77 			$menu_file = $submenu[$item[2]][0][2];
    85 			$menu_file = $submenu_items[0][2];
    78 			if ( false !== $pos = strpos($menu_file, '?') )
    86 			if ( false !== ( $pos = strpos( $menu_file, '?' ) ) )
    79 				$menu_file = substr($menu_file, 0, $pos);
    87 				$menu_file = substr( $menu_file, 0, $pos );
    80 			if ( ( ('index.php' != $submenu[$item[2]][0][2]) && file_exists(WP_PLUGIN_DIR . "/$menu_file") ) || !empty($menu_hook)) {
    88 			if ( ! empty( $menu_hook ) || ( ('index.php' != $submenu_items[0][2]) && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) ) ) {
    81 				$admin_is_parent = true;
    89 				$admin_is_parent = true;
    82 				echo "<div class='wp-menu-image'><a href='admin.php?page={$submenu[$item[2]][0][2]}'>$img</a></div>$toggle<a href='admin.php?page={$submenu[$item[2]][0][2]}'$class$tabindex>{$item[0]}</a>";
    90 				echo "<div class='wp-menu-image'><a href='admin.php?page={$submenu_items[0][2]}' aria-label='$aria_label'>$img</a></div>$arrow<a href='admin.php?page={$submenu_items[0][2]}'$class $aria_attributes>$title</a>";
    83 			} else {
    91 			} else {
    84 				echo "\n\t<div class='wp-menu-image'><a href='{$submenu[$item[2]][0][2]}'>$img</a></div>$toggle<a href='{$submenu[$item[2]][0][2]}'$class$tabindex>{$item[0]}</a>";
    92 				echo "\n\t<div class='wp-menu-image'><a href='{$submenu_items[0][2]}' aria-label='$aria_label'>$img</a></div>$arrow<a href='{$submenu_items[0][2]}'$class $aria_attributes>$title</a>";
    85 			}
    93 			}
    86 		} else if ( current_user_can($item[1]) ) {
    94 		} elseif ( ! empty( $item[2] ) && current_user_can( $item[1] ) ) {
    87 			$menu_hook = get_plugin_page_hook($item[2], 'admin.php');
    95 			$menu_hook = get_plugin_page_hook( $item[2], 'admin.php' );
    88 			$menu_file = $item[2];
    96 			$menu_file = $item[2];
    89 			if ( false !== $pos = strpos($menu_file, '?') )
    97 			if ( false !== ( $pos = strpos( $menu_file, '?' ) ) )
    90 				$menu_file = substr($menu_file, 0, $pos);
    98 				$menu_file = substr( $menu_file, 0, $pos );
    91 			if ( ('index.php' != $item[2]) && file_exists(WP_PLUGIN_DIR . "/$menu_file") || !empty($menu_hook) ) {
    99 			if ( ! empty( $menu_hook ) || ( ('index.php' != $item[2]) && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) ) ) {
    92 				$admin_is_parent = true;
   100 				$admin_is_parent = true;
    93 				echo "\n\t<div class='wp-menu-image'><a href='admin.php?page={$item[2]}'>$img</a></div>$toggle<a href='admin.php?page={$item[2]}'$class$tabindex>{$item[0]}</a>";
   101 				echo "\n\t<div class='wp-menu-image'><a href='admin.php?page={$item[2]}' aria-label='$aria_label'>$img</a></div>$arrow<a href='admin.php?page={$item[2]}'$class $aria_attributes>{$item[0]}</a>";
    94 			} else {
   102 			} else {
    95 				echo "\n\t<div class='wp-menu-image'><a href='{$item[2]}'>$img</a></div>$toggle<a href='{$item[2]}'$class$tabindex>{$item[0]}</a>";
   103 				echo "\n\t<div class='wp-menu-image'><a href='{$item[2]}' aria-label='$aria_label'>$img</a></div>$arrow<a href='{$item[2]}'$class $aria_attributes>{$item[0]}</a>";
    96 			}
   104 			}
    97 		}
   105 		}
    98 
   106 
    99 		if ( !empty($submenu[$item[2]]) ) {
   107 		if ( ! empty( $submenu_items ) ) {
   100 			echo "\n\t<div class='wp-submenu'><div class='wp-submenu-head'>{$item[0]}</div><ul>";
   108 			echo "\n\t<div class='wp-submenu'><div class='wp-submenu-wrap'>";
       
   109 			echo "<div class='wp-submenu-head'>{$item[0]}</div><ul>";
   101 			$first = true;
   110 			$first = true;
   102 			foreach ( $submenu[$item[2]] as $sub_key => $sub_item ) {
   111 			foreach ( $submenu_items as $sub_key => $sub_item ) {
   103 				if ( !current_user_can($sub_item[1]) )
   112 				if ( ! current_user_can( $sub_item[1] ) )
   104 					continue;
   113 					continue;
   105 
   114 
       
   115 				$aria_attributes = 'tabindex="1"';
   106 				$class = array();
   116 				$class = array();
   107 				if ( $first ) {
   117 				if ( $first ) {
   108 					$class[] = 'wp-first-item';
   118 					$class[] = 'wp-first-item';
   109 					$first = false;
   119 					$first = false;
   110 				}
   120 				}
   111 
   121 
   112 				$menu_file = $item[2];
   122 				$menu_file = $item[2];
   113 				if ( false !== $pos = strpos($menu_file, '?') )
       
   114 					$menu_file = substr($menu_file, 0, $pos);
       
   115 
   123 
   116 				if ( isset($submenu_file) ) {
   124 				if ( false !== ( $pos = strpos( $menu_file, '?' ) ) )
       
   125 					$menu_file = substr( $menu_file, 0, $pos );
       
   126 
       
   127 				// Handle current for post_type=post|page|foo pages, which won't match $self.
       
   128 				$self_type = ! empty( $typenow ) ? $self . '?post_type=' . $typenow : 'nothing';
       
   129 
       
   130 				if ( isset( $submenu_file ) ) {
   117 					if ( $submenu_file == $sub_item[2] )
   131 					if ( $submenu_file == $sub_item[2] )
   118 						$class[] = 'current';
   132 						$class[] = 'current';
   119 				// If plugin_page is set the parent must either match the current page or not physically exist.
   133 				// If plugin_page is set the parent must either match the current page or not physically exist.
   120 				// This allows plugin pages with the same hook to exist under different parents.
   134 				// This allows plugin pages with the same hook to exist under different parents.
   121 				} else if ( (isset($plugin_page) && $plugin_page == $sub_item[2] && (!file_exists($menu_file) || ($item[2] == $self))) || (!isset($plugin_page) && $self == $sub_item[2]) ) {
   135 				} else if (
       
   136 					( ! isset( $plugin_page ) && $self == $sub_item[2] ) ||
       
   137 					( isset( $plugin_page ) && $plugin_page == $sub_item[2] && ( $item[2] == $self_type || $item[2] == $self || file_exists($menu_file) === false ) )
       
   138 				) {
   122 					$class[] = 'current';
   139 					$class[] = 'current';
   123 				}
   140 				}
   124 
   141 
   125 				$class = $class ? ' class="' . join( ' ', $class ) . '"' : '';
   142 				$class = $class ? ' class="' . join( ' ', $class ) . '"' : '';
   126 
   143 
   127 				$menu_hook = get_plugin_page_hook($sub_item[2], $item[2]);
   144 				$menu_hook = get_plugin_page_hook($sub_item[2], $item[2]);
   128 				$sub_file = $sub_item[2];
   145 				$sub_file = $sub_item[2];
   129 				if ( false !== $pos = strpos($sub_file, '?') )
   146 				if ( false !== ( $pos = strpos( $sub_file, '?' ) ) )
   130 					$sub_file = substr($sub_file, 0, $pos);
   147 					$sub_file = substr($sub_file, 0, $pos);
   131 
   148 
   132 				if ( ( ('index.php' != $sub_item[2]) && file_exists(WP_PLUGIN_DIR . "/$sub_file") ) || ! empty($menu_hook) ) {
   149 				$title = wptexturize($sub_item[0]);
       
   150 
       
   151 				if ( ! empty( $menu_hook ) || ( ('index.php' != $sub_item[2]) && file_exists( WP_PLUGIN_DIR . "/$sub_file" ) ) ) {
   133 					// If admin.php is the current page or if the parent exists as a file in the plugins or admin dir
   152 					// If admin.php is the current page or if the parent exists as a file in the plugins or admin dir
       
   153 					if ( (!$admin_is_parent && file_exists(WP_PLUGIN_DIR . "/$menu_file") && !is_dir(WP_PLUGIN_DIR . "/{$item[2]}")) || file_exists($menu_file) )
       
   154 						$sub_item_url = add_query_arg( array('page' => $sub_item[2]), $item[2] );
       
   155 					else
       
   156 						$sub_item_url = add_query_arg( array('page' => $sub_item[2]), 'admin.php' );
   134 
   157 
   135 					$parent_exists = (!$admin_is_parent && file_exists(WP_PLUGIN_DIR . "/$menu_file") && !is_dir(WP_PLUGIN_DIR . "/{$item[2]}") ) || file_exists($menu_file);
   158 					$sub_item_url = esc_url( $sub_item_url );
   136 					if ( $parent_exists )
   159 					echo "<li$class><a href='$sub_item_url'$class $aria_attributes>$title</a></li>";
   137 						echo "<li$class><a href='{$item[2]}?page={$sub_item[2]}'$class$tabindex>{$sub_item[0]}</a></li>";
       
   138 					elseif ( 'admin.php' == $pagenow || !$parent_exists )
       
   139 						echo "<li$class><a href='admin.php?page={$sub_item[2]}'$class$tabindex>{$sub_item[0]}</a></li>";
       
   140 					else
       
   141 						echo "<li$class><a href='{$item[2]}?page={$sub_item[2]}'$class$tabindex>{$sub_item[0]}</a></li>";
       
   142 				} else {
   160 				} else {
   143 					echo "<li$class><a href='{$sub_item[2]}'$class$tabindex>{$sub_item[0]}</a></li>";
   161 					echo "<li$class><a href='{$sub_item[2]}'$class $aria_attributes>$title</a></li>";
   144 				}
   162 				}
   145 			}
   163 			}
   146 			echo "</ul></div>";
   164 			echo "</ul></div></div>";
   147 		}
   165 		}
   148 		echo "</li>";
   166 		echo "</li>";
   149 	}
   167 	}
       
   168 
       
   169 	echo '<li id="collapse-menu" class="hide-if-no-js"><div id="collapse-button"><div></div></div>';
       
   170 	echo '<span>' . esc_html__( 'Collapse menu' ) . '</span>';
       
   171 	echo '</li>';
   150 }
   172 }
   151 
   173 
   152 ?>
   174 ?>
   153 
   175 
   154 <ul id="adminmenu">
   176 <div id="adminmenuback"></div>
       
   177 <div id="adminmenuwrap">
       
   178 <div id="adminmenushadow"></div>
       
   179 <ul id="adminmenu" role="navigation">
   155 
   180 
   156 <?php
   181 <?php
   157 
   182 
   158 _wp_menu_output( $menu, $submenu );
   183 _wp_menu_output( $menu, $submenu );
   159 do_action( 'adminmenu' );
   184 do_action( 'adminmenu' );
   160 
   185 
   161 ?>
   186 ?>
   162 </ul>
   187 </ul>
       
   188 </div>