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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-layered-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 7 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-layered-nav.php
474 lines
1 <?php
2 /**
3 * Layered nav widget
4 *
5 * @package WooCommerce\Widgets
6 * @version 2.6.0
7 */
8
9 use Automattic\WooCommerce\Internal\ProductAttributesLookup\Filterer;
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Widget layered nav class.
15 */
16 class WC_Widget_Layered_Nav extends WC_Widget {
17
18 /**
19 * Constructor.
20 */
21 public function __construct() {
22 $this->widget_cssclass = 'woocommerce widget_layered_nav woocommerce-widget-layered-nav';
23 $this->widget_description = __( 'Display a list of attributes to filter products in your store.', 'woocommerce' );
24 $this->widget_id = 'woocommerce_layered_nav';
25 $this->widget_name = __( 'Filter Products by Attribute', 'woocommerce' );
26 parent::__construct();
27 }
28
29 /**
30 * Updates a particular instance of a widget.
31 *
32 * @see WP_Widget->update
33 *
34 * @param array $new_instance New Instance.
35 * @param array $old_instance Old Instance.
36 *
37 * @return array
38 */
39 public function update( $new_instance, $old_instance ) {
40 $this->init_settings();
41 return parent::update( $new_instance, $old_instance );
42 }
43
44 /**
45 * Outputs the settings update form.
46 *
47 * @see WP_Widget->form
48 *
49 * @param array $instance Instance.
50 */
51 public function form( $instance ) {
52 $this->init_settings();
53 parent::form( $instance );
54 }
55
56 /**
57 * Init settings after post types are registered.
58 */
59 public function init_settings() {
60 $attribute_array = array();
61 $std_attribute = '';
62 $attribute_taxonomies = wc_get_attribute_taxonomies();
63
64 if ( ! empty( $attribute_taxonomies ) ) {
65 foreach ( $attribute_taxonomies as $tax ) {
66 if ( taxonomy_exists( wc_attribute_taxonomy_name( $tax->attribute_name ) ) ) {
67 $attribute_array[ $tax->attribute_name ] = $tax->attribute_name;
68 }
69 }
70 $std_attribute = current( $attribute_array );
71 }
72
73 $this->settings = array(
74 'title' => array(
75 'type' => 'text',
76 'std' => __( 'Filter by', 'woocommerce' ),
77 'label' => __( 'Title', 'woocommerce' ),
78 ),
79 'attribute' => array(
80 'type' => 'select',
81 'std' => $std_attribute,
82 'label' => __( 'Attribute', 'woocommerce' ),
83 'options' => $attribute_array,
84 ),
85 'display_type' => array(
86 'type' => 'select',
87 'std' => 'list',
88 'label' => __( 'Display type', 'woocommerce' ),
89 'options' => array(
90 'list' => __( 'List', 'woocommerce' ),
91 'dropdown' => __( 'Dropdown', 'woocommerce' ),
92 ),
93 ),
94 'query_type' => array(
95 'type' => 'select',
96 'std' => 'and',
97 'label' => __( 'Query type', 'woocommerce' ),
98 'options' => array(
99 'and' => __( 'AND', 'woocommerce' ),
100 'or' => __( 'OR', 'woocommerce' ),
101 ),
102 ),
103 );
104 }
105
106 /**
107 * Get this widgets taxonomy.
108 *
109 * @param array $instance Array of instance options.
110 * @return string
111 */
112 protected function get_instance_taxonomy( $instance ) {
113 if ( isset( $instance['attribute'] ) ) {
114 return wc_attribute_taxonomy_name( $instance['attribute'] );
115 }
116
117 $attribute_taxonomies = wc_get_attribute_taxonomies();
118
119 if ( ! empty( $attribute_taxonomies ) ) {
120 foreach ( $attribute_taxonomies as $tax ) {
121 if ( taxonomy_exists( wc_attribute_taxonomy_name( $tax->attribute_name ) ) ) {
122 return wc_attribute_taxonomy_name( $tax->attribute_name );
123 }
124 }
125 }
126
127 return '';
128 }
129
130 /**
131 * Get this widgets query type.
132 *
133 * @param array $instance Array of instance options.
134 * @return string
135 */
136 protected function get_instance_query_type( $instance ) {
137 return isset( $instance['query_type'] ) ? $instance['query_type'] : 'and';
138 }
139
140 /**
141 * Get this widgets display type.
142 *
143 * @param array $instance Array of instance options.
144 * @return string
145 */
146 protected function get_instance_display_type( $instance ) {
147 return isset( $instance['display_type'] ) ? $instance['display_type'] : 'list';
148 }
149
150 /**
151 * Output widget.
152 *
153 * @see WP_Widget
154 *
155 * @param array $args Arguments.
156 * @param array $instance Instance.
157 */
158 public function widget( $args, $instance ) {
159 if ( ! is_shop() && ! is_product_taxonomy() ) {
160 return;
161 }
162
163 $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
164 $taxonomy = $this->get_instance_taxonomy( $instance );
165 $query_type = $this->get_instance_query_type( $instance );
166 $display_type = $this->get_instance_display_type( $instance );
167
168 if ( ! taxonomy_exists( $taxonomy ) ) {
169 return;
170 }
171
172 $terms = get_terms( $taxonomy, array( 'hide_empty' => '1' ) );
173
174 if ( 0 === count( $terms ) ) {
175 return;
176 }
177
178 ob_start();
179
180 $this->widget_start( $args, $instance );
181
182 if ( 'dropdown' === $display_type ) {
183 wp_enqueue_script( 'selectWoo' );
184 wp_enqueue_style( 'select2' );
185 $found = $this->layered_nav_dropdown( $terms, $taxonomy, $query_type );
186 } else {
187 $found = $this->layered_nav_list( $terms, $taxonomy, $query_type );
188 }
189
190 $this->widget_end( $args );
191
192 // Force found when option is selected - do not force found on taxonomy attributes.
193 if ( ! is_tax() && is_array( $_chosen_attributes ) && array_key_exists( $taxonomy, $_chosen_attributes ) ) {
194 $found = true;
195 }
196
197 if ( ! $found ) {
198 ob_end_clean();
199 } else {
200 echo ob_get_clean(); // @codingStandardsIgnoreLine
201 }
202 }
203
204 /**
205 * Return the currently viewed taxonomy name.
206 *
207 * @return string
208 */
209 protected function get_current_taxonomy() {
210 return is_tax() ? get_queried_object()->taxonomy : '';
211 }
212
213 /**
214 * Return the currently viewed term ID.
215 *
216 * @return int
217 */
218 protected function get_current_term_id() {
219 return absint( is_tax() ? get_queried_object()->term_id : 0 );
220 }
221
222 /**
223 * Return the currently viewed term slug.
224 *
225 * @return int
226 */
227 protected function get_current_term_slug() {
228 return absint( is_tax() ? get_queried_object()->slug : 0 );
229 }
230
231 /**
232 * Show dropdown layered nav.
233 *
234 * @param array $terms Terms.
235 * @param string $taxonomy Taxonomy.
236 * @param string $query_type Query Type.
237 * @return bool Will nav display?
238 */
239 protected function layered_nav_dropdown( $terms, $taxonomy, $query_type ) {
240 global $wp;
241 $found = false;
242
243 if ( $taxonomy !== $this->get_current_taxonomy() ) {
244 $term_counts = $this->get_filtered_term_product_counts( wp_list_pluck( $terms, 'term_id' ), $taxonomy, $query_type );
245 $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
246 $taxonomy_filter_name = wc_attribute_taxonomy_slug( $taxonomy );
247 $taxonomy_label = wc_attribute_label( $taxonomy );
248
249 /* translators: %s: taxonomy name */
250 $any_label = apply_filters( 'woocommerce_layered_nav_any_label', sprintf( __( 'Any %s', 'woocommerce' ), $taxonomy_label ), $taxonomy_label, $taxonomy );
251 $multiple = 'or' === $query_type;
252 $current_values = isset( $_chosen_attributes[ $taxonomy ]['terms'] ) ? $_chosen_attributes[ $taxonomy ]['terms'] : array();
253
254 if ( '' === get_option( 'permalink_structure' ) ) {
255 $form_action = remove_query_arg( array( 'page', 'paged' ), add_query_arg( $wp->query_string, '', home_url( $wp->request ) ) );
256 } else {
257 $form_action = preg_replace( '%\/page/[0-9]+%', '', home_url( user_trailingslashit( $wp->request ) ) );
258 }
259
260 echo '<form method="get" action="' . esc_url( $form_action ) . '" class="woocommerce-widget-layered-nav-dropdown">';
261 echo '<select class="woocommerce-widget-layered-nav-dropdown dropdown_layered_nav_' . esc_attr( $taxonomy_filter_name ) . '"' . ( $multiple ? 'multiple="multiple"' : '' ) . '>';
262 echo '<option value="">' . esc_html( $any_label ) . '</option>';
263
264 foreach ( $terms as $term ) {
265
266 // If on a term page, skip that term in widget list.
267 if ( $term->term_id === $this->get_current_term_id() ) {
268 continue;
269 }
270
271 // Get count based on current view.
272 $option_is_set = in_array( $term->slug, $current_values, true );
273 $count = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : 0;
274
275 // Only show options with count > 0.
276 if ( 0 < $count ) {
277 $found = true;
278 } elseif ( 0 === $count && ! $option_is_set ) {
279 continue;
280 }
281
282 echo '<option value="' . esc_attr( urldecode( $term->slug ) ) . '" ' . selected( $option_is_set, true, false ) . '>' . esc_html( $term->name ) . '</option>';
283 }
284
285 echo '</select>';
286
287 if ( $multiple ) {
288 echo '<button class="woocommerce-widget-layered-nav-dropdown__submit" type="submit" value="' . esc_attr__( 'Apply', 'woocommerce' ) . '">' . esc_html__( 'Apply', 'woocommerce' ) . '</button>';
289 }
290
291 if ( 'or' === $query_type ) {
292 echo '<input type="hidden" name="query_type_' . esc_attr( $taxonomy_filter_name ) . '" value="or" />';
293 }
294
295 echo '<input type="hidden" name="filter_' . esc_attr( $taxonomy_filter_name ) . '" value="' . esc_attr( implode( ',', $current_values ) ) . '" />';
296 echo wc_query_string_form_fields( null, array( 'filter_' . $taxonomy_filter_name, 'query_type_' . $taxonomy_filter_name ), '', true ); // @codingStandardsIgnoreLine
297 echo '</form>';
298
299 $handle = 'wc-widget-dropdown-layered-nav-' . $taxonomy_filter_name;
300 wp_register_script( $handle, '', array( 'jquery', 'selectWoo' ), WC_VERSION, array( 'in_footer' => true ) );
301 wp_enqueue_script( $handle );
302 wp_add_inline_script(
303 $handle,
304 "
305 // Update value on change.
306 jQuery( '.dropdown_layered_nav_" . esc_js( $taxonomy_filter_name ) . "' ).on( 'change', function() {
307 var slug = jQuery( this ).val();
308 jQuery( ':input[name=\"filter_" . esc_js( $taxonomy_filter_name ) . "\"]' ).val( slug );
309
310 // Submit form on change if standard dropdown.
311 if ( ! jQuery( this ).attr( 'multiple' ) ) {
312 jQuery( this ).closest( 'form' ).trigger( 'submit' );
313 }
314 });
315
316 // Use Select2 enhancement if possible
317 if ( jQuery().selectWoo ) {
318 var wc_layered_nav_select = function() {
319 jQuery( '.dropdown_layered_nav_" . esc_js( $taxonomy_filter_name ) . "' ).selectWoo( {
320 placeholder: decodeURIComponent('" . rawurlencode( (string) wp_specialchars_decode( $any_label ) ) . "'),
321 minimumResultsForSearch: 5,
322 width: '100%',
323 allowClear: " . ( $multiple ? 'false' : 'true' ) . ",
324 language: {
325 noResults: function() {
326 return '" . esc_js( _x( 'No matches found', 'enhanced select', 'woocommerce' ) ) . "';
327 }
328 }
329 } );
330 };
331 wc_layered_nav_select();
332 }
333 "
334 );
335 }
336
337 return $found;
338 }
339
340 /**
341 * Count products within certain terms, taking the main WP query into consideration.
342 *
343 * This query allows counts to be generated based on the viewed products, not all products.
344 *
345 * @param array $term_ids Term IDs.
346 * @param string $taxonomy Taxonomy.
347 * @param string $query_type Query Type.
348 * @return array
349 */
350 protected function get_filtered_term_product_counts( $term_ids, $taxonomy, $query_type ) {
351 return wc_get_container()->get( Filterer::class )->get_filtered_term_product_counts( $term_ids, $taxonomy, $query_type );
352 }
353
354 /**
355 * Wrapper for WC_Query::get_main_tax_query() to ease unit testing.
356 *
357 * @since 4.4.0
358 * @return array
359 */
360 protected function get_main_tax_query() {
361 return WC_Query::get_main_tax_query();
362 }
363
364 /**
365 * Wrapper for WC_Query::get_main_search_query_sql() to ease unit testing.
366 *
367 * @since 4.4.0
368 * @return string
369 */
370 protected function get_main_search_query_sql() {
371 return WC_Query::get_main_search_query_sql();
372 }
373
374 /**
375 * Wrapper for WC_Query::get_main_search_queryget_main_meta_query to ease unit testing.
376 *
377 * @since 4.4.0
378 * @return array
379 */
380 protected function get_main_meta_query() {
381 return WC_Query::get_main_meta_query();
382 }
383
384 /**
385 * Show list based layered nav.
386 *
387 * @param array $terms Terms.
388 * @param string $taxonomy Taxonomy.
389 * @param string $query_type Query Type.
390 * @return bool Will nav display?
391 */
392 protected function layered_nav_list( $terms, $taxonomy, $query_type ) {
393 // List display.
394 echo '<ul class="woocommerce-widget-layered-nav-list">';
395
396 $term_counts = $this->get_filtered_term_product_counts( wp_list_pluck( $terms, 'term_id' ), $taxonomy, $query_type );
397 $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
398 $found = false;
399 $base_link = $this->get_current_page_url();
400
401 foreach ( $terms as $term ) {
402 $current_values = isset( $_chosen_attributes[ $taxonomy ]['terms'] ) ? $_chosen_attributes[ $taxonomy ]['terms'] : array();
403 $option_is_set = in_array( $term->slug, $current_values, true );
404 $count = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : 0;
405
406 // Skip the term for the current archive.
407 if ( $this->get_current_term_id() === $term->term_id ) {
408 continue;
409 }
410
411 // Only show options with count > 0.
412 if ( 0 < $count ) {
413 $found = true;
414 } elseif ( 0 === $count && ! $option_is_set ) {
415 continue;
416 }
417
418 $filter_name = 'filter_' . wc_attribute_taxonomy_slug( $taxonomy );
419 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
420 $current_filter = isset( $_GET[ $filter_name ] ) ? explode( ',', wc_clean( wp_unslash( $_GET[ $filter_name ] ) ) ) : array();
421 $current_filter = array_map( 'sanitize_title', $current_filter );
422
423 if ( ! in_array( $term->slug, $current_filter, true ) ) {
424 $current_filter[] = $term->slug;
425 }
426
427 $link = remove_query_arg( $filter_name, $base_link );
428
429 // Add current filters to URL.
430 foreach ( $current_filter as $key => $value ) {
431 // Exclude query arg for current term archive term.
432 if ( $value === $this->get_current_term_slug() ) {
433 unset( $current_filter[ $key ] );
434 }
435
436 // Exclude self so filter can be unset on click.
437 if ( $option_is_set && $value === $term->slug ) {
438 unset( $current_filter[ $key ] );
439 }
440 }
441
442 if ( ! empty( $current_filter ) ) {
443 asort( $current_filter );
444 $link = add_query_arg( $filter_name, implode( ',', $current_filter ), $link );
445
446 // Add Query type Arg to URL.
447 if ( 'or' === $query_type && ! ( 1 === count( $current_filter ) && $option_is_set ) ) {
448 $link = add_query_arg( 'query_type_' . wc_attribute_taxonomy_slug( $taxonomy ), 'or', $link );
449 }
450 $link = str_replace( '%2C', ',', $link );
451 }
452
453 if ( $count > 0 || $option_is_set ) {
454 $link = apply_filters( 'woocommerce_layered_nav_link', $link, $term, $taxonomy );
455 $term_html = '<a rel="nofollow" href="' . esc_url( $link ) . '">' . esc_html( $term->name ) . '</a>';
456 } else {
457 $link = false;
458 $term_html = '<span>' . esc_html( $term->name ) . '</span>';
459 }
460
461 $term_html .= ' ' . apply_filters( 'woocommerce_layered_nav_count', '<span class="count">(' . absint( $count ) . ')</span>', $count, $term );
462
463 echo '<li class="woocommerce-widget-layered-nav-list__item wc-layered-nav-term ' . ( $option_is_set ? 'woocommerce-widget-layered-nav-list__item--chosen chosen' : '' ) . '">';
464 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.EscapeOutput.OutputNotEscaped
465 echo apply_filters( 'woocommerce_layered_nav_term_html', $term_html, $term, $link, $count );
466 echo '</li>';
467 }
468
469 echo '</ul>';
470
471 return $found;
472 }
473 }
474