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

Second pass at Fix a rare edge-case bug where invalid values in tax_query terms resulted in a broken query by passing terms array via array_filter to remove false and null #2583

Merged
merged 3 commits into from
Feb 10, 2022
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
2 changes: 1 addition & 1 deletion includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1859,7 +1859,7 @@ protected function parse_tax_query( $query ) {

// Set up our terms object
$terms_obj = array(
'terms.' . $single_tax_query['taxonomy'] . '.' . $field => array_filter( $terms ),
'terms.' . $single_tax_query['taxonomy'] . '.' . $field => array_values( array_filter( $terms ) ),
);

$operator = ( ! empty( $single_tax_query['operator'] ) ) ? strtolower( $single_tax_query['operator'] ) : 'in';
Expand Down
89 changes: 89 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,95 @@ public function testTaxQueryTermName() {
$this->assertEquals( 2, $query->found_posts );
}

/**
* Test a taxonomy query with invalid terms
*
* @since 4.0.0
* @group post
*/
public function testTaxQueryWithInvalidTerms() {
$post = Functions\create_and_sync_post(
array(
'post_content' => 'findme test 1',
'tags_input' => array(
'one',
'two',
),
)
);
Functions\create_and_sync_post( array( 'post_content' => 'findme test 2' ) );
Functions\create_and_sync_post(
array(
'post_content' => 'findme test 3',
'tags_input' => array(
'one',
'three',
),
)
);

$tags = wp_get_post_tags( $post );
$tag_id = 0;

foreach ( $tags as $tag ) {
if ( 'one' === $tag->slug ) {
$tag_id = $tag->term_id;
}
}

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

$args = array(
's' => 'findme',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'terms' => array( $tag_id, null ),
'field' => 'term_id',
),
),
);

$query = new \WP_Query( $args );

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 2, $query->post_count );
$this->assertEquals( 2, $query->found_posts );

$args = array(
's' => 'findme',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'terms' => array( null, $tag_id, false ),
'field' => 'term_id',
),
),
);

$query = new \WP_Query( $args );

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 2, $query->post_count );
$this->assertEquals( 2, $query->found_posts );

$args = array(
's' => 'findme',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'terms' => array( $tag_id, null ),
),
),
);

$query = new \WP_Query( $args );

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 2, $query->post_count );
$this->assertEquals( 2, $query->found_posts );
}

/**
* Test a category_name query
*
Expand Down