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