menu.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. * @param string $add
  171. * @param string $class
  172. * @return string
  173. */
  174. function add_cssclass( $add, $class ) {
  175. $class = empty( $class ) ? $add : $class .= ' ' . $add;
  176. return $class;
  177. }
  178. /**
  179. * @param array $menu
  180. * @return array
  181. */
  182. function add_menu_classes( $menu ) {
  183. $first = false;
  184. $lastorder = false;
  185. $i = 0;
  186. $mc = count( $menu );
  187. foreach ( $menu as $order => $top ) {
  188. $i++;
  189. if ( 0 == $order ) { // dashboard is always shown/single
  190. $menu[0][4] = add_cssclass( 'menu-top-first', $top[4] );
  191. $lastorder = 0;
  192. continue;
  193. }
  194. if ( 0 === strpos( $top[2], 'separator' ) && false !== $lastorder ) { // if separator
  195. $first = true;
  196. $c = $menu[ $lastorder ][4];
  197. $menu[ $lastorder ][4] = add_cssclass( 'menu-top-last', $c );
  198. continue;
  199. }
  200. if ( $first ) {
  201. $c = $menu[ $order ][4];
  202. $menu[ $order ][4] = add_cssclass( 'menu-top-first', $c );
  203. $first = false;
  204. }
  205. if ( $mc == $i ) { // last item
  206. $c = $menu[ $order ][4];
  207. $menu[ $order ][4] = add_cssclass( 'menu-top-last', $c );
  208. }
  209. $lastorder = $order;
  210. }
  211. /**
  212. * Filters administration menus array with classes added for top-level items.
  213. *
  214. * @since 2.7.0
  215. *
  216. * @param array $menu Associative array of administration menu items.
  217. */
  218. return apply_filters( 'add_menu_classes', $menu );
  219. }
  220. uksort( $menu, 'strnatcasecmp' ); // make it all pretty
  221. /**
  222. * Filters whether to enable custom ordering of the administration menu.
  223. *
  224. * See the {@see 'menu_order'} filter for reordering menu items.
  225. *
  226. * @since 2.8.0
  227. *
  228. * @param bool $custom Whether custom ordering is enabled. Default false.
  229. */
  230. if ( apply_filters( 'custom_menu_order', false ) ) {
  231. $menu_order = array();
  232. foreach ( $menu as $menu_item ) {
  233. $menu_order[] = $menu_item[2];
  234. }
  235. unset( $menu_item );
  236. $default_menu_order = $menu_order;
  237. /**
  238. * Filters the order of administration menu items.
  239. *
  240. * A truthy value must first be passed to the {@see 'custom_menu_order'} filter
  241. * for this filter to work. Use the following to enable custom menu ordering:
  242. *
  243. * add_filter( 'custom_menu_order', '__return_true' );
  244. *
  245. * @since 2.8.0
  246. *
  247. * @param array $menu_order An ordered array of menu items.
  248. */
  249. $menu_order = apply_filters( 'menu_order', $menu_order );
  250. $menu_order = array_flip( $menu_order );
  251. $default_menu_order = array_flip( $default_menu_order );
  252. /**
  253. * @global array $menu_order
  254. * @global array $default_menu_order
  255. *
  256. * @param array $a
  257. * @param array $b
  258. * @return int
  259. */
  260. function sort_menu( $a, $b ) {
  261. global $menu_order, $default_menu_order;
  262. $a = $a[2];
  263. $b = $b[2];
  264. if ( isset( $menu_order[ $a ] ) && ! isset( $menu_order[ $b ] ) ) {
  265. return -1;
  266. } elseif ( ! isset( $menu_order[ $a ] ) && isset( $menu_order[ $b ] ) ) {
  267. return 1;
  268. } elseif ( isset( $menu_order[ $a ] ) && isset( $menu_order[ $b ] ) ) {
  269. if ( $menu_order[ $a ] == $menu_order[ $b ] ) {
  270. return 0;
  271. }
  272. return ( $menu_order[ $a ] < $menu_order[ $b ] ) ? -1 : 1;
  273. } else {
  274. return ( $default_menu_order[ $a ] <= $default_menu_order[ $b ] ) ? -1 : 1;
  275. }
  276. }
  277. usort( $menu, 'sort_menu' );
  278. unset( $menu_order, $default_menu_order );
  279. }
  280. // Prevent adjacent separators
  281. $prev_menu_was_separator = false;
  282. foreach ( $menu as $id => $data ) {
  283. if ( false === stristr( $data[4], 'wp-menu-separator' ) ) {
  284. // This item is not a separator, so falsey the toggler and do nothing
  285. $prev_menu_was_separator = false;
  286. } else {
  287. // The previous item was a separator, so unset this one
  288. if ( true === $prev_menu_was_separator ) {
  289. unset( $menu[ $id ] );
  290. }
  291. // This item is a separator, so truthy the toggler and move on
  292. $prev_menu_was_separator = true;
  293. }
  294. }
  295. unset( $id, $data, $prev_menu_was_separator );
  296. // Remove the last menu item if it is a separator.
  297. $last_menu_key = array_keys( $menu );
  298. $last_menu_key = array_pop( $last_menu_key );
  299. if ( ! empty( $menu ) && 'wp-menu-separator' == $menu[ $last_menu_key ][4] ) {
  300. unset( $menu[ $last_menu_key ] );
  301. }
  302. unset( $last_menu_key );
  303. if ( ! user_can_access_admin_page() ) {
  304. /**
  305. * Fires when access to an admin page is denied.
  306. *
  307. * @since 2.5.0
  308. */
  309. do_action( 'admin_page_access_denied' );
  310. wp_die( __( 'Sorry, you are not allowed to access this page.' ), 403 );
  311. }
  312. $menu = add_menu_classes( $menu );