您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

346 行
8.5 KiB

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