Administration: Ensure an integer is used for menu priority in add_menu_page().

This change adds a verification of the `$position` parameter in `add_menu_page()` to ensure an integer is used. If not, the function informs developers of the wrong parameter type via a `_doing_it_wrong` message. This brings consistency with a similar check used in `add_submenu_page()`.

This change also typecasts any floating number to string to ensure that in case a float value was passed, at least it doesn't override existing menus.

Follow-up to [46570].

Props kirtan95.
Fixes #54798. See #48249.


git-svn-id: https://develop.svn.wordpress.org/trunk@52569 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jb Audras 2022-01-12 23:23:41 +00:00
parent 0b010f16c9
commit 3685f95eb6
2 changed files with 48 additions and 2 deletions

View File

@ -1329,6 +1329,21 @@ function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $func
$position = $position + substr( base_convert( md5( $menu_slug . $menu_title ), 16, 10 ), -5 ) * 0.00001;
$menu[ "$position" ] = $new_menu;
} else {
if ( ! is_int( $position ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: %s: add_submenu_page() */
__( 'The seventh parameter passed to %s should be an integer representing menu position.' ),
'<code>add_menu_page()</code>'
),
'6.0.0'
);
// If the position is not a string (i.e. float), convert it to string.
if ( ! is_string( $position ) ) {
$position = (string) $position;
}
}
$menu[ $position ] = $new_menu;
}

View File

@ -296,11 +296,11 @@ class Tests_Admin_IncludesPlugin extends WP_UnitTestCase {
}
/**
* Passing a string as position will fail.
* Passing a string as position will fail in submenu.
*
* @ticket 48599
*/
public function test_passing_string_as_position_fires_doing_it_wrong() {
public function test_passing_string_as_position_fires_doing_it_wrong_submenu() {
$this->setExpectedIncorrectUsage( 'add_submenu_page' );
global $submenu, $menu;
@ -324,6 +324,37 @@ class Tests_Admin_IncludesPlugin extends WP_UnitTestCase {
$this->assertSame( 'submenu_page_1', $submenu['main_slug'][1][2] );
}
/**
* Passing a string as position will fail in menu.
*
* @ticket 48599
*/
public function test_passing_string_as_position_fires_doing_it_wrong_menu() {
$this->setExpectedIncorrectUsage( 'add_menu_page' );
global $submenu, $menu;
// Reset menus.
$submenu = array();
$menu = array();
$current_user = get_current_user_id();
$admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
wp_set_current_user( $admin_user );
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 );
// 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 menu was inserted correctly on passing float as position.
$this->assertSame( 'main1_slug', $menu['1.5'][2] );
}
public function test_is_plugin_active_true() {
activate_plugin( 'hello.php' );
$test = is_plugin_active( 'hello.php' );