WordPress.org

Make WordPress Core

Changeset 46869


Ignore:
Timestamp:
12/09/2019 09:04:07 PM (8 months ago)
Author:
SergeyBiryukov
Message:

Menus: Avoid a PHP warning in add_submenu_page() when the same value is passed for both $parent_slug and $menu_slug parameters.

Props welcher, ispreview, ayeshrajans, NextScripts, adamsilverstein, garrett-eclipse, 123host.
Merges [46868] to the 5.3 branch.
Fixes #48599.

Location:
branches/5.3
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.3

  • branches/5.3/src/wp-admin/includes/plugin.php

    r46570 r46869  
    13891389        $submenu[ $parent_slug ][] = $new_sub_menu;
    13901390    } else {
    1391         // If position is equal or higher than the number of items in the array, append the submenu.
    1392         if ( $position >= count( $submenu[ $parent_slug ] ) ) {
     1391        // Append the submenu if the parent item is not present in the submenu,
     1392        // or if position is equal or higher than the number of items in the array.
     1393        if ( ! isset( $submenu[ $parent_slug ] ) || $position >= count( $submenu[ $parent_slug ] ) ) {
    13931394            $submenu[ $parent_slug ][] = $new_sub_menu;
    13941395        } else {
  • branches/5.3/tests/phpunit/tests/admin/includesPlugin.php

    r46197 r46869  
    260260    }
    261261
     262    /**
     263     * Test that when a submenu has the same slug as a parent item, that it's just appended and ignores the priority.
     264     *
     265     * @ticket 48599
     266     */
     267    function test_priority_when_parent_slug_child_slug_are_the_same() {
     268        global $submenu, $menu;
     269
     270        // Reset menus.
     271        $submenu      = array();
     272        $menu         = array();
     273        $current_user = get_current_user_id();
     274        $admin_user   = self::factory()->user->create( array( 'role' => 'administrator' ) );
     275        wp_set_current_user( $admin_user );
     276        set_current_screen( 'dashboard' );
     277
     278        // Setup a menu with some items.
     279        add_menu_page( 'Main Menu', 'Main Menu', 'manage_options', 'main_slug', 'main_page_callback' );
     280        add_submenu_page( 'main_slug', 'SubMenu 1', 'SubMenu 1', 'manage_options', 'main_slug', 'submenu_callback_1', 1 );
     281        add_submenu_page( 'main_slug', 'SubMenu 2', 'SubMenu 2', 'manage_options', 'submenu_page2', 'submenu_callback_2', 2 );
     282        add_submenu_page( 'main_slug', 'SubMenu 3', 'SubMenu 3', 'manage_options', 'submenu_page3', 'submenu_callback_3', 3 );
     283
     284        // Clean up the temporary user.
     285        wp_set_current_user( $current_user );
     286        wp_delete_user( $admin_user );
     287
     288        // Verify the menu was inserted at the expected position.
     289        $this->assertSame( 'main_slug', $submenu['main_slug'][0][2] );
     290        $this->assertSame( 'submenu_page2', $submenu['main_slug'][1][2] );
     291        $this->assertSame( 'submenu_page3', $submenu['main_slug'][2][2] );
     292    }
     293
     294    /**
     295     * Passing a string as priority will fail.
     296     *
     297     * @ticket 48599
     298     */
     299    function test_passing_string_as_priority_fires_doing_it_wrong() {
     300        $this->setExpectedIncorrectUsage( 'add_submenu_page' );
     301        global $submenu, $menu;
     302
     303        // Reset menus.
     304        $submenu      = array();
     305        $menu         = array();
     306        $current_user = get_current_user_id();
     307        $admin_user   = self::factory()->user->create( array( 'role' => 'administrator' ) );
     308        wp_set_current_user( $admin_user );
     309        set_current_screen( 'dashboard' );
     310
     311        // Setup a menu with some items.
     312        add_menu_page( 'Main Menu', 'Main Menu', 'manage_options', 'main_slug', 'main_page_callback' );
     313        add_submenu_page( 'main_slug', 'SubMenu 1', 'SubMenu 1', 'manage_options', 'submenu_page_1', 'submenu_callback_1', '2' );
     314
     315        // Clean up the temporary user.
     316        wp_set_current_user( $current_user );
     317        wp_delete_user( $admin_user );
     318
     319        // Verify the menu was inserted at the expected position.
     320        $this->assertSame( 'submenu_page_1', $submenu['main_slug'][1][2] );
     321    }
     322
    262323    function test_is_plugin_active_true() {
    263324        activate_plugin( 'hello.php' );
Note: See TracChangeset for help on using the changeset viewer.