Skip to content

Commit b008960

Browse files
committed
Merge pull request #32 from alleyinteractive/tax-query-updates
Assorted updates for taxonomy queries
2 parents 917f868 + f028ac2 commit b008960

File tree

5 files changed

+1621
-196
lines changed

5 files changed

+1621
-196
lines changed

adapters/travis.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ function es_wp_query_index_test_data() {
132132
"properties": {
133133
"name": { "type": "string", "index": "not_analyzed" },
134134
"term_id": { "type": "long" },
135+
"term_taxonomy_id": { "type": "long" },
135136
"slug": { "type": "string", "index": "not_analyzed" }
136137
}
137138
}
@@ -297,10 +298,21 @@ function travis_es_verify_response_code( $response ) {
297298
if ( is_wp_error( $response ) ) {
298299
printf( "Message: %s\n", $response->get_error_message() );
299300
}
301+
printf( "Backtrace: %s\n", travis_es_debug_backtrace_summary() );
300302
exit( 1 );
301303
}
302304
}
303305

306+
function travis_es_debug_backtrace_summary() {
307+
$backtrace = wp_debug_backtrace_summary( null, 0, false );
308+
foreach ( $backtrace as $k => $call ) {
309+
if ( preg_match( '/PHPUnit_(TextUI_(Command|TestRunner)|Framework_(TestSuite|TestCase|TestResult))|ReflectionMethod|travis_es_(verify_response_code|debug_backtrace_summary)/', $call ) ) {
310+
unset( $backtrace[ $k ] );
311+
}
312+
}
313+
return join( ', ', array_reverse( $backtrace ) );
314+
}
315+
304316
/**
305317
* Taken from SearchPress
306318
*/
@@ -439,9 +451,10 @@ public function get_terms( $post ) {
439451
$terms = array();
440452
foreach ( (array) $object_terms as $term ) {
441453
$terms[ $term->taxonomy ][] = array(
442-
'term_id' => $term->term_id,
443-
'slug' => $term->slug,
444-
'name' => $term->name,
454+
'term_id' => $term->term_id,
455+
'term_taxonomy_id' => $term->term_taxonomy_id,
456+
'slug' => $term->slug,
457+
'name' => $term->name,
445458
);
446459
}
447460

class-es-wp-query-wrapper.php

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ public function get_posts() {
524524
// Taxonomies
525525
if ( ! $this->is_singular ) {
526526
$this->parse_tax_query( $q );
527-
$this->tax_query = new ES_WP_Tax_Query( $this->tax_query );
527+
$this->tax_query = ES_WP_Tax_Query::get_from_tax_query( $this->tax_query );
528528

529529
$tax_filter = $this->tax_query->get_dsl( $this );
530530
if ( false === $tax_filter ) {
@@ -540,7 +540,7 @@ public function get_posts() {
540540
if ( empty($post_type) ) {
541541
// Do a fully inclusive search for currently registered post types of queried taxonomies
542542
$post_type = array();
543-
$taxonomies = wp_list_pluck( $this->tax_query->queries, 'taxonomy' );
543+
$taxonomies = array_keys( $this->tax_query->queried_terms );
544544
foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
545545
$object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
546546
if ( array_intersect( $taxonomies, $object_taxonomies ) )
@@ -551,59 +551,62 @@ public function get_posts() {
551551
elseif ( count( $post_type ) == 1 )
552552
$post_type = $post_type[0];
553553

554-
// @todo: no good way to do this in ES; workarounds?
555554
$post_status_join = true;
556555
} elseif ( in_array('attachment', (array) $post_type) ) {
557-
// @todo: no good way to do this in ES; workarounds?
558556
$post_status_join = true;
559557
}
560558
}
561559

562-
// Back-compat
563-
if ( ! empty( $this->tax_query->queries ) ) {
564-
$tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
565-
if ( !empty( $tax_query_in_and ) ) {
566-
if ( !isset( $q['taxonomy'] ) ) {
567-
foreach ( $tax_query_in_and as $a_tax_query ) {
568-
if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
569-
$q['taxonomy'] = $a_tax_query['taxonomy'];
570-
if ( 'slug' == $a_tax_query['field'] )
571-
$q['term'] = $a_tax_query['terms'][0];
572-
else
573-
$q['term_id'] = $a_tax_query['terms'][0];
560+
/*
561+
* Ensure that 'taxonomy', 'term', 'term_id', 'cat', and
562+
* 'category_name' vars are set for backward compatibility.
563+
*/
564+
if ( ! empty( $this->tax_query->queried_terms ) ) {
574565

575-
break;
576-
}
566+
/*
567+
* Set 'taxonomy', 'term', and 'term_id' to the
568+
* first taxonomy other than 'post_tag' or 'category'.
569+
*/
570+
if ( ! isset( $q['taxonomy'] ) ) {
571+
foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) {
572+
if ( empty( $queried_items['terms'][0] ) ) {
573+
continue;
577574
}
578-
}
579575

580-
$cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
581-
if ( ! empty( $cat_query ) ) {
582-
$cat_query = reset( $cat_query );
576+
if ( ! in_array( $queried_taxonomy, array( 'category', 'post_tag' ) ) ) {
577+
$q['taxonomy'] = $queried_taxonomy;
583578

584-
if ( ! empty( $cat_query['terms'][0] ) ) {
585-
$the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
586-
if ( $the_cat ) {
587-
$this->set( 'cat', $the_cat->term_id );
588-
$this->set( 'category_name', $the_cat->slug );
579+
if ( 'slug' === $queried_items['field'] ) {
580+
$q['term'] = $queried_items['terms'][0];
581+
} else {
582+
$q['term_id'] = $queried_items['terms'][0];
589583
}
590-
unset( $the_cat );
591584
}
592585
}
593-
unset( $cat_query );
586+
}
594587

595-
$tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
596-
if ( ! empty( $tag_query ) ) {
597-
$tag_query = reset( $tag_query );
588+
// 'cat', 'category_name', 'tag_id'
589+
foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) {
590+
if ( empty( $queried_items['terms'][0] ) ) {
591+
continue;
592+
}
593+
594+
if ( 'category' === $queried_taxonomy ) {
595+
$the_cat = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'category' );
596+
if ( $the_cat ) {
597+
$this->set( 'cat', $the_cat->term_id );
598+
$this->set( 'category_name', $the_cat->slug );
599+
}
600+
unset( $the_cat );
601+
}
598602

599-
if ( ! empty( $tag_query['terms'][0] ) ) {
600-
$the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
601-
if ( $the_tag )
602-
$this->set( 'tag_id', $the_tag->term_id );
603-
unset( $the_tag );
603+
if ( 'post_tag' === $queried_taxonomy ) {
604+
$the_tag = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'post_tag' );
605+
if ( $the_tag ) {
606+
$this->set( 'tag_id', $the_tag->term_id );
604607
}
608+
unset( $the_tag );
605609
}
606-
unset( $tag_query );
607610
}
608611
}
609612

@@ -1365,4 +1368,12 @@ public static function dsl_match( $field, $value, $args = array() ) {
13651368
public static function dsl_multi_match( $fields, $query, $args = array() ) {
13661369
return array( 'multi_match' => array_merge( array( 'query' => $query, 'fields' => (array) $fields ), $args ) );
13671370
}
1368-
}
1371+
1372+
public static function dsl_all_terms( $field, $values ) {
1373+
$queries = array();
1374+
foreach ( $values as $value ) {
1375+
$queries[] = array( 'term' => array( $field => $value ) );
1376+
}
1377+
return array( 'bool' => array( 'must' => $queries ) );
1378+
}
1379+
}

0 commit comments

Comments
 (0)