WordPress 3.4
[autoinstalls/wordpress.git] / wp-admin / nav-menus.php
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
12 /** Load WordPress Administration Bootstrap */
13 require_once( './admin.php' );
14
15 // Load all the nav menu interface functions
16 require_once( ABSPATH . 'wp-admin/includes/nav-menu.php' );
17
18 if ( ! current_theme_supports( 'menus' ) && ! current_theme_supports( 'widgets' ) )
19         wp_die( __( 'Your theme does not support navigation menus or widgets.' ) );
20
21 // Permissions Check
22 if ( ! current_user_can('edit_theme_options') )
23         wp_die( __( 'Cheatin&#8217; uh?' ) );
24
25 // jQuery
26 wp_enqueue_script( 'jquery-ui-draggable' );
27 wp_enqueue_script( 'jquery-ui-droppable' );
28 wp_enqueue_script( 'jquery-ui-sortable' );
29
30 // Nav Menu functions
31 wp_enqueue_script( 'nav-menu' );
32
33 // Metaboxes
34 wp_enqueue_script( 'common' );
35 wp_enqueue_script( 'wp-lists' );
36 wp_enqueue_script( 'postbox' );
37
38 if ( wp_is_mobile() )
39         wp_enqueue_script( 'jquery-touch-punch' );
40
41 // Container for any messages displayed to the user
42 $messages = array();
43
44 // Container that stores the name of the active menu
45 $nav_menu_selected_title = '';
46
47 // The menu id of the current menu being edited
48 $nav_menu_selected_id = isset( $_REQUEST['menu'] ) ? (int) $_REQUEST['menu'] : 0;
49
50 // Allowed actions: add, update, delete
51 $action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : 'edit';
52
53 switch ( $action ) {
54         case 'add-menu-item':
55                 check_admin_referer( 'add-menu_item', 'menu-settings-column-nonce' );
56                 if ( isset( $_REQUEST['nav-menu-locations'] ) )
57                         set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_REQUEST['menu-locations'] ) );
58                 elseif ( isset( $_REQUEST['menu-item'] ) )
59                         wp_save_nav_menu_items( $nav_menu_selected_id, $_REQUEST['menu-item'] );
60                 break;
61         case 'move-down-menu-item' :
62                 // moving down a menu item is the same as moving up the next in order
63                 check_admin_referer( 'move-menu_item' );
64                 $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0;
65                 if ( is_nav_menu_item( $menu_item_id ) ) {
66                         $menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );
67                         if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
68                                 $menu_id = (int) $menus[0];
69                                 $ordered_menu_items = wp_get_nav_menu_items( $menu_id );
70                                 $menu_item_data = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) );
71
72                                 // set up the data we need in one pass through the array of menu items
73                                 $dbids_to_orders = array();
74                                 $orders_to_dbids = array();
75                                 foreach( (array) $ordered_menu_items as $ordered_menu_item_object ) {
76                                         if ( isset( $ordered_menu_item_object->ID ) ) {
77                                                 if ( isset( $ordered_menu_item_object->menu_order ) ) {
78                                                         $dbids_to_orders[$ordered_menu_item_object->ID] = $ordered_menu_item_object->menu_order;
79                                                         $orders_to_dbids[$ordered_menu_item_object->menu_order] = $ordered_menu_item_object->ID;
80                                                 }
81                                         }
82                                 }
83
84                                 // get next in order
85                                 if (
86                                         isset( $orders_to_dbids[$dbids_to_orders[$menu_item_id] + 1] )
87                                 ) {
88                                         $next_item_id = $orders_to_dbids[$dbids_to_orders[$menu_item_id] + 1];
89                                         $next_item_data = (array) wp_setup_nav_menu_item( get_post( $next_item_id ) );
90
91                                         // if not siblings of same parent, bubble menu item up but keep order
92                                         if (
93                                                 ! empty( $menu_item_data['menu_item_parent'] ) &&
94                                                 (
95                                                         empty( $next_item_data['menu_item_parent'] ) ||
96                                                         $next_item_data['menu_item_parent'] != $menu_item_data['menu_item_parent']
97                                                 )
98                                         ) {
99
100                                                 $parent_db_id = in_array( $menu_item_data['menu_item_parent'], $orders_to_dbids ) ? (int) $menu_item_data['menu_item_parent'] : 0;
101
102                                                 $parent_object = wp_setup_nav_menu_item( get_post( $parent_db_id ) );
103
104                                                 if ( ! is_wp_error( $parent_object ) ) {
105                                                         $parent_data = (array) $parent_object;
106                                                         $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
107                                                         update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
108
109                                                 }
110
111                                         // make menu item a child of its next sibling
112                                         } else {
113                                                 $next_item_data['menu_order'] = $next_item_data['menu_order'] - 1;
114                                                 $menu_item_data['menu_order'] = $menu_item_data['menu_order'] + 1;
115
116                                                 $menu_item_data['menu_item_parent'] = $next_item_data['ID'];
117                                                 update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
118
119                                                 wp_update_post($menu_item_data);
120                                                 wp_update_post($next_item_data);
121                                         }
122
123                                 // the item is last but still has a parent, so bubble up
124                                 } elseif (
125                                         ! empty( $menu_item_data['menu_item_parent'] ) &&
126                                         in_array( $menu_item_data['menu_item_parent'], $orders_to_dbids )
127                                 ) {
128                                         $menu_item_data['menu_item_parent'] = (int) get_post_meta( $menu_item_data['menu_item_parent'], '_menu_item_menu_item_parent', true);
129                                         update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
130                                 }
131                         }
132                 }
133
134                 break;
135         case 'move-up-menu-item' :
136                 check_admin_referer( 'move-menu_item' );
137                 $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0;
138                 if ( is_nav_menu_item( $menu_item_id ) ) {
139                         $menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );
140                         if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
141                                 $menu_id = (int) $menus[0];
142                                 $ordered_menu_items = wp_get_nav_menu_items( $menu_id );
143                                 $menu_item_data = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) );
144
145                                 // set up the data we need in one pass through the array of menu items
146                                 $dbids_to_orders = array();
147                                 $orders_to_dbids = array();
148                                 foreach( (array) $ordered_menu_items as $ordered_menu_item_object ) {
149                                         if ( isset( $ordered_menu_item_object->ID ) ) {
150                                                 if ( isset( $ordered_menu_item_object->menu_order ) ) {
151                                                         $dbids_to_orders[$ordered_menu_item_object->ID] = $ordered_menu_item_object->menu_order;
152                                                         $orders_to_dbids[$ordered_menu_item_object->menu_order] = $ordered_menu_item_object->ID;
153                                                 }
154                                         }
155                                 }
156
157                                 // if this menu item is not first
158                                 if ( ! empty( $dbids_to_orders[$menu_item_id] ) && ! empty( $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] ) ) {
159
160                                         // if this menu item is a child of the previous
161                                         if (
162                                                 ! empty( $menu_item_data['menu_item_parent'] ) &&
163                                                 in_array( $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ) ) &&
164                                                 isset( $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] ) &&
165                                                 ( $menu_item_data['menu_item_parent'] == $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] )
166                                         ) {
167                                                 $parent_db_id = in_array( $menu_item_data['menu_item_parent'], $orders_to_dbids ) ? (int) $menu_item_data['menu_item_parent'] : 0;
168                                                 $parent_object = wp_setup_nav_menu_item( get_post( $parent_db_id ) );
169
170                                                 if ( ! is_wp_error( $parent_object ) ) {
171                                                         $parent_data = (array) $parent_object;
172
173                                                         // if there is something before the parent and parent a child of it, make menu item a child also of it
174                                                         if (
175                                                                 ! empty( $dbids_to_orders[$parent_db_id] ) &&
176                                                                 ! empty( $orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1] ) &&
177                                                                 ! empty( $parent_data['menu_item_parent'] )
178                                                         ) {
179                                                                 $menu_item_data['menu_item_parent'] = $parent_data['menu_item_parent'];
180
181                                                         // else if there is something before parent and parent not a child of it, make menu item a child of that something's parent
182                                                         } elseif (
183                                                                 ! empty( $dbids_to_orders[$parent_db_id] ) &&
184                                                                 ! empty( $orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1] )
185                                                         ) {
186                                                                 $_possible_parent_id = (int) get_post_meta( $orders_to_dbids[$dbids_to_orders[$parent_db_id] - 1], '_menu_item_menu_item_parent', true);
187                                                                 if ( in_array( $_possible_parent_id, array_keys( $dbids_to_orders ) ) )
188                                                                         $menu_item_data['menu_item_parent'] = $_possible_parent_id;
189                                                                 else
190                                                                         $menu_item_data['menu_item_parent'] = 0;
191
192                                                         // else there isn't something before the parent
193                                                         } else {
194                                                                 $menu_item_data['menu_item_parent'] = 0;
195                                                         }
196
197                                                         // set former parent's [menu_order] to that of menu-item's
198                                                         $parent_data['menu_order'] = $parent_data['menu_order'] + 1;
199
200                                                         // set menu-item's [menu_order] to that of former parent
201                                                         $menu_item_data['menu_order'] = $menu_item_data['menu_order'] - 1;
202
203                                                         // save changes
204                                                         update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
205                                                         wp_update_post($menu_item_data);
206                                                         wp_update_post($parent_data);
207                                                 }
208
209                                         // else this menu item is not a child of the previous
210                                         } elseif (
211                                                 empty( $menu_item_data['menu_order'] ) ||
212                                                 empty( $menu_item_data['menu_item_parent'] ) ||
213                                                 ! in_array( $menu_item_data['menu_item_parent'], array_keys( $dbids_to_orders ) ) ||
214                                                 empty( $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] ) ||
215                                                 $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1] != $menu_item_data['menu_item_parent']
216                                         ) {
217                                                 // just make it a child of the previous; keep the order
218                                                 $menu_item_data['menu_item_parent'] = (int) $orders_to_dbids[$dbids_to_orders[$menu_item_id] - 1];
219                                                 update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );
220                                                 wp_update_post($menu_item_data);
221                                         }
222                                 }
223                         }
224                 }
225                 break;
226
227         case 'delete-menu-item':
228                 $menu_item_id = (int) $_REQUEST['menu-item'];
229
230                 check_admin_referer( 'delete-menu_item_' . $menu_item_id );
231
232                 if ( is_nav_menu_item( $menu_item_id ) && wp_delete_post( $menu_item_id, true ) )
233                         $messages[] = '<div id="message" class="updated"><p>' . __('The menu item has been successfully deleted.') . '</p></div>';
234                 break;
235         case 'delete':
236                 check_admin_referer( 'delete-nav_menu-' . $nav_menu_selected_id );
237
238                 if ( is_nav_menu( $nav_menu_selected_id ) ) {
239                         $deleted_nav_menu = wp_get_nav_menu_object( $nav_menu_selected_id );
240                         $delete_nav_menu = wp_delete_nav_menu( $nav_menu_selected_id );
241
242                         if ( is_wp_error($delete_nav_menu) ) {
243                                 $messages[] = '<div id="message" class="error"><p>' . $delete_nav_menu->get_error_message() . '</p></div>';
244                         } else {
245                                 // Remove this menu from any locations.
246                                 $locations = get_theme_mod( 'nav_menu_locations' );
247                                 foreach ( (array) $locations as $location => $menu_id ) {
248                                         if ( $menu_id == $nav_menu_selected_id )
249                                                 $locations[ $location ] = 0;
250                                 }
251                                 set_theme_mod( 'nav_menu_locations', $locations );
252                                 $messages[] = '<div id="message" class="updated"><p>' . __('The menu has been successfully deleted.') . '</p></div>';
253                                 // Select the next available menu
254                                 $nav_menu_selected_id = 0;
255                                 $_nav_menus = wp_get_nav_menus( array('orderby' => 'name') );
256                                 foreach( $_nav_menus as $index => $_nav_menu ) {
257                                         if ( strcmp( $_nav_menu->name, $deleted_nav_menu->name ) >= 0
258                                          || $index == count( $_nav_menus ) - 1 ) {
259                                                 $nav_menu_selected_id = $_nav_menu->term_id;
260                                                 break;
261                                         }
262                                 }
263                         }
264                         unset( $delete_nav_menu, $deleted_nav_menu, $_nav_menus );
265                 } else {
266                         // Reset the selected menu
267                         $nav_menu_selected_id = 0;
268                         unset( $_REQUEST['menu'] );
269                 }
270                 break;
271
272         case 'update':
273                 check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
274
275                 // Update menu theme locations
276                 if ( isset( $_POST['menu-locations'] ) )
277                         set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_POST['menu-locations'] ) );
278
279                 // Add Menu
280                 if ( 0 == $nav_menu_selected_id ) {
281                         $new_menu_title = trim( esc_html( $_POST['menu-name'] ) );
282
283                         if ( $new_menu_title ) {
284                                 $_nav_menu_selected_id = wp_update_nav_menu_object( 0, array('menu-name' => $new_menu_title) );
285
286                                 if ( is_wp_error( $_nav_menu_selected_id ) ) {
287                                         $messages[] = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
288                                 } else {
289                                         $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
290                                         $nav_menu_selected_id = $_nav_menu_selected_id;
291                                         $nav_menu_selected_title = $_menu_object->name;
292                                         $messages[] = '<div id="message" class="updated"><p>' . sprintf( __('The <strong>%s</strong> menu has been successfully created.'), $nav_menu_selected_title ) . '</p></div>';
293                                 }
294                         } else {
295                                 $messages[] = '<div id="message" class="error"><p>' . __('Please enter a valid menu name.') . '</p></div>';
296                         }
297
298                 // update existing menu
299                 } else {
300
301                         $_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
302
303                         $menu_title = trim( esc_html( $_POST['menu-name'] ) );
304                         if ( ! $menu_title ) {
305                                 $messages[] = '<div id="message" class="error"><p>' . __('Please enter a valid menu name.') . '</p></div>';
306                                 $menu_title = $_menu_object->name;
307                         }
308
309                         if ( ! is_wp_error( $_menu_object ) ) {
310                                 $_nav_menu_selected_id = wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $menu_title ) );
311                                 if ( is_wp_error( $_nav_menu_selected_id ) ) {
312                                         $_menu_object = $_nav_menu_selected_id;
313                                         $messages[] = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
314                                 } else {
315                                         $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
316                                         $nav_menu_selected_title = $_menu_object->name;
317                                 }
318                         }
319
320                         // Update menu items
321
322                         if ( ! is_wp_error( $_menu_object ) ) {
323                                 $unsorted_menu_items = wp_get_nav_menu_items( $nav_menu_selected_id, array('orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID', 'post_status' => 'draft,publish') );
324                                 $menu_items = array();
325                                 // Index menu items by db ID
326                                 foreach( $unsorted_menu_items as $_item )
327                                         $menu_items[$_item->db_id] = $_item;
328
329                                 $post_fields = array( 'menu-item-db-id', 'menu-item-object-id', 'menu-item-object', 'menu-item-parent-id', 'menu-item-position', 'menu-item-type', 'menu-item-title', 'menu-item-url', 'menu-item-description', 'menu-item-attr-title', 'menu-item-target', 'menu-item-classes', 'menu-item-xfn' );
330                                 wp_defer_term_counting(true);
331                                 // Loop through all the menu items' POST variables
332                                 if ( ! empty( $_POST['menu-item-db-id'] ) ) {
333                                         foreach( (array) $_POST['menu-item-db-id'] as $_key => $k ) {
334
335                                                 // Menu item title can't be blank
336                                                 if ( empty( $_POST['menu-item-title'][$_key] ) )
337                                                         continue;
338
339                                                 $args = array();
340                                                 foreach ( $post_fields as $field )
341                                                         $args[$field] = isset( $_POST[$field][$_key] ) ? $_POST[$field][$_key] : '';
342
343                                                 $menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key ), $args );
344
345                                                 if ( is_wp_error( $menu_item_db_id ) )
346                                                         $messages[] = '<div id="message" class="error"><p>' . $menu_item_db_id->get_error_message() . '</p></div>';
347                                                 elseif ( isset( $menu_items[$menu_item_db_id] ) )
348                                                         unset( $menu_items[$menu_item_db_id] );
349                                         }
350                                 }
351
352                                 // Remove menu items from the menu that weren't in $_POST
353                                 if ( ! empty( $menu_items ) ) {
354                                         foreach ( array_keys( $menu_items ) as $menu_item_id ) {
355                                                 if ( is_nav_menu_item( $menu_item_id ) ) {
356                                                         wp_delete_post( $menu_item_id );
357                                                 }
358                                         }
359                                 }
360
361                                 // Store 'auto-add' pages.
362                                 $auto_add = ! empty( $_POST['auto-add-pages'] );
363                                 $nav_menu_option = (array) get_option( 'nav_menu_options' );
364                                 if ( ! isset( $nav_menu_option['auto_add'] ) )
365                                         $nav_menu_option['auto_add'] = array();
366                                 if ( $auto_add ) {
367                                         if ( ! in_array( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) )
368                                                 $nav_menu_option['auto_add'][] = $nav_menu_selected_id;
369                                 } else {
370                                         if ( false !== ( $key = array_search( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) ) )
371                                                 unset( $nav_menu_option['auto_add'][$key] );
372                                 }
373                                 // Remove nonexistent/deleted menus
374                                 $nav_menu_option['auto_add'] = array_intersect( $nav_menu_option['auto_add'], wp_get_nav_menus( array( 'fields' => 'ids' ) ) );
375                                 update_option( 'nav_menu_options', $nav_menu_option );
376
377                                 wp_defer_term_counting(false);
378
379                                 do_action( 'wp_update_nav_menu', $nav_menu_selected_id );
380
381                                 $messages[] = '<div id="message" class="updated"><p>' . sprintf( __('The <strong>%s</strong> menu has been updated.'), $nav_menu_selected_title ) . '</p></div>';
382                                 unset( $menu_items, $unsorted_menu_items );
383                         }
384                 }
385                 break;
386 }
387
388 // Get all nav menus
389 $nav_menus = wp_get_nav_menus( array('orderby' => 'name') );
390
391 // Get recently edited nav menu
392 $recently_edited = (int) get_user_option( 'nav_menu_recently_edited' );
393
394 // If there was no recently edited menu, and $nav_menu_selected_id is a nav menu, update recently edited menu.
395 if ( !$recently_edited && is_nav_menu( $nav_menu_selected_id ) ) {
396         $recently_edited = $nav_menu_selected_id;
397
398 // Else if $nav_menu_selected_id is not a menu and not requesting that we create a new menu, but $recently_edited is a menu, grab that one.
399 } elseif ( 0 == $nav_menu_selected_id && ! isset( $_REQUEST['menu'] ) && is_nav_menu( $recently_edited ) ) {
400         $nav_menu_selected_id = $recently_edited;
401
402 // Else try to grab the first menu from the menus list
403 } elseif ( 0 == $nav_menu_selected_id && ! isset( $_REQUEST['menu'] ) && ! empty($nav_menus) ) {
404         $nav_menu_selected_id = $nav_menus[0]->term_id;
405 }
406
407 // Update the user's setting
408 if ( $nav_menu_selected_id != $recently_edited && is_nav_menu( $nav_menu_selected_id ) )
409         update_user_meta( $current_user->ID, 'nav_menu_recently_edited', $nav_menu_selected_id );
410
411 // If there's a menu, get its name.
412 if ( ! $nav_menu_selected_title && is_nav_menu( $nav_menu_selected_id ) ) {
413         $_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
414         $nav_menu_selected_title = ! is_wp_error( $_menu_object ) ? $_menu_object->name : '';
415 }
416
417 // Generate truncated menu names
418 foreach( (array) $nav_menus as $key => $_nav_menu ) {
419         $_nav_menu->truncated_name = trim( wp_html_excerpt( $_nav_menu->name, 40 ) );
420         if ( $_nav_menu->truncated_name != $_nav_menu->name )
421                 $_nav_menu->truncated_name .= '&hellip;';
422
423         $nav_menus[$key]->truncated_name = $_nav_menu->truncated_name;
424 }
425
426 // Ensure the user will be able to scroll horizontally
427 // by adding a class for the max menu depth.
428 global $_wp_nav_menu_max_depth;
429 $_wp_nav_menu_max_depth = 0;
430
431 // Calling wp_get_nav_menu_to_edit generates $_wp_nav_menu_max_depth
432 if ( is_nav_menu( $nav_menu_selected_id ) )
433         $edit_markup = wp_get_nav_menu_to_edit( $nav_menu_selected_id );
434
435 function wp_nav_menu_max_depth($classes) {
436         global $_wp_nav_menu_max_depth;
437         return "$classes menu-max-depth-$_wp_nav_menu_max_depth";
438 }
439
440 add_filter('admin_body_class', 'wp_nav_menu_max_depth');
441
442 wp_nav_menu_setup();
443 wp_initial_nav_menu_meta_boxes();
444
445 if ( ! current_theme_supports( 'menus' ) && ! wp_get_nav_menus() )
446         $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>';
447
448 get_current_screen()->add_help_tab( array(
449 'id'            => 'overview',
450 'title'         => __('Overview'),
451 'content'       =>
452         '<p>' . __('This feature allows you to use a custom menu in place of your theme&#8217;s default menus.') . '</p>' .
453         '<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, you can choose which custom menu to associate with each. You can also use custom menus in conjunction with the Custom Menus widget.') . '</p>' .
454         '<p>' . __('If your theme does not support the custom menus feature yet (the default themes, Twenty Eleven and Twenty Ten, do), you can learn about adding this support by following the Documentation link to the side.') . '</p>'
455 ) );
456 get_current_screen()->add_help_tab( array(
457 'id'            => 'create-menus',
458 'title'         => __('Create Menus'),
459 'content'       =>
460         '<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 put them in order. You can also drag a menu item a little to the right to make it a submenu, to create menus with hierarchy. Drop the item into its new nested placement when the dotted rectangle target shifts over, also a little to the right. Don&#8217;t forget to click Save when you&#8217;re finished.') . '</p>'
461 ) );
462
463 get_current_screen()->set_help_sidebar(
464         '<p><strong>' . __('For more information:') . '</strong></p>' .
465         '<p>' . __('<a href="http://codex.wordpress.org/Appearance_Menus_Screen" target="_blank">Documentation on Menus</a>') . '</p>' .
466         '<p>' . __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
467 );
468
469 // Get the admin header
470 require_once( './admin-header.php' );
471 ?>
472 <div class="wrap">
473         <?php screen_icon(); ?>
474         <h2><?php esc_html_e('Menus'); ?></h2>
475         <?php
476         foreach( $messages as $message ) :
477                 echo $message . "\n";
478         endforeach;
479         ?>
480         <div id="nav-menus-frame">
481         <div id="menu-settings-column" class="metabox-holder<?php if ( !$nav_menu_selected_id ) { echo ' metabox-holder-disabled'; } ?>">
482
483                 <form id="nav-menu-meta" action="<?php echo admin_url( 'nav-menus.php' ); ?>" class="nav-menu-meta" method="post" enctype="multipart/form-data">
484                         <input type="hidden" name="menu" id="nav-menu-meta-object-id" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
485                         <input type="hidden" name="action" value="add-menu-item" />
486                         <?php wp_nonce_field( 'add-menu_item', 'menu-settings-column-nonce' ); ?>
487                         <?php do_meta_boxes( 'nav-menus', 'side', null ); ?>
488                 </form>
489
490         </div><!-- /#menu-settings-column -->
491         <div id="menu-management-liquid">
492                 <div id="menu-management">
493                         <div id="select-nav-menu-container" class="hide-if-js">
494                                 <form id="select-nav-menu" action="">
495                                         <strong><label for="select-nav-menu"><?php esc_html_e( 'Select Menu:' ); ?></label></strong>
496                                         <select class="select-nav-menu" name="menu">
497                                                 <?php foreach( (array) $nav_menus as $_nav_menu ) : ?>
498                                                         <option value="<?php echo esc_attr($_nav_menu->term_id) ?>" <?php selected($nav_menu_selected_id, $_nav_menu->term_id); ?>>
499                                                                 <?php echo esc_html( $_nav_menu->truncated_name ); ?>
500                                                         </option>
501                                                 <?php endforeach; ?>
502                                                 <option value="0"><?php esc_html_e('Add New Menu'); ?></option>
503                                         </select>
504                                         <input type="hidden" name="action" value="edit" />
505                                         <?php submit_button( __( 'Select' ), 'secondary', 'select_menu', false ); ?>
506                                 </form>
507                         </div>
508                         <div class="nav-tabs-wrapper">
509                         <div class="nav-tabs">
510                                 <?php
511                                 foreach( (array) $nav_menus as $_nav_menu ) :
512                                         if ( $nav_menu_selected_id == $_nav_menu->term_id ) : ?><span class="nav-tab nav-tab-active">
513                                                         <?php echo esc_html( $_nav_menu->truncated_name ); ?>
514                                                 </span><?php else : ?><a href="<?php
515                                                         echo esc_url(add_query_arg(
516                                                                 array(
517                                                                         'action' => 'edit',
518                                                                         'menu' => $_nav_menu->term_id,
519                                                                 ),
520                                                                 admin_url( 'nav-menus.php' )
521                                                         ));
522                                                 ?>" class="nav-tab hide-if-no-js">
523                                                         <?php echo esc_html( $_nav_menu->truncated_name ); ?>
524                                                 </a><?php endif;
525                                 endforeach;
526                                 if ( 0 == $nav_menu_selected_id ) : ?><span class="nav-tab menu-add-new nav-tab-active">
527                                         <?php printf( '<abbr title="%s">+</abbr>', esc_html__( 'Add menu' ) ); ?>
528                                 </span><?php else : ?><a href="<?php
529                                         echo esc_url(add_query_arg(
530                                                 array(
531                                                         'action' => 'edit',
532                                                         'menu' => 0,
533                                                 ),
534                                                 admin_url( 'nav-menus.php' )
535                                         ));
536                                 ?>" class="nav-tab menu-add-new">
537                                         <?php printf( '<abbr title="%s">+</abbr>', esc_html__( 'Add menu' ) ); ?>
538                                 </a><?php endif; ?>
539                         </div>
540                         </div>
541                         <div class="menu-edit">
542                                 <form id="update-nav-menu" action="<?php echo admin_url( 'nav-menus.php' ); ?>" method="post" enctype="multipart/form-data">
543                                         <div id="nav-menu-header">
544                                                 <div id="submitpost" class="submitbox">
545                                                         <div class="major-publishing-actions">
546                                                                 <label class="menu-name-label howto open-label" for="menu-name">
547                                                                         <span><?php _e('Menu Name'); ?></span>
548                                                                         <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 echo esc_attr( $nav_menu_selected_title ); ?>" />
549                                                                 </label>
550                                                                 <?php if ( !empty( $nav_menu_selected_id ) ) :
551                                                                         if ( ! isset( $auto_add ) ) {
552                                                                                 $auto_add = get_option( 'nav_menu_options' );
553                                                                                 if ( ! isset( $auto_add['auto_add'] ) )
554                                                                                         $auto_add = false;
555                                                                                 elseif ( false !== array_search( $nav_menu_selected_id, $auto_add['auto_add'] ) )
556                                                                                         $auto_add = true;
557                                                                                 else
558                                                                                         $auto_add = false;
559                                                                         }
560                                                                 ?>
561                                                                 <div class="auto-add-pages">
562                                                                         <label class="howto"><input type="checkbox"<?php checked( $auto_add ); ?> name="auto-add-pages" value="1" /> <?php printf( __('Automatically add new top-level pages' ), esc_url( admin_url( 'edit.php?post_type=page' ) ) ); ?></label>
563                                                                 </div>
564                                                                 <?php endif; ?>
565                                                                 <br class="clear" />
566                                                                 <div class="publishing-action">
567                                                                         <?php submit_button( empty( $nav_menu_selected_id ) ? __( 'Create Menu' ) : __( 'Save Menu' ), 'button-primary menu-save', 'save_menu', false, array( 'id' => 'save_menu_header' ) ); ?>
568                                                                 </div><!-- END .publishing-action -->
569
570                                                                 <?php if ( ! empty( $nav_menu_selected_id ) ) : ?>
571                                                                 <div class="delete-action">
572                                                                         <a class="submitdelete deletion menu-delete" href="<?php echo esc_url( wp_nonce_url( admin_url('nav-menus.php?action=delete&amp;menu=' . $nav_menu_selected_id), 'delete-nav_menu-' . $nav_menu_selected_id ) ); ?>"><?php _e('Delete Menu'); ?></a>
573                                                                 </div><!-- END .delete-action -->
574                                                                 <?php endif; ?>
575                                                         </div><!-- END .major-publishing-actions -->
576                                                 </div><!-- END #submitpost .submitbox -->
577                                                 <?php
578                                                 wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
579                                                 wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
580                                                 wp_nonce_field( 'update-nav_menu', 'update-nav-menu-nonce' );
581                                                 ?>
582                                                 <input type="hidden" name="action" value="update" />
583                                                 <input type="hidden" name="menu" id="menu" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
584                                         </div><!-- END #nav-menu-header -->
585                                         <div id="post-body">
586                                                 <div id="post-body-content">
587                                                         <?php
588                                                         if ( isset( $edit_markup ) ) {
589                                                                 if ( ! is_wp_error( $edit_markup ) )
590                                                                         echo $edit_markup;
591                                                         } else if ( empty( $nav_menu_selected_id ) ) {
592                                                                 echo '<div class="post-body-plain">';
593                                                                 echo '<p>' . __('To create a custom menu, give it a name above and click Create Menu. Then choose items like pages, categories or custom links from the left column to add to this menu.') . '</p>';
594                                                                 echo '<p>' . __('After you have added your items, drag and drop to put them in the order you want. You can also click each item to reveal additional configuration options.') . '</p>';
595                                                                 echo '<p>' . __('When you have finished building your custom menu, make sure you click the Save Menu button.') . '</p>';
596                                                                 echo '</div>';
597                                                         }
598                                                         ?>
599                                                 </div><!-- /#post-body-content -->
600                                         </div><!-- /#post-body -->
601                                         <div id="nav-menu-footer">
602                                                 <div class="major-publishing-actions">
603                                                 <div class="publishing-action">
604                                                         <?php
605                                                         if ( ! empty( $nav_menu_selected_id ) )
606                                                                 submit_button( __( 'Save Menu' ), 'button-primary menu-save', 'save_menu', false, array( 'id' => 'save_menu_footer' ) );
607                                                         ?>
608                                                 </div>
609                                                 </div>
610                                         </div><!-- /#nav-menu-footer -->
611                                 </form><!-- /#update-nav-menu -->
612                         </div><!-- /.menu-edit -->
613                 </div><!-- /#menu-management -->
614         </div><!-- /#menu-management-liquid -->
615         </div><!-- /#nav-menus-frame -->
616 </div><!-- /.wrap-->
617
618 <?php include( './admin-footer.php' ); ?>