WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/26/2016 01:57:57 AM (20 months 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.