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