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