';
iwptp_parse_style_2($row);
}
}
return $markup;
}
function iwptp_parse_ctx_2($element, $elm_tpl, $elm_type, $product = false)
{
if (!$elm_tpl || !file_exists($elm_tpl)) {
return;
}
extract($element);
ob_start();
if (empty($html_class)) {
$html_class = '';
}
$html_class .= ' iwptp-' . $element['id'];
include $elm_tpl;
return ob_get_clean();
}
/**
* Error checking for shortcode - in case user has made some mistake
*/
function iwptp_sc_error_checks($table_data, $atts)
{
$message = '';
if (empty($table_data)) {
$message = __('No product table settings were found for this id. Please try clicking the "Save settings" button at the bottom of the table editor page to save your table settings.', 'advanced-product-table-for-woocommerce');
} else if ((empty($table_data['columns']['laptop']) || !is_array($table_data['columns']['laptop']) || !count($table_data['columns']['laptop'])) && (empty($atts['form_mode']))) {
$message = __('It looks like you haven\'t set any laptop columns for your product table. So, without any columns, your table has no content to display.', 'advanced-product-table-for-woocommerce');
} else if (iwptp_device_columns_empty($table_data['columns']['laptop']) && empty($atts['form_mode'])) {
$message = __('While you have created at least one column in the Laptop Columns section for this table, it seems you have not created any elements in the columns. Please create at least one element in at least one Laptop Column for this table, then save your table settings and reload this page to see your table.', 'advanced-product-table-for-woocommerce');
}
if ($message && (!$table_data || current_user_can('edit_iwptp_product_table', intval($table_data['id'])))) {
return '
' . IWPTPL_LABEL . '' . $message . '
';
} else {
return false;
}
}
/**
* Error checking for shortcode - in case user has made some mistake
*/
function iwptp_device_columns_empty($device_columns)
{
$no_element = true;
foreach ($device_columns as $column) {
// iterate rows
//-- heading
foreach ($column['heading']['content'] as $row) {
if (!empty($row['elements']) && count($row['elements'])) {
$no_element = false;
}
}
//-- cell
foreach ($column['cell']['template'] as $row) {
if (!empty($row['elements']) && count($row['elements'])) {
$no_element = false;
}
}
}
return $no_element;
}
function iwptp_get_cheapest_variation($product, $available_variations)
{
$lowest_price = false;
$variation_id = false;
foreach ($available_variations as $variation_details) {
if (false === $lowest_price || $variation_details['display_price'] < $lowest_price) {
$lowest_price = $variation_details['display_price'];
$variation_id = $variation_details['variation_id'];
}
}
return wc_get_product($variation_id);
}
function iwptp_get_most_expensive_variation($product, $available_variations)
{
$highest_price = false;
$variation_id = false;
foreach ($available_variations as $variation_details) {
if (false === $highest_price || $variation_details['display_price'] > $highest_price) {
$highest_price = $variation_details['display_price'];
$variation_id = $variation_details['variation_id'];
}
}
return wc_get_product($variation_id);
}
function iwptp_woocommerce_available_variation_filter($variation_details, $product, $variation)
{
global $iwptp_table_data;
foreach ($iwptp_table_data['columns'] as $key => $column) {
$variation_details['column_' . $key] = iwptp_parse_2($column['template'], $product, $variation, $variation_details);
}
return $variation_details;
}
function iwptp_update_user_filters($new_filter, $single = true)
{
$found_filter = false;
foreach ($GLOBALS['iwptp_user_filters'] as &$filter_info) {
if ($filter_info['filter'] !== $new_filter['filter']) {
continue;
}
if (in_array($new_filter['filter'], array('orderby', 'price_range', 'search', 'on_sale', 'rating', 'availability'))) {
$found_filter = true;
break;
}
// taxonomy
if (
in_array($filter_info['filter'], array('taxonomy', 'attribute', 'category')) &&
$filter_info['taxonomy'] == $new_filter['taxonomy']
) {
$found_filter = true;
break;
}
// custom field
if (
$filter_info['filter'] == 'custom_field' &&
strtolower($filter_info['meta_key']) == strtolower($new_filter['meta_key'])
) {
$found_filter = true;
break;
}
}
if ($found_filter) {
foreach ($new_filter as $key => $val) {
// add value
if ($key == 'values') {
if (!$single) {
if (!is_array($filter_info['values'])) {
$filter_info['values'] = [];
}
if ($filter_info['filter'] == 'custom_field') { // avoid duplicates
$new_filter['values'] = array_map('strtolower', $new_filter['values']);
$filter_info['values'] = array_map('strtolower', $filter_info['values']);
}
$diff = array_diff($new_filter['values'], $filter_info['values']);
$filter_info['values'] = array_merge($filter_info['values'], $diff);
} else {
$filter_info['values'] = $val;
}
// add clear label
} else if ($key == 'clear_labels_2') {
if (!$single) {
if (!is_array($filter_info['clear_labels_2'])) {
$filter_info['clear_labels_2'] = [];
}
if ($new_filter['clear_labels_2']) {
foreach ($new_filter['clear_labels_2'] as $key => $val) {
if (empty($filter_info['clear_labels_2'][$key]) || $filter_info['clear_labels_2'][$key] !== $val) {
$filter_info['clear_labels_2'][$key] = $val;
}
}
}
} else {
$filter_info['clear_labels_2'] = $val;
}
// other key
} else {
$filter_info[$key] = $val;
}
}
} else {
$GLOBALS['iwptp_user_filters'][] = $new_filter;
}
}
// Relabel items
function iwptp_relabel_items(&$items, $relabels = [])
{
foreach ($items as &$item) {
foreach ($relabels as $relabel) {
if (strtolower($item['item']) === strtolower($relabel['item'])) {
$item['label'] = iwptp_parse_2($relabel['label']);
}
}
}
return $items;
}
// iwptp price
function iwptp_price($price, $trim_zeros = false, $safari_fix = true)
{
$num = number_format(
(float) $price,
wc_get_price_decimals(),
wc_get_price_decimal_separator(),
wc_get_price_thousand_separator()
);
if (
$trim_zeros &&
strstr($num, '.')
) {
$num = rtrim(rtrim($num, '0'), '.');
}
$price = '' . $num . '';
$currency_symbol = esc_attr(get_woocommerce_currency_symbol());
// safari $ fix
if (
iwptp_get_the_browser() == "Safari" &&
$safari_fix
) {
$html_class = '';
if ($currency_symbol == '$') {
$currency_symbol = wp_kses(iwptp_icon('dollar-sign'), iwptp_allowed_html_tags());
$html_class = 'iwptp-safari-currency';
}
$currency = '' . $currency_symbol . '';
} else {
$currency = '' . $currency_symbol . '';
}
return str_replace(array('%1$s', '%2$s'), array($currency, $price), get_woocommerce_price_format());
}
// product form modal
// triggered for: variable, addons, measurement, name your price
function iwptp_include_descendant_slugs($slugs = [], $taxonomy = null)
{
if (!$slugs) {
return [];
}
if (!$taxonomy) {
$taxonomy = 'product_cat';
}
// convert slugs to term ids
$term_ids = get_terms(array(
'taxonomy' => $taxonomy,
'fields' => 'ids',
'slug' => $slugs
));
// -- include children
foreach ($term_ids as $term_id) {
$child_slugs = get_terms(array(
'taxonomy' => $taxonomy,
'fields' => 'slugs',
'child_of' => $term_id
));
if ($child_slugs) {
$slugs = array_merge($slugs, $child_slugs);
}
}
return $slugs;
}
// icon
function iwptp_icon($icon_name, $html_class = '', $style = null, $tooltip = '', $title = '', $attrs = [])
{
$output = '';
$icon_file = esc_url(IWPTPL_PLUGIN_PATH . 'assets/feather/' . $icon_name . '.svg');
if (file_exists($icon_file)) {
if ($style) {
$style = ' style="' . $style . '"';
}
$tooltip_html_class = '';
if ($tooltip) {
$tooltip_html_class = 'iwptp-tooltip';
}
if ($title) {
$title = 'title="' . htmlentities($title) . '"';
}
$attr_string = '';
if ($attrs) {
$attr_string = ' ';
foreach ($attrs as $key => $val) {
$attr_string .= $key . '="' . $val . '" ';
}
}
$output = '';
if ($tooltip) {
$output .= '' . wp_kses($tooltip, iwptp_allowed_html_tags()) . '';
}
ob_start();
include($icon_file);
$output .= ob_get_clean();
$output .= '';
}
return $output;
}
function iwptp_get_icon($icon_name, $html_class = '', $style = null, $tooltip = '', $title = '', $attrs = [])
{
return iwptp_icon($icon_name, $html_class, $style, $tooltip, $title, $attrs);
}
function iwptp_get_column_by_index($column_index = 0, $device = 'laptop', &$data = false)
{
$device_columns = iwptp_get_device_columns($device, $data);
if (!$device_columns) {
return false;
} else {
return $device_columns[$column_index];
}
}
function iwptp_sortby_get_matching_option_index($match_user_filter, $available_options)
{
if (!$available_options) {
return false;
}
foreach ($available_options as $option_index => $option) {
if (iwptp_check_sort_match($option, $match_user_filter)) {
return $option_index;
}
}
return false;
}
function iwptp_check_sort_match($option, $current_sorting)
{
// match begins from 'orderby'
if ($option['orderby'] !== $current_sorting['orderby']) {
return false;
}
// no other params needs to match for these (there is no optional 'order' for these)
if (in_array($option['orderby'], array('price', 'price-desc', 'rating', 'popularity', 'rand'))) {
return true;
}
// order must also match for remaining - title, custom field, sku, ID, etc
if (strtolower($option['order']) != strtolower($current_sorting['order'])) {
return false;
}
// enough match for these
if (in_array($option['orderby'], array('title', 'menu_order', 'id', 'sku', 'sku_num', 'date'))) {
return true;
}
// custom field
if (
!empty($option['meta_key']) &&
!empty($current_sorting['meta_key']) &&
$option['meta_key'] == $current_sorting['meta_key']
) {
return true;
}
// category
$current_sorting__focus_category = empty($current_sorting['orderby_focus_category']) ? '' : $current_sorting['orderby_focus_category'];
$current_sorting__ignore_category = empty($current_sorting['orderby_ignore_category']) ? '' : $current_sorting['orderby_ignore_category'];
$option__focus_category = empty($option['orderby_focus_category']) ? '' : $option['orderby_focus_category'];
$option__ignore_category = empty($option['orderby_ignore_category']) ? '' : $option['orderby_ignore_category'];
if (
$option['orderby'] == 'category' &&
$current_sorting__focus_category == $option__focus_category &&
$current_sorting__ignore_category == $option__ignore_category
) {
return true;
}
// attribute
$current_sorting__attribute = empty($current_sorting['orderby_attribute']) ? '' : $current_sorting['orderby_attribute'];
$current_sorting__focus_attribute_terms = empty($current_sorting['orderby_focus_attribute_terms']) ? '' : $current_sorting['orderby_focus_attribute_terms'];
$current_sorting__ignore_attribute_terms = empty($current_sorting['orderby_ignore_attribute_terms']) ? '' : $current_sorting['orderby_ignore_attribute_terms'];
$option__attribute = empty($option['orderby_attribute']) ? '' : $option['orderby_attribute'];
$option__focus_attribute_terms = empty($option['orderby_focus_attribute_terms']) ? '' : $option['orderby_focus_attribute_terms'];
$option__ignore_attribute_terms = empty($option['orderby_ignore_attribute_terms']) ? '' : $option['orderby_ignore_attribute_terms'];
if (
in_array($option['orderby'], array('attribute', 'attribute_num')) &&
$current_sorting__attribute == $option__attribute &&
$current_sorting__focus_attribute_terms == $option__focus_attribute_terms &&
$current_sorting__ignore_attribute_terms == $option__ignore_attribute_terms
) {
return true;
}
// taxonomy
$current_sorting__taxonomy = empty($current_sorting['orderby_taxonomy']) ? '' : $current_sorting['orderby_taxonomy'];
$current_sorting__focus_taxonomy_terms = empty($current_sorting['orderby_focus_taxonomy_terms']) ? '' : $current_sorting['orderby_focus_taxonomy_terms'];
$current_sorting__ignore_taxonomy_terms = empty($current_sorting['orderby_ignore_taxonomy_terms']) ? '' : $current_sorting['orderby_ignore_taxonomy_terms'];
$option__taxonomy = empty($option['orderby_taxonomy']) ? '' : $option['orderby_taxonomy'];
$option__focus_taxonomy_terms = empty($option['orderby_focus_taxonomy_terms']) ? '' : $option['orderby_focus_taxonomy_terms'];
$option__ignore_taxonomy_terms = empty($option['orderby_ignore_taxonomy_terms']) ? '' : $option['orderby_ignore_taxonomy_terms'];
if (
$option['orderby'] == 'taxonomy' &&
$current_sorting__focus_taxonomy_terms == $option__focus_taxonomy_terms &&
$current_sorting__ignore_taxonomy_terms == $option__ignore_taxonomy_terms
) {
return true;
}
}
function iwptp_get_column_sort_filter_info()
{
$field_name_prefix = $GLOBALS['iwptp_table_data']['id'] . '_';
$column_index = (int) substr(sanitize_text_field(wp_unslash($_GET[$field_name_prefix . 'orderby'])), 7); //phpcs:ignore
$device = sanitize_text_field(wp_unslash($_GET[$field_name_prefix . 'device'])); //phpcs:ignore
$order = sanitize_text_field(wp_unslash($_GET[$field_name_prefix . 'order'])); //phpcs:ignore
$column = iwptp_get_column_by_index($column_index, $device);
$filter_info = array(
'filter' => 'orderby',
);
if ($column['sorting_enabled']) {
$filter_info['orderby'] = $column['orderby'];
$filter_info['order'] = $order;
if ($column['orderby'] == 'meta_value' || $column['orderby'] == 'meta_value_num') {
$filter_info['meta_key'] = $column['meta_key']; //phpcs:ignore
}
// special case price-desc
if ($column['orderby'] == 'price' && $order == 'DESC') {
$filter_info['orderby'] = 'price-desc';
}
}
return $filter_info;
}
function iwptp_get_nav_filter($name, $second = false)
{
foreach ($GLOBALS['iwptp_user_filters'] as $filter_info) {
if ($filter_info['filter'] == $name) {
if (!$second) {
return $filter_info;
} else {
switch ($name) {
case 'custom_field':
if (strtolower($filter_info['meta_key']) == strtolower($second)) {
return $filter_info;
}
break;
default: // attribute / taxonomy / product_cat
if ($filter_info['taxonomy'] == $second) {
return $filter_info;
}
break;
}
}
}
}
return false;
}
function iwptp_clear_nav_filter($name, $second = false)
{
foreach ($GLOBALS['iwptp_user_filters'] as $key => &$filter_info) {
if ($filter_info['filter'] == $name) {
if (!$second) {
unset($GLOBALS['iwptp_user_filters'][$key]);
} else {
switch ($name) {
case 'custom_field':
if (strtolower($filter_info['meta_key']) == strtolower($second)) {
unset($GLOBALS['iwptp_user_filters'][$key]);
}
break;
case 'search':
if (
$second == 'native' &&
!empty($GLOBALS['iwptp_user_filters'][$key]['searches'])
) {
foreach ($GLOBALS['iwptp_user_filters'][$key]['searches'] as $key2 => &$search) {
if (!empty($search['native'])) {
unset($GLOBALS['iwptp_user_filters'][$key]['searches'][$key2]);
}
}
}
break;
default: // attribute / taxonomy / product_cat
if (!empty($filter_info['taxonomy']) && $filter_info['taxonomy'] == $second) {
unset($GLOBALS['iwptp_user_filters'][$key]);
}
break;
}
}
}
}
}
function iwptp_get_sorting_html_classes($col_orderby, $col_meta_key = false, $col_orderby_attribute = false, $col_orderby_taxonomy = false)
{
extract(iwptp_get_current_sorting());
$col_sorted = false;
if ($current_orderby == $col_orderby) {
if (in_array($current_orderby, array('meta_value', 'meta_value_num'))) {
if ($current_meta_key == $col_meta_key) {
$col_sorted = true;
}
} else if (in_array($current_orderby, array('attribute', 'attribute_num'))) {
if (
!empty($current_orderby_attribute) &&
!empty($col_orderby_attribute) &&
$current_orderby_attribute == $col_orderby_attribute
) {
$col_sorted = true;
}
} else if (in_array($current_orderby, array('rating'))) {
// fixed order
$current_order = 'desc';
$col_sorted = true;
} else {
$col_sorted = true;
}
} else if ($current_orderby == 'price-desc' && $col_orderby == 'price') {
$current_order = 'desc';
$col_sorted = true;
} else if (in_array($current_orderby, array('meta_value', 'meta_value_num')) && $current_meta_key == '_sku' && in_array($col_orderby, array('sku', 'sku_num'))) {
$col_sorted = true;
}
if ($col_sorted) {
// if( $col_orderby == 'rating' || $col_orderby == 'date' ){
if ($col_orderby == 'rating') {
return array(
'sorting_class' => 'iwptp-sorting-' . $current_order,
'sorting_class_asc' => 'iwptp-hide',
'sorting_class_desc' => $current_order == 'desc' ? 'iwptp-active' : 'iwptp-inactive',
);
}
return array(
'sorting_class' => 'iwptp-sorting-' . $current_order,
'sorting_class_asc' => $current_order == 'asc' ? 'iwptp-active' : 'iwptp-inactive',
'sorting_class_desc' => $current_order == 'desc' ? 'iwptp-active' : 'iwptp-inactive',
);
}
// column not sorted
return array(
'sorting_class' => '',
'sorting_class_asc' => ($col_orderby == 'rating') ? 'iwptp-hide' : 'iwptp-inactive',
'sorting_class_desc' => 'iwptp-inactive',
);
}
function iwptp_get_current_sorting()
{
$sorting = iwptp_get_nav_filter('orderby');
$current_sorting = [];
foreach (iwptp_get_nav_filter('orderby') as $key => $val) {
$current_sorting['current_' . $key] = ($key == 'order') ? strtolower($val) : $val;
}
return $current_sorting;
}
function iwptp_get_column_sorting_info($col_index, $device = 'laptop')
{
$col_index = (int) $col_index;
if (!in_array($device, array('laptop', 'tablet', 'phone'))) {
$device = 'laptop';
}
// rows
if (!empty($GLOBALS['iwptp_table_data']['columns'][$device][$col_index]['heading']['content'])) {
foreach ($GLOBALS['iwptp_table_data']['columns'][$device][$col_index]['heading']['content'] as $row) {
// elements
foreach ($row['elements'] as $element) {
if ($element['type'] == 'sorting') {
return $element;
}
}
}
}
return NULL;
}
/* get table data from post or cache */
function iwptp_get_table_data($id = false, $context = 'view')
{
if ($id) {
// get true iwptp post id
$true_id = $id;
if (FALSE !== strpos($id, '-')) {
$true_id = substr($id, 0, strpos($id, '-'));
}
if (get_post_type($id) !== 'iwptp_product_table') {
return false;
}
$table_data = iwptp_sanitize_array(json_decode(get_post_meta($true_id, 'iwptp_data', true), true)); //phpcs:ignore
$table_data['id'] = $id;
return apply_filters('iwptp_data', $table_data, $context);
} else {
// return current cached table
return !empty($GLOBALS['iwptp_table_data']) ? $GLOBALS['iwptp_table_data'] : false;
}
}
// get price with filters applied
function iwptp_get_price_to_display($product = null)
{
if (!$product) {
global $product;
}
if (apply_filters('iwptp_product_is_on_sale', $product->is_on_sale(), $product)) {
$price = apply_filters('iwptp_product_get_sale_price', $product->get_sale_price(), $product);
} else {
$price = apply_filters('iwptp_product_get_regular_price', $product->get_regular_price(), $product);
}
return $price;
}
/* columns related */
function iwptp_get_device_columns($device, &$data = false)
{
if (!$data) {
$data = &$GLOBALS['iwptp_table_data'];
}
return !empty($data['columns'][$device]) ? $data['columns'][$device] : false;
}
/* columns related */
function iwptp_get_device_columns_2($device, &$data = false)
{
if (!$data) {
$data = &$GLOBALS['iwptp_table_data'];
}
return !empty($data['columns'][$device]) ? $data['columns'][$device] : false;
}
/* elements related */
function iwptp_get_shortcode_element_manager($shortcode_tag)
{
if ('_filter' == substr($shortcode_tag, -7) || in_array($shortcode_tag, array('sort_by', 'result_count'))) {
return 'navigation';
} else {
return 'column';
}
}
function iwptp_get_column_elements($data = false)
{
if (!$data) {
$data = iwptp_get_table_data();
}
return $data['elements']['column'];
}
function iwptp_get_navigation_elements($data = false)
{
if (!$data) {
$data = iwptp_get_table_data();
}
return $data['elements']['navigation'];
}
// returns references for specific nav filter type
function iwptp_get_nav_elms_ref($type = false, &$data = false)
{
if (!$data) {
$data = iwptp_get_table_data();
}
$navigation = &$data['navigation']['laptop'];
$rows = array(&$navigation['left_sidebar'][0]); // single BE row
if (!empty($navigation['header']['rows']) && is_array($navigation['header']['rows'])) {
foreach ($navigation['header']['rows'] as &$header_row) {
foreach ($header_row['columns'] as &$column) {
$rows[] = &$column['template'][0]; // append header BE rows
}
}
}
// iterate combined rows from sidebar and header
$elements = [];
foreach ($rows as &$row) {
if (!empty($row['elements'])) {
foreach ($row['elements'] as &$element) {
if (
$type &&
$type !== $element['type']
) {
continue;
}
$elements[] = &$element;
}
}
}
return $elements;
}
// returns references for column elements of a type
function iwptp_get_col_elms_ref($type, &$data)
{
$elements = [];
foreach ($data['columns'] as &$device) {
if (empty($device)) {
continue;
}
foreach ($device as &$column) {
foreach ($column['cell']['template'] as &$template_row) {
foreach ($template_row['elements'] as &$element) {
if (
$type &&
$type !== $element['type']
) {
continue;
}
$elements[] = &$element;
}
}
}
}
return $elements;
}
// global settings for plugin
function iwptp_update_settings_data()
{
$data = iwptp_sanitize_array(json_decode(stripslashes(get_option('iwptp_settings', '')), true));
$data['version'] = IWPTPL_VERSION;
$data['timestamp'] = time();
// update meta
update_option('iwptp_settings', addslashes(wp_json_encode($data)));
return $data;
}
$iwptp_device = null;
function iwptp_get_device()
{
global $iwptp_device;
if (!empty($iwptp_device)) {
return $iwptp_device;
}
if (!class_exists('Mobile_Detect')) {
require(IWPTPL_PLUGIN_PATH . 'vendor/Mobile_Detect.php');
}
$mobile_detect = new Mobile_Detect;
$device = 'laptop';
if (
method_exists($mobile_detect, 'isTablet') &&
$mobile_detect->isTablet()
) {
$device = 'tablet';
} else if ($mobile_detect->isMobile()) {
$device = 'phone';
}
$iwptp_device = $device;
return $iwptp_device;
}
function iwptp_get_shop_table_id()
{
$shop_table_id = false;
$shop_vars = iwptp_archive_override__get_table_vars('shop');
if ($shop_vars['iwptp_table_id'] == 'custom') {
$shop_table_id = iwptp_extract_id_from_shortcode($shop_vars['iwptp_custom_table']);
} else {
$shop_table_id = $shop_vars['iwptp_table_id'];
}
return $shop_table_id;
}
function iwptp_get_the_browser()
{
global $iwptp_browser;
if (!empty($iwptp_browser)) {
return $iwptp_browser;
}
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) //phpcs:ignore
$iwptp_browser = 'Internet explorer';
elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident') !== false) //phpcs:ignore
$iwptp_browser = 'Internet explorer';
elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false) //phpcs:ignore
$iwptp_browser = 'Mozilla Firefox';
elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false) //phpcs:ignore
$iwptp_browser = 'Google Chrome';
elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false) //phpcs:ignore
$iwptp_browser = "Opera Mini";
elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== false) //phpcs:ignore
$iwptp_browser = "Opera";
elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) //phpcs:ignore
$iwptp_browser = "Safari";
else
$iwptp_browser = 'Other';
return $iwptp_browser;
}
/* ajax add to cart */
add_action('wc_ajax_iwptp_cart', 'iwptp_cart');
add_action('wp_ajax_iwptp_cart', 'iwptp_cart');
add_action('wp_ajax_nopriv_iwptp_cart', 'iwptp_cart');
function iwptp_cart()
{
if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'iwptp_ajax_nonce')) {
die();
}
$in_cart = [];
$in_cart_total = [];
foreach (WC()->cart->get_cart() as $item => $values) {
if (!$values['quantity'] || !apply_filters('iwptp_permit_item_in_cart_count', TRUE, $values)) {
continue;
}
// variation
if (!empty($values['variation_id'])) {
if (empty($in_cart[$values['product_id']])) {
$in_cart[$values['product_id']] = [];
$in_cart_total[$values['product_id']] = [];
}
if (empty($in_cart[$values['product_id']][$values['variation_id']])) {
$in_cart[$values['product_id']][$values['variation_id']] = (string) $values['quantity'];
} else {
$in_cart[$values['product_id']][$values['variation_id']] = (string)((float) $in_cart[$values['product_id']][$values['variation_id']] + $values['quantity']);
}
if (empty($in_cart_total[$values['product_id']][$values['variation_id']])) {
$in_cart_total[$values['product_id']][$values['variation_id']] = $values['line_subtotal'];
} else {
$in_cart_total[$values['product_id']][$values['variation_id']] = (string)((float) $in_cart_total[$values['product_id']][$values['variation_id']] + $values['line_subtotal']);
}
// other than variation
} else {
$in_cart[$values['product_id']] = (string) $values['quantity'];
$in_cart_total[$values['product_id']] = $values['line_subtotal'];
}
}
$notice = '';
if (!($success = !wc_notice_count('error'))) {
ob_start();
wc_print_notices();
$notice = ob_get_clean();
}
wc_clear_notices();
if (!empty($_POST['iwptp_payload']['mini_cart'])) { //phpcs:ignore
$mini_cart = iwptp_sanitize_array(json_decode(sanitize_text_field(wp_unslash($_POST['iwptp_payload']['mini_cart'])), true)); //phpcs:ignore
if (!empty($mini_cart)) {
extract($mini_cart);
ob_start();
include_once(apply_filters('iwptp_template', IWPTPL_PLUGIN_PATH . 'templates/mini_cart_items.php', 'mini_cart_items.php'));
$cart_widget = ob_get_clean();
}
}
ob_start();
woocommerce_mini_cart();
$mini_cart = ob_get_clean();
$payload = [];
if (!empty($_REQUEST['_iwptp_payload'])) { //phpcs:ignore
$payload = iwptp_sanitize_array($_REQUEST['_iwptp_payload']); //phpcs:ignore
} else if (!empty($_REQUEST['iwptp_payload'])) { //phpcs:ignore
$payload = iwptp_sanitize_array($_REQUEST['iwptp_payload']); //phpcs:ignore
}
$data = array(
'success' => $success,
'cart_hash' => apply_filters('woocommerce_add_to_cart_hash', WC()->cart->get_cart_for_session() ? md5(wp_json_encode(WC()->cart->get_cart_for_session())) : '', WC()->cart->get_cart_for_session()),
'cart_subtotal' => WC()->cart->get_cart_subtotal(),
'cart_quantity' => WC()->cart->get_cart_contents_count(),
'in_cart' => $in_cart,
'in_cart_total' => $in_cart_total,
'notice' => $notice,
'payload' => $payload,
'cart_widget' => (!empty($cart_widget)) ? $cart_widget : '',
'fragments' => apply_filters(
'woocommerce_add_to_cart_fragments',
array(
'div.widget_shopping_cart_content' => '
' . $mini_cart . '
',
)
),
'cart_hash' => apply_filters('woocommerce_add_to_cart_hash', WC()->cart->get_cart_for_session() ? md5(wp_json_encode(WC()->cart->get_cart_for_session())) : '', WC()->cart->get_cart_for_session()),
'cart_quantity' => WC()->cart->get_cart_contents_count(),
);
wp_send_json($data);
}
add_filter('iwptp_template', 'iwptp_get_template_from_theme', 10, 2);
function iwptp_get_template_from_theme($location, $template)
{
if (file_exists(get_stylesheet_directory() . '/advanced-product-table-for-woocommerce/' . $template)) { // child theme
return get_stylesheet_directory() . '/advanced-product-table-for-woocommerce/' . $template;
} else if (file_exists(get_template_directory() . '/advanced-product-table-for-woocommerce/' . $template)) { // parent theme
return get_template_directory() . '/advanced-product-table-for-woocommerce/' . $template;
}
return $location;
}
function iwptp_get_table_query_string()
{
$data = iwptp_get_table_data();
$table_id = $data['id'];
$query_string_arr = [];
foreach ($_GET as $key => $val) { //phpcs:ignore
if (
!in_array(
strtolower($key),
apply_filters('iwptp_permitted_params', array('s', 'post_type'))
) &&
(empty($val) ||
0 !== strpos($key, (string) $table_id) || // table id should be key prefix
in_array(
strtolower($key),
array( // excluded
$table_id . '_sc_attrs',
// $table_id . '_paged',
$table_id . '_url',
// $table_id . '_fresh_search',
)
)
)
) {
continue;
}
if (is_array($val)) {
$imploded_val = implode('', iwptp_sanitize_array($val));
if (!$imploded_val) {
continue;
}
$val = array_unique(array_values(iwptp_sanitize_array($val)));
}
if (
0 !== strpos($key, 'search') &&
!is_array($val)
) {
// $val = htmlentities( stripslashes( $val ) );
$val = htmlentities(sanitize_text_field(wp_unslash($val)));
}
$query_string_arr[$key] = $val;
}
return add_query_arg($query_string_arr, '');
}
function iwptp_get_archive_query_string($archive, $term_id = '')
{
if ($archive == 'category') {
$term = get_term_by('id', $term_id, 'product_cat');
extract(iwptp_archive_override__get_table_vars($archive, $term->slug));
} else if ($archive == 'shop') {
extract(iwptp_archive_override__get_table_vars($archive));
}
if (!$iwptp_table_id) {
return '';
}
if (
$iwptp_table_id == 'custom' &&
$iwptp_custom_table
) {
$iwptp_table_id = iwptp_extract_id_from_shortcode($iwptp_custom_table);
}
if (is_numeric($iwptp_table_id)) {
$table_data = iwptp_get_table_data();
$table_id = $table_data['id'];
$query_string = iwptp_get_table_query_string();
parse_str(ltrim($query_string, '?'), $params);
if (isset($params[$table_id . '_product_cat'])) {
unset($params[$table_id . '_product_cat']);
}
if (isset($params[$table_id . '_paged'])) {
unset($params[$table_id . '_paged']);
}
$query_string = '?' . http_build_query($params);
// from shop
if (
empty($params[$table_id . '_from_shop']) &&
(!empty($table_data['query']['sc_attrs']['_archive']) &&
$table_data['query']['sc_attrs']['_archive'] == 'shop'
)
) {
$query_string .= '&' . $table_data['id'] . '_from_shop=true';
}
return str_replace($table_id . '_', $iwptp_table_id . '_', $query_string);
}
return '';
}
function iwptp_extract_id_from_shortcode($shortcode)
{
$id = '';
// get id from shortcode
$arr = [];
preg_match('/id\s*=\s*[\'"](.*)[\'"]/U', $shortcode, $arr);
if (!empty($arr[1])) {
$id = $arr[1];
}
// get id from name in shortcode
if (!$id) {
preg_match('/name\s*=\s*[\'"](.*)[\'"]/U', $shortcode, $arr);
if (!empty($arr[1])) {
$name = $arr[1];
$id = iwptp_get_table_id_from_name($name);
}
}
return $id;
}
function iwptp_get_grouped_product_price($product = false)
{
$prices = array(
'min_price' => 0,
'max_price' => 0
);
if (!$product) {
global $product;
}
$tax_display_mode = get_option('woocommerce_tax_display_shop');
$child_prices = [];
$children = array_filter(array_map('wc_get_product', $product->get_children()), 'wc_products_array_filter_visible_grouped');
foreach ($children as $child) {
if ('' !== $child->get_price()) {
// $child_price = 'incl' === $tax_display_mode ? wc_get_price_including_tax( $child ) : wc_get_price_excluding_tax( $child );
if (apply_filters('iwptp_product_is_on_sale', $child->is_on_sale(), $child)) {
$child_prices[] = apply_filters('iwptp_product_get_sale_price', $child->get_sale_price(), $child);
} else {
$child_prices[] = apply_filters('iwptp_product_get_regular_price', $child->get_price(), $child);
}
}
}
if (!empty($child_prices)) {
$min_price = $prices['min_price'] = min($child_prices);
$max_price = $prices['max_price'] = max($child_prices);
} else {
$min_price = '';
$max_price = '';
}
if ('' !== $min_price) {
$is_free = 0 === $min_price && 0 === $max_price;
if ($is_free) {
$prices = apply_filters('woocommerce_grouped_free_price_html', __('Free!', 'advanced-product-table-for-woocommerce'), $product);
}
} else {
$prices = apply_filters('woocommerce_grouped_empty_price_html', '', $product);
}
return $prices;
}
// make shortcode column sortable in IWPTPL All Product Tables page
add_filter('manage_edit-iwptp_product_table_sortable_columns', 'iwptp_shortcode_column_sortable');
function iwptp_shortcode_column_sortable($columns)
{
$columns['shortcode'] = 'id';
return $columns;
}
// terms for variation
add_action('wp_ajax_iwptp_get_attribute_terms', 'iwptp_get_attribute_terms_ajax');
function iwptp_get_attribute_terms_ajax()
{
if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'iwptp_ajax_nonce')) {
die();
}
if (empty($_POST['taxonomy'])) {
return false;
}
$terms = get_terms(array(
'taxonomy' => sanitize_text_field(wp_unslash($_POST['taxonomy'])), //phpcs:ignore
'hide_empty' => false,
'orderby' => 'menu_order',
));
if (is_wp_error($terms)) {
return false;
die();
}
foreach ($terms as &$term) {
$term_obj = get_term($term->term_id, sanitize_text_field(wp_unslash($_POST['taxonomy']))); //phpcs:ignore
$term->name = wp_kses($term_obj->name, iwptp_allowed_html_tags());
}
wp_send_json($terms);
}
// get matching variation from attribute_terms
function iwptp_find_matching_product_variation($product, $attributes)
{
foreach ($attributes as $key => $value) {
if (strpos($key, 'attribute_') === 0) {
continue;
}
unset($attributes[$key]);
$attributes[sprintf('attribute_%s', $key)] = $value;
}
if (class_exists('WC_Data_Store')) {
$data_store = WC_Data_Store::load('product');
return $data_store->find_matching_product_variation($product, $attributes);
} else {
return $product->get_matching_variation($attributes);
}
}
function iwptp_find_closests_matching_product_variation($product, $attributes)
{
// iterate the variations
$partial_match = false; // variation has some extra attributes
$matched_variation = false;
$variation_attributes = []; // attributes of the complete / partial variation (used for pre-set in form)
$last_attributes_diff = 100000; // extra attributes in the last partial match variation
$total_attributes = count(array_keys($attributes));
// wmpl
global $sitepress;
if (
!empty($sitepress) &&
$sitepress->get_default_language() !== $sitepress->get_current_language()
) {
$_attributes = [];
foreach ($attributes as $attr => $term_slug) {
$term = get_term_by('slug', $term_slug, substr($attr, 10));
$_attributes[$attr] = $term->slug;
}
$attributes = $_attributes;
}
$variations = iwptp_get_variations($product);
foreach ($variations as $variation) {
// skip if variation has too few attributes
$total_variation_attributes = count(array_keys($variation['attributes']));
if ($total_variation_attributes < $total_attributes) {
continue;
}
// all the desired attributes must be in the variation
$match = true;
foreach ($attributes as $attribute => $term) {
// skip variation if it does not have a desired attribute / match
if (
empty($variation['attributes'][$attribute]) ||
$variation['attributes'][$attribute] !== $term
) {
$match = false;
break;
}
}
if (!$match) {
continue;
} else {
// complete match
$attributes_diff = $total_variation_attributes - $total_attributes;
if (!$attributes_diff) {
return array(
'type' => 'complete_match',
'variation' => $variation,
'variation_id' => $variation['variation_id'],
'variation_attributes' => $variation['attributes']
);
// partial match
} else if ($attributes_diff < $last_attributes_diff) {
$partial_match = $variation['variation_id'];
$variation_attributes = $variation['attributes'];
$last_attributes_diff = $attributes_diff;
$matched_variation = $variation;
}
}
}
if ($partial_match) {
return array(
'type' => 'partial_match',
'variation' => $matched_variation,
'variation_id' => $partial_match,
'variation_attributes' => $variation_attributes,
);
} else {
return false;
}
}
// get variations array for the product
$iwptp_variations_cache = [];
function iwptp_get_variations($product = '')
{
global $iwptp_variations_cache;
if (gettype($product) !== 'object') {
$product = wc_get_product($product);
}
$id = $product->get_id();
if (!empty($iwptp_variations_cache[$id])) {
return $iwptp_variations_cache[$id];
} else {
$iwptp_variations_cache[$id] = apply_filters('iwptp_get_variations', $product->get_available_variations());
foreach ($iwptp_variations_cache[$id] as &$variation) {
if ($variation['display_price']) {
$variation['display_price'] = iwptp_price_decimal($variation['display_price']);
}
if ($variation['display_regular_price']) {
$variation['display_regular_price'] = iwptp_price_decimal($variation['display_regular_price']);
}
if (iwptp_get_the_browser() == "Safari") {
$currency_symbol = iwptp_icon('dollar-sign');
$variation['price_html'] = str_replace('$', $currency_symbol, $variation['price_html']);
}
}
return $iwptp_variations_cache[$id];
}
}
// get default variation for current product
function iwptp_get_default_variation($product)
{
if (!$default_attributes = $product->get_default_attributes()) {
return false;
}
$_default_attributes = [];
foreach ($default_attributes as $key => $value) {
$_default_attributes['attribute_' . $key] = $value;
}
return iwptp_find_closests_matching_product_variation($product, $_default_attributes);
}
// check if current variation is incomplete
function iwptp_is_incomplete_variation($product, $variation)
{
foreach ($product->get_variation_attributes() as $attribute => $terms) {
if (substr($attribute, 0, 3) !== 'pa_') { // custom attribute
$attribute = sanitize_title($attribute);
}
if (empty($variation['attributes']['attribute_' . $attribute])) {
return true;
}
}
return false;
}
// check if all variations are out of stock
function iwptp_all_variations_out_of_stock($product_id)
{
$product = wc_get_product($product_id);
$children = $product->get_children();
$out_of_stock = true;
foreach ($children as $variation_id) {
$variation = wc_get_product($variation_id);
if ($variation->is_in_stock()) {
$out_of_stock = false;
break;
}
}
return $out_of_stock;
}
/* clear product transients */
add_action('before_delete_post', 'iwptp_clear_product_transients');
add_action('save_post', 'iwptp_clear_product_transients');
function iwptp_clear_product_transients($post_id)
{
if (get_post_type($post_id) == 'product') {
delete_transient('iwptp_variations_' . $post_id);
}
}
// duplicate post
add_filter('post_row_actions', 'iwptp_duplicate_post_link', 10000, 2);
function iwptp_duplicate_post_link($actions, $post)
{
if (current_user_can('edit_posts') && $post->post_type == 'iwptp_product_table') {
$actions['duplicate'] = 'Duplicate table';
}
return $actions;
}
// gets the required filter from nav -- recursive
function iwptp_check_if_nav_has_filter($arr, $type, $second = false)
{
if (null === $arr) {
$arr = iwptp_get_table_data();
}
foreach ($arr as $key => &$val) {
if (
$key === 'type' &&
$val === $type &&
(!$second ||
$type === 'taxonomy_filter' && $second === $arr['taxonomy'] ||
$type === 'attribute_filter' && $second === $arr['attribute_name']
)
) {
return true;
} else if (
gettype($val) == 'array' &&
TRUE === iwptp_check_if_nav_has_filter($val, $type, $second)
) {
return true;
}
}
}
// ensure search settings are attached
add_filter('iwptp_settings', 'iwptp_settings__search', 2, 10);
function iwptp_settings__search($data, $ctx)
{
if ($ctx == 'view') {
return $data;
}
// attribute integrity
$attributes = [];
foreach (wc_get_attribute_taxonomies() as $attribute) {
$match = false;
if (isset($data['search']['attribute'])) {
foreach ($data['search']['attribute']['items'] as $item) {
if ($item['item'] === $attribute->attribute_name) {
$attributes[] = $item;
$match = true;
break;
}
}
}
if (!$match) {
$attributes[] = array(
'item' => $attribute->attribute_name,
'label' => $attribute->attribute_label,
'enabled' => true,
'custom_rules_enabled' => false,
'rules' => array(
'phrase_exact_enabled' => true,
'phrase_exact_score' => 100,
'phrase_like_enabled' => true,
'phrase_like_score' => 60,
'keyword_exact_enabled' => true,
'keyword_exact_score' => 40,
'keyword_like_enabled' => true,
'keyword_like_score' => 20,
)
);
}
}
$data['search']['attribute']['items'] = $attributes;
// custom field integrity
if (!isset($data['search']['custom_field'])) {
$data['search']['custom_field'] = [];
}
if (!isset($data['search']['custom_field']['items'])) {
$data['search']['custom_field']['items'] = [];
}
$custom_fields = [];
foreach (iwptp_get_product_custom_fields() as $meta_name) {
$match = false;
// get previous settings
foreach ($data['search']['custom_field']['items'] as $item) {
if ($item['item'] == $meta_name) {
$custom_fields[] = $item;
$match = true;
break;
}
}
// generate fresh settings
if (!$match) {
$custom_fields[] = array(
'item' => $meta_name,
'label' => $meta_name,
'enabled' => true,
'custom_rules_enabled' => false,
'rules' => array(
'phrase_exact_enabled' => true,
'phrase_exact_score' => 80,
'phrase_like_enabled' => true,
'phrase_like_score' => 60,
'keyword_exact_enabled' => true,
'keyword_exact_score' => 40,
'keyword_like_enabled' => true,
'keyword_like_score' => 20,
)
);
}
}
$data['search']['custom_field']['items'] = $custom_fields;
return $data;
}
function iwptp_get_product_custom_fields()
{
if (!empty($GLOBALS['IWPTPL_CF'])) {
return $GLOBALS['IWPTPL_CF'];
}
global $wpdb;
// product ids
$query = "SELECT ID FROM $wpdb->posts WHERE post_type='product'";
$product_ids = $wpdb->get_col($query); //phpcs:ignore
// custom fields
$query = "SELECT DISTINCT meta_key FROM $wpdb->postmeta meta WHERE post_id IN (" . implode(", ", $product_ids) . ")";
$custom_fields = [];
foreach ($wpdb->get_col($query) as $meta_name) { //phpcs:ignore
if (
'_' == substr($meta_name, 0, 1) ||
'total_sales' == $meta_name
) {
continue;
} else {
$custom_fields[] = $meta_name;
}
}
$GLOBALS['IWPTPL_CF'] = $custom_fields;
return $custom_fields;
}
function iwptp_general_placeholders__parse($str, $source = false)
{
if (function_exists('iwptp_general_placeholders__parse__pro')) {
return iwptp_general_placeholders__parse__pro($str, $source);
} else {
return $str;
}
}
// iterate over arr and refresh all ids
function iwptp_new_ids(&$arr, $fresh = true)
{
if ($fresh) {
$GLOBALS['iwptp_new_ids_id'] = time() + wp_rand(0, 100000);
}
global $iwptp_new_ids_id;
foreach ($arr as $key => &$val) {
if ($key === 'id') {
$val = ++$iwptp_new_ids_id;
} else if (gettype($val) == 'array') {
iwptp_new_ids($val, false);
}
}
}
// presets
if (file_exists(IWPTPL_PLUGIN_PATH . 'presets/presets.php')) {
require_once(IWPTPL_PLUGIN_PATH . 'presets/presets.php');
}
// skip default relabels
function iwptp_is_default_relabel($rule)
{
if (
!$rule['label'][0]['elements'] ||
(count($rule['label'][0]['elements']) == 1 &&
!empty($rule['label'][0]['elements'][0]['text']) &&
$rule['label'][0]['elements'][0]['text'] == "[term]"
)
) {
return true;
}
}
// replace < and > with htmlentities
function iwptp_esc_tag($text)
{
return str_replace('>', '>', str_replace('<', '<', $text));
}
function iwptp_option($val, $label)
{
?>
" readonly />
$label) {
$new_columns[$name] = $label;
if ($name == 'title') {
$new_columns['shortcode'] = __('Shortcode', 'advanced-product-table-for-woocommerce');
}
}
return $new_columns;
}
// use default search
add_filter('iwptp_shortcode_attributes', 'iwptp_shortcode_attributes__use_default_search');
function iwptp_shortcode_attributes__use_default_search($atts)
{
if (!empty($atts['use_default_search'])) {
add_filter('iwptp_search_args', 'iwptp_search_args__use_default_search');
} else {
remove_filter('iwptp_search_args', 'iwptp_search_args__use_default_search');
}
return $atts;
}
function iwptp_search_args__use_default_search($args)
{
$args['use_default_search'] = true;
return $args;
}
// LiteSpeed Cache compatbility fix
// force js > iwptp_cart to run on every page load
add_action('wp_enqueue_scripts', 'iwptp_lightspeed_cache_compatibility_fix', 1000);
function iwptp_lightspeed_cache_compatibility_fix()
{
if (class_exists('LiteSpeed\Core')) {
wp_add_inline_script('iwptp', "iwptp_params.initially_empty_cart = false;", 'after');
}
}
// Jupiter theme v ~ 6.8 compatibility
// stop regenerating images each time Select Variation is called
add_action('iwptp_before_loop', 'iwptp_jupiter_remove_image_regen_handler');
function iwptp_jupiter_remove_image_regen_handler()
{
if (has_filter('image_downsize', 'gambit_otf_regen_thumbs_media_downsize')) {
remove_filter('image_downsize', 'gambit_otf_regen_thumbs_media_downsize', 10, 3);
add_filter('iwptp_container_close', 'iwptp_jupiter_reattach_image_regen_handler');
}
}
function iwptp_jupiter_reattach_image_regen_handler()
{
add_filter('image_downsize', 'gambit_otf_regen_thumbs_media_downsize', 10, 3);
}
// genral placeholders
// -- print options
function iwptp_general_placeholders__print_placeholders($destination = false)
{
ob_start();
?>
Available placeholders:
[product_id]: Product ID
[product_url]: Product url (no trailing slash "/")
[custom_field: name]: Replace name with custom field name
[attribute: slug]: Replace slug with attribute slug
[product_slug]: Product slug, eg: red-shoes-02
[product_sku]: Product SKU
[product_name]: Product name
[product_menu_order]: Product menu order
[site_url]: Site URL (no trailing slash "/")
[page_url]: Current page URL (no trailing slash "/")
autoembed($wp_embed->run_shortcode($excerpt)));
}
function iwptp_truncate_string($text, $limit)
{
if (str_word_count($text, 0) > $limit) {
$words = str_word_count($text, 2);
$pos = array_keys($words);
$text = substr($text, 0, $pos[$limit]);
}
return $text;
}
function iwptp_how_to_use_link($link)
{
?>
How to use
intval(get_option("{$size}_size_w")),
'height' => intval(get_option("{$size}_size_h")),
'crop' => get_option("{$size}_crop") ? get_option("{$size}_crop") : false,
);
}
if (isset($_wp_additional_image_sizes) && count($_wp_additional_image_sizes)) {
$image_sizes = array_merge($image_sizes, $_wp_additional_image_sizes);
}
return $image_sizes;
}
// WooCommerce Product Search compatibility
add_filter('iwptp_query_args', 'iwptp_compatibility__woocommerce_product_search', 10, 1);
function iwptp_compatibility__woocommerce_product_search($args)
{
if (class_exists('WooCommerce_Product_Search_Service')) {
remove_filter('pre_get_posts', 'WooCommerce_Product_Search_Service::wps_pre_get_posts', 10);
}
return $args;
}
function iwptp_price_decimal($price)
{
$unformatted_price = $price;
$negative = $price < 0;
$price = apply_filters('raw_woocommerce_price', floatval($negative ? $price * -1 : $price));
$price = apply_filters(
'formatted_woocommerce_price',
number_format(
$price,
wc_get_price_decimals(),
wc_get_price_decimal_separator(),
wc_get_price_thousand_separator()
),
$price,
wc_get_price_decimals(),
wc_get_price_decimal_separator(),
wc_get_price_thousand_separator()
);
if (apply_filters('woocommerce_price_trim_zeros', false) && wc_get_price_decimals() > 0) {
$price = wc_trim_zeros($price);
}
return $price;
}
// session
$iwptp_session_instance = false;
$iwptp_session_instance_dummy = false;
function iwptp_session()
{
if (isset($GLOBALS['iwptp_session_instance']) && $GLOBALS['iwptp_session_instance']) {
return $GLOBALS['iwptp_session_instance'];
} else {
// the dummy is useful to keep the code running when sessions is disabled
if (empty($GLOBALS['iwptp_session_instance_dummy'])) {
class IWPTPL_Session_Handler_Dummy
{
public function init() {}
public function get($arg) {}
public function set($arg) {}
}
$GLOBALS['iwptp_session_instance_dummy'] = new IWPTPL_Session_Handler_Dummy();
}
return $GLOBALS['iwptp_session_instance_dummy'];
}
}
// to disable sessions (in case of conflict)
// add define( 'IWPTPL_DISABLE_SESSION', TRUE ) to wp-config.php
add_action('plugins_loaded', 'iwptp_load_session', 100);
function iwptp_load_session()
{
if (defined('IWPTPL_DISABLE_SESSION')) {
return;
}
global $wpdb;
// create db if required
if (!get_option('iwptp_sessions_db_version')) {
$collate = $wpdb->has_cap('collation') ? $wpdb->get_charset_collate() : '';
$sql = "CREATE TABLE {$wpdb->prefix}iwptp_sessions (
session_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
session_key char(32) NOT NULL,
session_value longtext NOT NULL,
session_expiry BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (session_id),
UNIQUE KEY session_key (session_key)
) $collate";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql); //phpcs:ignore
update_option('iwptp_sessions_db_version', '1.0', TRUE);
// schedule cleanup
wp_clear_scheduled_hook('iwptp_cleanup_sessions');
wp_schedule_event(time() + (6 * HOUR_IN_SECONDS), 'twicedaily', 'iwptp_cleanup_sessions');
}
// init session
global $iwptp_session_instance;
if ($wpdb->get_var("SHOW TABLES LIKE '{$wpdb->prefix}iwptp_sessions'")) { //phpcs:ignore
if (class_exists('WooCommerce')) {
require_once(IWPTPL_PLUGIN_PATH . 'class-iwptp-session-handler.php');
$iwptp_session_instance = new IWPTPL_Session_Handler();
iwptp_session()->init();
}
}
}
add_action('iwptp_cleanup_sessions', 'iwptp_cleanup_session_data');
function iwptp_cleanup_session_data()
{
global $wpdb;
if (
class_exists('WooCommerce') &&
$wpdb->get_var("SHOW TABLES LIKE '{$wpdb->prefix}iwptp_sessions'") //phpcs:ignore
) {
require_once(IWPTPL_PLUGIN_PATH . 'class-iwptp-session-handler.php');
$session = new IWPTPL_Session_Handler();
$session->cleanup_sessions();
}
}
function iwptp_get_post_meta_min_max($custom_field)
{
$non_numeric_is_zero = false;
global $wpdb;
$query = $wpdb->prepare("SELECT $wpdb->postmeta.meta_value FROM $wpdb->postmeta WHERE $wpdb->postmeta.meta_key = %s ORDER BY ($wpdb->postmeta.meta_value + 0) ASC ", array($custom_field)); //phpcs:ignore
$vals = $wpdb->get_col($query); //phpcs:ignore
foreach ($vals as $key => &$value) {
$value = (float) $value;
}
$vals = array_values($vals);
if (empty($vals)) {
$min = 0;
$max = 0;
} else {
$min = $vals[0];
$max = array_slice($vals, -1)[0];
}
return array(
'min' => $min + 0,
'max' => $max + 0
);
}
function iwptp_get_translation($mixed)
{
if (FALSE === strpos($mixed, ":")) {
return $mixed;
}
$chopped = array_map('trim', explode("|", $mixed));
foreach ($chopped as $translation_info) {
$translation_info_chopped = array_map('trim', explode(":", $translation_info));
if ('default' == strtolower($translation_info_chopped[0])) {
$default = $translation_info_chopped[1];
}
if (strtolower(get_locale()) == strtolower($translation_info_chopped[0])) {
$translation = $translation_info_chopped[1];
break;
}
}
if (empty($translation)) {
if ($default) {
$translation = $default;
} else {
$translation = '';
}
}
return $translation;
}
add_filter('iwptp_element', 'iwptp_content__max_width', 10, 1);
function iwptp_content__max_width($elm)
{
if (in_array($elm['type'], array('content', 'excerpt'))) {
if (
empty($elm['style']) ||
empty($elm['style']['[id]']) ||
(empty($elm['style']['[id]']['width']) &&
empty($elm['style']['[id]']['max-width'])
)
) {
if (empty($elm['html_class'])) {
$elm['html_class'] = '';
}
$elm['html_class'] .= " iwptp-content--max-width ";
}
}
return $elm;
}
// salient qty width fix
$iwptp_salient_fixed_qty_ids = [];
add_filter('iwptp_element_markup', 'iwptp_salient_qty_width_fix', 100, 2);
function iwptp_salient_qty_width_fix($markup, $element)
{
if (
!empty($element) &&
!empty($element['type']) &&
$element['type'] === 'quantity' &&
!in_array($element['id'], $GLOBALS['iwptp_salient_fixed_qty_ids'])
) {
$theme = wp_get_theme();
if ('Salient' == $theme->name || 'Salient' == $theme->parent_theme) {
if ( // force width
!empty($element['style']) &&
!empty($element['style']['[id].iwptp-display-type-input']) &&
!empty($element['style']['[id].iwptp-display-type-input']['width'])
) {
if (is_numeric($element['style']['[id].iwptp-display-type-input']['width'])) {
$width = $element['style']['[id].iwptp-display-type-input']['width'] . 'px';
} else {
$width = $element['style']['[id].iwptp-display-type-input']['width'];
}
$markup .= ' ';
} else { // default width
$markup .= ' ';
}
$GLOBALS['iwptp_salient_fixed_qty_ids'][] = $element['id'];
}
}
return $markup;
}
// IWPTPL PRO buttons, covers and markers
function iwptp_elm_type_list($element_types)
{
sort($element_types);
?>