controls-helper.php
4 years ago
elementor-data-map.php
4 years ago
helper.php
4 years ago
notice.php
4 years ago
shipping-calculation.php
4 years ago
helper.php
494 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 kses($raw) { |
| 76 | |
| 77 | $allowed_tags = [ |
| 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 | if(function_exists('wp_kses')) { // WP is here |
| 175 | return wp_kses($raw, $allowed_tags); |
| 176 | } else { |
| 177 | return $raw; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | public static function kspan($text) { |
| 182 | return str_replace(['{', '}'], ['<span>', '</span>'], self::kses($text)); |
| 183 | } |
| 184 | |
| 185 | public static function category_list_by_taxonomy($taxonomy = '') { |
| 186 | $query_args = [ |
| 187 | 'orderby' => 'ID', |
| 188 | 'order' => 'DESC', |
| 189 | 'hide_empty' => 1, |
| 190 | 'taxonomy' => $taxonomy, |
| 191 | ]; |
| 192 | |
| 193 | $categories = get_categories($query_args); |
| 194 | |
| 195 | return $categories; |
| 196 | } |
| 197 | |
| 198 | public static function trim_words($text, $num_words) { |
| 199 | return wp_trim_words($text, $num_words, ''); |
| 200 | } |
| 201 | |
| 202 | public static function array_push_assoc($array, $key, $value) { |
| 203 | $array[$key] = $value; |
| 204 | |
| 205 | return $array; |
| 206 | } |
| 207 | |
| 208 | public static function render($content) { |
| 209 | if(stripos($content, "shopengine-has-lisence") !== false) { |
| 210 | return null; |
| 211 | } |
| 212 | |
| 213 | return $content; |
| 214 | } |
| 215 | |
| 216 | public static function render_elementor_content($content_id) { |
| 217 | $elementor_instance = \Elementor\Plugin::instance(); |
| 218 | |
| 219 | return $elementor_instance->frontend->get_builder_content_for_display($content_id); |
| 220 | } |
| 221 | |
| 222 | public static function esc_options($str, $options = [], $default = '') { |
| 223 | if(!in_array($str, $options)) { |
| 224 | return $default; |
| 225 | } |
| 226 | |
| 227 | return $str; |
| 228 | } |
| 229 | |
| 230 | public static function img_meta($id) { |
| 231 | $attachment = get_post($id); |
| 232 | if($attachment == null || $attachment->post_type != 'attachment') { |
| 233 | return null; |
| 234 | } |
| 235 | |
| 236 | return [ |
| 237 | 'alt' => get_post_meta($attachment->ID, '_wp_attachment_image_alt', true), |
| 238 | 'caption' => $attachment->post_excerpt, |
| 239 | 'description' => $attachment->post_content, |
| 240 | 'href' => get_permalink($attachment->ID), |
| 241 | 'src' => $attachment->guid, |
| 242 | 'title' => $attachment->post_title, |
| 243 | ]; |
| 244 | } |
| 245 | |
| 246 | public static function _product_tag_sale_badge($settings = null) { |
| 247 | global $product; |
| 248 | $terms = get_the_terms(get_the_ID(), 'product_tag'); |
| 249 | |
| 250 | $badge_position = (isset($settings['badge_position']) && !empty($settings['badge_position'])) ? esc_attr($settings['badge_position']) : 'top-right'; |
| 251 | $badge_align = (isset($settings['badge_align']) && !empty($settings['badge_align'])) ? esc_attr($settings['badge_align']) : 'horizontal'; |
| 252 | |
| 253 | if($product->is_on_sale() || !empty($terms)) : ?> |
| 254 | <div class="product-tag-sale-badge position-<?php echo esc_attr($badge_position); ?> align-<?php echo esc_attr($badge_align); ?>"> |
| 255 | <ul> |
| 256 | <?php if(!empty($terms)) : $term = $terms[0]; |
| 257 | $bg = get_term_meta($term->term_id, 'shopengine_tag_bg_color', true); |
| 258 | ?> |
| 259 | <?php if(!empty(self::_discount_percentage())) : ?> |
| 260 | <li class="badge no-link off"><?php echo '-' . esc_html(self::_discount_percentage()) . '%'; ?></li> |
| 261 | <?php endif; ?> |
| 262 | <li class="badge tag"> |
| 263 | <a <?php if(!empty($bg)) : ?>style="background-color:<?php echo esc_attr($bg); ?>" <?php endif; ?> |
| 264 | href="<?php echo get_term_link($term->term_id); ?>"><?php echo esc_html($term->name); ?></a> |
| 265 | </li> |
| 266 | <?php endif; |
| 267 | |
| 268 | if($product->is_on_sale()) { |
| 269 | echo "<li class='badge no-link sale'>" . esc_html__('Sale!', 'shopengine') . "</li>"; |
| 270 | } |
| 271 | ?> |
| 272 | </ul> |
| 273 | </div> |
| 274 | <?php |
| 275 | endif; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | public static function _product_image($settings = null) { |
| 280 | global $product; |
| 281 | ?> |
| 282 | <div class='product-thumb'> |
| 283 | <a href="<?php echo get_the_permalink(); ?>"> |
| 284 | <?php echo woocommerce_get_product_thumbnail($product->get_id()); ?> |
| 285 | </a> |
| 286 | |
| 287 | <!-- end sale date --> |
| 288 | <?php |
| 289 | if(!empty($settings['counter_position']) && $settings['counter_position'] == 'image') { |
| 290 | self::_product_sale_end_date($settings); |
| 291 | } |
| 292 | ?> |
| 293 | <!-- tag and sale badge --> |
| 294 | <?php self::_product_tag_sale_badge($settings); ?> |
| 295 | |
| 296 | <!-- show group buttons --> |
| 297 | <?php |
| 298 | if(isset($settings['shopengine_group_btns']) && $settings['shopengine_group_btns'] === 'yes') { |
| 299 | |
| 300 | $data_attr = apply_filters('shopengine/group_btns/optional_tooltip_data_attr', ''); |
| 301 | |
| 302 | ?> |
| 303 | <div class="loop-product--btns" <?php echo esc_attr($data_attr)?>> |
| 304 | <div class="loop-product--btns-inner"> |
| 305 | <?php woocommerce_template_loop_add_to_cart(); ?> |
| 306 | </div> |
| 307 | </div> |
| 308 | <?php |
| 309 | } |
| 310 | ?> |
| 311 | |
| 312 | </div> |
| 313 | <?php |
| 314 | } |
| 315 | |
| 316 | public static function _product_sale_end_date($settings) { |
| 317 | $date = get_post_meta(get_the_id(), '_sale_price_dates_to', true); |
| 318 | if(!empty($date)) : |
| 319 | $formatted_date = date("Y-m-d", $date); |
| 320 | $config = [ |
| 321 | 'days' => esc_html__('Days', 'shopengine'), |
| 322 | 'hours' => esc_html__('Hours', 'shopengine'), |
| 323 | 'minutes' => esc_html__('Minutes', 'shopengine'), |
| 324 | 'seconds' => esc_html__('Seconds', 'shopengine'), |
| 325 | ]; |
| 326 | |
| 327 | ?> |
| 328 | <div data-prefix="<?php echo !empty($settings['counter_prefix']) ? $settings['counter_prefix'] : ''; ?>" |
| 329 | class="product-end-sale-timer <?php echo !empty($settings['counter_position']) ? 'counter-position-' . esc_attr($settings['counter_position']) : ''; ?>" |
| 330 | data-config='<?php echo json_encode($config); ?>' |
| 331 | data-date="<?php echo esc_attr($formatted_date); ?>"></div> |
| 332 | <?php |
| 333 | endif; |
| 334 | } |
| 335 | |
| 336 | public static function _product_category($settings = null) { |
| 337 | global $product; |
| 338 | |
| 339 | $terms = get_the_terms($product->get_id(), 'product_cat'); |
| 340 | $terms_count = count($terms); |
| 341 | |
| 342 | if($terms_count > 0) { |
| 343 | echo "<div class='product-category'><ul>"; |
| 344 | foreach($terms as $key => $term) { |
| 345 | $sperator = $key !== ($terms_count - 1) ? ',' : ''; |
| 346 | echo "<li><a href='" . get_term_link($term->term_id) . "'>" . esc_html($term->name) . $sperator . "</a></li>"; |
| 347 | } |
| 348 | echo "</ul></div>"; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | public static function _discount_percentage($settings = null) { |
| 353 | global $product; |
| 354 | |
| 355 | $product_data = $product->get_data(); |
| 356 | $show_tag = (isset($settings['show_tag']) && !empty($settings['show_tag'])) ? esc_attr($settings['show_tag']) : 'yes'; |
| 357 | $output = ''; |
| 358 | if($show_tag == 'yes' && !empty($product_data['regular_price']) && $product_data['sale_price']) { |
| 359 | $percentage = round((($product_data['regular_price'] - $product_data['sale_price']) / $product_data['regular_price']) * 100); |
| 360 | |
| 361 | return $percentage; |
| 362 | } |
| 363 | |
| 364 | return ''; |
| 365 | } |
| 366 | |
| 367 | |
| 368 | public static function _product_title($settings = null) { |
| 369 | ?> |
| 370 | <h3 class='product-title'> |
| 371 | <a href="<?php echo get_the_permalink(); ?>"><?php echo get_the_title(); ?></a> |
| 372 | </h3> |
| 373 | <?php |
| 374 | } |
| 375 | |
| 376 | public static function _product_rating($settings = null) { |
| 377 | |
| 378 | global $product; |
| 379 | ?> |
| 380 | <div class="product-rating"> |
| 381 | <?php |
| 382 | if($product->get_rating_count() > 0) { |
| 383 | woocommerce_template_loop_rating(); |
| 384 | } else { |
| 385 | echo sprintf('<div class="star-rating">%1$s</div>', wc_get_star_rating_html(0, 0)); |
| 386 | } |
| 387 | |
| 388 | // review count |
| 389 | echo sprintf('<span class="rating-count">(%1$s)</span>', $product->get_review_count()); |
| 390 | ?> |
| 391 | </div> |
| 392 | <?php |
| 393 | } |
| 394 | |
| 395 | |
| 396 | public static function _product_price($settings = null) { |
| 397 | ?> |
| 398 | <div class="product-price"> |
| 399 | <?php woocommerce_template_single_price(); ?> |
| 400 | </div> |
| 401 | <?php |
| 402 | } |
| 403 | |
| 404 | public static function _product_description($settings = null) { |
| 405 | global $product; |
| 406 | $product_data = $product->get_data($product->get_id()); |
| 407 | ?> |
| 408 | <div class="prodcut-description"> |
| 409 | <?php echo apply_filters('shopengine_product_short_description', $product_data['description']); ?> |
| 410 | </div> |
| 411 | |
| 412 | <?php |
| 413 | } |
| 414 | |
| 415 | public static function _product_buttons($settings = null) { |
| 416 | |
| 417 | if( isset($settings['shopengine_group_btns']) && $settings['shopengine_group_btns'] === 'yes'): else: ?> |
| 418 | <div class="add-to-cart-bt"> |
| 419 | <?php woocommerce_template_loop_add_to_cart(); ?> |
| 420 | </div> |
| 421 | <?php endif; |
| 422 | } |
| 423 | |
| 424 | |
| 425 | /** |
| 426 | * todo - check the keys for refund and cancelled [wc-cancelled, wc-refunded ] |
| 427 | * |
| 428 | * @param $product_id |
| 429 | * @param int $variation_id |
| 430 | * @return int |
| 431 | */ |
| 432 | public static function get_total_sale_count($product_id, $variation_id = 0) { |
| 433 | |
| 434 | global $wpdb; |
| 435 | |
| 436 | $qry = 'SELECT sum(lookup.product_qty) as total FROM `'.$wpdb->prefix.'wc_order_product_lookup` as lookup'; |
| 437 | $qry .=' LEFT JOIN '.$wpdb->prefix.'wc_order_stats AS stat on lookup.order_id = stat.order_id '; |
| 438 | $qry .=' WHERE `product_id` = '.intval($product_id).' and variation_id = '.intval($variation_id); |
| 439 | $qry .= ' and stat.status NOT IN (\'wc-cancelled\', \'wc-refunded\') ;'; |
| 440 | $result = $wpdb->get_row($qry); |
| 441 | $total = is_object($result) ? $result->total : 0; |
| 442 | |
| 443 | return intval( $total ) ; |
| 444 | } |
| 445 | |
| 446 | |
| 447 | public static function is_guest_checkout_allowed() { |
| 448 | |
| 449 | return 'yes' === get_option('woocommerce_enable_guest_checkout'); |
| 450 | } |
| 451 | |
| 452 | public static function is_login_allowed_during_checkout() { |
| 453 | |
| 454 | return 'yes' === get_option('woocommerce_enable_checkout_login_reminder'); |
| 455 | } |
| 456 | |
| 457 | |
| 458 | private static function generate_products_meta() { |
| 459 | global $wpdb; |
| 460 | $post_type = 'product'; |
| 461 | $query = " |
| 462 | SELECT DISTINCT($wpdb->postmeta.meta_key) |
| 463 | FROM $wpdb->posts |
| 464 | LEFT JOIN $wpdb->postmeta |
| 465 | ON $wpdb->posts.ID = $wpdb->postmeta.post_id |
| 466 | WHERE $wpdb->posts.post_type = '%s' |
| 467 | AND $wpdb->postmeta.meta_key != '' |
| 468 | AND $wpdb->postmeta.meta_key NOT RegExp '(^[_0-9].+$)' |
| 469 | AND $wpdb->postmeta.meta_key NOT RegExp '(^[0-9]+$)' |
| 470 | "; |
| 471 | $meta_keys = $wpdb->get_col( $wpdb->prepare( $query, $post_type ) ); |
| 472 | //set_transient( 'shopengine-all-products_meta_keys', $meta_keys, 60 * 60 * 0.01 ); |
| 473 | |
| 474 | return $meta_keys; |
| 475 | } |
| 476 | |
| 477 | public static function get_products_meta_keys() { |
| 478 | //$cache = get_transient( 'shopengine-all-products_meta_keys' ); |
| 479 | return static::generate_products_meta(); |
| 480 | } |
| 481 | |
| 482 | |
| 483 | |
| 484 | public static function get_template_type($pid) { |
| 485 | |
| 486 | return get_post_meta($pid, Action::get_meta_key_for_type(), true); |
| 487 | } |
| 488 | |
| 489 | public static function get_template_builder_type($pid) { |
| 490 | |
| 491 | return get_post_meta($pid, Action::get_meta_key_for_edit_with(), true); |
| 492 | } |
| 493 | } |
| 494 |