You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

267 lines
9.0 KiB

  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
  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
  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. $class[] = ! empty( $submenu_items ) ? 'wp-has-current-submenu wp-menu-open' : 'current';
  83. } else {
  84. $class[] = 'wp-not-current-submenu';
  85. if ( ! empty( $submenu_items ) )
  86. $aria_attributes .= 'aria-haspopup="true"';
  87. }
  88. if ( ! empty( $item[4] ) )
  89. $class[] = esc_attr( $item[4] );
  90. $class = $class ? ' class="' . join( ' ', $class ) . '"' : '';
  91. $id = ! empty( $item[5] ) ? ' id="' . preg_replace( '|[^a-zA-Z0-9_:.]|', '-', $item[5] ) . '"' : '';
  92. $img = $img_style = '';
  93. $img_class = ' dashicons-before';
  94. if ( false !== strpos( $class, 'wp-menu-separator' ) ) {
  95. $is_separator = true;
  96. }
  97. /*
  98. * If the string 'none' (previously 'div') is passed instead of a URL, don't output
  99. * the default menu image so an icon can be added to div.wp-menu-image as background
  100. * with CSS. Dashicons and base64-encoded data:image/svg_xml URIs are also handled
  101. * as special cases.
  102. */
  103. if ( ! empty( $item[6] ) ) {
  104. $img = '<img src="' . $item[6] . '" alt="" />';
  105. if ( 'none' === $item[6] || 'div' === $item[6] ) {
  106. $img = '<br />';
  107. } elseif ( 0 === strpos( $item[6], 'data:image/svg+xml;base64,' ) ) {
  108. $img = '<br />';
  109. $img_style = ' style="background-image:url(\'' . esc_attr( $item[6] ) . '\')"';
  110. $img_class = ' svg';
  111. } elseif ( 0 === strpos( $item[6], 'dashicons-' ) ) {
  112. $img = '<br />';
  113. $img_class = ' dashicons-before ' . sanitize_html_class( $item[6] );
  114. }
  115. }
  116. $arrow = '<div class="wp-menu-arrow"><div></div></div>';
  117. $title = wptexturize( $item[0] );
  118. // hide separators from screen readers
  119. if ( $is_separator ) {
  120. $aria_hidden = ' aria-hidden="true"';
  121. }
  122. echo "\n\t<li$class$id$aria_hidden>";
  123. if ( $is_separator ) {
  124. echo '<div class="separator"></div>';
  125. } elseif ( $submenu_as_parent && ! empty( $submenu_items ) ) {
  126. $submenu_items = array_values( $submenu_items ); // Re-index.
  127. $menu_hook = get_plugin_page_hook( $submenu_items[0][2], $item[2] );
  128. $menu_file = $submenu_items[0][2];
  129. if ( false !== ( $pos = strpos( $menu_file, '?' ) ) )
  130. $menu_file = substr( $menu_file, 0, $pos );
  131. if ( ! empty( $menu_hook ) || ( ( 'index.php' != $submenu_items[0][2] ) && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! file_exists( ABSPATH . "/wp-admin/$menu_file" ) ) ) {
  132. $admin_is_parent = true;
  133. echo "<a href='admin.php?page={$submenu_items[0][2]}'$class $aria_attributes>$arrow<div class='wp-menu-image$img_class'$img_style>$img</div><div class='wp-menu-name'>$title</div></a>";
  134. } else {
  135. echo "\n\t<a href='{$submenu_items[0][2]}'$class $aria_attributes>$arrow<div class='wp-menu-image$img_class'$img_style>$img</div><div class='wp-menu-name'>$title</div></a>";
  136. }
  137. } elseif ( ! empty( $item[2] ) && current_user_can( $item[1] ) ) {
  138. $menu_hook = get_plugin_page_hook( $item[2], 'admin.php' );
  139. $menu_file = $item[2];
  140. if ( false !== ( $pos = strpos( $menu_file, '?' ) ) )
  141. $menu_file = substr( $menu_file, 0, $pos );
  142. if ( ! empty( $menu_hook ) || ( ( 'index.php' != $item[2] ) && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! file_exists( ABSPATH . "/wp-admin/$menu_file" ) ) ) {
  143. $admin_is_parent = true;
  144. echo "\n\t<a href='admin.php?page={$item[2]}'$class $aria_attributes>$arrow<div class='wp-menu-image$img_class'$img_style>$img</div><div class='wp-menu-name'>{$item[0]}</div></a>";
  145. } else {
  146. echo "\n\t<a href='{$item[2]}'$class $aria_attributes>$arrow<div class='wp-menu-image$img_class'$img_style>$img</div><div class='wp-menu-name'>{$item[0]}</div></a>";
  147. }
  148. }
  149. if ( ! empty( $submenu_items ) ) {
  150. echo "\n\t<ul class='wp-submenu wp-submenu-wrap'>";
  151. echo "<li class='wp-submenu-head' aria-hidden='true'>{$item[0]}</li>";
  152. $first = true;
  153. // 0 = menu_title, 1 = capability, 2 = menu_slug, 3 = page_title, 4 = classes
  154. foreach ( $submenu_items as $sub_key => $sub_item ) {
  155. if ( ! current_user_can( $sub_item[1] ) )
  156. continue;
  157. $class = array();
  158. if ( $first ) {
  159. $class[] = 'wp-first-item';
  160. $first = false;
  161. }
  162. $menu_file = $item[2];
  163. if ( false !== ( $pos = strpos( $menu_file, '?' ) ) )
  164. $menu_file = substr( $menu_file, 0, $pos );
  165. // Handle current for post_type=post|page|foo pages, which won't match $self.
  166. $self_type = ! empty( $typenow ) ? $self . '?post_type=' . $typenow : 'nothing';
  167. if ( isset( $submenu_file ) ) {
  168. if ( $submenu_file == $sub_item[2] )
  169. $class[] = 'current';
  170. // If plugin_page is set the parent must either match the current page or not physically exist.
  171. // This allows plugin pages with the same hook to exist under different parents.
  172. } elseif (
  173. ( ! isset( $plugin_page ) && $self == $sub_item[2] ) ||
  174. ( isset( $plugin_page ) && $plugin_page == $sub_item[2] && ( $item[2] == $self_type || $item[2] == $self || file_exists($menu_file) === false ) )
  175. ) {
  176. $class[] = 'current';
  177. }
  178. if ( ! empty( $sub_item[4] ) ) {
  179. $class[] = esc_attr( $sub_item[4] );
  180. }
  181. $class = $class ? ' class="' . join( ' ', $class ) . '"' : '';
  182. $menu_hook = get_plugin_page_hook($sub_item[2], $item[2]);
  183. $sub_file = $sub_item[2];
  184. if ( false !== ( $pos = strpos( $sub_file, '?' ) ) )
  185. $sub_file = substr($sub_file, 0, $pos);
  186. $title = wptexturize($sub_item[0]);
  187. if ( ! empty( $menu_hook ) || ( ( 'index.php' != $sub_item[2] ) && file_exists( WP_PLUGIN_DIR . "/$sub_file" ) && ! file_exists( ABSPATH . "/wp-admin/$sub_file" ) ) ) {
  188. // If admin.php is the current page or if the parent exists as a file in the plugins or admin dir
  189. if ( ( ! $admin_is_parent && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! is_dir( WP_PLUGIN_DIR . "/{$item[2]}" ) ) || file_exists( $menu_file ) )
  190. $sub_item_url = add_query_arg( array( 'page' => $sub_item[2] ), $item[2] );
  191. else
  192. $sub_item_url = add_query_arg( array( 'page' => $sub_item[2] ), 'admin.php' );
  193. $sub_item_url = esc_url( $sub_item_url );
  194. echo "<li$class><a href='$sub_item_url'$class>$title</a></li>";
  195. } else {
  196. echo "<li$class><a href='{$sub_item[2]}'$class>$title</a></li>";
  197. }
  198. }
  199. echo "</ul>";
  200. }
  201. echo "</li>";
  202. }
  203. echo '<li id="collapse-menu" class="hide-if-no-js">' .
  204. '<button type="button" id="collapse-button" aria-label="' . esc_attr__( 'Collapse Main menu' ) . '" aria-expanded="true">' .
  205. '<span class="collapse-button-icon" aria-hidden="true"></span>' .
  206. '<span class="collapse-button-label">' . __( 'Collapse menu' ) . '</span>' .
  207. '</button></li>';
  208. }
  209. ?>
  210. <div id="adminmenumain" role="navigation" aria-label="<?php esc_attr_e( 'Main menu' ); ?>">
  211. <a href="#wpbody-content" class="screen-reader-shortcut"><?php _e( 'Skip to main content' ); ?></a>
  212. <a href="#wp-toolbar" class="screen-reader-shortcut"><?php _e( 'Skip to toolbar' ); ?></a>
  213. <div id="adminmenuback"></div>
  214. <div id="adminmenuwrap">
  215. <ul id="adminmenu">
  216. <?php
  217. _wp_menu_output( $menu, $submenu );
  218. /**
  219. * Fires after the admin menu has been output.
  220. *
  221. * @since 2.5.0
  222. */
  223. do_action( 'adminmenu' );
  224. ?>
  225. </ul>
  226. </div>
  227. </div>