$v) { $size += strlen($k) + getsize($v); } return $size; } else { return 1; } } public static function escape($value) { if (is_string($value)) { return sanitize_text_field(esc_html($value)); } } public static function parse_ext_data($file_path) { $info = array(); if (is_file($file_path)) { if (function_exists('parse_ini_file')) { $info = parse_ini_file($file_path); } if (empty($info)) { $array = file($file_path); foreach ($array as $val) { if (preg_match("#^([^=]*)=([^=]*)$#isU", $val)) { list($key, $value) = explode("=", trim($val)); $info[trim($key)] = trim($value, ' "'); } } } } return $info; } public static function strtolower($string) { if (function_exists('mb_strtolower')) { $string = mb_strtolower($string, 'UTF-8'); } else { $string = strtolower($string); } return $string; } public static function get_terms($taxonomy, $hide_empty = true, $get_childs = true, $selected = 0, $category_parent = 0) { static $collector = array(); global $APFFW; $lang_key = ""; if (class_exists('SitePress')) { $lang_key = ICL_LANGUAGE_CODE; } if (isset($collector[$taxonomy])) { return $collector[$taxonomy]; } if (isset($APFFW->settings['cache_terms']) AND $APFFW->settings['cache_terms'] == 1) { $cache_key = 'apffw_terms_cache_' . md5($taxonomy . '-' . (int) $hide_empty . '-' . (int) $get_childs . '-' . (int) $selected . '-' . (int) $category_parent . '-' . $lang_key); if (false !== ( $cats = get_transient($cache_key) )) { return $cats; } } $args = array( 'style' => 'list', 'show_count' => 0, 'hide_empty' => $hide_empty, 'use_desc_for_title' => 1, 'child_of' => 0, 'hierarchical' => true, 'title_li' => '', 'show_option_none' => '', 'number' => '', 'echo' => 0, 'depth' => 0, 'current_category' => $selected, 'pad_counts' => 0, 'taxonomy' => $taxonomy, 'walker' => 'Walker_Category'); $orderby = apply_filters('apffw_get_terms_orderby', $taxonomy); if (!empty($orderby)) { if ($orderby != $taxonomy) { $args['orderby'] = $orderby; } } $order = apply_filters('apffw_get_terms_order', $taxonomy, $orderby); if (!empty($order)) { if ($order != $taxonomy) { $args['order'] = $order; } } if (class_exists('SitePress')) { $args['lang'] = ICL_LANGUAGE_CODE; } $args = apply_filters('apffw_get_terms_args', $args); $cats_objects = get_categories($args); $cats = array(); if (!empty($cats_objects)) { foreach ($cats_objects as $value) { if (is_object($value) AND $value->category_parent == $category_parent) { $cats[$value->term_id] = array(); $cats[$value->term_id]['term_id'] = $value->term_id; $cats[$value->term_id]['slug'] = urldecode($value->slug); $cats[$value->term_id]['taxonomy'] = urldecode($value->taxonomy); $cats[$value->term_id]['name'] = $value->name; $cats[$value->term_id]['count'] = $value->count; $cats[$value->term_id]['parent'] = $value->parent; if ($get_childs) { $cats[$value->term_id]['childs'] = self::assemble_terms_childs($cats_objects, $value->term_id); } } } } if (isset($APFFW->settings['cache_terms']) AND $APFFW->settings['cache_terms'] == 1) { $period = 0; $periods = array( 0 => 0, 'hourly' => HOUR_IN_SECONDS, 'twicedaily' => 12 * HOUR_IN_SECONDS, 'daily' => DAY_IN_SECONDS, 'days2' => 2 * DAY_IN_SECONDS, 'days3' => 3 * DAY_IN_SECONDS, 'days4' => 4 * DAY_IN_SECONDS, 'days5' => 5 * DAY_IN_SECONDS, 'days6' => 6 * DAY_IN_SECONDS, 'days7' => 7 * DAY_IN_SECONDS ); if (isset($APFFW->settings['cache_terms_auto_clean'])) { $period = $APFFW->settings['cache_terms_auto_clean']; if (!$period) { $period = 0; } } set_transient($cache_key, $cats, $periods[$period]); } $collector[$taxonomy] = $cats; return $cats; } private static function assemble_terms_childs($cats_objects, $parent_id) { global $APFFW; $res = array(); foreach ($cats_objects as $value) { if ($value->category_parent == $parent_id) { $res[$value->term_id]['term_id'] = $value->term_id; $res[$value->term_id]['name'] = $value->name; $res[$value->term_id]['slug'] = urldecode($value->slug); $res[$value->term_id]['taxonomy'] = urldecode($value->taxonomy); $res[$value->term_id]['count'] = $value->count; $res[$value->term_id]['parent'] = $value->parent; $res[$value->term_id]['childs'] = self::assemble_terms_childs($cats_objects, $value->term_id); } } return $res; } public static function wpml_translate($taxonomy_info, $string = '', $index = -1) { global $APFFW; if (empty($string)) { if (is_object($taxonomy_info)) { $string = stripcslashes($APFFW->settings['custom_tax_label'][$taxonomy_info->name]); } } if (empty($string)) { if (is_object($taxonomy_info)) { if (isset($taxonomy_info->labels->name) AND!empty($taxonomy_info->labels->name)) { $string = $taxonomy_info->labels->name; } else { $string = $taxonomy_info->label; } } } $check_for_custom_label = false; if (class_exists('SitePress') OR class_exists("Polylang")) { if (class_exists('SitePress')) { $lang = ICL_LANGUAGE_CODE; } if (class_exists('Polylang')) { $lang = get_locale(); } $apffw_settings = get_option('apffw_settings'); if (isset($apffw_settings['wpml_tax_labels']) AND!empty($apffw_settings['wpml_tax_labels'])) { $translations = $apffw_settings['wpml_tax_labels']; if (isset($translations[$lang])) { foreach ($translations[$lang] as $k => $data) { $translations[$lang][self::strtolower($k)] = $data; } if (isset($translations[$lang][self::strtolower(trim($string))])) { $string = $translations[$lang][self::strtolower(trim($string))]; } else { $check_for_custom_label = TRUE; } } else { $check_for_custom_label = TRUE; } } else { $check_for_custom_label = TRUE; } } if (empty($string)) { $check_for_custom_label = FALSE; } if ($index != -1) { if (stripos($string, '+')) { $tmp = explode('+', $string); if (isset($tmp[$index])) { $string = explode('^', $tmp[$index]); $string = $string[0]; } } } return $string; } public static function price_filter($additional_taxes = "") { global $_chosen_attributes, $wpdb, $wp, $APFFW; $request = $APFFW->get_request_data(); $min_price = $APFFW->is_isset_in_request_data('min_price') ? esc_attr($request['min_price']) : ''; $max_price = $APFFW->is_isset_in_request_data('max_price') ? esc_attr($request['max_price']) : ''; $fields = ''; if (get_search_query()) { $fields .= ''; } if (!empty($_GET['post_type'])) { $fields .= ''; } if (!empty($_GET['product_cat'])) { $fields .= ''; } if (!empty($_GET['product_tag'])) { $fields .= ''; } if (!empty($_GET['orderby'])) { $fields .= ''; } if ($_chosen_attributes) { foreach ($_chosen_attributes as $attribute => $data) { $taxonomy_filter = 'filter_' . str_replace('pa_', '', $attribute); $fields .= ''; if ('or' == $data['query_type']) { $fields .= ''; } } } if (!isset($additional_taxes)) { $additional_taxes = ""; } $min = self::get_min_price($additional_taxes); $max = self::get_max_price($additional_taxes); if (wc_tax_enabled() && 'incl' === get_option('woocommerce_tax_display_shop') && !wc_prices_include_tax()) { $tax_classes = array_merge(array(''), WC_Tax::get_tax_classes()); $class_max = $max; $class_min = $min; foreach ($tax_classes as $tax_class) { if ($tax_rates = WC_Tax::get_rates($tax_class)) { $class_max = ceil($max + WC_Tax::get_tax_total(WC_Tax::calc_exclusive_tax($max, $tax_rates))); $class_min = floor($min + WC_Tax::get_tax_total(WC_Tax::calc_exclusive_tax($min, $tax_rates))); } } $min = $class_min; $max = $class_max; } $min_price = ($min_price) ?: $min; $max_price = ($max_price) ?: $max; if ($min == $max) { _e(""); return; } if ('' == get_option('permalink_structure')) { $form_action = remove_query_arg(array('page', 'paged'), add_query_arg($wp->query_string, '', home_url($wp->request))); } else { $form_action = preg_replace('%\/page/[0-9]+%', '', home_url(trailingslashit($wp->request))); } $price_slider_html = '
' . $fields . '
'; $price_slider_data = array( 'form_action' => esc_url($form_action), 'min_price' => esc_attr($min_price), 'max_price' => esc_attr($max_price), 'fields' => $fields ); $price_slider_html = apply_filters('apffw_price_slider_html', $price_slider_html, $price_slider_data); _e($price_slider_html); } public static function get_price2_filter_data($additional_taxes = '') { $apffw_settings = get_option('apffw_settings', array()); if (isset($apffw_settings['by_price']['ranges']) AND!empty($apffw_settings['by_price']['ranges'])) { global $APFFW; $res = array(); $request = $APFFW->get_request_data(); $res['selected'] = ''; if (isset($request['min_price']) AND isset($request['max_price'])) { $res['selected'] = $request['min_price'] . '-' . $request['max_price']; } $r = array(); $rc = array(); $ranges = explode(',', trim($apffw_settings['by_price']['ranges'])); $get = $request; $max = self::get_max_price(); $show_count = get_option('apffw_show_count', 0); foreach ($ranges as $value) { $key = str_replace('i', $max, $value); $tmp = explode('-', trim($value)); $wc_price_args = array(); $tmp[0] = wc_price(floatval($tmp[0]), $wc_price_args); if ($tmp[1] != 'i') { $tmp[1] = wc_price(floatval($tmp[1]), $wc_price_args); } else { $tmp[1] = '∞'; } $value = $tmp[0] . ' - ' . $tmp[1]; $v = explode('-', $key); $_GET['min_price'] = $v[0]; $_GET['max_price'] = $v[1]; if ($v[0] >= $v[1]) { continue; } $r[$key] = $value; if ($show_count) { $rc[$key] = $APFFW->dynamic_count(NULL, 'none', $additional_taxes); } else { $rc[$key] = 0; } } $_GET = $get; $ranges['options'] = $r; $ranges['count'] = $rc; $res['ranges'] = $ranges; return $res; } return array(); } public static function set_layered_nav_product_ids() { if (isset($_REQUEST['apffw_wp_query_ids'])) { if ((defined('DOING_AJAX') && DOING_AJAX) AND isset($_REQUEST['predict_ids_and_continue'])) { WC()->query->layered_nav_product_ids = sanitize_text_field($_REQUEST['apffw_wp_query_ids']); } } } public static function get_filtered_price($additional_taxes = "") { global $wpdb, $wp_the_query, $APFFW; $args = $wp_the_query->query_vars; $tax_query = isset($args['tax_query']) ? $args['tax_query'] : array(); if (is_object($wp_the_query->tax_query)) { $tax_query = $wp_the_query->tax_query->queries; } $meta_query = isset($args['meta_query']) ? $args['meta_query'] : array(); $tax_query = self::expand_additional_taxes_string($additional_taxes, $tax_query); $current_term = $APFFW->get_really_current_term(); if (!empty($current_term )) { $tax_query[] = array( 'taxonomy' => $current_term->taxonomy, 'field' => 'slug', 'terms' => $current_term->slug ); } $temp_arr = array(); if (isset($args['taxonomy']) AND isset($args[$args['taxonomy']]) AND!empty($args[$args['taxonomy']])) { $temp_arr = explode(',', $args[$args['taxonomy']]); if (!$temp_arr OR count($temp_arr) < 1) { $temp_arr = array(); } } if (!empty($args['taxonomy']) && !empty($args['term'])) { $tax_query[] = array( 'taxonomy' => $args['taxonomy'], 'terms' => (empty($temp_arr)) ? array($args['term']) : $temp_arr, 'field' => 'slug', ); } if (!empty($meta_query) AND is_array($meta_query)) { foreach ($meta_query as $key => $query) { if (!empty($query['price_filter']) || !empty($query['rating_filter'])) { unset($meta_query[$key]); } } } $meta_query = new WP_Meta_Query($meta_query); $tax_query = new WP_Tax_Query($tax_query); $meta_query_sql = $meta_query->get_sql('post', $wpdb->posts, 'ID'); $tax_query_sql = $tax_query->get_sql($wpdb->posts, 'ID'); $sql = "SELECT min( FLOOR( price_meta.meta_value + 0.0) ) as min_price, max( CEILING( price_meta.meta_value + 0.0) )as max_price FROM {$wpdb->posts} "; $sql .= " LEFT JOIN {$wpdb->postmeta} as price_meta ON {$wpdb->posts}.ID = price_meta.post_id " . $tax_query_sql['join'] . $meta_query_sql['join']; $sql .= " WHERE {$wpdb->posts}.post_type = 'product' AND {$wpdb->posts}.post_status = 'publish' AND price_meta.meta_key IN ('" . implode("','", array_map('esc_sql', apply_filters('woocommerce_price_filter_meta_keys', array('_price')))) . "') AND price_meta.meta_value > '' "; $sql .= $tax_query_sql['where'] . $meta_query_sql['where']; $sql = apply_filters('apffw_get_filtered_price_query', $sql); if (isset($APFFW->settings['price_transient']) AND $APFFW->settings['price_transient']) { $data_key = md5($sql . 'apffw'); $data = get_transient('apffw_min_max_prices'); if (!is_array($data)) { $data = array(); } if (isset($data[$data_key])) { $value = $data[$data_key]; return $value; } $prices = $wpdb->get_row($sql); $data[$data_key] = $prices; set_transient('apffw_min_max_prices', $data, 1 * 24 * 3600); } $prices = $wpdb->get_row($sql); return $prices; } public static function get_max_price($additional_taxes = "") { global $wpdb; if (version_compare(WOOCOMMERCE_VERSION, '2.6', '>')) { $prices = self::get_filtered_price($additional_taxes); $max = ceil($prices->max_price); } else { self::set_layered_nav_product_ids(); if (0 === sizeof(WC()->query->layered_nav_product_ids)) { $sql_data = array( array( 'val' => $wpdb->posts, 'type' => 'string', ), array( 'val' => $wpdb->postmeta, 'type' => 'string', ), array( 'val' => '_price', 'type' => 'string', ), ); $query_txt = self::apffw_prepare('SELECT max(meta_value + 0) FROM %1$s LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id WHERE meta_key IN ("' . implode('","', apply_filters('woocommerce_price_filter_meta_keys', array('_price'))) . '") ', $sql_data); $max = ceil($wpdb->get_var($query_txt)); } else { $sql_data = array( array( 'val' => $wpdb->posts, 'type' => 'string', ), array( 'val' => $wpdb->postmeta, 'type' => 'string', ), ); $max = ceil($wpdb->get_var( self::apffw_prepare(' SELECT max(meta_value + 0) FROM %1$s LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id WHERE meta_key IN ("' . implode('","', apply_filters('woocommerce_price_filter_meta_keys', array('_price'))) . '") AND ( %1$s.ID IN (' . implode(',', array_map('absint', WC()->query->layered_nav_product_ids)) . ') OR ( %1$s.post_parent IN (' . implode(',', array_map('absint', WC()->query->layered_nav_product_ids)) . ') AND %1$s.post_parent != 0 ) ) ', $sql_data ))); } } return $max; } public static function get_min_price($additional_taxes = "") { global $wpdb; if (version_compare(WOOCOMMERCE_VERSION, '2.6', '>')) { $prices = self::get_filtered_price($additional_taxes); $min = floor($prices->min_price); } else { self::set_layered_nav_product_ids(); if (0 === sizeof(WC()->query->layered_nav_product_ids)) { $sql_data = array( array( 'val' => $wpdb->posts, 'type' => 'string', ), array( 'val' => $wpdb->postmeta, 'type' => 'string', ), ); $min = floor($wpdb->get_var( self::apffw_prepare(' SELECT min(meta_value + 0) FROM %1$s LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id WHERE meta_key IN ("' . implode('","', apply_filters('woocommerce_price_filter_meta_keys', array('_price', '_min_variation_price'))) . '") AND meta_value != "" ', $sql_data) )); } else { $sql_data = array( array( 'val' => $wpdb->posts, 'type' => 'string', ), array( 'val' => $wpdb->postmeta, 'type' => 'string', ), ); $min = floor($wpdb->get_var( self::apffw_prepare(' SELECT min(meta_value + 0) FROM %1$s LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id WHERE meta_key IN ("' . implode('","', apply_filters('woocommerce_price_filter_meta_keys', array('_price', '_min_variation_price'))) . '") AND meta_value != "" AND ( %1$s.ID IN (' . implode(',', array_map('absint', WC()->query->layered_nav_product_ids)) . ') OR ( %1$s.post_parent IN (' . implode(',', array_map('absint', WC()->query->layered_nav_product_ids)) . ') AND %1$s.post_parent != 0 ) ) ', $sql_data ))); } } return $min; } public static function is_mobile_device() { return wp_is_mobile(); } public static function show_admin_notice($key) { global $APFFW; _e($APFFW->render_html(APFFW_PATH . 'views/notices/' . $key . '.php')); } public static function add_notice($key) { $notices = get_option('apffw_notices', array()); $is_hidden = false; if (isset($notices[$key]) AND $notices[$key] == 'hidden') { $is_hidden = true; } if (!$is_hidden) { self::$notices[] = $key; $notices[] = $key; update_option('apffw_notices', $notices); } } public static function hide_admin_notices() { if (isset($_GET['apffw_hide_notice']) && isset($_GET['_wpnonce'])) { if (!wp_verify_nonce($_GET['_wpnonce'])) { wp_die(__('Action failed. Please refresh the page and retry.', 'apffw-products-filter')); } if (!current_user_can('manage_woocommerce')) { wp_die(__('Cheatin’ huh?', 'apffw-products-filter')); } $key = self::escape(sanitize_text_field($_GET['apffw_hide_notice'])); unset(self::$notices[$key]); $notices = get_option('apffw_notices', array()); $notices[$key] = 'hidden'; update_option('apffw_notices', $notices); } } public static function draw_tooltipe($title, $tooltip_text) { if (!$tooltip_text OR empty($tooltip_text) OR $tooltip_text == 'none') { return""; } global $APFFW; if (!isset($APFFW->settings['use_tooltip'])) { $show_tooltip = 1; } else { $show_tooltip = $APFFW->settings['use_tooltip']; } if (!$show_tooltip) { return""; } $tooltip_text = self::wpml_translate(null, stripcslashes(wp_strip_all_tags($tooltip_text))); $toggle_image = ((isset($APFFW->settings['apffw_tooltip_img']) AND!empty($APFFW->settings['apffw_tooltip_img'])) ? $APFFW->settings['apffw_tooltip_img'] : APFFW_LINK . 'img/apffw_info_icon.png'); $current_id = uniqid("apffw_tooltip_content"); ?>
settings['toggle_type']) AND!empty($APFFW->settings['toggle_type'])) ? $APFFW->settings['toggle_type'] : 'text'); if ($block_is_closed) { $toggle_text = ((isset($APFFW->settings['toggle_closed_text']) AND!empty($APFFW->settings['toggle_closed_text'])) ? self::wpml_translate(null, $APFFW->settings['toggle_closed_text']) : '-'); $toggle_image = ((isset($APFFW->settings['toggle_closed_image']) AND!empty($APFFW->settings['toggle_closed_image'])) ? $APFFW->settings['toggle_closed_image'] : APFFW_LINK . 'img/plus3.png'); } else { $toggle_text = ((isset($APFFW->settings['toggle_opened_text']) AND!empty($APFFW->settings['toggle_opened_text'])) ? self::wpml_translate(null, $APFFW->settings['toggle_opened_text']) : '+'); $toggle_image = ((isset($APFFW->settings['toggle_opened_image']) AND!empty($APFFW->settings['toggle_opened_image'])) ? $APFFW->settings['toggle_opened_image'] : APFFW_LINK . 'img/minus3.png'); $condition = 'opened'; } if ($toggle_type == 'text' OR empty($toggle_image)) { ?> <?php esc_html_e('toggle', 'apffw-products-filter') ?> 0) { $size += $handlesize; } } } } closedir($handle); } return $size; } public static function expand_additional_taxes_string($additional_taxes, $res = array()) { if (!empty($additional_taxes)) { $t = explode('+', $additional_taxes); if (!empty($t) AND is_array($t)) { foreach ($t as $string) { $tmp = explode(':', $string); $tax_slug = $tmp[0]; $tax_terms = explode(',', $tmp[1]); $slugs = array(); foreach ($tax_terms as $term_id) { $term = get_term(intval($term_id), $tax_slug); if (is_object($term) AND!is_wp_error($term)) { $slugs[] = $term->slug; } } if (!empty($slugs)) { $res[] = array( 'taxonomy' => $tax_slug, 'field' => 'slug', 'terms' => $slugs ); } } } } return $res; } public static function apffw_prepare($query, $args) { global $wpdb; return $wpdb->prepare($query, $sql_val); if (is_null($query)) { return; } $sql_val = array(); $query = str_replace("'%s'", '%s', $query); $query = str_replace('"%s"', '%s', $query); $query = preg_replace('|(? $args, 'type' => 'string'); } foreach ($args as $item) { if (!is_array($item) OR!isset($item['val'])) { continue; } if (!isset($item['type'])) { $item['type'] = 'string'; } $sql_val[] = self::apffw_escape_sql($item['type'], $item['val']); } return @aprintf($query, $sql_val); } public static function apffw_escape_sql($type, $value) { switch ($type) { case'string': global $wpdb; return $wpdb->_real_escape($value); break; case'int': return intval($value); break; case'float': return floatval($value); break; default : global $wpdb; return $wpdb->_real_escape($value); } } public static function recursiveRemoval(&$array, $val) { if (is_array($array)) { foreach ($array as $key => &$arrayElement) { if (isset($arrayElement['key']) AND $arrayElement['key'] == $val) { unset($array[$key]); } if (is_array($arrayElement)) { self::recursiveRemoval($arrayElement, $val); } } } } public static function sanitize_array( $array ) { $is_html = array('override_no_products'); $is_textarea = array('init_only_on', 'custom_css_code', 'js_after_ajax_done'); $is_js = array(); foreach ($array as $key => $data) { if (is_array($data)) { self::sanitize_array($data); } else{ $key = sanitize_key( $key ); if (in_array($key, $is_html)){ $array[$key] = wp_kses_post($data); } elseif(in_array($key, $is_textarea)){ $array[$key] = sanitize_textarea_field($data); }else { $array[$key] = sanitize_text_field($data); } } } return $array; } public static function sanitize_html_fields_array($fiels){ $array = array(); foreach ($fiels as $key => $data) { $key = sanitize_text_field( $key ); $array[$key] = wp_kses($data, wp_kses_allowed_html('post')); } return $array; } }