nav-menus.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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 __DIR__ . '/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. }
  18. // Permissions check.
  19. if ( ! current_user_can( 'edit_theme_options' ) ) {
  20. wp_die(
  21. '<h1>' . __( 'You need a higher level of permission.' ) . '</h1>' .
  22. '<p>' . __( 'Sorry, you are not allowed to edit theme options on this site.' ) . '</p>',
  23. 403
  24. );
  25. }
  26. wp_enqueue_script( 'nav-menu' );
  27. if ( wp_is_mobile() ) {
  28. wp_enqueue_script( 'jquery-touch-punch' );
  29. }
  30. // Container for any messages displayed to the user.
  31. $messages = array();
  32. // Container that stores the name of the active menu.
  33. $nav_menu_selected_title = '';
  34. // The menu id of the current menu being edited.
  35. $nav_menu_selected_id = isset( $_REQUEST['menu'] ) ? (int) $_REQUEST['menu'] : 0;
  36. // Get existing menu locations assignments.
  37. $locations = get_registered_nav_menus();
  38. $menu_locations = get_nav_menu_locations();
  39. $num_locations = count( array_keys( $locations ) );
  40. // Allowed actions: add, update, delete.
  41. $action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : 'edit';
  42. /*
  43. * If a JSON blob of navigation menu data is found, expand it and inject it
  44. * into `$_POST` to avoid PHP `max_input_vars` limitations. See #14134.
  45. */
  46. _wp_expand_nav_menu_post_data();
  47. switch ( $action ) {
  48. case 'add-menu-item':
  49. check_admin_referer( 'add-menu_item', 'menu-settings-column-nonce' );
  50. if ( isset( $_REQUEST['nav-menu-locations'] ) ) {
  51. set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_REQUEST['menu-locations'] ) );
  52. } elseif ( isset( $_REQUEST['menu-item'] ) ) {
  53. wp_save_nav_menu_items( $nav_menu_selected_id, $_REQUEST['menu-item'] );
  54. }
  55. break;
  56. case 'move-down-menu-item':
  57. // Moving down a menu item is the same as moving up the next in order.
  58. check_admin_referer( 'move-menu_item' );
  59. $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0;
  60. if ( is_nav_menu_item( $menu_item_id ) ) {
  61. $menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );
  62. if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
  63. $menu_id = (int) $menus[0];
  64. $ordered_menu_items = wp_get_nav_menu_items( $menu_id );
  65. $menu_item_data = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) );
  66. // Set up the data we need in one pass through the array of menu items.
  67. $dbids_to_orders = array();
  68. $orders_to_dbids = array();
  69. foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) {
  70. if ( isset( $ordered_menu_item_object->ID ) ) {
  71. if ( isset( $ordered_menu_item_object->menu_order ) ) {
  72. $dbids_to_orders[ $ordered_menu_item_object->ID ] = $ordered_menu_item_object->menu_order;
  73. $orders_to_dbids[ $ordered_menu_item_object->menu_order ] = $ordered_menu_item_object->ID;
  74. }
  75. }
  76. }
  77. // Get next in order.
  78. if ( isset( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] + 1 ] ) ) {
  79. $next_item_id = $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] + 1 ];
  80. $next_item_data = (array) wp_setup_nav_menu_item( get_post( $next_item_id ) );
  81. // If not siblings of same parent, bubble menu item up but keep order.
  82. if ( ! empty( $menu_item_data['menu_item_parent'] )
  83. && ( empty( $next_item_data['menu_item_parent'] )
  84. || (int) $next_item_data['menu_item_parent'] !== (int) $menu_item_data['menu_item_parent'] )
  85. ) {
  86. if ( in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true ) ) {
  87. $parent_db_id = (int) $menu_item_data['menu_item_parent'];
  88. } else {
  89. $parent_db_id = 0;
  90. }
  91. $parent_object = wp_setup_nav_menu_item( get_post( $parent_db_id ) );
  92. if ( ! is_wp_error( $parent_object ) ) {
  93. $parent_data = (array) $parent_object;
  94. $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
  95. update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
  96. }
  97. // Make menu item a child of its next sibling.
  98. } else {
  99. $next_item_data['menu_order'] = $next_item_data['menu_order'] - 1;
  100. $menu_item_data['menu_order'] = $menu_item_data['menu_order'] + 1;
  101. $menu_item_data['menu_item_parent'] = $next_item_data['ID'];
  102. update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
  103. wp_update_post( $menu_item_data );
  104. wp_update_post( $next_item_data );
  105. }
  106. // The item is last but still has a parent, so bubble up.
  107. } elseif ( ! empty( $menu_item_data['menu_item_parent'] )
  108. && in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true )
  109. ) {
  110. $menu_item_data['menu_item_parent'] = (int) get_post_meta( $menu_item_data['menu_item_parent'], '_menu_item_menu_item_parent', true );
  111. update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
  112. }
  113. }
  114. }
  115. break;
  116. case 'move-up-menu-item':
  117. check_admin_referer( 'move-menu_item' );
  118. $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0;
  119. if ( is_nav_menu_item( $menu_item_id ) ) {
  120. if ( isset( $_REQUEST['menu'] ) ) {
  121. $menus = array( (int) $_REQUEST['menu'] );
  122. } else {
  123. $menus = wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );
  124. }
  125. if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
  126. $menu_id = (int) $menus[0];
  127. $ordered_menu_items = wp_get_nav_menu_items( $menu_id );
  128. $menu_item_data = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) );
  129. // Set up the data we need in one pass through the array of menu items.
  130. $dbids_to_orders = array();
  131. $orders_to_dbids = array();
  132. foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) {
  133. if ( isset( $ordered_menu_item_object->ID ) ) {
  134. if ( isset( $ordered_menu_item_object->menu_order ) ) {
  135. $dbids_to_orders[ $ordered_menu_item_object->ID ] = $ordered_menu_item_object->menu_order;
  136. $orders_to_dbids[ $ordered_menu_item_object->menu_order ] = $ordered_menu_item_object->ID;
  137. }
  138. }
  139. }
  140. // If this menu item is not first.
  141. if ( ! empty( $dbids_to_orders[ $menu_item_id ] )
  142. && ! empty( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] )
  143. ) {
  144. // If this menu item is a child of the previous.
  145. if ( ! empty( $menu_item_data['menu_item_parent'] )
  146. && in_array( (int) $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ), true )
  147. && isset( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] )
  148. && ( (int) $menu_item_data['menu_item_parent'] === $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] )
  149. ) {
  150. if ( in_array( (int) $menu_item_data['menu_item_parent'], $orders_to_dbids, true ) ) {
  151. $parent_db_id = (int) $menu_item_data['menu_item_parent'];
  152. } else {
  153. $parent_db_id = 0;
  154. }
  155. $parent_object = wp_setup_nav_menu_item( get_post( $parent_db_id ) );
  156. if ( ! is_wp_error( $parent_object ) ) {
  157. $parent_data = (array) $parent_object;
  158. /*
  159. * If there is something before the parent and parent a child of it,
  160. * make menu item a child also of it.
  161. */
  162. if ( ! empty( $dbids_to_orders[ $parent_db_id ] )
  163. && ! empty( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ] )
  164. && ! empty( $parent_data['menu_item_parent'] )
  165. ) {
  166. $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
  167. /*
  168. * Else if there is something before parent and parent not a child of it,
  169. * make menu item a child of that something's parent
  170. */
  171. } elseif ( ! empty( $dbids_to_orders[ $parent_db_id ] )
  172. && ! empty( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ] )
  173. ) {
  174. $_possible_parent_id = (int) get_post_meta( $orders_to_dbids[ $dbids_to_orders[ $parent_db_id ] - 1 ], '_menu_item_menu_item_parent', true );
  175. if ( in_array( $_possible_parent_id, array_keys( $dbids_to_orders ), true ) ) {
  176. $menu_item_data['menu_item_parent'] = $_possible_parent_id;
  177. } else {
  178. $menu_item_data['menu_item_parent'] = 0;
  179. }
  180. // Else there isn't something before the parent.
  181. } else {
  182. $menu_item_data['menu_item_parent'] = 0;
  183. }
  184. // Set former parent's [menu_order] to that of menu-item's.
  185. $parent_data['menu_order'] = $parent_data['menu_order'] + 1;
  186. // Set menu-item's [menu_order] to that of former parent.
  187. $menu_item_data['menu_order'] = $menu_item_data['menu_order'] - 1;
  188. // Save changes.
  189. update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
  190. wp_update_post( $menu_item_data );
  191. wp_update_post( $parent_data );
  192. }
  193. // Else this menu item is not a child of the previous.
  194. } elseif ( empty( $menu_item_data['menu_order'] )
  195. || empty( $menu_item_data['menu_item_parent'] )
  196. || ! in_array( (int) $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ), true )
  197. || empty( $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] )
  198. || $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ] !== (int) $menu_item_data['menu_item_parent']
  199. ) {
  200. // Just make it a child of the previous; keep the order.
  201. $menu_item_data['menu_item_parent'] = (int) $orders_to_dbids[ $dbids_to_orders[ $menu_item_id ] - 1 ];
  202. update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
  203. wp_update_post( $menu_item_data );
  204. }
  205. }
  206. }
  207. }
  208. break;
  209. case 'delete-menu-item':
  210. $menu_item_id = (int) $_REQUEST['menu-item'];
  211. check_admin_referer( 'delete-menu_item_' . $menu_item_id );
  212. if ( is_nav_menu_item( $menu_item_id ) && wp_delete_post( $menu_item_id, true ) ) {
  213. $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'The menu item has been successfully deleted.' ) . '</p></div>';
  214. }
  215. break;
  216. case 'delete':
  217. check_admin_referer( 'delete-nav_menu-' . $nav_menu_selected_id );
  218. if ( is_nav_menu( $nav_menu_selected_id ) ) {
  219. $deletion = wp_delete_nav_menu( $nav_menu_selected_id );
  220. } else {
  221. // Reset the selected menu.
  222. $nav_menu_selected_id = 0;
  223. unset( $_REQUEST['menu'] );
  224. }
  225. if ( ! isset( $deletion ) ) {
  226. break;
  227. }
  228. if ( is_wp_error( $deletion ) ) {
  229. $messages[] = '<div id="message" class="error notice is-dismissible"><p>' . $deletion->get_error_message() . '</p></div>';
  230. } else {
  231. $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'The menu has been successfully deleted.' ) . '</p></div>';
  232. }
  233. break;
  234. case 'delete_menus':
  235. check_admin_referer( 'nav_menus_bulk_actions' );
  236. foreach ( $_REQUEST['delete_menus'] as $menu_id_to_delete ) {
  237. if ( ! is_nav_menu( $menu_id_to_delete ) ) {
  238. continue;
  239. }
  240. $deletion = wp_delete_nav_menu( $menu_id_to_delete );
  241. if ( is_wp_error( $deletion ) ) {
  242. $messages[] = '<div id="message" class="error notice is-dismissible"><p>' . $deletion->get_error_message() . '</p></div>';
  243. $deletion_error = true;
  244. }
  245. }
  246. if ( empty( $deletion_error ) ) {
  247. $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'Selected menus have been successfully deleted.' ) . '</p></div>';
  248. }
  249. break;
  250. case 'update':
  251. check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
  252. // Merge new and existing menu locations if any new ones are set.
  253. $new_menu_locations = array();
  254. if ( isset( $_POST['menu-locations'] ) ) {
  255. $new_menu_locations = array_map( 'absint', $_POST['menu-locations'] );
  256. $menu_locations = array_merge( $menu_locations, $new_menu_locations );
  257. }
  258. // Add Menu.
  259. if ( 0 === $nav_menu_selected_id ) {
  260. $new_menu_title = trim( esc_html( $_POST['menu-name'] ) );
  261. if ( $new_menu_title ) {
  262. $_nav_menu_selected_id = wp_update_nav_menu_object( 0, array( 'menu-name' => $new_menu_title ) );
  263. if ( is_wp_error( $_nav_menu_selected_id ) ) {
  264. $messages[] = '<div id="message" class="error notice is-dismissible"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
  265. } else {
  266. $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
  267. $nav_menu_selected_id = $_nav_menu_selected_id;
  268. $nav_menu_selected_title = $_menu_object->name;
  269. if ( isset( $_REQUEST['menu-item'] ) ) {
  270. wp_save_nav_menu_items( $nav_menu_selected_id, absint( $_REQUEST['menu-item'] ) );
  271. }
  272. if ( isset( $_REQUEST['zero-menu-state'] ) || ! empty( $_POST['auto-add-pages'] ) ) {
  273. // If there are menu items, add them.
  274. wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selected_title );
  275. }
  276. if ( isset( $_REQUEST['zero-menu-state'] ) ) {
  277. // Auto-save nav_menu_locations.
  278. $locations = get_nav_menu_locations();
  279. foreach ( $locations as $location => $menu_id ) {
  280. $locations[ $location ] = $nav_menu_selected_id;
  281. break; // There should only be 1.
  282. }
  283. set_theme_mod( 'nav_menu_locations', $locations );
  284. } elseif ( count( $new_menu_locations ) > 0 ) {
  285. // If locations have been selected for the new menu, save those.
  286. $locations = get_nav_menu_locations();
  287. foreach ( array_keys( $new_menu_locations ) as $location ) {
  288. $locations[ $location ] = $nav_menu_selected_id;
  289. }
  290. set_theme_mod( 'nav_menu_locations', $locations );
  291. }
  292. if ( isset( $_REQUEST['use-location'] ) ) {
  293. $locations = get_registered_nav_menus();
  294. $menu_locations = get_nav_menu_locations();
  295. if ( isset( $locations[ $_REQUEST['use-location'] ] ) ) {
  296. $menu_locations[ $_REQUEST['use-location'] ] = $nav_menu_selected_id;
  297. }
  298. set_theme_mod( 'nav_menu_locations', $menu_locations );
  299. }
  300. wp_redirect( admin_url( 'nav-menus.php?menu=' . $_nav_menu_selected_id ) );
  301. exit;
  302. }
  303. } else {
  304. $messages[] = '<div id="message" class="error notice is-dismissible"><p>' . __( 'Please enter a valid menu name.' ) . '</p></div>';
  305. }
  306. // Update existing menu.
  307. } else {
  308. // Remove menu locations that have been unchecked.
  309. foreach ( $locations as $location => $description ) {
  310. if ( ( empty( $_POST['menu-locations'] ) || empty( $_POST['menu-locations'][ $location ] ) )
  311. && isset( $menu_locations[ $location ] ) && $menu_locations[ $location ] === $nav_menu_selected_id
  312. ) {
  313. unset( $menu_locations[ $location ] );
  314. }
  315. }
  316. // Set menu locations.
  317. set_theme_mod( 'nav_menu_locations', $menu_locations );
  318. $_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
  319. $menu_title = trim( esc_html( $_POST['menu-name'] ) );
  320. if ( ! $menu_title ) {
  321. $messages[] = '<div id="message" class="error notice is-dismissible"><p>' . __( 'Please enter a valid menu name.' ) . '</p></div>';
  322. $menu_title = $_menu_object->name;
  323. }
  324. if ( ! is_wp_error( $_menu_object ) ) {
  325. $_nav_menu_selected_id = wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $menu_title ) );
  326. if ( is_wp_error( $_nav_menu_selected_id ) ) {
  327. $_menu_object = $_nav_menu_selected_id;
  328. $messages[] = '<div id="message" class="error notice is-dismissible"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
  329. } else {
  330. $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
  331. $nav_menu_selected_title = $_menu_object->name;
  332. }
  333. }
  334. // Update menu items.
  335. if ( ! is_wp_error( $_menu_object ) ) {
  336. $messages = array_merge( $messages, wp_nav_menu_update_menu_items( $_nav_menu_selected_id, $nav_menu_selected_title ) );
  337. // If the menu ID changed, redirect to the new URL.
  338. if ( $nav_menu_selected_id !== $_nav_menu_selected_id ) {
  339. wp_redirect( admin_url( 'nav-menus.php?menu=' . (int) $_nav_menu_selected_id ) );
  340. exit;
  341. }
  342. }
  343. }
  344. break;
  345. case 'locations':
  346. if ( ! $num_locations ) {
  347. wp_redirect( admin_url( 'nav-menus.php' ) );
  348. exit;
  349. }
  350. add_filter( 'screen_options_show_screen', '__return_false' );
  351. if ( isset( $_POST['menu-locations'] ) ) {
  352. check_admin_referer( 'save-menu-locations' );
  353. $new_menu_locations = array_map( 'absint', $_POST['menu-locations'] );
  354. $menu_locations = array_merge( $menu_locations, $new_menu_locations );
  355. // Set menu locations.
  356. set_theme_mod( 'nav_menu_locations', $menu_locations );
  357. $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'Menu locations updated.' ) . '</p></div>';
  358. }
  359. break;
  360. }
  361. // Get all nav menus.
  362. $nav_menus = wp_get_nav_menus();
  363. $menu_count = count( $nav_menus );
  364. // Are we on the add new screen?
  365. $add_new_screen = ( isset( $_GET['menu'] ) && 0 === (int) $_GET['menu'] ) ? true : false;
  366. $locations_screen = ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) ? true : false;
  367. $page_count = wp_count_posts( 'page' );
  368. /*
  369. * If we have one theme location, and zero menus, we take them right
  370. * into editing their first menu.
  371. */
  372. if ( 1 === count( get_registered_nav_menus() ) && ! $add_new_screen
  373. && empty( $nav_menus ) && ! empty( $page_count->publish )
  374. ) {
  375. $one_theme_location_no_menus = true;
  376. } else {
  377. $one_theme_location_no_menus = false;
  378. }
  379. $nav_menus_l10n = array(
  380. 'oneThemeLocationNoMenus' => $one_theme_location_no_menus,
  381. 'moveUp' => __( 'Move up one' ),
  382. 'moveDown' => __( 'Move down one' ),
  383. 'moveToTop' => __( 'Move to the top' ),
  384. /* translators: %s: Previous item name. */
  385. 'moveUnder' => __( 'Move under %s' ),
  386. /* translators: %s: Previous item name. */
  387. 'moveOutFrom' => __( 'Move out from under %s' ),
  388. /* translators: %s: Previous item name. */
  389. 'under' => __( 'Under %s' ),
  390. /* translators: %s: Previous item name. */
  391. 'outFrom' => __( 'Out from under %s' ),
  392. /* translators: 1: Item name, 2: Item position, 3: Total number of items. */
  393. 'menuFocus' => __( '%1$s. Menu item %2$d of %3$d.' ),
  394. /* translators: 1: Item name, 2: Item position, 3: Parent item name. */
  395. 'subMenuFocus' => __( '%1$s. Sub item number %2$d under %3$s.' ),
  396. /* translators: %s: Item name. */
  397. 'menuItemDeletion' => __( 'item %s' ),
  398. /* translators: %s: Item name. */
  399. 'itemsDeleted' => __( 'Deleted menu item: %s.' ),
  400. 'itemAdded' => __( 'Menu item added' ),
  401. 'itemRemoved' => __( 'Menu item removed' ),
  402. 'movedUp' => __( 'Menu item moved up' ),
  403. 'movedDown' => __( 'Menu item moved down' ),
  404. 'movedTop' => __( 'Menu item moved to the top' ),
  405. 'movedLeft' => __( 'Menu item moved out of submenu' ),
  406. 'movedRight' => __( 'Menu item is now a sub-item' ),
  407. );
  408. wp_localize_script( 'nav-menu', 'menus', $nav_menus_l10n );
  409. /*
  410. * Redirect to add screen if there are no menus and this users has either zero,
  411. * or more than 1 theme locations.
  412. */
  413. if ( 0 === $menu_count && ! $add_new_screen && ! $one_theme_location_no_menus ) {
  414. wp_redirect( admin_url( 'nav-menus.php?action=edit&menu=0' ) );
  415. }
  416. // Get recently edited nav menu.
  417. $recently_edited = absint( get_user_option( 'nav_menu_recently_edited' ) );
  418. if ( empty( $recently_edited ) && is_nav_menu( $nav_menu_selected_id ) ) {
  419. $recently_edited = $nav_menu_selected_id;
  420. }
  421. // Use $recently_edited if none are selected.
  422. if ( empty( $nav_menu_selected_id ) && ! isset( $_GET['menu'] ) && is_nav_menu( $recently_edited ) ) {
  423. $nav_menu_selected_id = $recently_edited;
  424. }
  425. // On deletion of menu, if another menu exists, show it.
  426. if ( ! $add_new_screen && $menu_count > 0 && isset( $_GET['action'] ) && 'delete' === $_GET['action'] ) {
  427. $nav_menu_selected_id = $nav_menus[0]->term_id;
  428. }
  429. // Set $nav_menu_selected_id to 0 if no menus.
  430. if ( $one_theme_location_no_menus ) {
  431. $nav_menu_selected_id = 0;
  432. } elseif ( empty( $nav_menu_selected_id ) && ! empty( $nav_menus ) && ! $add_new_screen ) {
  433. // If we have no selection yet, and we have menus, set to the first one in the list.
  434. $nav_menu_selected_id = $nav_menus[0]->term_id;
  435. }
  436. // Update the user's setting.
  437. if ( $nav_menu_selected_id !== $recently_edited && is_nav_menu( $nav_menu_selected_id ) ) {
  438. update_user_meta( $current_user->ID, 'nav_menu_recently_edited', $nav_menu_selected_id );
  439. }
  440. // If there's a menu, get its name.
  441. if ( ! $nav_menu_selected_title && is_nav_menu( $nav_menu_selected_id ) ) {
  442. $_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
  443. $nav_menu_selected_title = ! is_wp_error( $_menu_object ) ? $_menu_object->name : '';
  444. }
  445. // Generate truncated menu names.
  446. foreach ( (array) $nav_menus as $key => $_nav_menu ) {
  447. $nav_menus[ $key ]->truncated_name = wp_html_excerpt( $_nav_menu->name, 40, '&hellip;' );
  448. }
  449. // Retrieve menu locations.
  450. if ( current_theme_supports( 'menus' ) ) {
  451. $locations = get_registered_nav_menus();
  452. $menu_locations = get_nav_menu_locations();
  453. }
  454. /*
  455. * Ensure the user will be able to scroll horizontally
  456. * by adding a class for the max menu depth.
  457. *
  458. * @global int $_wp_nav_menu_max_depth
  459. */
  460. global $_wp_nav_menu_max_depth;
  461. $_wp_nav_menu_max_depth = 0;
  462. // Calling wp_get_nav_menu_to_edit generates $_wp_nav_menu_max_depth.
  463. if ( is_nav_menu( $nav_menu_selected_id ) ) {
  464. $menu_items = wp_get_nav_menu_items( $nav_menu_selected_id, array( 'post_status' => 'any' ) );
  465. $edit_markup = wp_get_nav_menu_to_edit( $nav_menu_selected_id );
  466. }
  467. /**
  468. * @global int $_wp_nav_menu_max_depth
  469. *
  470. * @param string $classes
  471. * @return string
  472. */
  473. function wp_nav_menu_max_depth( $classes ) {
  474. global $_wp_nav_menu_max_depth;
  475. return "$classes menu-max-depth-$_wp_nav_menu_max_depth";
  476. }
  477. add_filter( 'admin_body_class', 'wp_nav_menu_max_depth' );
  478. wp_nav_menu_setup();
  479. wp_initial_nav_menu_meta_boxes();
  480. if ( ! current_theme_supports( 'menus' ) && ! $num_locations ) {
  481. $messages[] = '<div id="message" class="updated"><p>' . sprintf(
  482. /* translators: %s: URL to Widgets screen. */
  483. __( 'Your theme does not natively support menus, but you can use them in sidebars by adding a &#8220;Navigation Menu&#8221; widget on the <a href="%s">Widgets</a> screen.' ),
  484. admin_url( 'widgets.php' )
  485. ) . '</p></div>';
  486. }
  487. if ( ! $locations_screen ) : // Main tab.
  488. $overview = '<p>' . __( 'This screen is used for managing your navigation menus.' ) . '</p>';
  489. $overview .= '<p>' . sprintf(
  490. /* translators: 1: URL to Widgets screen, 2 and 3: The names of the default themes. */
  491. __( 'Menus can be displayed in locations defined by your theme, even used in sidebars by adding a &#8220;Navigation Menu&#8221; widget on the <a href="%1$s">Widgets</a> screen. If your theme does not support the navigation 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.' ),
  492. admin_url( 'widgets.php' ),
  493. 'Twenty Twenty',
  494. 'Twenty Twenty-One'
  495. ) . '</p>';
  496. $overview .= '<p>' . __( 'From this screen you can:' ) . '</p>';
  497. $overview .= '<ul><li>' . __( 'Create, edit, and delete menus' ) . '</li>';
  498. $overview .= '<li>' . __( 'Add, organize, and modify individual menu items' ) . '</li></ul>';
  499. get_current_screen()->add_help_tab(
  500. array(
  501. 'id' => 'overview',
  502. 'title' => __( 'Overview' ),
  503. 'content' => $overview,
  504. )
  505. );
  506. $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>';
  507. $menu_management .= '<ul><li>' . __( 'To edit an existing menu, <strong>choose a menu from the dropdown and click Select</strong>' ) . '</li>';
  508. $menu_management .= '<li>' . __( 'If you have not yet created any menus, <strong>click the &#8217;create a new menu&#8217; link</strong> to get started' ) . '</li></ul>';
  509. $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>';
  510. get_current_screen()->add_help_tab(
  511. array(
  512. 'id' => 'menu-management',
  513. 'title' => __( 'Menu Management' ),
  514. 'content' => $menu_management,
  515. )
  516. );
  517. $editing_menus = '<p>' . __( 'Each navigation 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>';
  518. $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>';
  519. $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>';
  520. $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>';
  521. $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>';
  522. $editing_menus .= '<li>' . __( 'Delete a menu item by <strong>expanding it and clicking the Remove link</strong>' ) . '</li></ul>';
  523. get_current_screen()->add_help_tab(
  524. array(
  525. 'id' => 'editing-menus',
  526. 'title' => __( 'Editing Menus' ),
  527. 'content' => $editing_menus,
  528. )
  529. );
  530. else : // Locations tab.
  531. $locations_overview = '<p>' . __( 'This screen is used for globally assigning menus to locations defined by your theme.' ) . '</p>';
  532. $locations_overview .= '<ul><li>' . __( 'To assign menus to one or more theme locations, <strong>select a menu from each location&#8217;s dropdown</strong>. When you are finished, <strong>click Save Changes</strong>' ) . '</li>';
  533. $locations_overview .= '<li>' . __( 'To edit a menu currently assigned to a theme location, <strong>click the adjacent &#8217;Edit&#8217; link</strong>' ) . '</li>';
  534. $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>';
  535. get_current_screen()->add_help_tab(
  536. array(
  537. 'id' => 'locations-overview',
  538. 'title' => __( 'Overview' ),
  539. 'content' => $locations_overview,
  540. )
  541. );
  542. endif;
  543. get_current_screen()->set_help_sidebar(
  544. '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
  545. '<p>' . __( '<a href="https://wordpress.org/support/article/appearance-menus-screen/">Documentation on Menus</a>' ) . '</p>' .
  546. '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
  547. );
  548. // Get the admin header.
  549. require_once ABSPATH . 'wp-admin/admin-header.php';
  550. ?>
  551. <div class="wrap">
  552. <h1 class="wp-heading-inline"><?php esc_html_e( 'Menus' ); ?></h1>
  553. <?php
  554. if ( current_user_can( 'customize' ) ) :
  555. $focus = $locations_screen ? array( 'section' => 'menu_locations' ) : array( 'panel' => 'nav_menus' );
  556. printf(
  557. ' <a class="page-title-action hide-if-no-customize" href="%1$s">%2$s</a>',
  558. esc_url(
  559. add_query_arg(
  560. array(
  561. array( 'autofocus' => $focus ),
  562. 'return' => urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ),
  563. ),
  564. admin_url( 'customize.php' )
  565. )
  566. ),
  567. __( 'Manage with Live Preview' )
  568. );
  569. endif;
  570. $nav_tab_active_class = '';
  571. $nav_aria_current = '';
  572. if ( ! isset( $_GET['action'] ) || isset( $_GET['action'] ) && 'locations' !== $_GET['action'] ) {
  573. $nav_tab_active_class = ' nav-tab-active';
  574. $nav_aria_current = ' aria-current="page"';
  575. }
  576. ?>
  577. <hr class="wp-header-end">
  578. <nav class="nav-tab-wrapper wp-clearfix" aria-label="<?php esc_attr_e( 'Secondary menu' ); ?>">
  579. <a href="<?php echo esc_url( admin_url( 'nav-menus.php' ) ); ?>" class="nav-tab<?php echo $nav_tab_active_class; ?>"<?php echo $nav_aria_current; ?>><?php esc_html_e( 'Edit Menus' ); ?></a>
  580. <?php
  581. if ( $num_locations && $menu_count ) {
  582. $active_tab_class = '';
  583. $aria_current = '';
  584. if ( $locations_screen ) {
  585. $active_tab_class = ' nav-tab-active';
  586. $aria_current = ' aria-current="page"';
  587. }
  588. ?>
  589. <a href="<?php echo esc_url( add_query_arg( array( 'action' => 'locations' ), admin_url( 'nav-menus.php' ) ) ); ?>" class="nav-tab<?php echo $active_tab_class; ?>"<?php echo $aria_current; ?>><?php esc_html_e( 'Manage Locations' ); ?></a>
  590. <?php
  591. }
  592. ?>
  593. </nav>
  594. <?php
  595. foreach ( $messages as $message ) :
  596. echo $message . "\n";
  597. endforeach;
  598. ?>
  599. <?php
  600. if ( $locations_screen ) :
  601. if ( 1 === $num_locations ) {
  602. echo '<p>' . __( 'Your theme supports one menu. Select which menu you would like to use.' ) . '</p>';
  603. } else {
  604. echo '<p>' . sprintf(
  605. /* translators: %s: Number of menus. */
  606. _n(
  607. 'Your theme supports %s menu. Select which menu appears in each location.',
  608. 'Your theme supports %s menus. Select which menu appears in each location.',
  609. $num_locations
  610. ),
  611. number_format_i18n( $num_locations )
  612. ) . '</p>';
  613. }
  614. ?>
  615. <div id="menu-locations-wrap">
  616. <form method="post" action="<?php echo esc_url( add_query_arg( array( 'action' => 'locations' ), admin_url( 'nav-menus.php' ) ) ); ?>">
  617. <table class="widefat fixed" id="menu-locations-table">
  618. <thead>
  619. <tr>
  620. <th scope="col" class="manage-column column-locations"><?php _e( 'Theme Location' ); ?></th>
  621. <th scope="col" class="manage-column column-menus"><?php _e( 'Assigned Menu' ); ?></th>
  622. </tr>
  623. </thead>
  624. <tbody class="menu-locations">
  625. <?php foreach ( $locations as $_location => $_name ) { ?>
  626. <tr class="menu-locations-row">
  627. <td class="menu-location-title"><label for="locations-<?php echo $_location; ?>"><?php echo $_name; ?></label></td>
  628. <td class="menu-location-menus">
  629. <select name="menu-locations[<?php echo $_location; ?>]" id="locations-<?php echo $_location; ?>">
  630. <option value="0"><?php printf( '&mdash; %s &mdash;', esc_html__( 'Select a Menu' ) ); ?></option>
  631. <?php
  632. foreach ( $nav_menus as $menu ) :
  633. $data_orig = '';
  634. $selected = isset( $menu_locations[ $_location ] ) && $menu_locations[ $_location ] === $menu->term_id;
  635. if ( $selected ) {
  636. $data_orig = 'data-orig="true"';
  637. }
  638. ?>
  639. <option <?php echo $data_orig; ?> <?php selected( $selected ); ?> value="<?php echo $menu->term_id; ?>">
  640. <?php echo wp_html_excerpt( $menu->name, 40, '&hellip;' ); ?>
  641. </option>
  642. <?php endforeach; ?>
  643. </select>
  644. <div class="locations-row-links">
  645. <?php if ( isset( $menu_locations[ $_location ] ) && 0 !== $menu_locations[ $_location ] ) : ?>
  646. <span class="locations-edit-menu-link">
  647. <a href="
  648. <?php
  649. echo esc_url(
  650. add_query_arg(
  651. array(
  652. 'action' => 'edit',
  653. 'menu' => $menu_locations[ $_location ],
  654. ),
  655. admin_url( 'nav-menus.php' )
  656. )
  657. );
  658. ?>
  659. ">
  660. <span aria-hidden="true"><?php _ex( 'Edit', 'menu' ); ?></span><span class="screen-reader-text"><?php _e( 'Edit selected menu' ); ?></span>
  661. </a>
  662. </span>
  663. <?php endif; ?>
  664. <span class="locations-add-menu-link">
  665. <a href="
  666. <?php
  667. echo esc_url(
  668. add_query_arg(
  669. array(
  670. 'action' => 'edit',
  671. 'menu' => 0,
  672. 'use-location' => $_location,
  673. ),
  674. admin_url( 'nav-menus.php' )
  675. )
  676. );
  677. ?>
  678. ">
  679. <?php _ex( 'Use new menu', 'menu' ); ?>
  680. </a>
  681. </span>
  682. </div><!-- .locations-row-links -->
  683. </td><!-- .menu-location-menus -->
  684. </tr><!-- .menu-locations-row -->
  685. <?php } // End foreach. ?>
  686. </tbody>
  687. </table>
  688. <p class="button-controls wp-clearfix"><?php submit_button( __( 'Save Changes' ), 'primary left', 'nav-menu-locations', false ); ?></p>
  689. <?php wp_nonce_field( 'save-menu-locations' ); ?>
  690. <input type="hidden" name="menu" id="nav-menu-meta-object-id" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
  691. </form>
  692. </div><!-- #menu-locations-wrap -->
  693. <?php
  694. /**
  695. * Fires after the menu locations table is displayed.
  696. *
  697. * @since 3.6.0
  698. */
  699. do_action( 'after_menu_locations_table' );
  700. ?>
  701. <?php else : ?>
  702. <div class="manage-menus">
  703. <?php if ( $menu_count < 1 ) : ?>
  704. <span class="first-menu-message">
  705. <?php _e( 'Create your first menu below.' ); ?>
  706. <span class="screen-reader-text"><?php _e( 'Fill in the Menu Name and click the Create Menu button to create your first menu.' ); ?></span>
  707. </span><!-- /first-menu-message -->
  708. <?php elseif ( $menu_count < 2 ) : ?>
  709. <span class="add-edit-menu-action">
  710. <?php
  711. printf(
  712. /* translators: %s: URL to create a new menu. */
  713. __( 'Edit your menu below, or <a href="%s">create a new menu</a>. Do not forget to save your changes!' ),
  714. esc_url(
  715. add_query_arg(
  716. array(
  717. 'action' => 'edit',
  718. 'menu' => 0,
  719. ),
  720. admin_url( 'nav-menus.php' )
  721. )
  722. )
  723. );
  724. ?>
  725. <span class="screen-reader-text"><?php _e( 'Click the Save Menu button to save your changes.' ); ?></span>
  726. </span><!-- /add-edit-menu-action -->
  727. <?php else : ?>
  728. <form method="get" action="<?php echo esc_url( admin_url( 'nav-menus.php' ) ); ?>">
  729. <input type="hidden" name="action" value="edit" />
  730. <label for="select-menu-to-edit" class="selected-menu"><?php _e( 'Select a menu to edit:' ); ?></label>
  731. <select name="menu" id="select-menu-to-edit">
  732. <?php if ( $add_new_screen ) : ?>
  733. <option value="0" selected="selected"><?php _e( '&mdash; Select &mdash;' ); ?></option>
  734. <?php endif; ?>
  735. <?php foreach ( (array) $nav_menus as $_nav_menu ) : ?>
  736. <option value="<?php echo esc_attr( $_nav_menu->term_id ); ?>" <?php selected( $_nav_menu->term_id, $nav_menu_selected_id ); ?>>
  737. <?php
  738. echo esc_html( $_nav_menu->truncated_name );
  739. if ( ! empty( $menu_locations ) && in_array( $_nav_menu->term_id, $menu_locations, true ) ) {
  740. $locations_assigned_to_this_menu = array();
  741. foreach ( array_keys( $menu_locations, $_nav_menu->term_id, true ) as $menu_location_key ) {
  742. if ( isset( $locations[ $menu_location_key ] ) ) {
  743. $locations_assigned_to_this_menu[] = $locations[ $menu_location_key ];
  744. }
  745. }
  746. /**
  747. * Filters the number of locations listed per menu in the drop-down select.
  748. *
  749. * @since 3.6.0
  750. *
  751. * @param int $locations Number of menu locations to list. Default 3.
  752. */
  753. $locations_listed_per_menu = absint( apply_filters( 'wp_nav_locations_listed_per_menu', 3 ) );
  754. $assigned_locations = array_slice( $locations_assigned_to_this_menu, 0, $locations_listed_per_menu );
  755. // Adds ellipses following the number of locations defined in $assigned_locations.
  756. if ( ! empty( $assigned_locations ) ) {
  757. printf(
  758. ' (%1$s%2$s)',
  759. implode( ', ', $assigned_locations ),
  760. count( $locations_assigned_to_this_menu ) > count( $assigned_locations ) ? ' &hellip;' : ''
  761. );
  762. }
  763. }
  764. ?>
  765. </option>
  766. <?php endforeach; ?>
  767. </select>
  768. <span class="submit-btn"><input type="submit" class="button" value="<?php esc_attr_e( 'Select' ); ?>"></span>
  769. <span class="add-new-menu-action">
  770. <?php
  771. printf(
  772. /* translators: %s: URL to create a new menu. */
  773. __( 'or <a href="%s">create a new menu</a>. Do not forget to save your changes!' ),
  774. esc_url(
  775. add_query_arg(
  776. array(
  777. 'action' => 'edit',
  778. 'menu' => 0,
  779. ),
  780. admin_url( 'nav-menus.php' )
  781. )
  782. )
  783. );
  784. ?>
  785. <span class="screen-reader-text"><?php _e( 'Click the Save Menu button to save your changes.' ); ?></span>
  786. </span><!-- /add-new-menu-action -->
  787. </form>
  788. <?php
  789. endif;
  790. $metabox_holder_disabled_class = '';
  791. if ( isset( $_GET['menu'] ) && 0 === (int) $_GET['menu'] ) {
  792. $metabox_holder_disabled_class = ' metabox-holder-disabled';
  793. }
  794. ?>
  795. </div><!-- /manage-menus -->
  796. <div id="nav-menus-frame" class="wp-clearfix">
  797. <div id="menu-settings-column" class="metabox-holder<?php echo $metabox_holder_disabled_class; ?>">
  798. <div class="clear"></div>
  799. <form id="nav-menu-meta" class="nav-menu-meta" method="post" enctype="multipart/form-data">
  800. <input type="hidden" name="menu" id="nav-menu-meta-object-id" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
  801. <input type="hidden" name="action" value="add-menu-item" />
  802. <?php wp_nonce_field( 'add-menu_item', 'menu-settings-column-nonce' ); ?>
  803. <h2><?php _e( 'Add menu items' ); ?></h2>
  804. <?php do_accordion_sections( 'nav-menus', 'side', null ); ?>
  805. </form>
  806. </div><!-- /#menu-settings-column -->
  807. <div id="menu-management-liquid">
  808. <div id="menu-management">
  809. <form id="update-nav-menu" method="post" enctype="multipart/form-data">
  810. <h2><?php _e( 'Menu structure' ); ?></h2>
  811. <div class="menu-edit">
  812. <input type="hidden" name="nav-menu-data">
  813. <?php
  814. wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
  815. wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
  816. wp_nonce_field( 'update-nav_menu', 'update-nav-menu-nonce' );
  817. $menu_name_aria_desc = $add_new_screen ? ' aria-describedby="menu-name-desc"' : '';
  818. if ( $one_theme_location_no_menus ) {
  819. $menu_name_val = 'value="' . esc_attr( 'Menu 1' ) . '"';
  820. ?>
  821. <input type="hidden" name="zero-menu-state" value="true" />
  822. <?php
  823. } else {
  824. $menu_name_val = 'value="' . esc_attr( $nav_menu_selected_title ) . '"';
  825. }
  826. ?>
  827. <input type="hidden" name="action" value="update" />
  828. <input type="hidden" name="menu" id="menu" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
  829. <div id="nav-menu-header">
  830. <div class="major-publishing-actions wp-clearfix">
  831. <label class="menu-name-label" for="menu-name"><?php _e( 'Menu Name' ); ?></label>
  832. <input name="menu-name" id="menu-name" type="text" class="menu-name regular-text menu-item-textbox form-required" required="required" <?php echo $menu_name_val . $menu_name_aria_desc; ?> />
  833. <div class="publishing-action">
  834. <?php submit_button( empty( $nav_menu_selected_id ) ? __( 'Create Menu' ) : __( 'Save Menu' ), 'primary large menu-save', 'save_menu', false, array( 'id' => 'save_menu_header' ) ); ?>
  835. </div><!-- END .publishing-action -->
  836. </div><!-- END .major-publishing-actions -->
  837. </div><!-- END .nav-menu-header -->
  838. <div id="post-body">
  839. <div id="post-body-content" class="wp-clearfix">
  840. <?php if ( ! $add_new_screen ) : ?>
  841. <?php
  842. $hide_style = '';
  843. if ( isset( $menu_items ) && 0 === count( $menu_items ) ) {
  844. $hide_style = 'style="display: none;"';
  845. }
  846. if ( $one_theme_location_no_menus ) {
  847. $starter_copy = __( 'Edit your default menu by adding or removing items. Drag the items into the order you prefer. Click Create Menu to save your changes.' );
  848. } else {
  849. $starter_copy = __( 'Drag the items into the order you prefer. Click the arrow on the right of the item to reveal additional configuration options.' );
  850. }
  851. ?>
  852. <div class="drag-instructions post-body-plain" <?php echo $hide_style; ?>>
  853. <p><?php echo $starter_copy; ?></p>
  854. </div>
  855. <?php if ( ! $add_new_screen ) : ?>
  856. <div id="nav-menu-bulk-actions-top" class="bulk-actions" <?php echo $hide_style; ?>>
  857. <label class="bulk-select-button" for="bulk-select-switcher-top">
  858. <input type="checkbox" id="bulk-select-switcher-top" name="bulk-select-switcher-top" class="bulk-select-switcher">
  859. <span class="bulk-select-button-label"><?php _e( 'Bulk Select' ); ?></span>
  860. </label>
  861. </div>
  862. <?php endif; ?>
  863. <?php
  864. if ( isset( $edit_markup ) && ! is_wp_error( $edit_markup ) ) {
  865. echo $edit_markup;
  866. } else {
  867. ?>
  868. <ul class="menu" id="menu-to-edit"></ul>
  869. <?php } ?>
  870. <?php endif; ?>
  871. <?php if ( $add_new_screen ) : ?>
  872. <p class="post-body-plain" id="menu-name-desc"><?php _e( 'Give your menu a name, then click Create Menu.' ); ?></p>
  873. <?php if ( isset( $_GET['use-location'] ) ) : ?>
  874. <input type="hidden" name="use-location" value="<?php echo esc_attr( $_GET['use-location'] ); ?>" />
  875. <?php endif; ?>
  876. <?php
  877. endif;
  878. $no_menus_style = '';
  879. if ( $one_theme_location_no_menus ) {
  880. $no_menus_style = 'style="display: none;"';
  881. }
  882. ?>
  883. <?php if ( ! $add_new_screen ) : ?>
  884. <div id="nav-menu-bulk-actions-bottom" class="bulk-actions" <?php echo $hide_style; ?>>
  885. <label class="bulk-select-button" for="bulk-select-switcher-bottom">
  886. <input type="checkbox" id="bulk-select-switcher-bottom" name="bulk-select-switcher-top" class="bulk-select-switcher">
  887. <span class="bulk-select-button-label"><?php _e( 'Bulk Select' ); ?></span>
  888. </label>
  889. <input type="button" class="deletion menu-items-delete disabled" value="<?php _e( 'Remove Selected Items' ); ?>">
  890. <div id="pending-menu-items-to-delete">
  891. <p><?php _e( 'List of menu items selected for deletion:' ); ?></p>
  892. <ul></ul>
  893. </div>
  894. </div>
  895. <?php endif; ?>
  896. <div class="menu-settings" <?php echo $no_menus_style; ?>>
  897. <h3><?php _e( 'Menu Settings' ); ?></h3>
  898. <?php
  899. if ( ! isset( $auto_add ) ) {
  900. $auto_add = get_option( 'nav_menu_options' );
  901. if ( ! isset( $auto_add['auto_add'] ) ) {
  902. $auto_add = false;
  903. } elseif ( false !== array_search( $nav_menu_selected_id, $auto_add['auto_add'], true ) ) {
  904. $auto_add = true;
  905. } else {
  906. $auto_add = false;
  907. }
  908. }
  909. ?>
  910. <fieldset class="menu-settings-group auto-add-pages">
  911. <legend class="menu-settings-group-name howto"><?php _e( 'Auto add pages' ); ?></legend>
  912. <div class="menu-settings-input checkbox-input">
  913. <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>
  914. </div>
  915. </fieldset>
  916. <?php if ( current_theme_supports( 'menus' ) ) : ?>
  917. <fieldset class="menu-settings-group menu-theme-locations">
  918. <legend class="menu-settings-group-name howto"><?php _e( 'Display location' ); ?></legend>
  919. <?php
  920. foreach ( $locations as $location => $description ) :
  921. $checked = false;
  922. if ( isset( $menu_locations[ $location ] )
  923. && 0 !== $nav_menu_selected_id
  924. && $menu_locations[ $location ] === $nav_menu_selected_id
  925. ) {
  926. $checked = true;
  927. }
  928. ?>
  929. <div class="menu-settings-input checkbox-input">
  930. <input type="checkbox"<?php checked( $checked ); ?> name="menu-locations[<?php echo esc_attr( $location ); ?>]" id="locations-<?php echo esc_attr( $location ); ?>" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
  931. <label for="locations-<?php echo esc_attr( $location ); ?>"><?php echo $description; ?></label>
  932. <?php if ( ! empty( $menu_locations[ $location ] ) && $menu_locations[ $location ] !== $nav_menu_selected_id ) : ?>
  933. <span class="theme-location-set">
  934. <?php
  935. printf(
  936. /* translators: %s: Menu name. */
  937. _x( '(Currently set to: %s)', 'menu location' ),
  938. wp_get_nav_menu_object( $menu_locations[ $location ] )->name
  939. );
  940. ?>
  941. </span>
  942. <?php endif; ?>
  943. </div>
  944. <?php endforeach; ?>
  945. </fieldset>
  946. <?php endif; ?>
  947. </div>
  948. </div><!-- /#post-body-content -->
  949. </div><!-- /#post-body -->
  950. <div id="nav-menu-footer">
  951. <div class="major-publishing-actions wp-clearfix">
  952. <?php if ( $menu_count > 0 ) : ?>
  953. <?php if ( $add_new_screen ) : ?>
  954. <span class="cancel-action">
  955. <a class="submitcancel cancellation menu-cancel" href="<?php echo esc_url( admin_url( 'nav-menus.php' ) ); ?>"><?php _e( 'Cancel' ); ?></a>
  956. </span><!-- END .cancel-action -->
  957. <?php else : ?>
  958. <span class="delete-action">
  959. <a class="submitdelete deletion menu-delete" href="
  960. <?php
  961. echo esc_url(
  962. wp_nonce_url(
  963. add_query_arg(
  964. array(
  965. 'action' => 'delete',
  966. 'menu' => $nav_menu_selected_id,
  967. ),
  968. admin_url( 'nav-menus.php' )
  969. ),
  970. 'delete-nav_menu-' . $nav_menu_selected_id
  971. )
  972. );
  973. ?>
  974. "><?php _e( 'Delete Menu' ); ?></a>
  975. </span><!-- END .delete-action -->
  976. <?php endif; ?>
  977. <?php endif; ?>
  978. <div class="publishing-action">
  979. <?php submit_button( empty( $nav_menu_selected_id ) ? __( 'Create Menu' ) : __( 'Save Menu' ), 'primary large menu-save', 'save_menu', false, array( 'id' => 'save_menu_footer' ) ); ?>
  980. </div><!-- END .publishing-action -->
  981. </div><!-- END .major-publishing-actions -->
  982. </div><!-- /#nav-menu-footer -->
  983. </div><!-- /.menu-edit -->
  984. </form><!-- /#update-nav-menu -->
  985. </div><!-- /#menu-management -->
  986. </div><!-- /#menu-management-liquid -->
  987. </div><!-- /#nav-menus-frame -->
  988. <?php endif; ?>
  989. </div><!-- /.wrap-->
  990. <?php require_once ABSPATH . 'wp-admin/admin-footer.php'; ?>