controls-helper.php
4 years ago
elementor-data-map.php
5 years ago
helper.php
4 years ago
notice.php
5 years ago
helper.php
461 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Utils; |
| 4 | |
| 5 | |
| 6 | defined('ABSPATH') || exit; |
| 7 | |
| 8 | /** |
| 9 | * Global helper class. |
| 10 | * |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | class Helper { |
| 14 | |
| 15 | public static function conditional_enqueue_quickview($template_type = 'quick_view') { |
| 16 | |
| 17 | $idd = \ShopEngine\Core\Builders\Templates::get_registered_template_id($template_type); |
| 18 | |
| 19 | $tm = get_post_modified_time('U', false, $idd); |
| 20 | |
| 21 | if(!empty($idd)) { |
| 22 | |
| 23 | $url = Helper::get_elementor_css_uri($idd); |
| 24 | |
| 25 | wp_enqueue_style('quickview-fly-' . $idd, $url, [], $tm); |
| 26 | |
| 27 | /** |
| 28 | * If shop page is not designed with shopengine but quickview is then some css is missing |
| 29 | * to resolve this we are enqueueing the elementor css |
| 30 | */ |
| 31 | wp_enqueue_style('elementor-frontend'); |
| 32 | wp_enqueue_script('wc-add-to-cart-variation'); |
| 33 | wp_enqueue_script('zoom'); |
| 34 | |
| 35 | /** |
| 36 | * photoswipe only load only on product single page but quickview can be load anywhere |
| 37 | * to resolve this we are enqueueing the photoswipe css, js and template |
| 38 | */ |
| 39 | wp_enqueue_style('photoswipe'); |
| 40 | wp_enqueue_style('photoswipe-default-skin'); |
| 41 | wp_enqueue_script('photoswipe'); |
| 42 | wp_enqueue_script('photoswipe-ui-default'); |
| 43 | add_action( 'wp_footer', 'woocommerce_photoswipe' ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public static function get_elementor_css_uri($pid) { |
| 48 | |
| 49 | global $blog_id; |
| 50 | |
| 51 | $wp_upload_dir = wp_upload_dir(null, false); |
| 52 | |
| 53 | $base = $wp_upload_dir['baseurl'] . '/elementor/css/'; |
| 54 | |
| 55 | return set_url_scheme($base . 'post-' . $pid . '.css'); |
| 56 | } |
| 57 | |
| 58 | public static function add_to_url($url, $param) { |
| 59 | $info = parse_url( $url ); |
| 60 | $query = []; |
| 61 | |
| 62 | if(isset($info['query'])){ |
| 63 | parse_str( $info['query'], $query ); |
| 64 | } |
| 65 | return $info['scheme'] . '://' . $info['host'] . $info['path'] . '?' . http_build_query( $query ? array_merge( $query, $param ) : $param ); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * Auto generate classname from path. |
| 71 | * |
| 72 | * @since 1.0.0 |
| 73 | * @access public |
| 74 | */ |
| 75 | public static function make_classname($dirname) { |
| 76 | $dirname = pathinfo($dirname, PATHINFO_FILENAME); |
| 77 | $class_name = explode('-', $dirname); |
| 78 | $class_name = array_map('ucfirst', $class_name); |
| 79 | $class_name = implode('_', $class_name); |
| 80 | |
| 81 | return $class_name; |
| 82 | } |
| 83 | |
| 84 | public static function kses($raw) { |
| 85 | |
| 86 | $allowed_tags = [ |
| 87 | 'a' => [ |
| 88 | 'class' => [], |
| 89 | 'href' => [], |
| 90 | 'rel' => [], |
| 91 | 'title' => [], |
| 92 | ], |
| 93 | 'abbr' => [ |
| 94 | 'title' => [], |
| 95 | ], |
| 96 | 'b' => [], |
| 97 | 'blockquote' => [ |
| 98 | 'cite' => [], |
| 99 | ], |
| 100 | 'cite' => [ |
| 101 | 'title' => [], |
| 102 | ], |
| 103 | 'code' => [], |
| 104 | 'del' => [ |
| 105 | 'datetime' => [], |
| 106 | 'title' => [], |
| 107 | ], |
| 108 | 'dd' => [], |
| 109 | 'div' => [ |
| 110 | 'class' => [], |
| 111 | 'title' => [], |
| 112 | 'style' => [], |
| 113 | ], |
| 114 | 'dl' => [], |
| 115 | 'dt' => [], |
| 116 | 'em' => [], |
| 117 | 'h1' => [ |
| 118 | 'class' => [], |
| 119 | ], |
| 120 | 'h2' => [ |
| 121 | 'class' => [], |
| 122 | ], |
| 123 | 'h3' => [ |
| 124 | 'class' => [], |
| 125 | ], |
| 126 | 'h4' => [ |
| 127 | 'class' => [], |
| 128 | ], |
| 129 | 'h5' => [ |
| 130 | 'class' => [], |
| 131 | ], |
| 132 | 'h6' => [ |
| 133 | 'class' => [], |
| 134 | ], |
| 135 | 'i' => [ |
| 136 | 'class' => [], |
| 137 | ], |
| 138 | 'img' => [ |
| 139 | 'alt' => [], |
| 140 | 'class' => [], |
| 141 | 'height' => [], |
| 142 | 'src' => [], |
| 143 | 'width' => [], |
| 144 | ], |
| 145 | 'li' => [ |
| 146 | 'class' => [], |
| 147 | ], |
| 148 | 'ol' => [ |
| 149 | 'class' => [], |
| 150 | ], |
| 151 | 'p' => [ |
| 152 | 'class' => [], |
| 153 | ], |
| 154 | 'q' => [ |
| 155 | 'cite' => [], |
| 156 | 'title' => [], |
| 157 | ], |
| 158 | 'span' => [ |
| 159 | 'class' => [], |
| 160 | 'title' => [], |
| 161 | 'style' => [], |
| 162 | ], |
| 163 | 'iframe' => [ |
| 164 | 'width' => [], |
| 165 | 'height' => [], |
| 166 | 'scrolling' => [], |
| 167 | 'frameborder' => [], |
| 168 | 'allow' => [], |
| 169 | 'src' => [], |
| 170 | ], |
| 171 | 'strike' => [], |
| 172 | 'br' => [], |
| 173 | 'strong' => [], |
| 174 | 'data-wow-duration' => [], |
| 175 | 'data-wow-delay' => [], |
| 176 | 'data-wallpaper-options' => [], |
| 177 | 'data-stellar-background-ratio' => [], |
| 178 | 'ul' => [ |
| 179 | 'class' => [], |
| 180 | ], |
| 181 | ]; |
| 182 | |
| 183 | if(function_exists('wp_kses')) { // WP is here |
| 184 | return wp_kses($raw, $allowed_tags); |
| 185 | } else { |
| 186 | return $raw; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | public static function kspan($text) { |
| 191 | return str_replace(['{', '}'], ['<span>', '</span>'], self::kses($text)); |
| 192 | } |
| 193 | |
| 194 | public static function category_list_by_taxonomy($taxonomy = '') { |
| 195 | $query_args = [ |
| 196 | 'orderby' => 'ID', |
| 197 | 'order' => 'DESC', |
| 198 | 'hide_empty' => 1, |
| 199 | 'taxonomy' => $taxonomy, |
| 200 | ]; |
| 201 | |
| 202 | $categories = get_categories($query_args); |
| 203 | |
| 204 | return $categories; |
| 205 | } |
| 206 | |
| 207 | public static function trim_words($text, $num_words) { |
| 208 | return wp_trim_words($text, $num_words, ''); |
| 209 | } |
| 210 | |
| 211 | public static function array_push_assoc($array, $key, $value) { |
| 212 | $array[$key] = $value; |
| 213 | |
| 214 | return $array; |
| 215 | } |
| 216 | |
| 217 | public static function render($content) { |
| 218 | if(stripos($content, "shopengine-has-lisence") !== false) { |
| 219 | return null; |
| 220 | } |
| 221 | |
| 222 | return $content; |
| 223 | } |
| 224 | |
| 225 | public static function render_elementor_content($content_id) { |
| 226 | $elementor_instance = \Elementor\Plugin::instance(); |
| 227 | |
| 228 | return $elementor_instance->frontend->get_builder_content_for_display($content_id); |
| 229 | } |
| 230 | |
| 231 | public static function esc_options($str, $options = [], $default = '') { |
| 232 | if(!in_array($str, $options)) { |
| 233 | return $default; |
| 234 | } |
| 235 | |
| 236 | return $str; |
| 237 | } |
| 238 | |
| 239 | public static function img_meta($id) { |
| 240 | $attachment = get_post($id); |
| 241 | if($attachment == null || $attachment->post_type != 'attachment') { |
| 242 | return null; |
| 243 | } |
| 244 | |
| 245 | return [ |
| 246 | 'alt' => get_post_meta($attachment->ID, '_wp_attachment_image_alt', true), |
| 247 | 'caption' => $attachment->post_excerpt, |
| 248 | 'description' => $attachment->post_content, |
| 249 | 'href' => get_permalink($attachment->ID), |
| 250 | 'src' => $attachment->guid, |
| 251 | 'title' => $attachment->post_title, |
| 252 | ]; |
| 253 | } |
| 254 | |
| 255 | public static function _product_tag_sale_badge($settings = null) { |
| 256 | global $product; |
| 257 | $terms = get_the_terms(get_the_ID(), 'product_tag'); |
| 258 | |
| 259 | $badge_position = (isset($settings['badge_position']) && !empty($settings['badge_position'])) ? esc_attr($settings['badge_position']) : 'top-right'; |
| 260 | $badge_align = (isset($settings['badge_align']) && !empty($settings['badge_align'])) ? esc_attr($settings['badge_align']) : 'horizontal'; |
| 261 | |
| 262 | if($product->is_on_sale() || !empty($terms)) : ?> |
| 263 | <div class="product-tag-sale-badge position-<?php echo esc_attr($badge_position); ?> align-<?php echo esc_attr($badge_align); ?>"> |
| 264 | <ul> |
| 265 | <?php if(!empty($terms)) : $term = $terms[0]; |
| 266 | $bg = get_term_meta($term->term_id, 'shopengine_tag_bg_color', true); |
| 267 | ?> |
| 268 | <?php if(!empty(self::_discount_percentage())) : ?> |
| 269 | <li class="badge no-link off"><?php echo '-' . esc_html(self::_discount_percentage()) . '%'; ?></li> |
| 270 | <?php endif; ?> |
| 271 | <li class="badge tag"> |
| 272 | <a <?php if(!empty($bg)) : ?>style="background-color:<?php echo esc_attr($bg); ?>" <?php endif; ?> |
| 273 | href="<?php echo get_term_link($term->term_id); ?>"><?php echo esc_html($term->name); ?></a> |
| 274 | </li> |
| 275 | <?php endif; |
| 276 | |
| 277 | if($product->is_on_sale()) { |
| 278 | echo "<li class='badge no-link sale'>" . esc_html__('Sale!', 'shopengine') . "</li>"; |
| 279 | } |
| 280 | ?> |
| 281 | </ul> |
| 282 | </div> |
| 283 | <?php |
| 284 | endif; |
| 285 | } |
| 286 | |
| 287 | |
| 288 | public static function _product_image($settings = null) { |
| 289 | global $product; |
| 290 | ?> |
| 291 | <div class='product-thumb'> |
| 292 | <a href="<?php echo get_the_permalink(); ?>"> |
| 293 | <?php echo woocommerce_get_product_thumbnail($product->get_id()); ?> |
| 294 | </a> |
| 295 | |
| 296 | <!-- end sale date --> |
| 297 | <?php |
| 298 | if(!empty($settings['counter_position']) && $settings['counter_position'] == 'image') { |
| 299 | self::_product_sale_end_date($settings); |
| 300 | } |
| 301 | ?> |
| 302 | <!-- tag and sale badge --> |
| 303 | <?php self::_product_tag_sale_badge($settings); ?> |
| 304 | |
| 305 | <!-- show group buttons --> |
| 306 | <?php |
| 307 | if(isset($settings['shopengine_group_btns']) && $settings['shopengine_group_btns'] === 'yes') { |
| 308 | ?> |
| 309 | <div class="loop-product--btns"> |
| 310 | <div class="loop-product--btns-inner"> |
| 311 | <?php woocommerce_template_loop_add_to_cart(); ?> |
| 312 | </div> |
| 313 | </div> |
| 314 | <?php |
| 315 | } |
| 316 | ?> |
| 317 | |
| 318 | </div> |
| 319 | <?php |
| 320 | } |
| 321 | |
| 322 | public static function _product_sale_end_date($settings) { |
| 323 | $date = get_post_meta(get_the_id(), '_sale_price_dates_to', true); |
| 324 | if(!empty($date)) : |
| 325 | $formatted_date = date("Y-m-d", $date); |
| 326 | $config = [ |
| 327 | 'days' => esc_html__('Days', 'shopengine'), |
| 328 | 'hours' => esc_html__('Hours', 'shopengine'), |
| 329 | 'minutes' => esc_html__('Minutes', 'shopengine'), |
| 330 | 'seconds' => esc_html__('Seconds', 'shopengine'), |
| 331 | ]; |
| 332 | |
| 333 | ?> |
| 334 | <div data-prefix="<?php echo !empty($settings['counter_prefix']) ? $settings['counter_prefix'] : ''; ?>" |
| 335 | class="product-end-sale-timer <?php echo !empty($settings['counter_position']) ? 'counter-position-' . esc_attr($settings['counter_position']) : ''; ?>" |
| 336 | data-config='<?php echo json_encode($config); ?>' |
| 337 | data-date="<?php echo esc_attr($formatted_date); ?>"></div> |
| 338 | <?php |
| 339 | endif; |
| 340 | } |
| 341 | |
| 342 | public static function _product_category($settings = null) { |
| 343 | global $product; |
| 344 | |
| 345 | $terms = get_the_terms(get_the_ID(), 'product_cat'); |
| 346 | $terms_count = count($terms); |
| 347 | |
| 348 | if($terms_count > 0) { |
| 349 | echo "<div class='product-category'><ul>"; |
| 350 | foreach($terms as $key => $term) { |
| 351 | $sperator = $key !== ($terms_count - 1) ? ',' : ''; |
| 352 | echo "<li><a href='" . get_term_link($term->term_id) . "'>" . esc_html($term->name) . $sperator . "</a></li>"; |
| 353 | } |
| 354 | echo "</ul></div>"; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | public static function _discount_percentage($settings = null) { |
| 359 | global $product; |
| 360 | |
| 361 | $product_data = $product->get_data(); |
| 362 | $show_tag = (isset($settings['show_tag']) && !empty($settings['show_tag'])) ? esc_attr($settings['show_tag']) : 'yes'; |
| 363 | $output = ''; |
| 364 | if($show_tag == 'yes' && !empty($product_data['regular_price']) && $product_data['sale_price']) { |
| 365 | $percentage = round((($product_data['regular_price'] - $product_data['sale_price']) / $product_data['regular_price']) * 100); |
| 366 | |
| 367 | return $percentage; |
| 368 | } |
| 369 | |
| 370 | return ''; |
| 371 | } |
| 372 | |
| 373 | |
| 374 | public static function _product_title($settings = null) { |
| 375 | ?> |
| 376 | <h3 class='product-title'> |
| 377 | <a href="<?php echo get_the_permalink(); ?>"><?php echo get_the_title(); ?></a> |
| 378 | </h3> |
| 379 | <?php |
| 380 | } |
| 381 | |
| 382 | public static function _product_rating($settings = null) { |
| 383 | |
| 384 | global $product; |
| 385 | ?> |
| 386 | <div class="product-rating"> |
| 387 | <?php |
| 388 | if($product->get_rating_count() > 0) { |
| 389 | woocommerce_template_loop_rating(); |
| 390 | } else { |
| 391 | echo sprintf('<div class="star-rating">%1$s</div>', wc_get_star_rating_html(0, 0)); |
| 392 | } |
| 393 | |
| 394 | // review count |
| 395 | echo sprintf('<span class="rating-count">(%1$s)</span>', $product->get_review_count()); |
| 396 | ?> |
| 397 | </div> |
| 398 | <?php |
| 399 | } |
| 400 | |
| 401 | |
| 402 | public static function _product_price($settings = null) { |
| 403 | ?> |
| 404 | <div class="product-price"> |
| 405 | <?php woocommerce_template_single_price(); ?> |
| 406 | </div> |
| 407 | <?php |
| 408 | } |
| 409 | |
| 410 | public static function _product_description($settings = null) { |
| 411 | global $product; |
| 412 | $product_data = $product->get_data($product->get_id()); |
| 413 | ?> |
| 414 | <div class="prodcut-description"> |
| 415 | <?php echo apply_filters('shopengine_product_short_description', $product_data['description']); ?> |
| 416 | </div> |
| 417 | |
| 418 | <?php |
| 419 | } |
| 420 | |
| 421 | public static function _product_buttons($settings = null) { |
| 422 | if($settings['shopengine_group_btns'] !== 'yes'): ?> |
| 423 | <div class="add-to-cart-bt"> |
| 424 | <?php woocommerce_template_loop_add_to_cart(); ?> |
| 425 | </div> |
| 426 | <?php endif; |
| 427 | } |
| 428 | |
| 429 | |
| 430 | /** |
| 431 | * todo - check the keys for refund and cancelled [wc-cancelled, wc-refunded ] |
| 432 | * |
| 433 | * @param $product_id |
| 434 | * @param int $variation_id |
| 435 | * @return int |
| 436 | */ |
| 437 | public static function get_total_sale_count($product_id, $variation_id = 0) { |
| 438 | |
| 439 | global $wpdb; |
| 440 | |
| 441 | $qry = 'SELECT sum(lookup.product_qty) as total FROM `'.$wpdb->prefix.'wc_order_product_lookup` as lookup'; |
| 442 | $qry .=' LEFT JOIN '.$wpdb->prefix.'wc_order_stats AS stat on lookup.order_id = stat.order_id '; |
| 443 | $qry .=' WHERE `product_id` = '.intval($product_id).' and variation_id = '.intval($variation_id); |
| 444 | $qry .= ' and stat.status NOT IN (\'wc-cancelled\', \'wc-refunded\') ;'; |
| 445 | $result = $wpdb->get_row($qry); |
| 446 | $total = is_object($result) ? $result->total : 0; |
| 447 | |
| 448 | return intval( $total ) ; |
| 449 | } |
| 450 | |
| 451 | |
| 452 | public static function is_guest_checkout_allowed() { |
| 453 | |
| 454 | return 'yes' === get_option('woocommerce_enable_guest_checkout'); |
| 455 | } |
| 456 | |
| 457 | public static function is_login_allowed_during_checkout() { |
| 458 | |
| 459 | return 'yes' === get_option('woocommerce_enable_checkout_login_reminder'); |
| 460 | } |
| 461 | } |