menu-header.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /**
  3. * Displays Administration Menu.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * The current page.
  10. *
  11. * @global string $self
  12. */
  13. $self = preg_replace( '|^.*/wp-admin/network/|i', '', $_SERVER['PHP_SELF'] );
  14. $self = preg_replace( '|^.*/wp-admin/|i', '', $self );
  15. $self = preg_replace( '|^.*/plugins/|i', '', $self );
  16. $self = preg_replace( '|^.*/mu-plugins/|i', '', $self );
  17. /**
  18. * For when admin-header is included from within a function.
  19. *
  20. * @global array $menu
  21. * @global array $submenu
  22. * @global string $parent_file
  23. * @global string $submenu_file
  24. */
  25. global $menu, $submenu, $parent_file, $submenu_file;
  26. /**
  27. * Filters the parent file of an admin menu sub-menu item.
  28. *
  29. * Allows plugins to move sub-menu items around.
  30. *
  31. * @since MU (3.0.0)
  32. *
  33. * @param string $parent_file The parent file.
  34. */
  35. $parent_file = apply_filters( 'parent_file', $parent_file );
  36. /**
  37. * Filters the file of an admin menu sub-menu item.
  38. *
  39. * @since 4.4.0
  40. *
  41. * @param string $submenu_file The submenu file.
  42. * @param string $parent_file The submenu item's parent file.
  43. */
  44. $submenu_file = apply_filters( 'submenu_file', $submenu_file, $parent_file );
  45. get_admin_page_parent();
  46. /**
  47. * Display menu.
  48. *
  49. * @access private
  50. * @since 2.7.0
  51. *
  52. * @global string $self
  53. * @global string $parent_file
  54. * @global string $submenu_file
  55. * @global string $plugin_page
  56. * @global string $typenow The post type of the current screen.
  57. *
  58. * @param array $menu
  59. * @param array $submenu
  60. * @param bool $submenu_as_parent
  61. */
  62. function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) {
  63. global $self, $parent_file, $submenu_file, $plugin_page, $typenow;
  64. $first = true;
  65. // 0 = menu_title, 1 = capability, 2 = menu_slug, 3 = page_title, 4 = classes, 5 = hookname, 6 = icon_url.
  66. foreach ( $menu as $key => $item ) {
  67. $admin_is_parent = false;
  68. $class = array();
  69. $aria_attributes = '';
  70. $aria_hidden = '';
  71. $is_separator = false;
  72. if ( $first ) {
  73. $class[] = 'wp-first-item';
  74. $first = false;
  75. }
  76. $submenu_items = array();
  77. if ( ! empty( $submenu[ $item[2] ] ) ) {
  78. $class[] = 'wp-has-submenu';
  79. $submenu_items = $submenu[ $item[2] ];
  80. }
  81. if ( ( $parent_file && $item[2] === $parent_file ) || ( empty( $typenow ) && $self === $item[2] ) ) {
  82. if ( ! empty( $submenu_items ) ) {
  83. $class[] = 'wp-has-current-submenu wp-menu-open';
  84. } else {
  85. $class[] = 'current';
  86. $aria_attributes .= 'aria-current="page"';
  87. }
  88. } else {
  89. $class[] = 'wp-not-current-submenu';
  90. if ( ! empty( $submenu_items ) ) {
  91. $aria_attributes .= 'aria-haspopup="true"';
  92. }
  93. }
  94. if ( ! empty( $item[4] ) ) {
  95. $class[] = esc_attr( $item[4] );
  96. }
  97. $class = $class ? ' class="' . implode( ' ', $class ) . '"' : '';
  98. $id = ! empty( $item[5] ) ? ' id="' . preg_replace( '|[^a-zA-Z0-9_:.]|', '-', $item[5] ) . '"' : '';
  99. $img = '';
  100. $img_style = '';
  101. $img_class = ' dashicons-before';
  102. if ( false !== strpos( $class, 'wp-menu-separator' ) ) {
  103. $is_separator = true;
  104. }
  105. /*
  106. * If the string 'none' (previously 'div') is passed instead of a URL, don't output
  107. * the default menu image so an icon can be added to div.wp-menu-image as background
  108. * with CSS. Dashicons and base64-encoded data:image/svg_xml URIs are also handled
  109. * as special cases.
  110. */
  111. if ( ! empty( $item[6] ) ) {
  112. $img = '<img src="' . esc_url( $item[6] ) . '" alt="" />';
  113. if ( 'none' === $item[6] || 'div' === $item[6] ) {
  114. $img = '<br />';
  115. } elseif ( 0 === strpos( $item[6], 'data:image/svg+xml;base64,' ) ) {
  116. $img = '<br />';
  117. // The value is base64-encoded data, so esc_attr() is used here instead of esc_url().
  118. $img_style = ' style="background-image:url(\'' . esc_attr( $item[6] ) . '\')"';
  119. $img_class = ' svg';
  120. } elseif ( 0 === strpos( $item[6], 'dashicons-' ) ) {
  121. $img = '<br />';
  122. $img_class = ' dashicons-before ' . sanitize_html_class( $item[6] );
  123. }
  124. }
  125. $arrow = '<div class="wp-menu-arrow"><div></div></div>';
  126. $title = wptexturize( $item[0] );
  127. // Hide separators from screen readers.
  128. if ( $is_separator ) {
  129. $aria_hidden = ' aria-hidden="true"';
  130. }
  131. echo "\n\t<li$class$id$aria_hidden>";
  132. if ( $is_separator ) {
  133. echo '<div class="separator"></div>';
  134. } elseif ( $submenu_as_parent && ! empty( $submenu_items ) ) {
  135. $submenu_items = array_values( $submenu_items ); // Re-index.
  136. $menu_hook = get_plugin_page_hook( $submenu_items[0][2], $item[2] );
  137. $menu_file = $submenu_items[0][2];
  138. $pos = strpos( $menu_file, '?' );
  139. if ( false !== $pos ) {
  140. $menu_file = substr( $menu_file, 0, $pos );
  141. }
  142. if ( ! empty( $menu_hook )
  143. || ( ( 'index.php' !== $submenu_items[0][2] )
  144. && file_exists( WP_PLUGIN_DIR . "/$menu_file" )
  145. && ! file_exists( ABSPATH . "/wp-admin/$menu_file" ) )
  146. ) {
  147. $admin_is_parent = true;
  148. echo "<a href='admin.php?page={$submenu_items[0][2]}'$class $aria_attributes>$arrow<div class='wp-menu-image$img_class'$img_style aria-hidden='true'>$img</div><div class='wp-menu-name'>$title</div></a>";
  149. } else {
  150. echo "\n\t<a href='{$submenu_items[0][2]}'$class $aria_attributes>$arrow<div class='wp-menu-image$img_class'$img_style aria-hidden='true'>$img</div><div class='wp-menu-name'>$title</div></a>";
  151. }
  152. } elseif ( ! empty( $item[2] ) && current_user_can( $item[1] ) ) {
  153. $menu_hook = get_plugin_page_hook( $item[2], 'admin.php' );
  154. $menu_file = $item[2];
  155. $pos = strpos( $menu_file, '?' );
  156. if ( false !== $pos ) {
  157. $menu_file = substr( $menu_file, 0, $pos );
  158. }
  159. if ( ! empty( $menu_hook )
  160. || ( ( 'index.php' !== $item[2] )
  161. && file_exists( WP_PLUGIN_DIR . "/$menu_file" )
  162. && ! file_exists( ABSPATH . "/wp-admin/$menu_file" ) )
  163. ) {
  164. $admin_is_parent = true;
  165. echo "\n\t<a href='admin.php?page={$item[2]}'$class $aria_attributes>$arrow<div class='wp-menu-image$img_class'$img_style aria-hidden='true'>$img</div><div class='wp-menu-name'>{$item[0]}</div></a>";
  166. } else {
  167. echo "\n\t<a href='{$item[2]}'$class $aria_attributes>$arrow<div class='wp-menu-image$img_class'$img_style aria-hidden='true'>$img</div><div class='wp-menu-name'>{$item[0]}</div></a>";
  168. }
  169. }
  170. if ( ! empty( $submenu_items ) ) {
  171. echo "\n\t<ul class='wp-submenu wp-submenu-wrap'>";
  172. echo "<li class='wp-submenu-head' aria-hidden='true'>{$item[0]}</li>";
  173. $first = true;
  174. // 0 = menu_title, 1 = capability, 2 = menu_slug, 3 = page_title, 4 = classes.
  175. foreach ( $submenu_items as $sub_key => $sub_item ) {
  176. if ( ! current_user_can( $sub_item[1] ) ) {
  177. continue;
  178. }
  179. $class = array();
  180. $aria_attributes = '';
  181. if ( $first ) {
  182. $class[] = 'wp-first-item';
  183. $first = false;
  184. }
  185. $menu_file = $item[2];
  186. $pos = strpos( $menu_file, '?' );
  187. if ( false !== $pos ) {
  188. $menu_file = substr( $menu_file, 0, $pos );
  189. }
  190. // Handle current for post_type=post|page|foo pages, which won't match $self.
  191. $self_type = ! empty( $typenow ) ? $self . '?post_type=' . $typenow : 'nothing';
  192. if ( isset( $submenu_file ) ) {
  193. if ( $submenu_file === $sub_item[2] ) {
  194. $class[] = 'current';
  195. $aria_attributes .= ' aria-current="page"';
  196. }
  197. // If plugin_page is set the parent must either match the current page or not physically exist.
  198. // This allows plugin pages with the same hook to exist under different parents.
  199. } elseif (
  200. ( ! isset( $plugin_page ) && $self === $sub_item[2] )
  201. || ( isset( $plugin_page ) && $plugin_page === $sub_item[2]
  202. && ( $item[2] === $self_type || $item[2] === $self || file_exists( $menu_file ) === false ) )
  203. ) {
  204. $class[] = 'current';
  205. $aria_attributes .= ' aria-current="page"';
  206. }
  207. if ( ! empty( $sub_item[4] ) ) {
  208. $class[] = esc_attr( $sub_item[4] );
  209. }
  210. $class = $class ? ' class="' . implode( ' ', $class ) . '"' : '';
  211. $menu_hook = get_plugin_page_hook( $sub_item[2], $item[2] );
  212. $sub_file = $sub_item[2];
  213. $pos = strpos( $sub_file, '?' );
  214. if ( false !== $pos ) {
  215. $sub_file = substr( $sub_file, 0, $pos );
  216. }
  217. $title = wptexturize( $sub_item[0] );
  218. if ( ! empty( $menu_hook )
  219. || ( ( 'index.php' !== $sub_item[2] )
  220. && file_exists( WP_PLUGIN_DIR . "/$sub_file" )
  221. && ! file_exists( ABSPATH . "/wp-admin/$sub_file" ) )
  222. ) {
  223. // If admin.php is the current page or if the parent exists as a file in the plugins or admin directory.
  224. if ( ( ! $admin_is_parent && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! is_dir( WP_PLUGIN_DIR . "/{$item[2]}" ) ) || file_exists( $menu_file ) ) {
  225. $sub_item_url = add_query_arg( array( 'page' => $sub_item[2] ), $item[2] );
  226. } else {
  227. $sub_item_url = add_query_arg( array( 'page' => $sub_item[2] ), 'admin.php' );
  228. }
  229. $sub_item_url = esc_url( $sub_item_url );
  230. echo "<li$class><a href='$sub_item_url'$class$aria_attributes>$title</a></li>";
  231. } else {
  232. echo "<li$class><a href='{$sub_item[2]}'$class$aria_attributes>$title</a></li>";
  233. }
  234. }
  235. echo '</ul>';
  236. }
  237. echo '</li>';
  238. }
  239. echo '<li id="collapse-menu" class="hide-if-no-js">' .
  240. '<button type="button" id="collapse-button" aria-label="' . esc_attr__( 'Collapse Main menu' ) . '" aria-expanded="true">' .
  241. '<span class="collapse-button-icon" aria-hidden="true"></span>' .
  242. '<span class="collapse-button-label">' . __( 'Collapse menu' ) . '</span>' .
  243. '</button></li>';
  244. }
  245. ?>
  246. <div id="adminmenumain" role="navigation" aria-label="<?php esc_attr_e( 'Main menu' ); ?>">
  247. <a href="#wpbody-content" class="screen-reader-shortcut"><?php _e( 'Skip to main content' ); ?></a>
  248. <a href="#wp-toolbar" class="screen-reader-shortcut"><?php _e( 'Skip to toolbar' ); ?></a>
  249. <div id="adminmenuback"></div>
  250. <div id="adminmenuwrap">
  251. <ul id="adminmenu">
  252. <?php
  253. _wp_menu_output( $menu, $submenu );
  254. /**
  255. * Fires after the admin menu has been output.
  256. *
  257. * @since 2.5.0
  258. */
  259. do_action( 'adminmenu' );
  260. ?>
  261. </ul>
  262. </div>
  263. </div>