Wordpress 3.6
[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_theme_mod( 'nav_menu_locations' );
304                                                 foreach ( (array) $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_theme_mod( 'nav_menu_locations' );
312                                                 if ( isset( $locations[$_REQUEST['use-location']] ) )
313                                                         $locations[$_REQUEST['use-location']] = $nav_menu_selected_id;
314                                                 set_theme_mod( 'nav_menu_locations', $locations );
315                                         }
316                                         // $messages[] = '<div id="message" class="updated"><p>' . sprintf( __( '<strong>%s</strong> has been created.' ), $nav_menu_selected_title ) . '</p></div>';
317                                         wp_redirect( admin_url( 'nav-menus.php?menu=' . $_nav_menu_selected_id ) );
318                                         exit();
319                                 }
320                         } else {
321                                 $messages[] = '<div id="message" class="error"><p>' . __( 'Please enter a valid menu name.' ) . '</p></div>';
322                         }
323
324                 // Update existing menu
325                 } else {
326
327                         $_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
328
329                         $menu_title = trim( esc_html( $_POST['menu-name'] ) );
330                         if ( ! $menu_title ) {
331                                 $messages[] = '<div id="message" class="error"><p>' . __( 'Please enter a valid menu name.' ) . '</p></div>';
332                                 $menu_title = $_menu_object->name;
333                         }
334
335                         if ( ! is_wp_error( $_menu_object ) ) {
336                                 $_nav_menu_selected_id = wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $menu_title ) );
337                                 if ( is_wp_error( $_nav_menu_selected_id ) ) {
338                                         $_menu_object = $_nav_menu_selected_id;
339                                         $messages[] = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
340                                 } else {
341                                         $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
342                                         $nav_menu_selected_title = $_menu_object->name;
343                                 }
344                         }
345
346                         // Update menu items
347                         if ( ! is_wp_error( $_menu_object ) ) {
348                                 $messages = array_merge( $messages, wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selected_title ) );
349                         }
350                 }
351                 break;
352         case 'locations':
353                 if ( ! $num_locations ) {
354                         wp_redirect( admin_url( 'nav-menus.php' ) );
355                         exit();
356                 }
357
358                 add_filter( 'screen_options_show_screen', '__return_false' );
359
360                 if ( isset( $_POST['menu-locations'] ) ) {
361                         check_admin_referer( 'save-menu-locations' );
362
363                         $new_menu_locations = array_map( 'absint', $_POST['menu-locations'] );
364                         $menu_locations = array_merge( $menu_locations, $new_menu_locations );
365                         // Set menu locations
366                         set_theme_mod( 'nav_menu_locations', $menu_locations );
367
368                         $messages[] = '<div id="message" class="updated"><p>' . __( 'Menu locations updated.' ) . '</p></div>';
369                 }
370                 break;
371 }
372
373 // Get all nav menus
374 $nav_menus = wp_get_nav_menus( array('orderby' => 'name') );
375 $menu_count = count( $nav_menus );
376
377 // Are we on the add new screen?
378 $add_new_screen = ( isset( $_GET['menu'] ) && 0 == $_GET['menu'] ) ? true : false;
379
380 $locations_screen = ( isset( $_GET['action'] ) && 'locations' == $_GET['action'] ) ? true : false;
381
382 // If we have one theme location, and zero menus, we take them right into editing their first menu
383 $page_count = wp_count_posts( 'page' );
384 $one_theme_location_no_menus = ( 1 == count( get_registered_nav_menus() ) && ! $add_new_screen && empty( $nav_menus ) && ! empty( $page_count->publish ) ) ? true : false;
385
386 $nav_menus_l10n = array(
387         'oneThemeLocationNoMenus' => $one_theme_location_no_menus,
388         'moveUp'       => __( 'Move up one' ),
389         'moveDown'     => __( 'Move down one' ),
390         'moveToTop'    => __( 'Move to the top' ),
391         /* translators: %s: previous item name */
392         'moveUnder'    => __( 'Move under %s' ),
393         /* translators: %s: previous item name */
394         'moveOutFrom'  => __( 'Move out from under %s' ),
395         /* translators: %s: previous item name */
396         'under'        => __( 'Under %s' ),
397         /* translators: %s: previous item name */
398         'outFrom'      => __( 'Out from under %s' ),
399         /* translators: 1: item name, 2: item position, 3: total number of items */
400         'menuFocus'    => __( '%1$s. Menu item %2$d of %3$d.' ),
401         /* translators: 1: item name, 2: item position, 3: parent item name */
402         'subMenuFocus' => __( '%1$s. Sub item number %2$d under %3$s.' ),
403 );
404 wp_localize_script( 'nav-menu', 'menus', $nav_menus_l10n );
405
406 // Redirect to add screen if there are no menus and this users has either zero, or more than 1 theme locations
407 if ( 0 == $menu_count && ! $add_new_screen && ! $one_theme_location_no_menus )
408         wp_redirect( admin_url( 'nav-menus.php?action=edit&menu=0' ) );
409
410 // Get recently edited nav menu
411 $recently_edited = absint( get_user_option( 'nav_menu_recently_edited' ) );
412 if ( empty( $recently_edited ) && is_nav_menu( $nav_menu_selected_id ) )
413         $recently_edited = $nav_menu_selected_id;
414
415 // Use $recently_edited if none are selected
416 if ( empty( $nav_menu_selected_id ) && ! isset( $_GET['menu'] ) && is_nav_menu( $recently_edited ) )
417         $nav_menu_selected_id = $recently_edited;
418
419 // On deletion of menu, if another menu exists, show it
420 if ( ! $add_new_screen && 0 < $menu_count && isset( $_GET['action'] ) && 'delete' == $_GET['action'] )
421         $nav_menu_selected_id = $nav_menus[0]->term_id;
422
423 // Set $nav_menu_selected_id to 0 if no menus
424 if ( $one_theme_location_no_menus ) {
425         $nav_menu_selected_id = 0;
426 } elseif ( empty( $nav_menu_selected_id ) && ! empty( $nav_menus ) && ! $add_new_screen ) {
427         // if we have no selection yet, and we have menus, set to the first one in the list
428         $nav_menu_selected_id = $nav_menus[0]->term_id;
429 }
430
431 // Update the user's setting
432 if ( $nav_menu_selected_id != $recently_edited && is_nav_menu( $nav_menu_selected_id ) )
433         update_user_meta( $current_user->ID, 'nav_menu_recently_edited', $nav_menu_selected_id );
434
435 // If there's a menu, get its name.
436 if ( ! $nav_menu_selected_title && is_nav_menu( $nav_menu_selected_id ) ) {
437         $_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
438         $nav_menu_selected_title = ! is_wp_error( $_menu_object ) ? $_menu_object->name : '';
439 }
440
441 // Generate truncated menu names
442 foreach( (array) $nav_menus as $key => $_nav_menu ) {
443         $nav_menus[$key]->truncated_name = wp_html_excerpt( $_nav_menu->name, 40, '&hellip;' );
444 }
445
446 // Retrieve menu locations
447 if ( current_theme_supports( 'menus' ) ) {
448         $locations = get_registered_nav_menus();
449         $menu_locations = get_nav_menu_locations();
450 }
451
452 // Ensure the user will be able to scroll horizontally
453 // by adding a class for the max menu depth.
454 global $_wp_nav_menu_max_depth;
455 $_wp_nav_menu_max_depth = 0;
456
457 // Calling wp_get_nav_menu_to_edit generates $_wp_nav_menu_max_depth
458 if ( is_nav_menu( $nav_menu_selected_id ) ) {
459         $menu_items = wp_get_nav_menu_items( $nav_menu_selected_id, array( 'post_status' => 'any' ) );
460         $edit_markup = wp_get_nav_menu_to_edit( $nav_menu_selected_id );
461 }
462
463 function wp_nav_menu_max_depth($classes) {
464         global $_wp_nav_menu_max_depth;
465         return "$classes menu-max-depth-$_wp_nav_menu_max_depth";
466 }
467
468 add_filter('admin_body_class', 'wp_nav_menu_max_depth');
469
470 wp_nav_menu_setup();
471 wp_initial_nav_menu_meta_boxes();
472
473 if ( ! current_theme_supports( 'menus' ) && ! $num_locations )
474         $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>';
475
476 if ( ! $locations_screen ) : // Main tab
477         $overview  = '<p>' . __( 'This screen is used for managing your custom navigation menus.' ) . '</p>';
478         $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>';
479         $overview .= '<p>' . __( 'From this screen you can:' ) . '</p>';
480         $overview .= '<ul><li>' . __( 'Create, edit, and delete menus' ) . '</li>';
481         $overview .= '<li>' . __( 'Add, organize, and modify individual menu items' ) . '</li></ul>';
482
483         get_current_screen()->add_help_tab( array(
484                 'id'      => 'overview',
485                 'title'   => __( 'Overview' ),
486                 'content' => $overview
487         ) );
488
489         $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>';
490         $menu_management .= '<ul><li>' . __( 'To edit an existing menu, <strong>choose a menu from the drop down and click Select</strong>' ) . '</li>';
491         $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>';
492         $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>';
493
494         get_current_screen()->add_help_tab( array(
495                 'id'      => 'menu-management',
496                 'title'   => __( 'Menu Management' ),
497                 'content' => $menu_management
498         ) );
499
500         $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>';
501         $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>';
502         $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>';
503         $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>';
504         $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>';
505         $editing_menus .= '<li>' . __( 'Delete a menu item by <strong>expanding it and clicking the Remove link</strong>' ) . '</li></ul>';
506
507         get_current_screen()->add_help_tab( array(
508                 'id'      => 'editing-menus',
509                 'title'   => __( 'Editing Menus' ),
510                 'content' => $editing_menus
511         ) );
512 else : // Locations Tab
513         $locations_overview  = '<p>' . __( 'This screen is used for globally assigning menus to locations defined by your theme.' ) . '</p>';
514         $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>';
515         $locations_overview .= '<li>' . __( 'To edit a menu currently assigned to a theme location, <strong>click the adjacent &#8217;Edit&#8217; link</strong>' ) . '</li>';
516         $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>';
517
518         get_current_screen()->add_help_tab( array(
519                 'id'      => 'locations-overview',
520                 'title'   => __( 'Overview' ),
521                 'content' => $locations_overview
522         ) );
523 endif;
524
525 get_current_screen()->set_help_sidebar(
526         '<p><strong>' . __('For more information:') . '</strong></p>' .
527         '<p>' . __('<a href="http://codex.wordpress.org/Appearance_Menus_Screen" target="_blank">Documentation on Menus</a>') . '</p>' .
528         '<p>' . __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
529 );
530
531 // Get the admin header
532 require_once( './admin-header.php' );
533 ?>
534 <div class="wrap">
535         <?php screen_icon(); ?>
536         <h2 class="nav-tab-wrapper">
537                 <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>
538                 <?php if ( $num_locations && $menu_count ) : ?>
539                         <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>
540                 <?php endif; ?>
541         </h2>
542         <?php
543         foreach( $messages as $message ) :
544                 echo $message . "\n";
545         endforeach;
546         ?>
547         <?php
548         if ( $locations_screen ) :
549                 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>';
550         ?>
551         <div id="menu-locations-wrap">
552                 <form method="post" action="<?php echo esc_url( add_query_arg( array( 'action' => 'locations' ), admin_url( 'nav-menus.php' ) ) ); ?>">
553                         <table class="widefat fixed" cellspacing="0" id="menu-locations-table">
554                                 <thead>
555                                 <tr>
556                                         <th scope="col" class="manage-column column-locations"><?php _e( 'Theme Location' ); ?></th>
557                                         <th scope="col" class="manage-column column-menus"><?php _e( 'Assigned Menu' ); ?></th>
558                                 </tr>
559                                 </thead>
560                                 <!--<tfoot>
561                                 <tr>
562                                         <th scope="col" class="manage-column column-locations"><?php _e( 'Theme Location' ); ?></th>
563                                         <th scope="col" class="manage-column column-menus"><?php _e( 'Assigned Menu' ); ?></th>
564                                 </tr>
565                                 </tfoot>-->
566                                 <tbody class="menu-locations">
567                                 <?php foreach ( $locations as $_location => $_name ) { ?>
568                                         <tr id="menu-locations-row">
569                                                 <td class="menu-location-title"><strong><?php echo $_name; ?></strong></td>
570                                                 <td class="menu-location-menus">
571                                                         <select name="menu-locations[<?php echo $_location; ?>]" id="locations-<?php echo $_location; ?>">
572                                                                 <option value="0"><?php printf( '&mdash; %s &mdash;', esc_html__( 'Select a Menu' ) ); ?></option>
573                                                                 <?php foreach ( $nav_menus as $menu ) : ?>
574                                                                         <?php $selected = isset( $menu_locations[$_location] ) && $menu_locations[$_location] == $menu->term_id; ?>
575                                                                         <option <?php if ( $selected ) echo 'data-orig="true"'; ?> <?php selected( $selected ); ?> value="<?php echo $menu->term_id; ?>">
576                                                                                 <?php echo wp_html_excerpt( $menu->name, 40, '&hellip;' ); ?>
577                                                                         </option>
578                                                                 <?php endforeach; ?>
579                                                         </select>
580                                                         <div class="locations-row-links">
581                                                                 <?php if ( isset( $menu_locations[ $_location ] ) && 0 != $menu_locations[ $_location ] ) : ?>
582                                                                 <span class="locations-edit-menu-link">
583                                                                         <a href="<?php echo esc_url( add_query_arg( array( 'action' => 'edit', 'menu' => $menu_locations[$_location] ), admin_url( 'nav-menus.php' ) ) ); ?>">
584                                                                                 <?php _ex( 'Edit', 'menu' ); ?>
585                                                                         </a>
586                                                                 </span>
587                                                                 <?php endif; ?>
588                                                                 <span class="locations-add-menu-link">
589                                                                         <a href="<?php echo esc_url( add_query_arg( array( 'action' => 'edit', 'menu' => 0, 'use-location' => $_location ), admin_url( 'nav-menus.php' ) ) ); ?>">
590                                                                                 <?php _ex( 'Use new menu', 'menu' ); ?>
591                                                                         </a>
592                                                                 </span>
593                                                         </div><!-- #locations-row-links -->
594                                                 </td><!-- .menu-location-menus -->
595                                         </tr><!-- #menu-locations-row -->
596                                 <?php } // foreach ?>
597                                 </tbody>
598                         </table>
599                         <p class="button-controls"><?php submit_button( __( 'Save Changes' ), 'primary left', 'nav-menu-locations', false ); ?></p>
600                         <?php wp_nonce_field( 'save-menu-locations' ); ?>
601                         <input type="hidden" name="menu" id="nav-menu-meta-object-id" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
602                 </form>
603         </div><!-- #menu-locations-wrap -->
604         <?php do_action( 'after_menu_locations_table' ); ?>
605         <?php else : ?>
606         <div class="manage-menus">
607                 <?php if ( $menu_count < 2 ) : ?>
608                 <span class="add-edit-menu-action">
609                         <?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' ) ) ) ); ?>
610                 </span><!-- /add-edit-menu-action -->
611                 <?php else : ?>
612                         <form method="get" action="<?php echo admin_url( 'nav-menus.php' ); ?>">
613                         <input type="hidden" name="action" value="edit" />
614                         <label for="menu" class="selected-menu"><?php _e( 'Select a menu to edit:' ); ?></label>
615                         <select name="menu" id="menu">
616                                 <?php if ( $add_new_screen ) : ?>
617                                         <option value="0" selected="selected"><?php _e( '-- Select --' ); ?></option>
618                                 <?php endif; ?>
619                                 <?php foreach( (array) $nav_menus as $_nav_menu ) : ?>
620                                         <option value="<?php echo esc_attr( $_nav_menu->term_id ); ?>" <?php selected( $_nav_menu->term_id, $nav_menu_selected_id ); ?>>
621                                                 <?php
622                                                 echo esc_html( $_nav_menu->truncated_name ) ;
623
624                                                 if ( ! empty( $menu_locations ) && in_array( $_nav_menu->term_id, $menu_locations ) ) {
625                                                         $locations_assigned_to_this_menu = array();
626                                                         foreach ( array_keys( $menu_locations, $_nav_menu->term_id ) as $menu_location_key ) {
627                                                                  $locations_assigned_to_this_menu[] = $locations[ $menu_location_key ];
628                                                         }
629                                                         $assigned_locations = array_slice( $locations_assigned_to_this_menu, 0, absint( apply_filters( 'wp_nav_locations_listed_per_menu', 3 ) ) );
630
631                                                         // Adds ellipses following the number of locations defined in $assigned_locations
632                                                         printf( ' (%1$s%2$s)',
633                                                                 implode( ', ', $assigned_locations ),
634                                                                 count( $locations_assigned_to_this_menu ) > count( $assigned_locations ) ? ' &hellip;' : ''
635                                                         );
636                                                 }
637                                                 ?>
638                                         </option>
639                                 <?php endforeach; ?>
640                         </select>
641                         <span class="submit-btn"><input type="submit" class="button-secondary" value="<?php _e( 'Select' ); ?>"></span>
642                         <span class="add-new-menu-action">
643                                 <?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' ) ) ) ); ?>
644                         </span><!-- /add-new-menu-action -->
645                 </form>
646         <?php endif; ?>
647         </div><!-- /manage-menus -->
648         <div id="nav-menus-frame">
649         <div id="menu-settings-column" class="metabox-holder<?php if ( isset( $_GET['menu'] ) && '0' == $_GET['menu'] ) { echo ' metabox-holder-disabled'; } ?>">
650
651                 <div class="clear"></div>
652
653                 <form id="nav-menu-meta" action="" class="nav-menu-meta" method="post" enctype="multipart/form-data">
654                         <input type="hidden" name="menu" id="nav-menu-meta-object-id" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
655                         <input type="hidden" name="action" value="add-menu-item" />
656                         <?php wp_nonce_field( 'add-menu_item', 'menu-settings-column-nonce' ); ?>
657                         <?php do_accordion_sections( 'nav-menus', 'side', null ); ?>
658                 </form>
659
660         </div><!-- /#menu-settings-column -->
661         <div id="menu-management-liquid">
662                 <div id="menu-management">
663                         <form id="update-nav-menu" action="" method="post" enctype="multipart/form-data">
664                                 <div class="menu-edit <?php if ( $add_new_screen ) echo 'blank-slate'; ?>">
665                                         <?php
666                                         wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
667                                         wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
668                                         wp_nonce_field( 'update-nav_menu', 'update-nav-menu-nonce' );
669
670                                         if ( $one_theme_location_no_menus ) { ?>
671                                                 <input type="hidden" name="zero-menu-state" value="true" />
672                                         <?php } ?>
673                                         <input type="hidden" name="action" value="update" />
674                                         <input type="hidden" name="menu" id="menu" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" />
675                                         <div id="nav-menu-header">
676                                                 <div class="major-publishing-actions">
677                                                         <label class="menu-name-label howto open-label" for="menu-name">
678                                                                 <span><?php _e( 'Menu Name' ); ?></span>
679                                                                 <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 ); ?>" />
680                                                         </label>
681                                                         <div class="publishing-action">
682                                                                 <?php submit_button( empty( $nav_menu_selected_id ) ? __( 'Create Menu' ) : __( 'Save Menu' ), 'button-primary menu-save', 'save_menu', false, array( 'id' => 'save_menu_header' ) ); ?>
683                                                         </div><!-- END .publishing-action -->
684                                                 </div><!-- END .major-publishing-actions -->
685                                         </div><!-- END .nav-menu-header -->
686                                         <div id="post-body">
687                                                 <div id="post-body-content">
688                                                         <?php if ( ! $add_new_screen ) : ?>
689                                                         <h3><?php _e( 'Menu Structure' ); ?></h3>
690                                                         <?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.' ); ?>
691                                                         <div class="drag-instructions post-body-plain" <?php if ( isset( $menu_items ) && 0 == count( $menu_items ) ) { ?>style="display: none;"<?php } ?>>
692                                                                 <p><?php echo $starter_copy; ?></p>
693                                                         </div>
694                                                         <?php
695                                                         if ( isset( $edit_markup ) && ! is_wp_error( $edit_markup ) ) {
696                                                                 echo $edit_markup;
697                                                         } else {
698                                                         ?>
699                                                         <ul class="menu" id="menu-to-edit"></ul>
700                                                         <?php } ?>
701                                                         <?php endif; ?>
702                                                         <?php if ( $add_new_screen ) : ?>
703                                                                 <p class="post-body-plain"><?php _e( 'Give your menu a name above, then click Create Menu.' ); ?></p>
704                                                                 <?php if ( isset( $_GET['use-location'] ) ) : ?>
705                                                                         <input type="hidden" name="use-location" value="<?php echo esc_attr( $_GET['use-location'] ); ?>" />
706                                                                 <?php endif; ?>
707                                                         <?php endif; ?>
708                                                         <div class="menu-settings" <?php if ( $one_theme_location_no_menus ) { ?>style="display: none;"<?php } ?>>
709                                                                 <h3><?php _e( 'Menu Settings' ); ?></h3>
710                                                                 <?php
711                                                                 if ( ! isset( $auto_add ) ) {
712                                                                         $auto_add = get_option( 'nav_menu_options' );
713                                                                         if ( ! isset( $auto_add['auto_add'] ) )
714                                                                                 $auto_add = false;
715                                                                         elseif ( false !== array_search( $nav_menu_selected_id, $auto_add['auto_add'] ) )
716                                                                                 $auto_add = true;
717                                                                         else
718                                                                                 $auto_add = false;
719                                                                 } ?>
720
721                                                                 <dl class="auto-add-pages">
722                                                                         <dt class="howto"><?php _e( 'Auto add pages' ); ?></dt>
723                                                                         <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>
724                                                                 </dl>
725
726                                                                 <?php if ( current_theme_supports( 'menus' ) ) : ?>
727
728                                                                         <dl class="menu-theme-locations">
729                                                                                 <dt class="howto"><?php _e( 'Theme locations' ); ?></dt>
730                                                                                 <?php foreach ( $locations as $location => $description ) : ?>
731                                                                                 <dd class="checkbox-input">
732                                                                                         <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>
733                                                                                         <?php if ( ! empty( $menu_locations[ $location ] ) && $menu_locations[ $location ] != $nav_menu_selected_id ) : ?>
734                                                                                         <span class="theme-location-set"> <?php printf( __( "(Currently set to: %s)" ), wp_get_nav_menu_object( $menu_locations[ $location ] )->name ); ?> </span>
735                                                                                         <?php endif; ?>
736                                                                                 </dd>
737                                                                                 <?php endforeach; ?>
738                                                                         </dl>
739
740                                                                 <?php endif; ?>
741
742                                                         </div>
743                                                 </div><!-- /#post-body-content -->
744                                         </div><!-- /#post-body -->
745                                         <div id="nav-menu-footer">
746                                                 <div class="major-publishing-actions">
747                                                         <?php if ( 0 != $menu_count && ! $add_new_screen ) : ?>
748                                                         <span class="delete-action">
749                                                                 <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>
750                                                         </span><!-- END .delete-action -->
751                                                         <?php endif; ?>
752                                                         <div class="publishing-action">
753                                                                 <?php submit_button( empty( $nav_menu_selected_id ) ? __( 'Create Menu' ) : __( 'Save Menu' ), 'button-primary menu-save', 'save_menu', false, array( 'id' => 'save_menu_header' ) ); ?>
754                                                         </div><!-- END .publishing-action -->
755                                                 </div><!-- END .major-publishing-actions -->
756                                         </div><!-- /#nav-menu-footer -->
757                                 </div><!-- /.menu-edit -->
758                         </form><!-- /#update-nav-menu -->
759                 </div><!-- /#menu-management -->
760         </div><!-- /#menu-management-liquid -->
761         </div><!-- /#nav-menus-frame -->
762         <?php endif; ?>
763 </div><!-- /.wrap-->
764 <?php include( './admin-footer.php' ); ?>