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