Make WordPress Core

Ticket #12812: nav-menu-parent-classes.12812.diff

File nav-menu-parent-classes.12812.diff, 5.4 KB (added by filosofo, 15 years ago)
  • wp-includes/nav-menu-template.php

     
    7070
    7171                $classes = $value = '';
    7272
    73                 $classes = array( 'menu-item', 'menu-item-type-'. $item->type, $item->classes );
     73                $classes = array( 'menu-item', 'menu-item-type-'. $item->type, $item->classes, 'menu-item-object-'. $item->object );
    7474
    75                 if ( 'custom' == $item->object ) {
    76                         $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    77                         $item_url = strpos( $item->url, '#' ) ? substr( $item->url, 0, strpos( $item->url, '#' ) ) : $item->url;
    78                         if ( $item_url == $current_url )
    79                                 $classes[] = 'current-menu-item';
    80                 } else {
    81                         $classes[] = 'menu-item-object-'. $item->object;
    82                         if (
    83                                 $item->object_id == $wp_query->get_queried_object_id() &&
    84                                 ( ( 'post_type' == $item->type && $wp_query->is_singular ) ||
    85                                 ( 'taxonomy' == $item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) ) )
    86                         )
    87                                 $classes[] = 'current-menu-item';
    88                 }
    89 
    90                 // @todo add classes for parent/child relationships
    91 
    9275                $classes = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
    9376                $classes = ' class="' . esc_attr( $classes ) . '"';
    9477
     
    245228        }
    246229
    247230        // Set up the $menu_item variables
     231        if ( 'frontend' == $args->context )
     232                _wp_menu_item_classes_by_context( $menu_items );
     233
    248234        $sorted_menu_items = array();
    249235        foreach ( (array) $menu_items as $key => $menu_item )
    250236                $sorted_menu_items[$menu_item->menu_order] = $menu_item;
     237       
    251238        unset($menu_items);
    252239
    253240        $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
     
    279266}
    280267
    281268/**
     269 * Add the class property classes for the current frontend context, if applicable.
     270 *
     271 * @access private
     272 * @since 3.0
     273 *
     274 * @param array $menu_items The current menu item objects to which to add the class property information.
     275 */
     276function _wp_menu_item_classes_by_context( &$menu_items = array() ) {
     277        global $wp_query;
     278
     279        $queried_object = $wp_query->get_queried_object();
     280        $queried_object_id = (int) $wp_query->queried_object_id;
     281
     282        $active_object = '';
     283        $active_parent_item_ids = array();
     284        $active_parent_object_ids = array();
     285        $possible_object_parents = array();
     286       
     287        if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
     288                foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {
     289                        if ( is_taxonomy_hierarchical( $taxonomy ) ) {
     290                                $terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
     291                                if ( is_array( $terms ) )
     292                                        $possible_object_parents = array_merge( $possible_object_parents, $terms );
     293                        }
     294                }
     295        }
     296
     297        $possible_object_parents = array_filter( $possible_object_parents );
     298
     299        foreach( (array) $menu_items as $key => $menu_item ) {
     300                // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object
     301                if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) {
     302                        $active_parent_object_ids[] = (int) $menu_item->object_id;
     303                        $active_parent_item_ids[] = (int) $menu_item->db_id;
     304                        $active_object = $queried_object->post_type;
     305               
     306                // if the menu item corresponds to the currently-queried post or taxonomy object
     307                } elseif (
     308                        $menu_item->object_id == $queried_object_id &&
     309                        (
     310                                ( 'post_type' == $menu_item->type && $wp_query->is_singular ) ||
     311                                ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) )
     312                        )
     313                ) {
     314                        $menu_items[$key]->classes = trim( $menu_item->classes . ' ' . 'current-menu-item' );
     315                        $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
     316                        $active_parent_object_ids[] = (int) $menu_item->post_parent;
     317                        $active_object = $menu_item->object;
     318
     319                // if the menu item corresponds to the currently-requested URL
     320                } elseif ( 'custom' == $menu_item->object ) {
     321                        $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
     322                        $item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
     323                        if ( $item_url == $current_url ) {
     324                                $menu_items[$key]->classes = trim( $menu_item->classes . ' ' . 'current-menu-item' );
     325                                $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
     326                                $active_parent_object_ids[] = (int) $menu_item->post_parent;
     327                                $active_object = $menu_item->object;
     328                        }
     329                }
     330        }
     331
     332        $active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) );
     333        $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) );
     334
     335        // set parent's class
     336        if ( ! empty( $active_parent_item_ids ) || ! empty( $active_parent_object_ids ) ) {
     337                foreach( (array) $menu_items as $key => $parent_item ) {
     338                        if ( in_array( $parent_item->db_id, $active_parent_item_ids ) )
     339                                $menu_items[$key]->classes = trim( $parent_item->classes . ' ' . 'current-menu-parent' );
     340                        if ( in_array( $parent_item->object_id, $active_parent_object_ids ) )
     341                                $menu_items[$key]->classes = trim( $parent_item->classes . ' ' . 'current-' . $active_object . '-parent' );
     342                }
     343        }
     344}
     345
     346/**
    282347 * Retrieve the HTML list content for nav menu items.
    283348 *
    284349 * @uses Walker_Nav_Menu to create HTML list content.