diff --git a/includes/classes/Feature/Comments/Comments.php b/includes/classes/Feature/Comments/Comments.php index d7631289d5..470bec373e 100644 --- a/includes/classes/Feature/Comments/Comments.php +++ b/includes/classes/Feature/Comments/Comments.php @@ -110,7 +110,7 @@ public function integrate_search_queries( $enabled, $query ) { return $enabled; } - if ( isset( $query->query_vars['ep_integrate'] ) && false === $query->query_vars['ep_integrate'] ) { + if ( isset( $query->query_vars['ep_integrate'] ) && ! filter_var( $query->query_vars['ep_integrate'], FILTER_VALIDATE_BOOL ) ) { $enabled = false; } elseif ( ! empty( $query->query_vars['search'] ) ) { $enabled = true; diff --git a/includes/classes/Feature/Search/Search.php b/includes/classes/Feature/Search/Search.php index 1d1e2d9ab0..2ad26d6233 100644 --- a/includes/classes/Feature/Search/Search.php +++ b/includes/classes/Feature/Search/Search.php @@ -576,6 +576,10 @@ public function integrate_search_queries( $enabled, $query ) { return $enabled; } + if ( isset( $query->query_vars['ep_integrate'] ) && ! filter_var( $query->query_vars['ep_integrate'], FILTER_VALIDATE_BOOL ) ) { + return false; + } + if ( method_exists( $query, 'is_search' ) && $query->is_search() && ! empty( $query->query_vars['s'] ) ) { $enabled = true; diff --git a/includes/classes/Feature/Terms/Terms.php b/includes/classes/Feature/Terms/Terms.php index e5fe3f079d..d9dbc05031 100644 --- a/includes/classes/Feature/Terms/Terms.php +++ b/includes/classes/Feature/Terms/Terms.php @@ -87,7 +87,7 @@ public function integrate_search_queries( $enabled, $query ) { return $enabled; } - if ( isset( $query->query_vars['ep_integrate'] ) && false === $query->query_vars['ep_integrate'] ) { + if ( isset( $query->query_vars['ep_integrate'] ) && ! filter_var( $query->query_vars['ep_integrate'], FILTER_VALIDATE_BOOL ) ) { $enabled = false; } elseif ( ! empty( $query->query_vars['search'] ) ) { $enabled = true; diff --git a/includes/classes/Feature/Users/Users.php b/includes/classes/Feature/Users/Users.php index cfdff5497f..1defe0ab9e 100644 --- a/includes/classes/Feature/Users/Users.php +++ b/includes/classes/Feature/Users/Users.php @@ -87,7 +87,7 @@ public function integrate_search_queries( $enabled, $query ) { return $enabled; } - if ( isset( $query->query_vars['ep_integrate'] ) && false === $query->query_vars['ep_integrate'] ) { + if ( isset( $query->query_vars['ep_integrate'] ) && ! filter_var( $query->query_vars['ep_integrate'], FILTER_VALIDATE_BOOL ) ) { $enabled = false; } elseif ( ! empty( $query->query_vars['search'] ) ) { $enabled = true; diff --git a/includes/classes/Feature/WooCommerce/WooCommerce.php b/includes/classes/Feature/WooCommerce/WooCommerce.php index f8c5e3ee70..d88281d9ec 100644 --- a/includes/classes/Feature/WooCommerce/WooCommerce.php +++ b/includes/classes/Feature/WooCommerce/WooCommerce.php @@ -947,7 +947,7 @@ protected function should_integrate_with_query( $query ) { * @return {bool} New skip value */ if ( apply_filters( 'ep_skip_query_integration', false, $query ) || - ( isset( $query->query_vars['ep_integrate'] ) && false === $query->query_vars['ep_integrate'] ) ) { + ( isset( $query->query_vars['ep_integrate'] ) && ! filter_var( $query->query_vars['ep_integrate'], FILTER_VALIDATE_BOOL ) ) ) { return false; } diff --git a/includes/classes/Indexable.php b/includes/classes/Indexable.php index edfd0dd794..27ab62dd91 100644 --- a/includes/classes/Indexable.php +++ b/includes/classes/Indexable.php @@ -385,7 +385,7 @@ public function elasticpress_enabled( $query ) { */ $enabled = apply_filters( 'ep_elasticpress_enabled', $enabled, $query ); - if ( isset( $query->query_vars['ep_integrate'] ) && false === $query->query_vars['ep_integrate'] ) { + if ( isset( $query->query_vars['ep_integrate'] ) && ! filter_var( $query->query_vars['ep_integrate'], FILTER_VALIDATE_BOOL ) ) { $enabled = false; } diff --git a/tests/php/features/TestComments.php b/tests/php/features/TestComments.php index b33340a1cf..623fcd6079 100644 --- a/tests/php/features/TestComments.php +++ b/tests/php/features/TestComments.php @@ -126,6 +126,18 @@ public function testIntegrateSearchQueries() { $this->assertFalse( $this->get_feature()->integrate_search_queries( true, $query ) ); + $query = new WP_Comment_Query( [ + 'ep_integrate' => 0 + ] ); + + $this->assertFalse( $this->get_feature()->integrate_search_queries( true, $query ) ); + + $query = new WP_Comment_Query( [ + 'ep_integrate' => 'false' + ] ); + + $this->assertFalse( $this->get_feature()->integrate_search_queries( true, $query ) ); + $query = new WP_Comment_Query( [ 'search' => 'blog' ] ); diff --git a/tests/php/indexables/TestPost.php b/tests/php/indexables/TestPost.php index 3085a30279..43ca695fab 100644 --- a/tests/php/indexables/TestPost.php +++ b/tests/php/indexables/TestPost.php @@ -52,6 +52,15 @@ public function setUp() { ElasticPress\Features::factory()->get_registered_feature( 'search' )->search_setup(); } + /** + * Get Search feature + * + * @return ElasticPress\Feature\Search\ + */ + protected function get_feature() { + return ElasticPress\Features::factory()->get_registered_feature( 'search' ); + } + /** * Clean up after each test. Reset our mocks * @@ -6498,4 +6507,36 @@ public function testDeleteAllMetadata() { $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( $query->found_posts, 2 ); } + + /** + * Test integration with Post Queries. + */ + public function testIntegrateSearchQueries() { + $this->assertTrue( $this->get_feature()->integrate_search_queries( true, null ) ); + $this->assertFalse( $this->get_feature()->integrate_search_queries( false, null ) ); + + $query = new \WP_Query( [ + 'ep_integrate' => false + ] ); + + $this->assertFalse( $this->get_feature()->integrate_search_queries( true, $query ) ); + + $query = new \WP_Query( [ + 'ep_integrate' => 0 + ] ); + + $this->assertFalse( $this->get_feature()->integrate_search_queries( true, $query ) ); + + $query = new \WP_Query( [ + 'ep_integrate' => 'false' + ] ); + + $this->assertFalse( $this->get_feature()->integrate_search_queries( true, $query ) ); + + $query = new \WP_Query( [ + 's' => 'post' + ] ); + + $this->assertTrue( $this->get_feature()->integrate_search_queries( false, $query ) ); + } } diff --git a/tests/php/indexables/TestTerm.php b/tests/php/indexables/TestTerm.php index fee2075703..66f1be8872 100644 --- a/tests/php/indexables/TestTerm.php +++ b/tests/php/indexables/TestTerm.php @@ -46,6 +46,15 @@ public function setUp() { ElasticPress\Features::factory()->get_registered_feature( 'terms' )->search_setup(); } + /** + * Get Term feature + * + * @return ElasticPress\Feature\Terms + */ + protected function get_feature() { + return ElasticPress\Features::factory()->get_registered_feature( 'terms' ); + } + /** * Create and index terms for testing * @@ -1539,4 +1548,36 @@ public function testDeleteTerm() { $this->assertTrue( $term_query->elasticsearch_success ); $this->assertEquals( 0, $term_query->found_terms ); } + + /** + * Test integration with Term Queries. + */ + public function testIntegrateSearchQueries() { + $this->assertTrue( $this->get_feature()->integrate_search_queries( true, null ) ); + $this->assertFalse( $this->get_feature()->integrate_search_queries( false, null ) ); + + $query = new \WP_Term_Query( [ + 'ep_integrate' => false + ] ); + + $this->assertFalse( $this->get_feature()->integrate_search_queries( true, $query ) ); + + $query = new \WP_Term_Query( [ + 'ep_integrate' => 0 + ] ); + + $this->assertFalse( $this->get_feature()->integrate_search_queries( true, $query ) ); + + $query = new \WP_Term_Query( [ + 'ep_integrate' => 'false' + ] ); + + $this->assertFalse( $this->get_feature()->integrate_search_queries( true, $query ) ); + + $query = new \WP_Term_Query( [ + 'search' => 'term' + ] ); + + $this->assertTrue( $this->get_feature()->integrate_search_queries( false, $query ) ); + } } diff --git a/tests/php/indexables/TestUser.php b/tests/php/indexables/TestUser.php index 70955a35f1..7cfacfb101 100644 --- a/tests/php/indexables/TestUser.php +++ b/tests/php/indexables/TestUser.php @@ -59,6 +59,15 @@ public function setUp() { ElasticPress\Features::factory()->get_registered_feature( 'users' )->search_setup(); } + /** + * Get User feature + * + * @return ElasticPress\Feature\Users + */ + protected function get_feature() { + return ElasticPress\Features::factory()->get_registered_feature( 'users' ); + } + /** * Create and index users for testing * @@ -1398,4 +1407,36 @@ public function testMultipleUserFieldsQuery() { $this->assertSame( $users[ $i ]->display_name, $ep_users[ $i ]->display_name ); } } + + /** + * Test integration with User Queries. + */ + public function testIntegrateSearchQueries() { + $this->assertTrue( $this->get_feature()->integrate_search_queries( true, null ) ); + $this->assertFalse( $this->get_feature()->integrate_search_queries( false, null ) ); + + $query = new \WP_User_Query( [ + 'ep_integrate' => false + ] ); + + $this->assertFalse( $this->get_feature()->integrate_search_queries( true, $query ) ); + + $query = new \WP_User_Query( [ + 'ep_integrate' => 0 + ] ); + + $this->assertFalse( $this->get_feature()->integrate_search_queries( true, $query ) ); + + $query = new \WP_User_Query( [ + 'ep_integrate' => 'false' + ] ); + + $this->assertFalse( $this->get_feature()->integrate_search_queries( true, $query ) ); + + $query = new \WP_User_Query( [ + 'search' => 'user' + ] ); + + $this->assertTrue( $this->get_feature()->integrate_search_queries( false, $query ) ); + } }