WordPress.org

Make WordPress Core

Changeset 48151


Ignore:
Timestamp:
06/23/2020 11:13:35 PM (12 days ago)
Author:
whyisjake
Message:

Administration: Update WP_List_Table::get_sortable_columns() to support asc and desc arguments.

This makes the API a little more clear, whereas setting false used to mean asc and true meant desc, you can now use those directly, while maintaining back-compat.

Fixes #45089.

Props Tkama, SergeyBiryukov, shital-patel, desrosj, birgire, davidbaumwald.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-list-table.php

    r48134 r48151  
    947947
    948948    /**
    949      * Get a list of sortable columns. The format is:
    950      * 'internal-name' => 'orderby'
    951      * or
    952      * 'internal-name' => array( 'orderby', true )
    953      *
    954      * The second format will make the initial sorting order be descending
     949     * Get a list of sortable columns.
     950     *
     951     * The format is:
     952     * - `'internal-name' => 'orderby'`
     953     * - `'internal-name' => array( 'orderby', 'asc' )` - The second element set the initial sorting order.
     954     * - `'internal-name' => array( 'orderby', true )` - The second element will make the initial sorting order be descending.
    955955     *
    956956     * @since 3.1.0
     
    11621162                    $class[] = $current_order;
    11631163                } else {
    1164                     $order   = $desc_first ? 'desc' : 'asc';
     1164                    if ( in_array( strtolower( $desc_first ), array( 'desc', 'asc' ), true ) ) {
     1165                        $order = 'asc' === strtolower( $desc_first ) ? 'desc' : 'asc';
     1166                    } else {
     1167                        $order = $desc_first ? 'desc' : 'asc';
     1168                    }
    11651169                    $class[] = 'sortable';
    1166                     $class[] = $desc_first ? 'asc' : 'desc';
     1170                    $class[] = 'desc' === $order ? 'asc' : 'desc';
    11671171                }
    11681172
  • trunk/tests/phpunit/tests/admin/includesListTable.php

    r47122 r48151  
    294294        $this->assertNotContains( 'id="delete_all"', $output );
    295295    }
     296
     297    /**
     298     * @ticket 45089
     299     */
     300    public function test_sortable_columns() {
     301        require_once ABSPATH . 'wp-admin/includes/class-wp-comments-list-table.php';
     302
     303        $override_sortable_columns = array(
     304            'author'   => array( 'comment_author', true ),
     305            'response' => 'comment_post_ID',
     306            'date'     => array( 'comment_date', 'dEsC' ), // The ordering support should be case insensitive.
     307        );
     308
     309        // Stub the get_sortable_columns() method.
     310        $object = $this->getMockBuilder( 'WP_Comments_List_Table' )
     311            ->setConstructorArgs( array( array( 'screen' => 'edit-comments' ) ) )
     312            ->setMethods( array( 'get_sortable_columns' ) )
     313            ->getMock();
     314
     315        // Change the null return value of the stubbed get_sortable_columns() method.
     316        $object->method( 'get_sortable_columns' )
     317            ->willReturn( $override_sortable_columns );
     318
     319        $output = get_echo( array( $object, 'print_column_headers' ) );
     320
     321        $this->assertContains( '?orderby=comment_author&order=desc', $output, 'Mismatch of the default link ordering for comment author column. Should be desc.' );
     322        $this->assertContains( 'column-author sortable asc', $output, 'Mismatch of CSS classes for the comment author column.' );
     323
     324        $this->assertContains( '?orderby=comment_post_ID&order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' );
     325        $this->assertContains( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' );
     326
     327        $this->assertContains( '?orderby=comment_date&order=asc', $output, 'Mismatch of the default link ordering for comment author column. Should be asc.' );
     328        $this->assertContains( 'column-date sortable desc', $output, 'Mismatch of CSS classes for the comment date column.' );
     329    }
     330
     331    /**
     332     * @ticket 45089
     333     */
     334    public function test_sortable_columns_with_current_ordering() {
     335        require_once ABSPATH . 'wp-admin/includes/class-wp-comments-list-table.php';
     336
     337        $override_sortable_columns = array(
     338            'author'   => array( 'comment_author', false ),
     339            'response' => 'comment_post_ID',
     340            'date'     => array( 'comment_date', 'asc' ), // We will override this with current ordering.
     341        );
     342
     343        // Current ordering.
     344        $_GET['orderby'] = 'comment_date';
     345        $_GET['order']   = 'desc';
     346
     347        // Stub the get_sortable_columns() method.
     348        $object = $this->getMockBuilder( 'WP_Comments_List_Table' )
     349            ->setConstructorArgs( array( array( 'screen' => 'edit-comments' ) ) )
     350            ->setMethods( array( 'get_sortable_columns' ) )
     351            ->getMock();
     352
     353        // Change the null return value of the stubbed get_sortable_columns() method.
     354        $object->method( 'get_sortable_columns' )
     355            ->willReturn( $override_sortable_columns );
     356
     357        $output = get_echo( array( $object, 'print_column_headers' ) );
     358
     359        $this->assertContains( '?orderby=comment_author&order=asc', $output, 'Mismatch of the default link ordering for comment author column. Should be asc.' );
     360        $this->assertContains( 'column-author sortable desc', $output, 'Mismatch of CSS classes for the comment author column.' );
     361
     362        $this->assertContains( '?orderby=comment_post_ID&order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' );
     363        $this->assertContains( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' );
     364
     365        $this->assertContains( '?orderby=comment_date&order=asc', $output, 'Mismatch of the current link ordering for comment date column. Should be asc.' );
     366        $this->assertContains( 'column-date sorted desc', $output, 'Mismatch of CSS classes for the comment date column.' );
     367    }
     368
    296369}
Note: See TracChangeset for help on using the changeset viewer.