WordPress.org

Make WordPress Core

Changeset 46868


Ignore:
Timestamp:
12/09/2019 09:01:13 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.
Fixes #48599.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/plugin.php

    r46696 r46868  
    13911391        $submenu[ $parent_slug ][] = $new_sub_menu;
    13921392    } else {
    1393         // If position is equal or higher than the number of items in the array, append the submenu.
    1394         if ( $position >= count( $submenu[ $parent_slug ] ) ) {
     1393        // Append the submenu if the parent item is not present in the submenu,
     1394        // or if position is equal or higher than the number of items in the array.
     1395        if ( ! isset( $submenu[ $parent_slug ] ) || $position >= count( $submenu[ $parent_slug ] ) ) {
    13951396            $submenu[ $parent_slug ][] = $new_sub_menu;
    13961397        } else {
  • trunk/tests/phpunit/tests/admin/includesPlugin.php

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