'', 'content' => '');
public $only_loop = false;
public $disable_nav = false;
public $query_args = [];
public $attributes = [];
public $table_data = [];
public $search_ids = false;
public function __construct($attributes = [], $type = 'it_product_table', $table_data = [])
{
$this->type = $type;
$this->table_data = (!empty($table_data)) ? $table_data : iwptp_get_table_data();
$this->attributes = $this->parse_attributes($attributes);
}
public function get_transient_name()
{
if (!$this->transient_name) {
// initial query
$initial_query = &$GLOBALS['iwptp_table_data']['query'];
// user query
$user_query = [];
foreach ($_GET as $key => $val) { //phpcs:ignore
if (false !== strpos($key, $this->id . '_')) {
$user_query[sanitize_text_field($key)] = (is_array($val)) ? iwptp_sanitize_array($val) : sanitize_text_field($val);
}
}
// user role
$user = wp_get_current_user();
$user_roles = (array) $user->roles;
// merge all 3
$combined = array_merge($initial_query, $user_query, $user_roles);
sort($combined);
$this->transient_name = 'iwptp_' . $this->id . '_cache_' . md5(wp_json_encode($combined)) . WC_Cache_Helper::get_transient_version('product_loop');
}
return $this->transient_name;
}
public function get_cache()
{
if (!$this->caching) {
return false;
}
return get_transient($this->get_transient_name());
}
public function set_cache($markup)
{
if (!$this->caching) {
return;
}
set_transient($this->get_transient_name(), $markup, DAY_IN_SECONDS);
}
protected function get_products()
{
if (!empty($this->query_args['p']) && !empty($this->table_data['query']['sc_attrs']['ids'])) {
$this->query_args['post__in'] = array_map('trim', explode(',', $this->table_data['query']['sc_attrs']['ids']));
unset($this->query_args['p']);
}
$this->query_args = apply_filters('iwptp_query_args', $this->query_args);
if (!empty($this->table_data['query']['sc_attrs']['_return_query_args'])) {
return $this->query_args;
}
$GLOBALS['iwptp_query_args'] = $this->query_args;
$products = new WP_Query($this->query_args);
WC()->query->remove_ordering_args();
return apply_filters('iwptp_products', $products);
}
public function get_query_args()
{
return $this->product_loop('nav_markup');
}
public function get_nav_markup()
{
return $this->product_loop('nav_markup');
}
public function get_content()
{
return $this->product_loop();
}
private function lazy_load()
{
// prep vars
$tpl = plugin_dir_path(__FILE__) . 'templates/';
// lazy load?
if (
!empty($this->table_data['query']) &&
!empty($this->table_data['query']['sc_attrs']) &&
!empty($this->table_data['query']['sc_attrs']['lazy_load'])
) {
ob_start();
include $tpl . 'lazy-load.php';
return ob_get_clean();
}
return false;
}
private function ensure_device()
{
$table_id = $this->table_data['id'];
if (!empty($_GET[$table_id . '_device']) && in_array($_GET[$table_id . '_device'], array('laptop', 'tablet', 'phone'))) { //phpcs:ignore
return;
}
if (!empty($this->table_data['device'])) {
$_GET[$table_id . '_device'] = $this->table_data['device']; //phpcs:ignore
return;
}
// mobile detect library
if (!class_exists('Mobile_Detect')) {
require IWPTPL_PLUGIN_PATH . 'vendor/Mobile_Detect.php';
}
$mobile_detect = new Mobile_Detect;
// device
$tablet_device = (method_exists($mobile_detect, 'isTablet') && $mobile_detect->isTablet());
$phone_device = ($mobile_detect->isMobile() && !$tablet_device);
$laptop_device = !$tablet_device && !$phone_device;
// assign $_GET a device
$requested_device = 'laptop';
if ($tablet_device) {
if (!iwptp_get_device_columns_2('tablet')) {
$requested_device = 'laptop';
}
}
if ($phone_device) {
$requested_device = 'phone';
if (!iwptp_get_device_columns_2('phone')) {
$requested_device = 'tablet';
if (!iwptp_get_device_columns_2('tablet')) {
$requested_device = 'laptop';
}
}
}
$_GET[$table_id . '_device'] = $requested_device;
}
protected function product_loop($return = false)
{
if (!empty($this->attributes['include_users']) && is_array($this->attributes['include_users']) && !in_array(get_current_user_id(), $this->attributes['include_users'])) {
return;
}
// prep vars
$this->id = $table_id = $this->attributes['id'];
$data = &$GLOBALS['iwptp_table_data'];
$data['style_items'] = [];
$tpl = plugin_dir_path(__FILE__) . 'templates/';
$this->ensure_device();
$GLOBALS['iwptp_row_rand'] = wp_rand(0, 100000);
// lazy load
if ($lazy_load_mkp = $this->lazy_load()) {
return $lazy_load_mkp; // ... done
}
ob_start(); // for entire shortcode content
// initial query data from editor
$GLOBALS['iwptp_user_filters'] = [
//-- orderby
[
'filter' => 'orderby',
'orderby' => !empty($data['query']['orderby']) ? $data['query']['orderby'] : 'date',
'order' => !empty($data['query']['order']) ? $data['query']['order'] : 'DESC',
'meta_key' => !empty($data['query']['meta_key']) && in_array($data['query']['orderby'], array('meta_value_num', 'meta_value')) ? $data['query']['meta_key'] : '', //phpcs:ignore
'orderby_focus_category' => !empty($data['query']['orderby_focus_category']) ? $data['query']['orderby_focus_category'] : '',
'orderby_ignore_category' => !empty($data['query']['orderby_ignore_category']) ? $data['query']['orderby_ignore_category'] : '',
'orderby_attribute' => !empty($data['query']['orderby_attribute']) ? $data['query']['orderby_attribute'] : '',
'orderby_focus_attribute_term' => !empty($data['query']['orderby_focus_attribute_term']) ? $data['query']['orderby_focus_attribute_term'] : '',
'orderby_ignore_attribute_term' => !empty($data['query']['orderby_ignore_attribute_term']) ? $data['query']['orderby_ignore_attribute_term'] : '',
'orderby_attribute_include_all' => !empty($data['query']['orderby_attribute_include_all']),
'orderby_taxonomy' => !empty($data['query']['orderby_taxonomy']) ? $data['query']['orderby_taxonomy'] : '',
'orderby_focus_taxonomy_term' => !empty($data['query']['orderby_focus_taxonomy_term']) ? $data['query']['orderby_focus_taxonomy_term'] : '',
'orderby_ignore_taxonomy_term' => !empty($data['query']['orderby_ignore_taxonomy_term']) ? $data['query']['orderby_ignore_taxonomy_term'] : '',
'orderby_taxonomy_include_all' => !empty($data['query']['orderby_taxonomy_include_all']),
],
//-- out of stock
[
'filter' => 'availability',
'operator' => !empty($data['query']['hide_out_of_stock_items']) || (get_option('woocommerce_hide_out_of_stock_items', 'no') == 'yes') ? 'NOT IN' : 'ALSO',
],
];
// pre-selected filters
$sc_attrs = $data['query']['sc_attrs'];
//-- category
if (!empty($sc_attrs['category']) && strtolower(trim($sc_attrs['category'])) !== '_all') {
$category = [];
foreach (array_map('trim', explode(',', $sc_attrs['category'])) as $category_slug) {
$tt_id = get_terms(array(
'taxonomy' => 'product_cat',
'fields' => 'tt_ids',
'slug' => $category_slug,
'hide_empty' => false,
));
if ($tt_id) {
$category[] = $tt_id[0];
} else {
$category[] = 0;
}
}
$filter_info = array(
'filter' => 'category',
'taxonomy' => 'product_cat',
'values' => $category,
'operator' => 'IN',
'clear_label' => '',
'clear_labels_2' => '',
);
iwptp_update_user_filters($filter_info, false);
}
//-- search - orderby relevance
if (iwptp_maybe_apply_sortby_relevance()) {
$filter_info = array(
'filter' => 'orderby',
'orderby' => 'relevance',
'order' => 'DESC',
);
iwptp_update_user_filters($filter_info, true);
}
// operate on data before remaining user filters are applied
do_action('iwptp_before_apply_user_filters', $data);
// reset search count
$GLOBALS['iwptp_search_count'] = 0;
$GLOBALS['iwptp_nav_later'] = [];
add_filter('iwptp_navigation', [$this, 'nav_later']);
add_filter('iwptp_footer', [$this, 'nav_later']);
if (!$this->only_loop && !$this->disable_nav) {
$nav = iwptp_parse_navigation($data);
}
if (!$this->only_loop && !$this->disable_nav) {
$footer = iwptp_parse_footer($data);
}
// parse
$this->query_args = $this->parse_query_args($data);
// return query args, do not print
if (!empty($sc_attrs['_return_query_args'])) {
ob_end_clean();
return wp_json_encode($this->get_products());
}
// search
if (is_search()) {
$products = $GLOBALS['wp_query'];
if (!$products->found_posts) {
do_action('woocommerce_no_products_found');
return;
}
}
// inherit from main query
if ($this->only_loop) {
$products = $GLOBALS['wp_query'];
} else if (!empty($data['query']['sc_attrs']['product_variations']) && function_exists('iwptp_product_variations_query')) {
$products = iwptp_product_variations_query($this->query_args);
} else {
$products = $this->get_products();
}
$this->remove_ordering_args();
remove_filter('posts_where', array($this, 'search'));
$GLOBALS['iwptp_products'] = $products;
// begin container
include $tpl . 'container-open.php';
if (function_exists('wc_print_notices') && WC()->session) {
wc_print_notices();
}
iwptp_print_styles($data);
do_action('iwptp_before_loop', $this->attributes);
ob_start();
include IWPTPL_PLUGIN_PATH . 'templates/pagination_main.php';
$pagination = ob_get_clean();
// print navigation
if (!$this->only_loop) {
echo wp_kses(apply_filters('iwptp_navigation', str_replace('{pagination}', $pagination, $nav)), iwptp_allowed_html_tags());
}
echo '
';
if ($cache = $this->get_cache()) {
echo wp_kses($cache, iwptp_allowed_html_tags());
} else {
ob_start();
foreach (
[
'laptop' => iwptp_get_device_columns_2('laptop', $data),
'tablet' => iwptp_get_device_columns_2('tablet', $data),
'phone' => iwptp_get_device_columns_2('phone', $data),
] as $device => $columns
) {
$columns = apply_filters('iwptp_device_columns', $columns, $device);
// another device requested
if (!empty($_GET[$table_id . '_device']) && $_GET[$table_id . '_device'] != $device) { //phpcs:ignore
// "loading device view" screen
include $tpl . 'scroll-wrap-outer-open.php';
include $tpl . 'scroll-wrap-open.php';
echo wp_kses(iwptp_icon('loader', 'iwptp-device-view-loading-icon'), iwptp_allowed_html_tags());
include $tpl . 'scroll-wrap-close.php';
include $tpl . 'scroll-wrap-outer-close.php';
continue;
}
if ($products->have_posts()) {
do_action("woocommerce_shortcode_before_{$this->type}_loop", $this->attributes);
include $tpl . 'scroll-wrap-outer-open.php';
include $tpl . 'scroll-wrap-open.php';
include $tpl . 'table-open.php';
// column headings row
include $tpl . 'heading-row.php';
$limit = intval($data['query']['limit']);
$current_page = (!empty($_GET[$table_id . '_paged'])) ? intval($_GET[$table_id . '_paged']) : 1; //phpcs:ignore
if (!empty($current_page) && $current_page > 1) {
$GLOBALS['iwptp_row_iteration'] = ($limit * $current_page) - ($limit - 1);
} else {
$GLOBALS['iwptp_row_iteration'] = 1;
}
// product rows
while ($products->have_posts()) {
$products->the_post();
$GLOBALS['iwptp_row_rand']++;
// Set custom product visibility when quering hidden products.
add_action('woocommerce_product_is_visible', array($this, 'set_product_as_visible'));
global $product;
$product = apply_filters('iwptp_product', $product);
if (!empty($data['query']['sc_attrs']['enable_visibility_rules'])) {
if (!$product->is_visible()) {
continue;
}
}
ob_start();
include $tpl . 'row-open.php';
do_action('iwptp_after_row_open');
if (!empty($columns)) {
foreach ($columns as $column_index => $column) {
iwptp_parse_style_2($column['cell']);
include $tpl . 'cell-open.php';
ob_start();
echo wp_kses(apply_filters(
'iwptp_cell_value',
trim(iwptp_parse_2($column['cell']['template'], $product)),
$column_index,
$column,
$device
), iwptp_allowed_html_tags());
if ($cell_val = ob_get_clean()) {
include $tpl . 'cell-value-open.php';
echo wp_kses($cell_val, iwptp_allowed_html_tags());
include $tpl . 'cell-value-close.php';
}
include $tpl . 'cell-close.php';
}
}
do_action('iwptp_before_row_close');
include $tpl . 'row-close.php';
echo wp_kses(apply_filters('iwptp_row', ob_get_clean()), iwptp_allowed_html_tags());
// Restore product visibility.
remove_action('woocommerce_product_is_visible', array($this, 'set_product_as_visible'));
$GLOBALS['iwptp_row_iteration']++;
}
wc_reset_loop();
wp_reset_postdata();
include $tpl . 'table-close.php';
include $tpl . 'scroll-wrap-close.php';
include $tpl . 'scroll-wrap-outer-close.php';
include $tpl . 'loading-screen.php';
} else {
ob_start();
include $tpl . 'no-results.php';
$no_results_markup = ob_get_clean();
echo wp_kses(apply_filters('iwptp_no_results', $no_results_markup), iwptp_allowed_html_tags());
}
}
iwptp_item_styles($data);
do_action('iwptp_container_close');
$markup = ob_get_clean();
echo wp_kses($markup, iwptp_allowed_html_tags());
$this->set_cache($markup);
}
// print footer
if (!$this->only_loop) {
echo wp_kses(apply_filters('iwptp_footer', str_replace('{pagination}', $pagination, $footer)), iwptp_allowed_html_tags());
}
echo '
';
// update cart info
if (wp_doing_ajax()) {
echo '';
}
// edit table link
if (current_user_can('edit_others_iwptp_product_tables')) {
echo '';
}
include $tpl . 'container-close.php';
return ob_get_clean();
}
public function order_by_asc_popularity_post_clauses($args)
{
global $wpdb;
$args['orderby'] = "$wpdb->postmeta.meta_value+0 ASC, $wpdb->posts.post_date DESC";
return $args;
}
/**
* Remove ordering queries.
*/
public function remove_ordering_args()
{
remove_filter('posts_clauses', array($this, 'order_by_asc_popularity_post_clauses'));
remove_filter('get_meta_sql', array($this, 'cast_decimal_precision'));
}
public function parse_query_args($data = [])
{
$query_args_essential = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'no_found_rows' => false,
'orderby' => 'price',
'order' => 'DESC',
);
$query_args = array_merge($query_args_essential, $this->query_args);
if (empty($query_args['meta_query'])) {
$query_args['meta_query'] = []; //phpcs:ignore
}
if (empty($query_args['tax_query'])) {
$query_args['tax_query'] = []; //phpcs:ignore
}
// SKUs.
if (!empty($this->attributes['include_skus'])) {
$skus = array_map('trim', explode(',', $this->attributes['include_skus']));
$query_args['meta_query'][] = [
'key' => '_sku',
'value' => (1 === count($skus)) ? $skus[0] : $skus,
'compare' => (1 === count($skus)) ? '=' : 'IN',
];
}
if (!empty($this->attributes['exclude_skus'])) {
$skus = array_map('trim', explode(',', $this->attributes['exclude_skus']));
$query_args['meta_query'][] = [
'relation' => 'OR',
[
'key' => '_sku',
'compare' => 'NOT EXISTS',
],
[
'key' => '_sku',
'value' => (1 === count($skus)) ? $skus[0] : $skus,
'compare' => (1 === count($skus)) ? '!=' : 'NOT IN',
],
];
}
if (empty($data)) {
$data = (isset($GLOBALS['iwptp_table_data'])) ? $GLOBALS['iwptp_table_data'] : [];
}
// Visibility.
if (empty($this->attributes['include_hidden']) && (empty($data['query']) || (isset($data['query']['product_statuses']) && !in_array('hidden_products', $data['query']['product_statuses'])))) {
if (!is_array($query_args['tax_query'])) {
$query_args['tax_query'] = array( //phpcs:ignore
'relation' => 'AND',
);
}
$product_visibility_terms = wc_get_product_visibility_term_ids();
if (!empty($this->attributes['_archive']) && $this->attributes['_archive'] == 'search') {
$product_visibility_not_in = $product_visibility_terms['exclude-from-search'];
} else {
$product_visibility_not_in = $product_visibility_terms['exclude-from-catalog'];
}
if (!empty($product_visibility_not_in)) {
if (!is_array($product_visibility_not_in)) {
$product_visibility_not_in = [$product_visibility_not_in];
}
$query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => $product_visibility_not_in,
'operator' => 'NOT IN',
);
}
}
// IDs.
$this->set_ids_query_args($query_args);
// Set specific types query args.
$method = "set_{$this->type}_query_args";
if (method_exists($this, $method)) {
$this->{$method}($query_args);
}
// Attributes.
$this->set_attributes_query_args($query_args);
// Tags.
$this->set_tags_query_args($query_args);
// apply user filters
$table_id = $this->attributes['id'];
//-- flags
$user_set_cats = false;
//-- iterate user nav filters
if (!empty($GLOBALS['iwptp_user_filters'])) {
foreach ($GLOBALS['iwptp_user_filters'] as &$filter_info) {
// results per page
if ($filter_info['filter'] == 'results_per_page') {
$this->attributes['limit'] = $filter_info['results'];
}
// category
if ($filter_info['filter'] == 'category' && !empty($filter_info['values'])) {
$query_args['tax_query'][] = array(
'taxonomy' => 'product_cat',
'field' => 'term_taxonomy_id',
'terms' => $filter_info['values'],
'operator' => $filter_info['operator'],
);
$user_set_cats = true;
}
// taxonomy
if ($filter_info['filter'] == 'taxonomy') {
$query_args['tax_query'][] = array(
'taxonomy' => $filter_info['taxonomy'],
'field' => 'term_taxonomy_id',
'terms' => $filter_info['values'],
'operator' => $filter_info['operator'],
);
}
// rating
if ($filter_info['filter'] == 'rating') {
$query_args['meta_query'][] = array(
'key' => '_wc_average_rating',
'value' => array((int) $filter_info['values'][0], 5),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
);
}
// hide out of stock items
if ($filter_info['filter'] == 'availability' && in_array(strtoupper($filter_info['operator']), array('NOT IN', 'IN', 'ALSO'))) {
$product_visibility_terms = wc_get_product_visibility_term_ids();
$query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array($product_visibility_terms['outofstock']),
'operator' => $filter_info['operator'],
);
}
// attribute
if ($filter_info['filter'] == 'attribute') {
$query_args['tax_query'][] = array(
'taxonomy' => $filter_info['taxonomy'],
'field' => 'term_taxonomy_id',
'terms' => $filter_info['values'],
'operator' => !empty($filter_info['operator']) ? $filter_info['operator'] : 'IN',
);
}
// custom field
if ($filter_info['filter'] == 'custom_field') {
if ($filter_info['compare'] == 'BETWEEN') {
$arr = array(
'key' => $filter_info['meta_key'],
'value' => array($filter_info['min'], $filter_info['max']),
'compare' => 'BETWEEN',
'type' => $filter_info['type'],
);
if (!$filter_info['max']) {
$arr['compare'] = '>=';
$arr['value'] = $filter_info['min'];
}
if (!$filter_info['min']) {
$arr['compare'] = '<=';
$arr['value'] = $filter_info['max'];
}
$query_args['meta_query'][] = $arr;
} else if (in_array($filter_info['compare'], array('IN', 'NOT IN', 'LIKE'))) {
$query_args['meta_query'][] = array(
'key' => $filter_info['meta_key'],
'value' => $filter_info['values'],
'compare' => $filter_info['compare'],
);
} else if (in_array($filter_info['compare'], array('EXISTS', 'NOT EXISTS'))) {
$query_args['meta_query'][] = array(
'key' => $filter_info['meta_key'],
'compare' => $filter_info['compare'],
);
}
}
// orderby
if ($filter_info['filter'] == 'orderby') {
// order by a column
if (!empty($_GET[$data['id'] . '_' . 'orderby']) && substr($_GET[$data['id'] . '_' . 'orderby'], 0, 7) == 'column_') { //phpcs:ignore
$col_index = substr(sanitize_text_field(wp_unslash($_GET[$data['id'] . '_' . 'orderby'])), 7); //phpcs:ignore
$device = sanitize_text_field(wp_unslash($_GET[$data['id'] . '_' . 'device'])); //phpcs:ignore
if (!in_array($device, array('laptop', 'tablet', 'phone'))) {
$device = 'laptop';
}
$order = strtolower(sanitize_text_field(wp_unslash($_GET[$data['id'] . '_' . 'order']))); //phpcs:ignore
if (!in_array($order, array('asc', 'desc'))) {
$order = 'asc';
}
$column_sorting = iwptp_get_column_sorting_info($col_index, $device);
if ($column_sorting['orderby'] == 'price' && $order == 'desc') {
$filter_info['orderby'] = 'price-desc';
} else {
$filter_info['orderby'] = $column_sorting['orderby'];
}
$filter_info['order'] = $order;
$filter_info['meta_key'] = $column_sorting['meta_key']; //phpcs:ignore
if (in_array($column_sorting['orderby'], array('attribute', 'attribute_num'))) {
$filter_info['orderby_attribute'] = !empty($column_sorting['orderby_attribute']) ? $column_sorting['orderby_attribute'] : false;
$filter_info['orderby_focus_attribute_terms'] = !empty($column_sorting['orderby_focus_attribute_terms']) ? $column_sorting['orderby_focus_attribute_terms'] : false;
$filter_info['orderby_ignore_attribute_terms'] = !empty($column_sorting['orderby_ignore_attribute_terms']) ? $column_sorting['orderby_ignore_attribute_terms'] : false;
$filter_info['orderby_attribute_include_all'] = !empty($column_sorting['orderby_attribute_include_all']);
}
if ($column_sorting['orderby'] == 'taxonomy') {
$filter_info['orderby_taxonomy'] = !empty($column_sorting['orderby_taxonomy']) ? $column_sorting['orderby_taxonomy'] : false;
$filter_info['orderby_focus_taxonomy_terms'] = !empty($column_sorting['orderby_focus_taxonomy_terms']) ? $column_sorting['orderby_focus_taxonomy_terms'] : false;
$filter_info['orderby_ignore_taxonomy_terms'] = !empty($column_sorting['orderby_ignore_taxonomy_terms']) ? $column_sorting['orderby_ignore_taxonomy_terms'] : false;
$filter_info['orderby_taxonomy_include_all'] = !empty($column_sorting['orderby_taxonomy_include_all']);
}
}
if ($filter_info['orderby'] == 'sku') {
$filter_info['orderby'] = 'meta_value';
$filter_info['meta_key'] = '_sku'; //phpcs:ignore
} else if ($filter_info['orderby'] == 'sku_num') {
$filter_info['orderby'] = 'meta_value_num';
$filter_info['meta_key'] = '_sku'; //phpcs:ignore
}
$query_args['orderby'] = $filter_info['orderby'];
$query_args['order'] = $filter_info['order'];
if (!empty($filter_info['meta_key'])) {
$query_args['meta_key'] = $filter_info['meta_key']; //phpcs:ignore
}
if ($filter_info['orderby'] == 'relevance') {
$query_args['orderby'] = 'post__in';
}
}
// price range
if ($filter_info['filter'] == 'price_range' && (!empty($filter_info['min_price']) || !empty($filter_info['max_price']))) {
if (isset($filter_info['min_price']) && !$filter_info['min_price']) {
unset($filter_info['min_price']);
}
if (isset($filter_info['max_price']) && !$filter_info['max_price']) {
unset($filter_info['max_price']);
}
$current_min_price = isset($filter_info['min_price']) ? floatval($filter_info['min_price']) : 0;
$current_max_price = isset($filter_info['max_price']) ? floatval($filter_info['max_price']) : PHP_INT_MAX;
$meta_query = apply_filters(
'woocommerce_get_min_max_price_meta_query',
array(
'price_filter' => true,
'key' => '_price',
'value' => array($current_min_price, $current_max_price),
'compare' => 'BETWEEN',
'type' => 'DECIMAL(10,' . wc_get_price_decimals() . ')',
),
$filter_info
);
$meta_query['price_filter'] = true;
$query_args['meta_query']['price_filter'] = $meta_query;
}
// on sale
if ($filter_info['filter'] == 'on_sale') {
$query_args['post__in'] = wc_get_product_ids_on_sale();
}
// search
if ($filter_info['filter'] == 'search') {
foreach ($filter_info['searches'] as $search) {
iwptp_search($search, $query_args['post__in']);
}
}
}
}
// ensure category is applied
if (!$user_set_cats && !empty($this->attributes['category'])) {
$terms = explode(',', $this->attributes['category']);
$query_args['tax_query'][] = array(
'taxonomy' => 'product_cat',
'field' => 'term_taxonomy_id',
'terms' => $terms,
);
}
if ($query_args['orderby'] == 'price-desc') {
$query_args['orderby'] = 'price';
$query_args['order'] = 'DESC';
} else if ($query_args['orderby'] == 'price') {
$query_args['orderby'] = 'price';
$query_args['order'] = 'ASC';
} else if ($query_args['orderby'] == 'rating') {
$query_args['order'] = 'DESC';
}
$ordering_args = WC()->query->get_catalog_ordering_args($query_args['orderby'], $query_args['order']);
$query_args['orderby'] = $ordering_args['orderby'];
$query_args['order'] = $ordering_args['order'];
if ($ordering_args['meta_key']) {
$query_args['meta_key'] = $ordering_args['meta_key']; //phpcs:ignore
}
$query_args['posts_per_page'] = intval($this->attributes['limit']);
if (1 < $this->attributes['page']) {
$query_args['paged'] = absint($this->attributes['page']);
}
if (!empty($this->attributes['offset'])) {
$query_args['offset'] = intval($this->attributes['offset']);
}
if (!empty($this->attributes['sc_attrs']['offset'])) {
$query_args['offset'] = intval($this->attributes['sc_attrs']['offset']);
}
// apply pagination
if (!empty($_REQUEST[$table_id . '_paged'])) { //phpcs:ignore
$query_args['paged'] = intval($_REQUEST[$table_id . '_paged']); //phpcs:ignore
}
// parse additional query args string
if (!empty($this->attributes['additional_query_args'])) {
$query_args = wp_parse_args($this->attributes['additional_query_args'], $query_args);
}
if (!empty($this->attributes['include_products'])) {
$query_args['post__in'] = (!empty($query_args['post__in'])) ? array_map('intval', array_intersect($query_args['post__in'], $this->attributes['include_products'])) : array_map('intval', $this->attributes['include_products']);
}
if (!empty($this->attributes['exclude_products'])) {
$query_args['post__not_in'] = array_map('intval', $this->attributes['exclude_products']); //phpcs:ignore
}
if (!empty($this->attributes['product_statuses'])) {
$meta_query_key = (isset($data['query']['sc_attrs']['product_variations']) && $data['query']['sc_attrs']['product_variations'] === true) ? 'variation_meta_query' : 'meta_query';
// Featured
if (in_array('featured', $this->attributes['product_statuses'])) {
$query_args['tax_query'][] = [
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
];
}
// On sale
if (in_array('on_sale', $this->attributes['product_statuses'])) {
$query_args[$meta_query_key][] = [
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
];
}
// Only in stock
if (in_array('only_in_stock', $this->attributes['product_statuses'])) {
$query_args[$meta_query_key][] = [
'key' => '_stock_status',
'value' => 'instock',
'compare' => '=',
];
}
// Only out of stock
if (in_array('only_out_of_stock', $this->attributes['product_statuses'])) {
$query_args[$meta_query_key][] = [
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => '=',
];
}
// Hidden products
if (in_array('hidden_products', $this->attributes['product_statuses'])) {
$query_args['tax_query'][] = [
[
'relation' => 'AND',
[
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => ['exclude-from-catalog'],
],
[
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => ['exclude-from-search'],
],
],
];
}
// Private products
if (in_array('private_products', $this->attributes['product_statuses'])) {
$query_args['post_status'] = 'private';
}
}
if (!empty($this->attributes['include_product_types']) && is_array($this->attributes['include_product_types'])) {
$query_args['tax_query'][] = [
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => array_map('sanitize_text_field', $this->attributes['include_product_types']),
'operator' => 'IN',
];
}
if (!empty($this->attributes['exclude_product_types']) && is_array($this->attributes['exclude_product_types'])) {
$query_args['tax_query'][] = [
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => array_map('sanitize_text_field', $this->attributes['exclude_product_types']),
'operator' => 'NOT IN',
];
}
if ((isset($this->attributes['price_from']) && $this->attributes['price_from'] != '') || (isset($this->attributes['price_to']) && $this->attributes['price_to'] != '')) {
$min_price = (isset($this->attributes['price_from']) && $this->attributes['price_from'] != '') ? floatval($this->attributes['price_from']) : 0;
$max_price = (isset($this->attributes['price_to']) && $this->attributes['price_to'] != '') ? floatval($this->attributes['price_to']) : PHP_INT_MAX;
$query_args['meta_query'][] = apply_filters(
'woocommerce_get_min_max_price_meta_query',
array(
'price_filter' => true,
'key' => '_price',
'value' => array($min_price, $max_price),
'compare' => 'BETWEEN',
'type' => 'DECIMAL(10,' . wc_get_price_decimals() . ')',
)
);
}
if (!empty($this->attributes['include_taxonomies']) && is_array($this->attributes['include_taxonomies'])) {
foreach ($this->attributes['include_taxonomies'] as $item) {
if (strpos($item, '{iwptp-tax-id}') !== false) {
$taxonomy = explode('{iwptp-tax-id}', $item);
if (!empty($taxonomy) && is_array($taxonomy) && !empty($taxonomy[0]) && !empty($taxonomy[1])) {
$is_attribute = (strpos($taxonomy[0], 'pa_') === 0);
$query_args['tax_query'][] = [
'taxonomy' => sanitize_text_field($taxonomy[0]),
'field' => (in_array($taxonomy[0], ['product_cat', 'category'])) ? 'term_id' : 'slug',
'terms' => [($is_attribute) ? sanitize_text_field($taxonomy[1]) : intval($taxonomy[1])],
'operator' => 'IN',
];
}
}
}
}
if (!empty($this->attributes['exclude_taxonomies']) && is_array($this->attributes['exclude_taxonomies'])) {
foreach ($this->attributes['exclude_taxonomies'] as $item) {
if (strpos($item, '{iwptp-tax-id}') !== false) {
$taxonomy = explode('{iwptp-tax-id}', $item);
if (!empty($taxonomy) && is_array($taxonomy) && !empty($taxonomy[0]) && !empty($taxonomy[1])) {
$is_attribute = (strpos($taxonomy[0], 'pa_') === 0);
$query_args['tax_query'][] = [
'taxonomy' => sanitize_text_field($taxonomy[0]),
'field' => (in_array($taxonomy[0], ['product_cat', 'category'])) ? 'term_id' : 'slug',
'terms' => [($is_attribute) ? sanitize_text_field($taxonomy[1]) : intval($taxonomy[1])],
'operator' => 'NOT IN',
];
}
}
}
}
// date filter
if (!empty($_GET[$table_id . '_date_from'])) { //phpcs:ignore
$from_array = explode('-', sanitize_text_field(wp_unslash($_GET[$table_id . '_date_from']))); //phpcs:ignore
if (!empty($from_array) && count($from_array) == 3) {
$query_args['date_query']['after'] = [
'year' => sanitize_text_field($from_array[0]),
'month' => sanitize_text_field($from_array[1]),
'day' => sanitize_text_field($from_array[2]),
];
}
}
if (!empty($_GET[$table_id . '_date_to'])) { //phpcs:ignore
$to_array = explode('-', sanitize_text_field(wp_unslash($_GET[$table_id . '_date_to']))); //phpcs:ignore
if (!empty($to_array) && count($to_array) == 3) {
$query_args['date_query']['before'] = [
'year' => sanitize_text_field($to_array[0]),
'month' => sanitize_text_field($to_array[1]),
'day' => sanitize_text_field($to_array[2]),
];
}
}
if (!empty($query_args['date_query'])) {
$query_args['date_query']['inclusive'] = true;
}
$query_args = apply_filters('woocommerce_shortcode_products_query', $query_args, $this->attributes, $this->type);
// Always query only IDs.
// $query_args['fields'] = 'ids';
return $query_args;
}
protected function parse_attributes($attributes)
{
$table_data = $GLOBALS['iwptp_table_data'];
if (empty($GLOBALS['iwptp_table_data']['query']['sc_attrs'])) {
$GLOBALS['iwptp_table_data']['query']['sc_attrs'] = [];
}
if (!empty($table_data['settings']['loading_image_url']) && !is_admin()) {
$size = !empty($table_data['settings']['loading_image_size']) ? intval(esc_attr($table_data['settings']['loading_image_size'])) : 'auto';
$image_url = esc_url($table_data['settings']['loading_image_url']);
$background_color = (!empty($table_data['settings']['loading_background_color'])) ? esc_attr($table_data['settings']['loading_background_color']) : '#ffffff';
echo "";
}
$GLOBALS['iwptp_table_data']['query']['sc_attrs'] = (!empty($table_data['settings'])) ? array_merge($table_data['settings'], $attributes) : $attributes;
// don't want wc sc to process these its own way
foreach (array('attribute', 'custom_field') as $key) {
if (!empty($attributes[$key])) {
unset($attributes[$key]);
}
}
foreach (array('name', 'id') as $key) {
if (!empty($GLOBALS['iwptp_table_data']['query']['sc_attrs'][$key])) {
unset($GLOBALS['iwptp_table_data']['query']['sc_attrs'][$key]);
}
}
$attributes = $this->parse_legacy_attributes($attributes);
$attributes = shortcode_atts(array(
'limit' => '-1', // Results limit.
'columns' => '3', // Number of columns.
'rows' => '', // Number of rows. If defined, limit will be ignored.
'orderby' => '', // menu_order, title, date, rand, price, popularity, rating, or id.
'order' => '', // ASC or DESC.
'meta_key' => '', //phpcs:ignore
'ids' => '', // Comma separated IDs.
'skus' => '', // Comma separated SKUs.
'category' => '', // Comma separated category slugs.
'nav_category' => '',
'nav_category_id' => '',
'cat_operator' => 'IN', // Operator to compare categories. Possible values are 'IN', 'NOT IN', 'AND'.
'attribute' => '', // Single attribute slug.
'terms' => '', // Comma separated term slugs.
'terms_operator' => 'IN', // Operator to compare terms. Possible values are 'IN', 'NOT IN', 'AND'.
'tag' => '', // Comma separated tag slugs.
'tag_operator' => 'IN', // Comma separated tag slugs.
'class' => '', // HTML class.
'page' => 1, // Page for pagination.
'cache' => false, // Should shortcode output be cached.
'exclude_category' => '', // Comma separated category slugs.
'include_hidden' => false, // Hidden from shop / search.
'offset' => 0, // Post offset.
'id' => 0, // Table id.
'_archive' => false,
'_only_loop' => false,
'_disable_nav' => false,
), $attributes, $this->type);
// only render loop based on gloal query
// hide navigation and pagination
if ($attributes['_only_loop'] && in_array(trim(strtolower($attributes['_only_loop'])), array('true', '1', 'yes'))) {
$this->only_loop = true;
}
if ($attributes['_disable_nav']) {
$this->disable_navigation = true;
}
// cache
$this->caching = !!$attributes['cache'];
$query = &$GLOBALS['iwptp_table_data']['query'];
// category
if ($attributes['category'] == '_all') {
$query['category'] = [];
}
// nav_category - category names
if (!empty($attributes['nav_category'])) {
// modify the original set of categories
$query['category'] = get_terms(array(
'taxonomy' => 'product_cat',
'fields' => 'tt_ids',
'slug' => array_map('trim', explode(',', $attributes['nav_category'])),
'hide_empty' => false,
));
}
// tag / tags
$sc_attrs = &$GLOBALS['iwptp_table_data']['query']['sc_attrs'];
if (!empty($sc_attrs['tags'])) {
$attributes['tag'] = $sc_attrs['tags'];
}
// empty query category for archive
if (!empty($attributes['_archive'])) {
$query['category'] = [];
}
// nav_category_id - category term taxonomy ids
if (!empty($attributes['nav_category_id'])) {
// modify the original set of categories
$query['category'] = array_map('trim', explode(',', $attributes['nav_category_id']));
}
// ids
if (!empty($attributes['ids'])) {
$query['ids'] = $attributes['ids'];
}
// set_ids_query_args requires ","
if (
!empty($query['ids']) &&
trim($query['ids']) &&
false === strpos($query['ids'], ",")
) {
$query['ids'] .= ',';
}
// skus
if (!empty($attributes['skus'])) {
$query['skus'] = $attributes['skus'];
}
// orderby
if (!empty($attributes['orderby'])) {
$orderby = trim(strtolower($attributes['orderby']));
if ($orderby == 'custom_field') {
$orderby = 'meta_value';
} else if ($orderby == 'custom_field_num') {
$orderby = 'meta_value_num';
}
$arr = array(
'ids',
'skus',
'title',
'date',
'menu_order',
'rating',
'price',
'price-desc',
'popularity',
'rand',
'meta_value_num',
'meta_value',
'id',
'sku',
'sku_num',
);
if (in_array($orderby, $arr)) {
$query['orderby'] = $attributes['orderby'];
}
}
// order
if (!empty($attributes['order'])) {
$order = trim(strtoupper($attributes['order']));
if ($order == 'ASCENDING') {
$order = 'ASC';
} else if ($order == 'DESCENDING') {
$order = 'DESC';
}
if (in_array($order, array(
'ASC',
'DESC',
))) {
$query['order'] = $order;
}
}
if (!empty($attributes['custom_field'])) {
$attributes['meta_key'] = trim($attributes['custom_field']); //phpcs:ignore
}
if (!empty($attributes['meta_key'])) {
$attributes['meta_key'] = trim($attributes['meta_key']); //phpcs:ignore
}
// skus
if (!empty($attributes['skus'])) {
$query['skus'] = $attributes['skus'];
}
// limit
if (!empty($query['sc_attrs']['limit'])) {
$query['limit'] = (int) $GLOBALS['iwptp_table_data']['query']['sc_attrs']['limit'];
}
if (!empty($query['category']) && is_array($query['category'])) {
$query['category'] = array_map('intval', array_unique($query['category']));
// cats need to be comma separated string
$query['category'] = implode(',', $query['category']);
}
if (!empty($query['original_category']) && is_array($query['original_category'])) {
$query['original_category'] = array_map('intval', array_unique($query['original_category']));
// cats need to be comma separated string
$query['original_category'] = implode(',', $query['original_category']);
}
// merge
$attributes = array_merge($attributes, $query);
return apply_filters('iwptp_parse_attributes', $attributes);
}
public function nav_later($nav)
{
$GLOBALS['iwptp_nav_later_flag'] = true;
foreach ($GLOBALS['iwptp_nav_later'] as $elm) {
extract($elm);
if (!isset($product)) {
$product = false;
}
$markup = iwptp_parse_ctx_2($element, $elm_tpl, $elm_type, $product);
$nav = str_replace($placeholder, $markup, $nav);
}
$GLOBALS['iwptp_nav_later_flag'] = false;
return $nav;
}
public function cast_decimal_precision($array)
{
$array['where'] = str_replace('DECIMAL', 'DECIMAL(10,3)', $array['where']);
return $array;
}
}