Make WordPress Core

Ticket #39776: 39776.8.diff

File 39776.8.diff, 17.2 KB (added by welcher, 5 years ago)

Updates as per feedback

  • src/wp-admin/includes/plugin.php

    diff --git src/wp-admin/includes/plugin.php src/wp-admin/includes/plugin.php
    index 683cb7c215..5daf75d123 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, just append it.
     1380                if ( $position >= count( $submenu[ $parent_slug ] ) ) {
     1381                        $submenu[ $parent_slug ][] = $new_sub_menu;
     1382                } else {
     1383                        // Ensure we don't have a negative position.
     1384                        $position = max( $position, 0 );
     1385                        if ( 0 === $position ) {
     1386                                array_unshift( $submenu[ $parent_slug ], $new_sub_menu );
     1387                        } else {
     1388                                // Grab all of the items before the insertion point.
     1389                                $before_items = array_slice( $submenu[ $parent_slug ], 0, $position, true );
     1390                                // Grab all of the items after the insertion point.
     1391                                $after_items = array_slice( $submenu[ $parent_slug ], $position, null, true );
     1392                                // Add the new item.
     1393                                $before_items[] = $new_sub_menu;
     1394                                // Merge the items.
     1395                                $submenu[ $parent_slug ] = array_merge( $before_items, $after_items );
     1396                        }
     1397                }
     1398        }
     1399        // Sort the parent array
     1400        ksort( $submenu[ $parent_slug ] );
    13741401
    13751402        $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug );
    13761403        if ( ! empty( $function ) && ! empty( $hookname ) ) {
    function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, 
    14091436 * @param string   $capability The capability required for this menu to be displayed to the user.
    14101437 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    14111438 * @param callable $function   The function to be called to output the content for this page.
     1439 * @param int      $position   The position in the menu order this one should appear.
    14121440 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    14131441 */
    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 );
     1442function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1443        return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    14161444}
    14171445
    14181446/**
    function add_management_page( $page_title, $menu_title, $capability, $menu_slug, 
    14311459 * @param string   $capability The capability required for this menu to be displayed to the user.
    14321460 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    14331461 * @param callable $function   The function to be called to output the content for this page.
     1462 * @param int      $position   The position in the menu order this one should appear.
    14341463 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    14351464 */
    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 );
     1465function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1466        return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    14381467}
    14391468
    14401469/**
    function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $f 
    14531482 * @param string   $capability The capability required for this menu to be displayed to the user.
    14541483 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    14551484 * @param callable $function   The function to be called to output the content for this page.
     1485 * @param int      $position   The position in the menu order this one should appear.
    14561486 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    14571487 */
    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 );
     1488function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1489        return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    14601490}
    14611491
    14621492/**
    function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $fun 
    14751505 * @param string   $capability The capability required for this menu to be displayed to the user.
    14761506 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    14771507 * @param callable $function   The function to be called to output the content for this page.
     1508 * @param int      $position   The position in the menu order this one should appear.
    14781509 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    14791510 */
    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 );
     1511function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1512        return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    14821513}
    14831514
    14841515/**
    function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $f 
    14971528 * @param string   $capability The capability required for this menu to be displayed to the user.
    14981529 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    14991530 * @param callable $function   The function to be called to output the content for this page.
     1531 * @param int      $position   The position in the menu order this one should appear.
    15001532 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    15011533 */
    1502 function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
    1503         if ( current_user_can( 'edit_users' ) ) {
     1534
     1535function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1536        if ( current_user_can('edit_users') ) {
    15041537                $parent = 'users.php';
    15051538        } else {
    15061539                $parent = 'profile.php';
    15071540        }
    1508         return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function );
     1541        return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    15091542}
    15101543/**
    15111544 * Add submenu page to the Dashboard main menu.
    function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $fun 
    15231556 * @param string   $capability The capability required for this menu to be displayed to the user.
    15241557 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    15251558 * @param callable $function   The function to be called to output the content for this page.
     1559 * @param int      $position   The position in the menu order this one should appear.
    15261560 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    15271561 */
    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 );
     1562function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1563        return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    15301564}
    15311565
    15321566/**
    function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, 
    15451579 * @param string   $capability The capability required for this menu to be displayed to the user.
    15461580 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    15471581 * @param callable $function   The function to be called to output the content for this page.
     1582 * @param int      $position   The position in the menu order this one should appear.
    15481583 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    15491584 */
    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 );
     1585function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1586        return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    15521587}
    15531588
    15541589/**
    function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $fun 
    15671602 * @param string   $capability The capability required for this menu to be displayed to the user.
    15681603 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    15691604 * @param callable $function   The function to be called to output the content for this page.
     1605 * @param int      $position   The position in the menu order this one should appear.
    15701606 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    15711607 */
    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 );
     1608function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1609        return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    15741610}
    15751611
    15761612/**
    function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $fun 
    15891625 * @param string   $capability The capability required for this menu to be displayed to the user.
    15901626 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    15911627 * @param callable $function   The function to be called to output the content for this page.
     1628 * @param int      $position   The position in the menu order this one should appear.
    15921629 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    15931630 */
    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 );
     1631function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1632        return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    15961633}
    15971634
    15981635/**
    function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $fun 
    16111648 * @param string   $capability The capability required for this menu to be displayed to the user.
    16121649 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    16131650 * @param callable $function   The function to be called to output the content for this page.
     1651 * @param int      $position   The position in the menu order this one should appear.
    16141652 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    16151653 */
    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 );
     1654function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1655        return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function, $position);
    16181656}
    16191657
    16201658/**
    function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $fun 
    16331671 * @param string   $capability The capability required for this menu to be displayed to the user.
    16341672 * @param string   $menu_slug  The slug name to refer to this menu by (should be unique for this menu).
    16351673 * @param callable $function   The function to be called to output the content for this page.
     1674 * @param int      $position   The position in the menu order this one should appear.
    16361675 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
    16371676 */
    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 );
     1677function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
     1678        return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position );
    16401679}
    16411680
    16421681/**
  • tests/phpunit/tests/admin/includesPlugin.php

    diff --git tests/phpunit/tests/admin/includesPlugin.php tests/phpunit/tests/admin/includesPlugin.php
    index 916ca755b7..29b3f1cb16 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         * @param int $priority          The position of the new item.
     65         * @param int $expected_position Where the new item is expected to appear.
     66         * @dataProvider data_submenu_priority
     67         */
     68        function test_submenu_priority( $priority, $expected_position  ) {
     69                global $submenu;
     70                $current_user = get_current_user_id();
     71                wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
     72                set_current_screen( 'dashboard' );
     73
     74                // Setup a menu with some items.
     75                $parent = add_menu_page( 'Test Toplevel', 'Test Toplevel', 'manage_options', 'mt-top-level-handle', 'mt_toplevel_page' );
     76                foreach ( $this->submenus_to_add() as $menu ) {
     77                        add_submenu_page( $parent, $menu[0], $menu[1], $menu[2], $menu[3], $menu[4] );
     78                }
     79
     80                // Insert the new page.
     81                add_submenu_page( $parent, 'New Page', 'New Page', 'manage_options', 'custom-position', 'custom_pos' , $priority );
     82                wp_set_current_user( $current_user );
     83
     84                $this->assertSame( 'custom-position', $submenu[ $parent ][ $expected_position ][2] );
     85        }
     86
     87        /**
     88         * Helper to store the menus to add so getting the length is programmatically done.
     89         * @since 5.3.0
     90         *
     91         * @return array
     92         */
     93        function submenus_to_add() {
     94                return array(
     95                        array( 'Submenu Priority', 'Submenu Priority', 'manage_options', 'sub-page', '' ),
     96                        array( 'Submenu Priority 2', 'Submenu Priority 2', 'manage_options', 'sub-page2', '' ),
     97                        array( 'Submenu Priority 3', 'Submenu Priority 3', 'manage_options', 'sub-page3', '' ),
     98                        array( 'Submenu Priority 4', 'Submenu Priority 4', 'manage_options', 'sub-page4', '' ),
     99                        array( 'Submenu Priority 5', 'Submenu Priority 5', 'manage_options', 'sub-page5', '' ),
     100                );
     101        }
     102        /**
     103         * Data provider for the above tests
     104         * @return array
     105         */
     106        function data_submenu_priority() {
     107                $menu_count = count( $this->submenus_to_add() );
     108                return array(
     109                        array( null, $menu_count ),        // Insert at the end of the menu is null is passed. Default behaviour.
     110                        array( 0, 0 ),                     // Insert at the beginning of the menu if 0 is passed.
     111                        array( -1, 0 ),                    // Negative numbers are treated the same as passing 0.
     112                        array( -7, 0 ),                    // Negative numbers are treated the same as passing 0.
     113                        array( 1, 1 ),                     // Insert as the second item.
     114                        array( 3, 3 ),                     // Insert as the 4th item.
     115                        array( $menu_count, $menu_count ), // Numbers equal to the number of items are added at the end.
     116                        array( 123456, $menu_count )       // Numbers higher than the number of items are added at the end.
     117                );
     118        }
     119
    60120        function test_is_plugin_active_true() {
    61121                activate_plugin( 'hello.php' );
    62122                $test = is_plugin_active( 'hello.php' );