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

Evaluate 0 and false string as false booleans as well #2547

Merged
merged 9 commits into from
Jan 25, 2022
2 changes: 1 addition & 1 deletion includes/classes/Feature/Comments/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions includes/classes/Feature/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion includes/classes/Feature/Terms/Terms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion includes/classes/Feature/Users/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion includes/classes/Feature/WooCommerce/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion includes/classes/Indexable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/php/features/TestComments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
] );
Expand Down
41 changes: 41 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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 ) );
}
}
41 changes: 41 additions & 0 deletions tests/php/indexables/TestTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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 ) );
}
}
41 changes: 41 additions & 0 deletions tests/php/indexables/TestUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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 ) );
}
}