preset_id = (int) $preset_id; $this->id = (int) $filter_id; $this->prefix = self::get_prefix( $this->preset_id, $this->id ); $this->name = get_option( $this->prefix . 'name', '' ); $this->module = get_option( $this->prefix . 'module', '' ); $this->settings = get_option( $this->prefix . 'settings', array() ); } public static function get_prefix( $preset_id, $filter_id ) { return 'awf_filter_' . $preset_id . '-' . $filter_id . '_'; } public function get_filter_terms( $sort = true ) { $terms = array(); if( 'ppp' === $this->module ) { foreach( $this->settings['ppp_values'] as $value => $label ) { $label = ( -1 === $value ) ? esc_html( $label ) : esc_html( $value . ' ' . $label ); $terms[] = ( object ) array( 'slug' => $value, 'name' => $label, 'parent' => 0, 'term_id' => -1 ); } } elseif( 'price' === $this->module || 'rating' === $this->module ) { $last_i = count( $this->settings['type_options']['range_values'] ) - 1; $decimal_separator = wc_get_price_decimal_separator(); $thousand_separator = wc_get_price_thousand_separator(); foreach( $this->settings['type_options']['range_values'] as $i => $value ) { if( $i < $last_i ) { $next_value_i = $i + 1; $next_value = 0; if( $next_value_i === $last_i ) { $next_value = $this->settings['type_options']['range_values'][$next_value_i]; } else { $next_value = $this->settings['type_options']['range_values'][$next_value_i] - $this->settings['type_options']['precision']; $next_value = round( $next_value, 2, PHP_ROUND_HALF_UP ); } $formatted_value = number_format( $value, $this->settings['type_options']['decimals'], $decimal_separator, $thousand_separator ); $formatted_next_value = number_format( $next_value, $this->settings['type_options']['decimals'], $decimal_separator, $thousand_separator ); $prefix = isset( $this->settings['style_options']['value_prefix'] ) ? $this->settings['style_options']['value_prefix'] : ''; $postfix = isset( $this->settings['style_options']['value_postfix'] ) ? $this->settings['style_options']['value_postfix'] : ''; if( $formatted_value === $formatted_next_value ) { $name = $prefix . $formatted_value . $postfix; } else { $name = $prefix . $formatted_value . $postfix . ' - ' . $prefix . $formatted_next_value . $postfix; } $terms[] = ( object ) array( 'slug' => (float) $value, 'name' => $name, 'parent' => 0, 'term_id' => -1*($i+1), 'next_value' => (float) $next_value ); } } } elseif( 'stock' === $this->module ) { $terms = array( ( object ) array( 'slug' => 'all', 'name' => esc_html__( 'All', 'annasta-filters' ), 'parent' => 0, 'term_id' => -1 ), ( object ) array( 'slug' => 'instock', 'name' => esc_html__( 'In stock', 'annasta-filters' ), 'parent' => 0, 'term_id' => -2 ), ( object ) array( 'slug' => 'outofstock', 'name' => esc_html__( 'Out of stock', 'annasta-filters' ), 'parent' => 0, 'term_id' => -3 ), ( object ) array( 'slug' => 'onbackorder', 'name' => esc_html__( 'Awaited', 'annasta-filters' ), 'parent' => 0, 'term_id' => -4 ), ); } elseif( 'featured' === $this->module ) { $terms = array( ( object ) array( 'slug' => 'yes', 'name' => esc_html__( 'Featured products', 'annasta-filters' ), 'parent' => 0, 'term_id' => -1 ), ); } elseif( 'onsale' === $this->module ) { $terms = array( ( object ) array( 'slug' => 'yes', 'name' => esc_html__( 'On sale', 'annasta-filters' ), 'parent' => 0, 'term_id' => -1 ), ); } elseif( 'taxonomy' === $this->module ) { $terms_query = array( 'taxonomy' => $this->settings['taxonomy'], 'hide_empty' => false, 'menu_order' => false, 'orderby' => 'none', ); if( $sort && isset( $this->settings['sort_by'] ) ) { if( 'admin' === $this->settings['sort_by'] ) { $terms_query['menu_order'] = true; $terms_query['orderby'] = 'name'; } else { $terms_query['orderby'] = $this->settings['sort_by']; } $terms_query['order'] = strtoupper( $this->settings['sort_order'] ); } $taxonomy_terms = get_terms( $terms_query ); if( ! is_wp_error( $taxonomy_terms ) ) { $terms = $taxonomy_terms; } } return $terms; } public function get_unexcluded_filter_terms( $sort = true ) { $all_terms = $this->get_filter_terms( $sort ); $unexcluded_terms; if( empty( $this->settings['excluded_items'] ) ) { $unexcluded_terms = $all_terms; } else { $unexcluded_terms = array(); foreach( $all_terms as $term ) { if( in_array( $term->term_id, $this->settings['excluded_items'] ) ) { continue; } $unexcluded_terms[] = $term; } } return $unexcluded_terms; } public function build_terms_by_parent( $terms ) { $terms_by_parent = array(); foreach( $terms as $term ) { if( isset( $this->settings['excluded_items'] ) && in_array( $term->term_id, $this->settings['excluded_items'] ) ) { continue; } $terms_by_parent[$term->{'parent'}][] = $term; } return $terms_by_parent; } } } ?>