PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution / 2.3.0
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution v2.3.0
4.9.1 4.9.0 2.0.0 2.1.0 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.1 3.0.0 3.1.0 3.1.1 4.0.0 4.0.1 4.1.0 4.1.1 4.2.0 4.2.1 4.3.0 4.3.1 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.6.8 4.6.9 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.8.9 trunk 0.1.2-beta 0.1.3-beta 0.1.4-beta 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.5.0 1.5.1 1.6.0 1.6.1 1.7.0 1.8.0 1.8.1 1.9.0
shopengine / utils / helper.php
shopengine / utils Last commit date
controls-helper.php 3 years ago elementor-data-map.php 3 years ago helper.php 3 years ago notice.php 3 years ago shipping-calculation.php 3 years ago
helper.php
497 lines
1 <?php
2
3 namespace ShopEngine\Utils;
4
5
6 use ShopEngine\Core\Builders\Action;
7
8 defined('ABSPATH') || exit;
9
10 /**
11 * Global helper class.
12 *
13 * @since 1.0.0
14 */
15 class Helper {
16
17 public static function is_elementor_active() {
18
19 return did_action('elementor/loaded');
20 }
21
22 public static function is_gutenberg_active() {
23
24 return !did_action('elementor/loaded');
25 }
26
27 public static function is_elementor_editor_mode() {
28
29 if(self::is_elementor_active()) {
30
31 return \Elementor\Plugin::$instance->editor->is_edit_mode();
32 }
33
34 return false;
35 }
36
37
38 public static function get_elementor_css_uri($pid) {
39
40 global $blog_id;
41
42 $wp_upload_dir = wp_upload_dir(null, false);
43
44 $base = $wp_upload_dir['baseurl'] . '/elementor/css/';
45
46 return set_url_scheme($base . 'post-' . $pid . '.css');
47 }
48
49 public static function add_to_url($url, $param) {
50 $info = parse_url( $url );
51 $query = [];
52
53 if(isset($info['query'])){
54 parse_str( $info['query'], $query );
55 }
56 return $info['scheme'] . '://' . $info['host'] .( $info['path'] ?? '' ). '?' . http_build_query( $query ? array_merge( $query, $param ) : $param );
57 }
58
59
60 /**
61 * Auto generate classname from path.
62 *
63 * @since 1.0.0
64 * @access public
65 */
66 public static function make_classname($dirname) {
67 $dirname = pathinfo($dirname, PATHINFO_FILENAME);
68 $class_name = explode('-', $dirname);
69 $class_name = array_map('ucfirst', $class_name);
70 $class_name = implode('_', $class_name);
71
72 return $class_name;
73 }
74
75 public static function get_kses_array()
76 {
77 return [
78 'a' => [
79 'class' => [],
80 'href' => [],
81 'rel' => [],
82 'title' => [],
83 ],
84 'abbr' => [
85 'title' => [],
86 ],
87 'b' => [],
88 'blockquote' => [
89 'cite' => [],
90 ],
91 'cite' => [
92 'title' => [],
93 ],
94 'code' => [],
95 'del' => [
96 'datetime' => [],
97 'title' => [],
98 ],
99 'dd' => [],
100 'div' => [
101 'class' => [],
102 'title' => [],
103 'style' => [],
104 ],
105 'dl' => [],
106 'dt' => [],
107 'em' => [],
108 'h1' => [
109 'class' => [],
110 ],
111 'h2' => [
112 'class' => [],
113 ],
114 'h3' => [
115 'class' => [],
116 ],
117 'h4' => [
118 'class' => [],
119 ],
120 'h5' => [
121 'class' => [],
122 ],
123 'h6' => [
124 'class' => [],
125 ],
126 'i' => [
127 'class' => [],
128 ],
129 'img' => [
130 'alt' => [],
131 'class' => [],
132 'height' => [],
133 'src' => [],
134 'width' => [],
135 ],
136 'li' => [
137 'class' => [],
138 ],
139 'ol' => [
140 'class' => [],
141 ],
142 'p' => [
143 'class' => [],
144 ],
145 'q' => [
146 'cite' => [],
147 'title' => [],
148 ],
149 'span' => [
150 'class' => [],
151 'title' => [],
152 'style' => [],
153 ],
154 'iframe' => [
155 'width' => [],
156 'height' => [],
157 'scrolling' => [],
158 'frameborder' => [],
159 'allow' => [],
160 'src' => [],
161 ],
162 'strike' => [],
163 'br' => [],
164 'strong' => [],
165 'data-wow-duration' => [],
166 'data-wow-delay' => [],
167 'data-wallpaper-options' => [],
168 'data-stellar-background-ratio' => [],
169 'ul' => [
170 'class' => [],
171 ],
172 ];
173 }
174
175 public static function kses($raw) {
176
177 if(function_exists('wp_kses')) { // WP is here
178 return wp_kses($raw, self::get_kses_array());
179 } else {
180 return $raw;
181 }
182 }
183
184 public static function kspan($text) {
185 return str_replace(['{', '}'], ['<span>', '</span>'], self::kses($text));
186 }
187
188 public static function category_list_by_taxonomy($taxonomy = '') {
189 $query_args = [
190 'orderby' => 'ID',
191 'order' => 'DESC',
192 'hide_empty' => 1,
193 'taxonomy' => $taxonomy,
194 ];
195
196 $categories = get_categories($query_args);
197
198 return $categories;
199 }
200
201 public static function trim_words($text, $num_words) {
202 return wp_trim_words($text, $num_words, '');
203 }
204
205 public static function array_push_assoc($array, $key, $value) {
206 $array[$key] = $value;
207
208 return $array;
209 }
210
211 public static function render($content) {
212 if(stripos($content, "shopengine-has-lisence") !== false) {
213 return null;
214 }
215
216 return $content;
217 }
218
219 public static function render_elementor_content($content_id) {
220 $elementor_instance = \Elementor\Plugin::instance();
221
222 return $elementor_instance->frontend->get_builder_content_for_display($content_id);
223 }
224
225 public static function esc_options($str, $options = [], $default = '') {
226 if(!in_array($str, $options)) {
227 return $default;
228 }
229
230 return $str;
231 }
232
233 public static function img_meta($id) {
234 $attachment = get_post($id);
235 if($attachment == null || $attachment->post_type != 'attachment') {
236 return null;
237 }
238
239 return [
240 'alt' => get_post_meta($attachment->ID, '_wp_attachment_image_alt', true),
241 'caption' => $attachment->post_excerpt,
242 'description' => $attachment->post_content,
243 'href' => get_permalink($attachment->ID),
244 'src' => $attachment->guid,
245 'title' => $attachment->post_title,
246 ];
247 }
248
249 public static function _product_tag_sale_badge($settings = null) {
250 global $product;
251 $terms = get_the_terms(get_the_ID(), 'product_tag');
252
253 $badge_position = (isset($settings['badge_position']) && !empty($settings['badge_position'])) ? esc_attr($settings['badge_position']) : 'top-right';
254 $badge_align = (isset($settings['badge_align']) && !empty($settings['badge_align'])) ? esc_attr($settings['badge_align']) : 'horizontal';
255
256 if($product->is_on_sale() || !empty($terms)) : ?>
257 <div class="product-tag-sale-badge position-<?php echo esc_attr($badge_position); ?> align-<?php echo esc_attr($badge_align); ?>">
258 <ul>
259 <?php if(!empty($terms)) : $term = $terms[0];
260 $bg = get_term_meta($term->term_id, 'shopengine_tag_bg_color', true);
261 ?>
262 <?php if(!empty(self::_discount_percentage())) : ?>
263 <li class="badge no-link off"><?php echo '-' . esc_html(self::_discount_percentage()) . '%'; ?></li>
264 <?php endif; ?>
265 <li class="badge tag">
266 <a <?php if(!empty($bg)) : ?>style="background-color:<?php echo esc_attr($bg); ?>" <?php endif; ?>
267 href="<?php echo get_term_link($term->term_id); ?>"><?php echo esc_html($term->name); ?></a>
268 </li>
269 <?php endif;
270
271 if($product->is_on_sale()) {
272 echo "<li class='badge no-link sale'>" . esc_html__('Sale!', 'shopengine') . "</li>";
273 }
274 ?>
275 </ul>
276 </div>
277 <?php
278 endif;
279 }
280
281
282 public static function _product_image($settings = null) {
283 global $product;
284 ?>
285 <div class='product-thumb'>
286 <a href="<?php echo get_the_permalink(); ?>">
287 <?php echo woocommerce_get_product_thumbnail($product->get_id()); ?>
288 </a>
289
290 <!-- end sale date -->
291 <?php
292 if(!empty($settings['counter_position']) && $settings['counter_position'] == 'image') {
293 self::_product_sale_end_date($settings);
294 }
295 ?>
296 <!-- tag and sale badge -->
297 <?php self::_product_tag_sale_badge($settings); ?>
298
299 <!-- show group buttons -->
300 <?php
301 if(isset($settings['shopengine_group_btns']) && $settings['shopengine_group_btns'] === 'yes') {
302
303 $data_attr = apply_filters('shopengine/group_btns/optional_tooltip_data_attr', '');
304
305 ?>
306 <div class="loop-product--btns" <?php echo esc_attr($data_attr)?>>
307 <div class="loop-product--btns-inner">
308 <?php woocommerce_template_loop_add_to_cart(); ?>
309 </div>
310 </div>
311 <?php
312 }
313 ?>
314
315 </div>
316 <?php
317 }
318
319 public static function _product_sale_end_date($settings) {
320 $date = get_post_meta(get_the_id(), '_sale_price_dates_to', true);
321 if(!empty($date)) :
322 $formatted_date = date("Y-m-d", $date);
323 $config = [
324 'days' => esc_html__('Days', 'shopengine'),
325 'hours' => esc_html__('Hours', 'shopengine'),
326 'minutes' => esc_html__('Minutes', 'shopengine'),
327 'seconds' => esc_html__('Seconds', 'shopengine'),
328 ];
329
330 ?>
331 <div data-prefix="<?php echo !empty($settings['counter_prefix']) ? $settings['counter_prefix'] : ''; ?>"
332 class="product-end-sale-timer <?php echo !empty($settings['counter_position']) ? 'counter-position-' . esc_attr($settings['counter_position']) : ''; ?>"
333 data-config='<?php echo json_encode($config); ?>'
334 data-date="<?php echo esc_attr($formatted_date); ?>"></div>
335 <?php
336 endif;
337 }
338
339 public static function _product_category($settings = null) {
340 global $product;
341
342 $terms = get_the_terms($product->get_id(), 'product_cat');
343 $terms_count = count($terms);
344
345 if($terms_count > 0) {
346 echo "<div class='product-category'><ul>";
347 foreach($terms as $key => $term) {
348 $sperator = $key !== ($terms_count - 1) ? ',' : '';
349 echo "<li><a href='" . get_term_link($term->term_id) . "'>" . esc_html($term->name) . $sperator . "</a></li>";
350 }
351 echo "</ul></div>";
352 }
353 }
354
355 public static function _discount_percentage($settings = null) {
356 global $product;
357
358 $product_data = $product->get_data();
359 $show_tag = (isset($settings['show_tag']) && !empty($settings['show_tag'])) ? esc_attr($settings['show_tag']) : 'yes';
360 $output = '';
361 if($show_tag == 'yes' && !empty($product_data['regular_price']) && $product_data['sale_price']) {
362 $percentage = round((($product_data['regular_price'] - $product_data['sale_price']) / $product_data['regular_price']) * 100);
363
364 return $percentage;
365 }
366
367 return '';
368 }
369
370
371 public static function _product_title($settings = null) {
372 ?>
373 <h3 class='product-title'>
374 <a href="<?php echo get_the_permalink(); ?>"><?php echo get_the_title(); ?></a>
375 </h3>
376 <?php
377 }
378
379 public static function _product_rating($settings = null) {
380
381 global $product;
382 ?>
383 <div class="product-rating">
384 <?php
385 if($product->get_rating_count() > 0) {
386 woocommerce_template_loop_rating();
387 } else {
388 echo sprintf('<div class="star-rating">%1$s</div>', wc_get_star_rating_html(0, 0));
389 }
390
391 // review count
392 echo sprintf('<span class="rating-count">(%1$s)</span>', $product->get_review_count());
393 ?>
394 </div>
395 <?php
396 }
397
398
399 public static function _product_price($settings = null) {
400 ?>
401 <div class="product-price">
402 <?php woocommerce_template_single_price(); ?>
403 </div>
404 <?php
405 }
406
407 public static function _product_description($settings = null) {
408 global $product;
409 $product_data = $product->get_data($product->get_id());
410 ?>
411 <div class="prodcut-description">
412 <?php echo apply_filters('shopengine_product_short_description', $product_data['description']); ?>
413 </div>
414
415 <?php
416 }
417
418 public static function _product_buttons($settings = null) {
419
420 if( isset($settings['shopengine_group_btns']) && $settings['shopengine_group_btns'] === 'yes'): ?>
421 <div class="add-to-cart-bt">
422 <?php woocommerce_template_loop_add_to_cart(); ?>
423 </div>
424 <?php endif;
425 }
426
427
428 /**
429 * todo - check the keys for refund and cancelled [wc-cancelled, wc-refunded ]
430 *
431 * @param $product_id
432 * @param int $variation_id
433 * @return int
434 */
435 public static function get_total_sale_count($product_id, $variation_id = 0) {
436
437 global $wpdb;
438
439 $qry = 'SELECT sum(lookup.product_qty) as total FROM `'.$wpdb->prefix.'wc_order_product_lookup` as lookup';
440 $qry .=' LEFT JOIN '.$wpdb->prefix.'wc_order_stats AS stat on lookup.order_id = stat.order_id ';
441 $qry .=' WHERE `product_id` = '.intval($product_id).' and variation_id = '.intval($variation_id);
442 $qry .= ' and stat.status NOT IN (\'wc-cancelled\', \'wc-refunded\') ;';
443 $result = $wpdb->get_row($qry);
444 $total = is_object($result) ? $result->total : 0;
445
446 return intval( $total ) ;
447 }
448
449
450 public static function is_guest_checkout_allowed() {
451
452 return 'yes' === get_option('woocommerce_enable_guest_checkout');
453 }
454
455 public static function is_login_allowed_during_checkout() {
456
457 return 'yes' === get_option('woocommerce_enable_checkout_login_reminder');
458 }
459
460
461 private static function generate_products_meta() {
462 global $wpdb;
463 $post_type = 'product';
464 $query = "
465 SELECT DISTINCT($wpdb->postmeta.meta_key)
466 FROM $wpdb->posts
467 LEFT JOIN $wpdb->postmeta
468 ON $wpdb->posts.ID = $wpdb->postmeta.post_id
469 WHERE $wpdb->posts.post_type = '%s'
470 AND $wpdb->postmeta.meta_key != ''
471 AND $wpdb->postmeta.meta_key NOT RegExp '(^[_0-9].+$)'
472 AND $wpdb->postmeta.meta_key NOT RegExp '(^[0-9]+$)'
473 ";
474 $meta_keys = $wpdb->get_col( $wpdb->prepare( $query, $post_type ) );
475 //set_transient( 'shopengine-all-products_meta_keys', $meta_keys, 60 * 60 * 0.01 );
476
477 return $meta_keys;
478 }
479
480 public static function get_products_meta_keys() {
481 //$cache = get_transient( 'shopengine-all-products_meta_keys' );
482 return static::generate_products_meta();
483 }
484
485
486
487 public static function get_template_type($pid) {
488
489 return get_post_meta($pid, Action::get_meta_key_for_type(), true);
490 }
491
492 public static function get_template_builder_type($pid) {
493
494 return get_post_meta($pid, Action::get_meta_key_for_edit_with(), true);
495 }
496 }
497