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

Fix: EP returns nothing when an invalid value is passed to the tax_query field parameter #3030

Merged
merged 8 commits into from
Oct 17, 2022
24 changes: 19 additions & 5 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1067,11 +1067,7 @@ protected function parse_tax_query( $query ) {
$single_tax_query = $tax_queries;
if ( ! empty( $single_tax_query['taxonomy'] ) ) {
$terms = isset( $single_tax_query['terms'] ) ? (array) $single_tax_query['terms'] : array();
$field = ( ! empty( $single_tax_query['field'] ) ) ? $single_tax_query['field'] : 'term_id';

if ( 'name' === $field ) {
$field = 'name.raw';
}
$field = $this->parse_tax_query_field( $single_tax_query['field'] );

if ( 'slug' === $field ) {
$terms = array_map( 'sanitize_title', $terms );
Expand Down Expand Up @@ -2371,4 +2367,22 @@ protected function maybe_set_aggs( $formatted_args, $args, $filters ) {

return $formatted_args;
}

/**
* Parse tax query field value.
*
* @since 4.4.0
* @param string $field Field name
* @return string
*/
protected function parse_tax_query_field( string $field ) : string {

$from_to = [
'name' => 'name.raw',
'slug' => 'slug',
'term_taxonomy_id' => 'term_taxonomy_id',
];

return $from_to[ $field ] ?? 'term_id';
}
}
61 changes: 61 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,67 @@ public function testTaxQueryTermId() {
$this->assertEquals( 2, $query->found_posts );
}

/**
*
* Test a taxonomy query with invalid field value and make sure it falls back to term_id.
*
* @since 4.4.0
* @group post
*/
public function testTaxQueryInvalidWithInvalidField() {
$post = $this->ep_factory->post->create(
array(
'post_content' => 'findme test 1',
'tags_input' => array(
'one',
'two',
),
)
);
$this->ep_factory->post->create( array( 'post_content' => 'findme test 2' ) );
$this->ep_factory->post->create(
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(
'ep_integrate' => false,
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'terms' => array( $tag_id ),
'field' => 'invalid_field',
),
),
);
$query = new \WP_Query( $args );
$this->assertNull( $query->elasticsearch_success );

$expected_result = wp_list_pluck( $query->posts, 'ID' );

$args['ep_integrate'] = true;
$query = new \WP_Query( $args );

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( $expected_result, wp_list_pluck( $query->posts, 'ID' ) );
}

/**
* Test a taxonomy query with term name field
*
Expand Down