]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/nav-menu-template.php
WordPress 3.9.1
[autoinstalls/wordpress.git] / wp-includes / nav-menu-template.php
1 <?php
2 /**
3  * Navigation Menu template functions
4  *
5  * @package WordPress
6  * @subpackage Nav_Menus
7  * @since 3.0.0
8  */
9
10 /**
11  * Create HTML list of nav menu items.
12  *
13  * @since 3.0.0
14  * @uses Walker
15  */
16 class Walker_Nav_Menu extends Walker {
17         /**
18          * What the class handles.
19          *
20          * @see Walker::$tree_type
21          * @since 3.0.0
22          * @var string
23          */
24         var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
25
26         /**
27          * Database fields to use.
28          *
29          * @see Walker::$db_fields
30          * @since 3.0.0
31          * @todo Decouple this.
32          * @var array
33          */
34         var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
35
36         /**
37          * Starts the list before the elements are added.
38          *
39          * @see Walker::start_lvl()
40          *
41          * @since 3.0.0
42          *
43          * @param string $output Passed by reference. Used to append additional content.
44          * @param int    $depth  Depth of menu item. Used for padding.
45          * @param array  $args   An array of arguments. @see wp_nav_menu()
46          */
47         function start_lvl( &$output, $depth = 0, $args = array() ) {
48                 $indent = str_repeat("\t", $depth);
49                 $output .= "\n$indent<ul class=\"sub-menu\">\n";
50         }
51
52         /**
53          * Ends the list of after the elements are added.
54          *
55          * @see Walker::end_lvl()
56          *
57          * @since 3.0.0
58          *
59          * @param string $output Passed by reference. Used to append additional content.
60          * @param int    $depth  Depth of menu item. Used for padding.
61          * @param array  $args   An array of arguments. @see wp_nav_menu()
62          */
63         function end_lvl( &$output, $depth = 0, $args = array() ) {
64                 $indent = str_repeat("\t", $depth);
65                 $output .= "$indent</ul>\n";
66         }
67
68         /**
69          * Start the element output.
70          *
71          * @see Walker::start_el()
72          *
73          * @since 3.0.0
74          *
75          * @param string $output Passed by reference. Used to append additional content.
76          * @param object $item   Menu item data object.
77          * @param int    $depth  Depth of menu item. Used for padding.
78          * @param array  $args   An array of arguments. @see wp_nav_menu()
79          * @param int    $id     Current item ID.
80          */
81         function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
82                 $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
83
84                 $class_names = '';
85
86                 $classes = empty( $item->classes ) ? array() : (array) $item->classes;
87                 $classes[] = 'menu-item-' . $item->ID;
88
89                 /**
90                  * Filter the CSS class(es) applied to a menu item's <li>.
91                  *
92                  * @since 3.0.0
93                  *
94                  * @see wp_nav_menu()
95                  *
96                  * @param array  $classes The CSS classes that are applied to the menu item's <li>.
97                  * @param object $item    The current menu item.
98                  * @param array  $args    An array of wp_nav_menu() arguments.
99                  */
100                 $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
101                 $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
102
103                 /**
104                  * Filter the ID applied to a menu item's <li>.
105                  *
106                  * @since 3.0.1
107                  *
108                  * @see wp_nav_menu()
109                  *
110                  * @param string $menu_id The ID that is applied to the menu item's <li>.
111                  * @param object $item    The current menu item.
112                  * @param array  $args    An array of wp_nav_menu() arguments.
113                  */
114                 $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
115                 $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
116
117                 $output .= $indent . '<li' . $id . $class_names .'>';
118
119                 $atts = array();
120                 $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
121                 $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
122                 $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
123                 $atts['href']   = ! empty( $item->url )        ? $item->url        : '';
124
125                 /**
126                  * Filter the HTML attributes applied to a menu item's <a>.
127                  *
128                  * @since 3.6.0
129                  *
130                  * @see wp_nav_menu()
131                  *
132                  * @param array $atts {
133                  *     The HTML attributes applied to the menu item's <a>, empty strings are ignored.
134                  *
135                  *     @type string $title  Title attribute.
136                  *     @type string $target Target attribute.
137                  *     @type string $rel    The rel attribute.
138                  *     @type string $href   The href attribute.
139                  * }
140                  * @param object $item The current menu item.
141                  * @param array  $args An array of wp_nav_menu() arguments.
142                  */
143                 $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
144
145                 $attributes = '';
146                 foreach ( $atts as $attr => $value ) {
147                         if ( ! empty( $value ) ) {
148                                 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
149                                 $attributes .= ' ' . $attr . '="' . $value . '"';
150                         }
151                 }
152
153                 $item_output = $args->before;
154                 $item_output .= '<a'. $attributes .'>';
155                 /** This filter is documented in wp-includes/post-template.php */
156                 $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
157                 $item_output .= '</a>';
158                 $item_output .= $args->after;
159
160                 /**
161                  * Filter a menu item's starting output.
162                  *
163                  * The menu item's starting output only includes $args->before, the opening <a>,
164                  * the menu item's title, the closing </a>, and $args->after. Currently, there is
165                  * no filter for modifying the opening and closing <li> for a menu item.
166                  *
167                  * @since 3.0.0
168                  *
169                  * @see wp_nav_menu()
170                  *
171                  * @param string $item_output The menu item's starting HTML output.
172                  * @param object $item        Menu item data object.
173                  * @param int    $depth       Depth of menu item. Used for padding.
174                  * @param array  $args        An array of wp_nav_menu() arguments.
175                  */
176                 $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
177         }
178
179         /**
180          * Ends the element output, if needed.
181          *
182          * @see Walker::end_el()
183          *
184          * @since 3.0.0
185          *
186          * @param string $output Passed by reference. Used to append additional content.
187          * @param object $item   Page data object. Not used.
188          * @param int    $depth  Depth of page. Not Used.
189          * @param array  $args   An array of arguments. @see wp_nav_menu()
190          */
191         function end_el( &$output, $item, $depth = 0, $args = array() ) {
192                 $output .= "</li>\n";
193         }
194
195 } // Walker_Nav_Menu
196
197 /**
198  * Displays a navigation menu.
199  *
200  * @since 3.0.0
201  *
202  * @param array $args {
203  *     Optional. Array of nav menu arguments.
204  *
205  *     @type string        $menu            Desired menu. Accepts (matching in order) id, slug, name. Default empty.
206  *     @type string        $menu_class      CSS class to use for the ul element which forms the menu. Default 'menu'.
207  *     @type string        $menu_id         The ID that is applied to the ul element which forms the menu.
208  *                                          Default is the menu slug, incremented.
209  *     @type string        $container       Whether to wrap the ul, and what to wrap it with. Default 'div'.
210  *     @type string        $container_class Class that is applied to the container. Default 'menu-{menu slug}-container'.
211  *     @type string        $container_id    The ID that is applied to the container. Default empty.
212  *     @type callback|bool $fallback_cb     If the menu doesn't exists, a callback function will fire.
213  *                                          Default is 'wp_page_menu'. Set to false for no fallback.
214  *     @type string        $before          Text before the link text. Default empty.
215  *     @type string        $after           Text after the link text. Default empty.
216  *     @type string        $link_before     Text before the link. Default empty.
217  *     @type string        $link_after      Text after the link. Default empty.
218  *     @type bool          $echo            Whether to echo the menu or return it. Default true.
219  *     @type int           $depth           How many levels of the hierarchy are to be included. 0 means all. Default 0.
220  *     @type string        $walker          Allows a custom walker class to be specified. Default empty.
221  *     @type string        $theme_location  Theme location to be used. Must be registered with register_nav_menu()
222  *                                          in order to be selectable by the user.
223  *     @type string        $items_wrap      How the list items should be wrapped. Default is a ul with an id and class.
224  *                                          Uses printf() format with numbered placeholders.
225  * }
226  * @return mixed Menu output if $echo is false, false if there are no items or no menu was found.
227  */
228 function wp_nav_menu( $args = array() ) {
229         static $menu_id_slugs = array();
230
231         $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '',
232         'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
233         'depth' => 0, 'walker' => '', 'theme_location' => '' );
234
235         $args = wp_parse_args( $args, $defaults );
236         /**
237          * Filter the arguments used to display a navigation menu.
238          *
239          * @since 3.0.0
240          *
241          * @see wp_nav_menu()
242          *
243          * @param array $args Array of wp_nav_menu() arguments.
244          */
245         $args = apply_filters( 'wp_nav_menu_args', $args );
246         $args = (object) $args;
247
248         /**
249          * Filter whether to short-circuit the wp_nav_menu() output.
250          *
251          * Returning a non-null value to the filter will short-circuit
252          * wp_nav_menu(), echoing that value if $args->echo is true,
253          * returning that value otherwise.
254          *
255          * @since 3.9.0
256          *
257          * @see wp_nav_menu()
258          *
259          * @param string|null $output Nav menu output to short-circuit with. Default null.
260          * @param object      $args   An object containing wp_nav_menu() arguments.
261          */
262         $nav_menu = apply_filters( 'pre_wp_nav_menu', null, $args );
263
264         if ( null !== $nav_menu ) {
265                 if ( $args->echo ) {
266                         echo $nav_menu;
267                         return;
268                 }
269
270                 return $nav_menu;
271         }
272
273         // Get the nav menu based on the requested menu
274         $menu = wp_get_nav_menu_object( $args->menu );
275
276         // Get the nav menu based on the theme_location
277         if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) )
278                 $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
279
280         // get the first menu that has items if we still can't find a menu
281         if ( ! $menu && !$args->theme_location ) {
282                 $menus = wp_get_nav_menus();
283                 foreach ( $menus as $menu_maybe ) {
284                         if ( $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) ) ) {
285                                 $menu = $menu_maybe;
286                                 break;
287                         }
288                 }
289         }
290
291         // If the menu exists, get its items.
292         if ( $menu && ! is_wp_error($menu) && !isset($menu_items) )
293                 $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
294
295         /*
296          * If no menu was found:
297          *  - Fall back (if one was specified), or bail.
298          *
299          * If no menu items were found:
300          *  - Fall back, but only if no theme location was specified.
301          *  - Otherwise, bail.
302          */
303         if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) && !$args->theme_location ) )
304                 && $args->fallback_cb && is_callable( $args->fallback_cb ) )
305                         return call_user_func( $args->fallback_cb, (array) $args );
306
307         if ( ! $menu || is_wp_error( $menu ) )
308                 return false;
309
310         $nav_menu = $items = '';
311
312         $show_container = false;
313         if ( $args->container ) {
314                 /**
315                  * Filter the list of HTML tags that are valid for use as menu containers.
316                  *
317                  * @since 3.0.0
318                  *
319                  * @param array $tags The acceptable HTML tags for use as menu containers.
320                  *                    Default is array containing 'div' and 'nav'.
321                  */
322                 $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
323                 if ( in_array( $args->container, $allowed_tags ) ) {
324                         $show_container = true;
325                         $class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"';
326                         $id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
327                         $nav_menu .= '<'. $args->container . $id . $class . '>';
328                 }
329         }
330
331         // Set up the $menu_item variables
332         _wp_menu_item_classes_by_context( $menu_items );
333
334         $sorted_menu_items = $menu_items_with_children = array();
335         foreach ( (array) $menu_items as $menu_item ) {
336                 $sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
337                 if ( $menu_item->menu_item_parent )
338                         $menu_items_with_children[ $menu_item->menu_item_parent ] = true;
339         }
340
341         // Add the menu-item-has-children class where applicable
342         if ( $menu_items_with_children ) {
343                 foreach ( $sorted_menu_items as &$menu_item ) {
344                         if ( isset( $menu_items_with_children[ $menu_item->ID ] ) )
345                                 $menu_item->classes[] = 'menu-item-has-children';
346                 }
347         }
348
349         unset( $menu_items, $menu_item );
350
351         /**
352          * Filter the sorted list of menu item objects before generating the menu's HTML.
353          *
354          * @since 3.1.0
355          *
356          * @param array $sorted_menu_items The menu items, sorted by each menu item's menu order.
357          */
358         $sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );
359
360         $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
361         unset($sorted_menu_items);
362
363         // Attributes
364         if ( ! empty( $args->menu_id ) ) {
365                 $wrap_id = $args->menu_id;
366         } else {
367                 $wrap_id = 'menu-' . $menu->slug;
368                 while ( in_array( $wrap_id, $menu_id_slugs ) ) {
369                         if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) )
370                                 $wrap_id = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $wrap_id );
371                         else
372                                 $wrap_id = $wrap_id . '-1';
373                 }
374         }
375         $menu_id_slugs[] = $wrap_id;
376
377         $wrap_class = $args->menu_class ? $args->menu_class : '';
378
379         /**
380          * Filter the HTML list content for navigation menus.
381          *
382          * @since 3.0.0
383          *
384          * @see wp_nav_menu()
385          *
386          * @param string $items The HTML list content for the menu items.
387          * @param array  $args  Array of wp_nav_menu() arguments.
388          */
389         $items = apply_filters( 'wp_nav_menu_items', $items, $args );
390         /**
391          * Filter the HTML list content for a specific navigation menu.
392          *
393          * @since 3.0.0
394          *
395          * @see wp_nav_menu()
396          *
397          * @param string $items The HTML list content for the menu items.
398          * @param array  $args  Array of wp_nav_menu() arguments.
399          */
400         $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );
401
402         // Don't print any markup if there are no items at this point.
403         if ( empty( $items ) )
404                 return false;
405
406         $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );
407         unset( $items );
408
409         if ( $show_container )
410                 $nav_menu .= '</' . $args->container . '>';
411
412         /**
413          * Filter the HTML content for navigation menus.
414          *
415          * @since 3.0.0
416          *
417          * @see wp_nav_menu()
418          *
419          * @param string $nav_menu The HTML content for the navigation menu.
420          * @param array  $args     Array of wp_nav_menu() arguments.
421          */
422         $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
423
424         if ( $args->echo )
425                 echo $nav_menu;
426         else
427                 return $nav_menu;
428 }
429
430 /**
431  * Add the class property classes for the current context, if applicable.
432  *
433  * @access private
434  * @since 3.0.0
435  *
436  * @param array $menu_items The current menu item objects to which to add the class property information.
437  */
438 function _wp_menu_item_classes_by_context( &$menu_items ) {
439         global $wp_query, $wp_rewrite;
440
441         $queried_object = $wp_query->get_queried_object();
442         $queried_object_id = (int) $wp_query->queried_object_id;
443
444         $active_object = '';
445         $active_ancestor_item_ids = array();
446         $active_parent_item_ids = array();
447         $active_parent_object_ids = array();
448         $possible_taxonomy_ancestors = array();
449         $possible_object_parents = array();
450         $home_page_id = (int) get_option( 'page_for_posts' );
451
452         if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
453                 foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {
454                         if ( is_taxonomy_hierarchical( $taxonomy ) ) {
455                                 $term_hierarchy = _get_term_hierarchy( $taxonomy );
456                                 $terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
457                                 if ( is_array( $terms ) ) {
458                                         $possible_object_parents = array_merge( $possible_object_parents, $terms );
459                                         $term_to_ancestor = array();
460                                         foreach ( (array) $term_hierarchy as $anc => $descs ) {
461                                                 foreach ( (array) $descs as $desc )
462                                                         $term_to_ancestor[ $desc ] = $anc;
463                                         }
464
465                                         foreach ( $terms as $desc ) {
466                                                 do {
467                                                         $possible_taxonomy_ancestors[ $taxonomy ][] = $desc;
468                                                         if ( isset( $term_to_ancestor[ $desc ] ) ) {
469                                                                 $_desc = $term_to_ancestor[ $desc ];
470                                                                 unset( $term_to_ancestor[ $desc ] );
471                                                                 $desc = $_desc;
472                                                         } else {
473                                                                 $desc = 0;
474                                                         }
475                                                 } while ( ! empty( $desc ) );
476                                         }
477                                 }
478                         }
479                 }
480         } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
481                 $term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy );
482                 $term_to_ancestor = array();
483                 foreach ( (array) $term_hierarchy as $anc => $descs ) {
484                         foreach ( (array) $descs as $desc )
485                                 $term_to_ancestor[ $desc ] = $anc;
486                 }
487                 $desc = $queried_object->term_id;
488                 do {
489                         $possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc;
490                         if ( isset( $term_to_ancestor[ $desc ] ) ) {
491                                 $_desc = $term_to_ancestor[ $desc ];
492                                 unset( $term_to_ancestor[ $desc ] );
493                                 $desc = $_desc;
494                         } else {
495                                 $desc = 0;
496                         }
497                 } while ( ! empty( $desc ) );
498         }
499
500         $possible_object_parents = array_filter( $possible_object_parents );
501
502         $front_page_url = home_url();
503
504         foreach ( (array) $menu_items as $key => $menu_item ) {
505
506                 $menu_items[$key]->current = false;
507
508                 $classes = (array) $menu_item->classes;
509                 $classes[] = 'menu-item';
510                 $classes[] = 'menu-item-type-' . $menu_item->type;
511                 $classes[] = 'menu-item-object-' . $menu_item->object;
512
513                 // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object
514                 if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) {
515                         $active_parent_object_ids[] = (int) $menu_item->object_id;
516                         $active_parent_item_ids[] = (int) $menu_item->db_id;
517                         $active_object = $queried_object->post_type;
518
519                 // if the menu item corresponds to the currently-queried post or taxonomy object
520                 } elseif (
521                         $menu_item->object_id == $queried_object_id &&
522                         (
523                                 ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) ||
524                                 ( 'post_type' == $menu_item->type && $wp_query->is_singular ) ||
525                                 ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) && $queried_object->taxonomy == $menu_item->object )
526                         )
527                 ) {
528                         $classes[] = 'current-menu-item';
529                         $menu_items[$key]->current = true;
530                         $_anc_id = (int) $menu_item->db_id;
531
532                         while(
533                                 ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
534                                 ! in_array( $_anc_id, $active_ancestor_item_ids )
535                         ) {
536                                 $active_ancestor_item_ids[] = $_anc_id;
537                         }
538
539                         if ( 'post_type' == $menu_item->type && 'page' == $menu_item->object ) {
540                                 // Back compat classes for pages to match wp_page_menu()
541                                 $classes[] = 'page_item';
542                                 $classes[] = 'page-item-' . $menu_item->object_id;
543                                 $classes[] = 'current_page_item';
544                         }
545                         $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
546                         $active_parent_object_ids[] = (int) $menu_item->post_parent;
547                         $active_object = $menu_item->object;
548
549                 // if the menu item corresponds to the currently-requested URL
550                 } elseif ( 'custom' == $menu_item->object ) {
551                         $_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] );
552                         $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current );
553                         $raw_item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
554                         $item_url = untrailingslashit( $raw_item_url );
555                         $_indexless_current = untrailingslashit( preg_replace( '/' . preg_quote( $wp_rewrite->index, '/' ) . '$/', '', $current_url ) );
556
557                         if ( $raw_item_url && in_array( $item_url, array( $current_url, $_indexless_current, $_root_relative_current ) ) ) {
558                                 $classes[] = 'current-menu-item';
559                                 $menu_items[$key]->current = true;
560                                 $_anc_id = (int) $menu_item->db_id;
561
562                                 while(
563                                         ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
564                                         ! in_array( $_anc_id, $active_ancestor_item_ids )
565                                 ) {
566                                         $active_ancestor_item_ids[] = $_anc_id;
567                                 }
568
569                                 if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ) ) ) {
570                                         // Back compat for home link to match wp_page_menu()
571                                         $classes[] = 'current_page_item';
572                                 }
573                                 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
574                                 $active_parent_object_ids[] = (int) $menu_item->post_parent;
575                                 $active_object = $menu_item->object;
576
577                         // give front page item current-menu-item class when extra query arguments involved
578                         } elseif ( $item_url == $front_page_url && is_front_page() ) {
579                                 $classes[] = 'current-menu-item';
580                         }
581
582                         if ( untrailingslashit($item_url) == home_url() )
583                                 $classes[] = 'menu-item-home';
584                 }
585
586                 // back-compat with wp_page_menu: add "current_page_parent" to static home page link for any non-page query
587                 if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id )
588                         $classes[] = 'current_page_parent';
589
590                 $menu_items[$key]->classes = array_unique( $classes );
591         }
592         $active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) );
593         $active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) );
594         $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) );
595
596         // set parent's class
597         foreach ( (array) $menu_items as $key => $parent_item ) {
598                 $classes = (array) $parent_item->classes;
599                 $menu_items[$key]->current_item_ancestor = false;
600                 $menu_items[$key]->current_item_parent = false;
601
602                 if (
603                         isset( $parent_item->type ) &&
604                         (
605                                 // ancestral post object
606                                 (
607                                         'post_type' == $parent_item->type &&
608                                         ! empty( $queried_object->post_type ) &&
609                                         is_post_type_hierarchical( $queried_object->post_type ) &&
610                                         in_array( $parent_item->object_id, $queried_object->ancestors ) &&
611                                         $parent_item->object != $queried_object->ID
612                                 ) ||
613
614                                 // ancestral term
615                                 (
616                                         'taxonomy' == $parent_item->type &&
617                                         isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
618                                         in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
619                                         (
620                                                 ! isset( $queried_object->term_id ) ||
621                                                 $parent_item->object_id != $queried_object->term_id
622                                         )
623                                 )
624                         )
625                 ) {
626                         $classes[] = empty( $queried_object->taxonomy ) ? 'current-' . $queried_object->post_type . '-ancestor' : 'current-' . $queried_object->taxonomy . '-ancestor';
627                 }
628
629                 if ( in_array(  intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) {
630                         $classes[] = 'current-menu-ancestor';
631                         $menu_items[$key]->current_item_ancestor = true;
632                 }
633                 if ( in_array( $parent_item->db_id, $active_parent_item_ids ) ) {
634                         $classes[] = 'current-menu-parent';
635                         $menu_items[$key]->current_item_parent = true;
636                 }
637                 if ( in_array( $parent_item->object_id, $active_parent_object_ids ) )
638                         $classes[] = 'current-' . $active_object . '-parent';
639
640                 if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) {
641                         // Back compat classes for pages to match wp_page_menu()
642                         if ( in_array('current-menu-parent', $classes) )
643                                 $classes[] = 'current_page_parent';
644                         if ( in_array('current-menu-ancestor', $classes) )
645                                 $classes[] = 'current_page_ancestor';
646                 }
647
648                 $menu_items[$key]->classes = array_unique( $classes );
649         }
650 }
651
652 /**
653  * Retrieve the HTML list content for nav menu items.
654  *
655  * @uses Walker_Nav_Menu to create HTML list content.
656  * @since 3.0.0
657  * @see Walker::walk() for parameters and return description.
658  */
659 function walk_nav_menu_tree( $items, $depth, $r ) {
660         $walker = ( empty($r->walker) ) ? new Walker_Nav_Menu : $r->walker;
661         $args = array( $items, $depth, $r );
662
663         return call_user_func_array( array($walker, 'walk'), $args );
664 }
665
666 /**
667  * Prevents a menu item ID from being used more than once.
668  *
669  * @since 3.0.1
670  * @access private
671  */
672 function _nav_menu_item_id_use_once( $id, $item ) {
673         static $_used_ids = array();
674         if ( in_array( $item->ID, $_used_ids ) )
675                 return '';
676         $_used_ids[] = $item->ID;
677         return $id;
678 }
679 add_filter( 'nav_menu_item_id', '_nav_menu_item_id_use_once', 10, 2 );