id, $filter_id );
$this->preset = $preset_frontend;
$this->terms = $this->get_filter_terms();
if( 'taxonomy' === $this->module ) {
if( ! isset( A_W_F::$front->tax_var_names[$this->settings['taxonomy']] ) ) { return; }
$this->var_name = A_W_F::$front->tax_var_names[$this->settings['taxonomy']];
if( isset( A_W_F::$front->query->tax[$this->settings['taxonomy']] ) ) {
$this->active_values = A_W_F::$front->query->tax[$this->settings['taxonomy']];
}
if( ( ! empty( $this->settings['show_count'] ) || ( isset( $this->settings['hide_empty'] ) && 'none' !== $this->settings['hide_empty'] ) )
&& ! empty( $this->terms )
&& ! isset( A_W_F::$front->counts[$this->settings['taxonomy']] )
) { $this->build_counts(); }
} else {
if( 'price' === $this->module || 'rating' === $this->module ) {
$this->var_name = 'min_' . $this->module;
if( isset( A_W_F::$front->query->awf['min_' . $this->module] ) ) {
$this->active_values['min'] = (float) A_W_F::$front->query->awf['min_' . $this->module];
} else {
$this->active_values['min'] = (float) $this->settings['type_options']['range_values'][0];
}
if( isset( A_W_F::$front->query->awf['max_' . $this->module] ) ) {
$this->active_values['max'] = (float) A_W_F::$front->query->awf['max_' . $this->module];
} else {
$this->active_values['max'] = (float) $this->settings['type_options']['range_values'][count( $this->settings['type_options']['range_values'] ) - 1];
}
} else {
$this->var_name = A_W_F::$front->awf_var_names[$this->module];
if( isset( A_W_F::$front->query->awf[$this->module] ) ) {
$this->active_values[] = A_W_F::$front->query->awf[$this->module];
}
}
}
}
function get_html() {
$this->terms_by_parent = $this->build_terms_by_parent( $this->terms );
$this->input_classes = array( 'awf-filter' );
if( in_array( $this->settings['style'], array( 'labels', 'icons', 'images', 'colours', 'tags', 'custom-terms', 'range-stars' ) ) ) {
$this->input_classes[] = 'awf-hidden';
}
$html = '
settings['type'] ) ) { $html .= ' awf-' . $this->settings['type']; }
$html .= $this->add_filter_wrapper_classes( $html );
$html .= '">';
if( ! empty( $this->settings['show_title'] ) ) {
$html .= '
' . $this->settings['title'] . '
';
$html .= $this->collapse_btn_html();
$html .= '
';
}
if( ! empty( $this->settings['show_active'] ) ) {
$html .= '
';
}
$html .= '
';
return $html;
}
function searchbox_html() {
$html = '';
$html .= '';
$html .= 'active_values[0] . '" data-taxonomy="' . $this->var_name . '"';
$html .= ' placeholder="' . esc_attr__( 'Search products…', 'annasta-filters' ) . '" class="awf-filter">';
$html .= '
';
return $html;
}
function range_slider_html() {
$max_var_name = 'max_' . $this->module;
$max_range_value = $this->settings['type_options']['range_values'][count( $this->settings['type_options']['range_values'] ) - 1];
$html = 'settings['type_options']['range_values'][0] . '" data-max="' . $max_range_value . '"';
$html .= ' data-values="' . implode( '--', $this->settings['type_options']['range_values'] ) . '"';
$html .= ' data-step="' . $this->settings['style_options']['step'] . '"';
$html .= ' data-label="' . esc_attr( $this->settings['style_options']['badge_label'], 'annasta-filters' ) . '"';
$html .= ' data-prefix="' . esc_attr( $this->settings['style_options']['value_prefix'], 'annasta-filters' ) . '"';
$html .= ' data-postfix="' . esc_attr( $this->settings['style_options']['value_postfix'], 'annasta-filters' ) . '"';
$html .= '>';
$html .= 'awf_var_names[$this->var_name] . '" data-module="' . $this->module . '"';
$html .= ' class="awf-filter awf-range-slider-value awf-range-slider-min';
if( $this->active_values['min'] === $this->settings['type_options']['range_values'][0] ) {
$html .= ' awf-default';
}
$html .= '">';
$html .= 'active_values['max'] . '" data-taxonomy="' . A_W_F::$front->awf_var_names[$max_var_name] . '" data-module="' . $this->module . '"';
$html .= ' class="awf-filter awf-range-slider-value awf-range-slider-max';
if( $this->active_values['max'] === $max_range_value ) {
$html .= ' awf-default';
}
$html .= '">';
$html .= '
';
if( ! empty( $this->settings['style_options']['show_range_btn'] ) ) {
if( 'yes' === $this->preset->button_on && 'no' === $this->preset->ajax_on ) {
$html .= '';
} else {
$html .= '';
}
}
return $html;
}
function terms_list_html( $parent_id = 0 ) {
$html = '';
foreach( $this->terms_by_parent[$parent_id] as $i => $term ) {
$slug_for_classes = is_int( $term->slug ) ? $term->slug : sanitize_title( $term->slug );
$input_id = $this->preset->caller_id . '-' . $this->var_name . '-' . $slug_for_classes;
$container_classes = array( 'awf-filter-container', 'awf-' . $this->var_name . '-' . $slug_for_classes . '-container' );
$input_classes = $this->input_classes; //, 'awf-' . $this->var_name,
$input_props = array();
$product_count_html = '';
if( $term->slug === $this->default_value ) { $input_classes[] = 'awf-default'; }
if( 'range' === $this->settings['type'] ) {
$container_classes[] = 'awf-range-filter-container';
if( $term->slug === $this->active_values['min'] && $term->next_value === $this->active_values['max'] ) {
$container_classes[] = 'awf-active';
$input_props[] = 'checked="checked"';
}
} else {
if( in_array( $term->slug, $this->active_values ) ) {
$container_classes[] = 'awf-active';
$input_props[] = 'checked="checked"';
}
}
if( ! empty( $this->settings['children_collapsible'] ) && isset( $this->terms_by_parent[$term->term_id] ) ) {
$container_classes[] = 'awf-parent-container';
if( ! empty( $this->settings['children_collapsible_on'] ) ) { $container_classes[] = 'awf-collapsed-on'; }
}
if( 'taxonomy' === $this->module && isset( A_W_F::$front->counts[$this->settings['taxonomy']] ) ) {
$this->set_hide_empty( $term->slug, $container_classes, $input_props );
if( ! empty( $this->settings['show_count'] ) ) {
$product_count_html = '' . A_W_F::$front->counts[$this->settings['taxonomy']][$term->slug] . '';
}
}
$html .= '';
$filter_html = '';
if( 'single' === $this->settings['type'] ) {
$filter_html .= 'settings['type'] ) {
$filter_html .= 'settings['type'] ) {
$filter_html .= 'module . '"';
$filter_html .= ' data-max-name="' . A_W_F::$front->awf_var_names['max_' . $this->module] . '"';
if( in_array( $this->settings['type_options']['range_type'], array( 'auto_range', 'custom_range' ) ) ) {
$filter_html .= ' data-next-value="' . $term->next_value . '"';
$filter_html .= ' data-decimals="' . $this->settings['type_options']['decimals'] . '"';
}
}
$filter_html .= ' value="' . $term->slug . '" data-taxonomy="' . $this->var_name . '"';
$filter_html .= ' class="' . implode( ' ', $input_classes ) . '"' . implode( ' ', $input_props ) . '>';
$filter_html .= '';
if( true === $this->preset->is_url_query ) {
$href = 'javascript:void(0);';
if( ! in_array( 'disabled', $input_props ) ) {
if( empty( $this->settings['reset_all'] ) ) {
$f = 'get_' . $this->settings['type'] . '_type_term_url';
if( method_exists( $this, $f ) ) { $href = $this->$f( $term ); }
} else {
$href = $this->get_reset_all_url( $term );
}
}
$html .= '';
$html .= $filter_html;
$html .= '';
} else {
$html .= $filter_html;
}
$html .= '';
if( isset( $this->terms_by_parent[$term->term_id] ) ) {
$html .= '';
$html .= $this->terms_list_html( $term->term_id );
$html .= '
';
}
}
return $html;
}
public function get_single_type_term_url( $term ) {
$url_filters = A_W_F::$front->url_query;
$url = A_W_F::$front->current_url;
if( $term->slug === $this->default_value ) {
if( isset( $url_filters[$this->var_name] ) ) {
unset( $url_filters[$this->var_name] );
};
} else {
if( in_array( $term->slug, $this->active_values ) ) {
unset( $url_filters[$this->var_name] );
} else {
$url_filters[$this->var_name] = $term->slug;
}
}
if( A_W_F::$front->awf_settings['cat_views_on'] ) {
if( 'product_cat' === $this->var_name ) {
if( isset( $url_filters['product_cat'] ) ) {
$url = str_replace( 'awf-insert-product-cats', $url_filters['product_cat'], A_W_F::$front->cat_url_template );
unset( $url_filters['product_cat'] );
} else {
$url = A_W_F::$front->shop_url;
}
} else {
unset( $url_filters['product_cat'] );
}
}
return add_query_arg( $url_filters, $url );
}
public function get_multi_type_term_url( $term ) {
$url_filters = A_W_F::$front->url_query;
$url = A_W_F::$front->current_url;
if( empty( $this->active_values ) ) {
$url_filters[$this->var_name] = $term->slug;
} else {
$href_terms = $this->active_values;
$key = array_search( $term->slug, $href_terms );
if ( $key === false) {
if( 0 !== $term->parent ) {
$this->remove_ancestors( $term, $href_terms );
}
if( isset( A_W_F::$front->page_associations[$this->settings['taxonomy']][$term->term_id] ) ) {
$children = get_terms( array(
'taxonomy' => $this->settings['taxonomy'],
'fields' => 'slugs',
'child_of' => (int) $term->term_id,
'hide_empty' => false
));
$href_terms = array_diff( $href_terms, $children );
}
$href_terms[] = $term->slug;
} else {
unset( $href_terms[$key] );
}
if( empty( $href_terms ) ) {
unset( $url_filters[$this->var_name] );
} else {
$url_filters[$this->var_name] = implode( ',', $href_terms );
}
}
if( A_W_F::$front->awf_settings['cat_views_on'] ) {
if( 'product_cat' === $this->var_name ) {
if( isset( $url_filters['product_cat'] ) ) {
$url = str_replace( 'awf-insert-product-cats', $url_filters['product_cat'], A_W_F::$front->cat_url_template );
unset( $url_filters['product_cat'] );
} else {
$url = A_W_F::$front->shop_url;
}
} else {
unset( $url_filters['product_cat'] );
}
}
return add_query_arg( $url_filters, $url );
}
public function get_range_type_term_url( $term ) {
$url_filters = A_W_F::$front->url_query;
$url = A_W_F::$front->current_url;
if( in_array( $term->slug, $this->active_values ) & in_array( $term->next_value, $this->active_values ) ) {
unset( $url_filters[$this->var_name] );
unset( $url_filters['max_' . $this->module] );
} else {
$url_filters[$this->var_name] = $term->slug;
$url_filters['max_' . $this->module] = $term->next_value;
}
if( A_W_F::$front->awf_settings['cat_views_on'] ) {
unset( $url_filters['product_cat'] );
}
return add_query_arg( $url_filters, $url );
}
protected function remove_ancestors( $term, &$query_terms ) {
if( false !== ( $parent = get_term_by( 'id', $term->parent, $term->taxonomy ) ) ) {
if( false !== ( $key = array_search( $parent->slug, $query_terms ) ) ) {
unset( $query_terms[$key] );
}
if( 0 !== $parent->parent ) {
$this->remove_ancestors( $parent, $query_terms );
}
}
return;
}
public function build_counts() {
$terms_by_parent = array();
foreach( $this->terms as $term ) { $terms_by_parent[$term->{'parent'}][] = $term; }
if( isset( $terms_by_parent[0] ) ) {
$tax_backup = isset( A_W_F::$front->query->tax[$this->settings['taxonomy']] ) ? A_W_F::$front->query->tax[$this->settings['taxonomy']] : false;
$this->counts_walker( $terms_by_parent );
if( false === $tax_backup ) { unset( A_W_F::$front->query->tax[$this->settings['taxonomy']] ); }
else { A_W_F::$front->query->tax[$this->settings['taxonomy']] = $tax_backup; }
A_W_F::$front->update_counts_cache = true;
}
}
protected function counts_walker( $terms_by_parent, $parent_id = 0 ) {
if( empty( $terms_by_parent[$parent_id] ) ) { return; }
foreach ( $terms_by_parent[$parent_id] as $term ) {
A_W_F::$front->query->tax[$this->settings['taxonomy']] = array( $term->slug );
$args = array(
'post_type' => 'product',
'fields' => 'ids',
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'post_status' => 'publish',
'no_found_rows' => true,
'posts_per_page' => -1,
'paged' => '',
'post__in' => array(),
);
$args['tax_query'] = A_W_F::$front->set_wc_tax_query( array() );
A_W_F::$front->set_default_visibility( $args['tax_query'] );
$args['meta_query'] = A_W_F::$front->set_wc_meta_query( array() );
A_W_F::$front->set_sc_post__in( $args );
$args['post__in'] = A_W_F::$front->set_wc_post__in( $args['post__in'] );
$query = new WP_Query( $args );
A_W_F::$front->counts[$this->settings['taxonomy']][$term->slug] = (int) $query->post_count;
if( isset( $terms_by_parent[$term->term_id] ) ) {
$this->counts_walker( $terms_by_parent, $term->term_id );
}
}
return;
}
protected function add_filter_wrapper_classes() { return ''; }
protected function collapse_btn_html() { return ''; }
protected function set_term_label( &$term ) {}
protected function set_hide_empty( $slug, &$container_classes, &$input_props ) {}
protected function add_range_slider_container_classes() { return ''; }
}
}
?>