Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new setting to Protect Content to use WP default order in admin #4028

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions includes/classes/Feature/ProtectedContent/ProtectedContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function setup() {
add_filter( 'ep_admin_wp_query_integration', '__return_true' );
add_action( 'pre_get_posts', [ $this, 'integrate' ] );
add_filter( 'ep_post_query_db_args', [ $this, 'query_password_protected_posts' ] );
add_filter( 'ep_set_sort', [ $this, 'maybe_change_sort' ] );
}

if ( Features::factory()->get_registered_feature( 'comments' )->is_active() ) {
Expand Down Expand Up @@ -427,4 +428,52 @@ public function requirements_status() {
public function sync_password_protected( $new_skip, bool $skip ) : bool {
return $skip;
}

/**
* Maybe change the sort order for the WP Dashboard.
*
* If the admin user has enabled the setting to use the default WordPress sort order,
* we will change the sort order to (somewhat) match the default WP behavior.
*
* @since 5.1.4
*
* @param array $default_sort The previous value of the `ep_set_sort` filter
* @return array
*/
public function maybe_change_sort( $default_sort ) {
if ( ! function_exists( '\get_current_screen' ) ) {
return $default_sort;
}

$screen = get_current_screen();
if ( 'edit' !== $screen->base ) {
return $default_sort;
}

if ( ! $this->get_setting( 'use_default_wp_sort' ) ) {
return $default_sort;
}

return [
[ 'post_date' => [ 'order' => 'desc' ] ],
[ 'post_title.sortable' => [ 'order' => 'asc' ] ],
];
}

/**
* Set the `settings_schema` attribute
*
* @since 5.1.4
*/
protected function set_settings_schema() {
$this->settings_schema = [
[
'default' => '0',
'key' => 'use_default_wp_sort',
'help' => __( 'Enable to use WordPress default sort for searches inside the WP Dashboard.', 'elasticpress' ),
'label' => __( 'Use default WordPress sort on the WP Dashboard', 'elasticpress' ),
'type' => 'checkbox',
],
];
}
}
52 changes: 51 additions & 1 deletion tests/php/features/TestProtectedContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ public function testAdminOnDraftUpdated() {

/**
* Check posts filter by category in dashboard
*
* @group protected-content
*/
public function testAdminCategories() {
set_current_screen( 'edit.php' );
Expand Down Expand Up @@ -403,9 +405,9 @@ public function testFrontEndSearchPasswordedPost() {
* Check admin comment query are powered by Elasticsearch
*
* @since 4.4.1
* @group protected-content
*/
public function testAdminCommentQuery() {

set_current_screen( 'edit-comments.php' );
$this->assertTrue( is_admin() );

Expand Down Expand Up @@ -437,4 +439,52 @@ public function testAdminCommentQuery() {
$this->assertTrue( $comments_query->elasticsearch_success );
$this->assertEquals( 1, $comments_query->found_comments );
}

/**
* Test the `maybe_change_sort` method.
*
* @since 5.1.4
* @group protected-content
*/
public function test_maybe_change_sort() {
set_current_screen( 'edit.php' );
$this->assertTrue( is_admin() );

ElasticPress\Features::factory()->activate_feature( 'protected_content' );
ElasticPress\Features::factory()->setup_features();

$exact_match_id = $this->ep_factory->post->create(
[
'post_title' => 'exact match - beautiful',
'post_date' => '2021-12-31 23:59:59',
]
);
$not_so_good_match_id = $this->ep_factory->post->create(
[
'post_title' => 'not so good match - beautful',
'post_date' => '2022-12-31 23:59:59',
]
);

ElasticPress\Elasticsearch::factory()->refresh_indices();

// By default, display the best match first
$query = new \WP_Query( [ 's' => 'beautiful' ] );
$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 2, $query->found_posts );
$this->assertEquals( $exact_match_id, $query->posts[0]->ID );

$filter = function( $value ) {
$value['protected_content']['use_default_wp_sort'] = '1';
return $value;
};
add_filter( 'site_option_ep_feature_settings', $filter );
add_filter( 'option_ep_feature_settings', $filter );

// With the option enabled, order by date
$query = new \WP_Query( [ 's' => 'beautiful' ] );
$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 2, $query->found_posts );
$this->assertEquals( $not_so_good_match_id, $query->posts[0]->ID );
}
}
Loading