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