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.



git-svn-id: https://develop.svn.wordpress.org/trunk@53104 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson
2022-04-08 06:15:02 +00:00
parent c4545fde8d
commit 4f290ecd64
2 changed files with 87 additions and 83 deletions

View File

@@ -256,8 +256,16 @@ class Tests_Admin_IncludesPlugin extends WP_UnitTestCase {
array( 0, 0 ), // Insert at the beginning of the menu if 0 is passed.
array( -1, 0 ), // Negative numbers are treated the same as passing 0.
array( -7, 0 ), // Negative numbers are treated the same as passing 0.
array( '-7', 0 ), // Negative numbers are treated the same as passing 0.
array( 1, 1 ), // Insert as the second item.
array( '1', 1 ), // Insert as the second item.
array( 1.5, 1 ), // Insert as the second item.
array( '1.5', 1 ), // Insert as the second item.
array( 3, 3 ), // Insert as the 4th item.
array( '3', 3 ), // Insert as the 4th item.
array( '3e0', 3 ), // Insert as the 4th item.
array( 3.5, 3 ), // Insert as the 4th item.
array( '3.5', 3 ), // Insert as the 4th item.
array( $menu_count, $menu_count ), // Numbers equal to the number of items are added at the end.
array( 123456, $menu_count ), // Numbers higher than the number of items are added at the end.
);
@@ -314,7 +322,7 @@ class Tests_Admin_IncludesPlugin extends WP_UnitTestCase {
// Setup a menu with some items.
add_menu_page( 'Main Menu', 'Main Menu', 'manage_options', 'main_slug', 'main_page_callback' );
add_submenu_page( 'main_slug', 'SubMenu 1', 'SubMenu 1', 'manage_options', 'submenu_page_1', 'submenu_callback_1', '2' );
add_submenu_page( 'main_slug', 'SubMenu 1', 'SubMenu 1', 'manage_options', 'submenu_page_1', 'submenu_callback_1', 'First' );
// Clean up the temporary user.
wp_set_current_user( $current_user );
@@ -329,8 +337,7 @@ class Tests_Admin_IncludesPlugin extends WP_UnitTestCase {
*
* @ticket 54798
*/
public function test_passing_string_as_position_fires_doing_it_wrong_menu() {
$this->setExpectedIncorrectUsage( 'add_menu_page' );
public function test_passing_float_as_position_does_not_override_int() {
global $submenu, $menu;
// Reset menus.
@@ -342,17 +349,19 @@ class Tests_Admin_IncludesPlugin extends WP_UnitTestCase {
set_current_screen( 'dashboard' );
// Setup a menu with some items.
add_menu_page( 'Main Menu', 'Main Menu', 'manage_options', 'main_slug', 'main_page_callback', 'icon_url', '1' );
add_menu_page( 'Main Menu 1', 'Main Menu 1', 'manage_options', 'main1_slug', 'main1_page_callback', 'icon_url1', 1.5 );
add_menu_page( 'Main Menu 1', 'Main Menu 1', 'manage_options', 'main_slug_1', 'main_page_callback_1', 'icon_url_1', 1 );
add_menu_page( 'Main Menu 2', 'Main Menu 2', 'manage_options', 'main_slug_2', 'main_page_callback_2', 'icon_url_2', 2 );
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 );
// Clean up the temporary user.
wp_set_current_user( $current_user );
wp_delete_user( $admin_user );
// Verify the menu was inserted.
$this->assertSame( 'main_slug', $menu[1][2] );
// Verify the menus were inserted.
$this->assertSame( 'main_slug_1', $menu[1][2] );
$this->assertSame( 'main_slug_2', $menu[2][2] );
// Verify the menu was inserted correctly on passing float as position.
$this->assertSame( 'main1_slug', $menu['1.5'][2] );
$this->assertSame( 'main_slug_15', $menu['1.5'][2] );
}
public function test_is_plugin_active_true() {