]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/nav-menu.php
Wordpress 3.1
[autoinstalls/wordpress.git] / wp-admin / includes / nav-menu.php
1 <?php
2
3 /**
4  * Create HTML list of nav menu input items.
5  *
6  * @package WordPress
7  * @since 3.0.0
8  * @uses Walker_Nav_Menu
9  */
10 class Walker_Nav_Menu_Edit extends Walker_Nav_Menu  {
11         /**
12          * @see Walker_Nav_Menu::start_lvl()
13          * @since 3.0.0
14          *
15          * @param string $output Passed by reference.
16          */
17         function start_lvl(&$output) {}
18
19         /**
20          * @see Walker_Nav_Menu::end_lvl()
21          * @since 3.0.0
22          *
23          * @param string $output Passed by reference.
24          */
25         function end_lvl(&$output) {
26         }
27
28         /**
29          * @see Walker::start_el()
30          * @since 3.0.0
31          *
32          * @param string $output Passed by reference. Used to append additional content.
33          * @param object $item Menu item data object.
34          * @param int $depth Depth of menu item. Used for padding.
35          * @param object $args
36          */
37         function start_el(&$output, $item, $depth, $args) {
38                 global $_wp_nav_menu_max_depth;
39                 $_wp_nav_menu_max_depth = $depth > $_wp_nav_menu_max_depth ? $depth : $_wp_nav_menu_max_depth;
40
41                 $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
42
43                 ob_start();
44                 $item_id = esc_attr( $item->ID );
45                 $removed_args = array(
46                         'action',
47                         'customlink-tab',
48                         'edit-menu-item',
49                         'menu-item',
50                         'page-tab',
51                         '_wpnonce',
52                 );
53
54                 $original_title = '';
55                 if ( 'taxonomy' == $item->type ) {
56                         $original_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
57                 } elseif ( 'post_type' == $item->type ) {
58                         $original_object = get_post( $item->object_id );
59                         $original_title = $original_object->post_title;
60                 }
61
62                 $classes = array(
63                         'menu-item menu-item-depth-' . $depth,
64                         'menu-item-' . esc_attr( $item->object ),
65                         'menu-item-edit-' . ( ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? 'active' : 'inactive'),
66                 );
67
68                 $title = $item->title;
69
70                 if ( isset( $item->post_status ) && 'draft' == $item->post_status ) {
71                         $classes[] = 'pending';
72                         /* translators: %s: title of menu item in draft status */
73                         $title = sprintf( __('%s (Pending)'), $item->title );
74                 }
75
76                 $title = empty( $item->label ) ? $title : $item->label;
77
78                 ?>
79                 <li id="menu-item-<?php echo $item_id; ?>" class="<?php echo implode(' ', $classes ); ?>">
80                         <dl class="menu-item-bar">
81                                 <dt class="menu-item-handle">
82                                         <span class="item-title"><?php echo esc_html( $title ); ?></span>
83                                         <span class="item-controls">
84                                                 <span class="item-type"><?php echo esc_html( $item->type_label ); ?></span>
85                                                 <span class="item-order">
86                                                         <a href="<?php
87                                                                 echo wp_nonce_url(
88                                                                         add_query_arg(
89                                                                                 array(
90                                                                                         'action' => 'move-up-menu-item',
91                                                                                         'menu-item' => $item_id,
92                                                                                 ),
93                                                                                 remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
94                                                                         ),
95                                                                         'move-menu_item'
96                                                                 );
97                                                         ?>" class="item-move-up"><abbr title="<?php esc_attr_e('Move up'); ?>">&#8593;</abbr></a>
98                                                         |
99                                                         <a href="<?php
100                                                                 echo wp_nonce_url(
101                                                                         add_query_arg(
102                                                                                 array(
103                                                                                         'action' => 'move-down-menu-item',
104                                                                                         'menu-item' => $item_id,
105                                                                                 ),
106                                                                                 remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
107                                                                         ),
108                                                                         'move-menu_item'
109                                                                 );
110                                                         ?>" class="item-move-down"><abbr title="<?php esc_attr_e('Move down'); ?>">&#8595;</abbr></a>
111                                                 </span>
112                                                 <a class="item-edit" id="edit-<?php echo $item_id; ?>" title="<?php _e('Edit Menu Item'); ?>" href="<?php
113                                                         echo ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? admin_url( 'nav-menus.php' ) : add_query_arg( 'edit-menu-item', $item_id, remove_query_arg( $removed_args, admin_url( 'nav-menus.php#menu-item-settings-' . $item_id ) ) );
114                                                 ?>"><?php _e( 'Edit Menu Item' ); ?></a>
115                                         </span>
116                                 </dt>
117                         </dl>
118
119                         <div class="menu-item-settings" id="menu-item-settings-<?php echo $item_id; ?>">
120                                 <?php if( 'custom' == $item->type ) : ?>
121                                         <p class="field-url description description-wide">
122                                                 <label for="edit-menu-item-url-<?php echo $item_id; ?>">
123                                                         <?php _e( 'URL' ); ?><br />
124                                                         <input type="text" id="edit-menu-item-url-<?php echo $item_id; ?>" class="widefat code edit-menu-item-url" name="menu-item-url[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->url ); ?>" />
125                                                 </label>
126                                         </p>
127                                 <?php endif; ?>
128                                 <p class="description description-thin">
129                                         <label for="edit-menu-item-title-<?php echo $item_id; ?>">
130                                                 <?php _e( 'Navigation Label' ); ?><br />
131                                                 <input type="text" id="edit-menu-item-title-<?php echo $item_id; ?>" class="widefat edit-menu-item-title" name="menu-item-title[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->title ); ?>" />
132                                         </label>
133                                 </p>
134                                 <p class="description description-thin">
135                                         <label for="edit-menu-item-attr-title-<?php echo $item_id; ?>">
136                                                 <?php _e( 'Title Attribute' ); ?><br />
137                                                 <input type="text" id="edit-menu-item-attr-title-<?php echo $item_id; ?>" class="widefat edit-menu-item-attr-title" name="menu-item-attr-title[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->post_excerpt ); ?>" />
138                                         </label>
139                                 </p>
140                                 <p class="field-link-target description description-thin">
141                                         <label for="edit-menu-item-target-<?php echo $item_id; ?>">
142                                                 <?php _e( 'Link Target' ); ?><br />
143                                                 <select id="edit-menu-item-target-<?php echo $item_id; ?>" class="widefat edit-menu-item-target" name="menu-item-target[<?php echo $item_id; ?>]">
144                                                         <option value="" <?php selected( $item->target, ''); ?>><?php _e('Same window or tab'); ?></option>
145                                                         <option value="_blank" <?php selected( $item->target, '_blank'); ?>><?php _e('New window or tab'); ?></option>
146                                                 </select>
147                                         </label>
148                                 </p>
149                                 <p class="field-css-classes description description-thin">
150                                         <label for="edit-menu-item-classes-<?php echo $item_id; ?>">
151                                                 <?php _e( 'CSS Classes (optional)' ); ?><br />
152                                                 <input type="text" id="edit-menu-item-classes-<?php echo $item_id; ?>" class="widefat code edit-menu-item-classes" name="menu-item-classes[<?php echo $item_id; ?>]" value="<?php echo esc_attr( implode(' ', $item->classes ) ); ?>" />
153                                         </label>
154                                 </p>
155                                 <p class="field-xfn description description-thin">
156                                         <label for="edit-menu-item-xfn-<?php echo $item_id; ?>">
157                                                 <?php _e( 'Link Relationship (XFN)' ); ?><br />
158                                                 <input type="text" id="edit-menu-item-xfn-<?php echo $item_id; ?>" class="widefat code edit-menu-item-xfn" name="menu-item-xfn[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->xfn ); ?>" />
159                                         </label>
160                                 </p>
161                                 <p class="field-description description description-wide">
162                                         <label for="edit-menu-item-description-<?php echo $item_id; ?>">
163                                                 <?php _e( 'Description' ); ?><br />
164                                                 <textarea id="edit-menu-item-description-<?php echo $item_id; ?>" class="widefat edit-menu-item-description" rows="3" cols="20" name="menu-item-description[<?php echo $item_id; ?>]"><?php echo esc_html( $item->description ); // textarea_escaped ?></textarea>
165                                                 <span class="description"><?php _e('The description will be displayed in the menu if the current theme supports it.'); ?></span>
166                                         </label>
167                                 </p>
168
169                                 <div class="menu-item-actions description-wide submitbox">
170                                         <?php if( 'custom' != $item->type ) : ?>
171                                                 <p class="link-to-original">
172                                                         <?php printf( __('Original: %s'), '<a href="' . esc_attr( $item->url ) . '">' . esc_html( $original_title ) . '</a>' ); ?>
173                                                 </p>
174                                         <?php endif; ?>
175                                         <a class="item-delete submitdelete deletion" id="delete-<?php echo $item_id; ?>" href="<?php
176                                         echo wp_nonce_url(
177                                                 add_query_arg(
178                                                         array(
179                                                                 'action' => 'delete-menu-item',
180                                                                 'menu-item' => $item_id,
181                                                         ),
182                                                         remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
183                                                 ),
184                                                 'delete-menu_item_' . $item_id
185                                         ); ?>"><?php _e('Remove'); ?></a> <span class="meta-sep"> | </span> <a class="item-cancel submitcancel" id="cancel-<?php echo $item_id; ?>" href="<?php echo add_query_arg( array('edit-menu-item' => $item_id, 'cancel' => time()), remove_query_arg( $removed_args, admin_url( 'nav-menus.php' ) ) );
186                                                 ?>#menu-item-settings-<?php echo $item_id; ?>"><?php _e('Cancel'); ?></a>
187                                 </div>
188
189                                 <input class="menu-item-data-db-id" type="hidden" name="menu-item-db-id[<?php echo $item_id; ?>]" value="<?php echo $item_id; ?>" />
190                                 <input class="menu-item-data-object-id" type="hidden" name="menu-item-object-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object_id ); ?>" />
191                                 <input class="menu-item-data-object" type="hidden" name="menu-item-object[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object ); ?>" />
192                                 <input class="menu-item-data-parent-id" type="hidden" name="menu-item-parent-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_item_parent ); ?>" />
193                                 <input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_order ); ?>" />
194                                 <input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->type ); ?>" />
195                         </div><!-- .menu-item-settings-->
196                         <ul class="menu-item-transport"></ul>
197                 <?php
198                 $output .= ob_get_clean();
199         }
200 }
201
202 /**
203  * Create HTML list of nav menu input items.
204  *
205  * @package WordPress
206  * @since 3.0.0
207  * @uses Walker_Nav_Menu
208  */
209 class Walker_Nav_Menu_Checklist extends Walker_Nav_Menu  {
210
211         /**
212          * @see Walker::start_el()
213          * @since 3.0.0
214          *
215          * @param string $output Passed by reference. Used to append additional content.
216          * @param object $item Menu item data object.
217          * @param int $depth Depth of menu item. Used for padding.
218          * @param object $args
219          */
220         function start_el(&$output, $item, $depth, $args) {
221                 global $_nav_menu_placeholder;
222
223                 $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1;
224                 $possible_object_id = isset( $item->post_type ) && 'nav_menu_item' == $item->post_type ? $item->object_id : $_nav_menu_placeholder;
225                 $possible_db_id = ( ! empty( $item->ID ) ) && ( 0 < $possible_object_id ) ? (int) $item->ID : 0;
226
227                 $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
228
229                 $output .= $indent . '<li>';
230                 $output .= '<label class="menu-item-title">';
231                 $output .= '<input type="checkbox" class="menu-item-checkbox';
232                 if ( ! empty( $item->_add_to_top ) ) {
233                         $output .= ' add-to-top';
234                 }
235                 $output .= '" name="menu-item[' . $possible_object_id . '][menu-item-object-id]" value="'. esc_attr( $item->object_id ) .'" /> ';
236                 $output .= empty( $item->label ) ? esc_html( $item->title ) : esc_html( $item->label );
237                 $output .= '</label>';
238
239                 // Menu item hidden fields
240                 $output .= '<input type="hidden" class="menu-item-db-id" name="menu-item[' . $possible_object_id . '][menu-item-db-id]" value="' . $possible_db_id . '" />';
241                 $output .= '<input type="hidden" class="menu-item-object" name="menu-item[' . $possible_object_id . '][menu-item-object]" value="'. esc_attr( $item->object ) .'" />';
242                 $output .= '<input type="hidden" class="menu-item-parent-id" name="menu-item[' . $possible_object_id . '][menu-item-parent-id]" value="'. esc_attr( $item->menu_item_parent ) .'" />';
243                 $output .= '<input type="hidden" class="menu-item-type" name="menu-item[' . $possible_object_id . '][menu-item-type]" value="'. esc_attr( $item->type ) .'" />';
244                 $output .= '<input type="hidden" class="menu-item-title" name="menu-item[' . $possible_object_id . '][menu-item-title]" value="'. esc_attr( $item->title ) .'" />';
245                 $output .= '<input type="hidden" class="menu-item-url" name="menu-item[' . $possible_object_id . '][menu-item-url]" value="'. esc_attr( $item->url ) .'" />';
246                 $output .= '<input type="hidden" class="menu-item-target" name="menu-item[' . $possible_object_id . '][menu-item-target]" value="'. esc_attr( $item->target ) .'" />';
247                 $output .= '<input type="hidden" class="menu-item-attr_title" name="menu-item[' . $possible_object_id . '][menu-item-attr_title]" value="'. esc_attr( $item->attr_title ) .'" />';
248                 $output .= '<input type="hidden" class="menu-item-classes" name="menu-item[' . $possible_object_id . '][menu-item-classes]" value="'. esc_attr( implode( ' ', $item->classes ) ) .'" />';
249                 $output .= '<input type="hidden" class="menu-item-xfn" name="menu-item[' . $possible_object_id . '][menu-item-xfn]" value="'. esc_attr( $item->xfn ) .'" />';
250         }
251 }
252
253 /**
254  * Prints the appropriate response to a menu quick search.
255  *
256  * @since 3.0.0
257  *
258  * @param array $request The unsanitized request values.
259  */
260 function _wp_ajax_menu_quick_search( $request = array() ) {
261         $args = array();
262         $type = isset( $request['type'] ) ? $request['type'] : '';
263         $object_type = isset( $request['object_type'] ) ? $request['object_type'] : '';
264         $query = isset( $request['q'] ) ? $request['q'] : '';
265         $response_format = isset( $request['response-format'] ) && in_array( $request['response-format'], array( 'json', 'markup' ) ) ? $request['response-format'] : 'json';
266
267         if ( 'markup' == $response_format ) {
268                 $args['walker'] = new Walker_Nav_Menu_Checklist;
269         }
270
271         if ( 'get-post-item' == $type ) {
272                 if ( post_type_exists( $object_type ) ) {
273                         if ( isset( $request['ID'] ) ) {
274                                 $object_id = (int) $request['ID'];
275                                 if ( 'markup' == $response_format ) {
276                                         echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_post( $object_id ) ) ), 0, (object) $args );
277                                 } elseif ( 'json' == $response_format ) {
278                                         $post_obj = get_post( $object_id );
279                                         echo json_encode(
280                                                 array(
281                                                         'ID' => $object_id,
282                                                         'post_title' => get_the_title( $object_id ),
283                                                         'post_type' => get_post_type( $object_id ),
284                                                 )
285                                         );
286                                         echo "\n";
287                                 }
288                         }
289                 } elseif ( taxonomy_exists( $object_type ) ) {
290                         if ( isset( $request['ID'] ) ) {
291                                 $object_id = (int) $request['ID'];
292                                 if ( 'markup' == $response_format ) {
293                                         echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_term( $object_id, $object_type ) ) ), 0, (object) $args );
294                                 } elseif ( 'json' == $response_format ) {
295                                         $post_obj = get_term( $object_id, $object_type );
296                                         echo json_encode(
297                                                 array(
298                                                         'ID' => $object_id,
299                                                         'post_title' => $post_obj->name,
300                                                         'post_type' => $object_type,
301                                                 )
302                                         );
303                                         echo "\n";
304                                 }
305                         }
306
307                 }
308
309
310         } elseif ( preg_match('/quick-search-(posttype|taxonomy)-([a-zA-Z_-]*\b)/', $type, $matches) ) {
311                 if ( 'posttype' == $matches[1] && get_post_type_object( $matches[2] ) ) {
312                         query_posts(array(
313                                 'posts_per_page' => 10,
314                                 'post_type' => $matches[2],
315                                 's' => $query,
316                         ));
317                         if ( ! have_posts() )
318                                 return;
319                         while ( have_posts() ) {
320                                 the_post();
321                                 if ( 'markup' == $response_format ) {
322                                         echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_post( $var_by_ref = get_the_ID() ) ) ), 0, (object) $args );
323                                 } elseif ( 'json' == $response_format ) {
324                                         echo json_encode(
325                                                 array(
326                                                         'ID' => get_the_ID(),
327                                                         'post_title' => get_the_title(),
328                                                         'post_type' => get_post_type(),
329                                                 )
330                                         );
331                                         echo "\n";
332                                 }
333                         }
334                 } elseif ( 'taxonomy' == $matches[1] ) {
335                         $terms = get_terms( $matches[2], array(
336                                 'name__like' => $query,
337                                 'number' => 10,
338                         ));
339                         if ( empty( $terms ) || is_wp_error( $terms ) )
340                                 return;
341                         foreach( (array) $terms as $term ) {
342                                 if ( 'markup' == $response_format ) {
343                                         echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( $term ) ), 0, (object) $args );
344                                 } elseif ( 'json' == $response_format ) {
345                                         echo json_encode(
346                                                 array(
347                                                         'ID' => $term->term_id,
348                                                         'post_title' => $term->name,
349                                                         'post_type' => $matches[2],
350                                                 )
351                                         );
352                                         echo "\n";
353                                 }
354                         }
355                 }
356         }
357 }
358
359 /**
360  * Register nav menu metaboxes and advanced menu items
361  *
362  * @since 3.0.0
363  **/
364 function wp_nav_menu_setup() {
365         // Register meta boxes
366         if ( wp_get_nav_menus() )
367                 add_meta_box( 'nav-menu-theme-locations', __( 'Theme Locations' ), 'wp_nav_menu_locations_meta_box' , 'nav-menus', 'side', 'default' );
368         add_meta_box( 'add-custom-links', __('Custom Links'), 'wp_nav_menu_item_link_meta_box', 'nav-menus', 'side', 'default' );
369         wp_nav_menu_post_type_meta_boxes();
370         wp_nav_menu_taxonomy_meta_boxes();
371
372         // Register advanced menu items (columns)
373         add_filter( 'manage_nav-menus_columns', 'wp_nav_menu_manage_columns');
374
375         // If first time editing, disable advanced items by default.
376         if( false === get_user_option( 'managenav-menuscolumnshidden' ) ) {
377                 $user = wp_get_current_user();
378                 update_user_option($user->ID, 'managenav-menuscolumnshidden',
379                         array( 0 => 'link-target', 1 => 'css-classes', 2 => 'xfn', 3 => 'description', ),
380                         true);
381         }
382 }
383
384 /**
385  * Limit the amount of meta boxes to just links, pages and cats for first time users.
386  *
387  * @since 3.0.0
388  **/
389 function wp_initial_nav_menu_meta_boxes() {
390         global $wp_meta_boxes;
391
392         if ( get_user_option( 'metaboxhidden_nav-menus' ) !== false || ! is_array($wp_meta_boxes) )
393                 return;
394
395         $initial_meta_boxes = array( 'nav-menu-theme-locations', 'add-custom-links', 'add-page', 'add-category' );
396         $hidden_meta_boxes = array();
397
398         foreach ( array_keys($wp_meta_boxes['nav-menus']) as $context ) {
399                 foreach ( array_keys($wp_meta_boxes['nav-menus'][$context]) as $priority ) {
400                         foreach ( $wp_meta_boxes['nav-menus'][$context][$priority] as $box ) {
401                                 if ( in_array( $box['id'], $initial_meta_boxes ) ) {
402                                         unset( $box['id'] );
403                                 } else {
404                                         $hidden_meta_boxes[] = $box['id'];
405                                 }
406                         }
407                 }
408         }
409
410         $user = wp_get_current_user();
411         update_user_option( $user->ID, 'metaboxhidden_nav-menus', $hidden_meta_boxes, true );
412 }
413
414 /**
415  * Creates metaboxes for any post type menu item.
416  *
417  * @since 3.0.0
418  */
419 function wp_nav_menu_post_type_meta_boxes() {
420         $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
421
422         if ( ! $post_types )
423                 return;
424
425         foreach ( $post_types as $post_type ) {
426                 $post_type = apply_filters( 'nav_menu_meta_box_object', $post_type );
427                 if ( $post_type ) {
428                         $id = $post_type->name;
429                         add_meta_box( "add-{$id}", $post_type->labels->name, 'wp_nav_menu_item_post_type_meta_box', 'nav-menus', 'side', 'default', $post_type );
430                 }
431         }
432 }
433
434 /**
435  * Creates metaboxes for any taxonomy menu item.
436  *
437  * @since 3.0.0
438  */
439 function wp_nav_menu_taxonomy_meta_boxes() {
440         $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'object' );
441
442         if ( !$taxonomies )
443                 return;
444
445         foreach ( $taxonomies as $tax ) {
446                 $tax = apply_filters( 'nav_menu_meta_box_object', $tax );
447                 if ( $tax ) {
448                         $id = $tax->name;
449                         add_meta_box( "add-{$id}", $tax->labels->name, 'wp_nav_menu_item_taxonomy_meta_box', 'nav-menus', 'side', 'default', $tax );
450                 }
451         }
452 }
453
454 /**
455  * Displays a metabox for the nav menu theme locations.
456  *
457  * @since 3.0.0
458  */
459 function wp_nav_menu_locations_meta_box() {
460         global $nav_menu_selected_id;
461
462         if ( ! current_theme_supports( 'menus' ) ) {
463                 // We must only support widgets. Leave a message and bail.
464                 echo '<p class="howto">' . __('The current theme does not natively support menus, but you can use the &#8220;Custom Menu&#8221; widget to add any menus you create here to the theme&#8217;s sidebar.') . '</p>';
465                 return;
466         }
467
468         $locations = get_registered_nav_menus();
469         $menus = wp_get_nav_menus();
470         $menu_locations = get_nav_menu_locations();
471         $num_locations = count( array_keys($locations) );
472
473         echo '<p class="howto">' . sprintf( _n('Your theme supports %s menu. Select which menu you would like to use.', 'Your theme supports %s menus. Select which menu appears in each location.', $num_locations ), number_format_i18n($num_locations) ) . '</p>';
474
475         foreach ( $locations as $location => $description ) {
476                 ?>
477                 <p>
478                         <label class="howto" for="locations-<?php echo $location; ?>">
479                                 <span><?php echo $description; ?></span>
480                                 <select name="menu-locations[<?php echo $location; ?>]" id="locations-<?php echo $location; ?>">
481                                         <option value="0"></option>
482                                         <?php foreach ( $menus as $menu ) : ?>
483                                         <option<?php selected( isset( $menu_locations[ $location ] ) && $menu_locations[ $location ] == $menu->term_id ); ?>
484                                                 value="<?php echo $menu->term_id; ?>"><?php
485                                                 $truncated_name = wp_html_excerpt( $menu->name, 40 );
486                                                 echo $truncated_name == $menu->name ? $menu->name : trim( $truncated_name ) . '&hellip;';
487                                         ?></option>
488                                         <?php endforeach; ?>
489                                 </select>
490                         </label>
491                 </p>
492         <?php
493         }
494         ?>
495         <p class="button-controls">
496                 <img class="waiting" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />
497                 <?php submit_button( __( 'Save' ), 'primary', 'nav-menu-locations', false, disabled( $nav_menu_selected_id, 0, false ) ); ?>
498         </p>
499         <?php
500 }
501
502 /**
503  * Displays a metabox for the custom links menu item.
504  *
505  * @since 3.0.0
506  */
507 function wp_nav_menu_item_link_meta_box() {
508         global $_nav_menu_placeholder, $nav_menu_selected_id;
509         $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;
510
511         $current_tab = 'create';
512         if ( isset( $_REQUEST['customlink-tab'] ) && in_array( $_REQUEST['customlink-tab'], array('create', 'all') ) ) {
513                 $current_tab = $_REQUEST['customlink-tab'];
514         }
515
516         $removed_args = array(
517                 'action',
518                 'customlink-tab',
519                 'edit-menu-item',
520                 'menu-item',
521                 'page-tab',
522                 '_wpnonce',
523         );
524
525         ?>
526         <div class="customlinkdiv" id="customlinkdiv">
527
528                         <input type="hidden" value="custom" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-type]" />
529                         <p id="menu-item-url-wrap">
530                                 <label class="howto" for="custom-menu-item-url">
531                                         <span><?php _e('URL'); ?></span>
532                                         <input id="custom-menu-item-url" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-url]" type="text" class="code menu-item-textbox" value="http://" />
533                                 </label>
534                         </p>
535
536                         <p id="menu-item-name-wrap">
537                                 <label class="howto" for="custom-menu-item-name">
538                                         <span><?php _e('Label'); ?></span>
539                                         <input id="custom-menu-item-name" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-title]" type="text" class="regular-text menu-item-textbox input-with-default-title" title="<?php esc_attr_e('Menu Item'); ?>" />
540                                 </label>
541                         </p>
542
543                 <p class="button-controls">
544                         <span class="add-to-menu">
545                                 <img class="waiting" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />
546                                 <input type="submit"<?php disabled( $nav_menu_selected_id, 0 ); ?> class="button-secondary submit-add-to-menu" value="<?php esc_attr_e('Add to Menu'); ?>" name="add-custom-menu-item" id="submit-customlinkdiv" />
547                         </span>
548                 </p>
549
550         </div><!-- /.customlinkdiv -->
551         <?php
552 }
553
554 /**
555  * Displays a metabox for a post type menu item.
556  *
557  * @since 3.0.0
558  *
559  * @param string $object Not used.
560  * @param string $post_type The post type object.
561  */
562 function wp_nav_menu_item_post_type_meta_box( $object, $post_type ) {
563         global $_nav_menu_placeholder, $nav_menu_selected_id;
564
565         $post_type_name = $post_type['args']->name;
566
567         // paginate browsing for large numbers of post objects
568         $per_page = 50;
569         $pagenum = isset( $_REQUEST[$post_type_name . '-tab'] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
570         $offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;
571
572         $args = array(
573                 'offset' => $offset,
574                 'order' => 'ASC',
575                 'orderby' => 'title',
576                 'posts_per_page' => $per_page,
577                 'post_type' => $post_type_name,
578                 'suppress_filters' => true,
579                 'update_post_term_cache' => false,
580                 'update_post_meta_cache' => false
581         );
582
583         if ( isset( $post_type['args']->_default_query ) )
584                 $args = array_merge($args, (array) $post_type['args']->_default_query );
585
586         // @todo transient caching of these results with proper invalidation on updating of a post of this type
587         $get_posts = new WP_Query;
588         $posts = $get_posts->query( $args );
589         if ( ! $get_posts->post_count ) {
590                 echo '<p>' . __( 'No items.' ) . '</p>';
591                 return;
592         }
593
594         $post_type_object = get_post_type_object($post_type_name);
595
596         $num_pages = $get_posts->max_num_pages;
597
598         $page_links = paginate_links( array(
599                 'base' => add_query_arg(
600                         array(
601                                 $post_type_name . '-tab' => 'all',
602                                 'paged' => '%#%',
603                                 'item-type' => 'post_type',
604                                 'item-object' => $post_type_name,
605                         )
606                 ),
607                 'format' => '',
608                 'prev_text' => __('&laquo;'),
609                 'next_text' => __('&raquo;'),
610                 'total' => $num_pages,
611                 'current' => $pagenum
612         ));
613
614         if ( !$posts )
615                 $error = '<li id="error">'. $post_type['args']->labels->not_found .'</li>';
616
617         $walker = new Walker_Nav_Menu_Checklist;
618
619         $current_tab = 'most-recent';
620         if ( isset( $_REQUEST[$post_type_name . '-tab'] ) && in_array( $_REQUEST[$post_type_name . '-tab'], array('all', 'search') ) ) {
621                 $current_tab = $_REQUEST[$post_type_name . '-tab'];
622         }
623
624         if ( ! empty( $_REQUEST['quick-search-posttype-' . $post_type_name] ) ) {
625                 $current_tab = 'search';
626         }
627
628         $removed_args = array(
629                 'action',
630                 'customlink-tab',
631                 'edit-menu-item',
632                 'menu-item',
633                 'page-tab',
634                 '_wpnonce',
635         );
636
637         ?>
638         <div id="posttype-<?php echo $post_type_name; ?>" class="posttypediv">
639                 <ul id="posttype-<?php echo $post_type_name; ?>-tabs" class="posttype-tabs add-menu-item-tabs">
640                         <li <?php echo ( 'most-recent' == $current_tab ? ' class="tabs"' : '' ); ?>><a class="nav-tab-link" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($post_type_name . '-tab', 'most-recent', remove_query_arg($removed_args))); ?>#tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent"><?php _e('Most Recent'); ?></a></li>
641                         <li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>><a class="nav-tab-link" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($post_type_name . '-tab', 'all', remove_query_arg($removed_args))); ?>#<?php echo $post_type_name; ?>-all"><?php _e('View All'); ?></a></li>
642                         <li <?php echo ( 'search' == $current_tab ? ' class="tabs"' : '' ); ?>><a class="nav-tab-link" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($post_type_name . '-tab', 'search', remove_query_arg($removed_args))); ?>#tabs-panel-posttype-<?php echo $post_type_name; ?>-search"><?php _e('Search'); ?></a></li>
643                 </ul>
644
645                 <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent" class="tabs-panel <?php
646                         echo ( 'most-recent' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
647                 ?>">
648                         <ul id="<?php echo $post_type_name; ?>checklist-most-recent" class="categorychecklist form-no-clear">
649                                 <?php
650                                 $recent_args = array_merge( $args, array( 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => 15 ) );
651                                 $most_recent = $get_posts->query( $recent_args );
652                                 $args['walker'] = $walker;
653                                 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $most_recent), 0, (object) $args );
654                                 ?>
655                         </ul>
656                 </div><!-- /.tabs-panel -->
657
658                 <div class="tabs-panel <?php
659                         echo ( 'search' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
660                 ?>" id="tabs-panel-posttype-<?php echo $post_type_name; ?>-search">
661                         <?php
662                         if ( isset( $_REQUEST['quick-search-posttype-' . $post_type_name] ) ) {
663                                 $searched = esc_attr( $_REQUEST['quick-search-posttype-' . $post_type_name] );
664                                 $search_results = get_posts( array( 's' => $searched, 'post_type' => $post_type_name, 'fields' => 'all', 'order' => 'DESC', ) );
665                         } else {
666                                 $searched = '';
667                                 $search_results = array();
668                         }
669                         ?>
670                         <p class="quick-search-wrap">
671                                 <input type="text" class="quick-search input-with-default-title" title="<?php esc_attr_e('Search'); ?>" value="<?php echo $searched; ?>" name="quick-search-posttype-<?php echo $post_type_name; ?>" />
672                                 <img class="waiting" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />
673                                 <?php submit_button( __( 'Search' ), 'quick-search-submit button-secondary hide-if-js', 'submit', false ); ?>
674                         </p>
675
676                         <ul id="<?php echo $post_type_name; ?>-search-checklist" class="list:<?php echo $post_type_name?> categorychecklist form-no-clear">
677                         <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?>
678                                 <?php
679                                 $args['walker'] = $walker;
680                                 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args );
681                                 ?>
682                         <?php elseif ( is_wp_error( $search_results ) ) : ?>
683                                 <li><?php echo $search_results->get_error_message(); ?></li>
684                         <?php elseif ( ! empty( $searched ) ) : ?>
685                                 <li><?php _e('No results found.'); ?></li>
686                         <?php endif; ?>
687                         </ul>
688                 </div><!-- /.tabs-panel -->
689
690
691                 <div id="<?php echo $post_type_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php
692                         echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
693                 ?>">
694                         <?php if ( ! empty( $page_links ) ) : ?>
695                                 <div class="add-menu-item-pagelinks">
696                                         <?php echo $page_links; ?>
697                                 </div>
698                         <?php endif; ?>
699                         <ul id="<?php echo $post_type_name; ?>checklist" class="list:<?php echo $post_type_name?> categorychecklist form-no-clear">
700                                 <?php
701                                 $args['walker'] = $walker;
702
703                                 // if we're dealing with pages, let's put a checkbox for the front page at the top of the list
704                                 if ( 'page' == $post_type_name ) {
705                                         $front_page = 'page' == get_option('show_on_front') ? (int) get_option( 'page_on_front' ) : 0;
706                                         if ( ! empty( $front_page ) ) {
707                                                 $front_page_obj = get_post( $front_page );
708                                                 $front_page_obj->_add_to_top = true;
709                                                 $front_page_obj->label = sprintf( _x('Home: %s', 'nav menu front page title'), $front_page_obj->post_title );
710                                                 array_unshift( $posts, $front_page_obj );
711                                         } else {
712                                                 $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1;
713                                                 array_unshift( $posts, (object) array(
714                                                         '_add_to_top' => true,
715                                                         'ID' => 0,
716                                                         'object_id' => $_nav_menu_placeholder,
717                                                         'post_content' => '',
718                                                         'post_excerpt' => '',
719                                                         'post_title' => _x('Home', 'nav menu home label'),
720                                                         'post_type' => 'nav_menu_item',
721                                                         'type' => 'custom',
722                                                         'url' => home_url('/'),
723                                                 ) );
724                                         }
725                                 }
726
727                                 $checkbox_items = walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $posts), 0, (object) $args );
728
729                                 if ( 'all' == $current_tab && ! empty( $_REQUEST['selectall'] ) ) {
730                                         $checkbox_items = preg_replace('/(type=(.)checkbox(\2))/', '$1 checked=$2checked$2', $checkbox_items);
731
732                                 }
733
734                                 echo $checkbox_items;
735                                 ?>
736                         </ul>
737                         <?php if ( ! empty( $page_links ) ) : ?>
738                                 <div class="add-menu-item-pagelinks">
739                                         <?php echo $page_links; ?>
740                                 </div>
741                         <?php endif; ?>
742                 </div><!-- /.tabs-panel -->
743
744
745                 <p class="button-controls">
746                         <span class="list-controls">
747                                 <a href="<?php
748                                         echo esc_url(add_query_arg(
749                                                 array(
750                                                         $post_type_name . '-tab' => 'all',
751                                                         'selectall' => 1,
752                                                 ),
753                                                 remove_query_arg($removed_args)
754                                         ));
755                                 ?>#posttype-<?php echo $post_type_name; ?>" class="select-all"><?php _e('Select All'); ?></a>
756                         </span>
757
758                         <span class="add-to-menu">
759                                 <img class="waiting" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />
760                                 <input type="submit"<?php disabled( $nav_menu_selected_id, 0 ); ?> class="button-secondary submit-add-to-menu" value="<?php esc_attr_e('Add to Menu'); ?>" name="add-post-type-menu-item" id="submit-posttype-<?php echo $post_type_name; ?>" />
761                         </span>
762                 </p>
763
764         </div><!-- /.posttypediv -->
765         <?php
766 }
767
768 /**
769  * Displays a metabox for a taxonomy menu item.
770  *
771  * @since 3.0.0
772  *
773  * @param string $object Not used.
774  * @param string $taxonomy The taxonomy object.
775  */
776 function wp_nav_menu_item_taxonomy_meta_box( $object, $taxonomy ) {
777         global $nav_menu_selected_id;
778         $taxonomy_name = $taxonomy['args']->name;
779
780         // paginate browsing for large numbers of objects
781         $per_page = 50;
782         $pagenum = isset( $_REQUEST[$taxonomy_name . '-tab'] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
783         $offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;
784
785         $args = array(
786                 'child_of' => 0,
787                 'exclude' => '',
788                 'hide_empty' => false,
789                 'hierarchical' => 1,
790                 'include' => '',
791                 'include_last_update_time' => false,
792                 'number' => $per_page,
793                 'offset' => $offset,
794                 'order' => 'ASC',
795                 'orderby' => 'name',
796                 'pad_counts' => false,
797         );
798
799         $terms = get_terms( $taxonomy_name, $args );
800
801         if ( ! $terms || is_wp_error($terms) ) {
802                 echo '<p>' . __( 'No items.' ) . '</p>';
803                 return;
804         }
805
806         $num_pages = ceil( wp_count_terms( $taxonomy_name , array_merge( $args, array('number' => '', 'offset' => '') ) ) / $per_page );
807
808         $page_links = paginate_links( array(
809                 'base' => add_query_arg(
810                         array(
811                                 $taxonomy_name . '-tab' => 'all',
812                                 'paged' => '%#%',
813                                 'item-type' => 'taxonomy',
814                                 'item-object' => $taxonomy_name,
815                         )
816                 ),
817                 'format' => '',
818                 'prev_text' => __('&laquo;'),
819                 'next_text' => __('&raquo;'),
820                 'total' => $num_pages,
821                 'current' => $pagenum
822         ));
823
824         $walker = new Walker_Nav_Menu_Checklist;
825
826         $current_tab = 'most-used';
827         if ( isset( $_REQUEST[$taxonomy_name . '-tab'] ) && in_array( $_REQUEST[$taxonomy_name . '-tab'], array('all', 'most-used', 'search') ) ) {
828                 $current_tab = $_REQUEST[$taxonomy_name . '-tab'];
829         }
830
831         if ( ! empty( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] ) ) {
832                 $current_tab = 'search';
833         }
834
835         $removed_args = array(
836                 'action',
837                 'customlink-tab',
838                 'edit-menu-item',
839                 'menu-item',
840                 'page-tab',
841                 '_wpnonce',
842         );
843
844         ?>
845         <div id="taxonomy-<?php echo $taxonomy_name; ?>" class="taxonomydiv">
846                 <ul id="taxonomy-<?php echo $taxonomy_name; ?>-tabs" class="taxonomy-tabs add-menu-item-tabs">
847                         <li <?php echo ( 'most-used' == $current_tab ? ' class="tabs"' : '' ); ?>><a class="nav-tab-link" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($taxonomy_name . '-tab', 'most-used', remove_query_arg($removed_args))); ?>#tabs-panel-<?php echo $taxonomy_name; ?>-pop"><?php _e('Most Used'); ?></a></li>
848                         <li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>><a class="nav-tab-link" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($taxonomy_name . '-tab', 'all', remove_query_arg($removed_args))); ?>#tabs-panel-<?php echo $taxonomy_name; ?>-all"><?php _e('View All'); ?></a></li>
849                         <li <?php echo ( 'search' == $current_tab ? ' class="tabs"' : '' ); ?>><a class="nav-tab-link" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($taxonomy_name . '-tab', 'search', remove_query_arg($removed_args))); ?>#tabs-panel-search-taxonomy-<?php echo $taxonomy_name; ?>"><?php _e('Search'); ?></a></li>
850                 </ul>
851
852                 <div id="tabs-panel-<?php echo $taxonomy_name; ?>-pop" class="tabs-panel <?php
853                         echo ( 'most-used' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
854                 ?>">
855                         <ul id="<?php echo $taxonomy_name; ?>checklist-pop" class="categorychecklist form-no-clear" >
856                                 <?php
857                                 $popular_terms = get_terms( $taxonomy_name, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
858                                 $args['walker'] = $walker;
859                                 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $popular_terms), 0, (object) $args );
860                                 ?>
861                         </ul>
862                 </div><!-- /.tabs-panel -->
863
864                 <div id="tabs-panel-<?php echo $taxonomy_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php
865                         echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
866                 ?>">
867                         <?php if ( ! empty( $page_links ) ) : ?>
868                                 <div class="add-menu-item-pagelinks">
869                                         <?php echo $page_links; ?>
870                                 </div>
871                         <?php endif; ?>
872                         <ul id="<?php echo $taxonomy_name; ?>checklist" class="list:<?php echo $taxonomy_name?> categorychecklist form-no-clear">
873                                 <?php
874                                 $args['walker'] = $walker;
875                                 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $terms), 0, (object) $args );
876                                 ?>
877                         </ul>
878                         <?php if ( ! empty( $page_links ) ) : ?>
879                                 <div class="add-menu-item-pagelinks">
880                                         <?php echo $page_links; ?>
881                                 </div>
882                         <?php endif; ?>
883                 </div><!-- /.tabs-panel -->
884
885                 <div class="tabs-panel <?php
886                         echo ( 'search' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
887                 ?>" id="tabs-panel-search-taxonomy-<?php echo $taxonomy_name; ?>">
888                         <?php
889                         if ( isset( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] ) ) {
890                                 $searched = esc_attr( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] );
891                                 $search_results = get_terms( $taxonomy_name, array( 'name__like' => $searched, 'fields' => 'all', 'orderby' => 'count', 'order' => 'DESC', 'hierarchical' => false ) );
892                         } else {
893                                 $searched = '';
894                                 $search_results = array();
895                         }
896                         ?>
897                         <p class="quick-search-wrap">
898                                 <input type="text" class="quick-search input-with-default-title" title="<?php esc_attr_e('Search'); ?>" value="<?php echo $searched; ?>" name="quick-search-taxonomy-<?php echo $taxonomy_name; ?>" />
899                                 <img class="waiting" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />
900                                 <?php submit_button( __( 'Search' ), 'quick-search-submit button-secondary hide-if-js', 'submit', false ); ?>
901                         </p>
902
903                         <ul id="<?php echo $taxonomy_name; ?>-search-checklist" class="list:<?php echo $taxonomy_name?> categorychecklist form-no-clear">
904                         <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?>
905                                 <?php
906                                 $args['walker'] = $walker;
907                                 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args );
908                                 ?>
909                         <?php elseif ( is_wp_error( $search_results ) ) : ?>
910                                 <li><?php echo $search_results->get_error_message(); ?></li>
911                         <?php elseif ( ! empty( $searched ) ) : ?>
912                                 <li><?php _e('No results found.'); ?></li>
913                         <?php endif; ?>
914                         </ul>
915                 </div><!-- /.tabs-panel -->
916
917                 <p class="button-controls">
918                         <span class="list-controls">
919                                 <a href="<?php
920                                         echo esc_url(add_query_arg(
921                                                 array(
922                                                         $taxonomy_name . '-tab' => 'all',
923                                                         'selectall' => 1,
924                                                 ),
925                                                 remove_query_arg($removed_args)
926                                         ));
927                                 ?>#taxonomy-<?php echo $taxonomy_name; ?>" class="select-all"><?php _e('Select All'); ?></a>
928                         </span>
929
930                         <span class="add-to-menu">
931                                 <img class="waiting" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />
932                                 <input type="submit"<?php disabled( $nav_menu_selected_id, 0 ); ?> class="button-secondary submit-add-to-menu" value="<?php esc_attr_e('Add to Menu'); ?>" name="add-taxonomy-menu-item" id="submit-taxonomy-<?php echo $taxonomy_name; ?>" />
933                         </span>
934                 </p>
935
936         </div><!-- /.taxonomydiv -->
937         <?php
938 }
939
940 /**
941  * Save posted nav menu item data.
942  *
943  * @since 3.0.0
944  *
945  * @param int $menu_id The menu ID for which to save this item. $menu_id of 0 makes a draft, orphaned menu item.
946  * @param array $menu_data The unsanitized posted menu item data.
947  * @return array The database IDs of the items saved
948  */
949 function wp_save_nav_menu_items( $menu_id = 0, $menu_data = array() ) {
950         $menu_id = (int) $menu_id;
951         $items_saved = array();
952
953         if ( 0 == $menu_id || is_nav_menu( $menu_id ) ) {
954
955                 // Loop through all the menu items' POST values
956                 foreach( (array) $menu_data as $_possible_db_id => $_item_object_data ) {
957                         if (
958                                 empty( $_item_object_data['menu-item-object-id'] ) && // checkbox is not checked
959                                 (
960                                         ! isset( $_item_object_data['menu-item-type'] ) || // and item type either isn't set
961                                         in_array( $_item_object_data['menu-item-url'], array( 'http://', '' ) ) || // or URL is the default
962                                         ! ( 'custom' == $_item_object_data['menu-item-type'] && ! isset( $_item_object_data['menu-item-db-id'] ) ) ||  // or it's not a custom menu item (but not the custom home page)
963                                         ! empty( $_item_object_data['menu-item-db-id'] ) // or it *is* a custom menu item that already exists
964                                 )
965                         ) {
966                                 continue; // then this potential menu item is not getting added to this menu
967                         }
968
969                         // if this possible menu item doesn't actually have a menu database ID yet
970                         if (
971                                 empty( $_item_object_data['menu-item-db-id'] ) ||
972                                 ( 0 > $_possible_db_id ) ||
973                                 $_possible_db_id != $_item_object_data['menu-item-db-id']
974                         ) {
975                                 $_actual_db_id = 0;
976                         } else {
977                                 $_actual_db_id = (int) $_item_object_data['menu-item-db-id'];
978                         }
979
980                         $args = array(
981                                 'menu-item-db-id' => ( isset( $_item_object_data['menu-item-db-id'] ) ? $_item_object_data['menu-item-db-id'] : '' ),
982                                 'menu-item-object-id' => ( isset( $_item_object_data['menu-item-object-id'] ) ? $_item_object_data['menu-item-object-id'] : '' ),
983                                 'menu-item-object' => ( isset( $_item_object_data['menu-item-object'] ) ? $_item_object_data['menu-item-object'] : '' ),
984                                 'menu-item-parent-id' => ( isset( $_item_object_data['menu-item-parent-id'] ) ? $_item_object_data['menu-item-parent-id'] : '' ),
985                                 'menu-item-position' => ( isset( $_item_object_data['menu-item-position'] ) ? $_item_object_data['menu-item-position'] : '' ),
986                                 'menu-item-type' => ( isset( $_item_object_data['menu-item-type'] ) ? $_item_object_data['menu-item-type'] : '' ),
987                                 'menu-item-title' => ( isset( $_item_object_data['menu-item-title'] ) ? $_item_object_data['menu-item-title'] : '' ),
988                                 'menu-item-url' => ( isset( $_item_object_data['menu-item-url'] ) ? $_item_object_data['menu-item-url'] : '' ),
989                                 'menu-item-description' => ( isset( $_item_object_data['menu-item-description'] ) ? $_item_object_data['menu-item-description'] : '' ),
990                                 'menu-item-attr-title' => ( isset( $_item_object_data['menu-item-attr-title'] ) ? $_item_object_data['menu-item-attr-title'] : '' ),
991                                 'menu-item-target' => ( isset( $_item_object_data['menu-item-target'] ) ? $_item_object_data['menu-item-target'] : '' ),
992                                 'menu-item-classes' => ( isset( $_item_object_data['menu-item-classes'] ) ? $_item_object_data['menu-item-classes'] : '' ),
993                                 'menu-item-xfn' => ( isset( $_item_object_data['menu-item-xfn'] ) ? $_item_object_data['menu-item-xfn'] : '' ),
994                         );
995
996                         $items_saved[] = wp_update_nav_menu_item( $menu_id, $_actual_db_id, $args );
997
998                 }
999         }
1000         return $items_saved;
1001 }
1002
1003 /**
1004  * Adds custom arguments to some of the meta box object types.
1005  *
1006  * @since 3.0.0
1007  *
1008  * @access private
1009  *
1010  * @param object $object The post type or taxonomy meta-object.
1011  * @return object The post type of taxonomy object.
1012  */
1013 function _wp_nav_menu_meta_box_object( $object = null ) {
1014         if ( isset( $object->name ) ) {
1015
1016                 if ( 'page' == $object->name ) {
1017                         $object->_default_query = array(
1018                                 'orderby' => 'menu_order title',
1019                                 'post_status' => 'publish',
1020                         );
1021
1022                 // posts should show only published items
1023                 } elseif ( 'post' == $object->name ) {
1024                         $object->_default_query = array(
1025                                 'post_status' => 'publish',
1026                         );
1027
1028                 // cats should be in reverse chronological order
1029                 } elseif ( 'category' == $object->name ) {
1030                         $object->_default_query = array(
1031                                 'orderby' => 'id',
1032                                 'order' => 'DESC',
1033                         );
1034
1035                 // custom post types should show only published items
1036                 } else {
1037                         $object->_default_query = array(
1038                                 'post_status' => 'publish',
1039                         );
1040                 }
1041         }
1042
1043         return $object;
1044 }
1045
1046 /**
1047  * Returns the menu formatted to edit.
1048  *
1049  * @since 3.0.0
1050  *
1051  * @param string $menu_id The ID of the menu to format.
1052  * @return string|WP_Error $output The menu formatted to edit or error object on failure.
1053  */
1054 function wp_get_nav_menu_to_edit( $menu_id = 0 ) {
1055         $menu = wp_get_nav_menu_object( $menu_id );
1056
1057         // If the menu exists, get its items.
1058         if ( is_nav_menu( $menu ) ) {
1059                 $menu_items = wp_get_nav_menu_items( $menu->term_id, array('post_status' => 'any') );
1060                 $result = '<div id="menu-instructions" class="post-body-plain';
1061                 $result .= ( ! empty($menu_items) ) ? ' menu-instructions-inactive">' : '">';
1062                 $result .= '<p>' . __('Select menu items (pages, categories, links) from the boxes at left to begin building your custom menu.') . '</p>';
1063                 $result .= '</div>';
1064
1065                 if( empty($menu_items) )
1066                         return $result . ' <ul class="menu" id="menu-to-edit"> </ul>';
1067
1068                 $walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $menu_id );
1069
1070                 if ( class_exists( $walker_class_name ) )
1071                         $walker = new $walker_class_name;
1072                 else
1073                         return new WP_Error( 'menu_walker_not_exist', sprintf( __('The Walker class named <strong>%s</strong> does not exist.'), $walker_class_name ) );
1074
1075                 $some_pending_menu_items = false;
1076                 foreach( (array) $menu_items as $menu_item ) {
1077                         if ( isset( $menu_item->post_status ) && 'draft' == $menu_item->post_status )
1078                                 $some_pending_menu_items = true;
1079                 }
1080
1081                 if ( $some_pending_menu_items )
1082                         $result .= '<div class="updated inline"><p>' . __('Click Save Menu to make pending menu items public.') . '</p></div>';
1083
1084                 $result .= '<ul class="menu" id="menu-to-edit"> ';
1085                 $result .= walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $menu_items), 0, (object) array('walker' => $walker ) );
1086                 $result .= ' </ul> ';
1087                 return $result;
1088         } elseif ( is_wp_error( $menu ) ) {
1089                 return $menu;
1090         }
1091
1092
1093 }
1094
1095 /**
1096  * Returns the columns for the nav menus page.
1097  *
1098  * @since 3.0.0
1099  *
1100  * @return string|WP_Error $output The menu formatted to edit or error object on failure.
1101  */
1102 function wp_nav_menu_manage_columns() {
1103         return array(
1104                 '_title' => __('Show advanced menu properties'),
1105                 'cb' => '<input type="checkbox" />',
1106                 'link-target' => __('Link Target'),
1107                 'css-classes' => __('CSS Classes'),
1108                 'xfn' => __('Link Relationship (XFN)'),
1109                 'description' => __('Description'),
1110         );
1111 }
1112
1113 /**
1114  * Deletes orphaned draft menu items
1115  *
1116  * @access private
1117  * @since 3.0.0
1118  *
1119  */
1120 function _wp_delete_orphaned_draft_menu_items() {
1121         global $wpdb;
1122         $delete_timestamp = time() - (60*60*24*EMPTY_TRASH_DAYS);
1123
1124         // delete orphaned draft menu items
1125         $menu_items_to_delete = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts AS p LEFT JOIN $wpdb->postmeta AS m ON p.ID = m.post_id WHERE post_type = 'nav_menu_item' AND post_status = 'draft' AND meta_key = '_menu_item_orphaned' AND meta_value < '%d'", $delete_timestamp ) );
1126
1127         foreach( (array) $menu_items_to_delete as $menu_item_id )
1128                 wp_delete_post( $menu_item_id, true );
1129 }
1130
1131 add_action('admin_head-nav-menus.php', '_wp_delete_orphaned_draft_menu_items');
1132
1133 ?>