PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/nav-menus.php

https://github.com/dipakdotyadav/WordPress
PHP | 637 lines | 480 code | 90 blank | 67 comment | 125 complexity | d4dfdc37a6461214002cac784ddbdc24 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  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( './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( __( 'Cheatin&#8217; uh?' ) );
  20. wp_enqueue_script( 'nav-menu' );
  21. wp_enqueue_script( 'accordion' );
  22. if ( wp_is_mobile() )
  23. wp_enqueue_script( 'jquery-touch-punch' );
  24. // Container for any messages displayed to the user
  25. $messages = array();
  26. // Container that stores the name of the active menu
  27. $nav_menu_selected_title = '';
  28. // The menu id of the current menu being edited
  29. $nav_menu_selected_id = isset( $_REQUEST['menu'] ) ? (int) $_REQUEST['menu'] : 0;
  30. // Allowed actions: add, update, delete
  31. $action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : 'edit';
  32. switch ( $action ) {
  33. case 'add-menu-item':
  34. check_admin_referer( 'add-menu_item', 'menu-settings-column-nonce' );
  35. if ( isset( $_REQUEST['nav-menu-locations'] ) )
  36. set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_REQUEST['menu-locations'] ) );
  37. elseif ( isset( $_REQUEST['menu-item'] ) )
  38. wp_save_nav_menu_items( $nav_menu_selected_id, $_REQUEST['menu-item'] );
  39. break;
  40. case 'move-down-menu-item' :
  41. // moving down a menu item is the same as moving up the next in order
  42. check_admin_referer( 'move-menu_item' );
  43. $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0;
  44. if ( is_nav_menu_item( $menu_item_id ) ) {
  45. $menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );
  46. if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
  47. $menu_id = (int) $menus[0];
  48. $ordered_menu_items = wp_get_nav_menu_items( $menu_id );
  49. $menu_item_data = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) );
  50. // set up the data we need in one pass through the array of menu items
  51. $dbids_to_orders = array();
  52. $orders_to_dbids = array();
  53. foreach( (array) $ordered_menu_items as $ordered_menu_item_object ) {
  54. if ( isset( $ordered_menu_item_object->ID ) ) {
  55. if ( isset( $ordered_menu_item_object->menu_order ) ) {
  56. $dbids_to_orders[$ordered_menu_item_object->ID] = $ordered_menu_item_object->menu_order;
  57. $orders_to_dbids[$ordered_menu_item_object->menu_order] = $ordered_menu_item_object->ID;
  58. }
  59. }
  60. }
  61. // get next in order
  62. if (
  63. isset( $orders_to_dbids[$dbids_to_orders[$menu_item_id] + 1] )
  64. ) {
  65. $next_item_id = $orders_to_dbids[$dbids_to_orders[$menu_item_id] + 1];
  66. $next_item_data = (array) wp_setup_nav_menu_item( get_post( $next_item_id ) );
  67. // if not siblings of same parent, bubble menu item up but keep order
  68. if (
  69. ! empty( $menu_item_data['menu_item_parent'] ) &&
  70. (
  71. empty( $next_item_data['menu_item_parent'] ) ||
  72. $next_item_data['menu_item_parent'] != $menu_item_data['menu_item_parent']
  73. )
  74. ) {
  75. $parent_db_id = in_array( $menu_item_data['menu_item_parent'], $orders_to_dbids ) ? (int) $menu_item_data['menu_item_parent'] : 0;
  76. $parent_object = wp_setup_nav_menu_item( get_post( $parent_db_id ) );
  77. if ( ! is_wp_error( $parent_object ) ) {
  78. $parent_data = (array) $parent_object;
  79. $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
  80. update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
  81. }
  82. // make menu item a child of its next sibling
  83. } else {
  84. $next_item_data['menu_order'] = $next_item_data['menu_order'] - 1;
  85. $menu_item_data['menu_order'] = $menu_item_data['menu_order'] + 1;
  86. $menu_item_data['menu_item_parent'] = $next_item_data['ID'];
  87. update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
  88. wp_update_post($menu_item_data);
  89. wp_update_post($next_item_data);
  90. }
  91. // the item is last but still has a parent, so bubble up
  92. } elseif (
  93. ! empty( $menu_item_data['menu_item_parent'] ) &&
  94. in_array( $menu_item_data['menu_item_parent'], $orders_to_dbids )
  95. ) {
  96. $menu_item_data['menu_item_parent'] = (int) get_post_meta( $menu_item_data['menu_item_parent'], '_menu_item_menu_item_parent', true);
  97. update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
  98. }
  99. }
  100. }
  101. break;
  102. case 'move-up-menu-item' :
  103. check_admin_referer( 'move-menu_item' );
  104. $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0;
  105. if ( is_nav_menu_item( $menu_item_id ) ) {
  106. $menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );
  107. if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
  108. $menu_id = (int) $menus[0];
  109. $ordered_menu_items = wp_get_nav_menu_items( $menu_id );
  110. $menu_item_data = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) );
  111. // set up the data we need in one pass through the array of menu items
  112. $dbids_to_orders = array();
  113. $orders_to_dbids = array();
  114. foreach( (array) $ordered_menu_items as $ordered_menu_item_object ) {
  115. if ( isset( $ordered_menu_item_object->ID ) ) {
  116. if ( isset( $ordered_menu_item_object->menu_order ) ) {
  117. $dbids_to_orders[$ordered_menu_item_object->ID] = $ordered_menu_item_object->menu_order;
  118. $orders_to_dbids[$ordered_menu_item_object->menu_order] = $ordered_menu_item_object->ID;
  119. }
  120. }
  121. }
  122. // if this menu item is not first
  123. if ( ! empty( $dbids_to_orders[$menu_item_id] ) && ! empty( $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] ) ) {
  124. // if this menu item is a child of the previous
  125. if (
  126. ! empty( $menu_item_data['menu_item_parent'] ) &&
  127. in_array( $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ) ) &&
  128. isset( $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] ) &&
  129. ( $menu_item_data['menu_item_parent'] == $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] )
  130. ) {
  131. $parent_db_id = in_array( $menu_item_data['menu_item_parent'], $orders_to_dbids ) ? (int) $menu_item_data['menu_item_parent'] : 0;
  132. $parent_object = wp_setup_nav_menu_item( get_post( $parent_db_id ) );
  133. if ( ! is_wp_error( $parent_object ) ) {
  134. $parent_data = (array) $parent_object;
  135. // if there is something before the parent and parent a child of it, make menu item a child also of it
  136. if (
  137. ! empty( $dbids_to_orders[$parent_db_id] ) &&
  138. ! empty( $orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1] ) &&
  139. ! empty( $parent_data['menu_item_parent'] )
  140. ) {
  141. $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
  142. // else if there is something before parent and parent not a child of it, make menu item a child of that something's parent
  143. } elseif (
  144. ! empty( $dbids_to_orders[$parent_db_id] ) &&
  145. ! empty( $orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1] )
  146. ) {
  147. $_possible_parent_id = (int) get_post_meta( $orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1], '_menu_item_menu_item_parent', true);
  148. if ( in_array( $_possible_parent_id, array_keys( $dbids_to_orders ) ) )
  149. $menu_item_data['menu_item_parent'] = $_possible_parent_id;
  150. else
  151. $menu_item_data['menu_item_parent'] = 0;
  152. // else there isn't something before the parent
  153. } else {
  154. $menu_item_data['menu_item_parent'] = 0;
  155. }
  156. // set former parent's [menu_order] to that of menu-item's
  157. $parent_data['menu_order'] = $parent_data['menu_order'] + 1;
  158. // set menu-item's [menu_order] to that of former parent
  159. $menu_item_data['menu_order'] = $menu_item_data['menu_order'] - 1;
  160. // save changes
  161. update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
  162. wp_update_post($menu_item_data);
  163. wp_update_post($parent_data);
  164. }
  165. // else this menu item is not a child of the previous
  166. } elseif (
  167. empty( $menu_item_data['menu_order'] ) ||
  168. empty( $menu_item_data['menu_item_parent'] ) ||
  169. ! in_array( $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ) ) ||
  170. empty( $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] ) ||
  171. $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] != $menu_item_data['menu_item_parent']
  172. ) {
  173. // just make it a child of the previous; keep the order
  174. $menu_item_data['menu_item_parent'] = (int) $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1];
  175. update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
  176. wp_update_post($menu_item_data);
  177. }
  178. }
  179. }
  180. }
  181. break;
  182. case 'delete-menu-item':
  183. $menu_item_id = (int) $_REQUEST['menu-item'];
  184. check_admin_referer( 'delete-menu_item_' . $menu_item_id );
  185. if ( is_nav_menu_item( $menu_item_id ) && wp_delete_post( $menu_item_id, true ) )
  186. $messages[] = '<div id="message" class="updated"><p>' . __('The menu item has been successfully deleted.') . '</p></div>';
  187. break;
  188. case 'delete':
  189. check_admin_referer( 'delete-nav_menu-' . $nav_menu_selected_id );
  190. if ( is_nav_menu( $nav_menu_selected_id ) ) {
  191. $deletion = _wp_delete_nav_menu( $nav_menu_selected_id );
  192. } else {
  193. // Reset the selected menu
  194. $nav_menu_selected_id = 0;
  195. unset( $_REQUEST['menu'] );
  196. }
  197. if ( ! isset( $deletion ) )
  198. break;
  199. if ( is_wp_error( $deletion ) )
  200. $messages[] = '<div id="message" class="error"><p>' . $deletion->get_error_message() . '</p></div>';
  201. else
  202. $messages[] = '<div id="message" class="updated"><p>' . __( 'The menu has been successfully deleted.' ) . '</p></div>';
  203. break;
  204. case 'delete_menus':
  205. check_admin_referer( 'nav_menus_bulk_actions' );
  206. foreach ( $_REQUEST['delete_menus'] as $menu_id_to_delete ) {
  207. if ( ! is_nav_menu( $menu_id_to_delete ) )
  208. continue;
  209. $deletion = _wp_delete_nav_menu( $menu_id_to_delete );
  210. if ( is_wp_error( $deletion ) ) {
  211. $messages[] = '<div id="message" class="error"><p>' . $deletion->get_error_message() . '</p></div>';
  212. $deletion_error = true;
  213. }
  214. }
  215. if ( empty( $deletion_error ) )
  216. $messages[] = '<div id="message" class="updated"><p>' . __( 'Selected menus have been successfully deleted.' ) . '</p></div>';
  217. break;
  218. case 'update':
  219. check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
  220. // Get existing menu locations assignments
  221. $locations = get_registered_nav_menus();
  222. $menu_locations = get_nav_menu_locations();
  223. // Remove menu locations that have been unchecked
  224. foreach ( $locations as $location => $description ) {
  225. if ( ( empty( $_POST['menu-locations'] ) || empty( $_POST['menu-locations'][ $location ] ) ) && isset( $menu_locations[ $location ] ) && $menu_locations[ $location ] == $nav_menu_selected_id )
  226. unset( $menu_locations[ $location ] );
  227. }
  228. // Merge new and existing menu locations if any new ones are set
  229. if ( isset( $_POST['menu-locations'] ) ) {
  230. $new_menu_locations = array_map( 'absint', $_POST['menu-locations'] );
  231. $menu_locations = array_merge( $menu_locations, $new_menu_locations );
  232. }
  233. // Set menu locations
  234. set_theme_mod( 'nav_menu_locations', $menu_locations );
  235. // Add Menu
  236. if ( 0 == $nav_menu_selected_id ) {
  237. $new_menu_title = trim( esc_html( $_POST['menu-name'] ) );
  238. if ( $new_menu_title ) {
  239. $_nav_menu_selected_id = wp_update_nav_menu_object( 0, array('menu-name' => $new_menu_title) );
  240. if ( is_wp_error( $_nav_menu_selected_id ) ) {
  241. $messages[] = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
  242. } else {
  243. $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
  244. $nav_menu_selected_id = $_nav_menu_selected_id;
  245. $nav_menu_selected_title = $_menu_object->name;
  246. if ( isset( $_REQUEST['menu-item'] ) )
  247. wp_save_nav_menu_items( $nav_menu_selected_id, absint( $_REQUEST['menu-item'] ) );
  248. if ( isset( $_REQUEST['zero-menu-state'] ) ) {
  249. // If there are menu items, add them
  250. wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selected_title );
  251. // Auto-save nav_menu_locations
  252. $locations = get_theme_mod( 'nav_menu_locations' );
  253. foreach ( (array) $locations as $location => $menu_id ) {
  254. $locations[ $location ] = $nav_menu_selected_id;
  255. break; // There should only be 1
  256. }
  257. set_theme_mod( 'nav_menu_locations', $locations );
  258. }
  259. $messages[] = '<div id="message" class="updated"><p>' . sprintf( __( '<strong>%s</strong> has been created.' ), $nav_menu_selected_title ) . '</p></div>';
  260. }
  261. } else {
  262. $messages[] = '<div id="message" class="error"><p>' . __( 'Please enter a valid menu name.' ) . '</p></div>';
  263. }
  264. // Update existing menu
  265. } else {
  266. $_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
  267. $menu_title = trim( esc_html( $_POST['menu-name'] ) );
  268. if ( ! $menu_title ) {
  269. $messages[] = '<div id="message" class="error"><p>' . __( 'Please enter a valid menu name.' ) . '</p></div>';
  270. $menu_title = $_menu_object->name;
  271. }
  272. if ( ! is_wp_error( $_menu_object ) ) {
  273. $_nav_menu_selected_id = wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $menu_title ) );
  274. if ( is_wp_error( $_nav_menu_selected_id ) ) {
  275. $_menu_object = $_nav_menu_selected_id;
  276. $messages[] = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
  277. } else {
  278. $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
  279. $nav_menu_selected_title = $_menu_object->name;
  280. }
  281. }
  282. // Update menu items
  283. if ( ! is_wp_error( $_menu_object ) ) {
  284. $messages = array_merge( $messages, wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selected_title ) );
  285. }
  286. }
  287. break;
  288. }
  289. // Get all nav menus
  290. $nav_menus = wp_get_nav_menus( array('orderby' => 'name') );
  291. $menu_count = count( $nav_menus );
  292. // Are we on the add new screen?
  293. $add_new_screen = ( isset( $_GET['menu'] ) && 0 == $_GET['menu'] ) ? true : false;
  294. // If we have one theme location, and zero menus, we take them right into editing their first menu
  295. $page_count = wp_count_posts( 'page' );
  296. $one_theme_location_no_menus = ( 1 == count( get_registered_nav_menus() ) && ! $add_new_screen && empty( $nav_menus ) && ! empty( $page_count->publish ) ) ? true : false;
  297. $l10n = array(
  298. 'oneThemeLocationNoMenus' => $one_theme_location_no_menus,
  299. 'moveUp' => __( 'Move up one' ),
  300. 'moveDown' => __( 'Move down one' ),
  301. 'moveToTop' => __( 'Move to the top' ),
  302. /* translators: %s: previous item name */
  303. 'moveUnder' => __( 'Move under %s' ),
  304. /* translators: %s: previous item name */
  305. 'moveOutFrom' => __( 'Move out from under %s' ),
  306. /* translators: %s: previous item name */
  307. 'under' => __( 'Under %s' ),
  308. /* translators: %s: previous item name */
  309. 'outFrom' => __( 'Out from under %s' ),
  310. /* translators: 1: item name, 2: item position, 3: total number of items */
  311. 'menuFocus' => __( '%1$s. Menu item %2$d of %3$d.' ),
  312. /* translators: 1: item name, 2: item position, 3: parent item name */
  313. 'subMenuFocus' => __( '%1$s. Sub item number %2$d under %3$s.' ),
  314. );
  315. wp_localize_script( 'nav-menu', 'menus', $l10n );
  316. // Redirect to add screen if there are no menus and this users has either zero, or more than 1 theme locations
  317. if ( 0 == $menu_count && ! $add_new_screen && ! $one_theme_location_no_menus )
  318. wp_redirect( admin_url( 'nav-menus.php?action=edit&menu=0' ) );
  319. // Get recently edited nav menu
  320. $recently_edited = absint( get_user_option( 'nav_menu_recently_edited' ) );
  321. if ( empty( $recently_edited ) && is_nav_menu( $nav_menu_selected_id ) )
  322. $recently_edited = $nav_menu_selected_id;
  323. // Use $recently_edited if none are selected
  324. if ( empty( $nav_menu_selected_id ) && ! isset( $_GET['menu'] ) && is_nav_menu( $recently_edited ) )
  325. $nav_menu_selected_id = $recently_edited;
  326. // On deletion of menu, if another menu exists, show it
  327. if ( ! $add_new_screen && 0 < $menu_count && isset( $_GET['action'] ) && 'delete' == $_GET['action'] )
  328. $nav_menu_selected_id = $nav_menus[0]->term_id;
  329. // Set $nav_menu_selected_id to 0 if no menus
  330. if ( $one_theme_location_no_menus ) {
  331. $nav_menu_selected_id = 0;
  332. } elseif ( empty( $nav_menu_selected_id ) && ! empty( $nav_menus ) && ! $add_new_screen ) {
  333. // if we have no selection yet, and we have menus, set to the first one in the list
  334. $nav_menu_selected_id = $nav_menus[0]->term_id;
  335. }
  336. // Update the user's setting
  337. if ( $nav_menu_selected_id != $recently_edited && is_nav_menu( $nav_menu_selected_id ) )
  338. update_user_meta( $current_user->ID, 'nav_menu_recently_edited', $nav_menu_selected_id );
  339. // If there's a menu, get its name.
  340. if ( ! $nav_menu_selected_title && is_nav_menu( $nav_menu_selected_id ) ) {
  341. $_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
  342. $nav_menu_selected_title = ! is_wp_error( $_menu_object ) ? $_menu_object->name : '';
  343. }
  344. // Generate truncated menu names
  345. foreach( (array) $nav_menus as $key => $_nav_menu ) {
  346. $_nav_menu->truncated_name = trim( wp_html_excerpt( $_nav_menu->name, 40 ) );
  347. if ( $_nav_menu->truncated_name != $_nav_menu->name )
  348. $_nav_menu->truncated_name .= '&hellip;';
  349. $nav_menus[$key]->truncated_name = $_nav_menu->truncated_name;
  350. }
  351. // Retrieve menu locations
  352. if ( current_theme_supports( 'menus' ) ) {
  353. $locations = get_registered_nav_menus();
  354. $menu_locations = get_nav_menu_locations();
  355. }
  356. // Ensure the user will be able to scroll horizontally
  357. // by adding a class for the max menu depth.
  358. global $_wp_nav_menu_max_depth;
  359. $_wp_nav_menu_max_depth = 0;
  360. // Calling wp_get_nav_menu_to_edit generates $_wp_nav_menu_max_depth
  361. if ( is_nav_menu( $nav_menu_selected_id ) ) {
  362. $menu_items = wp_get_nav_menu_items( $nav_menu_selected_id, array( 'post_status' => 'any' ) );
  363. $edit_markup = wp_get_nav_menu_to_edit( $nav_menu_selected_id );
  364. }
  365. function wp_nav_menu_max_depth($classes) {
  366. global $_wp_nav_menu_max_depth;
  367. return "$classes menu-max-depth-$_wp_nav_menu_max_depth";
  368. }
  369. add_filter('admin_body_class', 'wp_nav_menu_max_depth');
  370. wp_nav_menu_setup();
  371. wp_initial_nav_menu_meta_boxes();
  372. if ( ! current_theme_supports( 'menus' ) && ! wp_get_nav_menus() )
  373. $messages[] = '<div id="message" class="updated"><p>' . __('The current theme does not natively support menus, but you can use the &#8220;Custom Menu&#8221; widget to add any menus you create here to the theme&#8217;s sidebar.') . '</p></div>';
  374. get_current_screen()->add_help_tab( array(
  375. 'id' => 'overview',
  376. 'title' => __('Overview'),
  377. 'content' =>
  378. '<p>' . __('This feature allows you to use a custom menu in place of your theme&#8217;s default menus.') . '</p>' .
  379. '<p>' . __('Custom menus may contain links to pages, categories, custom links or other content types (use the Screen Options tab to decide which ones to show on the screen). You can specify a different navigation label for a menu item as well as other attributes. You can create multiple menus. If your theme includes more than one menu location, you can choose which custom menu to associate with each. You can also use custom menus in conjunction with the Custom Menus widget.') . '</p>' .
  380. '<p>' . sprintf( __('If your theme does not support the custom menus feature (the default themes, %1$s and %2$s, do), you can learn about adding this support by following the Documentation link to the side.'), 'Twenty Thirteen', 'Twenty Twelve' ) . '</p>'
  381. ) );
  382. get_current_screen()->add_help_tab( array(
  383. 'id' => 'create-menus',
  384. 'title' => __('Create Menus'),
  385. 'content' =>
  386. '<p>' . __('To create a new custom menu, click on the + tab, give the menu a name, and click Create Menu. Next, add menu items from the appropriate boxes. You&#8217;ll be able to edit the information for each menu item, and can drag and drop to change their order. You can also drag a menu item a little to the right to make it a submenu. Don&#8217;t forget to click Save Menu when you&#8217;re finished.') . '</p>'
  387. ) );
  388. get_current_screen()->set_help_sidebar(
  389. '<p><strong>' . __('For more information:') . '</strong></p>' .
  390. '<p>' . __('<a href="http://codex.wordpress.org/Appearance_Menus_Screen" target="_blank">Documentation on Menus</a>') . '</p>' .
  391. '<p>' . __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
  392. );
  393. // Get the admin header
  394. require_once( './admin-header.php' );
  395. ?>
  396. <div class="wrap">
  397. <?php screen_icon(); ?>
  398. <h2><?php _e( 'Menus' ); ?> <a href="<?php echo esc_url( add_query_arg( array( 'action' => 'edit', 'menu' => 0, ), admin_url( 'nav-menus.php' ) ) ); ?>" class="add-new-h2"><?php _ex( 'Add New', 'menu' ); ?></a></h2>
  399. <?php
  400. foreach( $messages as $message ) :
  401. echo $message . "\n";
  402. endforeach;
  403. ?>
  404. <div class="manage-menus">
  405. <?php if ( $menu_count < 2 ) : ?>
  406. <span class="add-edit-menu-action">
  407. <?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' ) ) ) ); ?>
  408. </span><!-- /add-edit-menu-action -->
  409. <?php else : ?>
  410. <form method="post" action="<?php echo admin_url( 'nav-menus.php' ); ?>">
  411. <input type="hidden" name="action" value="edit" />
  412. <label for="menu" class="selected-menu"><?php _e( 'Select a menu to edit:' ); ?></label>
  413. <select name="menu" id="menu">
  414. <?php if ( $add_new_screen ) : ?>
  415. <option value="0" selected="selected"><?php _e( '-- Select --' ); ?></option>
  416. <?php endif; ?>
  417. <?php foreach( (array) $nav_menus as $_nav_menu ) : ?>
  418. <option value="<?php echo esc_attr( $_nav_menu->term_id ); ?>" <?php selected( $_nav_menu->term_id, $nav_menu_selected_id ); ?>>
  419. <?php
  420. echo esc_html( $_nav_menu->truncated_name ) ;
  421. if ( ! empty( $menu_locations ) && in_array( $_nav_menu->term_id, $menu_locations ) ) {
  422. $locations_assigned_to_this_menu = array();
  423. foreach ( array_keys( $menu_locations, $_nav_menu->term_id ) as $menu_location_key ) {
  424. $locations_assigned_to_this_menu[] = $locations[ $menu_location_key ];
  425. }
  426. $assigned_locations = array_slice( $locations_assigned_to_this_menu, 0, absint( apply_filters( 'wp_nav_locations_listed_per_menu', 3 ) ) );
  427. // Adds ellipses following the number of locations defined in $assigned_locations
  428. printf( ' (%1$s%2$s)',
  429. implode( ', ', $assigned_locations ),
  430. count( $locations_assigned_to_this_menu ) > count( $assigned_locations ) ? ' &hellip;' : ''
  431. );
  432. }
  433. ?>
  434. </option>
  435. <?php endforeach; ?>
  436. </select>
  437. <span class="submit-btn"><input type="submit" class="button-secondary" value="<?php _e( 'Select' ); ?>"></span>
  438. <span class="add-new-menu-action">
  439. <?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' ) ) ) ); ?>
  440. </span><!-- /add-new-menu-action -->
  441. </form>
  442. <?php endif; ?>
  443. </div><!-- /manage-menus -->
  444. <div id="nav-menus-frame">
  445. <div id="menu-settings-column" class="metabox-holder<?php if ( isset( $_GET['menu'] ) && '0' == $_GET['menu'] ) { echo ' metabox-holder-disabled'; } ?>">
  446. <div class="clear"></div>
  447. <form id="nav-menu-meta" action="<?php echo admin_url( 'nav-menus.php' ); ?>" class="nav-menu-meta" method="post" enctype="multipart/form-data">
  448. <input type="hidden" name="menu" id="nav-menu-meta-object-id" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
  449. <input type="hidden" name="action" value="add-menu-item" />
  450. <?php wp_nonce_field( 'add-menu_item', 'menu-settings-column-nonce' ); ?>
  451. <?php do_accordion_sections( 'nav-menus', 'side', null ); ?>
  452. </form>
  453. </div><!-- /#menu-settings-column -->
  454. <div id="menu-management-liquid">
  455. <div id="menu-management">
  456. <form id="update-nav-menu" action="<?php echo esc_url( admin_url( 'nav-menus.php' ) ); ?>" method="post" enctype="multipart/form-data">
  457. <div class="menu-edit <?php if ( $add_new_screen ) echo 'blank-slate'; ?>">
  458. <?php
  459. wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
  460. wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
  461. wp_nonce_field( 'update-nav_menu', 'update-nav-menu-nonce' );
  462. if ( $one_theme_location_no_menus ) { ?>
  463. <input type="hidden" name="zero-menu-state" value="true" />
  464. <?php } ?>
  465. <input type="hidden" name="action" value="update" />
  466. <input type="hidden" name="menu" id="menu" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
  467. <div id="nav-menu-header">
  468. <div class="major-publishing-actions">
  469. <label class="menu-name-label howto open-label" for="menu-name">
  470. <span><?php _e( 'Menu Name' ); ?></span>
  471. <input name="menu-name" id="menu-name" type="text" class="menu-name regular-text menu-item-textbox input-with-default-title" title="<?php esc_attr_e( 'Enter menu name here' ); ?>" value="<?php if ( $one_theme_location_no_menus ) _e( 'Menu 1' ); else echo esc_attr( $nav_menu_selected_title ); ?>" />
  472. </label>
  473. <div class="publishing-action">
  474. <?php submit_button( empty( $nav_menu_selected_id ) ? __( 'Create Menu' ) : __( 'Save Menu' ), 'button-primary menu-save', 'save_menu', false, array( 'id' => 'save_menu_header' ) ); ?>
  475. </div><!-- END .publishing-action -->
  476. </div><!-- END .major-publishing-actions -->
  477. </div><!-- END .nav-menu-header -->
  478. <div id="post-body">
  479. <div id="post-body-content">
  480. <?php if ( ! $add_new_screen ) : ?>
  481. <h3><?php _e( 'Menu Structure' ); ?></h3>
  482. <?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 an item to reveal additional configuration options.' ); ?>
  483. <div class="drag-instructions post-body-plain" <?php if ( isset( $menu_items ) && 0 == count( $menu_items ) ) { ?>style="display: none;"<?php } ?>>
  484. <p><?php echo $starter_copy; ?></p>
  485. </div>
  486. <?php
  487. if ( isset( $edit_markup ) && ! is_wp_error( $edit_markup ) ) {
  488. echo $edit_markup;
  489. } else {
  490. ?>
  491. <ul class="menu" id="menu-to-edit"></ul>
  492. <?php } ?>
  493. <?php endif; ?>
  494. <?php if ( $add_new_screen ) : ?>
  495. <p class="post-body-plain"><?php _e( 'Give your menu a name above, then click Create Menu.' ); ?></p>
  496. <?php endif; ?>
  497. <div class="menu-settings" <?php if ( $one_theme_location_no_menus ) { ?>style="display: none;"<?php } ?>>
  498. <h3><?php _e( 'Menu Settings' ); ?></h3>
  499. <?php
  500. if ( ! isset( $auto_add ) ) {
  501. $auto_add = get_option( 'nav_menu_options' );
  502. if ( ! isset( $auto_add['auto_add'] ) )
  503. $auto_add = false;
  504. elseif ( false !== array_search( $nav_menu_selected_id, $auto_add['auto_add'] ) )
  505. $auto_add = true;
  506. else
  507. $auto_add = false;
  508. } ?>
  509. <dl class="auto-add-pages">
  510. <dt class="howto"><?php _e( 'Auto add pages' ); ?></dt>
  511. <dd class="checkbox-input"><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></dd>
  512. </dl>
  513. <?php if ( current_theme_supports( 'menus' ) ) : ?>
  514. <dl class="menu-theme-locations">
  515. <dt class="howto"><?php _e( 'Theme locations' ); ?></dt>
  516. <?php foreach ( $locations as $location => $description ) : ?>
  517. <dd class="checkbox-input">
  518. <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 ); ?>" /> <label for="locations-<?php echo esc_attr( $location ); ?>"><?php echo $description; ?></label>
  519. <?php if ( ! empty( $menu_locations[ $location ] ) && $menu_locations[ $location ] != $nav_menu_selected_id ) : ?>
  520. <span class="theme-location-set"> <?php printf( __( "(Currently set to: %s)" ), wp_get_nav_menu_object( $menu_locations[ $location ] )->name ); ?> </span>
  521. <?php endif; ?>
  522. </dd>
  523. <?php endforeach; ?>
  524. </dl>
  525. <?php endif; ?>
  526. </div>
  527. </div><!-- /#post-body-content -->
  528. </div><!-- /#post-body -->
  529. <div id="nav-menu-footer">
  530. <div class="major-publishing-actions">
  531. <?php if ( 0 != $menu_count && ! $add_new_screen ) : ?>
  532. <span class="delete-action">
  533. <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() ) ), 'delete-nav_menu-' . $nav_menu_selected_id) ); ?>"><?php _e('Delete Menu'); ?></a>
  534. </span><!-- END .delete-action -->
  535. <?php endif; ?>
  536. <div class="publishing-action">
  537. <?php submit_button( empty( $nav_menu_selected_id ) ? __( 'Create Menu' ) : __( 'Save Menu' ), 'button-primary menu-save', 'save_menu', false, array( 'id' => 'save_menu_header' ) ); ?>
  538. </div><!-- END .publishing-action -->
  539. </div><!-- END .major-publishing-actions -->
  540. </div><!-- /#nav-menu-footer -->
  541. </div><!-- /.menu-edit -->
  542. </form><!-- /#update-nav-menu -->
  543. </div><!-- /#menu-management -->
  544. </div><!-- /#menu-management-liquid -->
  545. </div><!-- /#nav-menus-frame -->
  546. </div><!-- /.wrap-->
  547. <?php include( './admin-footer.php' ); ?>