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