Make WordPress Core


Ignore:
Timestamp:
09/20/2019 02:59:04 PM (5 years ago)
Author:
adamsilverstein
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/includesPlugin.php

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