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