menu.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. /**
  3. * Build Administration Menu.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. if ( is_network_admin() ) {
  9. /**
  10. * Fires before the administration menu loads in the Network Admin.
  11. *
  12. * The hook fires before menus and sub-menus are removed based on user privileges.
  13. *
  14. * @private
  15. * @since 3.1.0
  16. */
  17. do_action( '_network_admin_menu' );
  18. } elseif ( is_user_admin() ) {
  19. /**
  20. * Fires before the administration menu loads in the User Admin.
  21. *
  22. * The hook fires before menus and sub-menus are removed based on user privileges.
  23. *
  24. * @private
  25. * @since 3.1.0
  26. */
  27. do_action( '_user_admin_menu' );
  28. } else {
  29. /**
  30. * Fires before the administration menu loads in the admin.
  31. *
  32. * The hook fires before menus and sub-menus are removed based on user privileges.
  33. *
  34. * @private
  35. * @since 2.2.0
  36. */
  37. do_action( '_admin_menu' );
  38. }
  39. // Create list of page plugin hook names.
  40. foreach ( $menu as $menu_page ) {
  41. $pos = strpos( $menu_page[2], '?' );
  42. if ( false !== $pos ) {
  43. // Handle post_type=post|page|foo pages.
  44. $hook_name = substr( $menu_page[2], 0, $pos );
  45. $hook_args = substr( $menu_page[2], $pos + 1 );
  46. wp_parse_str( $hook_args, $hook_args );
  47. // Set the hook name to be the post type.
  48. if ( isset( $hook_args['post_type'] ) ) {
  49. $hook_name = $hook_args['post_type'];
  50. } else {
  51. $hook_name = basename( $hook_name, '.php' );
  52. }
  53. unset( $hook_args );
  54. } else {
  55. $hook_name = basename( $menu_page[2], '.php' );
  56. }
  57. $hook_name = sanitize_title( $hook_name );
  58. if ( isset( $compat[ $hook_name ] ) ) {
  59. $hook_name = $compat[ $hook_name ];
  60. } elseif ( ! $hook_name ) {
  61. continue;
  62. }
  63. $admin_page_hooks[ $menu_page[2] ] = $hook_name;
  64. }
  65. unset( $menu_page, $compat );
  66. $_wp_submenu_nopriv = array();
  67. $_wp_menu_nopriv = array();
  68. // Loop over submenus and remove pages for which the user does not have privs.
  69. foreach ( $submenu as $parent => $sub ) {
  70. foreach ( $sub as $index => $data ) {
  71. if ( ! current_user_can( $data[1] ) ) {
  72. unset( $submenu[ $parent ][ $index ] );
  73. $_wp_submenu_nopriv[ $parent ][ $data[2] ] = true;
  74. }
  75. }
  76. unset( $index, $data );
  77. if ( empty( $submenu[ $parent ] ) ) {
  78. unset( $submenu[ $parent ] );
  79. }
  80. }
  81. unset( $sub, $parent );
  82. /*
  83. * Loop over the top-level menu.
  84. * Menus for which the original parent is not accessible due to lack of privileges
  85. * will have the next submenu in line be assigned as the new menu parent.
  86. */
  87. foreach ( $menu as $id => $data ) {
  88. if ( empty( $submenu[ $data[2] ] ) ) {
  89. continue;
  90. }
  91. $subs = $submenu[ $data[2] ];
  92. $first_sub = reset( $subs );
  93. $old_parent = $data[2];
  94. $new_parent = $first_sub[2];
  95. /*
  96. * If the first submenu is not the same as the assigned parent,
  97. * make the first submenu the new parent.
  98. */
  99. if ( $new_parent != $old_parent ) {
  100. $_wp_real_parent_file[ $old_parent ] = $new_parent;
  101. $menu[ $id ][2] = $new_parent;
  102. foreach ( $submenu[ $old_parent ] as $index => $data ) {
  103. $submenu[ $new_parent ][ $index ] = $submenu[ $old_parent ][ $index ];
  104. unset( $submenu[ $old_parent ][ $index ] );
  105. }
  106. unset( $submenu[ $old_parent ], $index );
  107. if ( isset( $_wp_submenu_nopriv[ $old_parent ] ) ) {
  108. $_wp_submenu_nopriv[ $new_parent ] = $_wp_submenu_nopriv[ $old_parent ];
  109. }
  110. }
  111. }
  112. unset( $id, $data, $subs, $first_sub, $old_parent, $new_parent );
  113. if ( is_network_admin() ) {
  114. /**
  115. * Fires before the administration menu loads in the Network Admin.
  116. *
  117. * @since 3.1.0
  118. *
  119. * @param string $context Empty context.
  120. */
  121. do_action( 'network_admin_menu', '' );
  122. } elseif ( is_user_admin() ) {
  123. /**
  124. * Fires before the administration menu loads in the User Admin.
  125. *
  126. * @since 3.1.0
  127. *
  128. * @param string $context Empty context.
  129. */
  130. do_action( 'user_admin_menu', '' );
  131. } else {
  132. /**
  133. * Fires before the administration menu loads in the admin.
  134. *
  135. * @since 1.5.0
  136. *
  137. * @param string $context Empty context.
  138. */
  139. do_action( 'admin_menu', '' );
  140. }
  141. /*
  142. * Remove menus that have no accessible submenus and require privileges
  143. * that the user does not have. Run re-parent loop again.
  144. */
  145. foreach ( $menu as $id => $data ) {
  146. if ( ! current_user_can( $data[1] ) ) {
  147. $_wp_menu_nopriv[ $data[2] ] = true;
  148. }
  149. /*
  150. * If there is only one submenu and it is has same destination as the parent,
  151. * remove the submenu.
  152. */
  153. if ( ! empty( $submenu[ $data[2] ] ) && 1 === count( $submenu[ $data[2] ] ) ) {
  154. $subs = $submenu[ $data[2] ];
  155. $first_sub = reset( $subs );
  156. if ( $data[2] == $first_sub[2] ) {
  157. unset( $submenu[ $data[2] ] );
  158. }
  159. }
  160. // If submenu is empty...
  161. if ( empty( $submenu[ $data[2] ] ) ) {
  162. // And user doesn't have privs, remove menu.
  163. if ( isset( $_wp_menu_nopriv[ $data[2] ] ) ) {
  164. unset( $menu[ $id ] );
  165. }
  166. }
  167. }
  168. unset( $id, $data, $subs, $first_sub );
  169. /**
  170. * Adds a CSS class to a string.
  171. *
  172. * @since 2.7.0
  173. *
  174. * @param string $class_to_add The CSS class to add.
  175. * @param string $classes The string to add the CSS class to.
  176. * @return string The string with the CSS class added.
  177. */
  178. function add_cssclass( $class_to_add, $classes ) {
  179. if ( empty( $classes ) ) {
  180. return $class_to_add;
  181. }
  182. return $classes . ' ' . $class_to_add;
  183. }
  184. /**
  185. * Adds CSS classes for top-level administration menu items.
  186. *
  187. * The list of added classes includes `.menu-top-first` and `.menu-top-last`.
  188. *
  189. * @since 2.7.0
  190. *
  191. * @param array $menu The array of administration menu items.
  192. * @return array The array of administration menu items with the CSS classes added.
  193. */
  194. function add_menu_classes( $menu ) {
  195. $first_item = false;
  196. $last_order = false;
  197. $items_count = count( $menu );
  198. $i = 0;
  199. foreach ( $menu as $order => $top ) {
  200. $i++;
  201. if ( 0 == $order ) { // Dashboard is always shown/single.
  202. $menu[0][4] = add_cssclass( 'menu-top-first', $top[4] );
  203. $last_order = 0;
  204. continue;
  205. }
  206. if ( 0 === strpos( $top[2], 'separator' ) && false !== $last_order ) { // If separator.
  207. $first_item = true;
  208. $classes = $menu[ $last_order ][4];
  209. $menu[ $last_order ][4] = add_cssclass( 'menu-top-last', $classes );
  210. continue;
  211. }
  212. if ( $first_item ) {
  213. $classes = $menu[ $order ][4];
  214. $menu[ $order ][4] = add_cssclass( 'menu-top-first', $classes );
  215. $first_item = false;
  216. }
  217. if ( $i == $items_count ) { // Last item.
  218. $classes = $menu[ $order ][4];
  219. $menu[ $order ][4] = add_cssclass( 'menu-top-last', $classes );
  220. }
  221. $last_order = $order;
  222. }
  223. /**
  224. * Filters administration menu array with classes added for top-level items.
  225. *
  226. * @since 2.7.0
  227. *
  228. * @param array $menu Associative array of administration menu items.
  229. */
  230. return apply_filters( 'add_menu_classes', $menu );
  231. }
  232. uksort( $menu, 'strnatcasecmp' ); // Make it all pretty.
  233. /**
  234. * Filters whether to enable custom ordering of the administration menu.
  235. *
  236. * See the {@see 'menu_order'} filter for reordering menu items.
  237. *
  238. * @since 2.8.0
  239. *
  240. * @param bool $custom Whether custom ordering is enabled. Default false.
  241. */
  242. if ( apply_filters( 'custom_menu_order', false ) ) {
  243. $menu_order = array();
  244. foreach ( $menu as $menu_item ) {
  245. $menu_order[] = $menu_item[2];
  246. }
  247. unset( $menu_item );
  248. $default_menu_order = $menu_order;
  249. /**
  250. * Filters the order of administration menu items.
  251. *
  252. * A truthy value must first be passed to the {@see 'custom_menu_order'} filter
  253. * for this filter to work. Use the following to enable custom menu ordering:
  254. *
  255. * add_filter( 'custom_menu_order', '__return_true' );
  256. *
  257. * @since 2.8.0
  258. *
  259. * @param array $menu_order An ordered array of menu items.
  260. */
  261. $menu_order = apply_filters( 'menu_order', $menu_order );
  262. $menu_order = array_flip( $menu_order );
  263. $default_menu_order = array_flip( $default_menu_order );
  264. /**
  265. * @global array $menu_order
  266. * @global array $default_menu_order
  267. *
  268. * @param array $a
  269. * @param array $b
  270. * @return int
  271. */
  272. function sort_menu( $a, $b ) {
  273. global $menu_order, $default_menu_order;
  274. $a = $a[2];
  275. $b = $b[2];
  276. if ( isset( $menu_order[ $a ] ) && ! isset( $menu_order[ $b ] ) ) {
  277. return -1;
  278. } elseif ( ! isset( $menu_order[ $a ] ) && isset( $menu_order[ $b ] ) ) {
  279. return 1;
  280. } elseif ( isset( $menu_order[ $a ] ) && isset( $menu_order[ $b ] ) ) {
  281. if ( $menu_order[ $a ] == $menu_order[ $b ] ) {
  282. return 0;
  283. }
  284. return ( $menu_order[ $a ] < $menu_order[ $b ] ) ? -1 : 1;
  285. } else {
  286. return ( $default_menu_order[ $a ] <= $default_menu_order[ $b ] ) ? -1 : 1;
  287. }
  288. }
  289. usort( $menu, 'sort_menu' );
  290. unset( $menu_order, $default_menu_order );
  291. }
  292. // Prevent adjacent separators.
  293. $prev_menu_was_separator = false;
  294. foreach ( $menu as $id => $data ) {
  295. if ( false === stristr( $data[4], 'wp-menu-separator' ) ) {
  296. // This item is not a separator, so falsey the toggler and do nothing.
  297. $prev_menu_was_separator = false;
  298. } else {
  299. // The previous item was a separator, so unset this one.
  300. if ( true === $prev_menu_was_separator ) {
  301. unset( $menu[ $id ] );
  302. }
  303. // This item is a separator, so truthy the toggler and move on.
  304. $prev_menu_was_separator = true;
  305. }
  306. }
  307. unset( $id, $data, $prev_menu_was_separator );
  308. // Remove the last menu item if it is a separator.
  309. $last_menu_key = array_keys( $menu );
  310. $last_menu_key = array_pop( $last_menu_key );
  311. if ( ! empty( $menu ) && 'wp-menu-separator' === $menu[ $last_menu_key ][4] ) {
  312. unset( $menu[ $last_menu_key ] );
  313. }
  314. unset( $last_menu_key );
  315. if ( ! user_can_access_admin_page() ) {
  316. /**
  317. * Fires when access to an admin page is denied.
  318. *
  319. * @since 2.5.0
  320. */
  321. do_action( 'admin_page_access_denied' );
  322. wp_die( __( 'Sorry, you are not allowed to access this page.' ), 403 );
  323. }
  324. $menu = add_menu_classes( $menu );