PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/menu.php

https://github.com/schr/wordpress
PHP | 262 lines | 175 code | 48 blank | 39 comment | 28 complexity | d35aa68343532ba10e4c7d007eccbce9 MD5 | raw file
  1. <?php
  2. /**
  3. * Build Administration Menu.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Constructs the admin menu bar.
  10. *
  11. * The elements in the array are :
  12. * 0: Menu item name
  13. * 1: Minimum level or capability required.
  14. * 2: The URL of the item's file
  15. * 3: Class
  16. * 4: ID
  17. * 5: Icon for top level menu
  18. *
  19. * @global array $menu
  20. * @name $menu
  21. * @var array
  22. */
  23. $awaiting_mod = wp_count_comments();
  24. $awaiting_mod = $awaiting_mod->moderated;
  25. $menu[0] = array( __('Dashboard'), 'read', 'index.php', '', 'menu-top', 'menu-dashboard', 'div' );
  26. $menu[4] = array( '', 'read', '', '', 'wp-menu-separator' );
  27. $menu[5] = array( __('Posts'), 'edit_posts', 'edit.php', '', 'wp-menu-open menu-top', 'menu-posts', 'div' );
  28. $submenu['edit.php'][5] = array( __('Edit'), 'edit_posts', 'edit.php' );
  29. /* translators: add new post */
  30. $submenu['edit.php'][10] = array( _x('Add New', 'post'), 'edit_posts', 'post-new.php' );
  31. $i = 15;
  32. foreach ( $wp_taxonomies as $tax ) {
  33. if ( $tax->hierarchical || ! in_array('post', (array) $tax->object_type, true) )
  34. continue;
  35. $submenu['edit.php'][$i] = array( attribute_escape($tax->label), 'manage_categories', 'edit-tags.php?taxonomy=' . $tax->name );
  36. ++$i;
  37. }
  38. $submenu['edit.php'][50] = array( __('Categories'), 'manage_categories', 'categories.php' );
  39. $menu[10] = array( __('Media'), 'upload_files', 'upload.php', '', 'menu-top', 'menu-media', 'div' );
  40. $submenu['upload.php'][5] = array( __('Library'), 'upload_files', 'upload.php');
  41. /* translators: add new file */
  42. $submenu['upload.php'][10] = array( _x('Add New', 'file'), 'upload_files', 'media-new.php');
  43. $menu[15] = array( __('Links'), 'manage_links', 'link-manager.php', '', 'menu-top', 'menu-links', 'div' );
  44. $submenu['link-manager.php'][5] = array( __('Edit'), 'manage_links', 'link-manager.php' );
  45. /* translators: add new links */
  46. $submenu['link-manager.php'][10] = array( _x('Add New', 'links'), 'manage_links', 'link-add.php' );
  47. $submenu['link-manager.php'][15] = array( __('Link Categories'), 'manage_categories', 'edit-link-categories.php' );
  48. $menu[20] = array( __('Pages'), 'edit_pages', 'edit-pages.php', '', 'menu-top', 'menu-pages', 'div' );
  49. $submenu['edit-pages.php'][5] = array( __('Edit'), 'edit_pages', 'edit-pages.php' );
  50. /* translators: add new page */
  51. $submenu['edit-pages.php'][10] = array( _x('Add New', 'page'), 'edit_pages', 'page-new.php' );
  52. $menu[25] = array( sprintf( __('Comments %s'), "<span id='awaiting-mod' class='count-$awaiting_mod'><span class='pending-count'>" . number_format_i18n($awaiting_mod) . "</span></span>" ), 'edit_posts', 'edit-comments.php', '', 'menu-top', 'menu-comments', 'div' );
  53. $_wp_last_object_menu = 25; // The index of the last top-level menu in the object menu group
  54. $menu[59] = array( '', 'read', '', '', 'wp-menu-separator' );
  55. $menu[60] = array( __('Appearance'), 'switch_themes', 'themes.php', '', 'menu-top', 'menu-appearance', 'div' );
  56. $submenu['themes.php'][5] = array(__('Themes'), 'switch_themes', 'themes.php');
  57. $submenu['themes.php'][10] = array(__('Editor'), 'edit_themes', 'theme-editor.php');
  58. $submenu['themes.php'][15] = array(__('Add New Themes'), 'install_themes', 'theme-install.php');
  59. $update_plugins = get_transient( 'update_plugins' );
  60. $update_count = 0;
  61. if ( !empty($update_plugins->response) )
  62. $update_count = count( $update_plugins->response );
  63. $menu[65] = array( sprintf( __('Plugins %s'), "<span class='update-plugins count-$update_count'><span class='plugin-count'>" . number_format_i18n($update_count) . "</span></span>" ), 'activate_plugins', 'plugins.php', '', 'menu-top', 'menu-plugins', 'div' );
  64. $submenu['plugins.php'][5] = array( __('Installed'), 'activate_plugins', 'plugins.php' );
  65. /* translators: add new plugin */
  66. $submenu['plugins.php'][10] = array(_x('Add New', 'plugin'), 'install_plugins', 'plugin-install.php');
  67. $submenu['plugins.php'][15] = array( __('Editor'), 'edit_plugins', 'plugin-editor.php' );
  68. if ( current_user_can('edit_users') )
  69. $menu[70] = array( __('Users'), 'edit_users', 'users.php', '', 'menu-top', 'menu-users', 'div' );
  70. else
  71. $menu[70] = array( __('Profile'), 'read', 'profile.php', '', 'menu-top', 'menu-users', 'div' );
  72. if ( current_user_can('edit_users') ) {
  73. $_wp_real_parent_file['profile.php'] = 'users.php'; // Back-compat for plugins adding submenus to profile.php.
  74. $submenu['users.php'][5] = array(__('Authors &amp; Users'), 'edit_users', 'users.php');
  75. $submenu['users.php'][10] = array(__('Add New'), 'create_users', 'user-new.php');
  76. $submenu['users.php'][15] = array(__('Your Profile'), 'read', 'profile.php');
  77. } else {
  78. $_wp_real_parent_file['users.php'] = 'profile.php';
  79. $submenu['profile.php'][5] = array(__('Your Profile'), 'read', 'profile.php');
  80. }
  81. $menu[75] = array( __('Tools'), 'read', 'tools.php', '', 'menu-top', 'menu-tools', 'div' );
  82. $submenu['tools.php'][5] = array( __('Tools'), 'read', 'tools.php' );
  83. $submenu['tools.php'][10] = array( __('Import'), 'import', 'import.php' );
  84. $submenu['tools.php'][15] = array( __('Export'), 'import', 'export.php' );
  85. $submenu['tools.php'][20] = array( __('Upgrade'), 'install_plugins', 'update-core.php');
  86. $menu[80] = array( __('Settings'), 'manage_options', 'options-general.php', '', 'menu-top', 'menu-settings', 'div' );
  87. $submenu['options-general.php'][10] = array(__('General'), 'manage_options', 'options-general.php');
  88. $submenu['options-general.php'][15] = array(__('Writing'), 'manage_options', 'options-writing.php');
  89. $submenu['options-general.php'][20] = array(__('Reading'), 'manage_options', 'options-reading.php');
  90. $submenu['options-general.php'][25] = array(__('Discussion'), 'manage_options', 'options-discussion.php');
  91. $submenu['options-general.php'][30] = array(__('Media'), 'manage_options', 'options-media.php');
  92. $submenu['options-general.php'][35] = array(__('Privacy'), 'manage_options', 'options-privacy.php');
  93. $submenu['options-general.php'][40] = array(__('Permalinks'), 'manage_options', 'options-permalink.php');
  94. $submenu['options-general.php'][45] = array(__('Miscellaneous'), 'manage_options', 'options-misc.php');
  95. $_wp_last_utility_menu = 80; // The index of the last top-level menu in the utility menu group
  96. $menu[99] = array( '', 'read', '', '', 'wp-menu-separator-last' );
  97. // Back-compat for old top-levels
  98. $_wp_real_parent_file['post.php'] = 'edit.php';
  99. $_wp_real_parent_file['post-new.php'] = 'edit.php';
  100. $_wp_real_parent_file['page-new.php'] = 'edit-pages.php';
  101. do_action('_admin_menu');
  102. // Create list of page plugin hook names.
  103. foreach ($menu as $menu_page) {
  104. $admin_page_hooks[$menu_page[2]] = sanitize_title($menu_page[0]);
  105. }
  106. $_wp_submenu_nopriv = array();
  107. $_wp_menu_nopriv = array();
  108. // Loop over submenus and remove pages for which the user does not have privs.
  109. foreach ( array( 'submenu' ) as $sub_loop ) {
  110. foreach ($$sub_loop as $parent => $sub) {
  111. foreach ($sub as $index => $data) {
  112. if ( ! current_user_can($data[1]) ) {
  113. unset(${$sub_loop}[$parent][$index]);
  114. $_wp_submenu_nopriv[$parent][$data[2]] = true;
  115. }
  116. }
  117. if ( empty(${$sub_loop}[$parent]) )
  118. unset(${$sub_loop}[$parent]);
  119. }
  120. }
  121. // Loop over the top-level menu.
  122. // Menus for which the original parent is not acessible due to lack of privs will have the next
  123. // submenu in line be assigned as the new menu parent.
  124. foreach ( $menu as $id => $data ) {
  125. if ( empty($submenu[$data[2]]) )
  126. continue;
  127. $subs = $submenu[$data[2]];
  128. $first_sub = array_shift($subs);
  129. $old_parent = $data[2];
  130. $new_parent = $first_sub[2];
  131. // If the first submenu is not the same as the assigned parent,
  132. // make the first submenu the new parent.
  133. if ( $new_parent != $old_parent ) {
  134. $_wp_real_parent_file[$old_parent] = $new_parent;
  135. $menu[$id][2] = $new_parent;
  136. foreach ($submenu[$old_parent] as $index => $data) {
  137. $submenu[$new_parent][$index] = $submenu[$old_parent][$index];
  138. unset($submenu[$old_parent][$index]);
  139. }
  140. unset($submenu[$old_parent]);
  141. if ( isset($_wp_submenu_nopriv[$old_parent]) )
  142. $_wp_submenu_nopriv[$new_parent] = $_wp_submenu_nopriv[$old_parent];
  143. }
  144. }
  145. do_action('admin_menu', '');
  146. // Remove menus that have no accessible submenus and require privs that the user does not have.
  147. // Run re-parent loop again.
  148. foreach ( $menu as $id => $data ) {
  149. // If submenu is empty...
  150. if ( empty($submenu[$data[2]]) ) {
  151. // And user doesn't have privs, remove menu.
  152. if ( ! current_user_can($data[1]) ) {
  153. $_wp_menu_nopriv[$data[2]] = true;
  154. unset($menu[$id]);
  155. }
  156. }
  157. }
  158. // Remove any duplicated seperators
  159. $seperator_found = false;
  160. foreach ( $menu as $id => $data ) {
  161. if ( 0 == strcmp('wp-menu-separator', $data[4] ) ) {
  162. if (false == $seperator_found) {
  163. $seperator_found = true;
  164. } else {
  165. unset($menu[$id]);
  166. $seperator_found = false;
  167. }
  168. } else {
  169. $seperator_found = false;
  170. }
  171. }
  172. unset($id);
  173. function add_cssclass($add, $class) {
  174. $class = empty($class) ? $add : $class .= ' ' . $add;
  175. return $class;
  176. }
  177. function add_menu_classes($menu) {
  178. $first = $lastorder = false;
  179. $i = 0;
  180. $mc = count($menu);
  181. foreach ( $menu as $order => $top ) {
  182. $i++;
  183. if ( 0 == $order ) { // dashboard is always shown/single
  184. $menu[0][4] = add_cssclass('menu-top-first menu-top-last', $top[4]);
  185. continue;
  186. }
  187. if ( empty($top[2]) ) { // if separator
  188. $first = true;
  189. if ( $lastorder ) {
  190. $c = $menu[$lastorder][4];
  191. $menu[$lastorder][4] = add_cssclass('menu-top-last', $c);
  192. }
  193. continue;
  194. }
  195. if ( $first ) {
  196. $c = $menu[$order][4];
  197. $menu[$order][4] = add_cssclass('menu-top-first', $c);
  198. $first = false;
  199. }
  200. if ( $mc == $i ) { // last item
  201. $c = $menu[$order][4];
  202. $menu[$order][4] = add_cssclass('menu-top-last', $c);
  203. }
  204. $lastorder = $order;
  205. }
  206. return apply_filters( 'add_menu_classes', $menu );
  207. }
  208. uksort($menu, "strnatcasecmp"); // make it all pretty
  209. $menu = add_menu_classes($menu);
  210. if (! user_can_access_admin_page()) {
  211. do_action('admin_page_access_denied');
  212. wp_die( __('You do not have sufficient permissions to access this page.') );
  213. }
  214. ?>