Make WordPress Core

Ticket #38836: 38836.3.diff

File 38836.3.diff, 3.3 KB (added by donmhico, 4 years ago)

Refreshed the patch. I've re-test the patch and re-run the unit tests as well. Everything looks good.

  • src/wp-includes/nav-menu-template.php

    diff --git src/wp-includes/nav-menu-template.php src/wp-includes/nav-menu-template.php
    index 6de77f24d5..268f2e89b6 100644
    function _wp_menu_item_classes_by_context( &$menu_items ) { 
    496496                        }
    497497                }
    498498
     499                // Get an array of all public custom post types.
     500                $post_types = get_post_types( array(
     501                        'public'   => true,
     502                        '_builtin' => false
     503                ) );
     504
     505                // Check if a custom post type single item is being viewed.
     506                if ( is_singular( $post_types ) ) {
     507                        // Get the post type being viewed.
     508                        $post_type = get_post_type();
     509                       
     510                        if ( $post_type === $menu_item->object ) {
     511                                $classes[] = 'current_page_parent';
     512                        }
     513                }
     514
    499515                // back-compat with wp_page_menu: add "current_page_parent" to static home page link for any non-page query
    500516                if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id ) {
    501517                        $classes[] = 'current_page_parent';
  • tests/phpunit/tests/post/nav-menu.php

    diff --git tests/phpunit/tests/post/nav-menu.php tests/phpunit/tests/post/nav-menu.php
    index 0f12e54ece..496d8ab98d 100644
    class Test_Nav_Menus extends WP_UnitTestCase { 
    956956                );
    957957        }
    958958
     959        /**
     960         * @ticket 38836
     961         */
     962        function test_class_applied_to_public_custom_post_type_archive_on_single_cpt_post() {
     963                $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'Test Page' ) );
     964
     965                register_post_type( 'cpt', array( 'public' => true, 'has_archive' => true ) );
     966                $cpt_id = self::factory()->post->create( array( 'post_type' => 'cpt', 'post_name' => 'cpt-name' ) );
     967               
     968                wp_update_nav_menu_item( $this->menu_id, 0, array(
     969                        'menu-item-type' => 'post_type_archive',
     970                        'menu-item-object' => 'cpt',
     971                        'menu-item-status' => 'publish',
     972                ) );
     973               
     974                wp_update_nav_menu_item( $this->menu_id, 0, array(
     975                        'menu-item-type' => 'post_type',
     976                        'menu-item-object' => 'page',
     977                        'menu-item-object-id' => $page_id,
     978                        'menu-item-status' => 'publish',
     979                ) );
     980               
     981                $permalink = get_permalink( $cpt_id );
     982                $this->go_to( $permalink );
     983               
     984                $menu_items = wp_get_nav_menu_items( $this->menu_id );
     985                _wp_menu_item_classes_by_context( $menu_items );
     986               
     987                $cptarchive_classes = $menu_items[0]->classes;
     988                $testpage_classes = $menu_items[1]->classes;
     989
     990                $this->assertContains( 'current_page_parent', $cptarchive_classes );
     991                $this->assertNotContains( 'current_page_parent', $testpage_classes );
     992        }
     993       
     994        /**
     995         * @ticket 38836
     996         */
     997        function test_class_not_applied_to_custom_post_type_archive_on_single_cpt_post() {
     998                register_post_type( 'cpt', array( 'has_archive' => true ) );
     999
     1000                $cpt_id = self::factory()->post->create(
     1001                        array(
     1002                                'post_type' => 'cpt',
     1003                                'post_name' => 'cpt-name'
     1004                        )
     1005                );
     1006               
     1007                wp_update_nav_menu_item( $this->menu_id, 0, array(
     1008                        'menu-item-type'   => 'post_type_archive',
     1009                        'menu-item-object' => 'cpt',
     1010                        'menu-item-status' => 'publish',
     1011                ) );
     1012               
     1013                $permalink = get_permalink( $cpt_id );
     1014                $this->go_to( $permalink );
     1015               
     1016                $menu_items = wp_get_nav_menu_items( $this->menu_id );
     1017                _wp_menu_item_classes_by_context( $menu_items );
     1018               
     1019                $cptarchive_classes = $menu_items[0]->classes;
     1020                $this->assertNotContains( 'current_page_parent', $cptarchive_classes );
     1021        }
     1022
    9591023}