/var/www/html_us/wp-content/plugins/revslider/includes/woocommerce.class.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
/**
 * @author    ThemePunch <[email protected]>
 * @link      https://www.themepunch.com/
 * @copyright 2024 ThemePunch
 */
 
if(!defined('ABSPATH')) exit();

class 
RevSliderWooCommerce extends RevSliderFunctions {
    
    const 
META_SKU     '_sku'//can be 'instock' or 'outofstock'
    
const META_STOCK '_stock'//can be 'instock' or 'outofstock'
    
    /**
     * return true / false if the woo commerce exists
     * @before RevSliderWooCommerce::isWooCommerceExists();
     */
    
public static function woo_exists(){
        return (
class_exists('Woocommerce')) ? true false;
    }
    
    
    
/**
     * compare wc current version to given version
     */
    
public static function version_check($version '1.0') {
        if(
self::woo_exists()){
            global 
$woocommerce;
            if(
version_compare($woocommerce->version$version'>=')){
                return 
true;
            }
        }
        return 
false;
    }
    
    
    
/**
     * get wc post types
     */
    
public static function getCustomPostTypes(){
        
$arr = array(
            
'product'            => __('Product''revslider'),
            
'product_variation'    => __('Product Variation''revslider')
        );
        
        return 
$arr;
    }
    
    
    
/**
     * get price query
     * @before: RevSliderWooCommerce::getPriceQuery()
     */
    
private static function get_price_query($from$to$meta_tag){
        
$from    = (empty($from)) ? $from;
        
$to        = (empty($to)) ? 9999999999 $to;
        
$query    = array(
            
'key'        => $meta_tag,
            
'value'        => array($from$to),
            
'type'        => 'numeric',
            
'compare'    => 'BETWEEN'
        
);
        
        return 
$query;
    }
    
    
    
/**
     * check if in pricerange
     */
    
private static function check_price_range($from$to$check){
        
$from    = (empty($from)) ? $from;
        
$to        = (empty($to)) ? 9999999999 $to;
        
        return (
$check $from && $check $to) ? true false;
    }
    
    
    
/**
     * get meta query for filtering woocommerce posts.
     * before: RevSliderWooCommerce::getMetaQuery();
     * @6.5.23: removed _regular_price and _sale_price here, will be later checked under filter_products_by_price() to add the children
     */
    
public static function get_meta_query($args){
        
$f            RevSliderGlobals::instance()->get('RevSliderFunctions');
        
$query        = array();
        
$meta_query    = array();
        
$tax_query    = array();
        
        if(
$f->get_val($args, array('source''woo''inStockOnly')) == true){
            
$meta_query[] = array(
                
'key' => '_stock_status',
                
'value' => 'instock',
                
'compare' => '='
            
);
        }
        
        if(
$f->get_val($args, array('source''woo''featuredOnly')) == true){
            
$tax_query[] = array(
                
'taxonomy' => 'product_visibility',
                
'field'    => 'name',
                
'terms'    => 'featured',
            );
        }

        
$tax_query['relation'] = 'AND';
        
$tax_query[] = array(
            
'taxonomy' => 'product_visibility',
            
'field'    => 'name',
            
'terms'    => 'exclude-from-catalog',
            
'operator' => 'NOT IN',
        );
        
        if(!empty(
$meta_query))    $query['meta_query'] = $meta_query;
        if(!empty(
$tax_query))    $query['tax_query'] = $tax_query;
        
        return 
$query;
    }


    
/**
     * filter posts by sales prices, also check for child products
     * @since: 6.5.23
     */
    
public static function filter_products_by_price($posts$args){
        if(empty(
$posts)) return $posts;

        
$f                    RevSliderGlobals::instance()->get('RevSliderFunctions');
        
$is_30                RevSliderWooCommerce::version_check('3.0');
        
$reg_price_from        $f->get_val($args, array('source''woo''regPriceFrom'));
        
$reg_price_to        $f->get_val($args, array('source''woo''regPriceTo'));
        
$sale_price_from    $f->get_val($args, array('source''woo''salePriceFrom'));
        
$sale_price_to        $f->get_val($args, array('source''woo''salePriceTo'));
        
$post_types            $f->get_val($args, array('source''woo''types'), 'any');

        
$meta_query = array();
        
//get regular price array
        
if(!empty($reg_price_from) || !empty($reg_price_to)){
            
$meta_query[] = self::get_price_query($reg_price_from$reg_price_to'_regular_price');
        }
        
        
//get sale price array
        
if(!empty($sale_price_from) || !empty($sale_price_to)){
            
$meta_query[] = self::get_price_query($sale_price_from$sale_price_to'_sale_price');
        }

        
$_good_posts = array();
        foreach(
$posts as $key => $post){
            
$product_id $f->get_val($post'ID'); // ID of parent product
            
$product    = ($is_30) ? wc_get_product($product_id) : get_product($product_id);

            if(
$product === false){
                
$_good_posts[] = $post;
                unset(
$posts[$key]);
                continue;
            }
            
            
//check if current post is okay with _regular_price and _sale_price
            
if(!empty($reg_price_from) || !empty($reg_price_to) || !empty($sale_price_from) || !empty($sale_price_to)){
                
$meta            get_post_meta($product_id);
                
$in_reg_range    false;
                
$in_sale_range    false;
                if(!empty(
$reg_price_from) || !empty($reg_price_to)){
                    
$in_reg_range    self::check_price_range($reg_price_from$reg_price_to$f->get_val($meta'_regular_price'));
                }
                if(!empty(
$sale_price_from) || !empty($sale_price_to)){
                    
$in_sale_range    self::check_price_range($sale_price_from$sale_price_to$f->get_val($meta'_sale_price'));
                }

                if(
$in_reg_range || $in_sale_range){
                    
$_good_posts[] = $post;
                    continue;
                }else{
                    unset(
$posts[$key]);
                }
            }
            
            if(!empty(
$meta_query)){
                
$my_posts    = new WP_Query(
                    array(
                        
'post_parent'    => $product_id// ID of a page, post, or custom type
                        
'post_type'        => $post_types,
                        
'meta_query'    => $meta_query
                    
)
                );
                
$_posts        $my_posts->posts;
                if(!empty(
$_posts)){
                    foreach(
$_posts as $child_post){
                        
$_good_posts[] = $child_post;
                    }
                }
            }else{
                
$_good_posts[] = $post;
            }
        }

        return 
$_good_posts;
    }
    
    
    
/**
     * get sortby function including standart wp sortby array
     */
    
public static function getArrSortBy(){
        
        
$sort_by = array(
            
'meta_num__regular_price'    => __('Regular Price''revslider'),
            
'meta_num__sale_price'        => __('Sale Price''revslider'),
            
'meta_num_total_sales'        => __('Number Of Sales''revslider'),
            
'meta_num__wc_average_rating' => __('Rating''revslider'),
            
//'meta__featured'            => __('Featured Products', 'revslider'),
            
'meta__sku'                    => __('SKU''revslider'),
            
'meta_num_stock'            => __('Stock Quantity''revslider')
        );
        
        return 
$sort_by;
    }

    
/**
     * since WooCommerce 3.0 this function is deprecated as it could lead to performance issues
     * this is a 1to1 copy of the named function without the deprecation message
     **/
    
public static function get_total_stock($product){
        if ( 
sizeof$product->get_children() ) > ) {
            
$total_stock max0$product->get_stock_quantity() );

            foreach ( 
$product->get_children() as $child_id ) {
                if ( 
'yes' === get_post_meta$child_id'_manage_stock'true ) ) {
                    
$stock get_post_meta$child_id'_stock'true );
                    
$total_stock += max0wc_stock_amount$stock ) );
                }
            }
        } else {
            
$total_stock $product->get_stock_quantity();
        }
        
        return 
wc_stock_amount$total_stock );
    }

    public static function 
get_wc_data($post_id$text ''){
        global 
$SR_GLOBALS;
        
$is_30 RevSliderWooCommerce::version_check('3.0');
        
$product = ($is_30) ? wc_get_product($post_id) : get_product($post_id);

        if(
$product === false) return false;

        
$f RevSliderGlobals::instance()->get('RevSliderFunctions');

        
$wc_stock        = ($is_30) ? RevSliderWooCommerce::get_total_stock($product) : $product->get_total_stock();
        
$wc_rating        = ($is_30) ? wc_get_rating_html($product->get_average_rating()) : $product->get_rating_html();
        
$wc_categories    = ($is_30) ? wc_get_product_category_list($product->get_id(), ',') : $product->get_categories(',');
        
$wc_tags        = ($is_30) ? wc_get_product_tag_list($product->get_id()) : $product->get_tags();
        
$wc_add_to_cart_button '';
        
$wc_star_rating = ($SR_GLOBALS['front_version'] === 7) ? '<div class="sr-starring">' '<div class="rs-starring">';
        
preg_match_all('#<strong class="rating">.*?</span>#'$wc_rating$match);
        if(!empty(
$match) && isset($match[0]) && isset($match[0][0])){
            
$wc_star_rating .= str_replace($match[0][0], ''$wc_rating);
            
$wc_star_rating str_replace("Rated ","",$wc_star_rating);
        }
        
$wc_star_rating .= '</div>';
        
        if(
strpos($text'wc_add_to_cart_button') !== false){
            
$pr_id            = ($is_30) ? $product->get_id() : $product->id;
            
$pr_type        = ($is_30) ? $product->get_type() : $product->product_type;
            
$suffix            defined('SCRIPT_DEBUG') && SCRIPT_DEBUG '' '.min';
            
$ajax_cart_en    get_option('woocommerce_enable_ajax_add_to_cart') == 'yes' true false;
            
$assets_path    $f->remove_http(WC()->plugin_url()) . '/assets/';
            
            if(
$ajax_cart_en){
                
wp_enqueue_script'wc-add-to-cart'$assets_path.'js/frontend/'.'add-to-cart'.$suffix.'.js', array('jquery'), WC_VERSIONtrue);
                
                global 
$wc_is_localized;
                if(
$wc_is_localized === false){ //load it only one time
                    
wp_localize_script('wc-add-to-cart''wc_add_to_cart_params'apply_filters('wc_add_to_cart_params', array(
                        
'ajax_url'            => WC()->ajax_url(),
                        
'ajax_loader_url'    => apply_filters('woocommerce_ajax_loader_url'$assets_path 'images/[email protected]'),
                        
'i18n_view_cart'    => esc_attr__('View Cart''woocommerce'),
                        
'cart_url'            => get_permalink(wc_get_page_id('cart')),
                        
'is_cart'            => is_cart(),
                        
'cart_redirect_after_add' => get_option('woocommerce_cart_redirect_after_add')
                    )));
                    
$wc_is_localized true;
                }
            }
            
            
$wc_add_to_cart_button apply_filters(
                
'woocommerce_loop_add_to_cart_link',
                
sprintf('<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="button %s product_type_%s">%s</a>',
                    
esc_url($product->add_to_cart_url()),
                    
esc_attr($pr_id),
                    
esc_attr($product->get_sku()),
                    
$product->is_purchasable() ? 'add_to_cart_button' '',
                    
esc_attr($pr_type),
                    
esc_html($product->add_to_cart_text())
                ),
                
$product
            
);
        }
        
        return array(
            
'wc_full_price'        => $product->get_price_html(),
            
'wc_price'            => wc_price($product->get_price()),
            
'wc_price_no_cur'    => $product->get_price(),
            
'wc_stock'            => $wc_stock,
            
'wc_rating'            => $wc_rating,
            
'wc_star_rating'    => $wc_star_rating,
            
'wc_categories'        => $wc_categories,
            
'wc_add_to_cart'    => $product->add_to_cart_url(),
            
'wc_add_to_cart_button'    => $wc_add_to_cart_button,
            
'wc_sku'            => $product->get_sku(),
            
'wc_stock_quantity'    => $product->get_stock_quantity(),
            
'wc_rating_count'    => $product->get_rating_count(),
            
'wc_review_count'    => $product->get_review_count(),
            
'wc_tags'            => $wc_tags,
        );
    }

    
/**
     * modify layer text, to replace all meta
     */
    
public static function add_wc_layer($text$post_id$slide){
        if(
RevSliderWooCommerce::woo_exists() === false) return $text;

        
$data RevSliderWooCommerce::get_wc_data($post_id$text);
        if(
$data === false) return $text;
        
        foreach(
$data ?? [] as $tag => $value){
            
$value = (empty($value)) ? '' $value//fix is null issue
            
$text str_replace(array('%'.$tag.'%''{{'.$tag.'}}'), $value$text);
        }
        
        return 
$text;
    }

    public static function 
add_wc_layer_v7($post_data$data$metas$slider){
        if(
RevSliderWooCommerce::woo_exists() === false) return $post_data;
        
$f RevSliderGlobals::instance()->get('RevSliderFunctions');

        foreach(
$post_data ?? [] as $key => $post){
            
$content $f->get_val($post, array('content''content'));
            
$data RevSliderWooCommerce::get_wc_data($f->get_val($post'id'), $content);
            if(
$data === false) continue;

            
//modify excerpt if empty to be filled with content
            
if(!isset($post['excerpt']) || trim($post['excerpt']) === ''){
                
$post['excerpt'] = str_replace(array('<br/>''<br />'), ''strip_tags($content'<b><br><i><strong><small>'));
            }

            
$post_data[$key] = array_merge($post$data);
        }

        return 
$post_data;
    }
    
}    
//end of the class

add_filter('sr_modify_layer_text', array('RevSliderWooCommerce''add_wc_layer'), 103);
add_filter('sr_streamline_post_data_post', array('RevSliderWooCommerce''add_wc_layer_v7'), 104);