3 * Core Navigation Menu API
6 * @subpackage Nav_Menus
10 /** Walker_Nav_Menu_Edit class */
11 require_once( ABSPATH . 'wp-admin/includes/class-walker-nav-menu-edit.php' );
13 /** Walker_Nav_Menu_Checklist class */
14 require_once( ABSPATH . 'wp-admin/includes/class-walker-nav-menu-checklist.php' );
17 * Prints the appropriate response to a menu quick search.
21 * @param array $request The unsanitized request values.
23 function _wp_ajax_menu_quick_search( $request = array() ) {
25 $type = isset( $request['type'] ) ? $request['type'] : '';
26 $object_type = isset( $request['object_type'] ) ? $request['object_type'] : '';
27 $query = isset( $request['q'] ) ? $request['q'] : '';
28 $response_format = isset( $request['response-format'] ) && in_array( $request['response-format'], array( 'json', 'markup' ) ) ? $request['response-format'] : 'json';
30 if ( 'markup' == $response_format ) {
31 $args['walker'] = new Walker_Nav_Menu_Checklist;
34 if ( 'get-post-item' == $type ) {
35 if ( post_type_exists( $object_type ) ) {
36 if ( isset( $request['ID'] ) ) {
37 $object_id = (int) $request['ID'];
38 if ( 'markup' == $response_format ) {
39 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_post( $object_id ) ) ), 0, (object) $args );
40 } elseif ( 'json' == $response_format ) {
44 'post_title' => get_the_title( $object_id ),
45 'post_type' => get_post_type( $object_id ),
51 } elseif ( taxonomy_exists( $object_type ) ) {
52 if ( isset( $request['ID'] ) ) {
53 $object_id = (int) $request['ID'];
54 if ( 'markup' == $response_format ) {
55 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_term( $object_id, $object_type ) ) ), 0, (object) $args );
56 } elseif ( 'json' == $response_format ) {
57 $post_obj = get_term( $object_id, $object_type );
61 'post_title' => $post_obj->name,
62 'post_type' => $object_type,
71 } elseif ( preg_match('/quick-search-(posttype|taxonomy)-([a-zA-Z_-]*\b)/', $type, $matches) ) {
72 if ( 'posttype' == $matches[1] && get_post_type_object( $matches[2] ) ) {
74 'posts_per_page' => 10,
75 'post_type' => $matches[2],
80 while ( have_posts() ) {
82 if ( 'markup' == $response_format ) {
83 $var_by_ref = get_the_ID();
84 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_post( $var_by_ref ) ) ), 0, (object) $args );
85 } elseif ( 'json' == $response_format ) {
89 'post_title' => get_the_title(),
90 'post_type' => get_post_type(),
96 } elseif ( 'taxonomy' == $matches[1] ) {
97 $terms = get_terms( $matches[2], array(
98 'name__like' => $query,
101 if ( empty( $terms ) || is_wp_error( $terms ) )
103 foreach ( (array) $terms as $term ) {
104 if ( 'markup' == $response_format ) {
105 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( $term ) ), 0, (object) $args );
106 } elseif ( 'json' == $response_format ) {
109 'ID' => $term->term_id,
110 'post_title' => $term->name,
111 'post_type' => $matches[2],
122 * Register nav menu metaboxes and advanced menu items
126 function wp_nav_menu_setup() {
127 // Register meta boxes
128 wp_nav_menu_post_type_meta_boxes();
129 add_meta_box( 'add-custom-links', __( 'Custom Links' ), 'wp_nav_menu_item_link_meta_box', 'nav-menus', 'side', 'default' );
130 wp_nav_menu_taxonomy_meta_boxes();
132 // Register advanced menu items (columns)
133 add_filter( 'manage_nav-menus_columns', 'wp_nav_menu_manage_columns' );
135 // If first time editing, disable advanced items by default.
136 if ( false === get_user_option( 'managenav-menuscolumnshidden' ) ) {
137 $user = wp_get_current_user();
138 update_user_option($user->ID, 'managenav-menuscolumnshidden',
139 array( 0 => 'link-target', 1 => 'css-classes', 2 => 'xfn', 3 => 'description', 4 => 'title-attribute', ),
145 * Limit the amount of meta boxes to pages, posts, links, and categories for first time users.
149 * @global array $wp_meta_boxes
151 function wp_initial_nav_menu_meta_boxes() {
152 global $wp_meta_boxes;
154 if ( get_user_option( 'metaboxhidden_nav-menus' ) !== false || ! is_array($wp_meta_boxes) )
157 $initial_meta_boxes = array( 'add-post-type-page', 'add-post-type-post', 'add-custom-links', 'add-category' );
158 $hidden_meta_boxes = array();
160 foreach ( array_keys($wp_meta_boxes['nav-menus']) as $context ) {
161 foreach ( array_keys($wp_meta_boxes['nav-menus'][$context]) as $priority ) {
162 foreach ( $wp_meta_boxes['nav-menus'][$context][$priority] as $box ) {
163 if ( in_array( $box['id'], $initial_meta_boxes ) ) {
166 $hidden_meta_boxes[] = $box['id'];
172 $user = wp_get_current_user();
173 update_user_option( $user->ID, 'metaboxhidden_nav-menus', $hidden_meta_boxes, true );
177 * Creates metaboxes for any post type menu item.
181 function wp_nav_menu_post_type_meta_boxes() {
182 $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
187 foreach ( $post_types as $post_type ) {
189 * Filter whether a menu items meta box will be added for the current
192 * If a falsey value is returned instead of an object, the menu items
193 * meta box for the current meta box object will not be added.
197 * @param object $meta_box_object The current object to add a menu items
200 $post_type = apply_filters( 'nav_menu_meta_box_object', $post_type );
202 $id = $post_type->name;
203 // Give pages a higher priority.
204 $priority = ( 'page' == $post_type->name ? 'core' : 'default' );
205 add_meta_box( "add-post-type-{$id}", $post_type->labels->name, 'wp_nav_menu_item_post_type_meta_box', 'nav-menus', 'side', $priority, $post_type );
211 * Creates metaboxes for any taxonomy menu item.
215 function wp_nav_menu_taxonomy_meta_boxes() {
216 $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'object' );
221 foreach ( $taxonomies as $tax ) {
222 /** This filter is documented in wp-admin/includes/nav-menu.php */
223 $tax = apply_filters( 'nav_menu_meta_box_object', $tax );
226 add_meta_box( "add-{$id}", $tax->labels->name, 'wp_nav_menu_item_taxonomy_meta_box', 'nav-menus', 'side', 'default', $tax );
232 * Check whether to disable the Menu Locations meta box submit button
236 * @global bool $one_theme_location_no_menus to determine if no menus exist
238 * @param int|string $nav_menu_selected_id (id, name or slug) of the currently-selected menu
239 * @return string Disabled attribute if at least one menu exists, false if not
241 function wp_nav_menu_disabled_check( $nav_menu_selected_id ) {
242 global $one_theme_location_no_menus;
244 if ( $one_theme_location_no_menus )
247 return disabled( $nav_menu_selected_id, 0 );
251 * Displays a metabox for the custom links menu item.
255 * @global int $_nav_menu_placeholder
256 * @global int|string $nav_menu_selected_id
258 function wp_nav_menu_item_link_meta_box() {
259 global $_nav_menu_placeholder, $nav_menu_selected_id;
261 $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;
264 <div class="customlinkdiv" id="customlinkdiv">
265 <input type="hidden" value="custom" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-type]" />
266 <p id="menu-item-url-wrap">
267 <label class="howto" for="custom-menu-item-url">
268 <span><?php _e('URL'); ?></span>
269 <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://" />
273 <p id="menu-item-name-wrap">
274 <label class="howto" for="custom-menu-item-name">
275 <span><?php _e( 'Link Text' ); ?></span>
276 <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'); ?>" />
280 <p class="button-controls">
281 <span class="add-to-menu">
282 <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" />
283 <span class="spinner"></span>
287 </div><!-- /.customlinkdiv -->
292 * Displays a metabox for a post type menu item.
296 * @global int $_nav_menu_placeholder
297 * @global int|string $nav_menu_selected_id
299 * @param string $object Not used.
300 * @param string $post_type The post type object.
302 function wp_nav_menu_item_post_type_meta_box( $object, $post_type ) {
303 global $_nav_menu_placeholder, $nav_menu_selected_id;
305 $post_type_name = $post_type['args']->name;
307 // Paginate browsing for large numbers of post objects.
309 $pagenum = isset( $_REQUEST[$post_type_name . '-tab'] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
310 $offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;
315 'orderby' => 'title',
316 'posts_per_page' => $per_page,
317 'post_type' => $post_type_name,
318 'suppress_filters' => true,
319 'update_post_term_cache' => false,
320 'update_post_meta_cache' => false
323 if ( isset( $post_type['args']->_default_query ) )
324 $args = array_merge($args, (array) $post_type['args']->_default_query );
326 // @todo transient caching of these results with proper invalidation on updating of a post of this type
327 $get_posts = new WP_Query;
328 $posts = $get_posts->query( $args );
329 if ( ! $get_posts->post_count ) {
330 echo '<p>' . __( 'No items.' ) . '</p>';
334 $num_pages = $get_posts->max_num_pages;
336 $page_links = paginate_links( array(
337 'base' => add_query_arg(
339 $post_type_name . '-tab' => 'all',
341 'item-type' => 'post_type',
342 'item-object' => $post_type_name,
346 'prev_text' => __('«'),
347 'next_text' => __('»'),
348 'total' => $num_pages,
349 'current' => $pagenum
353 if ( is_post_type_hierarchical( $post_type_name ) ) {
354 $db_fields = array( 'parent' => 'post_parent', 'id' => 'ID' );
357 $walker = new Walker_Nav_Menu_Checklist( $db_fields );
359 $current_tab = 'most-recent';
360 if ( isset( $_REQUEST[$post_type_name . '-tab'] ) && in_array( $_REQUEST[$post_type_name . '-tab'], array('all', 'search') ) ) {
361 $current_tab = $_REQUEST[$post_type_name . '-tab'];
364 if ( ! empty( $_REQUEST['quick-search-posttype-' . $post_type_name] ) ) {
365 $current_tab = 'search';
368 $removed_args = array(
378 <div id="posttype-<?php echo $post_type_name; ?>" class="posttypediv">
379 <ul id="posttype-<?php echo $post_type_name; ?>-tabs" class="posttype-tabs add-menu-item-tabs">
380 <li <?php echo ( 'most-recent' == $current_tab ? ' class="tabs"' : '' ); ?>>
381 <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">
382 <?php _e( 'Most Recent' ); ?>
385 <li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>>
386 <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">
387 <?php _e( 'View All' ); ?>
390 <li <?php echo ( 'search' == $current_tab ? ' class="tabs"' : '' ); ?>>
391 <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">
392 <?php _e( 'Search'); ?>
395 </ul><!-- .posttype-tabs -->
397 <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent" class="tabs-panel <?php
398 echo ( 'most-recent' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
400 <ul id="<?php echo $post_type_name; ?>checklist-most-recent" class="categorychecklist form-no-clear">
402 $recent_args = array_merge( $args, array( 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => 15 ) );
403 $most_recent = $get_posts->query( $recent_args );
404 $args['walker'] = $walker;
407 * Filter the posts displayed in the 'Most Recent' tab of the current
408 * post type's menu items meta box.
410 * The dynamic portion of the hook name, `$post_type_name`, refers to the post type name.
414 * @param array $most_recent An array of post objects being listed.
415 * @param array $args An array of WP_Query arguments.
416 * @param object $post_type The current post type object for this menu item meta box.
418 $most_recent = apply_filters( "nav_menu_items_{$post_type_name}_recent", $most_recent, $args, $post_type );
420 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $most_recent), 0, (object) $args );
423 </div><!-- /.tabs-panel -->
425 <div class="tabs-panel <?php
426 echo ( 'search' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
427 ?>" id="tabs-panel-posttype-<?php echo $post_type_name; ?>-search">
429 if ( isset( $_REQUEST['quick-search-posttype-' . $post_type_name] ) ) {
430 $searched = esc_attr( $_REQUEST['quick-search-posttype-' . $post_type_name] );
431 $search_results = get_posts( array( 's' => $searched, 'post_type' => $post_type_name, 'fields' => 'all', 'order' => 'DESC', ) );
434 $search_results = array();
437 <p class="quick-search-wrap">
438 <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; ?>" />
439 <span class="spinner"></span>
440 <?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 ) ); ?>
443 <ul id="<?php echo $post_type_name; ?>-search-checklist" data-wp-lists="list:<?php echo $post_type_name?>" class="categorychecklist form-no-clear">
444 <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?>
446 $args['walker'] = $walker;
447 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args );
449 <?php elseif ( is_wp_error( $search_results ) ) : ?>
450 <li><?php echo $search_results->get_error_message(); ?></li>
451 <?php elseif ( ! empty( $searched ) ) : ?>
452 <li><?php _e('No results found.'); ?></li>
455 </div><!-- /.tabs-panel -->
457 <div id="<?php echo $post_type_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php
458 echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
460 <?php if ( ! empty( $page_links ) ) : ?>
461 <div class="add-menu-item-pagelinks">
462 <?php echo $page_links; ?>
465 <ul id="<?php echo $post_type_name; ?>checklist" data-wp-lists="list:<?php echo $post_type_name?>" class="categorychecklist form-no-clear">
467 $args['walker'] = $walker;
470 * If we're dealing with pages, let's put a checkbox for the front
471 * page at the top of the list.
473 if ( 'page' == $post_type_name ) {
474 $front_page = 'page' == get_option('show_on_front') ? (int) get_option( 'page_on_front' ) : 0;
475 if ( ! empty( $front_page ) ) {
476 $front_page_obj = get_post( $front_page );
477 $front_page_obj->front_or_home = true;
478 array_unshift( $posts, $front_page_obj );
480 $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1;
481 array_unshift( $posts, (object) array(
482 'front_or_home' => true,
484 'object_id' => $_nav_menu_placeholder,
485 'post_content' => '',
486 'post_excerpt' => '',
488 'post_title' => _x('Home', 'nav menu home label'),
489 'post_type' => 'nav_menu_item',
491 'url' => home_url('/'),
496 $post_type = get_post_type_object( $post_type_name );
497 $archive_link = get_post_type_archive_link( $post_type_name );
498 if ( $post_type->has_archive ) {
499 $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1;
500 array_unshift( $posts, (object) array(
502 'object_id' => $_nav_menu_placeholder,
503 'object' => $post_type_name,
504 'post_content' => '',
505 'post_excerpt' => '',
506 'post_title' => $post_type->labels->archives,
507 'post_type' => 'nav_menu_item',
508 'type' => 'post_type_archive',
509 'url' => get_post_type_archive_link( $post_type_name ),
514 * Filter the posts displayed in the 'View All' tab of the current
515 * post type's menu items meta box.
517 * The dynamic portion of the hook name, `$post_type_name`, refers
518 * to the slug of the current post type.
522 * @see WP_Query::query()
524 * @param array $posts The posts for the current post type.
525 * @param array $args An array of WP_Query arguments.
526 * @param object $post_type The current post type object for this menu item meta box.
528 $posts = apply_filters( "nav_menu_items_{$post_type_name}", $posts, $args, $post_type );
529 $checkbox_items = walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $posts), 0, (object) $args );
531 if ( 'all' == $current_tab && ! empty( $_REQUEST['selectall'] ) ) {
532 $checkbox_items = preg_replace('/(type=(.)checkbox(\2))/', '$1 checked=$2checked$2', $checkbox_items);
536 echo $checkbox_items;
539 <?php if ( ! empty( $page_links ) ) : ?>
540 <div class="add-menu-item-pagelinks">
541 <?php echo $page_links; ?>
544 </div><!-- /.tabs-panel -->
546 <p class="button-controls">
547 <span class="list-controls">
549 echo esc_url( add_query_arg(
551 $post_type_name . '-tab' => 'all',
554 remove_query_arg( $removed_args )
556 ?>#posttype-<?php echo $post_type_name; ?>" class="select-all"><?php _e('Select All'); ?></a>
559 <span class="add-to-menu">
560 <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 ); ?>" />
561 <span class="spinner"></span>
565 </div><!-- /.posttypediv -->
570 * Displays a metabox for a taxonomy menu item.
574 * @global int|string $nav_menu_selected_id
576 * @param string $object Not used.
577 * @param string $taxonomy The taxonomy object.
579 function wp_nav_menu_item_taxonomy_meta_box( $object, $taxonomy ) {
580 global $nav_menu_selected_id;
581 $taxonomy_name = $taxonomy['args']->name;
583 // Paginate browsing for large numbers of objects.
585 $pagenum = isset( $_REQUEST[$taxonomy_name . '-tab'] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
586 $offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;
591 'hide_empty' => false,
594 'number' => $per_page,
598 'pad_counts' => false,
601 $terms = get_terms( $taxonomy_name, $args );
603 if ( ! $terms || is_wp_error($terms) ) {
604 echo '<p>' . __( 'No items.' ) . '</p>';
608 $num_pages = ceil( wp_count_terms( $taxonomy_name , array_merge( $args, array('number' => '', 'offset' => '') ) ) / $per_page );
610 $page_links = paginate_links( array(
611 'base' => add_query_arg(
613 $taxonomy_name . '-tab' => 'all',
615 'item-type' => 'taxonomy',
616 'item-object' => $taxonomy_name,
620 'prev_text' => __('«'),
621 'next_text' => __('»'),
622 'total' => $num_pages,
623 'current' => $pagenum
627 if ( is_taxonomy_hierarchical( $taxonomy_name ) ) {
628 $db_fields = array( 'parent' => 'parent', 'id' => 'term_id' );
631 $walker = new Walker_Nav_Menu_Checklist( $db_fields );
633 $current_tab = 'most-used';
634 if ( isset( $_REQUEST[$taxonomy_name . '-tab'] ) && in_array( $_REQUEST[$taxonomy_name . '-tab'], array('all', 'most-used', 'search') ) ) {
635 $current_tab = $_REQUEST[$taxonomy_name . '-tab'];
638 if ( ! empty( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] ) ) {
639 $current_tab = 'search';
642 $removed_args = array(
652 <div id="taxonomy-<?php echo $taxonomy_name; ?>" class="taxonomydiv">
653 <ul id="taxonomy-<?php echo $taxonomy_name; ?>-tabs" class="taxonomy-tabs add-menu-item-tabs">
654 <li <?php echo ( 'most-used' == $current_tab ? ' class="tabs"' : '' ); ?>>
655 <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">
656 <?php _e( 'Most Used' ); ?>
659 <li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>>
660 <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">
661 <?php _e( 'View All' ); ?>
664 <li <?php echo ( 'search' == $current_tab ? ' class="tabs"' : '' ); ?>>
665 <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; ?>">
666 <?php _e( 'Search' ); ?>
669 </ul><!-- .taxonomy-tabs -->
671 <div id="tabs-panel-<?php echo $taxonomy_name; ?>-pop" class="tabs-panel <?php
672 echo ( 'most-used' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
674 <ul id="<?php echo $taxonomy_name; ?>checklist-pop" class="categorychecklist form-no-clear" >
676 $popular_terms = get_terms( $taxonomy_name, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
677 $args['walker'] = $walker;
678 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $popular_terms), 0, (object) $args );
681 </div><!-- /.tabs-panel -->
683 <div id="tabs-panel-<?php echo $taxonomy_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php
684 echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
686 <?php if ( ! empty( $page_links ) ) : ?>
687 <div class="add-menu-item-pagelinks">
688 <?php echo $page_links; ?>
691 <ul id="<?php echo $taxonomy_name; ?>checklist" data-wp-lists="list:<?php echo $taxonomy_name?>" class="categorychecklist form-no-clear">
693 $args['walker'] = $walker;
694 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $terms), 0, (object) $args );
697 <?php if ( ! empty( $page_links ) ) : ?>
698 <div class="add-menu-item-pagelinks">
699 <?php echo $page_links; ?>
702 </div><!-- /.tabs-panel -->
704 <div class="tabs-panel <?php
705 echo ( 'search' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
706 ?>" id="tabs-panel-search-taxonomy-<?php echo $taxonomy_name; ?>">
708 if ( isset( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] ) ) {
709 $searched = esc_attr( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] );
710 $search_results = get_terms( $taxonomy_name, array( 'name__like' => $searched, 'fields' => 'all', 'orderby' => 'count', 'order' => 'DESC', 'hierarchical' => false ) );
713 $search_results = array();
716 <p class="quick-search-wrap">
717 <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; ?>" />
718 <span class="spinner"></span>
719 <?php submit_button( __( 'Search' ), 'button-small quick-search-submit button-secondary hide-if-js', 'submit', false, array( 'id' => 'submit-quick-search-taxonomy-' . $taxonomy_name ) ); ?>
722 <ul id="<?php echo $taxonomy_name; ?>-search-checklist" data-wp-lists="list:<?php echo $taxonomy_name?>" class="categorychecklist form-no-clear">
723 <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?>
725 $args['walker'] = $walker;
726 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args );
728 <?php elseif ( is_wp_error( $search_results ) ) : ?>
729 <li><?php echo $search_results->get_error_message(); ?></li>
730 <?php elseif ( ! empty( $searched ) ) : ?>
731 <li><?php _e('No results found.'); ?></li>
734 </div><!-- /.tabs-panel -->
736 <p class="button-controls">
737 <span class="list-controls">
739 echo esc_url(add_query_arg(
741 $taxonomy_name . '-tab' => 'all',
744 remove_query_arg($removed_args)
746 ?>#taxonomy-<?php echo $taxonomy_name; ?>" class="select-all"><?php _e('Select All'); ?></a>
749 <span class="add-to-menu">
750 <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 ); ?>" />
751 <span class="spinner"></span>
755 </div><!-- /.taxonomydiv -->
760 * Save posted nav menu item data.
764 * @param int $menu_id The menu ID for which to save this item. $menu_id of 0 makes a draft, orphaned menu item.
765 * @param array $menu_data The unsanitized posted menu item data.
766 * @return array The database IDs of the items saved
768 function wp_save_nav_menu_items( $menu_id = 0, $menu_data = array() ) {
769 $menu_id = (int) $menu_id;
770 $items_saved = array();
772 if ( 0 == $menu_id || is_nav_menu( $menu_id ) ) {
774 // Loop through all the menu items' POST values.
775 foreach ( (array) $menu_data as $_possible_db_id => $_item_object_data ) {
777 // Checkbox is not checked.
778 empty( $_item_object_data['menu-item-object-id'] ) &&
780 // And item type either isn't set.
781 ! isset( $_item_object_data['menu-item-type'] ) ||
782 // Or URL is the default.
783 in_array( $_item_object_data['menu-item-url'], array( 'http://', '' ) ) ||
784 ! ( '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)
785 // Or it *is* a custom menu item that already exists.
786 ! empty( $_item_object_data['menu-item-db-id'] )
789 // Then this potential menu item is not getting added to this menu.
793 // If this possible menu item doesn't actually have a menu database ID yet.
795 empty( $_item_object_data['menu-item-db-id'] ) ||
796 ( 0 > $_possible_db_id ) ||
797 $_possible_db_id != $_item_object_data['menu-item-db-id']
801 $_actual_db_id = (int) $_item_object_data['menu-item-db-id'];
805 'menu-item-db-id' => ( isset( $_item_object_data['menu-item-db-id'] ) ? $_item_object_data['menu-item-db-id'] : '' ),
806 'menu-item-object-id' => ( isset( $_item_object_data['menu-item-object-id'] ) ? $_item_object_data['menu-item-object-id'] : '' ),
807 'menu-item-object' => ( isset( $_item_object_data['menu-item-object'] ) ? $_item_object_data['menu-item-object'] : '' ),
808 'menu-item-parent-id' => ( isset( $_item_object_data['menu-item-parent-id'] ) ? $_item_object_data['menu-item-parent-id'] : '' ),
809 'menu-item-position' => ( isset( $_item_object_data['menu-item-position'] ) ? $_item_object_data['menu-item-position'] : '' ),
810 'menu-item-type' => ( isset( $_item_object_data['menu-item-type'] ) ? $_item_object_data['menu-item-type'] : '' ),
811 'menu-item-title' => ( isset( $_item_object_data['menu-item-title'] ) ? $_item_object_data['menu-item-title'] : '' ),
812 'menu-item-url' => ( isset( $_item_object_data['menu-item-url'] ) ? $_item_object_data['menu-item-url'] : '' ),
813 'menu-item-description' => ( isset( $_item_object_data['menu-item-description'] ) ? $_item_object_data['menu-item-description'] : '' ),
814 'menu-item-attr-title' => ( isset( $_item_object_data['menu-item-attr-title'] ) ? $_item_object_data['menu-item-attr-title'] : '' ),
815 'menu-item-target' => ( isset( $_item_object_data['menu-item-target'] ) ? $_item_object_data['menu-item-target'] : '' ),
816 'menu-item-classes' => ( isset( $_item_object_data['menu-item-classes'] ) ? $_item_object_data['menu-item-classes'] : '' ),
817 'menu-item-xfn' => ( isset( $_item_object_data['menu-item-xfn'] ) ? $_item_object_data['menu-item-xfn'] : '' ),
820 $items_saved[] = wp_update_nav_menu_item( $menu_id, $_actual_db_id, $args );
828 * Adds custom arguments to some of the meta box object types.
834 * @param object $object The post type or taxonomy meta-object.
835 * @return object The post type of taxonomy object.
837 function _wp_nav_menu_meta_box_object( $object = null ) {
838 if ( isset( $object->name ) ) {
840 if ( 'page' == $object->name ) {
841 $object->_default_query = array(
842 'orderby' => 'menu_order title',
843 'post_status' => 'publish',
846 // Posts should show only published items.
847 } elseif ( 'post' == $object->name ) {
848 $object->_default_query = array(
849 'post_status' => 'publish',
852 // Categories should be in reverse chronological order.
853 } elseif ( 'category' == $object->name ) {
854 $object->_default_query = array(
859 // Custom post types should show only published items.
861 $object->_default_query = array(
862 'post_status' => 'publish',
871 * Returns the menu formatted to edit.
875 * @param int $menu_id Optional. The ID of the menu to format. Default 0.
876 * @return string|WP_Error $output The menu formatted to edit or error object on failure.
878 function wp_get_nav_menu_to_edit( $menu_id = 0 ) {
879 $menu = wp_get_nav_menu_object( $menu_id );
881 // If the menu exists, get its items.
882 if ( is_nav_menu( $menu ) ) {
883 $menu_items = wp_get_nav_menu_items( $menu->term_id, array('post_status' => 'any') );
884 $result = '<div id="menu-instructions" class="post-body-plain';
885 $result .= ( ! empty($menu_items) ) ? ' menu-instructions-inactive">' : '">';
886 $result .= '<p>' . __( 'Add menu items from the column on the left.' ) . '</p>';
889 if ( empty($menu_items) )
890 return $result . ' <ul class="menu" id="menu-to-edit"> </ul>';
893 * Filter the Walker class used when adding nav menu items.
897 * @param string $class The walker class to use. Default 'Walker_Nav_Menu_Edit'.
898 * @param int $menu_id ID of the menu being rendered.
900 $walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $menu_id );
902 if ( class_exists( $walker_class_name ) ) {
903 $walker = new $walker_class_name;
905 return new WP_Error( 'menu_walker_not_exist',
906 /* translators: %s: walker class name */
907 sprintf( __( 'The Walker class named %s does not exist.' ),
908 '<strong>' . $walker_class_name . '</strong>'
913 $some_pending_menu_items = $some_invalid_menu_items = false;
914 foreach ( (array) $menu_items as $menu_item ) {
915 if ( isset( $menu_item->post_status ) && 'draft' == $menu_item->post_status )
916 $some_pending_menu_items = true;
917 if ( ! empty( $menu_item->_invalid ) )
918 $some_invalid_menu_items = true;
921 if ( $some_pending_menu_items )
922 $result .= '<div class="updated inline"><p>' . __('Click Save Menu to make pending menu items public.') . '</p></div>';
924 if ( $some_invalid_menu_items )
925 $result .= '<div class="error inline"><p>' . __('There are some invalid menu items. Please check or delete them.') . '</p></div>';
927 $result .= '<ul class="menu" id="menu-to-edit"> ';
928 $result .= walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $menu_items), 0, (object) array('walker' => $walker ) );
929 $result .= ' </ul> ';
931 } elseif ( is_wp_error( $menu ) ) {
938 * Returns the columns for the nav menus page.
942 * @return string|WP_Error $output The menu formatted to edit or error object on failure.
944 function wp_nav_menu_manage_columns() {
946 '_title' => __('Show advanced menu properties'),
947 'cb' => '<input type="checkbox" />',
948 'title-attribute' => __('Title Attribute'),
949 'link-target' => __('Link Target'),
950 'css-classes' => __('CSS Classes'),
951 'xfn' => __('Link Relationship (XFN)'),
952 'description' => __('Description'),
957 * Deletes orphaned draft menu items
962 * @global wpdb $wpdb WordPress database abstraction object.
964 function _wp_delete_orphaned_draft_menu_items() {
966 $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
968 // Delete orphaned draft menu items.
969 $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 ) );
971 foreach ( (array) $menu_items_to_delete as $menu_item_id )
972 wp_delete_post( $menu_item_id, true );
976 * Saves nav menu items
980 * @param int|string $nav_menu_selected_id (id, slug, or name ) of the currently-selected menu
981 * @param string $nav_menu_selected_title Title of the currently-selected menu
982 * @return array $messages The menu updated message
984 function wp_nav_menu_update_menu_items ( $nav_menu_selected_id, $nav_menu_selected_title ) {
985 $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' ) );
987 $menu_items = array();
988 // Index menu items by db ID
989 foreach ( $unsorted_menu_items as $_item )
990 $menu_items[$_item->db_id] = $_item;
992 $post_fields = array(
993 'menu-item-db-id', 'menu-item-object-id', 'menu-item-object',
994 'menu-item-parent-id', 'menu-item-position', 'menu-item-type',
995 'menu-item-title', 'menu-item-url', 'menu-item-description',
996 'menu-item-attr-title', 'menu-item-target', 'menu-item-classes', 'menu-item-xfn'
999 wp_defer_term_counting( true );
1000 // Loop through all the menu items' POST variables
1001 if ( ! empty( $_POST['menu-item-db-id'] ) ) {
1002 foreach ( (array) $_POST['menu-item-db-id'] as $_key => $k ) {
1004 // Menu item title can't be blank
1005 if ( ! isset( $_POST['menu-item-title'][ $_key ] ) || '' == $_POST['menu-item-title'][ $_key ] )
1009 foreach ( $post_fields as $field )
1010 $args[$field] = isset( $_POST[$field][$_key] ) ? $_POST[$field][$_key] : '';
1012 $menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key ), $args );
1014 if ( is_wp_error( $menu_item_db_id ) ) {
1015 $messages[] = '<div id="message" class="error"><p>' . $menu_item_db_id->get_error_message() . '</p></div>';
1017 unset( $menu_items[ $menu_item_db_id ] );
1022 // Remove menu items from the menu that weren't in $_POST
1023 if ( ! empty( $menu_items ) ) {
1024 foreach ( array_keys( $menu_items ) as $menu_item_id ) {
1025 if ( is_nav_menu_item( $menu_item_id ) ) {
1026 wp_delete_post( $menu_item_id );
1031 // Store 'auto-add' pages.
1032 $auto_add = ! empty( $_POST['auto-add-pages'] );
1033 $nav_menu_option = (array) get_option( 'nav_menu_options' );
1034 if ( ! isset( $nav_menu_option['auto_add'] ) )
1035 $nav_menu_option['auto_add'] = array();
1037 if ( ! in_array( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) )
1038 $nav_menu_option['auto_add'][] = $nav_menu_selected_id;
1040 if ( false !== ( $key = array_search( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) ) )
1041 unset( $nav_menu_option['auto_add'][$key] );
1043 // Remove nonexistent/deleted menus
1044 $nav_menu_option['auto_add'] = array_intersect( $nav_menu_option['auto_add'], wp_get_nav_menus( array( 'fields' => 'ids' ) ) );
1045 update_option( 'nav_menu_options', $nav_menu_option );
1047 wp_defer_term_counting( false );
1049 /** This action is documented in wp-includes/nav-menu.php */
1050 do_action( 'wp_update_nav_menu', $nav_menu_selected_id );
1052 $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' .
1053 /* translators: %s: nav menu title */
1054 sprintf( __( '%s has been updated.' ),
1055 '<strong>' . $nav_menu_selected_title . '</strong>'
1058 unset( $menu_items, $unsorted_menu_items );