From 72c7f2c41c6ead0a3712394405cbe9dcdbeeb0ef Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 25 Jun 2020 11:33:23 +0000 Subject: [PATCH] Administration: Correct and simplify the logic for `asc` and `desc` arguments in `WP_List_Table::get_sortable_columns()`. Setting the initial order didn't work as expected due to reversed logic. Follow-up to [48151]. See #45089. git-svn-id: https://develop.svn.wordpress.org/trunk@48165 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/class-wp-list-table.php | 12 ++++++++---- tests/phpunit/tests/admin/includesListTable.php | 6 +++--- 2 files changed, 11 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 f277a0b8a6..7cf6f0725f 100644 --- a/src/wp-admin/includes/class-wp-list-table.php +++ b/src/wp-admin/includes/class-wp-list-table.php @@ -1170,9 +1170,9 @@ class WP_List_Table { $class[] = 'sorted'; $class[] = $current_order; } else { - if ( in_array( strtolower( $desc_first ), array( 'desc', 'asc' ), true ) ) { - $order = 'asc' === strtolower( $desc_first ) ? 'desc' : 'asc'; - } else { + $order = strtolower( $desc_first ); + + if ( ! in_array( $order, array( 'desc', 'asc' ), true ) ) { $order = $desc_first ? 'desc' : 'asc'; } @@ -1180,7 +1180,11 @@ class WP_List_Table { $class[] = 'desc' === $order ? 'asc' : 'desc'; } - $column_display_name = '' . $column_display_name . ''; + $column_display_name = sprintf( + '%s', + esc_url( add_query_arg( compact( 'orderby', 'order' ), $current_url ) ), + $column_display_name + ); } $tag = ( 'cb' === $column_key ) ? 'td' : 'th'; diff --git a/tests/phpunit/tests/admin/includesListTable.php b/tests/phpunit/tests/admin/includesListTable.php index 56b3be2d20..745b224e9c 100644 --- a/tests/phpunit/tests/admin/includesListTable.php +++ b/tests/phpunit/tests/admin/includesListTable.php @@ -303,7 +303,7 @@ class Tests_Admin_includesListTable extends WP_UnitTestCase { $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. + 'date' => array( 'comment_date', 'dEsC' ), // The ordering support should be case-insensitive. ); // Stub the get_sortable_columns() method. @@ -324,8 +324,8 @@ class Tests_Admin_includesListTable extends WP_UnitTestCase { $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.' ); + $this->assertContains( '?orderby=comment_date&order=desc', $output, 'Mismatch of the default link ordering for comment date column. Should be asc.' ); + $this->assertContains( 'column-date sortable asc', $output, 'Mismatch of CSS classes for the comment date column.' ); } /**