woocommerce/includes/widgets/class-wc-widget-layered-nav...

470 lines
14 KiB
PHP
Raw Normal View History

2012-05-26 16:25:07 +00:00
<?php
/**
* Layered nav widget
*
* @package WooCommerce\Widgets
* @version 2.6.0
*/
use Automattic\WooCommerce\Internal\ProductAttributesLookup\Filterer;
defined( 'ABSPATH' ) || exit;
2012-05-26 16:25:07 +00:00
/**
* Widget layered nav class.
2012-05-26 16:25:07 +00:00
*/
class WC_Widget_Layered_Nav extends WC_Widget {
2012-08-14 17:37:50 +00:00
/**
2015-11-03 13:31:20 +00:00
* Constructor.
2012-08-14 17:37:50 +00:00
*/
public function __construct() {
2017-07-10 13:36:47 +00:00
$this->widget_cssclass = 'woocommerce widget_layered_nav woocommerce-widget-layered-nav';
$this->widget_description = __( 'Display a list of attributes to filter products in your store.', 'woocommerce' );
$this->widget_id = 'woocommerce_layered_nav';
$this->widget_name = __( 'Filter Products by Attribute', 'woocommerce' );
2013-06-20 11:05:07 +00:00
parent::__construct();
}
/**
* Updates a particular instance of a widget.
2013-06-20 11:05:07 +00:00
*
* @see WP_Widget->update
*
* @param array $new_instance New Instance.
* @param array $old_instance Old Instance.
*
2013-06-20 11:05:07 +00:00
* @return array
*/
public function update( $new_instance, $old_instance ) {
$this->init_settings();
return parent::update( $new_instance, $old_instance );
}
/**
* Outputs the settings update form.
2013-06-20 11:05:07 +00:00
*
* @see WP_Widget->form
*
* @param array $instance Instance.
2013-06-20 11:05:07 +00:00
*/
public function form( $instance ) {
$this->init_settings();
2013-11-28 13:12:08 +00:00
parent::form( $instance );
2013-06-20 11:05:07 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Init settings after post types are registered.
2013-06-20 11:05:07 +00:00
*/
public function init_settings() {
$attribute_array = array();
$std_attribute = '';
$attribute_taxonomies = wc_get_attribute_taxonomies();
2016-06-06 16:24:31 +00:00
if ( ! empty( $attribute_taxonomies ) ) {
foreach ( $attribute_taxonomies as $tax ) {
if ( taxonomy_exists( wc_attribute_taxonomy_name( $tax->attribute_name ) ) ) {
$attribute_array[ $tax->attribute_name ] = $tax->attribute_name;
}
}
$std_attribute = current( $attribute_array );
}
$this->settings = array(
'title' => array(
'type' => 'text',
'std' => __( 'Filter by', 'woocommerce' ),
'label' => __( 'Title', 'woocommerce' ),
),
'attribute' => array(
'type' => 'select',
'std' => $std_attribute,
'label' => __( 'Attribute', 'woocommerce' ),
'options' => $attribute_array,
),
'display_type' => array(
'type' => 'select',
'std' => 'list',
'label' => __( 'Display type', 'woocommerce' ),
'options' => array(
'list' => __( 'List', 'woocommerce' ),
'dropdown' => __( 'Dropdown', 'woocommerce' ),
),
),
'query_type' => array(
'type' => 'select',
'std' => 'and',
'label' => __( 'Query type', 'woocommerce' ),
'options' => array(
'and' => __( 'AND', 'woocommerce' ),
'or' => __( 'OR', 'woocommerce' ),
),
),
);
2012-05-26 16:25:07 +00:00
}
/**
* Get this widgets taxonomy.
*
* @param array $instance Array of instance options.
* @return string
*/
protected function get_instance_taxonomy( $instance ) {
if ( isset( $instance['attribute'] ) ) {
return wc_attribute_taxonomy_name( $instance['attribute'] );
}
$attribute_taxonomies = wc_get_attribute_taxonomies();
if ( ! empty( $attribute_taxonomies ) ) {
foreach ( $attribute_taxonomies as $tax ) {
if ( taxonomy_exists( wc_attribute_taxonomy_name( $tax->attribute_name ) ) ) {
return wc_attribute_taxonomy_name( $tax->attribute_name );
}
}
}
return '';
}
/**
* Get this widgets query type.
*
* @param array $instance Array of instance options.
* @return string
*/
protected function get_instance_query_type( $instance ) {
return isset( $instance['query_type'] ) ? $instance['query_type'] : 'and';
}
/**
* Get this widgets display type.
*
* @param array $instance Array of instance options.
* @return string
*/
protected function get_instance_display_type( $instance ) {
return isset( $instance['display_type'] ) ? $instance['display_type'] : 'list';
}
2012-08-14 17:37:50 +00:00
/**
* Output widget.
2012-08-14 17:37:50 +00:00
*
* @see WP_Widget
*
* @param array $args Arguments.
* @param array $instance Instance.
2012-08-14 17:37:50 +00:00
*/
public function widget( $args, $instance ) {
2017-11-09 11:16:47 +00:00
if ( ! is_shop() && ! is_product_taxonomy() ) {
2012-08-28 15:18:16 +00:00
return;
}
2012-08-14 17:37:50 +00:00
2016-02-10 10:19:32 +00:00
$_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
$taxonomy = $this->get_instance_taxonomy( $instance );
$query_type = $this->get_instance_query_type( $instance );
$display_type = $this->get_instance_display_type( $instance );
2012-05-26 16:25:07 +00:00
if ( ! taxonomy_exists( $taxonomy ) ) {
2012-08-28 15:18:16 +00:00
return;
}
2012-08-14 17:37:50 +00:00
2019-01-25 19:55:25 +00:00
$terms = get_terms( $taxonomy, array( 'hide_empty' => '1' ) );
2012-08-14 17:37:50 +00:00
if ( 0 === count( $terms ) ) {
return;
}
2012-08-14 17:37:50 +00:00
ob_start();
2012-08-14 17:37:50 +00:00
$this->widget_start( $args, $instance );
2012-08-14 17:37:50 +00:00
if ( 'dropdown' === $display_type ) {
2017-07-10 13:36:47 +00:00
wp_enqueue_script( 'selectWoo' );
2017-02-24 08:18:38 +00:00
wp_enqueue_style( 'select2' );
$found = $this->layered_nav_dropdown( $terms, $taxonomy, $query_type );
} else {
$found = $this->layered_nav_list( $terms, $taxonomy, $query_type );
}
2012-08-14 17:37:50 +00:00
$this->widget_end( $args );
2012-08-14 17:37:50 +00:00
// Force found when option is selected - do not force found on taxonomy attributes.
if ( ! is_tax() && is_array( $_chosen_attributes ) && array_key_exists( $taxonomy, $_chosen_attributes ) ) {
$found = true;
}
2012-08-14 17:37:50 +00:00
if ( ! $found ) {
ob_end_clean();
} else {
echo ob_get_clean(); // @codingStandardsIgnoreLine
}
}
2012-08-14 17:37:50 +00:00
/**
* Return the currently viewed taxonomy name.
*
* @return string
*/
protected function get_current_taxonomy() {
return is_tax() ? get_queried_object()->taxonomy : '';
}
2012-08-14 17:37:50 +00:00
/**
* Return the currently viewed term ID.
*
* @return int
*/
protected function get_current_term_id() {
return absint( is_tax() ? get_queried_object()->term_id : 0 );
}
2012-08-14 17:37:50 +00:00
/**
* Return the currently viewed term slug.
*
* @return int
*/
protected function get_current_term_slug() {
return absint( is_tax() ? get_queried_object()->slug : 0 );
}
2012-08-14 17:37:50 +00:00
/**
* Show dropdown layered nav.
*
* @param array $terms Terms.
* @param string $taxonomy Taxonomy.
* @param string $query_type Query Type.
* @return bool Will nav display?
*/
protected function layered_nav_dropdown( $terms, $taxonomy, $query_type ) {
2017-07-10 13:36:47 +00:00
global $wp;
$found = false;
2012-08-14 17:37:50 +00:00
if ( $taxonomy !== $this->get_current_taxonomy() ) {
$term_counts = $this->get_filtered_term_product_counts( wp_list_pluck( $terms, 'term_id' ), $taxonomy, $query_type );
2017-07-11 10:31:02 +00:00
$_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
$taxonomy_filter_name = wc_attribute_taxonomy_slug( $taxonomy );
$taxonomy_label = wc_attribute_label( $taxonomy );
/* translators: %s: taxonomy name */
$any_label = apply_filters( 'woocommerce_layered_nav_any_label', sprintf( __( 'Any %s', 'woocommerce' ), $taxonomy_label ), $taxonomy_label, $taxonomy );
$multiple = 'or' === $query_type;
$current_values = isset( $_chosen_attributes[ $taxonomy ]['terms'] ) ? $_chosen_attributes[ $taxonomy ]['terms'] : array();
2012-08-14 17:37:50 +00:00
2017-07-10 13:36:47 +00:00
if ( '' === get_option( 'permalink_structure' ) ) {
$form_action = remove_query_arg( array( 'page', 'paged' ), add_query_arg( $wp->query_string, '', home_url( $wp->request ) ) );
2017-02-24 08:18:38 +00:00
} else {
$form_action = preg_replace( '%\/page/[0-9]+%', '', home_url( user_trailingslashit( $wp->request ) ) );
2017-02-24 08:18:38 +00:00
}
2017-07-10 13:36:47 +00:00
echo '<form method="get" action="' . esc_url( $form_action ) . '" class="woocommerce-widget-layered-nav-dropdown">';
echo '<select class="woocommerce-widget-layered-nav-dropdown dropdown_layered_nav_' . esc_attr( $taxonomy_filter_name ) . '"' . ( $multiple ? 'multiple="multiple"' : '' ) . '>';
echo '<option value="">' . esc_html( $any_label ) . '</option>';
2012-08-14 17:37:50 +00:00
foreach ( $terms as $term ) {
2012-08-14 17:37:50 +00:00
// If on a term page, skip that term in widget list.
if ( $term->term_id === $this->get_current_term_id() ) {
continue;
2012-05-26 16:25:07 +00:00
}
2012-08-14 17:37:50 +00:00
// Get count based on current view.
$option_is_set = in_array( $term->slug, $current_values, true );
$count = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : 0;
2012-08-14 17:37:50 +00:00
// Only show options with count > 0.
2016-02-10 10:02:50 +00:00
if ( 0 < $count ) {
$found = true;
} elseif ( 0 === $count && ! $option_is_set ) {
2016-02-10 10:02:50 +00:00
continue;
}
2012-08-14 17:37:50 +00:00
echo '<option value="' . esc_attr( urldecode( $term->slug ) ) . '" ' . selected( $option_is_set, true, false ) . '>' . esc_html( $term->name ) . '</option>';
}
2012-08-14 17:37:50 +00:00
echo '</select>';
2012-08-14 17:37:50 +00:00
2017-07-10 13:36:47 +00:00
if ( $multiple ) {
echo '<button class="woocommerce-widget-layered-nav-dropdown__submit" type="submit" value="' . esc_attr__( 'Apply', 'woocommerce' ) . '">' . esc_html__( 'Apply', 'woocommerce' ) . '</button>';
2017-07-10 13:36:47 +00:00
}
if ( 'or' === $query_type ) {
echo '<input type="hidden" name="query_type_' . esc_attr( $taxonomy_filter_name ) . '" value="or" />';
}
echo '<input type="hidden" name="filter_' . esc_attr( $taxonomy_filter_name ) . '" value="' . esc_attr( implode( ',', $current_values ) ) . '" />';
echo wc_query_string_form_fields( null, array( 'filter_' . $taxonomy_filter_name, 'query_type_' . $taxonomy_filter_name ), '', true ); // @codingStandardsIgnoreLine
2017-07-10 13:36:47 +00:00
echo '</form>';
wc_enqueue_js(
"
2017-07-10 13:36:47 +00:00
// Update value on change.
jQuery( '.dropdown_layered_nav_" . esc_js( $taxonomy_filter_name ) . "' ).on( 'change', function() {
var slug = jQuery( this ).val();
2017-07-10 13:36:47 +00:00
jQuery( ':input[name=\"filter_" . esc_js( $taxonomy_filter_name ) . "\"]' ).val( slug );
2017-07-11 20:21:42 +00:00
// Submit form on change if standard dropdown.
if ( ! jQuery( this ).attr( 'multiple' ) ) {
jQuery( this ).closest( 'form' ).trigger( 'submit' );
2017-07-11 20:21:42 +00:00
}
});
2017-02-24 08:18:38 +00:00
// Use Select2 enhancement if possible
2017-07-10 13:36:47 +00:00
if ( jQuery().selectWoo ) {
2017-02-24 08:18:38 +00:00
var wc_layered_nav_select = function() {
2017-07-10 13:36:47 +00:00
jQuery( '.dropdown_layered_nav_" . esc_js( $taxonomy_filter_name ) . "' ).selectWoo( {
2019-02-20 12:00:47 +00:00
placeholder: decodeURIComponent('" . rawurlencode( (string) wp_specialchars_decode( $any_label ) ) . "'),
2017-07-10 13:36:47 +00:00
minimumResultsForSearch: 5,
width: '100%',
2017-10-09 18:10:32 +00:00
allowClear: " . ( $multiple ? 'false' : 'true' ) . ",
language: {
noResults: function() {
2017-10-09 18:37:09 +00:00
return '" . esc_js( _x( 'No matches found', 'enhanced select', 'woocommerce' ) ) . "';
2017-10-09 18:10:32 +00:00
}
}
2017-07-10 13:36:47 +00:00
} );
2017-02-24 08:18:38 +00:00
};
wc_layered_nav_select();
}
"
);
}
2012-08-14 17:37:50 +00:00
return $found;
}
2012-08-14 17:37:50 +00:00
2016-02-09 21:14:55 +00:00
/**
2016-02-10 11:07:42 +00:00
* Count products within certain terms, taking the main WP query into consideration.
*
* This query allows counts to be generated based on the viewed products, not all products.
*
* @param array $term_ids Term IDs.
* @param string $taxonomy Taxonomy.
* @param string $query_type Query Type.
2016-02-10 11:07:42 +00:00
* @return array
2016-02-09 21:14:55 +00:00
*/
2016-02-10 11:07:42 +00:00
protected function get_filtered_term_product_counts( $term_ids, $taxonomy, $query_type ) {
return wc_get_container()->get( Filterer::class )->get_filtered_term_product_counts( $term_ids, $taxonomy, $query_type );
2016-02-09 21:14:55 +00:00
}
/**
* Wrapper for WC_Query::get_main_tax_query() to ease unit testing.
*
* @since 4.4.0
* @return array
*/
protected function get_main_tax_query() {
return WC_Query::get_main_tax_query();
}
/**
* Wrapper for WC_Query::get_main_search_query_sql() to ease unit testing.
*
* @since 4.4.0
* @return string
*/
protected function get_main_search_query_sql() {
return WC_Query::get_main_search_query_sql();
}
/**
* Wrapper for WC_Query::get_main_search_queryget_main_meta_query to ease unit testing.
*
* @since 4.4.0
* @return array
*/
protected function get_main_meta_query() {
return WC_Query::get_main_meta_query();
}
/**
* Show list based layered nav.
*
* @param array $terms Terms.
* @param string $taxonomy Taxonomy.
* @param string $query_type Query Type.
* @return bool Will nav display?
*/
protected function layered_nav_list( $terms, $taxonomy, $query_type ) {
// List display.
2017-07-10 13:36:47 +00:00
echo '<ul class="woocommerce-widget-layered-nav-list">';
2014-02-24 15:45:43 +00:00
2016-02-10 11:07:42 +00:00
$term_counts = $this->get_filtered_term_product_counts( wp_list_pluck( $terms, 'term_id' ), $taxonomy, $query_type );
2016-02-10 10:19:32 +00:00
$_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
$found = false;
2019-01-30 16:29:01 +00:00
$base_link = $this->get_current_page_url();
2012-11-27 16:22:47 +00:00
foreach ( $terms as $term ) {
$current_values = isset( $_chosen_attributes[ $taxonomy ]['terms'] ) ? $_chosen_attributes[ $taxonomy ]['terms'] : array();
$option_is_set = in_array( $term->slug, $current_values, true );
$count = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : 0;
2012-08-14 17:37:50 +00:00
// Skip the term for the current archive.
if ( $this->get_current_term_id() === $term->term_id ) {
continue;
}
2012-08-14 17:37:50 +00:00
// Only show options with count > 0.
2016-02-10 10:02:50 +00:00
if ( 0 < $count ) {
$found = true;
} elseif ( 0 === $count && ! $option_is_set ) {
2016-02-10 10:02:50 +00:00
continue;
}
2012-08-14 17:37:50 +00:00
$filter_name = 'filter_' . wc_attribute_taxonomy_slug( $taxonomy );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$current_filter = isset( $_GET[ $filter_name ] ) ? explode( ',', wc_clean( wp_unslash( $_GET[ $filter_name ] ) ) ) : array();
$current_filter = array_map( 'sanitize_title', $current_filter );
2012-08-14 17:37:50 +00:00
if ( ! in_array( $term->slug, $current_filter, true ) ) {
$current_filter[] = $term->slug;
}
2012-08-14 17:37:50 +00:00
2019-01-30 16:29:01 +00:00
$link = remove_query_arg( $filter_name, $base_link );
2012-08-14 17:37:50 +00:00
// Add current filters to URL.
foreach ( $current_filter as $key => $value ) {
// Exclude query arg for current term archive term.
if ( $value === $this->get_current_term_slug() ) {
unset( $current_filter[ $key ] );
}
2012-08-14 17:37:50 +00:00
// Exclude self so filter can be unset on click.
if ( $option_is_set && $value === $term->slug ) {
unset( $current_filter[ $key ] );
}
}
2012-08-14 17:37:50 +00:00
if ( ! empty( $current_filter ) ) {
2017-10-27 15:07:30 +00:00
asort( $current_filter );
$link = add_query_arg( $filter_name, implode( ',', $current_filter ), $link );
2012-08-14 17:37:50 +00:00
// Add Query type Arg to URL.
if ( 'or' === $query_type && ! ( 1 === count( $current_filter ) && $option_is_set ) ) {
$link = add_query_arg( 'query_type_' . wc_attribute_taxonomy_slug( $taxonomy ), 'or', $link );
2012-05-26 16:25:07 +00:00
}
2017-10-27 14:59:47 +00:00
$link = str_replace( '%2C', ',', $link );
}
2012-08-14 17:37:50 +00:00
if ( $count > 0 || $option_is_set ) {
$link = apply_filters( 'woocommerce_layered_nav_link', $link, $term, $taxonomy );
$term_html = '<a rel="nofollow" href="' . esc_url( $link ) . '">' . esc_html( $term->name ) . '</a>';
} else {
$link = false;
$term_html = '<span>' . esc_html( $term->name ) . '</span>';
}
$term_html .= ' ' . apply_filters( 'woocommerce_layered_nav_count', '<span class="count">(' . absint( $count ) . ')</span>', $count, $term );
2017-07-10 13:36:47 +00:00
echo '<li class="woocommerce-widget-layered-nav-list__item wc-layered-nav-term ' . ( $option_is_set ? 'woocommerce-widget-layered-nav-list__item--chosen chosen' : '' ) . '">';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.EscapeOutput.OutputNotEscaped
echo apply_filters( 'woocommerce_layered_nav_term_html', $term_html, $term, $link, $count );
echo '</li>';
2012-05-26 16:25:07 +00:00
}
echo '</ul>';
return $found;
2012-05-26 16:25:07 +00:00
}
}