Make WordPress Core


Ignore:
Timestamp:
04/08/2022 06:15:02 AM (3 years ago)
Author:
peterwilsoncc
Message:

Administration: Allow floats for menu positions.

Permit plugin authors to pass the menu position as a float in add_menu_page() and add_submenu_page(). This allows for a common practice within major plugins to avoid menu collisions by passing a float.

Follow up to [52569].

Props justinbusa, dd32, welcher, SergeyBiryukov, kirtan95, audrasjb, Cybr, chaion07, costdev, peterwilsoncc.
See #40927.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/includesPlugin.php

    r52570 r53104  
    257257            array( -1, 0 ),                    // Negative numbers are treated the same as passing 0.
    258258            array( -7, 0 ),                    // Negative numbers are treated the same as passing 0.
     259            array( '-7', 0 ),                  // Negative numbers are treated the same as passing 0.
    259260            array( 1, 1 ),                     // Insert as the second item.
     261            array( '1', 1 ),                   // Insert as the second item.
     262            array( 1.5, 1 ),                   // Insert as the second item.
     263            array( '1.5', 1 ),                 // Insert as the second item.
    260264            array( 3, 3 ),                     // Insert as the 4th item.
     265            array( '3', 3 ),                   // Insert as the 4th item.
     266            array( '3e0', 3 ),                 // Insert as the 4th item.
     267            array( 3.5, 3 ),                   // Insert as the 4th item.
     268            array( '3.5', 3 ),                 // Insert as the 4th item.
    261269            array( $menu_count, $menu_count ), // Numbers equal to the number of items are added at the end.
    262270            array( 123456, $menu_count ),      // Numbers higher than the number of items are added at the end.
     
    315323        // Setup a menu with some items.
    316324        add_menu_page( 'Main Menu', 'Main Menu', 'manage_options', 'main_slug', 'main_page_callback' );
    317         add_submenu_page( 'main_slug', 'SubMenu 1', 'SubMenu 1', 'manage_options', 'submenu_page_1', 'submenu_callback_1', '2' );
     325        add_submenu_page( 'main_slug', 'SubMenu 1', 'SubMenu 1', 'manage_options', 'submenu_page_1', 'submenu_callback_1', 'First' );
    318326
    319327        // Clean up the temporary user.
     
    330338     * @ticket 54798
    331339     */
    332     public function test_passing_string_as_position_fires_doing_it_wrong_menu() {
    333         $this->setExpectedIncorrectUsage( 'add_menu_page' );
     340    public function test_passing_float_as_position_does_not_override_int() {
    334341        global $submenu, $menu;
    335342
     
    343350
    344351        // Setup a menu with some items.
    345         add_menu_page( 'Main Menu', 'Main Menu', 'manage_options', 'main_slug', 'main_page_callback', 'icon_url', '1' );
    346         add_menu_page( 'Main Menu 1', 'Main Menu 1', 'manage_options', 'main1_slug', 'main1_page_callback', 'icon_url1', 1.5 );
     352        add_menu_page( 'Main Menu 1', 'Main Menu 1', 'manage_options', 'main_slug_1', 'main_page_callback_1', 'icon_url_1', 1 );
     353        add_menu_page( 'Main Menu 2', 'Main Menu 2', 'manage_options', 'main_slug_2', 'main_page_callback_2', 'icon_url_2', 2 );
     354        add_menu_page( 'Main Menu 1.5', 'Main Menu 1.5', 'manage_options', 'main_slug_15', 'main_page_callback_15', 'icon_url_15', 1.5 );
    347355
    348356        // Clean up the temporary user.
     
    350358        wp_delete_user( $admin_user );
    351359
    352         // Verify the menu was inserted.
    353         $this->assertSame( 'main_slug', $menu[1][2] );
     360        // Verify the menus were inserted.
     361        $this->assertSame( 'main_slug_1', $menu[1][2] );
     362        $this->assertSame( 'main_slug_2', $menu[2][2] );
    354363        // Verify the menu was inserted correctly on passing float as position.
    355         $this->assertSame( 'main1_slug', $menu['1.5'][2] );
     364        $this->assertSame( 'main_slug_15', $menu['1.5'][2] );
    356365    }
    357366
Note: See TracChangeset for help on using the changeset viewer.