From 95b9933e64b460284f7c6bbc49fc12a75470fe0c Mon Sep 17 00:00:00 2001 From: Jake Spurlock Date: Tue, 23 Jun 2020 23:13:35 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@48151 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/class-wp-list-table.php | 18 +++-- .../phpunit/tests/admin/includesListTable.php | 73 +++++++++++++++++++ 2 files changed, 84 insertions(+), 7 deletions(-) diff --git a/src/wp-admin/includes/class-wp-list-table.php b/src/wp-admin/includes/class-wp-list-table.php index 3c7e6b3a3f..bc6f0a7fd8 100644 --- a/src/wp-admin/includes/class-wp-list-table.php +++ b/src/wp-admin/includes/class-wp-list-table.php @@ -946,12 +946,12 @@ class WP_List_Table { } /** - * Get a list of sortable columns. The format is: - * 'internal-name' => 'orderby' - * or - * 'internal-name' => array( 'orderby', true ) + * Get a list of sortable columns. * - * The second format will make the initial sorting order be descending + * The format is: + * - `'internal-name' => 'orderby'` + * - `'internal-name' => array( 'orderby', 'asc' )` - The second element set the initial sorting order. + * - `'internal-name' => array( 'orderby', true )` - The second element will make the initial sorting order be descending. * * @since 3.1.0 * @@ -1161,9 +1161,13 @@ class WP_List_Table { $class[] = 'sorted'; $class[] = $current_order; } else { - $order = $desc_first ? 'desc' : 'asc'; + if ( in_array( strtolower( $desc_first ), array( 'desc', 'asc' ), true ) ) { + $order = 'asc' === strtolower( $desc_first ) ? 'desc' : 'asc'; + } else { + $order = $desc_first ? 'desc' : 'asc'; + } $class[] = 'sortable'; - $class[] = $desc_first ? 'asc' : 'desc'; + $class[] = 'desc' === $order ? 'asc' : 'desc'; } $column_display_name = '' . $column_display_name . ''; diff --git a/tests/phpunit/tests/admin/includesListTable.php b/tests/phpunit/tests/admin/includesListTable.php index 9edbea1793..56b3be2d20 100644 --- a/tests/phpunit/tests/admin/includesListTable.php +++ b/tests/phpunit/tests/admin/includesListTable.php @@ -293,4 +293,77 @@ class Tests_Admin_includesListTable extends WP_UnitTestCase { $this->assertNotContains( 'id="delete_all"', $output ); } + + /** + * @ticket 45089 + */ + public function test_sortable_columns() { + require_once ABSPATH . 'wp-admin/includes/class-wp-comments-list-table.php'; + + $override_sortable_columns = array( + 'author' => array( 'comment_author', true ), + 'response' => 'comment_post_ID', + 'date' => array( 'comment_date', 'dEsC' ), // The ordering support should be case insensitive. + ); + + // Stub the get_sortable_columns() method. + $object = $this->getMockBuilder( 'WP_Comments_List_Table' ) + ->setConstructorArgs( array( array( 'screen' => 'edit-comments' ) ) ) + ->setMethods( array( 'get_sortable_columns' ) ) + ->getMock(); + + // Change the null return value of the stubbed get_sortable_columns() method. + $object->method( 'get_sortable_columns' ) + ->willReturn( $override_sortable_columns ); + + $output = get_echo( array( $object, 'print_column_headers' ) ); + + $this->assertContains( '?orderby=comment_author&order=desc', $output, 'Mismatch of the default link ordering for comment author column. Should be desc.' ); + $this->assertContains( 'column-author sortable asc', $output, 'Mismatch of CSS classes for the comment author column.' ); + + $this->assertContains( '?orderby=comment_post_ID&order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' ); + $this->assertContains( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' ); + + $this->assertContains( '?orderby=comment_date&order=asc', $output, 'Mismatch of the default link ordering for comment author column. Should be asc.' ); + $this->assertContains( 'column-date sortable desc', $output, 'Mismatch of CSS classes for the comment date column.' ); + } + + /** + * @ticket 45089 + */ + public function test_sortable_columns_with_current_ordering() { + require_once ABSPATH . 'wp-admin/includes/class-wp-comments-list-table.php'; + + $override_sortable_columns = array( + 'author' => array( 'comment_author', false ), + 'response' => 'comment_post_ID', + 'date' => array( 'comment_date', 'asc' ), // We will override this with current ordering. + ); + + // Current ordering. + $_GET['orderby'] = 'comment_date'; + $_GET['order'] = 'desc'; + + // Stub the get_sortable_columns() method. + $object = $this->getMockBuilder( 'WP_Comments_List_Table' ) + ->setConstructorArgs( array( array( 'screen' => 'edit-comments' ) ) ) + ->setMethods( array( 'get_sortable_columns' ) ) + ->getMock(); + + // Change the null return value of the stubbed get_sortable_columns() method. + $object->method( 'get_sortable_columns' ) + ->willReturn( $override_sortable_columns ); + + $output = get_echo( array( $object, 'print_column_headers' ) ); + + $this->assertContains( '?orderby=comment_author&order=asc', $output, 'Mismatch of the default link ordering for comment author column. Should be asc.' ); + $this->assertContains( 'column-author sortable desc', $output, 'Mismatch of CSS classes for the comment author column.' ); + + $this->assertContains( '?orderby=comment_post_ID&order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' ); + $this->assertContains( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' ); + + $this->assertContains( '?orderby=comment_date&order=asc', $output, 'Mismatch of the current link ordering for comment date column. Should be asc.' ); + $this->assertContains( 'column-date sorted desc', $output, 'Mismatch of CSS classes for the comment date column.' ); + } + }