]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/nav-menu.php
WordPress 4.5
[autoinstalls/wordpress.git] / wp-includes / nav-menu.php
1 <?php
2 /**
3  * Navigation Menu functions
4  *
5  * @package WordPress
6  * @subpackage Nav_Menus
7  * @since 3.0.0
8  */
9
10 /**
11  * Returns a navigation menu object.
12  *
13  * @since 3.0.0
14  *
15  * @param string $menu Menu ID, slug, or name - or the menu object.
16  * @return object|false False if $menu param isn't supplied or term does not exist, menu object if successful.
17  */
18 function wp_get_nav_menu_object( $menu ) {
19         $menu_obj = false;
20
21         if ( is_object( $menu ) ) {
22                 $menu_obj = $menu;
23         }
24
25         if ( $menu && ! $menu_obj ) {
26                 $menu_obj = get_term( $menu, 'nav_menu' );
27
28                 if ( ! $menu_obj ) {
29                         $menu_obj = get_term_by( 'slug', $menu, 'nav_menu' );
30                 }
31
32                 if ( ! $menu_obj ) {
33                         $menu_obj = get_term_by( 'name', $menu, 'nav_menu' );
34                 }
35         }
36
37         if ( ! $menu_obj || is_wp_error( $menu_obj ) ) {
38                 $menu_obj = false;
39         }
40
41         /**
42          * Filter the nav_menu term retrieved for wp_get_nav_menu_object().
43          *
44          * @since 4.3.0
45          *
46          * @param object|false $menu_obj Term from nav_menu taxonomy, or false if nothing had been found.
47          * @param string       $menu     The menu ID, slug, or name passed to wp_get_nav_menu_object().
48          */
49         return apply_filters( 'wp_get_nav_menu_object', $menu_obj, $menu );
50 }
51
52 /**
53  * Check if the given ID is a navigation menu.
54  *
55  * Returns true if it is; false otherwise.
56  *
57  * @since 3.0.0
58  *
59  * @param int|string $menu The menu to check (ID, slug, or name).
60  * @return bool Whether the menu exists.
61  */
62 function is_nav_menu( $menu ) {
63         if ( ! $menu )
64                 return false;
65
66         $menu_obj = wp_get_nav_menu_object( $menu );
67
68         if (
69                 $menu_obj &&
70                 ! is_wp_error( $menu_obj ) &&
71                 ! empty( $menu_obj->taxonomy ) &&
72                 'nav_menu' == $menu_obj->taxonomy
73         )
74                 return true;
75
76         return false;
77 }
78
79 /**
80  * Registers navigation menu locations for a theme.
81  *
82  * @since 3.0.0
83  *
84  * @global array $_wp_registered_nav_menus
85  *
86  * @param array $locations Associative array of menu location identifiers (like a slug) and descriptive text.
87  */
88 function register_nav_menus( $locations = array() ) {
89         global $_wp_registered_nav_menus;
90
91         add_theme_support( 'menus' );
92
93         $_wp_registered_nav_menus = array_merge( (array) $_wp_registered_nav_menus, $locations );
94 }
95
96 /**
97  * Unregisters a navigation menu location for a theme.
98  *
99  * @global array $_wp_registered_nav_menus
100  *
101  * @param string $location The menu location identifier.
102  * @return bool True on success, false on failure.
103  */
104 function unregister_nav_menu( $location ) {
105         global $_wp_registered_nav_menus;
106
107         if ( is_array( $_wp_registered_nav_menus ) && isset( $_wp_registered_nav_menus[$location] ) ) {
108                 unset( $_wp_registered_nav_menus[$location] );
109                 if ( empty( $_wp_registered_nav_menus ) ) {
110                         _remove_theme_support( 'menus' );
111                 }
112                 return true;
113         }
114         return false;
115 }
116
117 /**
118  * Registers a navigation menu location for a theme.
119  *
120  * @since 3.0.0
121  *
122  * @param string $location    Menu location identifier, like a slug.
123  * @param string $description Menu location descriptive text.
124  */
125 function register_nav_menu( $location, $description ) {
126         register_nav_menus( array( $location => $description ) );
127 }
128 /**
129  * Returns all registered navigation menu locations in a theme.
130  *
131  * @since 3.0.0
132  *
133  * @global array $_wp_registered_nav_menus
134  *
135  * @return array
136  */
137 function get_registered_nav_menus() {
138         global $_wp_registered_nav_menus;
139         if ( isset( $_wp_registered_nav_menus ) )
140                 return $_wp_registered_nav_menus;
141         return array();
142 }
143
144 /**
145  * Returns an array with the registered navigation menu locations and the menu assigned to it
146  *
147  * @since 3.0.0
148  * @return array
149  */
150
151 function get_nav_menu_locations() {
152         $locations = get_theme_mod( 'nav_menu_locations' );
153         return ( is_array( $locations ) ) ? $locations : array();
154 }
155
156 /**
157  * Determines whether a registered nav menu location has a menu assigned to it.
158  *
159  * @since 3.0.0
160  *
161  * @param string $location Menu location identifier.
162  * @return bool Whether location has a menu.
163  */
164 function has_nav_menu( $location ) {
165         $has_nav_menu = false;
166
167         $registered_nav_menus = get_registered_nav_menus();
168         if ( isset( $registered_nav_menus[ $location ] ) ) {
169                 $locations = get_nav_menu_locations();
170                 $has_nav_menu = ! empty( $locations[ $location ] );
171         }
172
173         /**
174          * Filter whether a nav menu is assigned to the specified location.
175          *
176          * @since 4.3.0
177          *
178          * @param bool   $has_nav_menu Whether there is a menu assigned to a location.
179          * @param string $location     Menu location.
180          */
181         return apply_filters( 'has_nav_menu', $has_nav_menu, $location );
182 }
183
184 /**
185  * Determines whether the given ID is a nav menu item.
186  *
187  * @since 3.0.0
188  *
189  * @param int $menu_item_id The ID of the potential nav menu item.
190  * @return bool Whether the given ID is that of a nav menu item.
191  */
192 function is_nav_menu_item( $menu_item_id = 0 ) {
193         return ( ! is_wp_error( $menu_item_id ) && ( 'nav_menu_item' == get_post_type( $menu_item_id ) ) );
194 }
195
196 /**
197  * Creates a navigation menu.
198  *
199  * Note that `$menu_name` is expected to be pre-slashed.
200  *
201  * @since 3.0.0
202  *
203  * @param string $menu_name Menu name.
204  * @return int|WP_Error Menu ID on success, WP_Error object on failure.
205  */
206 function wp_create_nav_menu( $menu_name ) {
207         // expected_slashed ($menu_name)
208         return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) );
209 }
210
211 /**
212  * Delete a Navigation Menu.
213  *
214  * @since 3.0.0
215  *
216  * @param string $menu Menu ID, slug, or name.
217  * @return bool|WP_Error True on success, false or WP_Error object on failure.
218  */
219 function wp_delete_nav_menu( $menu ) {
220         $menu = wp_get_nav_menu_object( $menu );
221         if ( ! $menu )
222                 return false;
223
224         $menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' );
225         if ( ! empty( $menu_objects ) ) {
226                 foreach ( $menu_objects as $item ) {
227                         wp_delete_post( $item );
228                 }
229         }
230
231         $result = wp_delete_term( $menu->term_id, 'nav_menu' );
232
233         // Remove this menu from any locations.
234         $locations = get_nav_menu_locations();
235         foreach ( $locations as $location => $menu_id ) {
236                 if ( $menu_id == $menu->term_id )
237                         $locations[ $location ] = 0;
238         }
239         set_theme_mod( 'nav_menu_locations', $locations );
240
241         if ( $result && !is_wp_error($result) )
242
243                 /**
244                  * Fires after a navigation menu has been successfully deleted.
245                  *
246                  * @since 3.0.0
247                  *
248                  * @param int $term_id ID of the deleted menu.
249                  */
250                 do_action( 'wp_delete_nav_menu', $menu->term_id );
251
252         return $result;
253 }
254
255 /**
256  * Save the properties of a menu or create a new menu with those properties.
257  *
258  * Note that `$menu_data` is expected to be pre-slashed.
259  *
260  * @since 3.0.0
261  *
262  * @param int   $menu_id   The ID of the menu or "0" to create a new menu.
263  * @param array $menu_data The array of menu data.
264  * @return int|WP_Error Menu ID on success, WP_Error object on failure.
265  */
266 function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
267         // expected_slashed ($menu_data)
268         $menu_id = (int) $menu_id;
269
270         $_menu = wp_get_nav_menu_object( $menu_id );
271
272         $args = array(
273                 'description' => ( isset( $menu_data['description'] ) ? $menu_data['description']  : '' ),
274                 'name'        => ( isset( $menu_data['menu-name']   ) ? $menu_data['menu-name']    : '' ),
275                 'parent'      => ( isset( $menu_data['parent']      ) ? (int) $menu_data['parent'] : 0  ),
276                 'slug'        => null,
277         );
278
279         // double-check that we're not going to have one menu take the name of another
280         $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
281         if (
282                 $_possible_existing &&
283                 ! is_wp_error( $_possible_existing ) &&
284                 isset( $_possible_existing->term_id ) &&
285                 $_possible_existing->term_id != $menu_id
286         ) {
287                 return new WP_Error( 'menu_exists',
288                         /* translators: %s: menu name */
289                         sprintf( __( 'The menu name %s conflicts with another menu name. Please try another.' ),
290                                 '<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>'
291                         )
292                 );
293         }
294
295         // menu doesn't already exist, so create a new menu
296         if ( ! $_menu || is_wp_error( $_menu ) ) {
297                 $menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
298
299                 if ( $menu_exists ) {
300                         return new WP_Error( 'menu_exists',
301                                 /* translators: %s: menu name */
302                                 sprintf( __( 'The menu name %s conflicts with another menu name. Please try another.' ),
303                                         '<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>'
304                                 )
305                         );
306                 }
307
308                 $_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args );
309
310                 if ( is_wp_error( $_menu ) )
311                         return $_menu;
312
313                 /**
314                  * Fires after a navigation menu is successfully created.
315                  *
316                  * @since 3.0.0
317                  *
318                  * @param int   $term_id   ID of the new menu.
319                  * @param array $menu_data An array of menu data.
320                  */
321                 do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data );
322
323                 return (int) $_menu['term_id'];
324         }
325
326         if ( ! $_menu || ! isset( $_menu->term_id ) )
327                 return 0;
328
329         $menu_id = (int) $_menu->term_id;
330
331         $update_response = wp_update_term( $menu_id, 'nav_menu', $args );
332
333         if ( is_wp_error( $update_response ) )
334                 return $update_response;
335
336         $menu_id = (int) $update_response['term_id'];
337
338         /**
339          * Fires after a navigation menu has been successfully updated.
340          *
341          * @since 3.0.0
342          *
343          * @param int   $menu_id   ID of the updated menu.
344          * @param array $menu_data An array of menu data.
345          */
346         do_action( 'wp_update_nav_menu', $menu_id, $menu_data );
347         return $menu_id;
348 }
349
350 /**
351  * Save the properties of a menu item or create a new one.
352  *
353  * The menu-item-title, menu-item-description, and menu-item-attr-title are expected
354  * to be pre-slashed since they are passed directly into `wp_insert_post()`.
355  *
356  * @since 3.0.0
357  *
358  * @param int   $menu_id         The ID of the menu. Required. If "0", makes the menu item a draft orphan.
359  * @param int   $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
360  * @param array $menu_item_data  The menu item's data.
361  * @return int|WP_Error The menu item's database ID or WP_Error object on failure.
362  */
363 function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array() ) {
364         $menu_id = (int) $menu_id;
365         $menu_item_db_id = (int) $menu_item_db_id;
366
367         // make sure that we don't convert non-nav_menu_item objects into nav_menu_item objects
368         if ( ! empty( $menu_item_db_id ) && ! is_nav_menu_item( $menu_item_db_id ) )
369                 return new WP_Error( 'update_nav_menu_item_failed', __( 'The given object ID is not that of a menu item.' ) );
370
371         $menu = wp_get_nav_menu_object( $menu_id );
372
373         if ( ! $menu && 0 !== $menu_id ) {
374                 return new WP_Error( 'invalid_menu_id', __( 'Invalid menu ID.' ) );
375         }
376
377         if ( is_wp_error( $menu ) ) {
378                 return $menu;
379         }
380
381         $defaults = array(
382                 'menu-item-db-id' => $menu_item_db_id,
383                 'menu-item-object-id' => 0,
384                 'menu-item-object' => '',
385                 'menu-item-parent-id' => 0,
386                 'menu-item-position' => 0,
387                 'menu-item-type' => 'custom',
388                 'menu-item-title' => '',
389                 'menu-item-url' => '',
390                 'menu-item-description' => '',
391                 'menu-item-attr-title' => '',
392                 'menu-item-target' => '',
393                 'menu-item-classes' => '',
394                 'menu-item-xfn' => '',
395                 'menu-item-status' => '',
396         );
397
398         $args = wp_parse_args( $menu_item_data, $defaults );
399
400         if ( 0 == $menu_id ) {
401                 $args['menu-item-position'] = 1;
402         } elseif ( 0 == (int) $args['menu-item-position'] ) {
403                 $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
404                 $last_item = array_pop( $menu_items );
405                 $args['menu-item-position'] = ( $last_item && isset( $last_item->menu_order ) ) ? 1 + $last_item->menu_order : count( $menu_items );
406         }
407
408         $original_parent = 0 < $menu_item_db_id ? get_post_field( 'post_parent', $menu_item_db_id ) : 0;
409
410         if ( 'custom' != $args['menu-item-type'] ) {
411                 /* if non-custom menu item, then:
412                         * use original object's URL
413                         * blank default title to sync with original object's
414                 */
415
416                 $args['menu-item-url'] = '';
417
418                 $original_title = '';
419                 if ( 'taxonomy' == $args['menu-item-type'] ) {
420                         $original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
421                         $original_title = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
422                 } elseif ( 'post_type' == $args['menu-item-type'] ) {
423
424                         $original_object = get_post( $args['menu-item-object-id'] );
425                         $original_parent = (int) $original_object->post_parent;
426                         $original_title = $original_object->post_title;
427                 } elseif ( 'post_type_archive' == $args['menu-item-type'] ) {
428                         $original_object = get_post_type_object( $args['menu-item-object'] );
429                         if ( $original_object ) {
430                                 $original_title = $original_object->labels->archives;
431                         }
432                 }
433
434                 if ( $args['menu-item-title'] == $original_title )
435                         $args['menu-item-title'] = '';
436
437                 // hack to get wp to create a post object when too many properties are empty
438                 if ( '' ==  $args['menu-item-title'] && '' == $args['menu-item-description'] )
439                         $args['menu-item-description'] = ' ';
440         }
441
442         // Populate the menu item object
443         $post = array(
444                 'menu_order' => $args['menu-item-position'],
445                 'ping_status' => 0,
446                 'post_content' => $args['menu-item-description'],
447                 'post_excerpt' => $args['menu-item-attr-title'],
448                 'post_parent' => $original_parent,
449                 'post_title' => $args['menu-item-title'],
450                 'post_type' => 'nav_menu_item',
451         );
452
453         $update = 0 != $menu_item_db_id;
454
455         // New menu item. Default is draft status
456         if ( ! $update ) {
457                 $post['ID'] = 0;
458                 $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
459                 $menu_item_db_id = wp_insert_post( $post );
460                 if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) )
461                         return $menu_item_db_id;
462
463                 /**
464                  * Fires immediately after a new navigation menu item has been added.
465                  *
466                  * @since 4.4.0
467                  *
468                  * @see wp_update_nav_menu_item()
469                  *
470                  * @param int   $menu_id         ID of the updated menu.
471                  * @param int   $menu_item_db_id ID of the new menu item.
472                  * @param array $args            An array of arguments used to update/add the menu item.
473                  */
474                 do_action( 'wp_add_nav_menu_item', $menu_id, $menu_item_db_id, $args );
475         }
476
477         // Associate the menu item with the menu term
478         // Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms()
479          if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) ) {
480                 wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
481         }
482
483         if ( 'custom' == $args['menu-item-type'] ) {
484                 $args['menu-item-object-id'] = $menu_item_db_id;
485                 $args['menu-item-object'] = 'custom';
486         }
487
488         $menu_item_db_id = (int) $menu_item_db_id;
489
490         update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key($args['menu-item-type']) );
491         update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', strval( (int) $args['menu-item-parent-id'] ) );
492         update_post_meta( $menu_item_db_id, '_menu_item_object_id', strval( (int) $args['menu-item-object-id'] ) );
493         update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key($args['menu-item-object']) );
494         update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key($args['menu-item-target']) );
495
496         $args['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-classes'] ) );
497         $args['menu-item-xfn'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) );
498         update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] );
499         update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] );
500         update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw($args['menu-item-url']) );
501
502         if ( 0 == $menu_id )
503                 update_post_meta( $menu_item_db_id, '_menu_item_orphaned', (string) time() );
504         elseif ( get_post_meta( $menu_item_db_id, '_menu_item_orphaned' ) )
505                 delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' );
506
507         // Update existing menu item. Default is publish status
508         if ( $update ) {
509                 $post['ID'] = $menu_item_db_id;
510                 $post['post_status'] = 'draft' == $args['menu-item-status'] ? 'draft' : 'publish';
511                 wp_update_post( $post );
512         }
513
514         /**
515          * Fires after a navigation menu item has been updated.
516          *
517          * @since 3.0.0
518          *
519          * @see wp_update_nav_menu_item()
520          *
521          * @param int   $menu_id         ID of the updated menu.
522          * @param int   $menu_item_db_id ID of the updated menu item.
523          * @param array $args            An array of arguments used to update a menu item.
524          */
525         do_action( 'wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args );
526
527         return $menu_item_db_id;
528 }
529
530 /**
531  * Returns all navigation menu objects.
532  *
533  * @since 3.0.0
534  * @since 4.1.0 Default value of the 'orderby' argument was changed from 'none'
535  *              to 'name'.
536  *
537  * @param array $args Optional. Array of arguments passed on to {@see get_terms()}.
538  *                    Default empty array.
539  * @return array Menu objects.
540  */
541 function wp_get_nav_menus( $args = array() ) {
542         $defaults = array( 'hide_empty' => false, 'orderby' => 'name' );
543         $args = wp_parse_args( $args, $defaults );
544
545         /**
546          * Filter the navigation menu objects being returned.
547          *
548          * @since 3.0.0
549          *
550          * @see get_terms()
551          *
552          * @param array $menus An array of menu objects.
553          * @param array $args  An array of arguments used to retrieve menu objects.
554          */
555         return apply_filters( 'wp_get_nav_menus', get_terms( 'nav_menu',  $args), $args );
556 }
557
558 /**
559  * Sort menu items by the desired key.
560  *
561  * @since 3.0.0
562  * @access private
563  *
564  * @global string $_menu_item_sort_prop
565  *
566  * @param object $a The first object to compare
567  * @param object $b The second object to compare
568  * @return int -1, 0, or 1 if $a is considered to be respectively less than, equal to, or greater than $b.
569  */
570 function _sort_nav_menu_items( $a, $b ) {
571         global $_menu_item_sort_prop;
572
573         if ( empty( $_menu_item_sort_prop ) )
574                 return 0;
575
576         if ( ! isset( $a->$_menu_item_sort_prop ) || ! isset( $b->$_menu_item_sort_prop ) )
577                 return 0;
578
579         $_a = (int) $a->$_menu_item_sort_prop;
580         $_b = (int) $b->$_menu_item_sort_prop;
581
582         if ( $a->$_menu_item_sort_prop == $b->$_menu_item_sort_prop )
583                 return 0;
584         elseif ( $_a == $a->$_menu_item_sort_prop && $_b == $b->$_menu_item_sort_prop )
585                 return $_a < $_b ? -1 : 1;
586         else
587                 return strcmp( $a->$_menu_item_sort_prop, $b->$_menu_item_sort_prop );
588 }
589
590 /**
591  * Return if a menu item is valid.
592  *
593  * @link https://core.trac.wordpress.org/ticket/13958
594  *
595  * @since 3.2.0
596  * @access private
597  *
598  * @param object $item The menu item to check.
599  * @return bool False if invalid, otherwise true.
600  */
601 function _is_valid_nav_menu_item( $item ) {
602         return empty( $item->_invalid );
603 }
604
605 /**
606  * Return all menu items of a navigation menu.
607  *
608  * @since 3.0.0
609  *
610  * @global string $_menu_item_sort_prop
611  * @staticvar array $fetched
612  *
613  * @param string $menu Menu name, ID, or slug.
614  * @param array  $args Optional. Arguments to pass to {@see get_posts()}.
615  * @return false|array $items Array of menu items, otherwise false.
616  */
617 function wp_get_nav_menu_items( $menu, $args = array() ) {
618         $menu = wp_get_nav_menu_object( $menu );
619
620         if ( ! $menu ) {
621                 return false;
622         }
623
624         static $fetched = array();
625
626         $items = get_objects_in_term( $menu->term_id, 'nav_menu' );
627         if ( is_wp_error( $items ) ) {
628                 return false;
629         }
630
631         $defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item',
632                 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true );
633         $args = wp_parse_args( $args, $defaults );
634         $args['include'] = $items;
635
636         if ( ! empty( $items ) ) {
637                 $items = get_posts( $args );
638         } else {
639                 $items = array();
640         }
641
642         // Get all posts and terms at once to prime the caches
643         if ( empty( $fetched[$menu->term_id] ) || wp_using_ext_object_cache() ) {
644                 $fetched[$menu->term_id] = true;
645                 $posts = array();
646                 $terms = array();
647                 foreach ( $items as $item ) {
648                         $object_id = get_post_meta( $item->ID, '_menu_item_object_id', true );
649                         $object    = get_post_meta( $item->ID, '_menu_item_object',    true );
650                         $type      = get_post_meta( $item->ID, '_menu_item_type',      true );
651
652                         if ( 'post_type' == $type )
653                                 $posts[$object][] = $object_id;
654                         elseif ( 'taxonomy' == $type)
655                                 $terms[$object][] = $object_id;
656                 }
657
658                 if ( ! empty( $posts ) ) {
659                         foreach ( array_keys($posts) as $post_type ) {
660                                 get_posts( array('post__in' => $posts[$post_type], 'post_type' => $post_type, 'nopaging' => true, 'update_post_term_cache' => false) );
661                         }
662                 }
663                 unset($posts);
664
665                 if ( ! empty( $terms ) ) {
666                         foreach ( array_keys($terms) as $taxonomy ) {
667                                 get_terms( $taxonomy, array(
668                                         'include' => $terms[ $taxonomy ],
669                                         'hierarchical' => false,
670                                 ) );
671                         }
672                 }
673                 unset($terms);
674         }
675
676         $items = array_map( 'wp_setup_nav_menu_item', $items );
677
678         if ( ! is_admin() ) { // Remove invalid items only in front end
679                 $items = array_filter( $items, '_is_valid_nav_menu_item' );
680         }
681
682         if ( ARRAY_A == $args['output'] ) {
683                 $GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
684                 usort($items, '_sort_nav_menu_items');
685                 $i = 1;
686                 foreach ( $items as $k => $item ) {
687                         $items[$k]->{$args['output_key']} = $i++;
688                 }
689         }
690
691         /**
692          * Filter the navigation menu items being returned.
693          *
694          * @since 3.0.0
695          *
696          * @param array  $items An array of menu item post objects.
697          * @param object $menu  The menu object.
698          * @param array  $args  An array of arguments used to retrieve menu item objects.
699          */
700         return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args );
701 }
702
703 /**
704  * Decorates a menu item object with the shared navigation menu item properties.
705  *
706  * Properties:
707  * - ID:               The term_id if the menu item represents a taxonomy term.
708  * - attr_title:       The title attribute of the link element for this menu item.
709  * - classes:          The array of class attribute values for the link element of this menu item.
710  * - db_id:            The DB ID of this item as a nav_menu_item object, if it exists (0 if it doesn't exist).
711  * - description:      The description of this menu item.
712  * - menu_item_parent: The DB ID of the nav_menu_item that is this item's menu parent, if any. 0 otherwise.
713  * - object:           The type of object originally represented, such as "category," "post", or "attachment."
714  * - object_id:        The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories.
715  * - post_parent:      The DB ID of the original object's parent object, if any (0 otherwise).
716  * - post_title:       A "no title" label if menu item represents a post that lacks a title.
717  * - target:           The target attribute of the link element for this menu item.
718  * - title:            The title of this menu item.
719  * - type:             The family of objects originally represented, such as "post_type" or "taxonomy."
720  * - type_label:       The singular label used to describe this type of menu item.
721  * - url:              The URL to which this menu item points.
722  * - xfn:              The XFN relationship expressed in the link of this menu item.
723  * - _invalid:         Whether the menu item represents an object that no longer exists.
724  *
725  * @since 3.0.0
726  *
727  * @param object $menu_item The menu item to modify.
728  * @return object $menu_item The menu item with standard menu item properties.
729  */
730 function wp_setup_nav_menu_item( $menu_item ) {
731         if ( isset( $menu_item->post_type ) ) {
732                 if ( 'nav_menu_item' == $menu_item->post_type ) {
733                         $menu_item->db_id = (int) $menu_item->ID;
734                         $menu_item->menu_item_parent = ! isset( $menu_item->menu_item_parent ) ? get_post_meta( $menu_item->ID, '_menu_item_menu_item_parent', true ) : $menu_item->menu_item_parent;
735                         $menu_item->object_id = ! isset( $menu_item->object_id ) ? get_post_meta( $menu_item->ID, '_menu_item_object_id', true ) : $menu_item->object_id;
736                         $menu_item->object = ! isset( $menu_item->object ) ? get_post_meta( $menu_item->ID, '_menu_item_object', true ) : $menu_item->object;
737                         $menu_item->type = ! isset( $menu_item->type ) ? get_post_meta( $menu_item->ID, '_menu_item_type', true ) : $menu_item->type;
738
739                         if ( 'post_type' == $menu_item->type ) {
740                                 $object = get_post_type_object( $menu_item->object );
741                                 if ( $object ) {
742                                         $menu_item->type_label = $object->labels->singular_name;
743                                 } else {
744                                         $menu_item->type_label = $menu_item->object;
745                                         $menu_item->_invalid = true;
746                                 }
747
748                                 $menu_item->url = get_permalink( $menu_item->object_id );
749
750                                 $original_object = get_post( $menu_item->object_id );
751                                 /** This filter is documented in wp-includes/post-template.php */
752                                 $original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );
753
754                                 if ( '' === $original_title ) {
755                                         /* translators: %d: ID of a post */
756                                         $original_title = sprintf( __( '#%d (no title)' ), $original_object->ID );
757                                 }
758
759                                 $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
760
761                         } elseif ( 'post_type_archive' == $menu_item->type ) {
762                                 $object =  get_post_type_object( $menu_item->object );
763                                 if ( $object ) {
764                                         $menu_item->title = '' == $menu_item->post_title ? $object->labels->archives : $menu_item->post_title;
765                                         $post_type_description = $object->description;
766                                 } else {
767                                         $menu_item->_invalid = true;
768                                         $post_type_description = '';
769                                 }
770
771                                 $menu_item->type_label = __( 'Post Type Archive' );
772                                 $post_content = wp_trim_words( $menu_item->post_content, 200 );
773                                 $post_type_description = '' == $post_content ? $post_type_description : $post_content; 
774                                 $menu_item->url = get_post_type_archive_link( $menu_item->object );
775                         } elseif ( 'taxonomy' == $menu_item->type ) {
776                                 $object = get_taxonomy( $menu_item->object );
777                                 if ( $object ) {
778                                         $menu_item->type_label = $object->labels->singular_name;
779                                 } else {
780                                         $menu_item->type_label = $menu_item->object;
781                                         $menu_item->_invalid = true;
782                                 }
783
784                                 $term_url = get_term_link( (int) $menu_item->object_id, $menu_item->object );
785                                 $menu_item->url = !is_wp_error( $term_url ) ? $term_url : '';
786
787                                 $original_title = get_term_field( 'name', $menu_item->object_id, $menu_item->object, 'raw' );
788                                 if ( is_wp_error( $original_title ) )
789                                         $original_title = false;
790                                 $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
791
792                         } else {
793                                 $menu_item->type_label = __('Custom Link');
794                                 $menu_item->title = $menu_item->post_title;
795                                 $menu_item->url = ! isset( $menu_item->url ) ? get_post_meta( $menu_item->ID, '_menu_item_url', true ) : $menu_item->url;
796                         }
797
798                         $menu_item->target = ! isset( $menu_item->target ) ? get_post_meta( $menu_item->ID, '_menu_item_target', true ) : $menu_item->target;
799
800                         /**
801                          * Filter a navigation menu item's title attribute.
802                          *
803                          * @since 3.0.0
804                          *
805                          * @param string $item_title The menu item title attribute.
806                          */
807                         $menu_item->attr_title = ! isset( $menu_item->attr_title ) ? apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt ) : $menu_item->attr_title;
808
809                         if ( ! isset( $menu_item->description ) ) {
810                                 /**
811                                  * Filter a navigation menu item's description.
812                                  *
813                                  * @since 3.0.0
814                                  *
815                                  * @param string $description The menu item description.
816                                  */
817                                 $menu_item->description = apply_filters( 'nav_menu_description', wp_trim_words( $menu_item->post_content, 200 ) );
818                         }
819
820                         $menu_item->classes = ! isset( $menu_item->classes ) ? (array) get_post_meta( $menu_item->ID, '_menu_item_classes', true ) : $menu_item->classes;
821                         $menu_item->xfn = ! isset( $menu_item->xfn ) ? get_post_meta( $menu_item->ID, '_menu_item_xfn', true ) : $menu_item->xfn;
822                 } else {
823                         $menu_item->db_id = 0;
824                         $menu_item->menu_item_parent = 0;
825                         $menu_item->object_id = (int) $menu_item->ID;
826                         $menu_item->type = 'post_type';
827
828                         $object = get_post_type_object( $menu_item->post_type );
829                         $menu_item->object = $object->name;
830                         $menu_item->type_label = $object->labels->singular_name;
831
832                         if ( '' === $menu_item->post_title ) {
833                                 /* translators: %d: ID of a post */
834                                 $menu_item->post_title = sprintf( __( '#%d (no title)' ), $menu_item->ID );
835                         }
836
837                         $menu_item->title = $menu_item->post_title;
838                         $menu_item->url = get_permalink( $menu_item->ID );
839                         $menu_item->target = '';
840
841                         /** This filter is documented in wp-includes/nav-menu.php */
842                         $menu_item->attr_title = apply_filters( 'nav_menu_attr_title', '' );
843
844                         /** This filter is documented in wp-includes/nav-menu.php */
845                         $menu_item->description = apply_filters( 'nav_menu_description', '' );
846                         $menu_item->classes = array();
847                         $menu_item->xfn = '';
848                 }
849         } elseif ( isset( $menu_item->taxonomy ) ) {
850                 $menu_item->ID = $menu_item->term_id;
851                 $menu_item->db_id = 0;
852                 $menu_item->menu_item_parent = 0;
853                 $menu_item->object_id = (int) $menu_item->term_id;
854                 $menu_item->post_parent = (int) $menu_item->parent;
855                 $menu_item->type = 'taxonomy';
856
857                 $object = get_taxonomy( $menu_item->taxonomy );
858                 $menu_item->object = $object->name;
859                 $menu_item->type_label = $object->labels->singular_name;
860
861                 $menu_item->title = $menu_item->name;
862                 $menu_item->url = get_term_link( $menu_item, $menu_item->taxonomy );
863                 $menu_item->target = '';
864                 $menu_item->attr_title = '';
865                 $menu_item->description = get_term_field( 'description', $menu_item->term_id, $menu_item->taxonomy );
866                 $menu_item->classes = array();
867                 $menu_item->xfn = '';
868
869         }
870
871         /**
872          * Filter a navigation menu item object.
873          *
874          * @since 3.0.0
875          *
876          * @param object $menu_item The menu item object.
877          */
878         return apply_filters( 'wp_setup_nav_menu_item', $menu_item );
879 }
880
881 /**
882  * Get the menu items associated with a particular object.
883  *
884  * @since 3.0.0
885  *
886  * @param int    $object_id   The ID of the original object.
887  * @param string $object_type The type of object, such as "taxonomy" or "post_type."
888  * @param string $taxonomy    If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
889  * @return array The array of menu item IDs; empty array if none;
890  */
891 function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
892         $object_id = (int) $object_id;
893         $menu_item_ids = array();
894
895         $query = new WP_Query;
896         $menu_items = $query->query(
897                 array(
898                         'meta_key' => '_menu_item_object_id',
899                         'meta_value' => $object_id,
900                         'post_status' => 'any',
901                         'post_type' => 'nav_menu_item',
902                         'posts_per_page' => -1,
903                 )
904         );
905         foreach ( (array) $menu_items as $menu_item ) {
906                 if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
907                         $menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
908                         if (
909                                 'post_type' == $object_type &&
910                                 'post_type' == $menu_item_type
911                         ) {
912                                 $menu_item_ids[] = (int) $menu_item->ID;
913                         } elseif (
914                                 'taxonomy' == $object_type &&
915                                 'taxonomy' == $menu_item_type &&
916                                 get_post_meta( $menu_item->ID, '_menu_item_object', true ) == $taxonomy
917                         ) {
918                                 $menu_item_ids[] = (int) $menu_item->ID;
919                         }
920                 }
921         }
922
923         return array_unique( $menu_item_ids );
924 }
925
926 /**
927  * Callback for handling a menu item when its original object is deleted.
928  *
929  * @since 3.0.0
930  * @access private
931  *
932  * @param int $object_id The ID of the original object being trashed.
933  *
934  */
935 function _wp_delete_post_menu_item( $object_id = 0 ) {
936         $object_id = (int) $object_id;
937
938         $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'post_type' );
939
940         foreach ( (array) $menu_item_ids as $menu_item_id ) {
941                 wp_delete_post( $menu_item_id, true );
942         }
943 }
944
945 /**
946  * Serves as a callback for handling a menu item when its original object is deleted.
947  *
948  * @since 3.0.0
949  * @access private
950  *
951  * @param int    $object_id Optional. The ID of the original object being trashed. Default 0.
952  * @param int    $tt_id     Term taxonomy ID. Unused.
953  * @param string $taxonomy  Taxonomy slug.
954  */
955 function _wp_delete_tax_menu_item( $object_id = 0, $tt_id, $taxonomy ) {
956         $object_id = (int) $object_id;
957
958         $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy', $taxonomy );
959
960         foreach ( (array) $menu_item_ids as $menu_item_id ) {
961                 wp_delete_post( $menu_item_id, true );
962         }
963 }
964
965 /**
966  * Automatically add newly published page objects to menus with that as an option.
967  *
968  * @since 3.0.0
969  * @access private
970  *
971  * @param string $new_status The new status of the post object.
972  * @param string $old_status The old status of the post object.
973  * @param object $post       The post object being transitioned from one status to another.
974  */
975 function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
976         if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type )
977                 return;
978         if ( ! empty( $post->post_parent ) )
979                 return;
980         $auto_add = get_option( 'nav_menu_options' );
981         if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) )
982                 return;
983         $auto_add = $auto_add['auto_add'];
984         if ( empty( $auto_add ) || ! is_array( $auto_add ) )
985                 return;
986
987         $args = array(
988                 'menu-item-object-id' => $post->ID,
989                 'menu-item-object' => $post->post_type,
990                 'menu-item-type' => 'post_type',
991                 'menu-item-status' => 'publish',
992         );
993
994         foreach ( $auto_add as $menu_id ) {
995                 $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
996                 if ( ! is_array( $items ) )
997                         continue;
998                 foreach ( $items as $item ) {
999                         if ( $post->ID == $item->object_id )
1000                                 continue 2;
1001                 }
1002                 wp_update_nav_menu_item( $menu_id, 0, $args );
1003         }
1004 }