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