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