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 * Starts the list before the elements are added.
14 * @see Walker_Nav_Menu::start_lvl()
18 * @param string $output Passed by reference.
19 * @param int $depth Depth of menu item. Used for padding.
20 * @param array $args Not used.
22 public function start_lvl( &$output, $depth = 0, $args = array() ) {}
25 * Ends the list of after the elements are added.
27 * @see Walker_Nav_Menu::end_lvl()
31 * @param string $output Passed by reference.
32 * @param int $depth Depth of menu item. Used for padding.
33 * @param array $args Not used.
35 public function end_lvl( &$output, $depth = 0, $args = array() ) {}
38 * Start the element output.
40 * @see Walker_Nav_Menu::start_el()
43 * @global int $_wp_nav_menu_max_depth
45 * @param string $output Passed by reference. Used to append additional content.
46 * @param object $item Menu item data object.
47 * @param int $depth Depth of menu item. Used for padding.
48 * @param array $args Not used.
49 * @param int $id Not used.
51 public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
52 global $_wp_nav_menu_max_depth;
53 $_wp_nav_menu_max_depth = $depth > $_wp_nav_menu_max_depth ? $depth : $_wp_nav_menu_max_depth;
56 $item_id = esc_attr( $item->ID );
57 $removed_args = array(
67 if ( 'taxonomy' == $item->type ) {
68 $original_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
69 if ( is_wp_error( $original_title ) )
70 $original_title = false;
71 } elseif ( 'post_type' == $item->type ) {
72 $original_object = get_post( $item->object_id );
73 $original_title = get_the_title( $original_object->ID );
77 'menu-item menu-item-depth-' . $depth,
78 'menu-item-' . esc_attr( $item->object ),
79 'menu-item-edit-' . ( ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? 'active' : 'inactive'),
82 $title = $item->title;
84 if ( ! empty( $item->_invalid ) ) {
85 $classes[] = 'menu-item-invalid';
86 /* translators: %s: title of menu item which is invalid */
87 $title = sprintf( __( '%s (Invalid)' ), $item->title );
88 } elseif ( isset( $item->post_status ) && 'draft' == $item->post_status ) {
89 $classes[] = 'pending';
90 /* translators: %s: title of menu item in draft status */
91 $title = sprintf( __('%s (Pending)'), $item->title );
94 $title = ( ! isset( $item->label ) || '' == $item->label ) ? $title : $item->label;
98 $submenu_text = 'style="display: none;"';
101 <li id="menu-item-<?php echo $item_id; ?>" class="<?php echo implode(' ', $classes ); ?>">
102 <div class="menu-item-bar">
103 <div class="menu-item-handle">
104 <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>
105 <span class="item-controls">
106 <span class="item-type"><?php echo esc_html( $item->type_label ); ?></span>
107 <span class="item-order hide-if-js">
112 'action' => 'move-up-menu-item',
113 'menu-item' => $item_id,
115 remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
119 ?>" class="item-move-up"><abbr title="<?php esc_attr_e('Move up'); ?>">↑</abbr></a>
125 'action' => 'move-down-menu-item',
126 'menu-item' => $item_id,
128 remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
132 ?>" class="item-move-down"><abbr title="<?php esc_attr_e('Move down'); ?>">↓</abbr></a>
134 <a class="item-edit" id="edit-<?php echo $item_id; ?>" title="<?php esc_attr_e('Edit Menu Item'); ?>" href="<?php
135 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 ) ) );
136 ?>"><?php _e( 'Edit Menu Item' ); ?></a>
141 <div class="menu-item-settings" id="menu-item-settings-<?php echo $item_id; ?>">
142 <?php if ( 'custom' == $item->type ) : ?>
143 <p class="field-url description description-wide">
144 <label for="edit-menu-item-url-<?php echo $item_id; ?>">
145 <?php _e( 'URL' ); ?><br />
146 <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 ); ?>" />
150 <p class="description description-wide">
151 <label for="edit-menu-item-title-<?php echo $item_id; ?>">
152 <?php _e( 'Navigation Label' ); ?><br />
153 <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 ); ?>" />
156 <p class="field-title-attribute description description-wide">
157 <label for="edit-menu-item-attr-title-<?php echo $item_id; ?>">
158 <?php _e( 'Title Attribute' ); ?><br />
159 <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 ); ?>" />
162 <p class="field-link-target description">
163 <label for="edit-menu-item-target-<?php echo $item_id; ?>">
164 <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' ); ?> />
165 <?php _e( 'Open link in a new window/tab' ); ?>
168 <p class="field-css-classes description description-thin">
169 <label for="edit-menu-item-classes-<?php echo $item_id; ?>">
170 <?php _e( 'CSS Classes (optional)' ); ?><br />
171 <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 ) ); ?>" />
174 <p class="field-xfn description description-thin">
175 <label for="edit-menu-item-xfn-<?php echo $item_id; ?>">
176 <?php _e( 'Link Relationship (XFN)' ); ?><br />
177 <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 ); ?>" />
180 <p class="field-description description description-wide">
181 <label for="edit-menu-item-description-<?php echo $item_id; ?>">
182 <?php _e( 'Description' ); ?><br />
183 <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>
184 <span class="description"><?php _e('The description will be displayed in the menu if the current theme supports it.'); ?></span>
188 <p class="field-move hide-if-no-js description description-wide">
190 <span><?php _e( 'Move' ); ?></span>
191 <a href="#" class="menus-move menus-move-up" data-dir="up"><?php _e( 'Up one' ); ?></a>
192 <a href="#" class="menus-move menus-move-down" data-dir="down"><?php _e( 'Down one' ); ?></a>
193 <a href="#" class="menus-move menus-move-left" data-dir="left"></a>
194 <a href="#" class="menus-move menus-move-right" data-dir="right"></a>
195 <a href="#" class="menus-move menus-move-top" data-dir="top"><?php _e( 'To the top' ); ?></a>
199 <div class="menu-item-actions description-wide submitbox">
200 <?php if ( 'custom' != $item->type && $original_title !== false ) : ?>
201 <p class="link-to-original">
202 <?php printf( __('Original: %s'), '<a href="' . esc_attr( $item->url ) . '">' . esc_html( $original_title ) . '</a>' ); ?>
205 <a class="item-delete submitdelete deletion" id="delete-<?php echo $item_id; ?>" href="<?php
209 'action' => 'delete-menu-item',
210 'menu-item' => $item_id,
212 admin_url( 'nav-menus.php' )
214 'delete-menu_item_' . $item_id
215 ); ?>"><?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' ) ) );
216 ?>#menu-item-settings-<?php echo $item_id; ?>"><?php _e('Cancel'); ?></a>
219 <input class="menu-item-data-db-id" type="hidden" name="menu-item-db-id[<?php echo $item_id; ?>]" value="<?php echo $item_id; ?>" />
220 <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 ); ?>" />
221 <input class="menu-item-data-object" type="hidden" name="menu-item-object[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object ); ?>" />
222 <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 ); ?>" />
223 <input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_order ); ?>" />
224 <input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->type ); ?>" />
225 </div><!-- .menu-item-settings-->
226 <ul class="menu-item-transport"></ul>
228 $output .= ob_get_clean();
231 } // Walker_Nav_Menu_Edit
234 * Create HTML list of nav menu input items.
237 * @uses Walker_Nav_Menu
239 class Walker_Nav_Menu_Checklist extends Walker_Nav_Menu {
242 * @param array $fields
244 public function __construct( $fields = false ) {
246 $this->db_fields = $fields;
251 * Starts the list before the elements are added.
253 * @see Walker_Nav_Menu::start_lvl()
257 * @param string $output Passed by reference. Used to append additional content.
258 * @param int $depth Depth of page. Used for padding.
259 * @param array $args Not used.
261 public function start_lvl( &$output, $depth = 0, $args = array() ) {
262 $indent = str_repeat( "\t", $depth );
263 $output .= "\n$indent<ul class='children'>\n";
267 * Ends the list of after the elements are added.
269 * @see Walker_Nav_Menu::end_lvl()
273 * @param string $output Passed by reference. Used to append additional content.
274 * @param int $depth Depth of page. Used for padding.
275 * @param array $args Not used.
277 public function end_lvl( &$output, $depth = 0, $args = array() ) {
278 $indent = str_repeat( "\t", $depth );
279 $output .= "\n$indent</ul>";
283 * Start the element output.
285 * @see Walker_Nav_Menu::start_el()
289 * @global int $_nav_menu_placeholder
291 * @param string $output Passed by reference. Used to append additional content.
292 * @param object $item Menu item data object.
293 * @param int $depth Depth of menu item. Used for padding.
294 * @param array $args Not used.
295 * @param int $id Not used.
297 public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
298 global $_nav_menu_placeholder;
300 $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1;
301 $possible_object_id = isset( $item->post_type ) && 'nav_menu_item' == $item->post_type ? $item->object_id : $_nav_menu_placeholder;
302 $possible_db_id = ( ! empty( $item->ID ) ) && ( 0 < $possible_object_id ) ? (int) $item->ID : 0;
304 $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
306 $output .= $indent . '<li>';
307 $output .= '<label class="menu-item-title">';
308 $output .= '<input type="checkbox" class="menu-item-checkbox';
310 if ( ! empty( $item->front_or_home ) )
311 $output .= ' add-to-top';
313 $output .= '" name="menu-item[' . $possible_object_id . '][menu-item-object-id]" value="'. esc_attr( $item->object_id ) .'" /> ';
315 if ( ! empty( $item->label ) ) {
316 $title = $item->label;
317 } elseif ( isset( $item->post_type ) ) {
318 /** This filter is documented in wp-includes/post-template.php */
319 $title = apply_filters( 'the_title', $item->post_title, $item->ID );
320 if ( ! empty( $item->front_or_home ) && _x( 'Home', 'nav menu home label' ) !== $title )
321 $title = sprintf( _x( 'Home: %s', 'nav menu front page title' ), $title );
324 $output .= isset( $title ) ? esc_html( $title ) : esc_html( $item->title );
325 $output .= '</label>';
327 // Menu item hidden fields
328 $output .= '<input type="hidden" class="menu-item-db-id" name="menu-item[' . $possible_object_id . '][menu-item-db-id]" value="' . $possible_db_id . '" />';
329 $output .= '<input type="hidden" class="menu-item-object" name="menu-item[' . $possible_object_id . '][menu-item-object]" value="'. esc_attr( $item->object ) .'" />';
330 $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 ) .'" />';
331 $output .= '<input type="hidden" class="menu-item-type" name="menu-item[' . $possible_object_id . '][menu-item-type]" value="'. esc_attr( $item->type ) .'" />';
332 $output .= '<input type="hidden" class="menu-item-title" name="menu-item[' . $possible_object_id . '][menu-item-title]" value="'. esc_attr( $item->title ) .'" />';
333 $output .= '<input type="hidden" class="menu-item-url" name="menu-item[' . $possible_object_id . '][menu-item-url]" value="'. esc_attr( $item->url ) .'" />';
334 $output .= '<input type="hidden" class="menu-item-target" name="menu-item[' . $possible_object_id . '][menu-item-target]" value="'. esc_attr( $item->target ) .'" />';
335 $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 ) .'" />';
336 $output .= '<input type="hidden" class="menu-item-classes" name="menu-item[' . $possible_object_id . '][menu-item-classes]" value="'. esc_attr( implode( ' ', $item->classes ) ) .'" />';
337 $output .= '<input type="hidden" class="menu-item-xfn" name="menu-item[' . $possible_object_id . '][menu-item-xfn]" value="'. esc_attr( $item->xfn ) .'" />';
340 } // Walker_Nav_Menu_Checklist
343 * Prints the appropriate response to a menu quick search.
347 * @param array $request The unsanitized request values.
349 function _wp_ajax_menu_quick_search( $request = array() ) {
351 $type = isset( $request['type'] ) ? $request['type'] : '';
352 $object_type = isset( $request['object_type'] ) ? $request['object_type'] : '';
353 $query = isset( $request['q'] ) ? $request['q'] : '';
354 $response_format = isset( $request['response-format'] ) && in_array( $request['response-format'], array( 'json', 'markup' ) ) ? $request['response-format'] : 'json';
356 if ( 'markup' == $response_format ) {
357 $args['walker'] = new Walker_Nav_Menu_Checklist;
360 if ( 'get-post-item' == $type ) {
361 if ( post_type_exists( $object_type ) ) {
362 if ( isset( $request['ID'] ) ) {
363 $object_id = (int) $request['ID'];
364 if ( 'markup' == $response_format ) {
365 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_post( $object_id ) ) ), 0, (object) $args );
366 } elseif ( 'json' == $response_format ) {
370 'post_title' => get_the_title( $object_id ),
371 'post_type' => get_post_type( $object_id ),
377 } elseif ( taxonomy_exists( $object_type ) ) {
378 if ( isset( $request['ID'] ) ) {
379 $object_id = (int) $request['ID'];
380 if ( 'markup' == $response_format ) {
381 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_term( $object_id, $object_type ) ) ), 0, (object) $args );
382 } elseif ( 'json' == $response_format ) {
383 $post_obj = get_term( $object_id, $object_type );
387 'post_title' => $post_obj->name,
388 'post_type' => $object_type,
397 } elseif ( preg_match('/quick-search-(posttype|taxonomy)-([a-zA-Z_-]*\b)/', $type, $matches) ) {
398 if ( 'posttype' == $matches[1] && get_post_type_object( $matches[2] ) ) {
400 'posts_per_page' => 10,
401 'post_type' => $matches[2],
404 if ( ! have_posts() )
406 while ( have_posts() ) {
408 if ( 'markup' == $response_format ) {
409 $var_by_ref = get_the_ID();
410 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_post( $var_by_ref ) ) ), 0, (object) $args );
411 } elseif ( 'json' == $response_format ) {
414 'ID' => get_the_ID(),
415 'post_title' => get_the_title(),
416 'post_type' => get_post_type(),
422 } elseif ( 'taxonomy' == $matches[1] ) {
423 $terms = get_terms( $matches[2], array(
424 'name__like' => $query,
427 if ( empty( $terms ) || is_wp_error( $terms ) )
429 foreach( (array) $terms as $term ) {
430 if ( 'markup' == $response_format ) {
431 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( $term ) ), 0, (object) $args );
432 } elseif ( 'json' == $response_format ) {
435 'ID' => $term->term_id,
436 'post_title' => $term->name,
437 'post_type' => $matches[2],
448 * Register nav menu metaboxes and advanced menu items
452 function wp_nav_menu_setup() {
453 // Register meta boxes
454 wp_nav_menu_post_type_meta_boxes();
455 add_meta_box( 'add-custom-links', __( 'Custom Links' ), 'wp_nav_menu_item_link_meta_box', 'nav-menus', 'side', 'default' );
456 wp_nav_menu_taxonomy_meta_boxes();
458 // Register advanced menu items (columns)
459 add_filter( 'manage_nav-menus_columns', 'wp_nav_menu_manage_columns' );
461 // If first time editing, disable advanced items by default.
462 if ( false === get_user_option( 'managenav-menuscolumnshidden' ) ) {
463 $user = wp_get_current_user();
464 update_user_option($user->ID, 'managenav-menuscolumnshidden',
465 array( 0 => 'link-target', 1 => 'css-classes', 2 => 'xfn', 3 => 'description', 4 => 'title-attribute', ),
471 * Limit the amount of meta boxes to pages, posts, links, and categories for first time users.
475 * @global array $wp_meta_boxes
477 function wp_initial_nav_menu_meta_boxes() {
478 global $wp_meta_boxes;
480 if ( get_user_option( 'metaboxhidden_nav-menus' ) !== false || ! is_array($wp_meta_boxes) )
483 $initial_meta_boxes = array( 'add-page', 'add-post', 'add-custom-links', 'add-category' );
484 $hidden_meta_boxes = array();
486 foreach ( array_keys($wp_meta_boxes['nav-menus']) as $context ) {
487 foreach ( array_keys($wp_meta_boxes['nav-menus'][$context]) as $priority ) {
488 foreach ( $wp_meta_boxes['nav-menus'][$context][$priority] as $box ) {
489 if ( in_array( $box['id'], $initial_meta_boxes ) ) {
492 $hidden_meta_boxes[] = $box['id'];
498 $user = wp_get_current_user();
499 update_user_option( $user->ID, 'metaboxhidden_nav-menus', $hidden_meta_boxes, true );
503 * Creates metaboxes for any post type menu item.
507 function wp_nav_menu_post_type_meta_boxes() {
508 $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
513 foreach ( $post_types as $post_type ) {
515 * Filter whether a menu items meta box will be added for the current
518 * If a falsey value is returned instead of an object, the menu items
519 * meta box for the current meta box object will not be added.
523 * @param object $meta_box_object The current object to add a menu items
526 $post_type = apply_filters( 'nav_menu_meta_box_object', $post_type );
528 $id = $post_type->name;
529 // Give pages a higher priority.
530 $priority = ( 'page' == $post_type->name ? 'core' : 'default' );
531 add_meta_box( "add-{$id}", $post_type->labels->name, 'wp_nav_menu_item_post_type_meta_box', 'nav-menus', 'side', $priority, $post_type );
537 * Creates metaboxes for any taxonomy menu item.
541 function wp_nav_menu_taxonomy_meta_boxes() {
542 $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'object' );
547 foreach ( $taxonomies as $tax ) {
548 /** This filter is documented in wp-admin/includes/nav-menu.php */
549 $tax = apply_filters( 'nav_menu_meta_box_object', $tax );
552 add_meta_box( "add-{$id}", $tax->labels->name, 'wp_nav_menu_item_taxonomy_meta_box', 'nav-menus', 'side', 'default', $tax );
558 * Check whether to disable the Menu Locations meta box submit button
562 * @global bool $one_theme_location_no_menus to determine if no menus exist
564 * @param int|string $nav_menu_selected_id (id, name or slug) of the currently-selected menu
565 * @return string Disabled attribute if at least one menu exists, false if not
567 function wp_nav_menu_disabled_check( $nav_menu_selected_id ) {
568 global $one_theme_location_no_menus;
570 if ( $one_theme_location_no_menus )
573 return disabled( $nav_menu_selected_id, 0 );
577 * Displays a metabox for the custom links menu item.
581 * @global int $_nav_menu_placeholder
582 * @global int|string $nav_menu_selected_id
584 function wp_nav_menu_item_link_meta_box() {
585 global $_nav_menu_placeholder, $nav_menu_selected_id;
587 $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;
590 <div class="customlinkdiv" id="customlinkdiv">
591 <input type="hidden" value="custom" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-type]" />
592 <p id="menu-item-url-wrap">
593 <label class="howto" for="custom-menu-item-url">
594 <span><?php _e('URL'); ?></span>
595 <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://" />
599 <p id="menu-item-name-wrap">
600 <label class="howto" for="custom-menu-item-name">
601 <span><?php _e( 'Link Text' ); ?></span>
602 <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'); ?>" />
606 <p class="button-controls">
607 <span class="add-to-menu">
608 <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" />
609 <span class="spinner"></span>
613 </div><!-- /.customlinkdiv -->
618 * Displays a metabox for a post type menu item.
622 * @global int $_nav_menu_placeholder
623 * @global int|string $nav_menu_selected_id
625 * @param string $object Not used.
626 * @param string $post_type The post type object.
628 function wp_nav_menu_item_post_type_meta_box( $object, $post_type ) {
629 global $_nav_menu_placeholder, $nav_menu_selected_id;
631 $post_type_name = $post_type['args']->name;
633 // Paginate browsing for large numbers of post objects.
635 $pagenum = isset( $_REQUEST[$post_type_name . '-tab'] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
636 $offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;
641 'orderby' => 'title',
642 'posts_per_page' => $per_page,
643 'post_type' => $post_type_name,
644 'suppress_filters' => true,
645 'update_post_term_cache' => false,
646 'update_post_meta_cache' => false
649 if ( isset( $post_type['args']->_default_query ) )
650 $args = array_merge($args, (array) $post_type['args']->_default_query );
652 // @todo transient caching of these results with proper invalidation on updating of a post of this type
653 $get_posts = new WP_Query;
654 $posts = $get_posts->query( $args );
655 if ( ! $get_posts->post_count ) {
656 echo '<p>' . __( 'No items.' ) . '</p>';
660 $num_pages = $get_posts->max_num_pages;
662 $page_links = paginate_links( array(
663 'base' => add_query_arg(
665 $post_type_name . '-tab' => 'all',
667 'item-type' => 'post_type',
668 'item-object' => $post_type_name,
672 'prev_text' => __('«'),
673 'next_text' => __('»'),
674 'total' => $num_pages,
675 'current' => $pagenum
679 if ( is_post_type_hierarchical( $post_type_name ) ) {
680 $db_fields = array( 'parent' => 'post_parent', 'id' => 'ID' );
683 $walker = new Walker_Nav_Menu_Checklist( $db_fields );
685 $current_tab = 'most-recent';
686 if ( isset( $_REQUEST[$post_type_name . '-tab'] ) && in_array( $_REQUEST[$post_type_name . '-tab'], array('all', 'search') ) ) {
687 $current_tab = $_REQUEST[$post_type_name . '-tab'];
690 if ( ! empty( $_REQUEST['quick-search-posttype-' . $post_type_name] ) ) {
691 $current_tab = 'search';
694 $removed_args = array(
704 <div id="posttype-<?php echo $post_type_name; ?>" class="posttypediv">
705 <ul id="posttype-<?php echo $post_type_name; ?>-tabs" class="posttype-tabs add-menu-item-tabs">
706 <li <?php echo ( 'most-recent' == $current_tab ? ' class="tabs"' : '' ); ?>>
707 <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">
708 <?php _e( 'Most Recent' ); ?>
711 <li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>>
712 <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">
713 <?php _e( 'View All' ); ?>
716 <li <?php echo ( 'search' == $current_tab ? ' class="tabs"' : '' ); ?>>
717 <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">
718 <?php _e( 'Search'); ?>
721 </ul><!-- .posttype-tabs -->
723 <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent" class="tabs-panel <?php
724 echo ( 'most-recent' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
726 <ul id="<?php echo $post_type_name; ?>checklist-most-recent" class="categorychecklist form-no-clear">
728 $recent_args = array_merge( $args, array( 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => 15 ) );
729 $most_recent = $get_posts->query( $recent_args );
730 $args['walker'] = $walker;
733 * Filter the posts displayed in the 'Most Recent' tab of the current
734 * post type's menu items meta box.
736 * The dynamic portion of the hook name, `$post_type_name`, refers to the post type name.
740 * @param array $most_recent An array of post objects being listed.
741 * @param array $args An array of WP_Query arguments.
742 * @param object $post_type The current post type object for this menu item meta box.
744 $most_recent = apply_filters( "nav_menu_items_{$post_type_name}_recent", $most_recent, $args, $post_type );
746 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $most_recent), 0, (object) $args );
749 </div><!-- /.tabs-panel -->
751 <div class="tabs-panel <?php
752 echo ( 'search' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
753 ?>" id="tabs-panel-posttype-<?php echo $post_type_name; ?>-search">
755 if ( isset( $_REQUEST['quick-search-posttype-' . $post_type_name] ) ) {
756 $searched = esc_attr( $_REQUEST['quick-search-posttype-' . $post_type_name] );
757 $search_results = get_posts( array( 's' => $searched, 'post_type' => $post_type_name, 'fields' => 'all', 'order' => 'DESC', ) );
760 $search_results = array();
763 <p class="quick-search-wrap">
764 <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; ?>" />
765 <span class="spinner"></span>
766 <?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 ) ); ?>
769 <ul id="<?php echo $post_type_name; ?>-search-checklist" data-wp-lists="list:<?php echo $post_type_name?>" class="categorychecklist form-no-clear">
770 <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?>
772 $args['walker'] = $walker;
773 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args );
775 <?php elseif ( is_wp_error( $search_results ) ) : ?>
776 <li><?php echo $search_results->get_error_message(); ?></li>
777 <?php elseif ( ! empty( $searched ) ) : ?>
778 <li><?php _e('No results found.'); ?></li>
781 </div><!-- /.tabs-panel -->
783 <div id="<?php echo $post_type_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php
784 echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
786 <?php if ( ! empty( $page_links ) ) : ?>
787 <div class="add-menu-item-pagelinks">
788 <?php echo $page_links; ?>
791 <ul id="<?php echo $post_type_name; ?>checklist" data-wp-lists="list:<?php echo $post_type_name?>" class="categorychecklist form-no-clear">
793 $args['walker'] = $walker;
796 * If we're dealing with pages, let's put a checkbox for the front
797 * page at the top of the list.
799 if ( 'page' == $post_type_name ) {
800 $front_page = 'page' == get_option('show_on_front') ? (int) get_option( 'page_on_front' ) : 0;
801 if ( ! empty( $front_page ) ) {
802 $front_page_obj = get_post( $front_page );
803 $front_page_obj->front_or_home = true;
804 array_unshift( $posts, $front_page_obj );
806 $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1;
807 array_unshift( $posts, (object) array(
808 'front_or_home' => true,
810 'object_id' => $_nav_menu_placeholder,
811 'post_content' => '',
812 'post_excerpt' => '',
814 'post_title' => _x('Home', 'nav menu home label'),
815 'post_type' => 'nav_menu_item',
817 'url' => home_url('/'),
823 * Filter the posts displayed in the 'View All' tab of the current
824 * post type's menu items meta box.
826 * The dynamic portion of the hook name, `$post_type_name`, refers
827 * to the slug of the current post type.
831 * @see WP_Query::query()
833 * @param array $posts The posts for the current post type.
834 * @param array $args An array of WP_Query arguments.
835 * @param object $post_type The current post type object for this menu item meta box.
837 $posts = apply_filters( "nav_menu_items_{$post_type_name}", $posts, $args, $post_type );
838 $checkbox_items = walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $posts), 0, (object) $args );
840 if ( 'all' == $current_tab && ! empty( $_REQUEST['selectall'] ) ) {
841 $checkbox_items = preg_replace('/(type=(.)checkbox(\2))/', '$1 checked=$2checked$2', $checkbox_items);
845 echo $checkbox_items;
848 <?php if ( ! empty( $page_links ) ) : ?>
849 <div class="add-menu-item-pagelinks">
850 <?php echo $page_links; ?>
853 </div><!-- /.tabs-panel -->
855 <p class="button-controls">
856 <span class="list-controls">
858 echo esc_url( add_query_arg(
860 $post_type_name . '-tab' => 'all',
863 remove_query_arg( $removed_args )
865 ?>#posttype-<?php echo $post_type_name; ?>" class="select-all"><?php _e('Select All'); ?></a>
868 <span class="add-to-menu">
869 <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 echo esc_attr( 'submit-posttype-' . $post_type_name ); ?>" />
870 <span class="spinner"></span>
874 </div><!-- /.posttypediv -->
879 * Displays a metabox for a taxonomy menu item.
883 * @global int|string $nav_menu_selected_id
885 * @param string $object Not used.
886 * @param string $taxonomy The taxonomy object.
888 function wp_nav_menu_item_taxonomy_meta_box( $object, $taxonomy ) {
889 global $nav_menu_selected_id;
890 $taxonomy_name = $taxonomy['args']->name;
892 // Paginate browsing for large numbers of objects.
894 $pagenum = isset( $_REQUEST[$taxonomy_name . '-tab'] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
895 $offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;
900 'hide_empty' => false,
903 'number' => $per_page,
907 'pad_counts' => false,
910 $terms = get_terms( $taxonomy_name, $args );
912 if ( ! $terms || is_wp_error($terms) ) {
913 echo '<p>' . __( 'No items.' ) . '</p>';
917 $num_pages = ceil( wp_count_terms( $taxonomy_name , array_merge( $args, array('number' => '', 'offset' => '') ) ) / $per_page );
919 $page_links = paginate_links( array(
920 'base' => add_query_arg(
922 $taxonomy_name . '-tab' => 'all',
924 'item-type' => 'taxonomy',
925 'item-object' => $taxonomy_name,
929 'prev_text' => __('«'),
930 'next_text' => __('»'),
931 'total' => $num_pages,
932 'current' => $pagenum
936 if ( is_taxonomy_hierarchical( $taxonomy_name ) ) {
937 $db_fields = array( 'parent' => 'parent', 'id' => 'term_id' );
940 $walker = new Walker_Nav_Menu_Checklist( $db_fields );
942 $current_tab = 'most-used';
943 if ( isset( $_REQUEST[$taxonomy_name . '-tab'] ) && in_array( $_REQUEST[$taxonomy_name . '-tab'], array('all', 'most-used', 'search') ) ) {
944 $current_tab = $_REQUEST[$taxonomy_name . '-tab'];
947 if ( ! empty( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] ) ) {
948 $current_tab = 'search';
951 $removed_args = array(
961 <div id="taxonomy-<?php echo $taxonomy_name; ?>" class="taxonomydiv">
962 <ul id="taxonomy-<?php echo $taxonomy_name; ?>-tabs" class="taxonomy-tabs add-menu-item-tabs">
963 <li <?php echo ( 'most-used' == $current_tab ? ' class="tabs"' : '' ); ?>>
964 <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">
965 <?php _e( 'Most Used' ); ?>
968 <li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>>
969 <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">
970 <?php _e( 'View All' ); ?>
973 <li <?php echo ( 'search' == $current_tab ? ' class="tabs"' : '' ); ?>>
974 <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; ?>">
975 <?php _e( 'Search' ); ?>
978 </ul><!-- .taxonomy-tabs -->
980 <div id="tabs-panel-<?php echo $taxonomy_name; ?>-pop" class="tabs-panel <?php
981 echo ( 'most-used' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
983 <ul id="<?php echo $taxonomy_name; ?>checklist-pop" class="categorychecklist form-no-clear" >
985 $popular_terms = get_terms( $taxonomy_name, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
986 $args['walker'] = $walker;
987 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $popular_terms), 0, (object) $args );
990 </div><!-- /.tabs-panel -->
992 <div id="tabs-panel-<?php echo $taxonomy_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php
993 echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
995 <?php if ( ! empty( $page_links ) ) : ?>
996 <div class="add-menu-item-pagelinks">
997 <?php echo $page_links; ?>
1000 <ul id="<?php echo $taxonomy_name; ?>checklist" data-wp-lists="list:<?php echo $taxonomy_name?>" class="categorychecklist form-no-clear">
1002 $args['walker'] = $walker;
1003 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $terms), 0, (object) $args );
1006 <?php if ( ! empty( $page_links ) ) : ?>
1007 <div class="add-menu-item-pagelinks">
1008 <?php echo $page_links; ?>
1011 </div><!-- /.tabs-panel -->
1013 <div class="tabs-panel <?php
1014 echo ( 'search' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
1015 ?>" id="tabs-panel-search-taxonomy-<?php echo $taxonomy_name; ?>">
1017 if ( isset( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] ) ) {
1018 $searched = esc_attr( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] );
1019 $search_results = get_terms( $taxonomy_name, array( 'name__like' => $searched, 'fields' => 'all', 'orderby' => 'count', 'order' => 'DESC', 'hierarchical' => false ) );
1022 $search_results = array();
1025 <p class="quick-search-wrap">
1026 <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; ?>" />
1027 <span class="spinner"></span>
1028 <?php submit_button( __( 'Search' ), 'button-small quick-search-submit button-secondary hide-if-js', 'submit', false, array( 'id' => 'submit-quick-search-taxonomy-' . $taxonomy_name ) ); ?>
1031 <ul id="<?php echo $taxonomy_name; ?>-search-checklist" data-wp-lists="list:<?php echo $taxonomy_name?>" class="categorychecklist form-no-clear">
1032 <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?>
1034 $args['walker'] = $walker;
1035 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args );
1037 <?php elseif ( is_wp_error( $search_results ) ) : ?>
1038 <li><?php echo $search_results->get_error_message(); ?></li>
1039 <?php elseif ( ! empty( $searched ) ) : ?>
1040 <li><?php _e('No results found.'); ?></li>
1043 </div><!-- /.tabs-panel -->
1045 <p class="button-controls">
1046 <span class="list-controls">
1048 echo esc_url(add_query_arg(
1050 $taxonomy_name . '-tab' => 'all',
1053 remove_query_arg($removed_args)
1055 ?>#taxonomy-<?php echo $taxonomy_name; ?>" class="select-all"><?php _e('Select All'); ?></a>
1058 <span class="add-to-menu">
1059 <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 echo esc_attr( 'submit-taxonomy-' . $taxonomy_name ); ?>" />
1060 <span class="spinner"></span>
1064 </div><!-- /.taxonomydiv -->
1069 * Save posted nav menu item data.
1073 * @param int $menu_id The menu ID for which to save this item. $menu_id of 0 makes a draft, orphaned menu item.
1074 * @param array $menu_data The unsanitized posted menu item data.
1075 * @return array The database IDs of the items saved
1077 function wp_save_nav_menu_items( $menu_id = 0, $menu_data = array() ) {
1078 $menu_id = (int) $menu_id;
1079 $items_saved = array();
1081 if ( 0 == $menu_id || is_nav_menu( $menu_id ) ) {
1083 // Loop through all the menu items' POST values.
1084 foreach( (array) $menu_data as $_possible_db_id => $_item_object_data ) {
1086 // Checkbox is not checked.
1087 empty( $_item_object_data['menu-item-object-id'] ) &&
1089 // And item type either isn't set.
1090 ! isset( $_item_object_data['menu-item-type'] ) ||
1091 // Or URL is the default.
1092 in_array( $_item_object_data['menu-item-url'], array( 'http://', '' ) ) ||
1093 ! ( '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)
1094 // Or it *is* a custom menu item that already exists.
1095 ! empty( $_item_object_data['menu-item-db-id'] )
1098 // Then this potential menu item is not getting added to this menu.
1102 // If this possible menu item doesn't actually have a menu database ID yet.
1104 empty( $_item_object_data['menu-item-db-id'] ) ||
1105 ( 0 > $_possible_db_id ) ||
1106 $_possible_db_id != $_item_object_data['menu-item-db-id']
1110 $_actual_db_id = (int) $_item_object_data['menu-item-db-id'];
1114 'menu-item-db-id' => ( isset( $_item_object_data['menu-item-db-id'] ) ? $_item_object_data['menu-item-db-id'] : '' ),
1115 'menu-item-object-id' => ( isset( $_item_object_data['menu-item-object-id'] ) ? $_item_object_data['menu-item-object-id'] : '' ),
1116 'menu-item-object' => ( isset( $_item_object_data['menu-item-object'] ) ? $_item_object_data['menu-item-object'] : '' ),
1117 'menu-item-parent-id' => ( isset( $_item_object_data['menu-item-parent-id'] ) ? $_item_object_data['menu-item-parent-id'] : '' ),
1118 'menu-item-position' => ( isset( $_item_object_data['menu-item-position'] ) ? $_item_object_data['menu-item-position'] : '' ),
1119 'menu-item-type' => ( isset( $_item_object_data['menu-item-type'] ) ? $_item_object_data['menu-item-type'] : '' ),
1120 'menu-item-title' => ( isset( $_item_object_data['menu-item-title'] ) ? $_item_object_data['menu-item-title'] : '' ),
1121 'menu-item-url' => ( isset( $_item_object_data['menu-item-url'] ) ? $_item_object_data['menu-item-url'] : '' ),
1122 'menu-item-description' => ( isset( $_item_object_data['menu-item-description'] ) ? $_item_object_data['menu-item-description'] : '' ),
1123 'menu-item-attr-title' => ( isset( $_item_object_data['menu-item-attr-title'] ) ? $_item_object_data['menu-item-attr-title'] : '' ),
1124 'menu-item-target' => ( isset( $_item_object_data['menu-item-target'] ) ? $_item_object_data['menu-item-target'] : '' ),
1125 'menu-item-classes' => ( isset( $_item_object_data['menu-item-classes'] ) ? $_item_object_data['menu-item-classes'] : '' ),
1126 'menu-item-xfn' => ( isset( $_item_object_data['menu-item-xfn'] ) ? $_item_object_data['menu-item-xfn'] : '' ),
1129 $items_saved[] = wp_update_nav_menu_item( $menu_id, $_actual_db_id, $args );
1133 return $items_saved;
1137 * Adds custom arguments to some of the meta box object types.
1143 * @param object $object The post type or taxonomy meta-object.
1144 * @return object The post type of taxonomy object.
1146 function _wp_nav_menu_meta_box_object( $object = null ) {
1147 if ( isset( $object->name ) ) {
1149 if ( 'page' == $object->name ) {
1150 $object->_default_query = array(
1151 'orderby' => 'menu_order title',
1152 'post_status' => 'publish',
1155 // Posts should show only published items.
1156 } elseif ( 'post' == $object->name ) {
1157 $object->_default_query = array(
1158 'post_status' => 'publish',
1161 // Categories should be in reverse chronological order.
1162 } elseif ( 'category' == $object->name ) {
1163 $object->_default_query = array(
1168 // Custom post types should show only published items.
1170 $object->_default_query = array(
1171 'post_status' => 'publish',
1180 * Returns the menu formatted to edit.
1184 * @param int $menu_id Optional. The ID of the menu to format. Default 0.
1185 * @return string|WP_Error $output The menu formatted to edit or error object on failure.
1187 function wp_get_nav_menu_to_edit( $menu_id = 0 ) {
1188 $menu = wp_get_nav_menu_object( $menu_id );
1190 // If the menu exists, get its items.
1191 if ( is_nav_menu( $menu ) ) {
1192 $menu_items = wp_get_nav_menu_items( $menu->term_id, array('post_status' => 'any') );
1193 $result = '<div id="menu-instructions" class="post-body-plain';
1194 $result .= ( ! empty($menu_items) ) ? ' menu-instructions-inactive">' : '">';
1195 $result .= '<p>' . __( 'Add menu items from the column on the left.' ) . '</p>';
1196 $result .= '</div>';
1198 if ( empty($menu_items) )
1199 return $result . ' <ul class="menu" id="menu-to-edit"> </ul>';
1202 * Filter the Walker class used when adding nav menu items.
1206 * @param string $class The walker class to use. Default 'Walker_Nav_Menu_Edit'.
1207 * @param int $menu_id ID of the menu being rendered.
1209 $walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $menu_id );
1211 if ( class_exists( $walker_class_name ) )
1212 $walker = new $walker_class_name;
1214 return new WP_Error( 'menu_walker_not_exist', sprintf( __('The Walker class named <strong>%s</strong> does not exist.'), $walker_class_name ) );
1216 $some_pending_menu_items = $some_invalid_menu_items = false;
1217 foreach( (array) $menu_items as $menu_item ) {
1218 if ( isset( $menu_item->post_status ) && 'draft' == $menu_item->post_status )
1219 $some_pending_menu_items = true;
1220 if ( ! empty( $menu_item->_invalid ) )
1221 $some_invalid_menu_items = true;
1224 if ( $some_pending_menu_items )
1225 $result .= '<div class="updated inline"><p>' . __('Click Save Menu to make pending menu items public.') . '</p></div>';
1227 if ( $some_invalid_menu_items )
1228 $result .= '<div class="error inline"><p>' . __('There are some invalid menu items. Please check or delete them.') . '</p></div>';
1230 $result .= '<ul class="menu" id="menu-to-edit"> ';
1231 $result .= walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $menu_items), 0, (object) array('walker' => $walker ) );
1232 $result .= ' </ul> ';
1234 } elseif ( is_wp_error( $menu ) ) {
1241 * Returns the columns for the nav menus page.
1245 * @return string|WP_Error $output The menu formatted to edit or error object on failure.
1247 function wp_nav_menu_manage_columns() {
1249 '_title' => __('Show advanced menu properties'),
1250 'cb' => '<input type="checkbox" />',
1251 'title-attribute' => __('Title Attribute'),
1252 'link-target' => __('Link Target'),
1253 'css-classes' => __('CSS Classes'),
1254 'xfn' => __('Link Relationship (XFN)'),
1255 'description' => __('Description'),
1260 * Deletes orphaned draft menu items
1265 * @global wpdb $wpdb
1267 function _wp_delete_orphaned_draft_menu_items() {
1269 $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
1271 // Delete orphaned draft menu items.
1272 $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 ) );
1274 foreach( (array) $menu_items_to_delete as $menu_item_id )
1275 wp_delete_post( $menu_item_id, true );
1279 * Saves nav menu items
1283 * @param int|string $nav_menu_selected_id (id, slug, or name ) of the currently-selected menu
1284 * @param string $nav_menu_selected_title Title of the currently-selected menu
1285 * @return array $messages The menu updated message
1287 function wp_nav_menu_update_menu_items ( $nav_menu_selected_id, $nav_menu_selected_title ) {
1288 $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' ) );
1289 $messages = array();
1290 $menu_items = array();
1291 // Index menu items by db ID
1292 foreach ( $unsorted_menu_items as $_item )
1293 $menu_items[$_item->db_id] = $_item;
1295 $post_fields = array(
1296 'menu-item-db-id', 'menu-item-object-id', 'menu-item-object',
1297 'menu-item-parent-id', 'menu-item-position', 'menu-item-type',
1298 'menu-item-title', 'menu-item-url', 'menu-item-description',
1299 'menu-item-attr-title', 'menu-item-target', 'menu-item-classes', 'menu-item-xfn'
1302 wp_defer_term_counting( true );
1303 // Loop through all the menu items' POST variables
1304 if ( ! empty( $_POST['menu-item-db-id'] ) ) {
1305 foreach( (array) $_POST['menu-item-db-id'] as $_key => $k ) {
1307 // Menu item title can't be blank
1308 if ( ! isset( $_POST['menu-item-title'][ $_key ] ) || '' == $_POST['menu-item-title'][ $_key ] )
1312 foreach ( $post_fields as $field )
1313 $args[$field] = isset( $_POST[$field][$_key] ) ? $_POST[$field][$_key] : '';
1315 $menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key ), $args );
1317 if ( is_wp_error( $menu_item_db_id ) ) {
1318 $messages[] = '<div id="message" class="error"><p>' . $menu_item_db_id->get_error_message() . '</p></div>';
1320 unset( $menu_items[ $menu_item_db_id ] );
1325 // Remove menu items from the menu that weren't in $_POST
1326 if ( ! empty( $menu_items ) ) {
1327 foreach ( array_keys( $menu_items ) as $menu_item_id ) {
1328 if ( is_nav_menu_item( $menu_item_id ) ) {
1329 wp_delete_post( $menu_item_id );
1334 // Store 'auto-add' pages.
1335 $auto_add = ! empty( $_POST['auto-add-pages'] );
1336 $nav_menu_option = (array) get_option( 'nav_menu_options' );
1337 if ( ! isset( $nav_menu_option['auto_add'] ) )
1338 $nav_menu_option['auto_add'] = array();
1340 if ( ! in_array( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) )
1341 $nav_menu_option['auto_add'][] = $nav_menu_selected_id;
1343 if ( false !== ( $key = array_search( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) ) )
1344 unset( $nav_menu_option['auto_add'][$key] );
1346 // Remove nonexistent/deleted menus
1347 $nav_menu_option['auto_add'] = array_intersect( $nav_menu_option['auto_add'], wp_get_nav_menus( array( 'fields' => 'ids' ) ) );
1348 update_option( 'nav_menu_options', $nav_menu_option );
1350 wp_defer_term_counting( false );
1352 /** This action is documented in wp-includes/nav-menu.php */
1353 do_action( 'wp_update_nav_menu', $nav_menu_selected_id );
1355 $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . sprintf( __( '<strong>%1$s</strong> has been updated.' ), $nav_menu_selected_title ) . '</p></div>';
1356 unset( $menu_items, $unsorted_menu_items );