From 8e0ddd18c6aee5e34c20f7bdca8466363a1fb5dd Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Thu, 5 Dec 2024 11:44:35 -0300 Subject: [PATCH 1/2] Add a new setting to Protect Content to use WP default order in admin --- .../ProtectedContent/ProtectedContent.php | 49 +++++++++++++++++ tests/php/features/TestProtectedContent.php | 52 ++++++++++++++++++- 2 files changed, 100 insertions(+), 1 deletion(-) diff --git a/includes/classes/Feature/ProtectedContent/ProtectedContent.php b/includes/classes/Feature/ProtectedContent/ProtectedContent.php index 2f001cbb3a..936227be18 100644 --- a/includes/classes/Feature/ProtectedContent/ProtectedContent.php +++ b/includes/classes/Feature/ProtectedContent/ProtectedContent.php @@ -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() ) { @@ -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', + ], + ]; + } } diff --git a/tests/php/features/TestProtectedContent.php b/tests/php/features/TestProtectedContent.php index 5ca71689c9..8741fbe025 100644 --- a/tests/php/features/TestProtectedContent.php +++ b/tests/php/features/TestProtectedContent.php @@ -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' ); @@ -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() ); @@ -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 ); + } } From 6d47a85ad74779f7ddc8292de19ee532ea0b1350 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Thu, 5 Dec 2024 12:21:36 -0300 Subject: [PATCH 2/2] PHP Lint --- tests/php/features/TestProtectedContent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/php/features/TestProtectedContent.php b/tests/php/features/TestProtectedContent.php index 8741fbe025..00557aedb5 100644 --- a/tests/php/features/TestProtectedContent.php +++ b/tests/php/features/TestProtectedContent.php @@ -453,7 +453,7 @@ public function test_maybe_change_sort() { ElasticPress\Features::factory()->activate_feature( 'protected_content' ); ElasticPress\Features::factory()->setup_features(); - $exact_match_id = $this->ep_factory->post->create( + $exact_match_id = $this->ep_factory->post->create( [ 'post_title' => 'exact match - beautiful', 'post_date' => '2021-12-31 23:59:59',