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