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

Exclude category taxonomy from root tax check #2299

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
41 changes: 32 additions & 9 deletions includes/classes/Feature/Facets/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,15 @@ public function widget( $args, $instance ) {

$flat_ordered_terms[] = $top_of_tree;

$to_process = $this->order_by_selected( $top_of_tree->children, $selected_filters['taxonomies'][ $taxonomy ]['terms'] );
$to_process = $this->order_by_selected( $top_of_tree->children, $selected_filters['taxonomies'][ $taxonomy ]['terms'], $order, $orderby );

while ( ! empty( $to_process ) ) {
$term = array_shift( $to_process );

$flat_ordered_terms[] = $term;

if ( ! empty( $term->children ) ) {
$to_process = array_merge( $this->order_by_selected( $term->children, $selected_filters['taxonomies'][ $taxonomy ]['terms'] ), $to_process );
$to_process = array_merge( $this->order_by_selected( $term->children, $selected_filters['taxonomies'][ $taxonomy ]['terms'] ), $to_process, $order, $orderby );
}
}

Expand Down Expand Up @@ -316,23 +316,22 @@ public function widget( $args, $instance ) {
/**
* Order terms putting selected at the top
*
* @param array $terms Array of terms
* @param array $selected_terms Selected terms
* @param array $terms Array of terms
* @param array $selected_terms Selected terms
* @param string $order The order to sort from. Desc or Asc.
* @param string $orderby The orderby to sort items from.
* @since 2.5
* @return array
*/
private function order_by_selected( $terms, $selected_terms ) {
private function order_by_selected( $terms, $selected_terms, $order = false, $orderby = false ) {
$ordered_terms = [];
$terms_by_slug = [];

foreach ( $terms as $term ) {
$terms_by_slug[ $term->slug ] = $term;
}

ksort( $selected_terms );
ksort( $terms_by_slug );

foreach ( $selected_terms as $term_slug => $nothing ) {
foreach ( $selected_terms as $term_slug ) {
if ( ! empty( $terms_by_slug[ $term_slug ] ) ) {
$ordered_terms[ $term_slug ] = $terms_by_slug[ $term_slug ];
}
Expand All @@ -344,6 +343,30 @@ private function order_by_selected( $terms, $selected_terms ) {
}
}

if ( 'count' === $orderby ) {
if ( 'asc' === $order ) {
uasort(
$ordered_terms,
function( $a, $b ) {
return $a->count > $b->count;
}
);
} else {
uasort(
$ordered_terms,
function( $a, $b ) {
return $a->count < $b->count;
}
);
}
} else {
if ( 'asc' === $order ) {
krsort( $ordered_terms );
} else {
ksort( $ordered_terms );
}
}

return array_values( $ordered_terms );
}

Expand Down
3 changes: 2 additions & 1 deletion includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,8 @@ function( $tax_query ) use ( $args ) {
$taxonomies = get_taxonomies( array(), 'objects' );

foreach ( $taxonomies as $tax_slug => $tax ) {
if ( $tax->query_var && ! empty( $args[ $tax->query_var ] ) ) {
// Exclude the category taxonomy from this check if we are performing a Tax Query as category_name will be set by core
if ( $tax->query_var && ! empty( $args[ $tax->query_var ] ) && 'category' !== $tax->name ) {
$args['tax_query'][] = array(
'taxonomy' => $tax_slug,
'terms' => (array) $args[ $tax->query_var ],
Expand Down
39 changes: 39 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use ElasticPress;
use ElasticPress\Indexables as Indexables;
use function ElasticPressTest\Functions\create_and_sync_term;

/**
* Test post indexable class
Expand Down Expand Up @@ -6196,4 +6197,42 @@ public function testPostPrepareDateTerms() {
$this->assertArrayHasKey( 'second', $return_prepare_date_terms );
$this->assertArrayHasKey( 'm', $return_prepare_date_terms );
}

/**
* Test when we perform a Tax Query with Id's for category taxonomy they are used instead of a cat slug.
*
* @return void
* @group post
*/
public function testCatIdTaxQuery() {
$term = Functions\create_and_sync_term( 'test', 'Test category', 'The testing category', 'category' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the PHPUnit is getting an error in this line Error: Call to a member function index() on bool


$args = [
'post_type' => 'post',
'post_status' => 'publish',
'ep_integrate' => true,
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => array( $term ),
'field' => 'term_id',
'operator' => 'in',
)
)
];

$query = new \WP_Query( $args );

$post = new \ElasticPress\Indexable\Post\Post();

$args = $post->format_args(
[
'cat_name' => 'test',
],
$query
);

$this->assertContains( $term, $args['post_filter']['bool']['must'][0]['bool']['must'][0]['terms']['terms.category.term_id'] );
$this->assertNotContains( 'test', $args['post_filter']['bool']['must'][0]['bool']['must'][1]['terms']['terms.category.slug'] );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to update the assertion because ['must'][1]['terms']['terms.category.slug'] doesn't exist and I think it'll always return true.

Maybe we need to check if the array $args['post_filter']['bool']['must'][0]['bool']['must'] has only one item (assertCount) and if this only item $args['post_filter']['bool']['must'][0]['bool']['must'][0]['terms'] has the key terms.category.term_id (assertArrayHasKey).

Makes sense?

}
}