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