Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/nav-menu-template.php

    r15408 r17228  
    7474                $classes[] = 'menu-item-' . $item->ID;
    7575
    76                 $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
     76                $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
    7777                $class_names = ' class="' . esc_attr( $class_names ) . '"';
    7878
    … …  
    120120 * container_class - the class that is applied to the container. Defaults to 'menu-{menu slug}-container'.
    121121 * container_id - The ID that is applied to the container. Defaults to blank.
    122  * fallback_cb - If the menu doesn't exists, a callback function will fire. Defaults to 'wp_page_menu'.
     122 * fallback_cb - If the menu doesn't exists, a callback function will fire. Defaults to 'wp_page_menu'. Set to false for no fallback.
    123123 * before - Text before the link text.
    124124 * after - Text after the link text.
    … …  
    129129 * walker - allows a custom walker to be specified.
    130130 * theme_location - the location in the theme to be used.  Must be registered with register_nav_menu() in order to be selectable by the user.
     131 * items_wrap - How the list items should be wrapped. Defaults to a ul with an id and class. Uses printf() format with numbered placeholders.
    131132 *
    132133 * @since 3.0.0
    … …  
    138139
    139140        $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '',
    140         'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '',
     141        '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>',
    141142        'depth' => 0, 'walker' => '', 'theme_location' => '' );
    142143
    … …  
    169170        // If no menu was found or if the menu has no items and no location was requested, call the fallback_cb if it exists
    170171        if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) && !$args->theme_location ) )
    171                 && ( function_exists($args->fallback_cb) || is_callable( $args->fallback_cb ) ) )
     172                && $args->fallback_cb && is_callable( $args->fallback_cb ) )
    172173                        return call_user_func( $args->fallback_cb, (array) $args );
    173174
    … …  
    198199        unset($menu_items);
    199200
     201        $sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );
     202
    200203        $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
    201204        unset($sorted_menu_items);
    … …  
    203206        // Attributes
    204207        if ( ! empty( $args->menu_id ) ) {
    205                 $slug = $args->menu_id;
     208                $wrap_id = $args->menu_id;
    206209        } else {
    207                 $slug = 'menu-' . $menu->slug;
    208                 while ( in_array( $slug, $menu_id_slugs ) ) {
    209                         if ( preg_match( '#-(\d+)$#', $slug, $matches ) )
    210                                 $slug = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $slug);
     210                $wrap_id = 'menu-' . $menu->slug;
     211                while ( in_array( $wrap_id, $menu_id_slugs ) ) {
     212                        if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) )
     213                                $wrap_id = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $wrap_id );
    211214                        else
    212                                 $slug = $slug . '-1';
    213                 }
    214         }
    215         $menu_id_slugs[] = $slug;
    216         $attributes = ' id="' . $slug . '"';
    217         $attributes .= $args->menu_class ? ' class="'. $args->menu_class .'"' : '';
    218 
    219         $nav_menu .= '<ul'. $attributes .'>';
     215                                $wrap_id = $wrap_id . '-1';
     216                }
     217        }
     218        $menu_id_slugs[] = $wrap_id;
     219
     220        $wrap_class = $args->menu_class ? $args->menu_class : '';
    220221
    221222        // Allow plugins to hook into the menu to add their own <li>'s
    222223        $items = apply_filters( 'wp_nav_menu_items', $items, $args );
    223224        $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );
    224         $nav_menu .= $items;
    225         unset($items);
    226 
    227         $nav_menu .= '</ul>';
     225
     226        $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );
     227        unset( $items );
    228228
    229229        if ( $show_container )
    … …  
    312312        $possible_object_parents = array_filter( $possible_object_parents );
    313313
     314        $front_page_url = home_url();
     315
    314316        foreach ( (array) $menu_items as $key => $menu_item ) {
     317
     318                $menu_items[$key]->current = false;
     319
    315320                $classes = (array) $menu_item->classes;
    316321                $classes[] = 'menu-item';
    317322                $classes[] = 'menu-item-type-' . $menu_item->type;
     323                $classes[] = 'menu-item-object-' . $menu_item->object;
    318324
    319325                // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object
    … …  
    333339                ) {
    334340                        $classes[] = 'current-menu-item';
     341                        $menu_items[$key]->current = true;
    335342                        $_anc_id = (int) $menu_item->db_id;
    336343
    … …  
    354361                // if the menu item corresponds to the currently-requested URL
    355362                } elseif ( 'custom' == $menu_item->object ) {
    356                         $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    357                         $item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
    358                         $_indexless_current = preg_replace( '/index.php$/', '', $current_url );
    359                        
     363                        $current_url = untrailingslashit( ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
     364                        $item_url = untrailingslashit( strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url );
     365                        $_indexless_current = untrailingslashit( preg_replace( '/index.php$/', '', $current_url ) );
     366
    360367                        if ( in_array( $item_url, array( $current_url, $_indexless_current ) ) ) {
    361368                                $classes[] = 'current-menu-item';
     369                                $menu_items[$key]->current = true;
    362370                                $_anc_id = (int) $menu_item->db_id;
    363371
    … …  
    370378
    371379                                if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ) ) ) {
    372                                         // Back compat for home limk to match wp_page_menu()
     380                                        // Back compat for home link to match wp_page_menu()
    373381                                        $classes[] = 'current_page_item';
    374382                                }
    … …  
    376384                                $active_parent_object_ids[] = (int) $menu_item->post_parent;
    377385                                $active_object = $menu_item->object;
    378                         }
    379                        
     386
     387                        // give front page item current-menu-item class when extra query arguments involved
     388                        } elseif ( $item_url == $front_page_url && is_front_page() ) {
     389                                $classes[] = 'current-menu-item';
     390                        }
     391
    380392                        if ( untrailingslashit($item_url) == home_url() )
    381393                                $classes[] = 'menu-item-home';
    … …  
    395407        foreach ( (array) $menu_items as $key => $parent_item ) {
    396408                $classes = (array) $parent_item->classes;
     409                $menu_items[$key]->current_item_ancestor = false;
     410                $menu_items[$key]->current_item_parent = false;
    397411
    398412                if (
    … …  
    404418                                        ! empty( $queried_object->post_type ) &&
    405419                                        is_post_type_hierarchical( $queried_object->post_type ) &&
    406                                         in_array( $parent_item->object_id, $queried_object->ancestors )
     420                                        in_array( $parent_item->object_id, $queried_object->ancestors ) &&
     421                                        $parent_item->object != $queried_object->ID
    407422                                ) ||
    408423
    … …  
    411426                                        'taxonomy' == $parent_item->type &&
    412427                                        isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
    413                                         in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] )
     428                                        in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
     429                                        (
     430                                                ! isset( $queried_object->term_id ) ||
     431                                                $parent_item->object_id != $queried_object->term_id
     432                                        )
    414433                                )
    415434                        )
    … …  
    420439                if ( in_array(  intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) {
    421440                        $classes[] = 'current-menu-ancestor';
    422                 }
    423                 if ( in_array( $parent_item->db_id, $active_parent_item_ids ) )
     441                        $menu_items[$key]->current_item_ancestor = true;
     442                }
     443                if ( in_array( $parent_item->db_id, $active_parent_item_ids ) ) {
    424444                        $classes[] = 'current-menu-parent';
     445                        $menu_items[$key]->current_item_parent = true;
     446                }
    425447                if ( in_array( $parent_item->object_id, $active_parent_object_ids ) )
    426448                        $classes[] = 'current-' . $active_object . '-parent';
    … …  
    466488}
    467489add_filter( 'nav_menu_item_id', '_nav_menu_item_id_use_once', 10, 2 );
    468 
    469 ?>
Note: See TracChangeset for help on using the changeset viewer.