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.
 
 
 
 
 

854 lines
40 KiB

  1. <?php
  2. /**
  3. * WordPress Administration for Navigation Menus
  4. * Interface functions
  5. *
  6. * @version 2.0.0
  7. *
  8. * @package WordPress
  9. * @subpackage Administration
  10. */
  11. /** Load WordPress Administration Bootstrap */
  12. require_once( dirname( __FILE__ ) . '/admin.php' );
  13. // Load all the nav menu interface functions
  14. require_once( ABSPATH . 'wp-admin/includes/nav-menu.php' );
  15. if ( ! current_theme_supports( 'menus' ) && ! current_theme_supports( 'widgets' ) )
  16. wp_die( __( 'Your theme does not support navigation menus or widgets.' ) );
  17. // Permissions Check
  18. if ( ! current_user_can( 'edit_theme_options' ) ) {
  19. wp_die(
  20. '<h1>' . __( 'Cheatin&#8217; uh?' ) . '</h1>' .
  21. '<p>' . __( 'Sorry, you are not allowed to edit theme options on this site.' ) . '</p>',
  22. 403
  23. );
  24. }
  25. wp_enqueue_script( 'nav-menu' );
  26. if ( wp_is_mobile() )
  27. wp_enqueue_script( 'jquery-touch-punch' );
  28. // Container for any messages displayed to the user
  29. $messages = array();
  30. // Container that stores the name of the active menu
  31. $nav_menu_selected_title = '';
  32. // The menu id of the current menu being edited
  33. $nav_menu_selected_id = isset( $_REQUEST['menu'] ) ? (int) $_REQUEST['menu'] : 0;
  34. // Get existing menu locations assignments
  35. $locations = get_registered_nav_menus();
  36. $menu_locations = get_nav_menu_locations();
  37. $num_locations = count( array_keys( $locations ) );
  38. // Allowed actions: add, update, delete
  39. $action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : 'edit';
  40. /*
  41. * If a JSON blob of navigation menu data is found, expand it and inject it
  42. * into `$_POST` to avoid PHP `max_input_vars` limitations. See #14134.
  43. */
  44. _wp_expand_nav_menu_post_data();
  45. switch ( $action ) {
  46. case 'add-menu-item':
  47. check_admin_referer( 'add-menu_item', 'menu-settings-column-nonce' );
  48. if ( isset( $_REQUEST['nav-menu-locations'] ) )
  49. set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_REQUEST['menu-locations'] ) );
  50. elseif ( isset( $_REQUEST['menu-item'] ) )
  51. wp_save_nav_menu_items( $nav_menu_selected_id, $_REQUEST['menu-item'] );
  52. break;
  53. case 'move-down-menu-item' :
  54. // Moving down a menu item is the same as moving up the next in order.
  55. check_admin_referer( 'move-menu_item' );
  56. $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0;
  57. if ( is_nav_menu_item( $menu_item_id ) ) {
  58. $menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );
  59. if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
  60. $menu_id = (int) $menus[0];
  61. $ordered_menu_items = wp_get_nav_menu_items( $menu_id );
  62. $menu_item_data = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) );
  63. // Set up the data we need in one pass through the array of menu items.
  64. $dbids_to_orders = array();
  65. $orders_to_dbids = array();
  66. foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) {
  67. if ( isset( $ordered_menu_item_object->ID ) ) {
  68. if ( isset( $ordered_menu_item_object->menu_order ) ) {
  69. $dbids_to_orders[$ordered_menu_item_object->ID] = $ordered_menu_item_object->menu_order;
  70. $orders_to_dbids[$ordered_menu_item_object->menu_order] = $ordered_menu_item_object->ID;
  71. }
  72. }
  73. }
  74. // Get next in order.
  75. if (
  76. isset( $orders_to_dbids[$dbids_to_orders[$menu_item_id] + 1] )
  77. ) {
  78. $next_item_id = $orders_to_dbids[$dbids_to_orders[$menu_item_id] + 1];
  79. $next_item_data = (array) wp_setup_nav_menu_item( get_post( $next_item_id ) );
  80. // If not siblings of same parent, bubble menu item up but keep order.
  81. if (
  82. ! empty( $menu_item_data['menu_item_parent'] ) &&
  83. (
  84. empty( $next_item_data['menu_item_parent'] ) ||
  85. $next_item_data['menu_item_parent'] != $menu_item_data['menu_item_parent']
  86. )
  87. ) {
  88. $parent_db_id = in_array( $menu_item_data['menu_item_parent'], $orders_to_dbids ) ? (int) $menu_item_data['menu_item_parent'] : 0;
  89. $parent_object = wp_setup_nav_menu_item( get_post( $parent_db_id ) );
  90. if ( ! is_wp_error( $parent_object ) ) {
  91. $parent_data = (array) $parent_object;
  92. $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
  93. update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
  94. }
  95. // Make menu item a child of its next sibling.
  96. } else {
  97. $next_item_data['menu_order'] = $next_item_data['menu_order'] - 1;
  98. $menu_item_data['menu_order'] = $menu_item_data['menu_order'] + 1;
  99. $menu_item_data['menu_item_parent'] = $next_item_data['ID'];
  100. update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
  101. wp_update_post($menu_item_data);
  102. wp_update_post($next_item_data);
  103. }
  104. // The item is last but still has a parent, so bubble up.
  105. } elseif (
  106. ! empty( $menu_item_data['menu_item_parent'] ) &&
  107. in_array( $menu_item_data['menu_item_parent'], $orders_to_dbids )
  108. ) {
  109. $menu_item_data['menu_item_parent'] = (int) get_post_meta( $menu_item_data['menu_item_parent'], '_menu_item_menu_item_parent', true);
  110. update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
  111. }
  112. }
  113. }
  114. break;
  115. case 'move-up-menu-item' :
  116. check_admin_referer( 'move-menu_item' );
  117. $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0;
  118. if ( is_nav_menu_item( $menu_item_id ) ) {
  119. $menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );
  120. if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
  121. $menu_id = (int) $menus[0];
  122. $ordered_menu_items = wp_get_nav_menu_items( $menu_id );
  123. $menu_item_data = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) );
  124. // Set up the data we need in one pass through the array of menu items.
  125. $dbids_to_orders = array();
  126. $orders_to_dbids = array();
  127. foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) {
  128. if ( isset( $ordered_menu_item_object->ID ) ) {
  129. if ( isset( $ordered_menu_item_object->menu_order ) ) {
  130. $dbids_to_orders[$ordered_menu_item_object->ID] = $ordered_menu_item_object->menu_order;
  131. $orders_to_dbids[$ordered_menu_item_object->menu_order] = $ordered_menu_item_object->ID;
  132. }
  133. }
  134. }
  135. // If this menu item is not first.
  136. if ( ! empty( $dbids_to_orders[$menu_item_id] ) && ! empty( $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] ) ) {
  137. // If this menu item is a child of the previous.
  138. if (
  139. ! empty( $menu_item_data['menu_item_parent'] ) &&
  140. in_array( $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ) ) &&
  141. isset( $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] ) &&
  142. ( $menu_item_data['menu_item_parent'] == $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] )
  143. ) {
  144. $parent_db_id = in_array( $menu_item_data['menu_item_parent'], $orders_to_dbids ) ? (int) $menu_item_data['menu_item_parent'] : 0;
  145. $parent_object = wp_setup_nav_menu_item( get_post( $parent_db_id ) );
  146. if ( ! is_wp_error( $parent_object ) ) {
  147. $parent_data = (array) $parent_object;
  148. /*
  149. * If there is something before the parent and parent a child of it,
  150. * make menu item a child also of it.
  151. */
  152. if (
  153. ! empty( $dbids_to_orders[$parent_db_id] ) &&
  154. ! empty( $orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1] ) &&
  155. ! empty( $parent_data['menu_item_parent'] )
  156. ) {
  157. $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
  158. /*
  159. * Else if there is something before parent and parent not a child of it,
  160. * make menu item a child of that something's parent
  161. */
  162. } elseif (
  163. ! empty( $dbids_to_orders[$parent_db_id] ) &&
  164. ! empty( $orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1] )
  165. ) {
  166. $_possible_parent_id = (int) get_post_meta( $orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1], '_menu_item_menu_item_parent', true);
  167. if ( in_array( $_possible_parent_id, array_keys( $dbids_to_orders ) ) )
  168. $menu_item_data['menu_item_parent'] = $_possible_parent_id;
  169. else
  170. $menu_item_data['menu_item_parent'] = 0;
  171. // Else there isn't something before the parent.
  172. } else {
  173. $menu_item_data['menu_item_parent'] = 0;
  174. }
  175. // Set former parent's [menu_order] to that of menu-item's.
  176. $parent_data['menu_order'] = $parent_data['menu_order'] + 1;
  177. // Set menu-item's [menu_order] to that of former parent.
  178. $menu_item_data['menu_order'] = $menu_item_data['menu_order'] - 1;
  179. // Save changes.
  180. update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
  181. wp_update_post($menu_item_data);
  182. wp_update_post($parent_data);
  183. }
  184. // Else this menu item is not a child of the previous.
  185. } elseif (
  186. empty( $menu_item_data['menu_order'] ) ||
  187. empty( $menu_item_data['menu_item_parent'] ) ||
  188. ! in_array( $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ) ) ||
  189. empty( $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] ) ||
  190. $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] != $menu_item_data['menu_item_parent']
  191. ) {
  192. // Just make it a child of the previous; keep the order.
  193. $menu_item_data['menu_item_parent'] = (int) $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1];
  194. update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
  195. wp_update_post($menu_item_data);
  196. }
  197. }
  198. }
  199. }
  200. break;
  201. case 'delete-menu-item':
  202. $menu_item_id = (int) $_REQUEST['menu-item'];
  203. check_admin_referer( 'delete-menu_item_' . $menu_item_id );
  204. if ( is_nav_menu_item( $menu_item_id ) && wp_delete_post( $menu_item_id, true ) )
  205. $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __('The menu item has been successfully deleted.') . '</p></div>';
  206. break;
  207. case 'delete':
  208. check_admin_referer( 'delete-nav_menu-' . $nav_menu_selected_id );
  209. if ( is_nav_menu( $nav_menu_selected_id ) ) {
  210. $deletion = wp_delete_nav_menu( $nav_menu_selected_id );
  211. } else {
  212. // Reset the selected menu.
  213. $nav_menu_selected_id = 0;
  214. unset( $_REQUEST['menu'] );
  215. }
  216. if ( ! isset( $deletion ) )
  217. break;
  218. if ( is_wp_error( $deletion ) )
  219. $messages[] = '<div id="message" class="error notice is-dismissible"><p>' . $deletion->get_error_message() . '</p></div>';
  220. else
  221. $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'The menu has been successfully deleted.' ) . '</p></div>';
  222. break;
  223. case 'delete_menus':
  224. check_admin_referer( 'nav_menus_bulk_actions' );
  225. foreach ( $_REQUEST['delete_menus'] as $menu_id_to_delete ) {
  226. if ( ! is_nav_menu( $menu_id_to_delete ) )
  227. continue;
  228. $deletion = wp_delete_nav_menu( $menu_id_to_delete );
  229. if ( is_wp_error( $deletion ) ) {
  230. $messages[] = '<div id="message" class="error notice is-dismissible"><p>' . $deletion->get_error_message() . '</p></div>';
  231. $deletion_error = true;
  232. }
  233. }
  234. if ( empty( $deletion_error ) )
  235. $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'Selected menus have been successfully deleted.' ) . '</p></div>';
  236. break;
  237. case 'update':
  238. check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
  239. // Remove menu locations that have been unchecked.
  240. foreach ( $locations as $location => $description ) {
  241. if ( ( empty( $_POST['menu-locations'] ) || empty( $_POST['menu-locations'][ $location ] ) ) && isset( $menu_locations[ $location ] ) && $menu_locations[ $location ] == $nav_menu_selected_id )
  242. unset( $menu_locations[ $location ] );
  243. }
  244. // Merge new and existing menu locations if any new ones are set.
  245. if ( isset( $_POST['menu-locations'] ) ) {
  246. $new_menu_locations = array_map( 'absint', $_POST['menu-locations'] );
  247. $menu_locations = array_merge( $menu_locations, $new_menu_locations );
  248. }
  249. // Set menu locations.
  250. set_theme_mod( 'nav_menu_locations', $menu_locations );
  251. // Add Menu.
  252. if ( 0 == $nav_menu_selected_id ) {
  253. $new_menu_title = trim( esc_html( $_POST['menu-name'] ) );
  254. if ( $new_menu_title ) {
  255. $_nav_menu_selected_id = wp_update_nav_menu_object( 0, array('menu-name' => $new_menu_title) );
  256. if ( is_wp_error( $_nav_menu_selected_id ) ) {
  257. $messages[] = '<div id="message" class="error notice is-dismissible"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
  258. } else {
  259. $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
  260. $nav_menu_selected_id = $_nav_menu_selected_id;
  261. $nav_menu_selected_title = $_menu_object->name;
  262. if ( isset( $_REQUEST['menu-item'] ) )
  263. wp_save_nav_menu_items( $nav_menu_selected_id, absint( $_REQUEST['menu-item'] ) );
  264. if ( isset( $_REQUEST['zero-menu-state'] ) ) {
  265. // If there are menu items, add them
  266. wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selected_title );
  267. // Auto-save nav_menu_locations
  268. $locations = get_nav_menu_locations();
  269. foreach ( $locations as $location => $menu_id ) {
  270. $locations[ $location ] = $nav_menu_selected_id;
  271. break; // There should only be 1
  272. }
  273. set_theme_mod( 'nav_menu_locations', $locations );
  274. }
  275. if ( isset( $_REQUEST['use-location'] ) ) {
  276. $locations = get_registered_nav_menus();
  277. $menu_locations = get_nav_menu_locations();
  278. if ( isset( $locations[ $_REQUEST['use-location'] ] ) )
  279. $menu_locations[ $_REQUEST['use-location'] ] = $nav_menu_selected_id;
  280. set_theme_mod( 'nav_menu_locations', $menu_locations );
  281. }
  282. // $messages[] = '<div id="message" class="updated"><p>' . sprintf( __( '<strong>%s</strong> has been created.' ), $nav_menu_selected_title ) . '</p></div>';
  283. wp_redirect( admin_url( 'nav-menus.php?menu=' . $_nav_menu_selected_id ) );
  284. exit();
  285. }
  286. } else {
  287. $messages[] = '<div id="message" class="error notice is-dismissible"><p>' . __( 'Please enter a valid menu name.' ) . '</p></div>';
  288. }
  289. // Update existing menu.
  290. } else {
  291. $_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
  292. $menu_title = trim( esc_html( $_POST['menu-name'] ) );
  293. if ( ! $menu_title ) {
  294. $messages[] = '<div id="message" class="error notice is-dismissible"><p>' . __( 'Please enter a valid menu name.' ) . '</p></div>';
  295. $menu_title = $_menu_object->name;
  296. }
  297. if ( ! is_wp_error( $_menu_object ) ) {
  298. $_nav_menu_selected_id = wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $menu_title ) );
  299. if ( is_wp_error( $_nav_menu_selected_id ) ) {
  300. $_menu_object = $_nav_menu_selected_id;
  301. $messages[] = '<div id="message" class="error notice is-dismissible"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
  302. } else {
  303. $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
  304. $nav_menu_selected_title = $_menu_object->name;
  305. }
  306. }
  307. // Update menu items.
  308. if ( ! is_wp_error( $_menu_object ) ) {
  309. $messages = array_merge( $messages, wp_nav_menu_update_menu_items( $_nav_menu_selected_id, $nav_menu_selected_title ) );
  310. // If the menu ID changed, redirect to the new URL.
  311. if ( $nav_menu_selected_id != $_nav_menu_selected_id ) {
  312. wp_redirect( admin_url( 'nav-menus.php?menu=' . intval( $_nav_menu_selected_id ) ) );
  313. exit();
  314. }
  315. }
  316. }
  317. break;
  318. case 'locations':
  319. if ( ! $num_locations ) {
  320. wp_redirect( admin_url( 'nav-menus.php' ) );
  321. exit();
  322. }
  323. add_filter( 'screen_options_show_screen', '__return_false' );
  324. if ( isset( $_POST['menu-locations'] ) ) {
  325. check_admin_referer( 'save-menu-locations' );
  326. $new_menu_locations = array_map( 'absint', $_POST['menu-locations'] );
  327. $menu_locations = array_merge( $menu_locations, $new_menu_locations );
  328. // Set menu locations
  329. set_theme_mod( 'nav_menu_locations', $menu_locations );
  330. $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'Menu locations updated.' ) . '</p></div>';
  331. }
  332. break;
  333. }
  334. // Get all nav menus.
  335. $nav_menus = wp_get_nav_menus();
  336. $menu_count = count( $nav_menus );
  337. // Are we on the add new screen?
  338. $add_new_screen = ( isset( $_GET['menu'] ) && 0 == $_GET['menu'] ) ? true : false;
  339. $locations_screen = ( isset( $_GET['action'] ) && 'locations' == $_GET['action'] ) ? true : false;
  340. /*
  341. * If we have one theme location, and zero menus, we take them right
  342. * into editing their first menu.
  343. */
  344. $page_count = wp_count_posts( 'page' );
  345. $one_theme_location_no_menus = ( 1 == count( get_registered_nav_menus() ) && ! $add_new_screen && empty( $nav_menus ) && ! empty( $page_count->publish ) ) ? true : false;
  346. $nav_menus_l10n = array(
  347. 'oneThemeLocationNoMenus' => $one_theme_location_no_menus,
  348. 'moveUp' => __( 'Move up one' ),
  349. 'moveDown' => __( 'Move down one' ),
  350. 'moveToTop' => __( 'Move to the top' ),
  351. /* translators: %s: previous item name */
  352. 'moveUnder' => __( 'Move under %s' ),
  353. /* translators: %s: previous item name */
  354. 'moveOutFrom' => __( 'Move out from under %s' ),
  355. /* translators: %s: previous item name */
  356. 'under' => __( 'Under %s' ),
  357. /* translators: %s: previous item name */
  358. 'outFrom' => __( 'Out from under %s' ),
  359. /* translators: 1: item name, 2: item position, 3: total number of items */
  360. 'menuFocus' => __( '%1$s. Menu item %2$d of %3$d.' ),
  361. /* translators: 1: item name, 2: item position, 3: parent item name */
  362. 'subMenuFocus' => __( '%1$s. Sub item number %2$d under %3$s.' ),
  363. );
  364. wp_localize_script( 'nav-menu', 'menus', $nav_menus_l10n );
  365. /*
  366. * Redirect to add screen if there are no menus and this users has either zero,
  367. * or more than 1 theme locations.
  368. */
  369. if ( 0 == $menu_count && ! $add_new_screen && ! $one_theme_location_no_menus )
  370. wp_redirect( admin_url( 'nav-menus.php?action=edit&menu=0' ) );
  371. // Get recently edited nav menu.
  372. $recently_edited = absint( get_user_option( 'nav_menu_recently_edited' ) );
  373. if ( empty( $recently_edited ) && is_nav_menu( $nav_menu_selected_id ) )
  374. $recently_edited = $nav_menu_selected_id;
  375. // Use $recently_edited if none are selected.
  376. if ( empty( $nav_menu_selected_id ) && ! isset( $_GET['menu'] ) && is_nav_menu( $recently_edited ) )
  377. $nav_menu_selected_id = $recently_edited;
  378. // On deletion of menu, if another menu exists, show it.
  379. if ( ! $add_new_screen && 0 < $menu_count && isset( $_GET['action'] ) && 'delete' == $_GET['action'] )
  380. $nav_menu_selected_id = $nav_menus[0]->term_id;
  381. // Set $nav_menu_selected_id to 0 if no menus.
  382. if ( $one_theme_location_no_menus ) {
  383. $nav_menu_selected_id = 0;
  384. } elseif ( empty( $nav_menu_selected_id ) && ! empty( $nav_menus ) && ! $add_new_screen ) {
  385. // if we have no selection yet, and we have menus, set to the first one in the list.
  386. $nav_menu_selected_id = $nav_menus[0]->term_id;
  387. }
  388. // Update the user's setting.
  389. if ( $nav_menu_selected_id != $recently_edited && is_nav_menu( $nav_menu_selected_id ) )
  390. update_user_meta( $current_user->ID, 'nav_menu_recently_edited', $nav_menu_selected_id );
  391. // If there's a menu, get its name.
  392. if ( ! $nav_menu_selected_title && is_nav_menu( $nav_menu_selected_id ) ) {
  393. $_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
  394. $nav_menu_selected_title = ! is_wp_error( $_menu_object ) ? $_menu_object->name : '';
  395. }
  396. // Generate truncated menu names.
  397. foreach ( (array) $nav_menus as $key => $_nav_menu ) {
  398. $nav_menus[$key]->truncated_name = wp_html_excerpt( $_nav_menu->name, 40, '&hellip;' );
  399. }
  400. // Retrieve menu locations.
  401. if ( current_theme_supports( 'menus' ) ) {
  402. $locations = get_registered_nav_menus();
  403. $menu_locations = get_nav_menu_locations();
  404. }
  405. /*
  406. * Ensure the user will be able to scroll horizontally
  407. * by adding a class for the max menu depth.
  408. *
  409. * @global int $_wp_nav_menu_max_depth
  410. */
  411. global $_wp_nav_menu_max_depth;
  412. $_wp_nav_menu_max_depth = 0;
  413. // Calling wp_get_nav_menu_to_edit generates $_wp_nav_menu_max_depth.
  414. if ( is_nav_menu( $nav_menu_selected_id ) ) {
  415. $menu_items = wp_get_nav_menu_items( $nav_menu_selected_id, array( 'post_status' => 'any' ) );
  416. $edit_markup = wp_get_nav_menu_to_edit( $nav_menu_selected_id );
  417. }
  418. /**
  419. *
  420. * @global int $_wp_nav_menu_max_depth
  421. *
  422. * @param string $classes
  423. * @return string
  424. */
  425. function wp_nav_menu_max_depth( $classes ) {
  426. global $_wp_nav_menu_max_depth;
  427. return "$classes menu-max-depth-$_wp_nav_menu_max_depth";
  428. }
  429. add_filter('admin_body_class', 'wp_nav_menu_max_depth');
  430. wp_nav_menu_setup();
  431. wp_initial_nav_menu_meta_boxes();
  432. if ( ! current_theme_supports( 'menus' ) && ! $num_locations )
  433. $messages[] = '<div id="message" class="updated"><p>' . sprintf( __( 'Your theme does not natively support menus, but you can use them in sidebars by adding a &#8220;Custom Menu&#8221; widget on the <a href="%s">Widgets</a> screen.' ), admin_url( 'widgets.php' ) ) . '</p></div>';
  434. if ( ! $locations_screen ) : // Main tab
  435. $overview = '<p>' . __( 'This screen is used for managing your custom navigation menus.' ) . '</p>';
  436. /* translators: 1: Widgets admin screen URL, 2 and 3: The name of the default themes */
  437. $overview .= '<p>' . sprintf( __( 'Menus can be displayed in locations defined by your theme, even used in sidebars by adding a &#8220;Custom Menu&#8221; widget on the <a href="%1$s">Widgets</a> screen. If your theme does not support the custom menus feature (the default themes, %2$s and %3$s, do), you can learn about adding this support by following the Documentation link to the side.' ), admin_url( 'widgets.php' ), 'Twenty Sixteen', 'Twenty Seventeen' ) . '</p>';
  438. $overview .= '<p>' . __( 'From this screen you can:' ) . '</p>';
  439. $overview .= '<ul><li>' . __( 'Create, edit, and delete menus' ) . '</li>';
  440. $overview .= '<li>' . __( 'Add, organize, and modify individual menu items' ) . '</li></ul>';
  441. get_current_screen()->add_help_tab( array(
  442. 'id' => 'overview',
  443. 'title' => __( 'Overview' ),
  444. 'content' => $overview
  445. ) );
  446. $menu_management = '<p>' . __( 'The menu management box at the top of the screen is used to control which menu is opened in the editor below.' ) . '</p>';
  447. $menu_management .= '<ul><li>' . __( 'To edit an existing menu, <strong>choose a menu from the drop down and click Select</strong>' ) . '</li>';
  448. $menu_management .= '<li>' . __( 'If you haven&#8217;t yet created any menus, <strong>click the &#8217;create a new menu&#8217; link</strong> to get started' ) . '</li></ul>';
  449. $menu_management .= '<p>' . __( 'You can assign theme locations to individual menus by <strong>selecting the desired settings</strong> at the bottom of the menu editor. To assign menus to all theme locations at once, <strong>visit the Manage Locations tab</strong> at the top of the screen.' ) . '</p>';
  450. get_current_screen()->add_help_tab( array(
  451. 'id' => 'menu-management',
  452. 'title' => __( 'Menu Management' ),
  453. 'content' => $menu_management
  454. ) );
  455. $editing_menus = '<p>' . __( 'Each custom menu may contain a mix of links to pages, categories, custom URLs or other content types. Menu links are added by selecting items from the expanding boxes in the left-hand column below.' ) . '</p>';
  456. $editing_menus .= '<p>' . __( '<strong>Clicking the arrow to the right of any menu item</strong> in the editor will reveal a standard group of settings. Additional settings such as link target, CSS classes, link relationships, and link descriptions can be enabled and disabled via the Screen Options tab.' ) . '</p>';
  457. $editing_menus .= '<ul><li>' . __( 'Add one or several items at once by <strong>selecting the checkbox next to each item and clicking Add to Menu</strong>' ) . '</li>';
  458. $editing_menus .= '<li>' . __( 'To add a custom link, <strong>expand the Custom Links section, enter a URL and link text, and click Add to Menu</strong>' ) .'</li>';
  459. $editing_menus .= '<li>' . __( 'To reorganize menu items, <strong>drag and drop items with your mouse or use your keyboard</strong>. Drag or move a menu item a little to the right to make it a submenu' ) . '</li>';
  460. $editing_menus .= '<li>' . __( 'Delete a menu item by <strong>expanding it and clicking the Remove link</strong>' ) . '</li></ul>';
  461. get_current_screen()->add_help_tab( array(
  462. 'id' => 'editing-menus',
  463. 'title' => __( 'Editing Menus' ),
  464. 'content' => $editing_menus
  465. ) );
  466. else : // Locations Tab.
  467. $locations_overview = '<p>' . __( 'This screen is used for globally assigning menus to locations defined by your theme.' ) . '</p>';
  468. $locations_overview .= '<ul><li>' . __( 'To assign menus to one or more theme locations, <strong>select a menu from each location&#8217;s drop down.</strong> When you&#8217;re finished, <strong>click Save Changes</strong>' ) . '</li>';
  469. $locations_overview .= '<li>' . __( 'To edit a menu currently assigned to a theme location, <strong>click the adjacent &#8217;Edit&#8217; link</strong>' ) . '</li>';
  470. $locations_overview .= '<li>' . __( 'To add a new menu instead of assigning an existing one, <strong>click the &#8217;Use new menu&#8217; link</strong>. Your new menu will be automatically assigned to that theme location' ) . '</li></ul>';
  471. get_current_screen()->add_help_tab( array(
  472. 'id' => 'locations-overview',
  473. 'title' => __( 'Overview' ),
  474. 'content' => $locations_overview
  475. ) );
  476. endif;
  477. get_current_screen()->set_help_sidebar(
  478. '<p><strong>' . __('For more information:') . '</strong></p>' .
  479. '<p>' . __('<a href="https://codex.wordpress.org/Appearance_Menus_Screen">Documentation on Menus</a>') . '</p>' .
  480. '<p>' . __('<a href="https://wordpress.org/support/">Support Forums</a>') . '</p>'
  481. );
  482. // Get the admin header.
  483. require_once( ABSPATH . 'wp-admin/admin-header.php' );
  484. ?>
  485. <div class="wrap">
  486. <h1><?php echo esc_html( __( 'Menus' ) ); ?>
  487. <?php
  488. if ( current_user_can( 'customize' ) ) :
  489. $focus = $locations_screen ? array( 'section' => 'menu_locations' ) : array( 'panel' => 'nav_menus' );
  490. printf(
  491. ' <a class="page-title-action hide-if-no-customize" href="%1$s">%2$s</a>',
  492. esc_url( add_query_arg( array(
  493. array( 'autofocus' => $focus ),
  494. 'return' => urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ),
  495. ), admin_url( 'customize.php' ) ) ),
  496. __( 'Manage with Live Preview' )
  497. );
  498. endif;
  499. ?>
  500. </h1>
  501. <h2 class="nav-tab-wrapper wp-clearfix">
  502. <a href="<?php echo admin_url( 'nav-menus.php' ); ?>" class="nav-tab<?php if ( ! isset( $_GET['action'] ) || isset( $_GET['action'] ) && 'locations' != $_GET['action'] ) echo ' nav-tab-active'; ?>"><?php esc_html_e( 'Edit Menus' ); ?></a>
  503. <?php if ( $num_locations && $menu_count ) : ?>
  504. <a href="<?php echo esc_url( add_query_arg( array( 'action' => 'locations' ), admin_url( 'nav-menus.php' ) ) ); ?>" class="nav-tab<?php if ( $locations_screen ) echo ' nav-tab-active'; ?>"><?php esc_html_e( 'Manage Locations' ); ?></a>
  505. <?php
  506. endif;
  507. ?>
  508. </h2>
  509. <?php
  510. foreach ( $messages as $message ) :
  511. echo $message . "\n";
  512. endforeach;
  513. ?>
  514. <?php
  515. if ( $locations_screen ) :
  516. if ( 1 == $num_locations ) {
  517. echo '<p>' . __( 'Your theme supports one menu. Select which menu you would like to use.' ) . '</p>';
  518. } else {
  519. echo '<p>' . sprintf( _n( 'Your theme supports %s menu. Select which menu appears in each location.', 'Your theme supports %s menus. Select which menu appears in each location.', $num_locations ), number_format_i18n( $num_locations ) ) . '</p>';
  520. }
  521. ?>
  522. <div id="menu-locations-wrap">
  523. <form method="post" action="<?php echo esc_url( add_query_arg( array( 'action' => 'locations' ), admin_url( 'nav-menus.php' ) ) ); ?>">
  524. <table class="widefat fixed" id="menu-locations-table">
  525. <thead>
  526. <tr>
  527. <th scope="col" class="manage-column column-locations"><?php _e( 'Theme Location' ); ?></th>
  528. <th scope="col" class="manage-column column-menus"><?php _e( 'Assigned Menu' ); ?></th>
  529. </tr>
  530. </thead>
  531. <tbody class="menu-locations">
  532. <?php foreach ( $locations as $_location => $_name ) { ?>
  533. <tr class="menu-locations-row">
  534. <td class="menu-location-title"><label for="locations-<?php echo $_location; ?>"><?php echo $_name; ?></label></td>
  535. <td class="menu-location-menus">
  536. <select name="menu-locations[<?php echo $_location; ?>]" id="locations-<?php echo $_location; ?>">
  537. <option value="0"><?php printf( '&mdash; %s &mdash;', esc_html__( 'Select a Menu' ) ); ?></option>
  538. <?php foreach ( $nav_menus as $menu ) : ?>
  539. <?php $selected = isset( $menu_locations[$_location] ) && $menu_locations[$_location] == $menu->term_id; ?>
  540. <option <?php if ( $selected ) echo 'data-orig="true"'; ?> <?php selected( $selected ); ?> value="<?php echo $menu->term_id; ?>">
  541. <?php echo wp_html_excerpt( $menu->name, 40, '&hellip;' ); ?>
  542. </option>
  543. <?php endforeach; ?>
  544. </select>
  545. <div class="locations-row-links">
  546. <?php if ( isset( $menu_locations[ $_location ] ) && 0 != $menu_locations[ $_location ] ) : ?>
  547. <span class="locations-edit-menu-link">
  548. <a href="<?php echo esc_url( add_query_arg( array( 'action' => 'edit', 'menu' => $menu_locations[$_location] ), admin_url( 'nav-menus.php' ) ) ); ?>">
  549. <span aria-hidden="true"><?php _ex( 'Edit', 'menu' ); ?></span><span class="screen-reader-text"><?php _e( 'Edit selected menu' ); ?></span>
  550. </a>
  551. </span>
  552. <?php endif; ?>
  553. <span class="locations-add-menu-link">
  554. <a href="<?php echo esc_url( add_query_arg( array( 'action' => 'edit', 'menu' => 0, 'use-location' => $_location ), admin_url( 'nav-menus.php' ) ) ); ?>">
  555. <?php _ex( 'Use new menu', 'menu' ); ?>
  556. </a>
  557. </span>
  558. </div><!-- .locations-row-links -->
  559. </td><!-- .menu-location-menus -->
  560. </tr><!-- .menu-locations-row -->
  561. <?php } // foreach ?>
  562. </tbody>
  563. </table>
  564. <p class="button-controls wp-clearfix"><?php submit_button( __( 'Save Changes' ), 'primary left', 'nav-menu-locations', false ); ?></p>
  565. <?php wp_nonce_field( 'save-menu-locations' ); ?>
  566. <input type="hidden" name="menu" id="nav-menu-meta-object-id" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
  567. </form>
  568. </div><!-- #menu-locations-wrap -->
  569. <?php
  570. /**
  571. * Fires after the menu locations table is displayed.
  572. *
  573. * @since 3.6.0
  574. */
  575. do_action( 'after_menu_locations_table' ); ?>
  576. <?php else : ?>
  577. <div class="manage-menus">
  578. <?php if ( $menu_count < 2 ) : ?>
  579. <span class="add-edit-menu-action">
  580. <?php printf( __( 'Edit your menu below, or <a href="%s">create a new menu</a>.' ), esc_url( add_query_arg( array( 'action' => 'edit', 'menu' => 0 ), admin_url( 'nav-menus.php' ) ) ) ); ?>
  581. </span><!-- /add-edit-menu-action -->
  582. <?php else : ?>
  583. <form method="get" action="<?php echo admin_url( 'nav-menus.php' ); ?>">
  584. <input type="hidden" name="action" value="edit" />
  585. <label for="select-menu-to-edit" class="selected-menu"><?php _e( 'Select a menu to edit:' ); ?></label>
  586. <select name="menu" id="select-menu-to-edit">
  587. <?php if ( $add_new_screen ) : ?>
  588. <option value="0" selected="selected"><?php _e( '&mdash; Select &mdash;' ); ?></option>
  589. <?php endif; ?>
  590. <?php foreach ( (array) $nav_menus as $_nav_menu ) : ?>
  591. <option value="<?php echo esc_attr( $_nav_menu->term_id ); ?>" <?php selected( $_nav_menu->term_id, $nav_menu_selected_id ); ?>>
  592. <?php
  593. echo esc_html( $_nav_menu->truncated_name ) ;
  594. if ( ! empty( $menu_locations ) && in_array( $_nav_menu->term_id, $menu_locations ) ) {
  595. $locations_assigned_to_this_menu = array();
  596. foreach ( array_keys( $menu_locations, $_nav_menu->term_id ) as $menu_location_key ) {
  597. if ( isset( $locations[ $menu_location_key ] ) ) {
  598. $locations_assigned_to_this_menu[] = $locations[ $menu_location_key ];
  599. }
  600. }
  601. /**
  602. * Filters the number of locations listed per menu in the drop-down select.
  603. *
  604. * @since 3.6.0
  605. *
  606. * @param int $locations Number of menu locations to list. Default 3.
  607. */
  608. $assigned_locations = array_slice( $locations_assigned_to_this_menu, 0, absint( apply_filters( 'wp_nav_locations_listed_per_menu', 3 ) ) );
  609. // Adds ellipses following the number of locations defined in $assigned_locations.
  610. if ( ! empty( $assigned_locations ) ) {
  611. printf( ' (%1$s%2$s)',
  612. implode( ', ', $assigned_locations ),
  613. count( $locations_assigned_to_this_menu ) > count( $assigned_locations ) ? ' &hellip;' : ''
  614. );
  615. }
  616. }
  617. ?>
  618. </option>
  619. <?php endforeach; ?>
  620. </select>
  621. <span class="submit-btn"><input type="submit" class="button" value="<?php esc_attr_e( 'Select' ); ?>"></span>
  622. <span class="add-new-menu-action">
  623. <?php printf( __( 'or <a href="%s">create a new menu</a>.' ), esc_url( add_query_arg( array( 'action' => 'edit', 'menu' => 0 ), admin_url( 'nav-menus.php' ) ) ) ); ?>
  624. </span><!-- /add-new-menu-action -->
  625. </form>
  626. <?php endif; ?>
  627. </div><!-- /manage-menus -->
  628. <div id="nav-menus-frame" class="wp-clearfix">
  629. <div id="menu-settings-column" class="metabox-holder<?php if ( isset( $_GET['menu'] ) && '0' == $_GET['menu'] ) { echo ' metabox-holder-disabled'; } ?>">
  630. <div class="clear"></div>
  631. <form id="nav-menu-meta" class="nav-menu-meta" method="post" enctype="multipart/form-data">
  632. <input type="hidden" name="menu" id="nav-menu-meta-object-id" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
  633. <input type="hidden" name="action" value="add-menu-item" />
  634. <?php wp_nonce_field( 'add-menu_item', 'menu-settings-column-nonce' ); ?>
  635. <?php do_accordion_sections( 'nav-menus', 'side', null ); ?>
  636. </form>
  637. </div><!-- /#menu-settings-column -->
  638. <div id="menu-management-liquid">
  639. <div id="menu-management">
  640. <form id="update-nav-menu" method="post" enctype="multipart/form-data">
  641. <div class="menu-edit <?php if ( $add_new_screen ) echo 'blank-slate'; ?>">
  642. <input type="hidden" name="nav-menu-data">
  643. <?php
  644. wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
  645. wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
  646. wp_nonce_field( 'update-nav_menu', 'update-nav-menu-nonce' );
  647. $menu_name_aria_desc = $add_new_screen ? ' aria-describedby="menu-name-desc"' : '';
  648. if ( $one_theme_location_no_menus ) {
  649. $menu_name_val = 'value="' . esc_attr( 'Menu 1' ) . '"';
  650. ?>
  651. <input type="hidden" name="zero-menu-state" value="true" />
  652. <?php } else {
  653. $menu_name_val = 'value="' . esc_attr( $nav_menu_selected_title ) . '"';
  654. } ?>
  655. <input type="hidden" name="action" value="update" />
  656. <input type="hidden" name="menu" id="menu" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
  657. <div id="nav-menu-header">
  658. <div class="major-publishing-actions wp-clearfix">
  659. <label class="menu-name-label" for="menu-name"><?php _e( 'Menu Name' ); ?></label>
  660. <input name="menu-name" id="menu-name" type="text" class="menu-name regular-text menu-item-textbox" <?php echo $menu_name_val . $menu_name_aria_desc; ?> />
  661. <div class="publishing-action">
  662. <?php submit_button( empty( $nav_menu_selected_id ) ? __( 'Create Menu' ) : __( 'Save Menu' ), 'primary large menu-save', 'save_menu', false, array( 'id' => 'save_menu_header' ) ); ?>
  663. </div><!-- END .publishing-action -->
  664. </div><!-- END .major-publishing-actions -->
  665. </div><!-- END .nav-menu-header -->
  666. <div id="post-body">
  667. <div id="post-body-content" class="wp-clearfix">
  668. <?php if ( ! $add_new_screen ) : ?>
  669. <h3><?php _e( 'Menu Structure' ); ?></h3>
  670. <?php $starter_copy = ( $one_theme_location_no_menus ) ? __( 'Edit your default menu by adding or removing items. Drag each item into the order you prefer. Click Create Menu to save your changes.' ) : __( 'Drag each item into the order you prefer. Click the arrow on the right of the item to reveal additional configuration options.' ); ?>
  671. <div class="drag-instructions post-body-plain" <?php if ( isset( $menu_items ) && 0 == count( $menu_items ) ) { ?>style="display: none;"<?php } ?>>
  672. <p><?php echo $starter_copy; ?></p>
  673. </div>
  674. <?php
  675. if ( isset( $edit_markup ) && ! is_wp_error( $edit_markup ) ) {
  676. echo $edit_markup;
  677. } else {
  678. ?>
  679. <ul class="menu" id="menu-to-edit"></ul>
  680. <?php } ?>
  681. <?php endif; ?>
  682. <?php if ( $add_new_screen ) : ?>
  683. <p class="post-body-plain" id="menu-name-desc"><?php _e( 'Give your menu a name, then click Create Menu.' ); ?></p>
  684. <?php if ( isset( $_GET['use-location'] ) ) : ?>
  685. <input type="hidden" name="use-location" value="<?php echo esc_attr( $_GET['use-location'] ); ?>" />
  686. <?php endif; ?>
  687. <?php endif; ?>
  688. <div class="menu-settings" <?php if ( $one_theme_location_no_menus ) { ?>style="display: none;"<?php } ?>>
  689. <h3><?php _e( 'Menu Settings' ); ?></h3>
  690. <?php
  691. if ( ! isset( $auto_add ) ) {
  692. $auto_add = get_option( 'nav_menu_options' );
  693. if ( ! isset( $auto_add['auto_add'] ) )
  694. $auto_add = false;
  695. elseif ( false !== array_search( $nav_menu_selected_id, $auto_add['auto_add'] ) )
  696. $auto_add = true;
  697. else
  698. $auto_add = false;
  699. } ?>
  700. <fieldset class="menu-settings-group auto-add-pages">
  701. <legend class="menu-settings-group-name howto"><?php _e( 'Auto add pages' ); ?></legend>
  702. <div class="menu-settings-input checkbox-input">
  703. <input type="checkbox"<?php checked( $auto_add ); ?> name="auto-add-pages" id="auto-add-pages" value="1" /> <label for="auto-add-pages"><?php printf( __('Automatically add new top-level pages to this menu' ), esc_url( admin_url( 'edit.php?post_type=page' ) ) ); ?></label>
  704. </div>
  705. </fieldset>
  706. <?php if ( current_theme_supports( 'menus' ) ) : ?>
  707. <fieldset class="menu-settings-group menu-theme-locations">
  708. <legend class="menu-settings-group-name howto"><?php _e( 'Display location' ); ?></legend>
  709. <?php foreach ( $locations as $location => $description ) : ?>
  710. <div class="menu-settings-input checkbox-input">
  711. <input type="checkbox"<?php checked( isset( $menu_locations[ $location ] ) && $menu_locations[ $location ] == $nav_menu_selected_id ); ?> name="menu-locations[<?php echo esc_attr( $location ); ?>]" id="locations-<?php echo esc_attr( $location ); ?>" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
  712. <label for="locations-<?php echo esc_attr( $location ); ?>"><?php echo $description; ?></label>
  713. <?php if ( ! empty( $menu_locations[ $location ] ) && $menu_locations[ $location ] != $nav_menu_selected_id ) : ?>
  714. <span class="theme-location-set"><?php
  715. /* translators: %s: menu name */
  716. printf( _x( '(Currently set to: %s)', 'menu location' ),
  717. wp_get_nav_menu_object( $menu_locations[ $location ] )->name
  718. );
  719. ?></span>
  720. <?php endif; ?>
  721. </div>
  722. <?php endforeach; ?>
  723. </fieldset>
  724. <?php endif; ?>
  725. </div>
  726. </div><!-- /#post-body-content -->
  727. </div><!-- /#post-body -->
  728. <div id="nav-menu-footer">
  729. <div class="major-publishing-actions wp-clearfix">
  730. <?php if ( 0 != $menu_count && ! $add_new_screen ) : ?>
  731. <span class="delete-action">
  732. <a class="submitdelete deletion menu-delete" href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'delete', 'menu' => $nav_menu_selected_id ), admin_url( 'nav-menus.php' ) ), 'delete-nav_menu-' . $nav_menu_selected_id) ); ?>"><?php _e('Delete Menu'); ?></a>
  733. </span><!-- END .delete-action -->
  734. <?php endif; ?>
  735. <div class="publishing-action">
  736. <?php submit_button( empty( $nav_menu_selected_id ) ? __( 'Create Menu' ) : __( 'Save Menu' ), 'primary large menu-save', 'save_menu', false, array( 'id' => 'save_menu_footer' ) ); ?>
  737. </div><!-- END .publishing-action -->
  738. </div><!-- END .major-publishing-actions -->
  739. </div><!-- /#nav-menu-footer -->
  740. </div><!-- /.menu-edit -->
  741. </form><!-- /#update-nav-menu -->
  742. </div><!-- /#menu-management -->
  743. </div><!-- /#menu-management-liquid -->
  744. </div><!-- /#nav-menus-frame -->
  745. <?php endif; ?>
  746. </div><!-- /.wrap-->
  747. <?php include( ABSPATH . 'wp-admin/admin-footer.php' ); ?>