__('Shows active filters so users can see and deactivate them.', 'wchweb')) // Args
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget($args, $instance) {
if (!is_post_type_archive('product') && !is_tax(get_object_taxonomies('product'))) {
return;
}
// enqueue necessary scripts
wp_enqueue_style('wchweb-style');
wp_enqueue_style('font-awesome');
wp_enqueue_script('wchweb-script');
global $wchweb;
$active_filters = $wchweb->getChosenFilters();
$active_filters = $active_filters['active_filters'];
$found = false;
$html = '';
if (sizeof($active_filters) > 0) {
$found = true;
$html .= '
';
foreach ($active_filters as $key => $active_filter) {
if ($key === 'term') {
foreach ($active_filter as $data_key => $terms) {
foreach ($terms as $term_id => $term_name) {
$html .= '
' . $term_name . '';
}
}
}
if ($key === 'keyword') {
$html .= '
' . __('Search For: ', 'wchweb') . $active_filter . '';
}
if ($key === 'orderby') {
$html .= '
' . __('Orderby: ', 'wchweb') . $active_filter . '';
}
if ($key === 'min_price') {
$html .= '
' . __('Min Price: ', 'wchweb') . $active_filter . '';
}
if ($key === 'max_price') {
$html .= '
' . __('Max Price: ', 'wchweb') . $active_filter . '';
}
}
if (!empty($instance['button_text'])) {
if ( defined( 'SHOP_IS_ON_FRONT' ) ) {
$link = home_url();
} elseif ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id('shop') ) ) {
$link = get_post_type_archive_link( 'product' );
} else {
$link = get_term_link( get_query_var('term'), get_query_var('taxonomy') );
}
/**
* Search Arg.
* To support quote characters, first they are decoded from " entities, then URL encoded.
*/
if ( get_search_query() ) {
$link = add_query_arg( 's', rawurlencode( htmlspecialchars_decode( get_search_query() ) ), $link );
}
// Post Type Arg
if ( isset( $_GET['post_type'] ) ) {
$link = add_query_arg( 'post_type', wc_clean( $_GET['post_type'] ), $link );
}
$html .= '
' . $instance['button_text'] . '';
}
$html .= '
';
}
extract($args);
// Add class to before_widget from within a custom widget
// http://wordpress.stackexchange.com/questions/18942/add-class-to-before-widget-from-within-a-custom-widget
if ($found === false) {
$widget_class = 'wchweb-widget-hidden woocommerce wchweb-ajax-term-filter';
} else {
$widget_class = 'woocommerce wchweb-ajax-term-filter';
}
// no class found, so add it
if (strpos($before_widget, 'class') === false) {
$before_widget = str_replace('>', 'class="' . $widget_class . '"', $before_widget);
}
// class found but not the one that we need, so add it
else {
$before_widget = str_replace('class="', 'class="' . $widget_class . ' ', $before_widget);
}
echo $before_widget;
if (!empty($instance['title'])) {
echo $args['before_title'] . apply_filters('widget_title', $instance['title']). $args['after_title'];
}
echo $html;
echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form($instance) {
?>