banner
2 years ago
emailkit
1 year ago
notice
1 year ago
plugins
2 years ago
pro-awareness
1 year ago
stories
2 years ago
controls-helper.php
4 years ago
elementor-data-map.php
4 years ago
global-helper.php
3 years ago
helper.php
1 year ago
notice.php
1 year ago
shipping-calculation.php
3 years ago
util.php
2 years ago
helper.php
555 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 | $content = $content ?? ''; |
| 234 | if(stripos($content, "shopengine-has-lisence") !== false) { |
| 235 | return null; |
| 236 | } |
| 237 | |
| 238 | return $content; |
| 239 | } |
| 240 | |
| 241 | public static function render_elementor_content($content_id) { |
| 242 | $elementor_instance = \Elementor\Plugin::instance(); |
| 243 | |
| 244 | return $elementor_instance->frontend->get_builder_content_for_display($content_id); |
| 245 | } |
| 246 | |
| 247 | public static function esc_options($str, $options = [], $default = '') { |
| 248 | if(!in_array($str, $options)) { |
| 249 | return $default; |
| 250 | } |
| 251 | |
| 252 | return $str; |
| 253 | } |
| 254 | |
| 255 | public static function img_meta($id) { |
| 256 | $attachment = get_post($id); |
| 257 | if($attachment == null || $attachment->post_type != 'attachment') { |
| 258 | return null; |
| 259 | } |
| 260 | |
| 261 | return [ |
| 262 | 'alt' => get_post_meta($attachment->ID, '_wp_attachment_image_alt', true), |
| 263 | 'caption' => $attachment->post_excerpt, |
| 264 | 'description' => $attachment->post_content, |
| 265 | 'href' => get_permalink($attachment->ID), |
| 266 | 'src' => $attachment->guid, |
| 267 | 'title' => $attachment->post_title, |
| 268 | ]; |
| 269 | } |
| 270 | |
| 271 | public static function _product_tag_sale_badge($settings = null) { |
| 272 | global $product; |
| 273 | $terms = get_the_terms(get_the_ID(), 'product_tag'); |
| 274 | |
| 275 | $badge_position = (isset($settings['badge_position']) && !empty($settings['badge_position'])) ? esc_attr($settings['badge_position']) : 'top-right'; |
| 276 | $badge_align = (isset($settings['badge_align']) && !empty($settings['badge_align'])) ? esc_attr($settings['badge_align']) : 'horizontal'; |
| 277 | |
| 278 | if($product->is_on_sale() || !empty($terms)) : ?> |
| 279 | <div class="product-tag-sale-badge position-<?php echo esc_attr($badge_position); ?> align-<?php echo esc_attr($badge_align); ?>"> |
| 280 | <ul> |
| 281 | <?php if(!empty($terms)) : $term = $terms[0]; |
| 282 | $bg = get_term_meta($term->term_id, 'shopengine_tag_bg_color', true); |
| 283 | ?> |
| 284 | <?php if(!empty(self::_discount_percentage())) : ?> |
| 285 | <li class="badge no-link off"><?php echo '-' . esc_html(self::_discount_percentage()) . '%'; ?></li> |
| 286 | <?php endif; ?> |
| 287 | <li class="badge tag"> |
| 288 | <a title="<?php echo esc_html($term->name,'shopengine');?>" <?php if(!empty($bg)) : ?>style="background-color:<?php echo esc_attr($bg); ?>" <?php endif; ?> |
| 289 | href="<?php echo esc_url(get_term_link($term->term_id)); ?>"><?php echo esc_html($term->name); ?></a> |
| 290 | </li> |
| 291 | <?php endif; |
| 292 | |
| 293 | if($product->is_on_sale()) { |
| 294 | echo "<li class='badge no-link sale'>" . esc_html__('Sale!', 'shopengine') . "</li>"; |
| 295 | } |
| 296 | ?> |
| 297 | </ul> |
| 298 | </div> |
| 299 | <?php |
| 300 | endif; |
| 301 | } |
| 302 | |
| 303 | public static function _product_image($settings = null) { |
| 304 | global $product; |
| 305 | ?> |
| 306 | <div class='product-thumb'> |
| 307 | <a title="<?php esc_html_e('Product Thumbnail','shopengine')?>" href="<?php echo esc_url(get_the_permalink($product->get_id())); ?>"> |
| 308 | <?php shopengine_content_render(woocommerce_get_product_thumbnail($product->get_id())) ?> |
| 309 | </a> |
| 310 | |
| 311 | <!-- end sale date --> |
| 312 | <?php |
| 313 | if(!empty($settings['counter_position']) && $settings['counter_position'] == 'image') { |
| 314 | self::_product_sale_end_date($settings); |
| 315 | } |
| 316 | ?> |
| 317 | <!-- tag and sale badge --> |
| 318 | <?php self::_product_tag_sale_badge($settings); ?> |
| 319 | |
| 320 | <!-- show group buttons --> |
| 321 | <?php |
| 322 | if(isset($settings['shopengine_group_btns']) && $settings['shopengine_group_btns'] === 'yes') { |
| 323 | |
| 324 | $data_attr = apply_filters('shopengine/group_btns/optional_tooltip_data_attr', ''); |
| 325 | |
| 326 | ?> |
| 327 | <div class="loop-product--btns" <?php echo esc_attr($data_attr)?>> |
| 328 | <div class="loop-product--btns-inner"> |
| 329 | <?php woocommerce_template_loop_add_to_cart(); ?> |
| 330 | </div> |
| 331 | </div> |
| 332 | <?php |
| 333 | } |
| 334 | ?> |
| 335 | |
| 336 | </div> |
| 337 | <?php |
| 338 | } |
| 339 | |
| 340 | public static function _product_sale_end_date($settings) { |
| 341 | $date = get_post_meta(get_the_id(), '_sale_price_dates_to', true); |
| 342 | if(!empty($date)) : |
| 343 | $formatted_date = date("Y-m-d", $date); |
| 344 | $config = [ |
| 345 | 'days' => esc_html__('Days', 'shopengine'), |
| 346 | 'hours' => esc_html__('Hours', 'shopengine'), |
| 347 | 'minutes' => esc_html__('Minutes', 'shopengine'), |
| 348 | 'seconds' => esc_html__('Seconds', 'shopengine'), |
| 349 | ]; |
| 350 | |
| 351 | ?> |
| 352 | <div data-prefix="<?php echo !empty($settings['counter_prefix']) ? esc_attr($settings['counter_prefix']) : ''; ?>" |
| 353 | class="product-end-sale-timer <?php echo !empty($settings['counter_position']) ? 'counter-position-' . esc_attr($settings['counter_position']) : ''; ?>" |
| 354 | data-config='<?php echo json_encode($config); ?>' |
| 355 | data-date="<?php echo esc_attr($formatted_date); ?>"></div> |
| 356 | <?php |
| 357 | endif; |
| 358 | } |
| 359 | |
| 360 | public static function _product_category($settings = null) { |
| 361 | global $product; |
| 362 | |
| 363 | $terms = get_the_terms($product->get_id(), 'product_cat'); |
| 364 | if($terms && is_array($terms)){ |
| 365 | $terms_count = count($terms); |
| 366 | $category = esc_html__('Product Category','shopengine'); |
| 367 | |
| 368 | if($terms_count > 0) { |
| 369 | echo "<div class='product-category'><ul>"; |
| 370 | foreach($terms as $key => $term) { |
| 371 | $sperator = $key !== ($terms_count - 1) ? ',' : ''; |
| 372 | echo "<li><a title='" . esc_attr($category) . "' href='" . esc_url(get_term_link($term->term_id)) . "'>" . esc_html($term->name) . esc_html($sperator) . "</a></li>"; |
| 373 | } |
| 374 | echo "</ul></div>"; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | public static function _discount_percentage($settings = null) { |
| 380 | global $product; |
| 381 | |
| 382 | $product_data = $product->get_data(); |
| 383 | $show_tag = (isset($settings['show_tag']) && !empty($settings['show_tag'])) ? esc_attr($settings['show_tag']) : 'yes'; |
| 384 | $output = ''; |
| 385 | if($show_tag == 'yes' && !empty($product_data['regular_price']) && $product_data['sale_price']) { |
| 386 | $percentage = round((($product_data['regular_price'] - $product_data['sale_price']) / $product_data['regular_price']) * 100); |
| 387 | |
| 388 | return $percentage; |
| 389 | } |
| 390 | |
| 391 | return ''; |
| 392 | } |
| 393 | |
| 394 | |
| 395 | public static function _product_title($settings = null) { |
| 396 | global $product; |
| 397 | ?> |
| 398 | <h3 class='product-title'> |
| 399 | <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> |
| 400 | </h3> |
| 401 | <?php |
| 402 | } |
| 403 | |
| 404 | public static function _product_rating($settings = null) { |
| 405 | |
| 406 | global $product; |
| 407 | ?> |
| 408 | <div class="product-rating"> |
| 409 | <?php |
| 410 | if($product->get_rating_count() > 0) { |
| 411 | woocommerce_template_loop_rating(); |
| 412 | } else { |
| 413 | shopengine_content_render(sprintf('<div class="star-rating">%1$s</div>', wc_get_star_rating_html(0, 0))); |
| 414 | } |
| 415 | |
| 416 | // review count |
| 417 | shopengine_content_render(sprintf('<span class="rating-count">(%1$s)</span>', $product->get_review_count())); |
| 418 | ?> |
| 419 | </div> |
| 420 | <?php |
| 421 | } |
| 422 | |
| 423 | |
| 424 | public static function _product_price($settings = null) { |
| 425 | ?> |
| 426 | <div class="product-price"> |
| 427 | <?php woocommerce_template_single_price(); ?> |
| 428 | </div> |
| 429 | <?php |
| 430 | } |
| 431 | |
| 432 | public static function _product_description($settings = null) { |
| 433 | global $product; |
| 434 | $product_data = $product->get_data($product->get_id()); |
| 435 | ?> |
| 436 | <div class="prodcut-description"> |
| 437 | <?php shopengine_content_render(apply_filters('shopengine_product_short_description', $product_data['description']))?> |
| 438 | </div> |
| 439 | |
| 440 | <?php |
| 441 | } |
| 442 | |
| 443 | public static function _product_buttons($settings = null) { |
| 444 | |
| 445 | if( isset($settings['shopengine_group_btns']) && $settings['shopengine_group_btns'] === 'yes'): ?> |
| 446 | <div class="add-to-cart-bt"> |
| 447 | <?php woocommerce_template_loop_add_to_cart(); ?> |
| 448 | </div> |
| 449 | <?php endif; |
| 450 | } |
| 451 | |
| 452 | |
| 453 | /** |
| 454 | * todo - check the keys for refund and cancelled [wc-cancelled, wc-refunded ] |
| 455 | * |
| 456 | * @param $product_id |
| 457 | * @param int $variation_id |
| 458 | * @return int |
| 459 | */ |
| 460 | public static function get_total_sale_count($product_id, $variation_id = 0) { |
| 461 | |
| 462 | global $wpdb; |
| 463 | |
| 464 | $result = $wpdb->get_row( $wpdb->prepare( "SELECT sum(lookup.product_qty) as total FROM `'.$wpdb->prefix.'wc_order_product_lookup` as lookup |
| 465 | LEFT JOIN '.$wpdb->prefix.'wc_order_stats AS stat on lookup.order_id = stat.order_id |
| 466 | WHERE `product_id` = %d and variation_id = %d |
| 467 | and stat.status NOT IN (\'wc-cancelled\', \'wc-refunded\')", intval( $product_id ), intval( $variation_id ) ) ); |
| 468 | $total = is_object( $result ) ? $result->total : 0; |
| 469 | |
| 470 | return intval( $total ); |
| 471 | } |
| 472 | |
| 473 | |
| 474 | public static function is_guest_checkout_allowed() { |
| 475 | |
| 476 | return 'yes' === get_option('woocommerce_enable_guest_checkout'); |
| 477 | } |
| 478 | |
| 479 | public static function is_login_allowed_during_checkout() { |
| 480 | |
| 481 | return 'yes' === get_option('woocommerce_enable_checkout_login_reminder'); |
| 482 | } |
| 483 | |
| 484 | |
| 485 | private static function generate_products_meta() { |
| 486 | global $wpdb; |
| 487 | $post_type = 'product'; |
| 488 | $meta_keys = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT($wpdb->postmeta.meta_key) |
| 489 | FROM $wpdb->posts |
| 490 | LEFT JOIN $wpdb->postmeta |
| 491 | ON $wpdb->posts.ID = $wpdb->postmeta.post_id |
| 492 | WHERE $wpdb->posts.post_type = %s |
| 493 | AND $wpdb->postmeta.meta_key != '' |
| 494 | AND $wpdb->postmeta.meta_key NOT RegExp '(^[_0-9].+$)' |
| 495 | AND $wpdb->postmeta.meta_key NOT RegExp '(^[0-9]+$)'", $post_type ) ); |
| 496 | //set_transient( 'shopengine-all-products_meta_keys', $meta_keys, 60 * 60 * 0.01 ); |
| 497 | |
| 498 | return $meta_keys; |
| 499 | } |
| 500 | |
| 501 | public static function get_products_meta_keys() { |
| 502 | //$cache = get_transient( 'shopengine-all-products_meta_keys' ); |
| 503 | return static::generate_products_meta(); |
| 504 | } |
| 505 | |
| 506 | |
| 507 | |
| 508 | public static function get_template_type($pid) { |
| 509 | |
| 510 | return get_post_meta($pid, Action::get_meta_key_for_type(), true); |
| 511 | } |
| 512 | |
| 513 | public static function get_template_builder_type($pid) { |
| 514 | |
| 515 | return get_post_meta($pid, Action::get_meta_key_for_edit_with(), true); |
| 516 | } |
| 517 | |
| 518 | public static function get_admin_list_template_url(){ |
| 519 | return get_admin_url(null, 'edit.php?post_type=' . Template_Cpt::TYPE . Template_Cpt ::TEMPLATE_SECTION_ID); |
| 520 | } |
| 521 | |
| 522 | public static function get_checkout_input_fields(string $form_type = 'billing') |
| 523 | { |
| 524 | $checkout = WC()->checkout(); |
| 525 | $fields = $checkout->get_checkout_fields($form_type); |
| 526 | |
| 527 | $billing_fields = []; |
| 528 | |
| 529 | foreach ($fields as $key => $field) { |
| 530 | $label = isset($field['label']) ? $field['label'] : ''; |
| 531 | $array = ['list_key' => $key, 'list_title' => $label]; |
| 532 | array_push($billing_fields, $array); |
| 533 | } |
| 534 | |
| 535 | return $billing_fields; |
| 536 | } |
| 537 | |
| 538 | public static function order_checkout_fields($settings, $form_type = 'billing') |
| 539 | { |
| 540 | WC()->checkout()->checkout_fields = null; |
| 541 | |
| 542 | add_filter("woocommerce_{$form_type}_fields", function ($fields) use ($settings) { |
| 543 | $priority = 10; |
| 544 | foreach ($settings as $input) { |
| 545 | $key = $input['list_key']; |
| 546 | if (isset($fields[$key])) { |
| 547 | $fields[$key]['priority'] = $priority; |
| 548 | $priority += 10; |
| 549 | } |
| 550 | } |
| 551 | return $fields; |
| 552 | }); |
| 553 | } |
| 554 | } |
| 555 |