Automattic\WooCommerce\Blocks\BlockTypes
ProductFilterTaxonomy::sort_terms_by_criteria
Sort terms by the specified criteria (name, count, or menu_order).
Метод класса: ProductFilterTaxonomy{}
Хуков нет.
Возвращает
array. Sorted terms.
Использование
// private - только в коде основоного (родительского) класса $result = $this->sort_terms_by_criteria( $terms, $orderby, $order, $taxonomy_counts ): array;
- $terms(массив) (обязательный)
- Array of term objects to sort.
- $orderby(строка) (обязательный)
- Sort field (name, count, menu_order).
- $order(строка) (обязательный)
- Sort direction (ASC, DESC).
- $taxonomy_counts(массив) (обязательный)
- Context-aware term counts.
Код ProductFilterTaxonomy::sort_terms_by_criteria() ProductFilterTaxonomy::sort terms by criteria WC 11.3.0
private function sort_terms_by_criteria( array $terms, string $orderby, string $order, array $taxonomy_counts ): array {
$sort_order = 'DESC' === strtoupper( $order ) ? -1 : 1;
usort(
$terms,
function ( $a, $b ) use ( $orderby, $sort_order, $taxonomy_counts ) {
$a = (object) $a;
$b = (object) $b;
switch ( $orderby ) {
case 'count':
$count_a = $taxonomy_counts[ $a->term_id ] ?? 0;
$count_b = $taxonomy_counts[ $b->term_id ] ?? 0;
$comparison = $count_a <=> $count_b;
break;
case 'menu_order':
$order_a = $a->menu_order ?? 0;
$order_b = $b->menu_order ?? 0;
$comparison = $order_a <=> $order_b;
// Secondary sort by name when menu_order is equal.
if ( 0 === $comparison ) {
$comparison = strcasecmp( $a->name, $b->name );
}
break;
case 'name':
default:
$comparison = strcasecmp( $a->name, $b->name );
break;
}//end switch
return $comparison * $sort_order;
}
);
return $terms;
}