Make WordPress Core

Changeset 38940


Ignore:
Timestamp:
10/26/2016 01:57:57 AM (8 years ago)
Author:
pento
Message:

Menus: Add the menu-item-home class to the static front page item.

When a site is using a static front page, and that page is in a menu, it isn't given the CSS class menu-item-home, contrary to the developer documentation.

An incorrect solution was originally added in [35272], and is now gone. Let us never speak of it again.

Props mdgl, adamsilverstein, welcher, pento.
Fixes #35272.

Location:
trunk
Files:
2 edited

Legend:

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

    r38882 r38940  
    335335
    336336    $front_page_url = home_url();
     337    $front_page_id  = (int) get_option( 'page_on_front' );
    337338
    338339    foreach ( (array) $menu_items as $key => $menu_item ) {
     
    344345        $classes[] = 'menu-item-type-' . $menu_item->type;
    345346        $classes[] = 'menu-item-object-' . $menu_item->object;
     347
     348        // This menu item is set as the 'Front Page'.
     349        if ( 'post_type' === $menu_item->type && $front_page_id === (int) $menu_item->object_id ) {
     350            $classes[] = 'menu-item-home';
     351        }
    346352
    347353        // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object
     
    376382                $classes[] = 'page-item-' . $menu_item->object_id;
    377383                $classes[] = 'current_page_item';
    378             }
    379 
    380             if ( 'page_on_front' ) {
    381                 $classes[] = 'menu-item-home';
    382384            }
    383385
  • trunk/tests/phpunit/tests/post/nav-menu.php

    r38744 r38940  
    445445        return $ignored_1;
    446446    }
     447
     448
     449    /**
     450     * @ticket 35272
     451     */
     452    function test_no_front_page_class_applied() {
     453        $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'Home Page' ) );
     454
     455        wp_update_nav_menu_item( $this->menu_id, 0, array(
     456            'menu-item-type' => 'post_type',
     457            'menu-item-object' => 'page',
     458            'menu-item-object-id' => $page_id,
     459            'menu-item-status' => 'publish',
     460        ));
     461
     462        $menu_items = wp_get_nav_menu_items( $this->menu_id );
     463        _wp_menu_item_classes_by_context( $menu_items );
     464
     465        $classes = $menu_items[0]->classes;
     466
     467        $this->assertNotContains( 'menu-item-home', $classes );
     468    }
     469
     470
     471    /**
     472     * @ticket 35272
     473     */
     474    function test_class_applied_to_front_page_item() {
     475        $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'Home Page' ) );
     476        update_option( 'page_on_front', $page_id );
     477
     478        wp_update_nav_menu_item( $this->menu_id, 0, array(
     479            'menu-item-type' => 'post_type',
     480            'menu-item-object' => 'page',
     481            'menu-item-object-id' => $page_id,
     482            'menu-item-status' => 'publish',
     483        ));
     484
     485        $menu_items = wp_get_nav_menu_items( $this->menu_id );
     486        _wp_menu_item_classes_by_context( $menu_items );
     487
     488        $classes = $menu_items[0]->classes;
     489
     490        delete_option( 'page_on_front' );
     491
     492        $this->assertContains( 'menu-item-home', $classes );
     493    }
     494
     495    /**
     496     * @ticket 35272
     497     */
     498    function test_class_not_applied_to_taxonomies_with_same_id_as_front_page_item() {
     499        global $wpdb;
     500
     501        $new_id = 35272;
     502
     503        $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'Home Page' ) );
     504        $tag_id = self::factory()->tag->create();
     505
     506        $wpdb->query( "UPDATE $wpdb->posts SET ID=$new_id WHERE ID=$page_id" );
     507        $wpdb->query( "UPDATE $wpdb->terms SET term_id=$new_id WHERE term_id=$page_id" );
     508        $wpdb->query( "UPDATE $wpdb->term_taxonomy SET term_id=$new_id WHERE term_id=$page_id" );
     509
     510        update_option( 'page_on_front', $new_id );
     511
     512        wp_update_nav_menu_item( $this->menu_id, 0, array(
     513            'menu-item-type' => 'taxonomy',
     514            'menu-item-object' => 'post_tag',
     515            'menu-item-object-id' => $new_id,
     516            'menu-item-status' => 'publish',
     517        ) );
     518
     519        $menu_items = wp_get_nav_menu_items( $this->menu_id );
     520        _wp_menu_item_classes_by_context( $menu_items );
     521
     522        $classes = $menu_items[0]->classes;
     523
     524        $this->assertNotContains( 'menu-item-home', $classes );
     525    }
    447526}
Note: See TracChangeset for help on using the changeset viewer.