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.0.9 6.2.0-rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / widgets / class-wc-widget-brand-nav.php
woocommerce / includes / widgets Last commit date
class-wc-widget-brand-description.php 1 year ago class-wc-widget-brand-nav.php 7 months ago class-wc-widget-brand-thumbnails.php 1 year ago class-wc-widget-cart.php 3 years ago class-wc-widget-layered-nav-filters.php 2 years ago class-wc-widget-layered-nav.php 3 months ago class-wc-widget-price-filter.php 8 months ago class-wc-widget-product-categories.php 7 months ago class-wc-widget-product-search.php 5 years ago class-wc-widget-product-tag-cloud.php 5 years ago class-wc-widget-products.php 1 year ago class-wc-widget-rating-filter.php 5 years ago class-wc-widget-recent-reviews.php 2 months ago class-wc-widget-recently-viewed.php 1 year ago class-wc-widget-top-rated-products.php 5 years ago
class-wc-widget-brand-nav.php
552 lines
1 <?php
2
3 declare( strict_types = 1);
4
5 /**
6 * Layered Navigation Widget for brands WC 2.6 version
7 *
8 * Important: For internal use only by the Automattic\WooCommerce\Internal\Brands package.
9 *
10 * @package WooCommerce\Widgets
11 * @version 9.4.0
12 * @extends WP_Widget
13 */
14 class WC_Widget_Brand_Nav extends WC_Widget {
15 /**
16 * Constructor
17 *
18 * @return void
19 */
20 public function __construct() {
21
22 /* Widget variable settings. */
23 $this->widget_cssclass = 'woocommerce widget_brand_nav widget_layered_nav';
24 $this->widget_description = __( 'Shows brands in a widget which lets you narrow down the list of products when viewing products.', 'woocommerce' );
25 $this->widget_id = 'woocommerce_brand_nav';
26 $this->widget_name = __( 'WooCommerce Brand Layered Nav', 'woocommerce' );
27
28 add_filter( 'woocommerce_product_subcategories_args', array( $this, 'filter_out_cats' ) );
29
30 /* Create the widget. */
31 parent::__construct();
32 }
33
34 /**
35 * Filter out all categories and not display them
36 *
37 * @param array $cat_args Category arguments.
38 */
39 public function filter_out_cats( $cat_args ) {
40 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
41 if ( ! empty( $_GET['filter_product_brand'] ) ) {
42 return array( 'taxonomy' => '' );
43 }
44
45 return $cat_args;
46 }
47
48 /**
49 * Return the currently viewed taxonomy name.
50 *
51 * @return string
52 */
53 protected function get_current_taxonomy() {
54 return is_tax() ? get_queried_object()->taxonomy : '';
55 }
56
57 /**
58 * Return the currently viewed term ID.
59 *
60 * @return int
61 */
62 protected function get_current_term_id() {
63 return absint( is_tax() ? get_queried_object()->term_id : 0 );
64 }
65
66 /**
67 * Return the currently viewed term slug.
68 *
69 * @return int
70 */
71 protected function get_current_term_slug() {
72 return absint( is_tax() ? get_queried_object()->slug : 0 );
73 }
74
75 /**
76 * Widget function.
77 *
78 * @see WP_Widget
79 *
80 * @param array $args Arguments.
81 * @param array $instance Widget instance.
82 * @return void
83 */
84 public function widget( $args, $instance ) {
85 $attribute_array = array();
86 $attribute_taxonomies = wc_get_attribute_taxonomies();
87
88 if ( ! empty( $attribute_taxonomies ) ) {
89 foreach ( $attribute_taxonomies as $tax ) {
90 $taxonomy_name = wc_attribute_taxonomy_name( $tax->attribute_name );
91 if ( taxonomy_exists( $taxonomy_name ) ) {
92 $attribute_array[] = $taxonomy_name;
93 }
94 }
95 }
96
97 if ( ! is_post_type_archive( 'product' ) && ! is_tax( array_merge( $attribute_array, array( 'product_cat', 'product_tag', 'product_brand' ) ) ) ) {
98 return;
99 }
100
101 $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
102
103 $current_term = $attribute_array && is_tax( $attribute_array ) ? get_queried_object()->term_id : '';
104 $current_tax = $attribute_array && is_tax( $attribute_array ) ? get_queried_object()->taxonomy : '';
105
106 /**
107 * Filter the widget's title.
108 *
109 * @since 9.4.0
110 *
111 * @param string $title Widget title
112 * @param array $instance The settings for the particular instance of the widget.
113 * @param string $woo_widget_idbase The widget's id base.
114 */
115 $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
116 $taxonomy = 'product_brand';
117 $display_type = isset( $instance['display_type'] ) ? $instance['display_type'] : 'list';
118
119 if ( ! taxonomy_exists( $taxonomy ) ) {
120 return;
121 }
122
123 // Get only parent terms. Methods will recursively retrieve children.
124 $terms = get_terms(
125 array(
126 'taxonomy' => $taxonomy,
127 'hide_empty' => true,
128 'parent' => 0,
129 )
130 );
131
132 if ( empty( $terms ) ) {
133 return;
134 }
135
136 ob_start();
137
138 $this->widget_start( $args, $instance );
139
140 if ( 'dropdown' === $display_type ) {
141 $found = $this->layered_nav_dropdown( $terms, $taxonomy );
142 } else {
143 $found = $this->layered_nav_list( $terms, $taxonomy );
144 }
145
146 $this->widget_end( $args );
147
148 // Force found when option is selected - do not force found on taxonomy attributes.
149 if ( ! is_tax() && is_array( $_chosen_attributes ) && array_key_exists( $taxonomy, $_chosen_attributes ) ) {
150 $found = true;
151 }
152
153 if ( ! $found ) {
154 ob_end_clean();
155 } else {
156 echo ob_get_clean(); // phpcs:ignore WordPress.Security.EscapeOutput
157 }
158 }
159
160 /**
161 * Update function.
162 *
163 * @see WP_Widget->update
164 *
165 * @param array $new_instance The new settings for the particular instance of the widget.
166 * @param array $old_instance The old settings for the particular instance of the widget.
167 * @return array
168 */
169 public function update( $new_instance, $old_instance ) {
170 global $woocommerce;
171
172 if ( empty( $new_instance['title'] ) ) {
173 $new_instance['title'] = __( 'Brands', 'woocommerce' );
174 }
175
176 $instance['title'] = wp_strip_all_tags( stripslashes( $new_instance['title'] ) );
177 $instance['display_type'] = stripslashes( $new_instance['display_type'] );
178
179 return $instance;
180 }
181
182 /**
183 * Form function.
184 *
185 * @see WP_Widget->form
186 *
187 * @param array $instance Widget instance.
188 * @return void
189 */
190 public function form( $instance ) {
191 global $woocommerce;
192
193 if ( ! isset( $instance['display_type'] ) ) {
194 $instance['display_type'] = 'list';
195 }
196 ?>
197 <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'woocommerce' ); ?></label>
198 <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; ?>" />
199 </p>
200
201 <p><label for="<?php echo esc_attr( $this->get_field_id( 'display_type' ) ); ?>"><?php esc_html_e( 'Display Type:', 'woocommerce' ); ?></label>
202 <select id="<?php echo esc_attr( $this->get_field_id( 'display_type' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'display_type' ) ); ?>">
203 <option value="list" <?php selected( $instance['display_type'], 'list' ); ?>><?php esc_html_e( 'List', 'woocommerce' ); ?></option>
204 <option value="dropdown" <?php selected( $instance['display_type'], 'dropdown' ); ?>><?php esc_html_e( 'Dropdown', 'woocommerce' ); ?></option>
205 </select></p>
206 <?php
207 }
208
209 /**
210 * Get current page URL for layered nav items.
211 *
212 * @param string $taxonomy Taxonomy.
213 * @return string
214 */
215 protected function get_page_base_url( $taxonomy ) {
216 if ( defined( 'SHOP_IS_ON_FRONT' ) ) {
217 $link = home_url();
218 } elseif ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) {
219 $link = get_post_type_archive_link( 'product' );
220 } elseif ( is_product_category() ) {
221 $link = get_term_link( get_query_var( 'product_cat' ), 'product_cat' );
222 } elseif ( is_product_tag() ) {
223 $link = get_term_link( get_query_var( 'product_tag' ), 'product_tag' );
224 } elseif ( is_tax() ) {
225 // Handle any taxonomy archive, including attributes
226 $queried_object = get_queried_object();
227 if ( is_null( $queried_object ) ) {
228 $link = get_post_type_archive_link( 'product' );
229 } else {
230 $link = get_term_link( $queried_object->term_id, $queried_object->taxonomy );
231 }
232 } else {
233 $link = get_post_type_archive_link( 'product' );
234 }
235 // phpcs:disable WordPress.Security.NonceVerification.Recommended
236
237 // Min/Max.
238 if ( isset( $_GET['min_price'] ) ) {
239 $link = add_query_arg( 'min_price', wc_clean( wp_unslash( $_GET['min_price'] ) ), $link );
240 }
241
242 if ( isset( $_GET['max_price'] ) ) {
243 $link = add_query_arg( 'max_price', wc_clean( wp_unslash( $_GET['max_price'] ) ), $link );
244 }
245
246 // Orderby.
247 if ( isset( $_GET['orderby'] ) ) {
248 $link = add_query_arg( 'orderby', wc_clean( wp_unslash( $_GET['orderby'] ) ), $link );
249 }
250
251 /**
252 * Search Arg.
253 * To support quote characters, first they are decoded from &quot; entities, then URL encoded.
254 */
255 if ( get_search_query() ) {
256 $link = add_query_arg( 's', rawurlencode( htmlspecialchars_decode( get_search_query() ) ), $link );
257 }
258
259 // Post Type Arg.
260 if ( isset( $_GET['post_type'] ) ) {
261 $link = add_query_arg( 'post_type', wc_clean( wp_unslash( $_GET['post_type'] ) ), $link );
262 }
263
264 // Min Rating Arg.
265 if ( isset( $_GET['min_rating'] ) ) {
266 $link = add_query_arg( 'min_rating', wc_clean( wp_unslash( $_GET['min_rating'] ) ), $link );
267 }
268
269 // All current filters.
270 $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
271 if ( $_chosen_attributes ) {
272 foreach ( $_chosen_attributes as $name => $data ) {
273 if ( $name === $taxonomy ) {
274 continue;
275 }
276 $filter_name = sanitize_title( str_replace( 'pa_', '', $name ) );
277 if ( ! empty( $data['terms'] ) ) {
278 $link = add_query_arg( 'filter_' . $filter_name, implode( ',', $data['terms'] ), $link );
279 }
280 if ( 'or' === $data['query_type'] ) {
281 $link = add_query_arg( 'query_type_' . $filter_name, 'or', $link );
282 }
283 }
284 }
285
286 return $link;
287 }
288
289 /**
290 * Gets the currently selected attributes
291 *
292 * @return array
293 */
294 public function get_chosen_attributes() {
295 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
296 if ( ! empty( $_GET['filter_product_brand'] ) ) {
297 $filter_product_brand = wc_clean( wp_unslash( $_GET['filter_product_brand'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
298 return array_map( 'intval', explode( ',', $filter_product_brand ) );
299 }
300
301 return array();
302 }
303
304 /**
305 * Show dropdown layered nav.
306 *
307 * @param array $terms Terms.
308 * @param string $taxonomy Taxonomy.
309 * @param int $depth Depth.
310 * @return bool Will nav display?
311 */
312 protected function layered_nav_dropdown( $terms, $taxonomy, $depth = 0 ) {
313 $found = false;
314
315 if ( $taxonomy !== $this->get_current_taxonomy() ) {
316 $term_counts = $this->get_filtered_term_product_counts( wp_list_pluck( $terms, 'term_id' ), $taxonomy, 'or' );
317 $_chosen_attributes = $this->get_chosen_attributes();
318
319 if ( 0 === $depth ) {
320 echo '<select class="wc-brand-dropdown-layered-nav-' . esc_attr( $taxonomy ) . '">';
321 echo '<option value="">' . esc_html__( 'Any Brand', 'woocommerce' ) . '</option>';
322 }
323
324 foreach ( $terms as $term ) {
325 // If on a term page, skip that term in widget list.
326 if ( $term->term_id === $this->get_current_term_id() ) {
327 continue;
328 }
329
330 // Get count based on current view.
331 $current_values = ! empty( $_chosen_attributes ) ? $_chosen_attributes : array();
332 $option_is_set = in_array( $term->term_id, $current_values, true );
333 $count = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : 0;
334
335 // Only show options with count > 0.
336 if ( 0 < $count ) {
337 $found = true;
338 } elseif ( 0 === $count && ! $option_is_set ) {
339 continue;
340 }
341
342 echo '<option value="' . esc_attr( $term->term_id ) . '" ' . selected( $option_is_set, true, false ) . '>' . esc_html( str_repeat( '&nbsp;', 2 * $depth ) . $term->name ) . '</option>';
343
344 $child_terms = get_terms(
345 array(
346 'taxonomy' => $taxonomy,
347 'hide_empty' => true,
348 'parent' => $term->term_id,
349 )
350 );
351
352 if ( ! empty( $child_terms ) ) {
353 $found |= $this->layered_nav_dropdown( $child_terms, $taxonomy, $depth + 1 );
354 }
355 }
356
357 if ( 0 === $depth ) {
358 $link = $this->get_page_base_url( $taxonomy );
359 echo '</select>';
360
361 $handle = 'wc-brand-widget-dropdown-layered-nav-' . $taxonomy;
362 wp_register_script( $handle, '', array(), WC_VERSION, array( 'in_footer' => true ) );
363 wp_enqueue_script( $handle );
364 $redirect_url = add_query_arg( 'filtering', '1', preg_replace( '%\/page\/[0-9]+%', '', esc_url_raw( $link ) ) );
365
366 wp_add_inline_script(
367 $handle,
368 "
369 (function() {
370 'use strict';
371 const dropdown = document.querySelector( '.wc-brand-dropdown-layered-nav-" . esc_js( $taxonomy ) . "' );
372 if ( dropdown ) {
373 dropdown.addEventListener( 'change', function() {
374 const slug = this.value;
375 location.href = '" . esc_js( $redirect_url ) . '&filter_' . esc_js( $taxonomy ) . "=' + slug;
376 } );
377 }
378 })();
379 "
380 );
381 }
382 }
383
384 return $found;
385 }
386
387 /**
388 * Show list based layered nav.
389 *
390 * @param array $terms Terms.
391 * @param string $taxonomy Taxonomy.
392 * @param int $depth Depth.
393 * @return bool Will nav display?
394 */
395 protected function layered_nav_list( $terms, $taxonomy, $depth = 0 ) {
396 // List display.
397 echo '<ul class="' . ( 0 === $depth ? '' : 'children ' ) . 'wc-brand-list-layered-nav-' . esc_attr( $taxonomy ) . '">';
398
399 $term_counts = $this->get_filtered_term_product_counts( wp_list_pluck( $terms, 'term_id' ), $taxonomy, 'or' );
400 $_chosen_attributes = $this->get_chosen_attributes();
401 $current_values = ! empty( $_chosen_attributes ) ? $_chosen_attributes : array();
402 $found = false;
403
404 $filter_name = 'filter_' . $taxonomy;
405
406 foreach ( $terms as $term ) {
407 $option_is_set = in_array( $term->term_id, $current_values, true );
408 $count = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : 0;
409
410 // skip the term for the current archive.
411 if ( $this->get_current_term_id() === $term->term_id ) {
412 continue;
413 }
414
415 // Only show options with count > 0.
416 if ( 0 < $count ) {
417 $found = true;
418 } elseif ( 0 === $count && ! $option_is_set ) {
419 continue;
420 }
421
422 $current_filter = isset( $_GET[ $filter_name ] ) ? explode( ',', wc_clean( wp_unslash( $_GET[ $filter_name ] ) ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
423 $current_filter = array_map( 'intval', $current_filter );
424
425 if ( ! in_array( $term->term_id, $current_filter, true ) ) {
426 $current_filter[] = $term->term_id;
427 }
428
429 $link = $this->get_page_base_url( $taxonomy );
430
431 // Add current filters to URL.
432 foreach ( $current_filter as $key => $value ) {
433 // Exclude query arg for current term archive term.
434 if ( $value === $this->get_current_term_id() ) {
435 unset( $current_filter[ $key ] );
436 }
437
438 // Exclude self so filter can be unset on click.
439 if ( $option_is_set && $value === $term->term_id ) {
440 unset( $current_filter[ $key ] );
441 }
442 }
443
444 if ( ! empty( $current_filter ) ) {
445 $link = add_query_arg(
446 array(
447 'filtering' => '1',
448 $filter_name => implode( ',', $current_filter ),
449 ),
450 $link
451 );
452 }
453
454 echo '<li class="wc-layered-nav-term ' . ( $option_is_set ? 'chosen' : '' ) . '">';
455
456 echo ( $count > 0 || $option_is_set ) ? '<a href="' . esc_url( apply_filters( 'woocommerce_layered_nav_link', $link ) ) . '">' : '<span>'; // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
457
458 echo esc_html( $term->name );
459
460 echo ( $count > 0 || $option_is_set ) ? '</a> ' : '</span> ';
461
462 echo wp_kses_post( apply_filters( 'woocommerce_layered_nav_count', '<span class="count">(' . absint( $count ) . ')</span>', $count, $term ) );// phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
463
464 $child_terms = get_terms(
465 array(
466 'taxonomy' => $taxonomy,
467 'hide_empty' => true,
468 'parent' => $term->term_id,
469 )
470 );
471
472 if ( ! empty( $child_terms ) ) {
473 $found |= $this->layered_nav_list( $child_terms, $taxonomy, $depth + 1 );
474 }
475
476 echo '</li>';
477 }
478
479 echo '</ul>';
480
481 return $found;
482 }
483
484 /**
485 * Count products within certain terms, taking the main WP query into consideration.
486 *
487 * @param array $term_ids Term IDs.
488 * @param string $taxonomy Taxonomy.
489 * @param string $query_type Query type.
490 * @return array
491 */
492 protected function get_filtered_term_product_counts( $term_ids, $taxonomy, $query_type = 'and' ) {
493 global $wpdb;
494
495 $tax_query = WC_Query::get_main_tax_query();
496 $meta_query = WC_Query::get_main_meta_query();
497
498 if ( 'or' === $query_type ) {
499 foreach ( $tax_query as $key => $query ) {
500 if ( is_array( $query ) && $taxonomy === $query['taxonomy'] ) {
501 unset( $tax_query[ $key ] );
502 }
503 }
504 }
505
506 $meta_query = new WP_Meta_Query( $meta_query );
507 $tax_query = new WP_Tax_Query( $tax_query );
508 $meta_query_sql = $meta_query->get_sql( 'post', $wpdb->posts, 'ID' );
509 $tax_query_sql = $tax_query->get_sql( $wpdb->posts, 'ID' );
510
511 // Generate query.
512 $query = array();
513 $query['select'] = "SELECT COUNT( DISTINCT {$wpdb->posts}.ID ) as term_count, terms.term_id as term_count_id";
514 $query['from'] = "FROM {$wpdb->posts}";
515 $query['join'] = "
516 INNER JOIN {$wpdb->term_relationships} AS term_relationships ON {$wpdb->posts}.ID = term_relationships.object_id
517 INNER JOIN {$wpdb->term_taxonomy} AS term_taxonomy USING( term_taxonomy_id )
518 INNER JOIN {$wpdb->terms} AS terms USING( term_id )
519 " . $tax_query_sql['join'] . $meta_query_sql['join'];
520 $query['where'] = "
521 WHERE {$wpdb->posts}.post_type IN ( 'product' )
522 AND {$wpdb->posts}.post_status = 'publish'
523 " . $tax_query_sql['where'] . $meta_query_sql['where'] . '
524 AND terms.term_id IN (' . implode( ',', array_map( 'absint', $term_ids ) ) . ')
525 ';
526 $query['group_by'] = 'GROUP BY terms.term_id';
527 $query = apply_filters( 'woocommerce_get_filtered_term_product_counts_query', $query ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
528 $query = implode( ' ', $query );
529
530 // We have a query - let's see if cached results of this query already exist.
531 $query_hash = md5( $query );
532
533 $cache = apply_filters( 'woocommerce_layered_nav_count_maybe_cache', true ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
534 if ( true === $cache ) {
535 $cached_counts = (array) get_transient( 'wc_layered_nav_counts_' . sanitize_title( $taxonomy ) );
536 } else {
537 $cached_counts = array();
538 }
539
540 if ( ! isset( $cached_counts[ $query_hash ] ) ) {
541 $results = $wpdb->get_results( $query, ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
542 $counts = array_map( 'absint', wp_list_pluck( $results, 'term_count', 'term_count_id' ) );
543 $cached_counts[ $query_hash ] = $counts;
544 if ( true === $cache ) {
545 set_transient( 'wc_layered_nav_counts_' . sanitize_title( $taxonomy ), $cached_counts, HOUR_IN_SECONDS );
546 }
547 }
548
549 return array_map( 'absint', (array) $cached_counts[ $query_hash ] );
550 }
551 }
552