WordPress.org

Make WordPress Core

Changeset 33985


Ignore:
Timestamp:
09/10/2015 01:26:26 AM (4 years ago)
Author:
wonderboymusic
Message:

Implement a priority system for Help Tabs to add them at specific positions.

Adds unit tests.

Props swissspidy.
Fixes #19828.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/screen.php

    r33689 r33985  
    719719     *
    720720     * @since 3.4.0
     721     * @since 4.4.0 Help tabs are ordered by their priority.
    721722     *
    722723     * @return array Help tabs with arguments.
    723724     */
    724725    public function get_help_tabs() {
    725         return $this->_help_tabs;
     726        $help_tabs = $this->_help_tabs;
     727        uasort( $help_tabs, array( $this, '_sort_help_tabs' ) );
     728        return $help_tabs;
     729    }
     730
     731    /**
     732     * Compares the difference between the help tabs priorities.
     733     *
     734     * Used for help tabs sorting.
     735     *
     736     * @since 4.4.0
     737     *
     738     * @param int $tab_a The priority argument for the first tab.
     739     * @param int $tab_b The priority argument for the second tab.
     740     * @return int The difference between the priority arguments.
     741     */
     742    protected function _sort_help_tabs( $tab_a, $tab_b ) {
     743        return $tab_a['priority'] - $tab_b['priority'];
    726744    }
    727745
     
    745763     *
    746764     * @since 3.3.0
    747      *
    748      * @param array $args
    749      * - string   - title    - Title for the tab.
    750      * - string   - id       - Tab ID. Must be HTML-safe.
    751      * - string   - content  - Help tab content in plain text or HTML. Optional.
    752      * - callback - callback - A callback to generate the tab content. Optional.
    753      *
     765     * @since 4.4.0 The `$priority` argument was added.
     766     *
     767     * @param array $args {
     768     *     Array of arguments used to display the help tab.
     769     *
     770     *     @type string $title    Title for the tab. Default false.
     771     *     @type string $id       Tab ID. Must be HTML-safe. Default false.
     772     *     @type string $content  Optional. Help tab content in plain text or HTML. Default empty string.
     773     *     @type string $callback Optional. A callback to generate the tab content. Default false.
     774     *     @type int    $priority Optional. The priority of the tab, used for ordering. Default 10.
     775     * }
    754776     */
    755777    public function add_help_tab( $args ) {
     
    759781            'content'  => '',
    760782            'callback' => false,
     783            'priority' => 10,
    761784        );
    762785        $args = wp_parse_args( $args, $defaults );
  • trunk/tests/phpunit/tests/admin/includesScreen.php

    r31622 r33985  
    164164        $screen = get_current_screen();
    165165        $screen->add_help_tab( $tab_args );
    166         $this->assertEquals( $screen->get_help_tab( $tab ), $tab_args );
     166        $this->assertEquals( $screen->get_help_tab( $tab ), array(
     167            'id' => $tab,
     168            'title' => 'Help!',
     169            'content' => 'Some content',
     170            'callback' => false,
     171            'priority' => 10,
     172        ) );
    167173
    168174        $tabs = $screen->get_help_tabs();
     
    171177        $screen->remove_help_tab( $tab );
    172178        $this->assertNull( $screen->get_help_tab( $tab ) );
     179
     180        $screen->remove_help_tabs();
     181        $this->assertEquals( $screen->get_help_tabs(), array() );
     182    }
     183
     184    /**
     185     * @ticket 19828
     186     */
     187    function test_help_tabs_priority() {
     188        $tab_1      = rand_str();
     189        $tab_1_args = array(
     190            'id'       => $tab_1,
     191            'title'    => 'Help!',
     192            'content'  => 'Some content',
     193            'callback' => false,
     194            'priority' => 11,
     195        );
     196
     197        $tab_2      = rand_str();
     198        $tab_2_args = array(
     199            'id'       => $tab_2,
     200            'title'    => 'Help!',
     201            'content'  => 'Some content',
     202            'callback' => false,
     203            'priority' => 9,
     204        );
     205
     206        $screen = get_current_screen();
     207
     208        // Add help tabs.
     209
     210        $screen->add_help_tab( $tab_1_args );
     211        $this->assertEquals( $screen->get_help_tab( $tab_1 ), $tab_1_args );
     212
     213        $screen->add_help_tab( $tab_2_args );
     214        $this->assertEquals( $screen->get_help_tab( $tab_2 ), $tab_2_args );
     215
     216        $tabs = $screen->get_help_tabs();
     217        $this->assertEquals( 2, count( $tabs ) );
     218        $this->assertArrayHasKey( $tab_1, $tabs );
     219        $this->assertArrayHasKey( $tab_2, $tabs );
     220
     221        // Test priority order.
     222
     223        $this->assertEquals( $tabs, array(
     224            $tab_2 => $tab_2_args,
     225            $tab_1 => $tab_1_args,
     226        ) );
     227
     228        $screen->remove_help_tab( $tab_1 );
     229        $this->assertNull( $screen->get_help_tab( $tab_1 ) );
     230        $this->assertEquals( 1, count( $screen->get_help_tabs() ) );
     231
     232        $screen->remove_help_tab( $tab_2 );
     233        $this->assertNull( $screen->get_help_tab( $tab_2 ) );
     234        $this->assertEquals( 0, count( $screen->get_help_tabs() ) );
    173235
    174236        $screen->remove_help_tabs();
Note: See TracChangeset for help on using the changeset viewer.