'orderby', // wc - hardcoded 'search' => 's', // wc - hardcoded 'ppp' => 'ppp', 'min_price' => 'min_price', 'max_price' => 'max_price', 'min_rating' => 'min_rating', 'max_rating' => 'max_rating', 'stock' => 'availability', 'featured' => 'featured', 'onsale' => 'onsale', ); public $tax_var_names = array( 'product_tag' => 'product-tags', 'product_cat' => 'product-categories' ); public $unregistered_tax_vars = array(); public $query; public $url_query; public $page_associations; public $shop_on_frontpage; public $shop_page_id; public $shop_url; public $current_url; public $permalinks_on = false; public $archived_taxonomies; public $cat_url_template; private $cat_names = ''; private $is_wp_page = false; private $is_sc_page = false; public $counts_cache_name; public $counts; public $update_counts_cache; private function __construct() { $this->query = (object) array( 'awf' => array(), 'tax' => array() ); add_action( 'init', array( $this, 'initialize'), 20 ); add_filter( 'query_vars', array( $this, 'register_query_vars' ) ); add_action( 'pre_get_posts', array( $this, 'before_wc_query' ), 0 ); add_action( 'template_redirect', array( $this, 'redirect_archives' ), 20 ); //add_action( 'woocommerce_product_query', array( $this, 'wc_query' ), 50, 2 ); //add_action( 'pre_get_posts', array( $this, 'after_wc_query' ), 20 ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts'), 10 ); add_action( 'shutdown', array( $this, 'update_counts_cache') ); $this->filters_manager = 'A_W_F_filter_frontend'; } public function ajax_controller() { if( 'filter' === $_GET['awf_action'] ) { $sc_args; $sc; if( isset( $_GET['awf_sc_page'] ) ) { $this->is_sc_page = (int) $_GET['awf_sc_page']; $this->initialize(); add_filter( 'shortcode_atts_products', array( $this, 'add_awf_sc_class' ) ); add_action( 'woocommerce_shortcode_before_products_loop', array( $this, 'insert_sc_ajax_vars' ) ); add_action( 'woocommerce_shortcode_products_loop_no_results', array( $this, 'insert_sc_ajax_vars' ) ); $sc_args = isset( $_GET['awf_sc'] ) ? $_GET['awf_sc'] : array(); } else { $this->initialize(); $sc_args = array( 'paginate' => true ); if( ! empty( $this->awf_settings['shop_columns'] ) ) { $sc_args['columns'] = $this->awf_settings['shop_columns']; } if( empty( $this->awf_settings['ppp_default'] ) ) { $sc_args['limit'] = get_option( 'posts_per_page', 12 ); } else { $sc_args['limit'] = $this->awf_settings['ppp_default']; } } $this->build_ajax_queries(); add_filter( 'woocommerce_shortcode_products_query', array( $this, 'wc_shortcode_query' ), 10, 3 ); add_filter( 'woocommerce_pagination_args', array( $this, 'adjust_wc_pagination' ) ); add_filter( 'paginate_links', function( $href ) { $remove_pagination_args = array_keys( $_REQUEST ); if( ! empty( $this->awf_settings['cat_views_on'] ) && ! empty( $this->permalinks_on ) ) { $remove_pagination_args[] = 'product_cat'; } return remove_query_arg( $remove_pagination_args, $href ); }); add_action( 'woocommerce_shortcode_products_loop_no_results', array( $this, 'display_no_results_msg' ) ); if( ! isset( $this->query->awf['orderby'] ) ) { $sc_args['orderby'] = 'popularity'; } $sc = '[products'; foreach( $sc_args as $k => $v ) { if( ! empty( $v ) ) { if( in_array( $k, array( 'limit', 'columns', 'rows', 'page' ) ) ) { $sc .= ' ' . $k . '="' . intval( $v ) . '"'; } else { $sc .= ' ' . sanitize_key( $k ) . '="' . sanitize_title_for_query( $v ) . '"'; } } } $sc .= ']'; echo do_shortcode( $sc ); } else if( 'update_filters' === $_GET['awf_action'] ) { // $microtime = microtime(); $response = array(); $this->awf_settings['cat_views_on'] = ( 'yes' === get_option( 'awf_cat_views_on' ) ); $this->awf_settings['include_children'] = ( 'yes' === get_option( 'awf_include_children_on', 'yes' ) ); $this->set_query_vars(); $this->build_ajax_queries(); // @ToDo remove excluded taxonomy $excluded_taxonomy = sanitize_text_field( $_GET['awf_exclude_taxonomy'] ); ksort( $this->query->tax ); ksort( $this->query->awf ); array_walk( $this->query->tax, function( &$value, $key ) { if( is_array( $value ) ) { sort( $value ); } }); array_walk( $this->query->awf, function( &$value, $key ) { if( is_array( $value ) ) { sort( $value ); } }); unset( $this->query->awf['orderby'] ); unset( $this->query->awf['ppp'] ); $this->counts_cache_name = 'awf_counts_' . md5( wp_json_encode( $this->query ) ); $this->counts = get_transient( $this->counts_cache_name ); if( false === $this->counts ) { $this->counts = array(); } foreach( $_GET['awf_callers'] as $caller ) { $caller = sanitize_text_field( $caller ); $pieces = substr( $caller, 0, -strlen( '-wrapper' ) ); $pieces = explode( '-', $pieces ); $preset_id = (int) array_pop( $pieces ); if( isset( A_W_F::$presets[$preset_id] ) ) { $preset = new A_W_F_preset_frontend( $preset_id ); } } if( true === $this->update_counts_cache && ! empty( $this->counts ) ) { set_transient( $this->counts_cache_name, $this->counts, DAY_IN_SECONDS * 30 ); } foreach( $this->counts as $taxonomy => $counts ) { $response['counts'][$this->tax_var_names[$taxonomy]] = $counts; } // $response['time'] = microtime() - $microtime ; echo( json_encode( $response ) ); } die(); } private function build_ajax_queries() { if( empty( $_GET['awf_query'] ) ) { return; } foreach( $_GET['awf_query'] as $var => $value ) { if( 'product_cat' === $var ) { $var = $this->tax_var_names['product_cat']; } if( ( false !== ( $taxonomy = array_search( $var, $this->tax_var_names ) ) ) ) { $terms = array_map( 'sanitize_text_field', explode( ',', $value ) ); if( 'product_cat' === $taxonomy ) { if( isset( $this->query->tax['product_cat'] ) ) { $this->query->tax['product_cat'] = array_merge( $this->query->tax['product_cat'], $terms ); } else { $this->query->tax['product_cat'] = $terms; } } else { $this->query->tax[$taxonomy] = $terms; } } else if( false !== ( $awf_var_name = array_search( $var, $this->awf_var_names ) ) ) { $this->query->awf[$awf_var_name] = sanitize_text_field( $value ); } } if( isset( $this->query->tax['product_cat'] ) ) { $this->query->tax['product_cat'] = array_unique( $this->query->tax['product_cat'] ); if( ! empty( $this->awf_settings['cat_views_on'] ) ) { $this->current_url = str_replace( 'awf-insert-product-cats', implode( ',', $this->query->tax['product_cat'] ), $this->cat_url_template ); } } if( 'filter' === $_GET['awf_action'] ) { $_GET = $this->get_url_query(); } } public function initialize() { $this->shop_page_id = wc_get_page_id( 'shop' ); if( 'page' === get_option( 'show_on_front' ) && intval( get_option( 'page_on_front' ) ) === $this->shop_page_id ) { $this->shop_on_frontpage = true; } $this->set_awf_settings(); $this->set_query_vars(); $this->setup_urls(); if( isset( $_POST['awf_submit'] ) ) { $url_query = array(); foreach( $_POST as $var => $value ) { if( is_array( $value ) ) $value = implode( ',', $value ); if( ! empty( $value ) ) { $url_query[$var] = $value; } } unset( $url_query['awf_submit'] ); if( ! empty( $this->shop_on_frontpage ) && empty( $this->is_sc_page ) && ( ! empty( $url_query['s'] ) || ! empty( $url_query['orderby'] ) ) ) { $url_query['post_type'] = 'product'; } $url = $this->shop_url; if( isset( $url_query[$this->tax_var_names['product_cat']] ) && ! empty( $this->awf_settings['cat_views_on'] ) ) { $url = str_replace( 'awf-insert-product-cats', $url_query[$this->tax_var_names['product_cat']], $this->cat_url_template ); unset( $url_query[$this->tax_var_names['product_cat']] ); } $url = add_query_arg( $url_query, $url ); wp_redirect( esc_url_raw( $url ) ); exit(); } } private function set_awf_settings() { if( false === $this->is_sc_page ) { $this->awf_settings['cat_views_on'] = ( 'yes' === get_option( 'awf_cat_views_on' ) ); } if( 0 < ( $this->awf_settings['shop_columns'] = (int) get_option( 'awf_shop_columns', 0 ) ) && ! wp_doing_ajax() ) { add_filter( 'loop_shop_columns', array( $this, 'set_shop_columns' ) ); } $this->awf_settings['ppp_default'] = (int) get_option( 'awf_ppp_default', 0 ); if( ! wp_doing_ajax() ) { add_filter('loop_shop_per_page', array( $this, 'set_products_per_page' ) ); } $this->awf_settings['include_children'] = ( 'yes' === get_option( 'awf_include_children_on', 'yes' ) ); } public function set_query_vars() { $taxonomies = get_object_taxonomies( 'product', 'objects' ); foreach( $taxonomies as $t ) { if( in_array( $t->name, A_W_F::$excluded_taxonomies ) ) continue; if( ! empty( $t->rewrite ) ) { $this->archived_taxonomies[$t->name]; } if( empty( $t->query_var ) ) { $this->unregistered_tax_vars[] = $t->name; $taxonomy_var_name = $t->name; } else { $taxonomy_var_name = $t->query_var; } if( ! isset( $this->tax_var_names[$t->name] ) ) { if( $pretty_name_from_options = get_option( 'awf_' . $t->name . '_pretty_name', false ) ) { $this->tax_var_names[$t->name] = $pretty_name_from_options; } else { $this->tax_var_names[$t->name] = $taxonomy_var_name; } } } if( ! empty( $this->awf_settings['cat_views_on'] ) ) { $this->tax_var_names['product_cat'] = 'product_cat'; } foreach( $this->awf_var_names as $var => $name ) { $pretty_name = get_option( 'awf_' . $var . '_pretty_name', '' ); if( ! empty( $pretty_name ) ) { $this->awf_var_names[$var] = $pretty_name; } } } public function setup_urls() { $this->permalinks_on = ! empty( get_option( 'permalink_structure' ) ); if ( false !== $this->is_sc_page ) { $this->shop_url = $this->current_url = get_permalink( $this->is_sc_page ); } elseif( ! empty( $this->shop_on_frontpage ) ) { $this->shop_url = $this->current_url = get_home_url() . '/'; } else { if( $this->permalinks_on ) { $this->shop_url = $this->current_url = get_permalink( $this->shop_page_id ); } else { $this->shop_url = $this->current_url = get_post_type_archive_link( 'product' ); } } if( ! empty( $this->awf_settings['cat_views_on'] ) ) { $bases = wc_get_permalink_structure(); $a_cat = get_terms( array( 'taxonomy' => 'product_cat', 'parent' => 0, 'fields' => 'slugs', 'number' => 1 ) ); if( ! empty( $a_cat ) ) { $a_cat_url = get_term_link( $a_cat[0], 'product_cat' ); if ( $this->permalinks_on ) { $this->cat_url_template = str_replace( '/' . $bases['category_rewrite_slug'] . '/' . $a_cat[0] . '/', '/' . $bases['category_rewrite_slug'] . '/awf-insert-product-cats/', $a_cat_url ); } else { $this->cat_url_template = str_replace( 'product_cat=' . $a_cat[0], 'product_cat=awf-insert-product-cats', $a_cat_url ); } } else { $this->awf_settings['cat_views_on'] = false; } } } public function register_query_vars( $vars ) { foreach( $this->tax_var_names as $var ) { if( ! in_array( $var, $vars ) ) { $vars[] = $var; } } foreach( $this->awf_var_names as $var ) { if( ! in_array( $var, $vars ) ) { $vars[] = $var; } } return $vars; } public function before_wc_query( $query ) { if( ! $query->is_main_query() || is_admin() ) { return; } if( ( (int) $query->get( 'page_id' ) === $this->shop_page_id ) || ( $query->is_home() && ! empty( $this->shop_on_frontpage ) ) ) { $query->set( 'post_type', 'product' ); $query->is_singular = false; $query->is_post_type_archive = true; $query->is_archive = true; } if( ! empty( $query->get( 'page_id' ) ) ) { $this->is_wp_page = $query->get( 'page_id' ) . ''; } elseif( $query->queried_object instanceof WP_Post ) { $this->is_wp_page = $query->queried_object_id . ''; } if( in_array( $this->is_wp_page, get_option( 'awf_shortcode_pages', array() ) ) ) { $this->is_sc_page = $this->is_wp_page; $this->filter_on = true; $this->awf_settings['cat_views_on'] = false; $this->permalinks_on = false; $this->shop_url = $this->current_url = get_permalink( $this->is_wp_page ); if( isset( $query->query['s'] ) ) { $this->query->awf['search'] = $query->query['s']; unset( $query->query_vars['s'] ); } add_filter( 'woocommerce_shortcode_products_query', array( $this, 'wc_shortcode_query' ), 10, 3 ); add_filter( 'shortcode_atts_products', array( $this, 'add_awf_sc_class' ) ); add_action( 'woocommerce_shortcode_before_products_loop', array( $this, 'insert_sc_ajax_vars' ) ); add_action( 'woocommerce_shortcode_products_loop_no_results', array( $this, 'insert_sc_ajax_vars' ) ); add_action( 'woocommerce_shortcode_products_loop_no_results', array( $this, 'display_shortcode_no_results_message' ), 20 ); } else if( ( $query->get( 'post_type' ) === 'product' ) || ! empty( $query->get( 'product_cat' ) ) ) { $this->filter_on = true; if( 'yes' === get_option( 'awf_force_products_display_on', 'yes' ) ) { add_filter( 'woocommerce_is_filtered', '__return_true' ); } add_filter( 'woocommerce_product_query_tax_query', array( $this, 'set_wc_tax_query' ) ); add_filter( 'woocommerce_product_query_meta_query', array( $this, 'set_wc_meta_query' ) ); add_filter( 'loop_shop_post_in', array( $this, 'set_wc_post__in' ) ); add_filter( 'woocommerce_redirect_single_search_result', array( $this, 'redirect_single_search_result' ) ); add_filter( 'woocommerce_page_title', array( $this, 'adjust_shop_title' ) ); add_filter( 'woocommerce_get_breadcrumb', array( $this, 'adjust_breadcrumbs' ), 10, 2 ); add_filter( 'document_title_parts', array( $this, 'adjust_document_title' ) ); } foreach( $query->query as $var => $value ) { if( ( false !== ( $var_name = array_search( $var, $this->tax_var_names ) ) ) || ( $var === 'product_cat' ) ) { if( is_array( $value ) ) { $terms = $value; } else { $terms = explode( ',', $value ); } if( 'product_cat' === $var_name ) { $var = 'product_cat'; } if( 'product_cat' === $var ) { if ( $this->permalinks_on ) { foreach( $terms as $i => $t ) { $t = explode( '/', $t ); $terms[$i] = array_pop( $t ); } } if( isset( $this->query->tax['product_cat'] ) ) { $this->query->tax['product_cat'] = array_merge( $this->query->tax['product_cat'], $terms ); } else { $this->query->tax['product_cat'] = $terms; } } else { $this->query->tax[$var_name] = $terms; } } else if( false !== ( $awf_var_name = array_search( $var, $this->awf_var_names ) ) ) { $this->query->awf[$awf_var_name] = $value; } } if( isset( $this->query->tax['product_cat'] ) ) { $this->query->tax['product_cat'] = array_unique( $this->query->tax['product_cat'] ); $query->set( 'product_cat', implode( ',', $this->query->tax['product_cat'] ) ); if( ! empty( $this->awf_settings['cat_views_on'] ) ) { $this->current_url = str_replace( 'awf-insert-product-cats', implode( ',', $this->query->tax['product_cat'] ), $this->cat_url_template ); } $cat_names = array(); foreach( $this->query->tax['product_cat'] as $slug ) { $cat_names[] = get_term_by( 'slug', $slug, 'product_cat' )->name; } $this->cat_names = implode( ', ', $cat_names ) ; } $this->url_query = $this->get_url_query(); } public function wc_shortcode_query( $args, $attrs, $type ) { if( isset( $this->query->awf['ppp'] ) ) { $args['posts_per_page'] = (int) $this->query->awf['ppp']; } $tax_query = $this->set_wc_tax_query( array() ); if( isset( $this->query->tax['product_cat'] ) && ! empty( $attrs['category'] ) ) { foreach( $args['tax_query'] as $i => $wc_tax ) { if( 'product_cat' === $wc_tax['taxonomy'] ) { foreach( $tax_query as $ii => $awf_tax ) { if( 'product_cat' === $awf_tax['taxonomy'] ) { $args['tax_query'][$i]['terms'] = array_unique( array_merge( $wc_tax['terms'], $awf_tax['terms'] ) ); $args['tax_query'][$i]['operator'] = $attrs['cat_operator']; $args['tax_query'][$i]['include_children'] = $awf_tax['include_children']; unset( $tax_query[$ii] ); break 2; } } } } } $args['tax_query'] = array_merge( $args['tax_query'], $tax_query ); $meta_query = $this->set_wc_meta_query( array() ); $args['meta_query'] = array_merge( $args['meta_query'], $meta_query ); $this->set_sc_post__in( $args ); if( isset( $this->query->awf['onsale'] ) ) { $postin = isset( $args['post__in'] ) ? $args['post__in'] : array(); $args['post__in'] = $this->set_wc_post__in( $postin ); } if( isset( $this->query->awf['orderby'] ) ) { $pieces = explode( '-', $this->query->awf['orderby'] ); if( empty( $pieces[1] ) ) { $pieces[1] = 'ASC'; } else { $pieces[1] = in_array( strtoupper( $pieces[1] ), array( 'ASC', 'DESC') ) ? strtoupper( $pieces[1] ) : 'ASC' ; } $ordering_args = WC()->query->get_catalog_ordering_args( $pieces[0], $pieces[1] ); $args['orderby'] = $ordering_args['orderby']; $args['order'] = $ordering_args['order']; if ( $ordering_args['meta_key'] ) { $args['meta_key'] = $ordering_args['meta_key']; } } return $args; } public function set_wc_tax_query( $query ) { foreach( $this->query->tax as $var => $terms ) { $operator = get_option( 'awf_' . $var . '_query_operator', 'IN' ); $query[] = array( 'taxonomy' => $var, 'field' => 'slug', 'terms' => $terms, 'operator' => $operator, 'include_children' => $this->awf_settings['include_children'], ); } if( isset( $this->query->awf['stock'] ) ) { if( in_array( $this->query->awf['stock'], array( 'instock', 'outofstock' ) ) ) { $this->{ 'set_visibility_' . $this->query->awf['stock'] }( $query ); } } if( isset( $this->query->awf['featured'] ) ) { $this->set_visibility_featured( $query ); } return $query; } public function set_wc_meta_query( $query ) { if( isset( $this->query->awf['stock'] ) && ( 'onbackorder' === $this->query->awf['stock'] ) ) { $query[] = array( 'key' => '_stock_status', 'value' => 'onbackorder' ); } if( ( isset( $this->query->awf['min_price'] ) || isset( $this->query->awf['max_price'] ) ) ) { if( version_compare( WC_VERSION, '3.6', '>=' ) ) { add_action( 'woocommerce_product_query', function() { remove_filter( 'posts_clauses', array( WC()->query, 'price_filter_post_clauses' ), 10 ); } ); } if( ! isset( $query['price_filter'] ) ) { $min_price = isset( $this->query->awf['min_price'] ) ? floatval( wp_unslash( $this->query->awf['min_price'] ) ) : 0; $max_price = isset( $this->query->awf['max_price'] ) ? floatval( wp_unslash( $this->query->awf['max_price'] ) ) : PHP_INT_MAX; if ( wc_tax_enabled() && 'incl' === get_option( 'woocommerce_tax_display_shop' ) && ! wc_prices_include_tax() ) { $tax_rates = WC_Tax::get_rates( '' ); if ( $tax_rates ) { $min_price -= WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $min_price, $tax_rates ) ); $max_price -= WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $max_price, $tax_rates ) ); } } $query['price_filter'] = array( 'key' => '_price', 'value' => array( $min_price, $max_price ), 'compare' => 'BETWEEN', 'type' => 'DECIMAL(10,' . wc_get_price_decimals() . ')', ); } } if( ( isset( $this->query->awf['min_rating'] ) || isset( $this->query->awf['max_rating'] ) ) & ! isset( $query['awf_rating_filter']['awf_rating_filter'] ) ) { $range_min = isset( $this->query->awf['min_rating'] ) ? (float) $this->query->awf['min_rating'] : (float) 0.01; if( floatval(0) === $range_min ) { $range_min = (float) 0.01; } $range_max = isset( $this->query->awf['max_rating'] ) ? (float) $this->query->awf['max_rating'] : (float) 5; $query['awf_rating_filter'] = array( 'key' => '_wc_average_rating', 'value' => array( $range_min, $range_max ), 'compare' => 'BETWEEN', 'type' => 'DECIMAL(3,2)', 'awf_rating_filter' => true, ); } return $query; } public function set_wc_post__in( $postin ) { if( isset( $this->query->awf['onsale'] ) ) { $onsale_posts = array_merge( array( 0 ), wc_get_product_ids_on_sale() ); if( empty( $postin ) ) { $postin = $onsale_posts; } else { $postin = array_intersect( $postin, $onsale_posts ); } } return $postin; } public function set_sc_post__in( &$args ) { if ( ! empty( $this->query->awf['search'] ) ) { $data_store = WC_Data_Store::load( 'product' ); $posts = $data_store->search_products( wc_clean( wp_unslash( $this->query->awf['search'] ) ), '', true, true ); if( ! empty( $args['p'] ) ) { $posts = array_intersect( $posts, array( $args['p'] ) ); unset( $args['p'] ); } if( ! empty( $args['post__in'] ) && is_array( $args['post__in'] ) ) { $posts = array_intersect( $posts, $args['post__in'] ); } $posts = array_unique( array_merge( $posts, array( 0 ) ) ); $args['post__in'] = $posts; } return; } public function set_default_visibility( &$tax_query ) { $tax_query[] = array( 'taxonomy' => 'product_visibility', 'terms' => 'exclude-from-catalog', 'field' => 'name', 'operator' => 'NOT IN', 'include_children' => false, ); } public function set_visibility_instock( &$tax_query ) { $tax_query[] = array( 'taxonomy' => 'product_visibility', 'terms' => array( 'outofstock' ), 'field' => 'name', 'operator' => 'NOT IN', 'include_children' => false, ); } public function set_visibility_outofstock( &$tax_query ) { $tax_query[] = array( 'taxonomy' => 'product_visibility', 'terms' => array( 'outofstock' ), 'field' => 'name', 'operator' => 'IN', 'include_children' => false, ); } public function set_visibility_featured( &$tax_query ) { $tax_query[] = array( 'taxonomy' => 'product_visibility', 'terms' => 'featured', 'field' => 'name', 'operator' => 'IN', 'include_children' => false, ); } public function wc_query( $query, $wc_query ) { return; } public function after_wc_query( $query ) { return; } public function set_shop_columns() { return $this->awf_settings['shop_columns']; } public function set_products_per_page( $ppp ) { if( isset( $this->query->awf['ppp'] ) ) { $ppp = $this->query->awf['ppp'] = (int) $this->query->awf['ppp']; // } else if(! empty( $this->awf_settings['ppp_default'] ) ) { $ppp = $this->awf_settings['ppp_default']; } return $ppp; } public function adjust_wc_pagination( $args ) { if( ! empty( $this->is_sc_page ) ) { $args['base'] = esc_url_raw( add_query_arg( array( 'product-page' => '%#%' ), $this->current_url ) ); } else { $args['base'] = esc_url_raw( add_query_arg( array( 'paged' => '%#%' ), $this->current_url ) ); } $args['add_args'] = $_GET; return $args; } public function add_awf_sc_class( $attrs ) { if( empty( $attrs['class'] ) ) { $attrs['class'] = 'awf-sc'; } else { $attrs['class'] .= ' awf-sc'; } return $attrs; } public function insert_sc_ajax_vars( $attrs ) { foreach( $attrs as $name => $value ) { echo ''; } } public function display_shortcode_no_results_message( $attrs ) { echo '

', __( 'No products were found matching your selection.', 'annasta_filters' ), '

'; } public function redirect_archives() { if( is_product_category() && empty( $this->awf_settings['cat_views_on'] ) ) { $queried_object = get_queried_object(); wp_redirect( add_query_arg( $this->tax_var_names['product_cat'], $queried_object->slug, $this->shop_url ) ); die; } elseif( is_product_tag() ) { $queried_object = get_queried_object(); wp_redirect( add_query_arg( $this->tax_var_names['product_tag'], $queried_object->slug, $this->shop_url ) ); die; } } public function enqueue_scripts( $hook ) { wp_enqueue_style( 'awf-nouislider', A_W_F_PLUGIN_URL . '/styles/nouislider.min.css', array(), A_W_F::$plugin_version ); if ( 'yes' === get_option( 'awf_pretty_scrollbars' ) ) { wp_enqueue_style( 'awf-pretty-scrollbars', A_W_F_PLUGIN_URL . '/styles/perfect-scrollbar.css' ); wp_enqueue_script( 'awf-pretty-scrollbars', A_W_F_PLUGIN_URL . '/code/js/perfect-scrollbar.min.js', array(), A_W_F::$plugin_version ); } wp_enqueue_style( 'awf', A_W_F_PLUGIN_URL . '/styles/awf.css', false, A_W_F::$plugin_version ); $uploads_dir = wp_upload_dir(); if( file_exists( $uploads_dir['basedir'] . '/annasta-filters/style-options.css' ) ) { $style_options_url = $uploads_dir['baseurl'] . '/annasta-filters/style-options.css'; if ( is_ssl() ) { $style_options_url = str_replace( 'http://', 'https://', $style_options_url ); } wp_enqueue_style( 'awf-style-options', $style_options_url, array( 'awf' ), A_W_F::$plugin_version ); } wp_enqueue_script( 'awf-nouislider', A_W_F_PLUGIN_URL . '/code/js/nouislider.min.js' ); wp_enqueue_script( 'awf', A_W_F_PLUGIN_URL . '/code/js/awf.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-sortable', 'awf-nouislider' ), A_W_F::$plugin_version ); wp_localize_script( 'awf', 'awf_data', $this->build_js_data() ); } public function enqueue_premium_scripts( $hook ) { wp_enqueue_script( 'awf-premium', A_W_F_PLUGIN_URL . '/code/js/awf-premium.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-sortable' ), A_W_F::$plugin_version ); } private function build_js_data() { $shop_url_pieces = explode( '?', $this->shop_url ); if( ! isset( $shop_url_pieces[1] ) ) { $shop_url_pieces[1] = ''; } $js_data = array( 'shop_url' => $shop_url_pieces[0], 'ajax_url' => admin_url( 'admin-ajax.php' ), 'query' => $this->filter_on ? $_GET : wp_parse_args( $shop_url_pieces[1] ), 'redirect_ajax' => ! $this->filter_on, 'product_counts' => new ArrayObject(), 'products_wrapper' => get_option( 'awf_products_html_wrapper', '#main' ), ); if ( $this->permalinks_on ) { $js_data['permalinks'] = 'yes'; if( $this->filter_on ) { if( ! empty( $this->awf_settings['cat_views_on'] ) && isset( $this->query->tax['product_cat'] ) ) { $js_data['query']['product_cat'] = implode( ',', $this->query->tax['product_cat'] ); } } if ( ! empty( $this->awf_settings['cat_views_on'] ) ) { $js_data['cat_url'] = $this->cat_url_template; } } if( false !== $this->is_sc_page ) { $js_data['sc_page'] = $this->is_sc_page; } elseif( ! empty( $this->shop_on_frontpage ) ) { $js_data['shop_on_frontpage'] = 'yes'; } unset( $js_data['query']['paged'] ); $js_data['query'] = new ArrayObject( $js_data['query'] ); return $js_data; } public function display_shortcode( $parameters ) { return $this->get_preset_html( $parameters['preset_id'], $parameters['shortcode_id'] ); } public function display_widget( $preset_id, $parameters ) { echo $this->get_preset_html( intval( $preset_id ), $parameters['id'] ); } private function get_preset_html( $preset_id, $caller_id ) { if( ! isset( A_W_F::$presets[$preset_id] ) ) return; if( is_null( $this->page_associations ) ) { $this->build_page_associations(); ksort( $this->query->tax ); ksort( $this->query->awf ); array_walk( $this->query->tax, function( &$value, $key ) { if( is_array( $value ) ) { sort( $value ); } }); array_walk( $this->query->awf, function( &$value, $key ) { if( is_array( $value ) ) { sort( $value ); } }); $awf_for_cache = $this->query->awf; unset( $awf_for_cache['orderby'] ); unset( $awf_for_cache['ppp'] ); $this->counts_cache_name = 'awf_counts_' . md5( wp_json_encode( $this->query ) ); $this->counts = get_transient( $this->counts_cache_name ); if( false === $this->counts ) { $this->counts = array(); } } $associated = false; foreach( A_W_F::$presets[$preset_id]['associations'] as $association_id ) { if( $this->in_page_associations( $association_id ) ) { $associated = true; break; } } if( ! $associated ) { return; } $preset_id = apply_filters( 'awf_display_preset_id', $preset_id ); $preset = new A_W_F_preset_frontend( $preset_id, $caller_id ); return $preset->get_html(); } private function build_page_associations() { $this->page_associations = array(); foreach( $this->query->tax as $taxonomy => $terms ) { foreach( $terms as $slug ) { if( false !== ( $term = get_term_by( 'slug', $slug, $taxonomy ) ) ) { $this->add_associations( $term, $taxonomy ); } } } if( false !== $this->is_wp_page ) { $this->page_associations['wp_page'][0][0] = $this->is_wp_page; } } private function add_associations( $term, $taxonomy ) { if( $term->parent > 0 ) { if( false !== ( $parent = get_term_by( 'id', $term->parent, $taxonomy ) ) ) { $this->add_associations( $parent, $taxonomy ); } } if( ! isset( $this->page_associations[$taxonomy] ) || ! isset( $this->page_associations[$taxonomy][$term->parent] ) || ! in_array( $term->slug, $this->page_associations[$taxonomy][$term->parent] ) ) { $this->page_associations[$taxonomy][$term->parent][] = $term->slug; } return; } public function in_page_associations( $association_id ) { if( ( $association_id === 'all' ) || ( ( $association_id === 'shop-pages' ) && ( is_shop() || is_product_category() ) ) || ( ( $association_id === 'cat-pages' ) && is_product_category() ) ) { return true; } $association = explode( '--', $association_id ); if( ! isset( $this->page_associations[$association[0]] ) ) { return false; } foreach( $this->page_associations[$association[0]] as $term ) { if( in_array( $association[1], $term ) ) { return true; } } return false; } public function get_url_query() { $url_query = array(); foreach( $this->query->tax as $var => $value ) { $url_query[$this->tax_var_names[$var]] = implode( ',', $value ); } foreach( $this->query->awf as $var => $value ) { $url_query[$this->awf_var_names[$var]] = $value; } if( ! empty( $this->shop_on_frontpage ) && empty( $this->is_sc_page ) && ( ! empty( $this->query->awf['orderby'] ) || ! empty( $this->query->awf['search'] ) ) ) { $url_query['post_type'] = 'product'; } return $url_query; } public function redirect_single_search_result() { if ( 'yes' === get_option( 'awf_redirect_search', false ) ) { return true; } return false; } public function display_no_results_msg( $attrs ) { echo '

' . esc_html__( 'No products were found matching your selection', 'annasta-filters' ) . '

'; } public function adjust_shop_title( $title ) { if( isset( $this->query->tax['product_cat'] ) ) { $title = $this->cat_names; } else { if ( is_search() ) { $title = get_the_title( $this->shop_page_id ); } } return $title; } public function adjust_document_title( $title ) { /* $title = array( 'title', 'page', 'tagline', 'site' ) */ if ( is_shop() ) { if ( is_search() ) { $title['title'] = get_the_title( $this->shop_page_id ); } } else if ( is_product_category() ) { $title['title'] = get_the_title( $this->shop_page_id ) . ': ' . $this->cat_names; } return $title; } public function adjust_breadcrumbs( $crumbs, $class ) { if( ! ( is_shop() || is_product_category() ) ) { return $crumbs; } if( $this->filter_on ) { $crumbs = array(); $crumbs[] = array( single_post_title( '', false ), home_url() ); $crumbs[] = array( get_the_title( $this->shop_page_id ), $this->shop_url ); if( ! empty( $this->cat_names ) ) { $crumbs[] = array( $this->cat_names ); } } return $crumbs; } public function update_counts_cache() { if( true === $this->update_counts_cache && ! empty( $this->counts ) ) { set_transient( $this->counts_cache_name, $this->counts, DAY_IN_SECONDS * 30 ); } } private final function __clone() {} private final function __wakeup() {} public static function get_instance() { if( is_null( self::$instance ) ) { self::$instance = new self(); } return self::$instance; } } } ?>