Skip to content

Commit

Permalink
Merge pull request #32 from alleyinteractive/tax-query-updates
Browse files Browse the repository at this point in the history
Assorted updates for taxonomy queries
  • Loading branch information
mboynes committed Mar 8, 2016
2 parents 917f868 + f028ac2 commit b008960
Show file tree
Hide file tree
Showing 5 changed files with 1,621 additions and 196 deletions.
19 changes: 16 additions & 3 deletions adapters/travis.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ function es_wp_query_index_test_data() {
"properties": {
"name": { "type": "string", "index": "not_analyzed" },
"term_id": { "type": "long" },
"term_taxonomy_id": { "type": "long" },
"slug": { "type": "string", "index": "not_analyzed" }
}
}
Expand Down Expand Up @@ -297,10 +298,21 @@ function travis_es_verify_response_code( $response ) {
if ( is_wp_error( $response ) ) {
printf( "Message: %s\n", $response->get_error_message() );
}
printf( "Backtrace: %s\n", travis_es_debug_backtrace_summary() );
exit( 1 );
}
}

function travis_es_debug_backtrace_summary() {
$backtrace = wp_debug_backtrace_summary( null, 0, false );
foreach ( $backtrace as $k => $call ) {
if ( preg_match( '/PHPUnit_(TextUI_(Command|TestRunner)|Framework_(TestSuite|TestCase|TestResult))|ReflectionMethod|travis_es_(verify_response_code|debug_backtrace_summary)/', $call ) ) {
unset( $backtrace[ $k ] );
}
}
return join( ', ', array_reverse( $backtrace ) );
}

/**
* Taken from SearchPress
*/
Expand Down Expand Up @@ -439,9 +451,10 @@ public function get_terms( $post ) {
$terms = array();
foreach ( (array) $object_terms as $term ) {
$terms[ $term->taxonomy ][] = array(
'term_id' => $term->term_id,
'slug' => $term->slug,
'name' => $term->name,
'term_id' => $term->term_id,
'term_taxonomy_id' => $term->term_taxonomy_id,
'slug' => $term->slug,
'name' => $term->name,
);
}

Expand Down
89 changes: 50 additions & 39 deletions class-es-wp-query-wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ public function get_posts() {
// Taxonomies
if ( ! $this->is_singular ) {
$this->parse_tax_query( $q );
$this->tax_query = new ES_WP_Tax_Query( $this->tax_query );
$this->tax_query = ES_WP_Tax_Query::get_from_tax_query( $this->tax_query );

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

// @todo: no good way to do this in ES; workarounds?
$post_status_join = true;
} elseif ( in_array('attachment', (array) $post_type) ) {
// @todo: no good way to do this in ES; workarounds?
$post_status_join = true;
}
}

// Back-compat
if ( ! empty( $this->tax_query->queries ) ) {
$tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
if ( !empty( $tax_query_in_and ) ) {
if ( !isset( $q['taxonomy'] ) ) {
foreach ( $tax_query_in_and as $a_tax_query ) {
if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
$q['taxonomy'] = $a_tax_query['taxonomy'];
if ( 'slug' == $a_tax_query['field'] )
$q['term'] = $a_tax_query['terms'][0];
else
$q['term_id'] = $a_tax_query['terms'][0];
/*
* Ensure that 'taxonomy', 'term', 'term_id', 'cat', and
* 'category_name' vars are set for backward compatibility.
*/
if ( ! empty( $this->tax_query->queried_terms ) ) {

break;
}
/*
* Set 'taxonomy', 'term', and 'term_id' to the
* first taxonomy other than 'post_tag' or 'category'.
*/
if ( ! isset( $q['taxonomy'] ) ) {
foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) {
if ( empty( $queried_items['terms'][0] ) ) {
continue;
}
}

$cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
if ( ! empty( $cat_query ) ) {
$cat_query = reset( $cat_query );
if ( ! in_array( $queried_taxonomy, array( 'category', 'post_tag' ) ) ) {
$q['taxonomy'] = $queried_taxonomy;

if ( ! empty( $cat_query['terms'][0] ) ) {
$the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
if ( $the_cat ) {
$this->set( 'cat', $the_cat->term_id );
$this->set( 'category_name', $the_cat->slug );
if ( 'slug' === $queried_items['field'] ) {
$q['term'] = $queried_items['terms'][0];
} else {
$q['term_id'] = $queried_items['terms'][0];
}
unset( $the_cat );
}
}
unset( $cat_query );
}

$tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
if ( ! empty( $tag_query ) ) {
$tag_query = reset( $tag_query );
// 'cat', 'category_name', 'tag_id'
foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) {
if ( empty( $queried_items['terms'][0] ) ) {
continue;
}

if ( 'category' === $queried_taxonomy ) {
$the_cat = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'category' );
if ( $the_cat ) {
$this->set( 'cat', $the_cat->term_id );
$this->set( 'category_name', $the_cat->slug );
}
unset( $the_cat );
}

if ( ! empty( $tag_query['terms'][0] ) ) {
$the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
if ( $the_tag )
$this->set( 'tag_id', $the_tag->term_id );
unset( $the_tag );
if ( 'post_tag' === $queried_taxonomy ) {
$the_tag = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'post_tag' );
if ( $the_tag ) {
$this->set( 'tag_id', $the_tag->term_id );
}
unset( $the_tag );
}
unset( $tag_query );
}
}

Expand Down Expand Up @@ -1365,4 +1368,12 @@ public static function dsl_match( $field, $value, $args = array() ) {
public static function dsl_multi_match( $fields, $query, $args = array() ) {
return array( 'multi_match' => array_merge( array( 'query' => $query, 'fields' => (array) $fields ), $args ) );
}
}

public static function dsl_all_terms( $field, $values ) {
$queries = array();
foreach ( $values as $value ) {
$queries[] = array( 'term' => array( $field => $value ) );
}
return array( 'bool' => array( 'must' => $queries ) );
}
}
Loading

0 comments on commit b008960

Please sign in to comment.