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