Skip to content

Commit

Permalink
Fix: Admin searches in WooCommerce taxonomies return no results
Browse files Browse the repository at this point in the history
Fix Linting issues
  • Loading branch information
burhandodhy committed Jun 4, 2022
1 parent c0aa3e7 commit 5794d0d
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 18 deletions.
2 changes: 2 additions & 0 deletions includes/classes/Indexable.php
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,8 @@ public function build_meta_query( $meta_queries ) {
$compare = '=';
if ( ! empty( $single_meta_query['compare'] ) ) {
$compare = strtolower( $single_meta_query['compare'] );
} elseif ( ! isset( $single_meta_query['value'] ) ) {
$compare = 'exists';
}

$type = null;
Expand Down
25 changes: 13 additions & 12 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1280,23 +1280,24 @@ public function format_args( $args, $wp_query ) {
$meta_queries = [];

/**
* Support meta_key
*
* @since 2.1
* Support `meta_key`, `meta_value`, `meta_value_num`, and `meta_compare` query args
*/
if ( ! empty( $args['meta_key'] ) ) {
if ( ! empty( $args['meta_value'] ) ) {
$meta_value = $args['meta_value'];
} elseif ( ! empty( $args['meta_value_num'] ) ) {
$meta_value = $args['meta_value_num'];
$meta_query_array = [
'key' => $args['meta_key'],
];

if ( isset( $args['meta_value'] ) && '' !== $args['meta_value'] ) {
$meta_query_array['value'] = $args['meta_value'];
} elseif ( isset( $args['meta_value_num'] ) && '' !== $args['meta_value_num'] ) {
$meta_query_array['value'] = $args['meta_value_num'];
}

if ( ! empty( $meta_value ) ) {
$meta_queries[] = array(
'key' => $args['meta_key'],
'value' => $meta_value,
);
if ( isset( $args['meta_compare'] ) ) {
$meta_query_array['compare'] = $args['meta_compare'];
}

$meta_queries[] = $meta_query_array;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/classes/Indexable/Term/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ public function format_args( $query_vars ) {
'key' => $query_vars['meta_key'],
];

if ( isset( $query_vars['meta_value'] ) ) {
if ( isset( $query_vars['meta_value'] ) && '' !== $query_vars['meta_value'] ) {
$meta_query_array['value'] = $query_vars['meta_value'];
}

Expand Down
64 changes: 59 additions & 5 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -5636,7 +5636,7 @@ public function testFormatArgsRootLevelTaxonomies() {
);

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

$query = new \WP_Query(
[
'ep_integrate' => true,
Expand All @@ -5649,7 +5649,7 @@ public function testFormatArgsRootLevelTaxonomies() {
$this->assertEqualsCanonicalizing( [ $post1 ], $query->posts );
$this->assertEquals( 1, $query->post_count );
$this->assertEquals( 1, $query->found_posts );

$query = new \WP_Query(
[
'ep_integrate' => true,
Expand All @@ -5662,7 +5662,7 @@ public function testFormatArgsRootLevelTaxonomies() {
$this->assertEqualsCanonicalizing( [ $post1, $post2 ], $query->posts );
$this->assertEquals( 2, $query->post_count );
$this->assertEquals( 2, $query->found_posts );

$query = new \WP_Query(
[
'ep_integrate' => true,
Expand All @@ -5675,7 +5675,7 @@ public function testFormatArgsRootLevelTaxonomies() {
$this->assertEqualsCanonicalizing( [ $post2 ], $query->posts );
$this->assertEquals( 1, $query->post_count );
$this->assertEquals( 1, $query->found_posts );

$query = new \WP_Query(
[
'ep_integrate' => true,
Expand All @@ -5687,7 +5687,7 @@ public function testFormatArgsRootLevelTaxonomies() {
$this->assertEqualsCanonicalizing( [ $post1, $post2, $post4 ], $query->posts );
$this->assertEquals( 3, $query->post_count );
$this->assertEquals( 3, $query->found_posts );

$query = new \WP_Query(
[
'ep_integrate' => true,
Expand Down Expand Up @@ -6813,4 +6813,58 @@ public function testPostEditedTerm() {
$this->assertEquals( 'different-tag-slug', $document['terms']['post_tag'][0]['slug'] );
$this->assertEquals( 'Different Tag Name', $document['terms']['post_tag'][0]['name'] );
}

/**
* Tests post without meta value.
*
* @return void
*/
public function testMetaWithoutValue() {

Functions\create_and_sync_post( array(), array( 'test_key' => '' ) );
Functions\create_and_sync_post( array(), array( 'test_key' => '' ) );
Functions\create_and_sync_post();

$expected_result = '2';

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

// Make sure WordPress returns only 2 posts.
$args = array(
'meta_query' => array(
array(
'key' => 'test_key',
),
),
);
$query = new \WP_Query( $args );

$this->assertEquals( $expected_result, $query->post_count );
$this->assertNull( $query->elasticsearch_success );

// Make sure ElasticPress returns only 2 posts when meta query is set
$args = array(
'ep_integrate' => true,
'meta_query' => array(
array(
'key' => 'test_key',
),
),
);
$query = new \WP_Query( $args );

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

// Make sure ElasticPress returns only 2 posts when meta key is set
$args = array(
'ep_integrate' => true,
'meta_key' => 'test_key',
);
$query = new \WP_Query( $args );

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

}
}
49 changes: 49 additions & 0 deletions tests/php/indexables/TestTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -1580,4 +1580,53 @@ public function testIntegrateSearchQueries() {

$this->assertTrue( $this->get_feature()->integrate_search_queries( false, $query ) );
}

/**
* Tests terms without meta value.
*
* @return void
*/
public function testMetaWithoutValue() {

$term_id = Functions\create_and_sync_term( 'term-name', 'term name', '', 'category' );
update_term_meta( $term_id, 'key', 'value' );
ElasticPress\Indexables::factory()->get( 'term' )->index( $term_id, true );

$term_id = Functions\create_and_sync_term( 'term-name-2', 'term name 2', '', 'category' );
update_term_meta( $term_id, 'key', 'value' );
ElasticPress\Indexables::factory()->get( 'term' )->index( $term_id, true );

Functions\create_and_sync_term( 'term-name-3', 'term name 3', '', 'category' );

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

// Make sure WP_Term_Query returns only taxonomies for whom meta exists.
$args = array(
'taxonomy' => 'category',
'meta_key' => 'key',
'ep_integrate' => true,
'hide_empty' => false,

);
$query = new \WP_Term_Query( $args );
$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 2, count( $query->terms ) );

// Make sure get_terms returns only taxonomies for whom meta exists.
$args = array(
'taxonomy' => 'category',
'hide_empty' => false,
'ep_integrate' => true,
'meta_query' => array(
array(
'key' => 'key',
)
)
);
$terms = get_terms( $args );
$this->assertTrue( $terms[0]->elasticsearch );
$this->assertEquals( 2, count( $terms ) );

}

}

0 comments on commit 5794d0d

Please sign in to comment.