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/facets order #2288

Merged
merged 19 commits into from
Aug 23, 2021
Merged
Changes from all 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
42 changes: 32 additions & 10 deletions includes/classes/Feature/Facets/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ public function widget( $args, $instance ) {
$order = isset( $instance['order'] ) ? $instance['order'] : 'count';

$terms = Utils\get_term_tree( $terms, $orderby, $order, true );
$term_tree = Utils\get_term_tree( $terms, 'count', 'desc', false );

$outputted_terms = array();

Expand Down Expand Up @@ -217,15 +216,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'], $order, $orderby ), $to_process );
}
}

Expand Down Expand Up @@ -316,23 +315,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 +342,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 ) {
ksort( $ordered_terms );
} else {
krsort( $ordered_terms );
}
}

return array_values( $ordered_terms );
}

Expand Down