]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/nav-menu.php
Wordpress 4.5.3-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / nav-menu.php
1 <?php
2 /**
3  * Core Navigation Menu API
4  *
5  * @package WordPress
6  * @subpackage Nav_Menus
7  * @since 3.0.0
8  */
9
10 /** Walker_Nav_Menu_Edit class */
11 require_once( ABSPATH . 'wp-admin/includes/class-walker-nav-menu-edit.php' );
12
13 /** Walker_Nav_Menu_Checklist class */
14 require_once( ABSPATH . 'wp-admin/includes/class-walker-nav-menu-checklist.php' );
15
16 /**
17  * Prints the appropriate response to a menu quick search.
18  *
19  * @since 3.0.0
20  *
21  * @param array $request The unsanitized request values.
22  */
23 function _wp_ajax_menu_quick_search( $request = array() ) {
24         $args = 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';
29
30         if ( 'markup' == $response_format ) {
31                 $args['walker'] = new Walker_Nav_Menu_Checklist;
32         }
33
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 ) {
41                                         echo wp_json_encode(
42                                                 array(
43                                                         'ID' => $object_id,
44                                                         'post_title' => get_the_title( $object_id ),
45                                                         'post_type' => get_post_type( $object_id ),
46                                                 )
47                                         );
48                                         echo "\n";
49                                 }
50                         }
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 );
58                                         echo wp_json_encode(
59                                                 array(
60                                                         'ID' => $object_id,
61                                                         'post_title' => $post_obj->name,
62                                                         'post_type' => $object_type,
63                                                 )
64                                         );
65                                         echo "\n";
66                                 }
67                         }
68
69                 }
70
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] ) ) {
73                         query_posts(array(
74                                 'posts_per_page' => 10,
75                                 'post_type' => $matches[2],
76                                 's' => $query,
77                         ));
78                         if ( ! have_posts() )
79                                 return;
80                         while ( have_posts() ) {
81                                 the_post();
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 ) {
86                                         echo wp_json_encode(
87                                                 array(
88                                                         'ID' => get_the_ID(),
89                                                         'post_title' => get_the_title(),
90                                                         'post_type' => get_post_type(),
91                                                 )
92                                         );
93                                         echo "\n";
94                                 }
95                         }
96                 } elseif ( 'taxonomy' == $matches[1] ) {
97                         $terms = get_terms( $matches[2], array(
98                                 'name__like' => $query,
99                                 'number' => 10,
100                         ));
101                         if ( empty( $terms ) || is_wp_error( $terms ) )
102                                 return;
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 ) {
107                                         echo wp_json_encode(
108                                                 array(
109                                                         'ID' => $term->term_id,
110                                                         'post_title' => $term->name,
111                                                         'post_type' => $matches[2],
112                                                 )
113                                         );
114                                         echo "\n";
115                                 }
116                         }
117                 }
118         }
119 }
120
121 /**
122  * Register nav menu metaboxes and advanced menu items
123  *
124  * @since 3.0.0
125  **/
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();
131
132         // Register advanced menu items (columns)
133         add_filter( 'manage_nav-menus_columns', 'wp_nav_menu_manage_columns' );
134
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', ),
140                         true);
141         }
142 }
143
144 /**
145  * Limit the amount of meta boxes to pages, posts, links, and categories for first time users.
146  *
147  * @since 3.0.0
148  *
149  * @global array $wp_meta_boxes
150  **/
151 function wp_initial_nav_menu_meta_boxes() {
152         global $wp_meta_boxes;
153
154         if ( get_user_option( 'metaboxhidden_nav-menus' ) !== false || ! is_array($wp_meta_boxes) )
155                 return;
156
157         $initial_meta_boxes = array( 'add-post-type-page', 'add-post-type-post', 'add-custom-links', 'add-category' );
158         $hidden_meta_boxes = array();
159
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 ) ) {
164                                         unset( $box['id'] );
165                                 } else {
166                                         $hidden_meta_boxes[] = $box['id'];
167                                 }
168                         }
169                 }
170         }
171
172         $user = wp_get_current_user();
173         update_user_option( $user->ID, 'metaboxhidden_nav-menus', $hidden_meta_boxes, true );
174 }
175
176 /**
177  * Creates metaboxes for any post type menu item.
178  *
179  * @since 3.0.0
180  */
181 function wp_nav_menu_post_type_meta_boxes() {
182         $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
183
184         if ( ! $post_types )
185                 return;
186
187         foreach ( $post_types as $post_type ) {
188                 /**
189                  * Filter whether a menu items meta box will be added for the current
190                  * object type.
191                  *
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.
194                  *
195                  * @since 3.0.0
196                  *
197                  * @param object $meta_box_object The current object to add a menu items
198                  *                                meta box for.
199                  */
200                 $post_type = apply_filters( 'nav_menu_meta_box_object', $post_type );
201                 if ( $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 );
206                 }
207         }
208 }
209
210 /**
211  * Creates metaboxes for any taxonomy menu item.
212  *
213  * @since 3.0.0
214  */
215 function wp_nav_menu_taxonomy_meta_boxes() {
216         $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'object' );
217
218         if ( !$taxonomies )
219                 return;
220
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 );
224                 if ( $tax ) {
225                         $id = $tax->name;
226                         add_meta_box( "add-{$id}", $tax->labels->name, 'wp_nav_menu_item_taxonomy_meta_box', 'nav-menus', 'side', 'default', $tax );
227                 }
228         }
229 }
230
231 /**
232  * Check whether to disable the Menu Locations meta box submit button
233  *
234  * @since 3.6.0
235  *
236  * @global bool $one_theme_location_no_menus to determine if no menus exist
237  *
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
240  */
241 function wp_nav_menu_disabled_check( $nav_menu_selected_id ) {
242         global $one_theme_location_no_menus;
243
244         if ( $one_theme_location_no_menus )
245                 return false;
246
247         return disabled( $nav_menu_selected_id, 0 );
248 }
249
250 /**
251  * Displays a metabox for the custom links menu item.
252  *
253  * @since 3.0.0
254  *
255  * @global int        $_nav_menu_placeholder
256  * @global int|string $nav_menu_selected_id
257  */
258 function wp_nav_menu_item_link_meta_box() {
259         global $_nav_menu_placeholder, $nav_menu_selected_id;
260
261         $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;
262
263         ?>
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" class="wp-clearfix">
267                         <label class="howto" for="custom-menu-item-url"><?php _e( 'URL' ); ?></label>
268                         <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://" />
269                 </p>
270
271                 <p id="menu-item-name-wrap" class="wp-clearfix">
272                         <label class="howto" for="custom-menu-item-name"><?php _e( 'Link Text' ); ?></label>
273                         <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" />
274                 </p>
275
276                 <p class="button-controls wp-clearfix">
277                         <span class="add-to-menu">
278                                 <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" />
279                                 <span class="spinner"></span>
280                         </span>
281                 </p>
282
283         </div><!-- /.customlinkdiv -->
284         <?php
285 }
286
287 /**
288  * Displays a metabox for a post type menu item.
289  *
290  * @since 3.0.0
291  *
292  * @global int        $_nav_menu_placeholder
293  * @global int|string $nav_menu_selected_id
294  *
295  * @param string $object Not used.
296  * @param string $post_type The post type object.
297  */
298 function wp_nav_menu_item_post_type_meta_box( $object, $post_type ) {
299         global $_nav_menu_placeholder, $nav_menu_selected_id;
300
301         $post_type_name = $post_type['args']->name;
302
303         // Paginate browsing for large numbers of post objects.
304         $per_page = 50;
305         $pagenum = isset( $_REQUEST[$post_type_name . '-tab'] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
306         $offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;
307
308         $args = array(
309                 'offset' => $offset,
310                 'order' => 'ASC',
311                 'orderby' => 'title',
312                 'posts_per_page' => $per_page,
313                 'post_type' => $post_type_name,
314                 'suppress_filters' => true,
315                 'update_post_term_cache' => false,
316                 'update_post_meta_cache' => false
317         );
318
319         if ( isset( $post_type['args']->_default_query ) )
320                 $args = array_merge($args, (array) $post_type['args']->_default_query );
321
322         // @todo transient caching of these results with proper invalidation on updating of a post of this type
323         $get_posts = new WP_Query;
324         $posts = $get_posts->query( $args );
325         if ( ! $get_posts->post_count ) {
326                 echo '<p>' . __( 'No items.' ) . '</p>';
327                 return;
328         }
329
330         $num_pages = $get_posts->max_num_pages;
331
332         $page_links = paginate_links( array(
333                 'base' => add_query_arg(
334                         array(
335                                 $post_type_name . '-tab' => 'all',
336                                 'paged' => '%#%',
337                                 'item-type' => 'post_type',
338                                 'item-object' => $post_type_name,
339                         )
340                 ),
341                 'format' => '',
342                 'prev_text' => __('&laquo;'),
343                 'next_text' => __('&raquo;'),
344                 'total' => $num_pages,
345                 'current' => $pagenum
346         ));
347
348         $db_fields = false;
349         if ( is_post_type_hierarchical( $post_type_name ) ) {
350                 $db_fields = array( 'parent' => 'post_parent', 'id' => 'ID' );
351         }
352
353         $walker = new Walker_Nav_Menu_Checklist( $db_fields );
354
355         $current_tab = 'most-recent';
356         if ( isset( $_REQUEST[$post_type_name . '-tab'] ) && in_array( $_REQUEST[$post_type_name . '-tab'], array('all', 'search') ) ) {
357                 $current_tab = $_REQUEST[$post_type_name . '-tab'];
358         }
359
360         if ( ! empty( $_REQUEST['quick-search-posttype-' . $post_type_name] ) ) {
361                 $current_tab = 'search';
362         }
363
364         $removed_args = array(
365                 'action',
366                 'customlink-tab',
367                 'edit-menu-item',
368                 'menu-item',
369                 'page-tab',
370                 '_wpnonce',
371         );
372
373         ?>
374         <div id="posttype-<?php echo $post_type_name; ?>" class="posttypediv">
375                 <ul id="posttype-<?php echo $post_type_name; ?>-tabs" class="posttype-tabs add-menu-item-tabs">
376                         <li <?php echo ( 'most-recent' == $current_tab ? ' class="tabs"' : '' ); ?>>
377                                 <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">
378                                         <?php _e( 'Most Recent' ); ?>
379                                 </a>
380                         </li>
381                         <li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>>
382                                 <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">
383                                         <?php _e( 'View All' ); ?>
384                                 </a>
385                         </li>
386                         <li <?php echo ( 'search' == $current_tab ? ' class="tabs"' : '' ); ?>>
387                                 <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">
388                                         <?php _e( 'Search'); ?>
389                                 </a>
390                         </li>
391                 </ul><!-- .posttype-tabs -->
392
393                 <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent" class="tabs-panel <?php
394                         echo ( 'most-recent' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
395                 ?>">
396                         <ul id="<?php echo $post_type_name; ?>checklist-most-recent" class="categorychecklist form-no-clear">
397                                 <?php
398                                 $recent_args = array_merge( $args, array( 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => 15 ) );
399                                 $most_recent = $get_posts->query( $recent_args );
400                                 $args['walker'] = $walker;
401
402                                 /**
403                                  * Filter the posts displayed in the 'Most Recent' tab of the current
404                                  * post type's menu items meta box.
405                                  *
406                                  * The dynamic portion of the hook name, `$post_type_name`, refers to the post type name.
407                                  *
408                                  * @since 4.3.0
409                                  *
410                                  * @param array  $most_recent An array of post objects being listed.
411                                  * @param array  $args        An array of WP_Query arguments.
412                                  * @param object $post_type   The current post type object for this menu item meta box.
413                                  */
414                                 $most_recent = apply_filters( "nav_menu_items_{$post_type_name}_recent", $most_recent, $args, $post_type );
415
416                                 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $most_recent), 0, (object) $args );
417                                 ?>
418                         </ul>
419                 </div><!-- /.tabs-panel -->
420
421                 <div class="tabs-panel <?php
422                         echo ( 'search' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
423                 ?>" id="tabs-panel-posttype-<?php echo $post_type_name; ?>-search">
424                         <?php
425                         if ( isset( $_REQUEST['quick-search-posttype-' . $post_type_name] ) ) {
426                                 $searched = esc_attr( $_REQUEST['quick-search-posttype-' . $post_type_name] );
427                                 $search_results = get_posts( array( 's' => $searched, 'post_type' => $post_type_name, 'fields' => 'all', 'order' => 'DESC', ) );
428                         } else {
429                                 $searched = '';
430                                 $search_results = array();
431                         }
432                         ?>
433                         <p class="quick-search-wrap">
434                                 <label for="quick-search-posttype-<?php echo $post_type_name; ?>" class="screen-reader-text"><?php _e( 'Search' ); ?></label>
435                                 <input type="search" class="quick-search" value="<?php echo $searched; ?>" name="quick-search-posttype-<?php echo $post_type_name; ?>" id="quick-search-posttype-<?php echo $post_type_name; ?>" />
436                                 <span class="spinner"></span>
437                                 <?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 ) ); ?>
438                         </p>
439
440                         <ul id="<?php echo $post_type_name; ?>-search-checklist" data-wp-lists="list:<?php echo $post_type_name?>" class="categorychecklist form-no-clear">
441                         <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?>
442                                 <?php
443                                 $args['walker'] = $walker;
444                                 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args );
445                                 ?>
446                         <?php elseif ( is_wp_error( $search_results ) ) : ?>
447                                 <li><?php echo $search_results->get_error_message(); ?></li>
448                         <?php elseif ( ! empty( $searched ) ) : ?>
449                                 <li><?php _e('No results found.'); ?></li>
450                         <?php endif; ?>
451                         </ul>
452                 </div><!-- /.tabs-panel -->
453
454                 <div id="<?php echo $post_type_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php
455                         echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
456                 ?>">
457                         <?php if ( ! empty( $page_links ) ) : ?>
458                                 <div class="add-menu-item-pagelinks">
459                                         <?php echo $page_links; ?>
460                                 </div>
461                         <?php endif; ?>
462                         <ul id="<?php echo $post_type_name; ?>checklist" data-wp-lists="list:<?php echo $post_type_name?>" class="categorychecklist form-no-clear">
463                                 <?php
464                                 $args['walker'] = $walker;
465
466                                 /*
467                                  * If we're dealing with pages, let's put a checkbox for the front
468                                  * page at the top of the list.
469                                  */
470                                 if ( 'page' == $post_type_name ) {
471                                         $front_page = 'page' == get_option('show_on_front') ? (int) get_option( 'page_on_front' ) : 0;
472                                         if ( ! empty( $front_page ) ) {
473                                                 $front_page_obj = get_post( $front_page );
474                                                 $front_page_obj->front_or_home = true;
475                                                 array_unshift( $posts, $front_page_obj );
476                                         } else {
477                                                 $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1;
478                                                 array_unshift( $posts, (object) array(
479                                                         'front_or_home' => true,
480                                                         'ID' => 0,
481                                                         'object_id' => $_nav_menu_placeholder,
482                                                         'post_content' => '',
483                                                         'post_excerpt' => '',
484                                                         'post_parent' => '',
485                                                         'post_title' => _x('Home', 'nav menu home label'),
486                                                         'post_type' => 'nav_menu_item',
487                                                         'type' => 'custom',
488                                                         'url' => home_url('/'),
489                                                 ) );
490                                         }
491                                 }
492
493                                 $post_type = get_post_type_object( $post_type_name );
494                                 $archive_link = get_post_type_archive_link( $post_type_name );
495                                 if ( $post_type->has_archive ) {
496                                         $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1;
497                                         array_unshift( $posts, (object) array(
498                                                 'ID' => 0,
499                                                 'object_id' => $_nav_menu_placeholder,
500                                                 'object'     => $post_type_name,
501                                                 'post_content' => '',
502                                                 'post_excerpt' => '',
503                                                 'post_title' => $post_type->labels->archives,
504                                                 'post_type' => 'nav_menu_item',
505                                                 'type' => 'post_type_archive',
506                                                 'url' => get_post_type_archive_link( $post_type_name ),
507                                         ) );
508                                 }
509
510                                 /**
511                                  * Filter the posts displayed in the 'View All' tab of the current
512                                  * post type's menu items meta box.
513                                  *
514                                  * The dynamic portion of the hook name, `$post_type_name`, refers
515                                  * to the slug of the current post type.
516                                  *
517                                  * @since 3.2.0
518                                  *
519                                  * @see WP_Query::query()
520                                  *
521                                  * @param array  $posts     The posts for the current post type.
522                                  * @param array  $args      An array of WP_Query arguments.
523                                  * @param object $post_type The current post type object for this menu item meta box.
524                                  */
525                                 $posts = apply_filters( "nav_menu_items_{$post_type_name}", $posts, $args, $post_type );
526                                 $checkbox_items = walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $posts), 0, (object) $args );
527
528                                 if ( 'all' == $current_tab && ! empty( $_REQUEST['selectall'] ) ) {
529                                         $checkbox_items = preg_replace('/(type=(.)checkbox(\2))/', '$1 checked=$2checked$2', $checkbox_items);
530
531                                 }
532
533                                 echo $checkbox_items;
534                                 ?>
535                         </ul>
536                         <?php if ( ! empty( $page_links ) ) : ?>
537                                 <div class="add-menu-item-pagelinks">
538                                         <?php echo $page_links; ?>
539                                 </div>
540                         <?php endif; ?>
541                 </div><!-- /.tabs-panel -->
542
543                 <p class="button-controls wp-clearfix">
544                         <span class="list-controls">
545                                 <a href="<?php
546                                         echo esc_url( add_query_arg(
547                                                 array(
548                                                         $post_type_name . '-tab' => 'all',
549                                                         'selectall' => 1,
550                                                 ),
551                                                 remove_query_arg( $removed_args )
552                                         ));
553                                 ?>#posttype-<?php echo $post_type_name; ?>" class="select-all"><?php _e('Select All'); ?></a>
554                         </span>
555
556                         <span class="add-to-menu">
557                                 <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 ); ?>" />
558                                 <span class="spinner"></span>
559                         </span>
560                 </p>
561
562         </div><!-- /.posttypediv -->
563         <?php
564 }
565
566 /**
567  * Displays a metabox for a taxonomy menu item.
568  *
569  * @since 3.0.0
570  *
571  * @global int|string $nav_menu_selected_id
572  *
573  * @param string $object Not used.
574  * @param string $taxonomy The taxonomy object.
575  */
576 function wp_nav_menu_item_taxonomy_meta_box( $object, $taxonomy ) {
577         global $nav_menu_selected_id;
578         $taxonomy_name = $taxonomy['args']->name;
579
580         // Paginate browsing for large numbers of objects.
581         $per_page = 50;
582         $pagenum = isset( $_REQUEST[$taxonomy_name . '-tab'] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
583         $offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;
584
585         $args = array(
586                 'child_of' => 0,
587                 'exclude' => '',
588                 'hide_empty' => false,
589                 'hierarchical' => 1,
590                 'include' => '',
591                 'number' => $per_page,
592                 'offset' => $offset,
593                 'order' => 'ASC',
594                 'orderby' => 'name',
595                 'pad_counts' => false,
596         );
597
598         $terms = get_terms( $taxonomy_name, $args );
599
600         if ( ! $terms || is_wp_error($terms) ) {
601                 echo '<p>' . __( 'No items.' ) . '</p>';
602                 return;
603         }
604
605         $num_pages = ceil( wp_count_terms( $taxonomy_name , array_merge( $args, array('number' => '', 'offset' => '') ) ) / $per_page );
606
607         $page_links = paginate_links( array(
608                 'base' => add_query_arg(
609                         array(
610                                 $taxonomy_name . '-tab' => 'all',
611                                 'paged' => '%#%',
612                                 'item-type' => 'taxonomy',
613                                 'item-object' => $taxonomy_name,
614                         )
615                 ),
616                 'format' => '',
617                 'prev_text' => __('&laquo;'),
618                 'next_text' => __('&raquo;'),
619                 'total' => $num_pages,
620                 'current' => $pagenum
621         ));
622
623         $db_fields = false;
624         if ( is_taxonomy_hierarchical( $taxonomy_name ) ) {
625                 $db_fields = array( 'parent' => 'parent', 'id' => 'term_id' );
626         }
627
628         $walker = new Walker_Nav_Menu_Checklist( $db_fields );
629
630         $current_tab = 'most-used';
631         if ( isset( $_REQUEST[$taxonomy_name . '-tab'] ) && in_array( $_REQUEST[$taxonomy_name . '-tab'], array('all', 'most-used', 'search') ) ) {
632                 $current_tab = $_REQUEST[$taxonomy_name . '-tab'];
633         }
634
635         if ( ! empty( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] ) ) {
636                 $current_tab = 'search';
637         }
638
639         $removed_args = array(
640                 'action',
641                 'customlink-tab',
642                 'edit-menu-item',
643                 'menu-item',
644                 'page-tab',
645                 '_wpnonce',
646         );
647
648         ?>
649         <div id="taxonomy-<?php echo $taxonomy_name; ?>" class="taxonomydiv">
650                 <ul id="taxonomy-<?php echo $taxonomy_name; ?>-tabs" class="taxonomy-tabs add-menu-item-tabs">
651                         <li <?php echo ( 'most-used' == $current_tab ? ' class="tabs"' : '' ); ?>>
652                                 <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">
653                                         <?php _e( 'Most Used' ); ?>
654                                 </a>
655                         </li>
656                         <li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>>
657                                 <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">
658                                         <?php _e( 'View All' ); ?>
659                                 </a>
660                         </li>
661                         <li <?php echo ( 'search' == $current_tab ? ' class="tabs"' : '' ); ?>>
662                                 <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; ?>">
663                                         <?php _e( 'Search' ); ?>
664                                 </a>
665                         </li>
666                 </ul><!-- .taxonomy-tabs -->
667
668                 <div id="tabs-panel-<?php echo $taxonomy_name; ?>-pop" class="tabs-panel <?php
669                         echo ( 'most-used' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
670                 ?>">
671                         <ul id="<?php echo $taxonomy_name; ?>checklist-pop" class="categorychecklist form-no-clear" >
672                                 <?php
673                                 $popular_terms = get_terms( $taxonomy_name, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
674                                 $args['walker'] = $walker;
675                                 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $popular_terms), 0, (object) $args );
676                                 ?>
677                         </ul>
678                 </div><!-- /.tabs-panel -->
679
680                 <div id="tabs-panel-<?php echo $taxonomy_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php
681                         echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
682                 ?>">
683                         <?php if ( ! empty( $page_links ) ) : ?>
684                                 <div class="add-menu-item-pagelinks">
685                                         <?php echo $page_links; ?>
686                                 </div>
687                         <?php endif; ?>
688                         <ul id="<?php echo $taxonomy_name; ?>checklist" data-wp-lists="list:<?php echo $taxonomy_name?>" class="categorychecklist form-no-clear">
689                                 <?php
690                                 $args['walker'] = $walker;
691                                 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $terms), 0, (object) $args );
692                                 ?>
693                         </ul>
694                         <?php if ( ! empty( $page_links ) ) : ?>
695                                 <div class="add-menu-item-pagelinks">
696                                         <?php echo $page_links; ?>
697                                 </div>
698                         <?php endif; ?>
699                 </div><!-- /.tabs-panel -->
700
701                 <div class="tabs-panel <?php
702                         echo ( 'search' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
703                 ?>" id="tabs-panel-search-taxonomy-<?php echo $taxonomy_name; ?>">
704                         <?php
705                         if ( isset( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] ) ) {
706                                 $searched = esc_attr( $_REQUEST['quick-search-taxonomy-' . $taxonomy_name] );
707                                 $search_results = get_terms( $taxonomy_name, array( 'name__like' => $searched, 'fields' => 'all', 'orderby' => 'count', 'order' => 'DESC', 'hierarchical' => false ) );
708                         } else {
709                                 $searched = '';
710                                 $search_results = array();
711                         }
712                         ?>
713                         <p class="quick-search-wrap">
714                                 <label for="quick-search-taxonomy-<?php echo $taxonomy_name; ?>" class="screen-reader-text"><?php _e( 'Search' ); ?></label>
715                                 <input type="search" class="quick-search" value="<?php echo $searched; ?>" name="quick-search-taxonomy-<?php echo $taxonomy_name; ?>" id="quick-search-taxonomy-<?php echo $taxonomy_name; ?>" />
716                                 <span class="spinner"></span>
717                                 <?php submit_button( __( 'Search' ), 'button-small quick-search-submit button-secondary hide-if-js', 'submit', false, array( 'id' => 'submit-quick-search-taxonomy-' . $taxonomy_name ) ); ?>
718                         </p>
719
720                         <ul id="<?php echo $taxonomy_name; ?>-search-checklist" data-wp-lists="list:<?php echo $taxonomy_name?>" class="categorychecklist form-no-clear">
721                         <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?>
722                                 <?php
723                                 $args['walker'] = $walker;
724                                 echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args );
725                                 ?>
726                         <?php elseif ( is_wp_error( $search_results ) ) : ?>
727                                 <li><?php echo $search_results->get_error_message(); ?></li>
728                         <?php elseif ( ! empty( $searched ) ) : ?>
729                                 <li><?php _e('No results found.'); ?></li>
730                         <?php endif; ?>
731                         </ul>
732                 </div><!-- /.tabs-panel -->
733
734                 <p class="button-controls wp-clearfix">
735                         <span class="list-controls">
736                                 <a href="<?php
737                                         echo esc_url(add_query_arg(
738                                                 array(
739                                                         $taxonomy_name . '-tab' => 'all',
740                                                         'selectall' => 1,
741                                                 ),
742                                                 remove_query_arg($removed_args)
743                                         ));
744                                 ?>#taxonomy-<?php echo $taxonomy_name; ?>" class="select-all"><?php _e('Select All'); ?></a>
745                         </span>
746
747                         <span class="add-to-menu">
748                                 <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 ); ?>" />
749                                 <span class="spinner"></span>
750                         </span>
751                 </p>
752
753         </div><!-- /.taxonomydiv -->
754         <?php
755 }
756
757 /**
758  * Save posted nav menu item data.
759  *
760  * @since 3.0.0
761  *
762  * @param int $menu_id The menu ID for which to save this item. $menu_id of 0 makes a draft, orphaned menu item.
763  * @param array $menu_data The unsanitized posted menu item data.
764  * @return array The database IDs of the items saved
765  */
766 function wp_save_nav_menu_items( $menu_id = 0, $menu_data = array() ) {
767         $menu_id = (int) $menu_id;
768         $items_saved = array();
769
770         if ( 0 == $menu_id || is_nav_menu( $menu_id ) ) {
771
772                 // Loop through all the menu items' POST values.
773                 foreach ( (array) $menu_data as $_possible_db_id => $_item_object_data ) {
774                         if (
775                                 // Checkbox is not checked.
776                                 empty( $_item_object_data['menu-item-object-id'] ) &&
777                                 (
778                                         // And item type either isn't set.
779                                         ! isset( $_item_object_data['menu-item-type'] ) ||
780                                         // Or URL is the default.
781                                         in_array( $_item_object_data['menu-item-url'], array( 'http://', '' ) ) ||
782                                         ! ( '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)
783                                         // Or it *is* a custom menu item that already exists.
784                                         ! empty( $_item_object_data['menu-item-db-id'] )
785                                 )
786                         ) {
787                                 // Then this potential menu item is not getting added to this menu.
788                                 continue;
789                         }
790
791                         // If this possible menu item doesn't actually have a menu database ID yet.
792                         if (
793                                 empty( $_item_object_data['menu-item-db-id'] ) ||
794                                 ( 0 > $_possible_db_id ) ||
795                                 $_possible_db_id != $_item_object_data['menu-item-db-id']
796                         ) {
797                                 $_actual_db_id = 0;
798                         } else {
799                                 $_actual_db_id = (int) $_item_object_data['menu-item-db-id'];
800                         }
801
802                         $args = array(
803                                 'menu-item-db-id' => ( isset( $_item_object_data['menu-item-db-id'] ) ? $_item_object_data['menu-item-db-id'] : '' ),
804                                 'menu-item-object-id' => ( isset( $_item_object_data['menu-item-object-id'] ) ? $_item_object_data['menu-item-object-id'] : '' ),
805                                 'menu-item-object' => ( isset( $_item_object_data['menu-item-object'] ) ? $_item_object_data['menu-item-object'] : '' ),
806                                 'menu-item-parent-id' => ( isset( $_item_object_data['menu-item-parent-id'] ) ? $_item_object_data['menu-item-parent-id'] : '' ),
807                                 'menu-item-position' => ( isset( $_item_object_data['menu-item-position'] ) ? $_item_object_data['menu-item-position'] : '' ),
808                                 'menu-item-type' => ( isset( $_item_object_data['menu-item-type'] ) ? $_item_object_data['menu-item-type'] : '' ),
809                                 'menu-item-title' => ( isset( $_item_object_data['menu-item-title'] ) ? $_item_object_data['menu-item-title'] : '' ),
810                                 'menu-item-url' => ( isset( $_item_object_data['menu-item-url'] ) ? $_item_object_data['menu-item-url'] : '' ),
811                                 'menu-item-description' => ( isset( $_item_object_data['menu-item-description'] ) ? $_item_object_data['menu-item-description'] : '' ),
812                                 'menu-item-attr-title' => ( isset( $_item_object_data['menu-item-attr-title'] ) ? $_item_object_data['menu-item-attr-title'] : '' ),
813                                 'menu-item-target' => ( isset( $_item_object_data['menu-item-target'] ) ? $_item_object_data['menu-item-target'] : '' ),
814                                 'menu-item-classes' => ( isset( $_item_object_data['menu-item-classes'] ) ? $_item_object_data['menu-item-classes'] : '' ),
815                                 'menu-item-xfn' => ( isset( $_item_object_data['menu-item-xfn'] ) ? $_item_object_data['menu-item-xfn'] : '' ),
816                         );
817
818                         $items_saved[] = wp_update_nav_menu_item( $menu_id, $_actual_db_id, $args );
819
820                 }
821         }
822         return $items_saved;
823 }
824
825 /**
826  * Adds custom arguments to some of the meta box object types.
827  *
828  * @since 3.0.0
829  *
830  * @access private
831  *
832  * @param object $object The post type or taxonomy meta-object.
833  * @return object The post type of taxonomy object.
834  */
835 function _wp_nav_menu_meta_box_object( $object = null ) {
836         if ( isset( $object->name ) ) {
837
838                 if ( 'page' == $object->name ) {
839                         $object->_default_query = array(
840                                 'orderby' => 'menu_order title',
841                                 'post_status' => 'publish',
842                         );
843
844                 // Posts should show only published items.
845                 } elseif ( 'post' == $object->name ) {
846                         $object->_default_query = array(
847                                 'post_status' => 'publish',
848                         );
849
850                 // Categories should be in reverse chronological order.
851                 } elseif ( 'category' == $object->name ) {
852                         $object->_default_query = array(
853                                 'orderby' => 'id',
854                                 'order' => 'DESC',
855                         );
856
857                 // Custom post types should show only published items.
858                 } else {
859                         $object->_default_query = array(
860                                 'post_status' => 'publish',
861                         );
862                 }
863         }
864
865         return $object;
866 }
867
868 /**
869  * Returns the menu formatted to edit.
870  *
871  * @since 3.0.0
872  *
873  * @param int $menu_id Optional. The ID of the menu to format. Default 0.
874  * @return string|WP_Error $output The menu formatted to edit or error object on failure.
875  */
876 function wp_get_nav_menu_to_edit( $menu_id = 0 ) {
877         $menu = wp_get_nav_menu_object( $menu_id );
878
879         // If the menu exists, get its items.
880         if ( is_nav_menu( $menu ) ) {
881                 $menu_items = wp_get_nav_menu_items( $menu->term_id, array('post_status' => 'any') );
882                 $result = '<div id="menu-instructions" class="post-body-plain';
883                 $result .= ( ! empty($menu_items) ) ? ' menu-instructions-inactive">' : '">';
884                 $result .= '<p>' . __( 'Add menu items from the column on the left.' ) . '</p>';
885                 $result .= '</div>';
886
887                 if ( empty($menu_items) )
888                         return $result . ' <ul class="menu" id="menu-to-edit"> </ul>';
889
890                 /**
891                  * Filter the Walker class used when adding nav menu items.
892                  *
893                  * @since 3.0.0
894                  *
895                  * @param string $class   The walker class to use. Default 'Walker_Nav_Menu_Edit'.
896                  * @param int    $menu_id ID of the menu being rendered.
897                  */
898                 $walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $menu_id );
899
900                 if ( class_exists( $walker_class_name ) ) {
901                         $walker = new $walker_class_name;
902                 } else {
903                         return new WP_Error( 'menu_walker_not_exist',
904                                 /* translators: %s: walker class name */
905                                 sprintf( __( 'The Walker class named %s does not exist.' ),
906                                         '<strong>' . $walker_class_name . '</strong>'
907                                 )
908                         );
909                 }
910
911                 $some_pending_menu_items = $some_invalid_menu_items = false;
912                 foreach ( (array) $menu_items as $menu_item ) {
913                         if ( isset( $menu_item->post_status ) && 'draft' == $menu_item->post_status )
914                                 $some_pending_menu_items = true;
915                         if ( ! empty( $menu_item->_invalid ) )
916                                 $some_invalid_menu_items = true;
917                 }
918
919                 if ( $some_pending_menu_items )
920                         $result .= '<div class="updated inline"><p>' . __('Click Save Menu to make pending menu items public.') . '</p></div>';
921
922                 if ( $some_invalid_menu_items )
923                         $result .= '<div class="error inline"><p>' . __('There are some invalid menu items. Please check or delete them.') . '</p></div>';
924
925                 $result .= '<ul class="menu" id="menu-to-edit"> ';
926                 $result .= walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $menu_items), 0, (object) array('walker' => $walker ) );
927                 $result .= ' </ul> ';
928                 return $result;
929         } elseif ( is_wp_error( $menu ) ) {
930                 return $menu;
931         }
932
933 }
934
935 /**
936  * Returns the columns for the nav menus page.
937  *
938  * @since 3.0.0
939  *
940  * @return array Columns.
941  */
942 function wp_nav_menu_manage_columns() {
943         return array(
944                 '_title'          => __( 'Show advanced menu properties' ),
945                 'cb'              => '<input type="checkbox" />',
946                 'link-target'     => __( 'Link Target' ),
947                 'title-attribute' => __( 'Title Attribute' ),
948                 'css-classes'     => __( 'CSS Classes' ),
949                 'xfn'             => __( 'Link Relationship (XFN)' ),
950                 'description'     => __( 'Description' ),
951         );
952 }
953
954 /**
955  * Deletes orphaned draft menu items
956  *
957  * @access private
958  * @since 3.0.0
959  *
960  * @global wpdb $wpdb WordPress database abstraction object.
961  */
962 function _wp_delete_orphaned_draft_menu_items() {
963         global $wpdb;
964         $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
965
966         // Delete orphaned draft menu items.
967         $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 ) );
968
969         foreach ( (array) $menu_items_to_delete as $menu_item_id )
970                 wp_delete_post( $menu_item_id, true );
971 }
972
973 /**
974  * Saves nav menu items
975  *
976  * @since 3.6.0
977  *
978  * @param int|string $nav_menu_selected_id (id, slug, or name ) of the currently-selected menu
979  * @param string $nav_menu_selected_title Title of the currently-selected menu
980  * @return array $messages The menu updated message
981  */
982 function wp_nav_menu_update_menu_items ( $nav_menu_selected_id, $nav_menu_selected_title ) {
983         $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' ) );
984         $messages = array();
985         $menu_items = array();
986         // Index menu items by db ID
987         foreach ( $unsorted_menu_items as $_item )
988                 $menu_items[$_item->db_id] = $_item;
989
990         $post_fields = array(
991                 'menu-item-db-id', 'menu-item-object-id', 'menu-item-object',
992                 'menu-item-parent-id', 'menu-item-position', 'menu-item-type',
993                 'menu-item-title', 'menu-item-url', 'menu-item-description',
994                 'menu-item-attr-title', 'menu-item-target', 'menu-item-classes', 'menu-item-xfn'
995         );
996
997         wp_defer_term_counting( true );
998         // Loop through all the menu items' POST variables
999         if ( ! empty( $_POST['menu-item-db-id'] ) ) {
1000                 foreach ( (array) $_POST['menu-item-db-id'] as $_key => $k ) {
1001
1002                         // Menu item title can't be blank
1003                         if ( ! isset( $_POST['menu-item-title'][ $_key ] ) || '' == $_POST['menu-item-title'][ $_key ] )
1004                                 continue;
1005
1006                         $args = array();
1007                         foreach ( $post_fields as $field )
1008                                 $args[$field] = isset( $_POST[$field][$_key] ) ? $_POST[$field][$_key] : '';
1009
1010                         $menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key ), $args );
1011
1012                         if ( is_wp_error( $menu_item_db_id ) ) {
1013                                 $messages[] = '<div id="message" class="error"><p>' . $menu_item_db_id->get_error_message() . '</p></div>';
1014                         } else {
1015                                 unset( $menu_items[ $menu_item_db_id ] );
1016                         }
1017                 }
1018         }
1019
1020         // Remove menu items from the menu that weren't in $_POST
1021         if ( ! empty( $menu_items ) ) {
1022                 foreach ( array_keys( $menu_items ) as $menu_item_id ) {
1023                         if ( is_nav_menu_item( $menu_item_id ) ) {
1024                                 wp_delete_post( $menu_item_id );
1025                         }
1026                 }
1027         }
1028
1029         // Store 'auto-add' pages.
1030         $auto_add = ! empty( $_POST['auto-add-pages'] );
1031         $nav_menu_option = (array) get_option( 'nav_menu_options' );
1032         if ( ! isset( $nav_menu_option['auto_add'] ) )
1033                 $nav_menu_option['auto_add'] = array();
1034         if ( $auto_add ) {
1035                 if ( ! in_array( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) )
1036                         $nav_menu_option['auto_add'][] = $nav_menu_selected_id;
1037         } else {
1038                 if ( false !== ( $key = array_search( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) ) )
1039                         unset( $nav_menu_option['auto_add'][$key] );
1040         }
1041         // Remove nonexistent/deleted menus
1042         $nav_menu_option['auto_add'] = array_intersect( $nav_menu_option['auto_add'], wp_get_nav_menus( array( 'fields' => 'ids' ) ) );
1043         update_option( 'nav_menu_options', $nav_menu_option );
1044
1045         wp_defer_term_counting( false );
1046
1047         /** This action is documented in wp-includes/nav-menu.php */
1048         do_action( 'wp_update_nav_menu', $nav_menu_selected_id );
1049
1050         $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' .
1051                 /* translators: %s: nav menu title */
1052                 sprintf( __( '%s has been updated.' ),
1053                         '<strong>' . $nav_menu_selected_title . '</strong>'
1054                 ) . '</p></div>';
1055
1056         unset( $menu_items, $unsorted_menu_items );
1057
1058         return $messages;
1059 }