Skip to content

Commit 1775d3f

Browse files
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. git-svn-id: https://develop.svn.wordpress.org/branches/5.3@46869 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 5f75309 commit 1775d3f

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

src/wp-admin/includes/plugin.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,8 +1388,9 @@ function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability,
13881388

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 {
13951396
// Test for a negative position.

tests/phpunit/tests/admin/includesPlugin.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,67 @@ function data_submenu_priority() {
259259
);
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' );
264325
$test = is_plugin_active( 'hello.php' );

0 commit comments

Comments
 (0)