From 78e8100b6b48bf3eb3121c1137c78e9d35843ad7 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Fri, 20 Sep 2019 14:59:04 +0000 Subject: [PATCH] Menus: add a position argument to `add_submenu_page` and the helper functions that use it. Add a position argument to the `add_submenu_page` function similar to the one already in `add_menu_page`. When adding sub menus enables setting the position in the sub menu where the item should appear. In addition, add the position argument to functions that call `add_submenu_page` under the hood: `add_management_page`, `add_options_page`, `add_theme_page`, `add_plugins_page`, `add_users_page`, `add_dashboard_page`, `add_posts_page`, `add_media_page`, `add_links_page`, `add_pages_page` and `add_comments_page`. Props welcher, birgire, alexvorn2. Fixes #39776. git-svn-id: https://develop.svn.wordpress.org/trunk@46197 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/plugin.php | 90 ++++++--- tests/phpunit/tests/admin/includesPlugin.php | 202 +++++++++++++++++++ 2 files changed, 267 insertions(+), 25 deletions(-) diff --git a/src/wp-admin/includes/plugin.php b/src/wp-admin/includes/plugin.php index 683cb7c215..3d138d176f 100644 --- a/src/wp-admin/includes/plugin.php +++ b/src/wp-admin/includes/plugin.php @@ -1267,7 +1267,7 @@ function uninstall_plugin( $plugin ) { * * Pass the name of a Dashicons helper class to use a font icon, * e.g. 'dashicons-chart-pie'. * * Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS. - * @param int $position The position in the menu order this one should appear. + * @param int $position The position in the menu order this item should appear. * @return string The resulting page's hook_suffix. */ function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null ) { @@ -1338,9 +1338,11 @@ function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $func * and only include lowercase alphanumeric, dashes, and underscores characters * to be compatible with sanitize_key(). * @param callable $function The function to be called to output the content for this page. + * @param int $position The position in the menu order this item should appear. + * * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. */ -function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { +function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv, $_registered_pages, $_parent_pages; @@ -1370,7 +1372,33 @@ function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, } } - $submenu[ $parent_slug ][] = array( $menu_title, $capability, $menu_slug, $page_title ); + $new_sub_menu = array( $menu_title, $capability, $menu_slug, $page_title ); + if ( null === $position ) { + $submenu[ $parent_slug ][] = $new_sub_menu; + } else { + // If position is equal or higher than the number of items in the array, append the submenu. + if ( $position >= count( $submenu[ $parent_slug ] ) ) { + $submenu[ $parent_slug ][] = $new_sub_menu; + } else { + // Test for a negative position. + $position = max( $position, 0 ); + if ( 0 === $position ) { + // For negative or `0` positions, prepend the submenu. + array_unshift( $submenu[ $parent_slug ], $new_sub_menu ); + } else { + // Grab all of the items before the insertion point. + $before_items = array_slice( $submenu[ $parent_slug ], 0, $position, true ); + // Grab all of the items after the insertion point. + $after_items = array_slice( $submenu[ $parent_slug ], $position, null, true ); + // Add the new item. + $before_items[] = $new_sub_menu; + // Merge the items. + $submenu[ $parent_slug ] = array_merge( $before_items, $after_items ); + } + } + } + // Sort the parent array + ksort( $submenu[ $parent_slug ] ); $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug ); if ( ! empty( $function ) && ! empty( $hookname ) ) { @@ -1409,10 +1437,11 @@ function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, * @param string $capability The capability required for this menu to be displayed to the user. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). * @param callable $function The function to be called to output the content for this page. + * @param int $position The position in the menu order this item should appear. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. */ -function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { - return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function ); +function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { + return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); } /** @@ -1431,10 +1460,11 @@ function add_management_page( $page_title, $menu_title, $capability, $menu_slug, * @param string $capability The capability required for this menu to be displayed to the user. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). * @param callable $function The function to be called to output the content for this page. + * @param int $position The position in the menu order this item should appear. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. */ -function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { - return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function ); +function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { + return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); } /** @@ -1453,10 +1483,11 @@ function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $f * @param string $capability The capability required for this menu to be displayed to the user. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). * @param callable $function The function to be called to output the content for this page. + * @param int $position The position in the menu order this item should appear. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. */ -function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { - return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function ); +function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { + return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); } /** @@ -1475,10 +1506,11 @@ function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $fun * @param string $capability The capability required for this menu to be displayed to the user. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). * @param callable $function The function to be called to output the content for this page. + * @param int $position The position in the menu order this item should appear. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. */ -function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { - return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function ); +function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { + return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); } /** @@ -1497,15 +1529,17 @@ function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $f * @param string $capability The capability required for this menu to be displayed to the user. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). * @param callable $function The function to be called to output the content for this page. + * @param int $position The position in the menu order this item should appear. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. */ -function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { + +function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { if ( current_user_can( 'edit_users' ) ) { $parent = 'users.php'; } else { $parent = 'profile.php'; } - return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function ); + return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function, $position ); } /** * Add submenu page to the Dashboard main menu. @@ -1523,10 +1557,11 @@ function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $fun * @param string $capability The capability required for this menu to be displayed to the user. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). * @param callable $function The function to be called to output the content for this page. + * @param int $position The position in the menu order this item should appear. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. */ -function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { - return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function ); +function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { + return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); } /** @@ -1545,10 +1580,11 @@ function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, * @param string $capability The capability required for this menu to be displayed to the user. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). * @param callable $function The function to be called to output the content for this page. + * @param int $position The position in the menu order this item should appear. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. */ -function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { - return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function ); +function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { + return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); } /** @@ -1567,10 +1603,11 @@ function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $fun * @param string $capability The capability required for this menu to be displayed to the user. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). * @param callable $function The function to be called to output the content for this page. + * @param int $position The position in the menu order this item should appear. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. */ -function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { - return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function ); +function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { + return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); } /** @@ -1589,10 +1626,11 @@ function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $fun * @param string $capability The capability required for this menu to be displayed to the user. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). * @param callable $function The function to be called to output the content for this page. + * @param int $position The position in the menu order this item should appear. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. */ -function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { - return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function ); +function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { + return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); } /** @@ -1611,10 +1649,11 @@ function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $fun * @param string $capability The capability required for this menu to be displayed to the user. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). * @param callable $function The function to be called to output the content for this page. + * @param int $position The position in the menu order this item should appear. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. */ -function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { - return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function ); +function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { + return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); } /** @@ -1633,10 +1672,11 @@ function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $fun * @param string $capability The capability required for this menu to be displayed to the user. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). * @param callable $function The function to be called to output the content for this page. + * @param int $position The position in the menu order this item should appear. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. */ -function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { - return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function ); +function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { + return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); } /** diff --git a/tests/phpunit/tests/admin/includesPlugin.php b/tests/phpunit/tests/admin/includesPlugin.php index 916ca755b7..233c6ea19f 100644 --- a/tests/phpunit/tests/admin/includesPlugin.php +++ b/tests/phpunit/tests/admin/includesPlugin.php @@ -57,6 +57,208 @@ class Tests_Admin_includesPlugin extends WP_UnitTestCase { wp_set_current_user( $current_user ); } + /** + * Tests the priority parameter. + * + * @ticket 39776 + * + * @covers ::add_submenu_page + * + * @param int $priority The position of the new item. + * @param int $expected_position Where the new item is expected to appear. + * + * @dataProvider data_submenu_priority + */ + function test_submenu_priority( $priority, $expected_position ) { + global $submenu; + global $menu; + $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. + $parent = add_menu_page( 'Test Toplevel', 'Test Toplevel', 'manage_options', 'mt-top-level-handle', 'mt_toplevel_page' ); + foreach ( $this->submenus_to_add() as $menu_to_add ) { + add_submenu_page( $parent, $menu_to_add[0], $menu_to_add[1], $menu_to_add[2], $menu_to_add[3], $menu_to_add[4] ); + } + + // Insert the new page. + add_submenu_page( $parent, 'New Page', 'New Page', 'manage_options', 'custom-position', 'custom_pos', $priority ); + wp_set_current_user( $current_user ); + + // Clean up the temporary user. + wp_delete_user( $admin_user ); + + // Verify the menu was inserted at the expected position. + $this->assertSame( 'custom-position', $submenu[ $parent ][ $expected_position ][2] ); + } + + /** + * Tests the priority parameter for menu helper functions. + * + * @ticket 39776 + * + * @covers ::add_management_page + * @covers ::add_options_page + * @covers ::add_theme_page + * @covers ::add_plugins_page + * @covers ::add_users_page + * @covers ::add_dashboard_page + * @covers ::add_posts_page + * @covers ::add_media_page + * @covers ::add_links_page + * @covers ::add_pages_page + * @covers ::add_comments_page + * + * @param int $priority The position of the new item. + * @param int $expected_position Where the new item is expected to appear. + * + * @dataProvider data_submenu_priority + */ + function test_submenu_helpers_priority( $priority, $expected_position ) { + global $submenu; + global $menu; + + // Skip for multisite. + if ( is_multisite() ) { + return; + } + + // 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' ); + + // Test the helper functions that use `add_submenu_page`. Each helper adds to a specific menu root. + $helper_functions = array( + array( + 'callback' => 'add_management_page', + 'menu_root' => 'tools.php', + ), + array( + 'callback' => 'add_options_page', + 'menu_root' => 'options-general.php', + ), + array( + 'callback' => 'add_theme_page', + 'menu_root' => 'themes.php', + ), + array( + 'callback' => 'add_plugins_page', + 'menu_root' => 'plugins.php', + ), + array( + 'callback' => 'add_users_page', + 'menu_root' => 'users.php', + ), + array( + 'callback' => 'add_dashboard_page', + 'menu_root' => 'index.php', + ), + array( + 'callback' => 'add_posts_page', + 'menu_root' => 'edit.php', + ), + array( + 'callback' => 'add_media_page', + 'menu_root' => 'upload.php', + ), + array( + 'callback' => 'add_links_page', + 'menu_root' => 'link-manager.php', + ), + array( + 'callback' => 'add_pages_page', + 'menu_root' => 'edit.php?post_type=page', + ), + array( + 'callback' => 'add_comments_page', + 'menu_root' => 'edit-comments.php', + ), + ); + + $actual_positions = array(); + + foreach ( $helper_functions as $helper_function ) { + + // Build up demo pages on the menu root. + foreach ( $this->submenus_to_add() as $menu_to_add ) { + add_menu_page( $menu_to_add[0], $menu_to_add[1], $menu_to_add[2], $helper_function['menu_root'], $helper_function['menu_root'] ); + } + + $test = 'test_' . $helper_function['callback']; + + // Call the helper function, passing the desired priority. + call_user_func_array( $helper_function['callback'], array( $test, $test, 'manage_options', 'custom-position', '', $priority ) ); + + $actual_positions[ $test ] = $submenu[ $helper_function['menu_root'] ][ $expected_position ][2]; + } + + // Clean up the temporary user. + wp_delete_user( $admin_user ); + + foreach ( $actual_positions as $test => $actual_position ) { + // Verify the menu was inserted at the expected position. + $this->assertSame( 'custom-position', $actual_position, 'Menu not inserted at the expected position with ' . $test ); + } + } + + /** + * Helper to store the menus that are to be added, so getting the length is programmatically done. + * + * @since 5.3.0 + * + * @return array { + * @type array { + * @type string Page title. + * @type string Menu_title. + * @type string Capability. + * @type string Menu slug. + * @type string Function. + * } + * } + */ + function submenus_to_add() { + return array( + array( 'Submenu Priority', 'Submenu Priority', 'manage_options', 'sub-page', '' ), + array( 'Submenu Priority 2', 'Submenu Priority 2', 'manage_options', 'sub-page2', '' ), + array( 'Submenu Priority 3', 'Submenu Priority 3', 'manage_options', 'sub-page3', '' ), + array( 'Submenu Priority 4', 'Submenu Priority 4', 'manage_options', 'sub-page4', '' ), + array( 'Submenu Priority 5', 'Submenu Priority 5', 'manage_options', 'sub-page5', '' ), + ); + } + + /** + * Data provider for test_submenu_helpers_priority(). + * + * @since 5.3.0 + * + * @return array { + * @type array { + * @type int|null Priority. + * @type int Expected position. + * } + * } + */ + function data_submenu_priority() { + $menu_count = count( $this->submenus_to_add() ); + return array( + array( null, $menu_count ), // Insert at the end of the menu if null is passed. Default behavior. + 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( 1, 1 ), // Insert as the second item. + array( 3, 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. + ); + } + function test_is_plugin_active_true() { activate_plugin( 'hello.php' ); $test = is_plugin_active( 'hello.php' );