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