Skip to content

Commit

Permalink
Merge pull request #4028 from 10up/fix/issue-3815
Browse files Browse the repository at this point in the history
Add a new setting to Protect Content to use WP default order in admin
  • Loading branch information
felipeelia authored Dec 5, 2024
2 parents 0109ece + 6d47a85 commit 7a2bcf4
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
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 );
}
}

0 comments on commit 7a2bcf4

Please sign in to comment.