Make WordPress Core

Ticket #39776: 39776.11.diff

File 39776.11.diff, 20.4 KB (added by adamsilverstein, 5 years ago)
  • src/wp-admin/includes/plugin.php

    diff --git src/wp-admin/includes/plugin.php src/wp-admin/includes/plugin.php
    index 683cb7c215..15a69f282b 100644
    function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $func 
    13381338 *                              and only include lowercase alphanumeric, dashes, and underscores characters
    13391339 *                              to be compatible with sanitize_key().
    13401340 * @param callable $function    The function to be called to output the content for this page.
     1341 * @param int      $position    The position order in the menu where this item should appear.
     1342 *
    13411343 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    13421344 */
    1343 function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
     1345function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
    13441346        global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv,
    13451347                $_registered_pages, $_parent_pages;
    13461348
    function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, 
    13701372                }
    13711373        }
    13721374
    1373         $submenu[ $parent_slug ][] = array( $menu_title, $capability, $menu_slug, $page_title );
     1375        $new_sub_menu = array( $menu_title, $capability, $menu_slug, $page_title );
     1376        if ( null === $position ) {
     1377                $submenu[ $parent_slug ][] = $new_sub_menu;
     1378        } else {
     1379                // If position is equal or higher than the number of items in the array, append the submenu.
     1380                if ( $position >= count( $submenu[ $parent_slug ] ) ) {
     1381                        $submenu[ $parent_slug ][] = $new_sub_menu;
     1382                } else {
     1383                        // Test for a negative position.
     1384                        $position = max( $position, 0 );
     1385                        if ( 0 === $position ) {
     1386                                // For negative or `0` positions, prepend the submenu.
     1387                                array_unshift( $submenu[ $parent_slug ], $new_sub_menu );
     1388                        } else {
     1389                                // Grab all of the items before the insertion point.
     1390                                $before_items = array_slice( $submenu[ $parent_slug ], 0, $position, true );
     1391                                // Grab all of the items after the insertion point.
     1392                                $after_items = array_slice( $submenu[ $parent_slug ], $position, null, true );
     1393                                // Add the new item.
     1394                                $before_items[] = $new_sub_menu;
     1395                                // Merge the items.
     1396                                $submenu[ $parent_slug ] = array_merge( $before_items, $after_items );
     1397                        }
     1398                }
     1399        }
     1400        // Sort the parent array
     1401        ksort( $submenu[ $parent_slug ] );
    13741402
    13751403        $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug );
    13761404        if ( ! empty( $function ) && ! empty( $hookname ) ) {
    function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, 
    14091437 * @param string   $capability The capability required for this menu to be displayed to the user.
    14101438 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    14111439 * @param callable $function   The function to be called to output the content for this page.
     1440 * @param int      $position   The position in the menu order this one should appear.
    14121441 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    14131442 */
    1414 function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
    1415         return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function );
     1443function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1444        return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    14161445}
    14171446
    14181447/**
    function add_management_page( $page_title, $menu_title, $capability, $menu_slug, 
    14311460 * @param string   $capability The capability required for this menu to be displayed to the user.
    14321461 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    14331462 * @param callable $function   The function to be called to output the content for this page.
     1463 * @param int      $position   The position in the menu order this one should appear.
    14341464 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    14351465 */
    1436 function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
    1437         return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function );
     1466function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1467        return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    14381468}
    14391469
    14401470/**
    function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $f 
    14531483 * @param string   $capability The capability required for this menu to be displayed to the user.
    14541484 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    14551485 * @param callable $function   The function to be called to output the content for this page.
     1486 * @param int      $position   The position in the menu order this one should appear.
    14561487 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    14571488 */
    1458 function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
    1459         return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function );
     1489function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1490        return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    14601491}
    14611492
    14621493/**
    function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $fun 
    14751506 * @param string   $capability The capability required for this menu to be displayed to the user.
    14761507 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    14771508 * @param callable $function   The function to be called to output the content for this page.
     1509 * @param int      $position   The position in the menu order this one should appear.
    14781510 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    14791511 */
    1480 function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
    1481         return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function );
     1512function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1513        return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    14821514}
    14831515
    14841516/**
    function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $f 
    14971529 * @param string   $capability The capability required for this menu to be displayed to the user.
    14981530 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    14991531 * @param callable $function   The function to be called to output the content for this page.
     1532 * @param int      $position   The position in the menu order this one should appear.
    15001533 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    15011534 */
    1502 function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
     1535
     1536function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
    15031537        if ( current_user_can( 'edit_users' ) ) {
    15041538                $parent = 'users.php';
    15051539        } else {
    15061540                $parent = 'profile.php';
    15071541        }
    1508         return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function );
     1542        return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    15091543}
    15101544/**
    15111545 * Add submenu page to the Dashboard main menu.
    function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $fun 
    15231557 * @param string   $capability The capability required for this menu to be displayed to the user.
    15241558 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    15251559 * @param callable $function   The function to be called to output the content for this page.
     1560 * @param int      $position   The position in the menu order this one should appear.
    15261561 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    15271562 */
    1528 function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
    1529         return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function );
     1563function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1564        return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    15301565}
    15311566
    15321567/**
    function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, 
    15451580 * @param string   $capability The capability required for this menu to be displayed to the user.
    15461581 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    15471582 * @param callable $function   The function to be called to output the content for this page.
     1583 * @param int      $position   The position in the menu order this one should appear.
    15481584 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    15491585 */
    1550 function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
    1551         return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function );
     1586function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1587        return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    15521588}
    15531589
    15541590/**
    function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $fun 
    15671603 * @param string   $capability The capability required for this menu to be displayed to the user.
    15681604 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    15691605 * @param callable $function   The function to be called to output the content for this page.
     1606 * @param int      $position   The position in the menu order this one should appear.
    15701607 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    15711608 */
    1572 function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
    1573         return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function );
     1609function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1610        return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    15741611}
    15751612
    15761613/**
    function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $fun 
    15891626 * @param string   $capability The capability required for this menu to be displayed to the user.
    15901627 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    15911628 * @param callable $function   The function to be called to output the content for this page.
     1629 * @param int      $position   The position in the menu order this one should appear.
    15921630 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    15931631 */
    1594 function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
    1595         return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function );
     1632function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1633        return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    15961634}
    15971635
    15981636/**
    function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $fun 
    16111649 * @param string   $capability The capability required for this menu to be displayed to the user.
    16121650 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    16131651 * @param callable $function   The function to be called to output the content for this page.
     1652 * @param int      $position   The position in the menu order this one should appear.
    16141653 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    16151654 */
    1616 function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
    1617         return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function );
     1655function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1656        return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    16181657}
    16191658
    16201659/**
    function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $fun 
    16331672 * @param string   $capability The capability required for this menu to be displayed to the user.
    16341673 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    16351674 * @param callable $function   The function to be called to output the content for this page.
     1675 * @param int      $position   The position in the menu order this one should appear.
    16361676 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    16371677 */
    1638 function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
    1639         return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function );
     1678function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1679        return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    16401680}
    16411681
    16421682/**
  • tests/phpunit/tests/admin/includesPlugin.php

    diff --git tests/phpunit/tests/admin/includesPlugin.php tests/phpunit/tests/admin/includesPlugin.php
    index 916ca755b7..907dbca8a9 100644
    class Tests_Admin_includesPlugin extends WP_UnitTestCase { 
    5757                wp_set_current_user( $current_user );
    5858        }
    5959
     60        /**
     61         * Tests the priority parameter.
     62         *
     63         * @ticket 39776
     64         * @covers ::add_submenu_page
     65         *
     66         * @param int $priority          The position of the new item.
     67         * @param int $expected_position Where the new item is expected to appear.
     68         * @dataProvider data_submenu_priority
     69         */
     70        function test_submenu_priority( $priority, $expected_position ) {
     71                global $submenu;
     72                global $menu;
     73                $current_user = get_current_user_id();
     74                $admin_user   = self::factory()->user->create( array( 'role' => 'administrator' ) );
     75                wp_set_current_user( $admin_user );
     76                set_current_screen( 'dashboard' );
     77
     78                // Setup a menu with some items.
     79                $parent = add_menu_page( 'Test Toplevel', 'Test Toplevel', 'manage_options', 'mt-top-level-handle', 'mt_toplevel_page' );
     80                foreach ( $this->submenus_to_add() as $menu_to_add ) {
     81                        add_submenu_page( $parent, $menu_to_add[0], $menu_to_add[1], $menu_to_add[2], $menu_to_add[3], $menu_to_add[4] );
     82                }
     83
     84                // Insert the new page.
     85                add_submenu_page( $parent, 'New Page', 'New Page', 'manage_options', 'custom-position', 'custom_pos', $priority );
     86                wp_set_current_user( $current_user );
     87
     88                // Clean up the temporary user.
     89                wp_delete_user( $admin_user );
     90
     91                // Verify the menu was inserted at the expected position.
     92                $this->assertSame( 'custom-position', $submenu[ $parent ][ $expected_position ][2] );
     93        }
     94
     95        /**
     96         * Tests the priority parameter for menu helper functions.
     97         *
     98         * @ticket 39776
     99         *
     100         * @covers ::add_management_page
     101         * @covers ::add_options_page
     102         * @covers ::add_theme_page
     103         * @covers ::add_plugins_page
     104         * @covers ::add_users_page
     105         * @covers ::add_dashboard_page
     106         * @covers ::add_posts_page
     107         * @covers ::add_media_page
     108         * @covers ::add_links_page
     109         * @covers ::add_pages_page
     110         * @covers ::add_comments_page
     111         *
     112         * @param int $priority          The position of the new item.
     113         * @param int $expected_position Where the new item is expected to appear.
     114         *
     115         * @dataProvider data_submenu_priority
     116         */
     117        function test_submenu_helpers_priority( $priority, $expected_position ) {
     118                global $submenu, $menu;
     119                $current_user = get_current_user_id();
     120                $admin_user   = self::factory()->user->create( array( 'role' => 'administrator' ) );
     121                wp_set_current_user( $admin_user );
     122                set_current_screen( 'dashboard' );
     123
     124
     125                // Test the helper functions that use `add_submenu_page`. Each helper adds to a specific menu root.
     126                $helper_functions = array(
     127                        array(
     128                                'callback'  => 'add_management_page',
     129                                'menu_root' => 'tools.php',
     130                        ),
     131                        array(
     132                                'callback'  => 'add_options_page',
     133                                'menu_root' => 'options-general.php',
     134                        ),
     135                        array(
     136                                'callback'  => 'add_theme_page',
     137                                'menu_root' => 'themes.php',
     138                        ),
     139                        array(
     140                                'callback' => 'add_plugins_page',
     141                                'menu_root' => 'plugins.php',
     142                        ),
     143                        array(
     144                                'callback'  => 'add_users_page',
     145                                'menu_root' => 'users.php',
     146                        ),
     147                        array(
     148                                'callback'  => 'add_dashboard_page',
     149                                'menu_root' => 'index.php',
     150                        ),
     151                        array(
     152                                'callback'  => 'add_posts_page',
     153                                'menu_root' => 'edit.php',
     154                        ),
     155                        array(
     156                                'callback'  => 'add_media_page',
     157                                'menu_root' => 'upload.php',
     158                        ),
     159                        array(
     160                                'callback'  => 'add_links_page',
     161                                'menu_root' => 'link-manager.php',
     162                        ),
     163                        array(
     164                                'callback'  => 'add_pages_page',
     165                                'menu_root' => 'edit.php?post_type=page',
     166                        ),
     167                        array(
     168                                'callback'  => 'add_comments_page',
     169                                'menu_root' => 'edit-comments.php',
     170                        ),
     171                );
     172
     173                foreach ( $helper_functions as $helper_function ) {
     174
     175                        // Reset menus.
     176                        $submenu = [];
     177                        $menu = [];
     178
     179                        // Build up demo pages on the menu root.
     180                        foreach ( $this->submenus_to_add() as $menu_to_add ) {
     181                                add_menu_page( $menu_to_add[0], $menu_to_add[1], $menu_to_add[2], $helper_function[ 'menu_root' ], $helper_function[ 'menu_root' ] );
     182                        }
     183
     184                        $test = 'test_' . $helper_function[ 'callback' ];
     185
     186                        // Call the helper function, passing the desired priority.
     187                        call_user_func( $helper_function[ 'callback' ], $test, $test, 'manage_options', 'custom-position', '', $priority );
     188
     189                        // Verify the menu was inserted at the expected position.
     190                        $this->assertSame( 'custom-position', $submenu[ $helper_function[ 'menu_root' ] ][ $expected_position ][2] );
     191                }
     192
     193                // Clean up the temporary user.
     194                wp_delete_user( $admin_user );
     195        }
     196
     197        /**
     198         * Helper to store the menus to add so getting the length is programmatically done.
     199         * @since 5.3.0
     200         *
     201         * @return array
     202         */
     203        function submenus_to_add() {
     204                return array(
     205                        array( 'Submenu Priority', 'Submenu Priority', 'manage_options', 'sub-page', '' ),
     206                        array( 'Submenu Priority 2', 'Submenu Priority 2', 'manage_options', 'sub-page2', '' ),
     207                        array( 'Submenu Priority 3', 'Submenu Priority 3', 'manage_options', 'sub-page3', '' ),
     208                        array( 'Submenu Priority 4', 'Submenu Priority 4', 'manage_options', 'sub-page4', '' ),
     209                        array( 'Submenu Priority 5', 'Submenu Priority 5', 'manage_options', 'sub-page5', '' ),
     210                );
     211        }
     212
     213        /**
     214         * Data provider for the above priority parameter tests.
     215         * @return array
     216         */
     217        function data_submenu_priority() {
     218                $menu_count = count( $this->submenus_to_add() );
     219                return array(
     220                        array( null, $menu_count ),        // Insert at the end of the menu if null is passed. Default behaviour.
     221                        array( 0, 0 ),                     // Insert at the beginning of the menu if 0 is passed.
     222                        array( -1, 0 ),                    // Negative numbers are treated the same as passing 0.
     223                        array( -7, 0 ),                    // Negative numbers are treated the same as passing 0.
     224                        array( 1, 1 ),                     // Insert as the second item.
     225                        array( 3, 3 ),                     // Insert as the 4th item.
     226                        array( $menu_count, $menu_count ), // Numbers equal to the number of items are added at the end.
     227                        array( 123456, $menu_count ),       // Numbers higher than the number of items are added at the end.
     228                );
     229        }
     230
    60231        function test_is_plugin_active_true() {
    61232                activate_plugin( 'hello.php' );
    62233                $test = is_plugin_active( 'hello.php' );