0) { array_push($term_ancestors, $term_id); } return (array)$term_ancestors; } } /** * Get details for given term. * * @param int $term_id * @param string $taxonomy * @return mixed */ if (!function_exists('wchweb_get_term_data')) { function wchweb_get_term_data($term_id, $taxonomy) { $transient_name = 'wchweb_term_data_' . md5(sanitize_key($taxonomy) . sanitize_key($term_id)); if (false === ($term_data = get_transient($transient_name))) { $term_data = get_term($term_id, $taxonomy); set_transient($transient_name, $term_data, WCHWEB_CACHE_TIME); } return $term_data; } } /** * Get product ids for given term. * * @param int $term_id * @param string $taxonomy * @return array */ if (!function_exists('wchweb_get_term_objects')) { function wchweb_get_term_objects($term_id, $taxonomy) { global $wchweb; $unfiltered_product_ids = $wchweb->unfilteredProductIds(); $transient_name = 'wchweb_term_objects_' . md5(sanitize_key($taxonomy) . sanitize_key($term_id)); if (false === ($objects_in_term = get_transient($transient_name))) { $objects_in_term = get_objects_in_term($term_id, $taxonomy); $objects_in_term = array_intersect($objects_in_term, $unfiltered_product_ids); set_transient($transient_name, $objects_in_term, WCHWEB_CACHE_TIME); } return (array)$objects_in_term; } } /** * Get product ids for given term, it will also pull the products from its child terms. * * @param int $term_id * @param string $taxonomy * @return array */ if (!function_exists('wchweb_get_term_products')) { function wchweb_get_term_products($term_id, $taxonomy) { $products_in_term = wchweb_get_term_objects($term_id, $taxonomy); $term_childs = wchweb_get_term_childs($term_id, $taxonomy); if (is_array($term_childs) && sizeof($term_childs) > 0) { foreach ($term_childs as $term_child) { $products_in_term = array_merge($products_in_term, wchweb_get_term_objects($term_child, $taxonomy)); } } return array_unique($products_in_term); } } /** * Function for clearing old transients stored by our plugin. */ if (!function_exists('wchweb_clear_transients')) { function wchweb_clear_transients() { global $wpdb; $sql = "DELETE FROM $wpdb->options WHERE `option_name` LIKE ('%\_transient_wchweb\_%') OR `option_name` LIKE ('%\_transient_timeout_wchweb\_%')"; return $wpdb->query($sql); } } /** * wchweb_list_sub_terms function * * @param array $sub_term_args * @param bool $found * @return mixed */ if (!function_exists('wchweb_list_sub_terms')) { function wchweb_list_sub_terms($sub_term_args, $found) { global $wchweb; $filtered_product_ids = $wchweb->filteredProductIds(); $unfiltered_product_ids = $wchweb->unfilteredProductIds(); $chosen_filters = $wchweb->getChosenFilters(); extract($sub_term_args); $html = '
aaaa'; print_r($parent_args);
if (sizeof($parent_terms) > 0) {
$html .= '';
$html .= '';
// store term ids from url for this attribute
// example: attra_size=9,29,45
$term_ids = array();
if (key_exists($data_key, $url_array) && !empty($url_array[$data_key])) {
$term_ids = explode(',', $url_array[$data_key]);
}
// store the ancestor ids for this term
$term_ancestors = $chosen_filters['term_ancestors'];
$ancestors = array();
if (sizeof($term_ancestors) > 0 && key_exists($data_key, $term_ancestors)) {
foreach ($term_ancestors[$data_key] as $chosen_filter) {
foreach ($chosen_filter as $ancestor) {
array_push($ancestors, $ancestor);
}
}
}
$ancestors = array_unique($ancestors);
foreach ($parent_terms as $parent_term) {
$parent_term_id = $parent_term->term_id;
// get sub term ids for this term
$sub_term_ids = wchweb_get_term_childs($parent_term_id, $taxonomy);
// get product ids for this term
$products_in_term = wchweb_get_term_products($parent_term_id, $taxonomy);
if ($query_type === 'and') {
// count product ids those are not present in $filtered_product_ids array
$count = sizeof(array_intersect($products_in_term, $filtered_product_ids));
} else {
// count product ids those are present in $unfiltered_product_ids
$count = sizeof(array_intersect($products_in_term, $unfiltered_product_ids));
}
$force_show = false;
// if this term id is present in $term_ids array we will force
if (in_array($parent_term_id, $term_ids)) {
$force_show = true;
}
// if child term is selected we will force
elseif (sizeof($ancestors) > 0 && in_array($parent_term_id, $ancestors)) {
$force_show = true;
}
if ($count > 0 || $force_show === true) {
$found = true;
if (in_array($parent_term_id, $term_ids)) {
$html .= '- ';
} else {
$html .= '
- ';
}
$html .= '' . $parent_term->name . '';
if ($show_count === true) {
$html .= '(' . $count . ')';
}
if (($enable_hierarchy === true && $show_children_only !== true) || ($show_children_only === true && (in_array($parent_term_id, $term_ids) || $force_show === true))) {
if (sizeof($sub_term_ids) > 0) {
$sub_term_args = array(
'taxonomy' => $taxonomy,
'data_key' => $data_key,
'query_type' => $query_type,
'enable_multiple' => $enable_multiple,
'show_count' => $show_count,
'enable_hierarchy' => $enable_hierarchy,
'show_children_only' => $show_children_only,
'parent_term_id' => $parent_term_id,
'sub_term_ids' => $sub_term_ids,
'term_ids' => $term_ids,
'ancestors' => $ancestors
);
$results = wchweb_list_sub_terms($sub_term_args, $found);
$html .= $results['html'];
$found = $results['found'];
}
}
$html .= '
';
}
}
$html .= '
';
$html .= '';
}
return array(
'html' => $html,
'found' => $found
);
}
}
/**
* wchweb_dropdown_sub_terms function
*
* @param array $sub_term_args
* @param bool $found
* @param bool $depth
* @return mixed
*/
if (!function_exists('wchweb_dropdown_sub_terms')) {
function wchweb_dropdown_sub_terms($sub_term_args, $found, $depth) {
global $wchweb;
$filtered_product_ids = $wchweb->filteredProductIds();
$unfiltered_product_ids = $wchweb->unfilteredProductIds();
$chosen_filters = $wchweb->getChosenFilters();
extract($sub_term_args);
$html = '';
foreach ($sub_term_ids as $sub_term_id) {
$sub_term_data = wchweb_get_term_data($sub_term_id, $taxonomy);
if ($sub_term_data && ($sub_term_data->parent == $parent_term_id)) {
$_parent_term_id = $sub_term_data->term_id;
$_parent_term_name = $sub_term_data->name;
// get sub term ids for this term
$_sub_term_ids = wchweb_get_term_childs($_parent_term_id, $taxonomy);
// get product ids for this term
$products_in_term = wchweb_get_term_products($_parent_term_id, $taxonomy);
if ($query_type === 'and') {
// count product ids those are not present in $filtered_product_ids array
$count = sizeof(array_intersect($products_in_term, $filtered_product_ids));
} else {
// count product ids those are present in $unfiltered_product_ids
$count = sizeof(array_intersect($products_in_term, $unfiltered_product_ids));
}
$force_show = false;
if (sizeof($ancestors) > 0) {
if (in_array($_parent_term_id, $ancestors)) {
$force_show = true;
}
}
if ($count > 0 || $force_show === true) {
$found = true;
$selected = (in_array($_parent_term_id, $term_ids)) ? ' selected="selected"' : '';
if ($show_count === true) {
$html .= '';
} else {
$html .= '';
}
if (($enable_hierarchy === true && $show_children_only !== true) || ($show_children_only == true && (in_array($_parent_term_id, $term_ids) || $force_show === true))) {
if (sizeof($_sub_term_ids) > 0) {
$sub_term_args = array(
'taxonomy' => $taxonomy,
'data_key' => $data_key,
'query_type' => $query_type,
'enable_multiple' => $enable_multiple,
'show_count' => $show_count,
'enable_hierarchy' => $enable_hierarchy,
'show_children_only' => $show_children_only,
'parent_term_id' => $_parent_term_id,
'sub_term_ids' => $_sub_term_ids,
'term_ids' => $term_ids,
'ancestors' => $ancestors
);
$results = wchweb_dropdown_sub_terms($sub_term_args, $found, $depth + 1);
$html .= $results['html'];
$found = $results['found'];
}
}
}
}
}
return array(
'html' => $html,
'found' => $found
);
}
}
/**
* wchweb_dropdown_terms function
*
* @param array $attr_args
* @return mixed
*/
if (!function_exists('wchweb_dropdown_terms')) {
function wchweb_dropdown_terms($attr_args) {
global $wchweb;
$filtered_product_ids = $wchweb->filteredProductIds();
$unfiltered_product_ids = $wchweb->unfilteredProductIds();
$chosen_filters = $wchweb->getChosenFilters();
extract($attr_args);
$parent_args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false
);
if ($enable_hierarchy === true) {
$parent_args['parent'] = 0;
}
$parent_terms = get_terms($taxonomy, $parent_args);
$html = '';
$found = false;
if (preg_match('/^attr/', $data_key)) {
$attr = str_replace(array('attra-', 'attro-'), '', $data_key);
$placeholder = sprintf(__('Choose %s', 'wchweb'), $attr);
} elseif (preg_match('/^product-cat/', $data_key)) {
$placeholder = sprintf(__('Choose category', 'wchweb'));
}
if ($enable_multiple === true) {
$multiple = 'multiple="multiple"';
} else {
$multiple = '';
}
if (sizeof($parent_terms) > 0) {
// required scripts
wp_enqueue_style('wchweb-select2');
wp_enqueue_script('wchweb-select2');
if ($enable_multiple === true) {
$select_holder_class = 'wchweb-select2 wchweb-select2-multiple';
} else {
$select_holder_class = 'wchweb-select2 wchweb-select2-single';
}
$html .= '';
$html .= '';
$html .= '';
}
return array(
'html' => $html,
'found' => $found
);
}
}