4 * Create HTML list of nav menu input items.
8 * @uses Walker_Nav_Menu
10 class Walker_Nav_Menu_Edit extends Walker_Nav_Menu {
12 * @see Walker_Nav_Menu::start_lvl()
15 * @param string $output Passed by reference.
17 function start_lvl( &$output, $depth = 0, $args = array() ) {}
20 * @see Walker_Nav_Menu::end_lvl()
23 * @param string $output Passed by reference.
25 function end_lvl( &$output, $depth = 0, $args = array() ) {}
28 * @see Walker::start_el()
31 * @param string $output Passed by reference. Used to append additional content.
32 * @param object $item Menu item data object.
33 * @param int $depth Depth of menu item. Used for padding.
36 function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
37 global $_wp_nav_menu_max_depth;
38 $_wp_nav_menu_max_depth = $depth > $_wp_nav_menu_max_depth ? $depth : $_wp_nav_menu_max_depth;
40 $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
43 $item_id = esc_attr( $item->ID );
44 $removed_args = array(
54 if ( 'taxonomy' == $item->type ) {
55 $original_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
56 if ( is_wp_error( $original_title ) )
57 $original_title = false;
58 } elseif ( 'post_type' == $item->type ) {
59 $original_object = get_post( $item->object_id );
60 $original_title = $original_object->post_title;
64 'menu-item menu-item-depth-' . $depth,
65 'menu-item-' . esc_attr( $item->object ),
66 'menu-item-edit-' . ( ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? 'active' : 'inactive'),
69 $title = $item->title;
71 if ( ! empty( $item->_invalid ) ) {
72 $classes[] = 'menu-item-invalid';
73 /* translators: %s: title of menu item which is invalid */
74 $title = sprintf( __( '%s (Invalid)' ), $item->title );
75 } elseif ( isset( $item->post_status ) && 'draft' == $item->post_status ) {
76 $classes[] = 'pending';
77 /* translators: %s: title of menu item in draft status */
78 $title = sprintf( __('%s (Pending)'), $item->title );
81 $title = ( ! isset( $item->label ) || '' == $item->label ) ? $title : $item->label;
85 $submenu_text = 'style="display: none;"';
88 <li id="menu-item-<?php echo $item_id; ?>" class="<?php echo implode(' ', $classes ); ?>">
89 <dl class="menu-item-bar">
90 <dt class="menu-item-handle">
91 <span class="item-title"><span class="menu-item-title"><?php echo esc_html( $title ); ?></span> <span class="is-submenu" <?php echo $submenu_text; ?>><?php _e( 'sub item' ); ?></span></span>
92 <span class="item-controls">
93 <span class="item-type"><?php echo esc_html( $item->type_label ); ?></span>
94 <span class="item-order hide-if-js">
99 'action' => 'move-up-menu-item',
100 'menu-item' => $item_id,
102 remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
106 ?>" class="item-move-up"><abbr title="<?php esc_attr_e('Move up'); ?>">↑</abbr></a>
112 'action' => 'move-down-menu-item',
113 'menu-item' => $item_id,
115 remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
119 ?>" class="item-move-down"><abbr title="<?php esc_attr_e('Move down'); ?>">↓</abbr></a>
121 <a class="item-edit" id="edit-<?php echo $item_id; ?>" title="<?php esc_attr_e('Edit Menu Item'); ?>" href="<?php
122 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 ) ) );
123 ?>"><?php _e( 'Edit Menu Item' ); ?></a>
128 <div class="menu-item-settings" id="menu-item-settings-<?php echo $item_id; ?>">
129 <?php if( 'custom' == $item->type ) : ?>
130 <p class="field-url description description-wide">
131 <label for="edit-menu-item-url-<?php echo $item_id; ?>">
132 <?php _e( 'URL' ); ?><br />
133 <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 ); ?>" />
137 <p class="description description-thin">
138 <label for="edit-menu-item-title-<?php echo $item_id; ?>">
139 <?php _e( 'Navigation Label' ); ?><br />
140 <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 ); ?>" />
143 <p class="description description-thin">
144 <label for="edit-menu-item-attr-title-<?php echo $item_id; ?>">
145 <?php _e( 'Title Attribute' ); ?><br />
146 <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 ); ?>" />
149 <p class="field-link-target description">
150 <label for="edit-menu-item-target-<?php echo $item_id; ?>">
151 <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' ); ?> />
152 <?php _e( 'Open link in a new window/tab' ); ?>
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 ) ); ?>" />
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 ); ?>" />
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>
175 <p class="field-move hide-if-no-js description description-wide">
177 <span><?php _e( 'Move' ); ?></span>
178 <a href="#" class="menus-move-up"><?php _e( 'Up one' ); ?></a>
179 <a href="#" class="menus-move-down"><?php _e( 'Down one' ); ?></a>
180 <a href="#" class="menus-move-left"></a>
181 <a href="#" class="menus-move-right"></a>
182 <a href="#" class="menus-move-top"><?php _e( 'To the top' ); ?></a>
186 <div class="menu-item-actions description-wide submitbox">
187 <?php if( 'custom' != $item->type && $original_title !== false ) : ?>
188 <p class="link-to-original">
189 <?php printf( __('Original: %s'), '<a href="' . esc_attr( $item->url ) . '">' . esc_html( $original_title ) . '</a>' ); ?>
192 <a class="item-delete submitdelete deletion" id="delete-<?php echo $item_id; ?>" href="<?php
196 'action' => 'delete-menu-item',
197 'menu-item' => $item_id,
199 admin_url( 'nav-menus.php' )
201 'delete-menu_item_' . $item_id
202 ); ?>"><?php _e( 'Remove' ); ?></a> <span class="meta-sep hide-if-no-js"> | </span> <a class="item-cancel submitcancel hide-if-no-js" id="cancel-<?php echo $item_id; ?>" href="<?php echo esc_url( add_query_arg( array( 'edit-menu-item' => $item_id, 'cancel' => time() ), admin_url( 'nav-menus.php' ) ) );
203 ?>#menu-item-settings-<?php echo $item_id; ?>"><?php _e('Cancel'); ?></a>
206 <input class="menu-item-data-db-id" type="hidden" name="menu-item-db-id[<?php echo $item_id; ?>]" value="<?php echo $item_id; ?>" />
207 <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 ); ?>" />
208 <input class="menu-item-data-object" type="hidden" name="menu-item-object[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object ); ?>" />
209 <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 ); ?>" />
210 <input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_order ); ?>" />
211 <input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->type ); ?>" />
212 </div><!-- .menu-item-settings-->
213 <ul class="menu-item-transport"></ul>
215 $output .= ob_get_clean();
220 * Create HTML list of nav menu input items.
224 * @uses Walker_Nav_Menu
226 class Walker_Nav_Menu_Checklist extends Walker_Nav_Menu {
227 function __construct( $fields = false ) {
229 $this->db_fields = $fields;
233 function start_lvl( &$output, $depth = 0, $args = array() ) {
234 $indent = str_repeat( "\t", $depth );
235 $output .= "\n$indent<ul class='children'>\n";
238 function end_lvl( &$output, $depth = 0, $args = array() ) {
239 $indent = str_repeat( "\t", $depth );
240 $output .= "\n$indent</ul>";
244 * @see Walker::start_el()
247 * @param string $output Passed by reference. Used to append additional content.
248 * @param object $item Menu item data object.
249 * @param int $depth Depth of menu item. Used for padding.
250 * @param object $args
252 function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
253 global $_nav_menu_placeholder;
255 $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1;
256 $possible_object_id = isset( $item->post_type ) && 'nav_menu_item' == $item->post_type ? $item->object_id : $_nav_menu_placeholder;
257 $possible_db_id = ( ! empty( $item->ID ) ) && ( 0 < $possible_object_id ) ? (int) $item->ID : 0;
259 $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
261 $output .= $indent . '<li>';
262 $output .= '<label class="menu-item-title">';
263 $output .= '<input type="checkbox" class="menu-item-checkbox';
264 if ( property_exists( $item, 'front_or_home' ) && $item->front_or_home ) {
265 $title = sprintf( _x( 'Home: %s', 'nav menu front page title' ), $item->post_title );
266 $output .= ' add-to-top';
267 } elseif ( property_exists( $item, 'label' ) ) {
268 $title = $item->label;
270 $output .= '" name="menu-item[' . $possible_object_id . '][menu-item-object-id]" value="'. esc_attr( $item->object_id ) .'" /> ';
271 $output .= isset( $title ) ? esc_html( $title ) : esc_html( $item->title );
272 $output .= '</label>';
274 // Menu item hidden fields
275 $output .= '<input type="hidden" class="menu-item-db-id" name="menu-item[' . $possible_object_id . '][menu-item-db-id]" value="' . $possible_db_id . '" />';
276 $output .= '<input type="hidden" class="menu-item-object" name="menu-item[' . $possible_object_id . '][menu-item-object]" value="'. esc_attr( $item->object ) .'" />';
277 $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 ) .'" />';
278 $output .= '<input type="hidden" class="menu-item-type" name="menu-item[' . $possible_object_id . '][menu-item-type]" value="'. esc_attr( $item->type ) .'" />';
279 $output .= '<input type="hidden" class="menu-item-title" name="menu-item[' . $possible_object_id . '][menu-item-title]" value="'. esc_attr( $item->title ) .'" />';
280 $output .= '<input type="hidden" class="menu-item-url" name="menu-item[' . $possible_object_id . '][menu-item-url]" value="'. esc_attr( $item->url ) .'" />';
281 $output .= '<input type="hidden" class="menu-item-target" name="menu-item[' . $possible_object_id . '][menu-item-target]" value="'. esc_attr( $item->target ) .'" />';
282 $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 ) .'" />';
283 $output .= '<input type="hidden" class="menu-item-classes" name="menu-item[' . $possible_object_id . '][menu-item-classes]" value="'. esc_attr( implode( ' ', $item->classes ) ) .'" />';
284 $output .= '<input type="hidden" class="menu-item-xfn" name="menu-item[' . $possible_object_id . '][menu-item-xfn]" value="'. esc_attr( $item->xfn ) .'" />';
289 * Prints the appropriate response to a menu quick search.
293 * @param array $request The unsanitized request values.
295 function _wp_ajax_menu_quick_search( $request = array() ) {
297 $type = isset( $request['type'] ) ? $request['type'] : '';
298 $object_type = isset( $request['object_type'] ) ? $request['object_type'] : '';
299 $query = isset( $request['q'] ) ? $request['q'] : '';
300 $response_format = isset( $request['response-format'] ) && in_array( $request['response-format'], array( 'json', 'markup' ) ) ? $request['response-format'] : 'json';
302 if ( 'markup' == $response_format ) {
303 $args['walker'] = new Walker_Nav_Menu_Checklist;
306 if ( 'get-post-item' == $type ) {
307 if ( post_type_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_post( $object_id ) ) ), 0, (object) $args );
312 } elseif ( 'json' == $response_format ) {
313 $post_obj = get_post( $object_id );
317 'post_title' => get_the_title( $object_id ),
318 'post_type' => get_post_type( $object_id ),
324 } elseif ( taxonomy_exists( $object_type ) ) {
325 if ( isset( $request['ID'] ) ) {
326 $object_id = (int) $request['ID'];
327 if ( 'markup' == $response_format ) {
328 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_term( $object_id, $object_type ) ) ), 0, (object) $args );
329 } elseif ( 'json' == $response_format ) {
330 $post_obj = get_term( $object_id, $object_type );
334 'post_title' => $post_obj->name,
335 'post_type' => $object_type,
344 } elseif ( preg_match('/quick-search-(posttype|taxonomy)-([a-zA-Z_-]*\b)/', $type, $matches) ) {
345 if ( 'posttype' == $matches[1] && get_post_type_object( $matches[2] ) ) {
347 'posts_per_page' => 10,
348 'post_type' => $matches[2],
351 if ( ! have_posts() )
353 while ( have_posts() ) {
355 if ( 'markup' == $response_format ) {
356 $var_by_ref = get_the_ID();
357 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_post( $var_by_ref ) ) ), 0, (object) $args );
358 } elseif ( 'json' == $response_format ) {
361 'ID' => get_the_ID(),
362 'post_title' => get_the_title(),
363 'post_type' => get_post_type(),
369 } elseif ( 'taxonomy' == $matches[1] ) {
370 $terms = get_terms( $matches[2], array(
371 'name__like' => $query,
374 if ( empty( $terms ) || is_wp_error( $terms ) )
376 foreach( (array) $terms as $term ) {
377 if ( 'markup' == $response_format ) {
378 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( $term ) ), 0, (object) $args );
379 } elseif ( 'json' == $response_format ) {
382 'ID' => $term->term_id,
383 'post_title' => $term->name,
384 'post_type' => $matches[2],
395 * Register nav menu metaboxes and advanced menu items
399 function wp_nav_menu_setup() {
400 // Register meta boxes
401 wp_nav_menu_post_type_meta_boxes();
402 add_meta_box( 'add-custom-links', __( 'Links' ), 'wp_nav_menu_item_link_meta_box', 'nav-menus', 'side', 'default' );
403 wp_nav_menu_taxonomy_meta_boxes();
405 // Register advanced menu items (columns)
406 add_filter( 'manage_nav-menus_columns', 'wp_nav_menu_manage_columns' );
408 // If first time editing, disable advanced items by default.
409 if( false === get_user_option( 'managenav-menuscolumnshidden' ) ) {
410 $user = wp_get_current_user();
411 update_user_option($user->ID, 'managenav-menuscolumnshidden',
412 array( 0 => 'link-target', 1 => 'css-classes', 2 => 'xfn', 3 => 'description', ),
418 * Limit the amount of meta boxes to just links, pages and cats for first time users.
422 function wp_initial_nav_menu_meta_boxes() {
423 global $wp_meta_boxes;
425 if ( get_user_option( 'metaboxhidden_nav-menus' ) !== false || ! is_array($wp_meta_boxes) )
428 $initial_meta_boxes = array( 'nav-menu-theme-locations', 'add-page', 'add-custom-links', 'add-category' );
429 $hidden_meta_boxes = array();
431 foreach ( array_keys($wp_meta_boxes['nav-menus']) as $context ) {
432 foreach ( array_keys($wp_meta_boxes['nav-menus'][$context]) as $priority ) {
433 foreach ( $wp_meta_boxes['nav-menus'][$context][$priority] as $box ) {
434 if ( in_array( $box['id'], $initial_meta_boxes ) ) {
437 $hidden_meta_boxes[] = $box['id'];
443 $user = wp_get_current_user();
444 update_user_option( $user->ID, 'metaboxhidden_nav-menus', $hidden_meta_boxes, true );
448 * Creates metaboxes for any post type menu item.
452 function wp_nav_menu_post_type_meta_boxes() {
453 $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
458 foreach ( $post_types as $post_type ) {
459 $post_type = apply_filters( 'nav_menu_meta_box_object', $post_type );
461 $id = $post_type->name;
462 // give pages a higher priority
463 $priority = ( 'page' == $post_type->name ? 'core' : 'default' );
464 add_meta_box( "add-{$id}", $post_type->labels->name, 'wp_nav_menu_item_post_type_meta_box', 'nav-menus', 'side', $priority, $post_type );
470 * Creates metaboxes for any taxonomy menu item.
474 function wp_nav_menu_taxonomy_meta_boxes() {
475 $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'object' );
480 foreach ( $taxonomies as $tax ) {
481 $tax = apply_filters( 'nav_menu_meta_box_object', $tax );
484 add_meta_box( "add-{$id}", $tax->labels->name, 'wp_nav_menu_item_taxonomy_meta_box', 'nav-menus', 'side', 'default', $tax );
490 * Displays a metabox for the nav menu theme locations.
494 function wp_nav_menu_locations_meta_box() {
495 global $nav_menu_selected_id;
497 if ( ! current_theme_supports( 'menus' ) ) {
498 // We must only support widgets. Leave a message and bail.
499 echo '<p class="howto">' . __('The current theme does not natively support menus, but you can use the “Custom Menu” widget to add any menus you create here to the theme’s sidebar.') . '</p>';
503 $locations = get_registered_nav_menus();
504 $menus = wp_get_nav_menus();
505 $menu_locations = get_nav_menu_locations();
506 $num_locations = count( array_keys($locations) );
508 echo '<p class="howto">' . _n( 'Select a menu to use within your theme.', 'Select the menus you will use in your theme.', $num_locations ) . '</p>';
510 foreach ( $locations as $location => $description ) {
513 <label class="howto" for="locations-<?php echo $location; ?>">
514 <span><?php echo $description; ?></span>
515 <select name="menu-locations[<?php echo $location; ?>]" id="locations-<?php echo $location; ?>">
516 <option value="0"></option>
517 <?php foreach ( $menus as $menu ) : ?>
518 <option<?php selected( isset( $menu_locations[ $location ] ) && $menu_locations[ $location ] == $menu->term_id ); ?>
519 value="<?php echo $menu->term_id; ?>"><?php echo wp_html_excerpt( $menu->name, 40, '…' ); ?></option>
527 <p class="button-controls">
528 <?php submit_button( __( 'Save' ), 'primary right', 'nav-menu-locations', false, wp_nav_menu_disabled_check( $nav_menu_selected_id ) ); ?>
529 <span class="spinner"></span>
535 * Check whether to disable the Menu Locations meta box submit button
539 * @uses global $one_theme_location_no_menus to determine if no menus exist
540 * @uses disabled() to output the disabled attribute in $other_attributes param in submit_button()
542 * @param int|string $nav_menu_selected_id (id, name or slug) of the currently-selected menu
543 * @return string Disabled attribute if at least one menu exists, false if not
545 function wp_nav_menu_disabled_check( $nav_menu_selected_id ) {
546 global $one_theme_location_no_menus;
548 if ( $one_theme_location_no_menus )
551 return disabled( $nav_menu_selected_id, 0 );
555 * Displays a metabox for the custom links menu item.
559 function wp_nav_menu_item_link_meta_box() {
560 global $_nav_menu_placeholder, $nav_menu_selected_id;
562 $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;
565 <div class="customlinkdiv" id="customlinkdiv">
566 <input type="hidden" value="custom" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-type]" />
567 <p id="menu-item-url-wrap">
568 <label class="howto" for="custom-menu-item-url">
569 <span><?php _e('URL'); ?></span>
570 <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://" />
574 <p id="menu-item-name-wrap">
575 <label class="howto" for="custom-menu-item-name">
576 <span><?php _e( 'Link Text' ); ?></span>
577 <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'); ?>" />
581 <p class="button-controls">
582 <span class="add-to-menu">
583 <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e('Add to Menu'); ?>" name="add-custom-menu-item" id="submit-customlinkdiv" />
584 <span class="spinner"></span>
588 </div><!-- /.customlinkdiv -->
593 * Displays a metabox for a post type menu item.
597 * @param string $object Not used.
598 * @param string $post_type The post type object.
600 function wp_nav_menu_item_post_type_meta_box( $object, $post_type ) {
601 global $_nav_menu_placeholder, $nav_menu_selected_id;
603 $post_type_name = $post_type['args']->name;
605 // paginate browsing for large numbers of post objects
607 $pagenum = isset( $_REQUEST[$post_type_name . '-tab'] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
608 $offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;
613 'orderby' => 'title',
614 'posts_per_page' => $per_page,
615 'post_type' => $post_type_name,
616 'suppress_filters' => true,
617 'update_post_term_cache' => false,
618 'update_post_meta_cache' => false
621 if ( isset( $post_type['args']->_default_query ) )
622 $args = array_merge($args, (array) $post_type['args']->_default_query );
624 // @todo transient caching of these results with proper invalidation on updating of a post of this type
625 $get_posts = new WP_Query;
626 $posts = $get_posts->query( $args );
627 if ( ! $get_posts->post_count ) {
628 echo '<p>' . __( 'No items.' ) . '</p>';
632 $post_type_object = get_post_type_object($post_type_name);
634 $num_pages = $get_posts->max_num_pages;
636 $page_links = paginate_links( array(
637 'base' => add_query_arg(
639 $post_type_name . '-tab' => 'all',
641 'item-type' => 'post_type',
642 'item-object' => $post_type_name,
646 'prev_text' => __('«'),
647 'next_text' => __('»'),
648 'total' => $num_pages,
649 'current' => $pagenum
653 $error = '<li id="error">'. $post_type['args']->labels->not_found .'</li>';
656 if ( is_post_type_hierarchical( $post_type_name ) ) {
657 $db_fields = array( 'parent' => 'post_parent', 'id' => 'ID' );
660 $walker = new Walker_Nav_Menu_Checklist( $db_fields );
662 $current_tab = 'most-recent';
663 if ( isset( $_REQUEST[$post_type_name . '-tab'] ) && in_array( $_REQUEST[$post_type_name . '-tab'], array('all', 'search') ) ) {
664 $current_tab = $_REQUEST[$post_type_name . '-tab'];
667 if ( ! empty( $_REQUEST['quick-search-posttype-' . $post_type_name] ) ) {
668 $current_tab = 'search';
671 $removed_args = array(
681 <div id="posttype-<?php echo $post_type_name; ?>" class="posttypediv">
682 <ul id="posttype-<?php echo $post_type_name; ?>-tabs" class="posttype-tabs add-menu-item-tabs">
683 <li <?php echo ( 'most-recent' == $current_tab ? ' class="tabs"' : '' ); ?>>
684 <a class="nav-tab-link" data-type="tabs-panel-posttype-<?php echo esc_attr( $post_type_name ); ?>-most-recent" 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">
685 <?php _e( 'Most Recent' ); ?>
688 <li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>>
689 <a class="nav-tab-link" data-type="<?php echo esc_attr( $post_type_name ); ?>-all" 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">
690 <?php _e( 'View All' ); ?>
693 <li <?php echo ( 'search' == $current_tab ? ' class="tabs"' : '' ); ?>>
694 <a class="nav-tab-link" data-type="tabs-panel-posttype-<?php echo esc_attr( $post_type_name ); ?>-search" 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">
695 <?php _e( 'Search'); ?>
698 </ul><!-- .posttype-tabs -->
700 <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent" class="tabs-panel <?php
701 echo ( 'most-recent' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
703 <ul id="<?php echo $post_type_name; ?>checklist-most-recent" class="categorychecklist form-no-clear">
705 $recent_args = array_merge( $args, array( 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => 15 ) );
706 $most_recent = $get_posts->query( $recent_args );
707 $args['walker'] = $walker;
708 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $most_recent), 0, (object) $args );
711 </div><!-- /.tabs-panel -->
713 <div class="tabs-panel <?php
714 echo ( 'search' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
715 ?>" id="tabs-panel-posttype-<?php echo $post_type_name; ?>-search">
717 if ( isset( $_REQUEST['quick-search-posttype-' . $post_type_name] ) ) {
718 $searched = esc_attr( $_REQUEST['quick-search-posttype-' . $post_type_name] );
719 $search_results = get_posts( array( 's' => $searched, 'post_type' => $post_type_name, 'fields' => 'all', 'order' => 'DESC', ) );
722 $search_results = array();
725 <p class="quick-search-wrap">
726 <input type="search" class="quick-search input-with-default-title" title="<?php esc_attr_e('Search'); ?>" value="<?php echo $searched; ?>" name="quick-search-posttype-<?php echo $post_type_name; ?>" />
727 <span class="spinner"></span>
728 <?php submit_button( __( 'Search' ), 'button-small quick-search-submit button-secondary hide-if-js', 'submit', false, array( 'id' => 'submit-quick-search-posttype-' . $post_type_name ) ); ?>
731 <ul id="<?php echo $post_type_name; ?>-search-checklist" data-wp-lists="list:<?php echo $post_type_name?>" class="categorychecklist form-no-clear">
732 <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?>
734 $args['walker'] = $walker;
735 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args );
737 <?php elseif ( is_wp_error( $search_results ) ) : ?>
738 <li><?php echo $search_results->get_error_message(); ?></li>
739 <?php elseif ( ! empty( $searched ) ) : ?>
740 <li><?php _e('No results found.'); ?></li>
743 </div><!-- /.tabs-panel -->
745 <div id="<?php echo $post_type_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php
746 echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
748 <?php if ( ! empty( $page_links ) ) : ?>
749 <div class="add-menu-item-pagelinks">
750 <?php echo $page_links; ?>
753 <ul id="<?php echo $post_type_name; ?>checklist" data-wp-lists="list:<?php echo $post_type_name?>" class="categorychecklist form-no-clear">
755 $args['walker'] = $walker;
757 // if we're dealing with pages, let's put a checkbox for the front page at the top of the list
758 if ( 'page' == $post_type_name ) {
759 $front_page = 'page' == get_option('show_on_front') ? (int) get_option( 'page_on_front' ) : 0;
760 if ( ! empty( $front_page ) ) {
761 $front_page_obj = get_post( $front_page );
762 $front_page_obj->front_or_home = true;
763 array_unshift( $posts, $front_page_obj );
765 $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1;
766 array_unshift( $posts, (object) array(
767 'front_or_home' => true,
769 'object_id' => $_nav_menu_placeholder,
770 'post_content' => '',
771 'post_excerpt' => '',
773 'post_title' => _x('Home', 'nav menu home label'),
774 'post_type' => 'nav_menu_item',
776 'url' => home_url('/'),
781 $posts = apply_filters( 'nav_menu_items_'.$post_type_name, $posts, $args, $post_type );
782 $checkbox_items = walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $posts), 0, (object) $args );
784 if ( 'all' == $current_tab && ! empty( $_REQUEST['selectall'] ) ) {
785 $checkbox_items = preg_replace('/(type=(.)checkbox(\2))/', '$1 checked=$2checked$2', $checkbox_items);
789 echo $checkbox_items;
792 <?php if ( ! empty( $page_links ) ) : ?>
793 <div class="add-menu-item-pagelinks">
794 <?php echo $page_links; ?>
797 </div><!-- /.tabs-panel -->
799 <p class="button-controls">
800 <span class="list-controls">
802 echo esc_url( add_query_arg(
804 $post_type_name . '-tab' => 'all',
807 remove_query_arg( $removed_args )
809 ?>#posttype-<?php echo $post_type_name; ?>" class="select-all"><?php _e('Select All'); ?></a>
812 <span class="add-to-menu">
813 <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( __( 'Add to Menu' ) ); ?>" name="add-post-type-menu-item" id="<?php esc_attr_e( 'submit-posttype-' . $post_type_name ); ?>" />
814 <span class="spinner"></span>
818 </div><!-- /.posttypediv -->
823 * Displays a metabox for a taxonomy menu item.
827 * @param string $object Not used.
828 * @param string $taxonomy The taxonomy object.
830 function wp_nav_menu_item_taxonomy_meta_box( $object, $taxonomy ) {
831 global $nav_menu_selected_id;
832 $taxonomy_name = $taxonomy['args']->name;
834 // paginate browsing for large numbers of objects
836 $pagenum = isset( $_REQUEST[$taxonomy_name . '-tab'] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
837 $offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;
842 'hide_empty' => false,
845 'number' => $per_page,
849 'pad_counts' => false,
852 $terms = get_terms( $taxonomy_name, $args );
854 if ( ! $terms || is_wp_error($terms) ) {
855 echo '<p>' . __( 'No items.' ) . '</p>';
859 $num_pages = ceil( wp_count_terms( $taxonomy_name , array_merge( $args, array('number' => '', 'offset' => '') ) ) / $per_page );
861 $page_links = paginate_links( array(
862 'base' => add_query_arg(
864 $taxonomy_name . '-tab' => 'all',
866 'item-type' => 'taxonomy',
867 'item-object' => $taxonomy_name,
871 'prev_text' => __('«'),
872 'next_text' => __('»'),
873 'total' => $num_pages,
874 'current' => $pagenum
878 if ( is_taxonomy_hierarchical( $taxonomy_name ) ) {
879 $db_fields = array( 'parent' => 'parent', 'id' => 'term_id' );
882 $walker = new Walker_Nav_Menu_Checklist( $db_fields );
884 $current_tab = 'most-used';
885 if ( isset( $_REQUEST[$taxonomy_name . '-tab'] ) && in_array( $_REQUEST[$taxonomy_name . '-tab'], array('all', 'most-used', 'search') ) ) {
886 $current_tab = $_REQUEST[$taxonomy_name . '-tab'];
889 if ( ! empty( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] ) ) {
890 $current_tab = 'search';
893 $removed_args = array(
903 <div id="taxonomy-<?php echo $taxonomy_name; ?>" class="taxonomydiv">
904 <ul id="taxonomy-<?php echo $taxonomy_name; ?>-tabs" class="taxonomy-tabs add-menu-item-tabs">
905 <li <?php echo ( 'most-used' == $current_tab ? ' class="tabs"' : '' ); ?>>
906 <a class="nav-tab-link" data-type="tabs-panel-<?php echo esc_attr( $taxonomy_name ); ?>-pop" 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">
907 <?php _e( 'Most Used' ); ?>
910 <li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>>
911 <a class="nav-tab-link" data-type="tabs-panel-<?php echo esc_attr( $taxonomy_name ); ?>-all" 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">
912 <?php _e( 'View All' ); ?>
915 <li <?php echo ( 'search' == $current_tab ? ' class="tabs"' : '' ); ?>>
916 <a class="nav-tab-link" data-type="tabs-panel-search-taxonomy-<?php echo esc_attr( $taxonomy_name ); ?>" 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; ?>">
917 <?php _e( 'Search' ); ?>
920 </ul><!-- .taxonomy-tabs -->
922 <div id="tabs-panel-<?php echo $taxonomy_name; ?>-pop" class="tabs-panel <?php
923 echo ( 'most-used' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
925 <ul id="<?php echo $taxonomy_name; ?>checklist-pop" class="categorychecklist form-no-clear" >
927 $popular_terms = get_terms( $taxonomy_name, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
928 $args['walker'] = $walker;
929 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $popular_terms), 0, (object) $args );
932 </div><!-- /.tabs-panel -->
934 <div id="tabs-panel-<?php echo $taxonomy_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php
935 echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
937 <?php if ( ! empty( $page_links ) ) : ?>
938 <div class="add-menu-item-pagelinks">
939 <?php echo $page_links; ?>
942 <ul id="<?php echo $taxonomy_name; ?>checklist" data-wp-lists="list:<?php echo $taxonomy_name?>" class="categorychecklist form-no-clear">
944 $args['walker'] = $walker;
945 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $terms), 0, (object) $args );
948 <?php if ( ! empty( $page_links ) ) : ?>
949 <div class="add-menu-item-pagelinks">
950 <?php echo $page_links; ?>
953 </div><!-- /.tabs-panel -->
955 <div class="tabs-panel <?php
956 echo ( 'search' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
957 ?>" id="tabs-panel-search-taxonomy-<?php echo $taxonomy_name; ?>">
959 if ( isset( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] ) ) {
960 $searched = esc_attr( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] );
961 $search_results = get_terms( $taxonomy_name, array( 'name__like' => $searched, 'fields' => 'all', 'orderby' => 'count', 'order' => 'DESC', 'hierarchical' => false ) );
964 $search_results = array();
967 <p class="quick-search-wrap">
968 <input type="search" class="quick-search input-with-default-title" title="<?php esc_attr_e('Search'); ?>" value="<?php echo $searched; ?>" name="quick-search-taxonomy-<?php echo $taxonomy_name; ?>" />
969 <span class="spinner"></span>
970 <?php submit_button( __( 'Search' ), 'button-small quick-search-submit button-secondary hide-if-js', 'submit', false, array( 'id' => 'submit-quick-search-taxonomy-' . $taxonomy_name ) ); ?>
973 <ul id="<?php echo $taxonomy_name; ?>-search-checklist" data-wp-lists="list:<?php echo $taxonomy_name?>" class="categorychecklist form-no-clear">
974 <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?>
976 $args['walker'] = $walker;
977 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args );
979 <?php elseif ( is_wp_error( $search_results ) ) : ?>
980 <li><?php echo $search_results->get_error_message(); ?></li>
981 <?php elseif ( ! empty( $searched ) ) : ?>
982 <li><?php _e('No results found.'); ?></li>
985 </div><!-- /.tabs-panel -->
987 <p class="button-controls">
988 <span class="list-controls">
990 echo esc_url(add_query_arg(
992 $taxonomy_name . '-tab' => 'all',
995 remove_query_arg($removed_args)
997 ?>#taxonomy-<?php echo $taxonomy_name; ?>" class="select-all"><?php _e('Select All'); ?></a>
1000 <span class="add-to-menu">
1001 <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( __( 'Add to Menu' ) ); ?>" name="add-taxonomy-menu-item" id="<?php esc_attr_e( 'submit-taxonomy-' . $taxonomy_name ); ?>" />
1002 <span class="spinner"></span>
1006 </div><!-- /.taxonomydiv -->
1011 * Save posted nav menu item data.
1015 * @param int $menu_id The menu ID for which to save this item. $menu_id of 0 makes a draft, orphaned menu item.
1016 * @param array $menu_data The unsanitized posted menu item data.
1017 * @return array The database IDs of the items saved
1019 function wp_save_nav_menu_items( $menu_id = 0, $menu_data = array() ) {
1020 $menu_id = (int) $menu_id;
1021 $items_saved = array();
1023 if ( 0 == $menu_id || is_nav_menu( $menu_id ) ) {
1025 // Loop through all the menu items' POST values
1026 foreach( (array) $menu_data as $_possible_db_id => $_item_object_data ) {
1028 empty( $_item_object_data['menu-item-object-id'] ) && // checkbox is not checked
1030 ! isset( $_item_object_data['menu-item-type'] ) || // and item type either isn't set
1031 in_array( $_item_object_data['menu-item-url'], array( 'http://', '' ) ) || // or URL is the default
1032 ! ( '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)
1033 ! empty( $_item_object_data['menu-item-db-id'] ) // or it *is* a custom menu item that already exists
1036 continue; // then this potential menu item is not getting added to this menu
1039 // if this possible menu item doesn't actually have a menu database ID yet
1041 empty( $_item_object_data['menu-item-db-id'] ) ||
1042 ( 0 > $_possible_db_id ) ||
1043 $_possible_db_id != $_item_object_data['menu-item-db-id']
1047 $_actual_db_id = (int) $_item_object_data['menu-item-db-id'];
1051 'menu-item-db-id' => ( isset( $_item_object_data['menu-item-db-id'] ) ? $_item_object_data['menu-item-db-id'] : '' ),
1052 'menu-item-object-id' => ( isset( $_item_object_data['menu-item-object-id'] ) ? $_item_object_data['menu-item-object-id'] : '' ),
1053 'menu-item-object' => ( isset( $_item_object_data['menu-item-object'] ) ? $_item_object_data['menu-item-object'] : '' ),
1054 'menu-item-parent-id' => ( isset( $_item_object_data['menu-item-parent-id'] ) ? $_item_object_data['menu-item-parent-id'] : '' ),
1055 'menu-item-position' => ( isset( $_item_object_data['menu-item-position'] ) ? $_item_object_data['menu-item-position'] : '' ),
1056 'menu-item-type' => ( isset( $_item_object_data['menu-item-type'] ) ? $_item_object_data['menu-item-type'] : '' ),
1057 'menu-item-title' => ( isset( $_item_object_data['menu-item-title'] ) ? $_item_object_data['menu-item-title'] : '' ),
1058 'menu-item-url' => ( isset( $_item_object_data['menu-item-url'] ) ? $_item_object_data['menu-item-url'] : '' ),
1059 'menu-item-description' => ( isset( $_item_object_data['menu-item-description'] ) ? $_item_object_data['menu-item-description'] : '' ),
1060 'menu-item-attr-title' => ( isset( $_item_object_data['menu-item-attr-title'] ) ? $_item_object_data['menu-item-attr-title'] : '' ),
1061 'menu-item-target' => ( isset( $_item_object_data['menu-item-target'] ) ? $_item_object_data['menu-item-target'] : '' ),
1062 'menu-item-classes' => ( isset( $_item_object_data['menu-item-classes'] ) ? $_item_object_data['menu-item-classes'] : '' ),
1063 'menu-item-xfn' => ( isset( $_item_object_data['menu-item-xfn'] ) ? $_item_object_data['menu-item-xfn'] : '' ),
1066 $items_saved[] = wp_update_nav_menu_item( $menu_id, $_actual_db_id, $args );
1070 return $items_saved;
1074 * Adds custom arguments to some of the meta box object types.
1080 * @param object $object The post type or taxonomy meta-object.
1081 * @return object The post type of taxonomy object.
1083 function _wp_nav_menu_meta_box_object( $object = null ) {
1084 if ( isset( $object->name ) ) {
1086 if ( 'page' == $object->name ) {
1087 $object->_default_query = array(
1088 'orderby' => 'menu_order title',
1089 'post_status' => 'publish',
1092 // posts should show only published items
1093 } elseif ( 'post' == $object->name ) {
1094 $object->_default_query = array(
1095 'post_status' => 'publish',
1098 // cats should be in reverse chronological order
1099 } elseif ( 'category' == $object->name ) {
1100 $object->_default_query = array(
1105 // custom post types should show only published items
1107 $object->_default_query = array(
1108 'post_status' => 'publish',
1117 * Returns the menu formatted to edit.
1121 * @param string $menu_id The ID of the menu to format.
1122 * @return string|WP_Error $output The menu formatted to edit or error object on failure.
1124 function wp_get_nav_menu_to_edit( $menu_id = 0 ) {
1125 $menu = wp_get_nav_menu_object( $menu_id );
1127 // If the menu exists, get its items.
1128 if ( is_nav_menu( $menu ) ) {
1129 $menu_items = wp_get_nav_menu_items( $menu->term_id, array('post_status' => 'any') );
1130 $result = '<div id="menu-instructions" class="post-body-plain';
1131 $result .= ( ! empty($menu_items) ) ? ' menu-instructions-inactive">' : '">';
1132 $result .= '<p>' . __( 'Add menu items from the column on the left.' ) . '</p>';
1133 $result .= '</div>';
1135 if( empty($menu_items) )
1136 return $result . ' <ul class="menu" id="menu-to-edit"> </ul>';
1138 $walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $menu_id );
1140 if ( class_exists( $walker_class_name ) )
1141 $walker = new $walker_class_name;
1143 return new WP_Error( 'menu_walker_not_exist', sprintf( __('The Walker class named <strong>%s</strong> does not exist.'), $walker_class_name ) );
1145 $some_pending_menu_items = $some_invalid_menu_items = false;
1146 foreach( (array) $menu_items as $menu_item ) {
1147 if ( isset( $menu_item->post_status ) && 'draft' == $menu_item->post_status )
1148 $some_pending_menu_items = true;
1149 if ( ! empty( $menu_item->_invalid ) )
1150 $some_invalid_menu_items = true;
1153 if ( $some_pending_menu_items )
1154 $result .= '<div class="updated inline"><p>' . __('Click Save Menu to make pending menu items public.') . '</p></div>';
1156 if ( $some_invalid_menu_items )
1157 $result .= '<div class="error inline"><p>' . __('There are some invalid menu items. Please check or delete them.') . '</p></div>';
1159 $result .= '<ul class="menu" id="menu-to-edit"> ';
1160 $result .= walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $menu_items), 0, (object) array('walker' => $walker ) );
1161 $result .= ' </ul> ';
1163 } elseif ( is_wp_error( $menu ) ) {
1170 * Returns the columns for the nav menus page.
1174 * @return string|WP_Error $output The menu formatted to edit or error object on failure.
1176 function wp_nav_menu_manage_columns() {
1178 '_title' => __('Show advanced menu properties'),
1179 'cb' => '<input type="checkbox" />',
1180 'link-target' => __('Link Target'),
1181 'css-classes' => __('CSS Classes'),
1182 'xfn' => __('Link Relationship (XFN)'),
1183 'description' => __('Description'),
1188 * Deletes orphaned draft menu items
1194 function _wp_delete_orphaned_draft_menu_items() {
1196 $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
1198 // delete orphaned draft menu items
1199 $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 ) );
1201 foreach( (array) $menu_items_to_delete as $menu_item_id )
1202 wp_delete_post( $menu_item_id, true );
1204 add_action('admin_head-nav-menus.php', '_wp_delete_orphaned_draft_menu_items');
1207 * Saves nav menu items
1211 * @uses wp_get_nav_menu_items() to retrieve the nav menu's menu items
1212 * @uses wp_defer_term_counter() to enable then disable term counting
1214 * @param int|string $nav_menu_selected_id (id, slug, or name ) of the currently-selected menu
1215 * @param string $nav_menu_selected_title Title of the currently-selected menu
1216 * @return array $messages The menu updated message
1218 function wp_nav_menu_update_menu_items ( $nav_menu_selected_id, $nav_menu_selected_title ) {
1219 $unsorted_menu_items = wp_get_nav_menu_items( $nav_menu_selected_id, array( 'orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID', 'post_status' => 'draft,publish' ) );
1221 $menu_items = array();
1222 // Index menu items by db ID
1223 foreach ( $unsorted_menu_items as $_item )
1224 $menu_items[$_item->db_id] = $_item;
1226 $post_fields = array(
1227 'menu-item-db-id', 'menu-item-object-id', 'menu-item-object',
1228 'menu-item-parent-id', 'menu-item-position', 'menu-item-type',
1229 'menu-item-title', 'menu-item-url', 'menu-item-description',
1230 'menu-item-attr-title', 'menu-item-target', 'menu-item-classes', 'menu-item-xfn'
1233 wp_defer_term_counting( true );
1234 // Loop through all the menu items' POST variables
1235 if ( ! empty( $_POST['menu-item-db-id'] ) ) {
1236 foreach( (array) $_POST['menu-item-db-id'] as $_key => $k ) {
1238 // Menu item title can't be blank
1239 if ( ! isset( $_POST['menu-item-title'][ $_key ] ) || '' == $_POST['menu-item-title'][ $_key ] )
1243 foreach ( $post_fields as $field )
1244 $args[$field] = isset( $_POST[$field][$_key] ) ? $_POST[$field][$_key] : '';
1246 $menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key ), $args );
1248 if ( is_wp_error( $menu_item_db_id ) )
1249 $messages[] = '<div id="message" class="error"><p>' . $menu_item_db_id->get_error_message() . '</p></div>';
1250 elseif ( isset( $menu_items[$menu_item_db_id] ) )
1251 unset( $menu_items[$menu_item_db_id] );
1255 // Remove menu items from the menu that weren't in $_POST
1256 if ( ! empty( $menu_items ) ) {
1257 foreach ( array_keys( $menu_items ) as $menu_item_id ) {
1258 if ( is_nav_menu_item( $menu_item_id ) ) {
1259 wp_delete_post( $menu_item_id );
1264 // Store 'auto-add' pages.
1265 $auto_add = ! empty( $_POST['auto-add-pages'] );
1266 $nav_menu_option = (array) get_option( 'nav_menu_options' );
1267 if ( ! isset( $nav_menu_option['auto_add'] ) )
1268 $nav_menu_option['auto_add'] = array();
1270 if ( ! in_array( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) )
1271 $nav_menu_option['auto_add'][] = $nav_menu_selected_id;
1273 if ( false !== ( $key = array_search( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) ) )
1274 unset( $nav_menu_option['auto_add'][$key] );
1276 // Remove nonexistent/deleted menus
1277 $nav_menu_option['auto_add'] = array_intersect( $nav_menu_option['auto_add'], wp_get_nav_menus( array( 'fields' => 'ids' ) ) );
1278 update_option( 'nav_menu_options', $nav_menu_option );
1280 wp_defer_term_counting( false );
1282 do_action( 'wp_update_nav_menu', $nav_menu_selected_id );
1284 $messages[] = '<div id="message" class="updated"><p>' . sprintf( __( '<strong>%1$s</strong> has been updated.' ), $nav_menu_selected_title ) . '</p></div>';
1285 unset( $menu_items, $unsorted_menu_items );