PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.6
ShopBuilder – WooCommerce Builder For Elementor v3.2.6
3.4.2 3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 All 63 releases
shopbuilder / app / Modules / Badges / BadgesFrontEnd.php

BadgesFrontEnd.php in ShopBuilder – WooCommerce Builder For Elementor 3.2.6, at app/Modules/Badges/BadgesFrontEnd.php

840 lines 23.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Main BadgesFrontEnd class.
4 *
5 * @package RadiusTheme\SB
6 */
7
8 namespace RadiusTheme\SB\Modules\Badges;
9
10 use RadiusTheme\SB\Elementor\Helper\RenderHelpers;
11 use RadiusTheme\SB\Helpers\Fns;
12 use RadiusTheme\SB\Traits\SingletonTrait;
13 use WC_Product;
14
15 defined( 'ABSPATH' ) || exit();
16
17 /**
18 * Main BadgesFrontEnd class.
19 */
20 class BadgesFrontEnd {
21 /**
22 * Singleton Trait.
23 */
24 use SingletonTrait;
25
26 /**
27 * @var array|mixed
28 */
29 private array $options;
30 /**
31 * @var array
32 */
33 private $cache = [];
34
35 /**
36 * @var array
37 */
38 private array $all_badges = [];
39
40 /**
41 * @var array
42 */
43 private array $group_badges_loop = [];
44 /**
45 * @var array
46 */
47 private array $group_badges_product = [];
48 /**
49 * @var array
50 */
51 private array $above_image_loop = [];
52 /**
53 * @var array
54 */
55 private array $above_image_product = [];
56
57 /**
58 * Class Constructor.
59 */
60 private function __construct() {
61 $this->options = Fns::get_options( 'modules', 'product_badges' );
62 add_filter( 'woocommerce_sale_flash', [ $this, 'woocommerce_sale_flash' ], 99 );
63 add_action( 'the_post', [ $this, 'wc_setup_product_badge_data' ], 20 );
64 add_action( 'rtsb_before_product_template_render', [ $this, 'wc_setup_product_badge_data' ], 12 );
65
66 // show_group_custom_position.
67 $loop_above_image = 'above_image' === ( $this->options['loop_group_position'] ?? '' );
68 if ( ( wp_doing_ajax() || ! is_admin() ) && $loop_above_image ) {
69 // Loop action will go here.
70 add_filter( 'woocommerce_product_get_image', [ $this, 'show_badge_on_loop_product_thumbnail' ], 99, 1 );
71 }
72
73 $product_page_above_image = 'above_image' === ( $this->options['product_page_group_position'] ?? '' );
74
75 if ( $product_page_above_image ) {
76 add_filter( 'woocommerce_single_product_image_thumbnail_html', [ $this, 'show_badge_on_product_page_thumbnail' ], 99, 1 );
77 add_action( 'rtwpvg_product_badge', [ $this, 'product_gallery_images_badge' ], 15, 5 );
78 add_action( 'rtsb_product_badge', [ $this, 'product_gallery_images_badge' ], 15, 5 );
79 // Product details action will go here.
80 }
81
82 $this->badges_position();
83
84 // The Hook only work if global product found.
85 add_action( 'rtsb/modules/product_badges/frontend/display', [ $this, 'all_badges_for_product' ] );
86
87 // ShortCode.
88 add_shortcode( 'rtsb_badges', [ $this, 'badges_shortcode' ] );
89 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_public_scripts' ], 99 );
90 }
91
92
93 /**
94 * @return void
95 */
96 public function enqueue_public_scripts() {
97 $cache_key = 'badge_style_cache';
98
99 if ( isset( $this->cache[ $cache_key ] ) && ! empty( $this->cache[ $cache_key ] ) ) {
100 wp_add_inline_style( Fns::optimized_handle( 'rtsb-frontend' ), $this->cache[ $cache_key ] );
101
102 return;
103 }
104
105 $product_badges = BadgesFns::get_product_badges_list();
106
107 if ( empty( $product_badges ) ) {
108 return;
109 }
110
111 $dynamic_css = '';
112
113 foreach ( $product_badges as $badge ) {
114 $parent_selectors = [];
115
116 if ( 'product' === ( $badge['apply_for'] ?? '' ) ) {
117 if ( ! empty( $badge['applicable_products'] ) ) {
118 foreach ( $badge['applicable_products'] as $product ) {
119 $parent_selectors[] = '.product.post-' . absint( $product['value'] );
120 }
121 }
122
123 if ( empty( $badge['applicable_products'] ) ) {
124 $parent_selectors[] = '.product';
125 }
126 }
127
128 if ( 'product_cat' === ( $badge['apply_for'] ?? '' ) ) {
129 if ( ! empty( $badge['applicable_categories'] ) ) {
130 foreach ( $badge['applicable_categories'] as $cat ) {
131 $cat_id = absint( $cat['value'] );
132 $term = get_term( $cat_id, 'product_cat' );
133
134 if ( is_wp_error( $term ) || empty( $term ) ) {
135 continue;
136 }
137
138 $parent_selectors[] = '.product.product_cat-' . $term->slug;
139 }
140 }
141
142 if ( empty( $badge['applicable_categories'] ) ) {
143 $parent_selectors[] = '.product';
144 }
145 }
146
147 if ( ! empty( $parent_selectors ) && 'text' == $badge['badges_type'] ) {
148 $dynamic_css .= $this->print_text_style( $parent_selectors, $badge );
149 }
150
151 if ( ! empty( $parent_selectors ) && 'image' == $badge['badges_type'] ) {
152 $dynamic_css .= $this->print_images_style( $parent_selectors, $badge );
153 }
154 }//end foreach
155
156 $dynamic_css .= $this->global_badge_css();
157
158 $this->cache[ $cache_key ] = $dynamic_css;
159
160 if ( ! empty( $dynamic_css ) ) {
161 wp_add_inline_style( Fns::optimized_handle( 'rtsb-frontend' ), $dynamic_css );
162 }
163 }
164
165 /**
166 * Setup Product Badges
167 *
168 * @return array|void
169 */
170 public function wc_setup_product_badge_data() {
171 global $product;
172
173 if ( ! $product instanceof WC_Product ) {
174 return;
175 }
176
177 $this->all_badges = [];
178 $this->above_image_loop = [];
179 $this->above_image_product = [];
180 $this->group_badges_loop = [];
181 $this->group_badges_product = [];
182 $cache_key = 'wc_setup_product_badge_data_' . $product->get_id();
183 if ( isset( $this->cache[ $cache_key ] ) && is_array( $this->cache[ $cache_key ] ) ) {
184 $this->all_badges = $this->cache[ $cache_key ]['all_badges'] ?? [];
185 $this->above_image_loop = $this->cache[ $cache_key ]['above_image_loop'] ?? [];
186 $this->above_image_product = $this->cache[ $cache_key ]['above_image_product'] ?? [];
187 $this->group_badges_loop = $this->cache[ $cache_key ]['group_badges_loop'] ?? [];
188 $this->group_badges_product = $this->cache[ $cache_key ]['group_badges_product'] ?? [];
189
190 return $this->cache[ $cache_key ];
191 }
192
193 $badges = BadgesFns::get_product_badges_for_current_product();
194
195 if ( empty( $badges ) ) {
196 return;
197 }
198
199 $loop_group_above_image = 'above_image' == ( $this->options['loop_group_position'] ?? '' );
200 $above_image_product = 'above_image' == ( $this->options['product_page_group_position'] ?? '' );
201
202 foreach ( $badges as $badge_applied ) {
203 $this->all_badges[] = $badge_applied;
204 // loop_group_position.
205 if ( $loop_group_above_image ) {
206 $this->above_image_loop[] = $badge_applied;
207 }
208
209 // loop_group_position.
210 if ( ! $loop_group_above_image ) {
211 $this->group_badges_loop[] = $badge_applied;
212 }
213
214 // Product Page Group Position.
215 if ( $above_image_product ) {
216 $this->above_image_product[] = $badge_applied;
217 }
218
219 // Product Page Group Position.
220 if ( ! $above_image_product ) {
221 $this->group_badges_product[] = $badge_applied;
222 }
223 }//end foreach
224 $this->cache[ $cache_key ] = [
225 'all_badges' => $this->all_badges,
226 'above_image_loop' => $this->above_image_loop,
227 'above_image_product' => $this->above_image_product,
228 'group_badges_loop' => $this->group_badges_loop,
229 'group_badges_product' => $this->group_badges_product,
230 ];
231
232 return $this->cache[ $cache_key ];
233 }
234
235 /**
236 * Get Badge Html
237 *
238 * @param array $badge_applied badge array.
239 *
240 * @return void
241 */
242 private function badge_html_for_text( $badge_applied ) {
243 $text = '';
244 $is_dynamic = 'dynamic' == ( $badge_applied['badge_condition'] ?? '' );
245 $is_on_sale = 'on_sale' == ( $badge_applied['badge_for'] ?? '' );
246 if ( $is_dynamic && $is_on_sale && 'on' === ( $badge_applied['show_badges_percent_text'] ?? 'off' ) ) {
247 $percent = Fns::calculate_sale_percentage();
248 if ( $percent ) {
249 $text = '-' . $percent . '%';
250 }
251 } else {
252 $text = ( $badge_applied['badges_text'] ?? '' );
253 }
254
255 $badge_preset = ( $badge_applied['badge_preset'] ?? 'preset1' );
256 $badge_class = RenderHelpers::badge_class( $badge_preset );
257 if ( empty( $text ) ) {
258 return;
259 }
260 ?>
261 <span class="rtsb-tag-<?php echo esc_attr( $badge_class ); ?>"><?php echo esc_html( $text ); ?></span>
262 <?php
263 }
264
265 /**
266 * Get Badge Html
267 *
268 * @param array $badge_applied badge array.
269 *
270 * @return void
271 */
272 private function badge_html_for_image( $badge_applied ) {
273 if ( empty( $badge_applied['upload_image']['id'] ) ) {
274 return;
275 }
276 $img_alt = trim( wp_strip_all_tags( get_post_meta( $badge_applied['upload_image']['id'], '_wp_attachment_image_alt', true ) ) );
277 $alt_tag = ! empty( $img_alt ) ? $img_alt : __( 'Badge Image', 'shopbuilder' );
278 ?>
279 <img class="badge-image" src="<?php echo esc_url( $badge_applied['upload_image']['source'] ); ?>" alt="<?php echo esc_attr( $alt_tag ); ?>"/>
280 <?php
281 }
282
283 /**
284 * Get Badge Html
285 *
286 * @param array $badge_applied badge array.
287 * @param string $badges_type badges type.
288 * @param array $badge_classes badges classes.
289 *
290 * @return void
291 */
292 private function generate_badges( $badge_applied, $badges_type = '', $badge_classes = [] ) {
293 ?>
294 <div class="rtsb-badge type-<?php echo esc_attr( $badges_type ) . ' ' . esc_attr( implode( ' ', $badge_classes ) ); ?>">
295 <?php
296 switch ( $badges_type ) {
297 case 'text':
298 $this->badge_html_for_text( $badge_applied );
299 break;
300 case 'image':
301 $this->badge_html_for_image( $badge_applied );
302 break;
303 }
304 ?>
305 </div>
306 <?php
307 }
308
309
310 /**
311 * @return void
312 */
313 public function product_gallery_images_badge() {
314 if ( ! apply_filters( 'rtsb/module/badges/show', true ) ) {
315 return;
316 }
317
318 global $product;
319
320 if ( ! ( $product && $product instanceof WC_Product ) ) {
321 return;
322 }
323
324 $group_classes = [];
325 if ( ! empty( $this->options['group_badge_display_as'] ) ) {
326 $group_classes[] = 'rtsb-group-display-as-' . $this->options['group_badge_display_as'];
327 }
328
329 $group_position = ( $this->options['product_page_group_badge_position'] ?? 'top-left' );
330 // group_badge_position.
331 $group_classes[] = 'rtsb-group-position-' . $group_position;
332 Fns::print_html( $this->group_badges_html( $this->above_image_product, $group_classes ) );
333 }
334
335 /**
336 * @return void
337 */
338 public function add_group_badges_for_product() {
339 if ( ! apply_filters( 'rtsb/module/badges/show', true ) ) {
340 return;
341 }
342
343 $group_classes = [ 'rtsb-group-custom-position' ];
344 Fns::print_html( $this->group_badges_html( $this->group_badges_product, $group_classes ) );
345 }
346
347 /**
348 * @return void
349 */
350 public function add_group_badges_for_loop() {
351 if ( ! apply_filters( 'rtsb/module/badges/show', true ) ) {
352 return;
353 }
354
355 $group_classes = [ 'rtsb-group-custom-position' ];
356 if ( ! empty( $this->options['group_badge_display_as'] ) ) {
357 $group_classes[] = 'rtsb-group-display-as-' . $this->options['group_badge_display_as'];
358 }
359
360 Fns::print_html( $this->group_badges_html( $this->group_badges_loop, $group_classes ) );
361 }
362
363 /**
364 * @return void
365 */
366 public function all_badges_for_product() {
367 if ( ! apply_filters( 'rtsb/module/badges/show', true ) ) {
368 return;
369 }
370
371 $group_classes = [ 'rtsb-group-custom-position' ];
372
373 $group_position = ( $this->options['group_badge_position'] ?? 'top-left' );
374 // group_badge_position.
375 if ( ! empty( $this->options['group_badge_display_as'] ) ) {
376 $group_classes[] = 'rtsb-group-display-as-' . $this->options['group_badge_display_as'];
377 }
378
379 $group_classes[] = 'rtsb-group-position-' . $group_position;
380 $group_classes = apply_filters( 'rtsb/all/badges/classes/for/product', $group_classes );
381 Fns::print_html( $this->group_badges_html( $this->all_badges, $group_classes ) );
382 }
383
384 /**
385 * Badges Position.
386 *
387 * @return void
388 */
389 public function badges_position() {
390 // Single Page Badges Position.
391 $product_hook_priority = ( $this->options['product_page_group_hook_priority'] ?? null );
392
393 $positions = apply_filters(
394 'rtsb/module/product_page_badges/positions',
395 [
396 'before_add_to_cart' => [
397 'hook' => 'woocommerce_single_product_summary',
398 'priority' => ( $product_hook_priority ?? 25 ),
399 ],
400 'after_add_to_cart' => [
401 'hook' => 'woocommerce_single_product_summary',
402 'priority' => ( $product_hook_priority ?? 31 ),
403 ],
404 'after_thumbnail' => [
405 'hook' => 'woocommerce_before_single_product_summary',
406 'priority' => ( $product_hook_priority ?? 25 ),
407 ],
408 'after_summary' => [
409 'hook' => 'woocommerce_after_single_product_summary',
410 'priority' => ( $product_hook_priority ?? 11 ),
411 ],
412 'after_short_desc' => [
413 'hook' => 'woocommerce_single_product_summary',
414 'priority' => ( $product_hook_priority ?? 21 ),
415 ],
416 'custom' => [
417 'hook' => ( $this->options['product_page_custom_hook_name'] ?? '' ),
418 'priority' => $product_hook_priority ?? 10,
419 ],
420 ]
421 );
422
423 $product_page_position = ( $this->options['product_page_group_position'] ?? 'after_add_to_cart' );
424
425 if ( 'shortcode' !== $product_page_position && isset( $positions[ $product_page_position ]['hook'] ) ) {
426 add_action( $positions[ $product_page_position ]['hook'], [ $this, 'add_group_badges_for_product' ], isset( $positions[ $product_page_position ]['priority'] ) ? absint( $positions[ $product_page_position ]['priority'] ) : '' );
427 }
428
429 $loop_hook_priority = ( $this->options['group_position_hook_priority'] ?? null );
430
431 // Shop Page/ Any Product Query loop.
432 $positions = apply_filters(
433 'rtsb/module/shop_page_badges/positions',
434 [
435 'before_product_title' => [
436 'hook' => 'woocommerce_shop_loop_item_title',
437 'priority' => ( $loop_hook_priority ?? 5 ),
438 ],
439 'after_product_title' => [
440 'hook' => 'woocommerce_shop_loop_item_title',
441 'priority' => ( $loop_hook_priority ?? 11 ),
442 ],
443 'before_add_to_cart' => [
444 'hook' => 'woocommerce_after_shop_loop_item',
445 'priority' => ( $loop_hook_priority ?? 7 ),
446 ],
447 'after_add_to_cart' => [
448 'hook' => 'woocommerce_after_shop_loop_item',
449 'priority' => ( $loop_hook_priority ?? 15 ),
450 ],
451 'custom' => [
452 'hook' => ( $this->options['loop_custom_hook_name'] ?? '' ),
453 'priority' => ( $loop_hook_priority ?? 10 ),
454 ],
455 ]
456 );
457
458 $loop_group_position = ( $this->options['loop_group_position'] ?? 'after_add_to_cart' );
459
460 if ( 'shortcode' !== $loop_group_position && ! empty( $positions[ $loop_group_position ]['hook'] ) ) {
461 add_action(
462 $positions[ $loop_group_position ]['hook'],
463 [
464 $this,
465 'add_group_badges_for_loop',
466 ],
467 ( $positions[ $loop_group_position ]['priority'] ?? '' )
468 );
469 }
470 }
471
472 /**
473 * Badges Shortcode callable function
474 *
475 * @return string [HTML]
476 */
477 public function badges_shortcode() {
478 global $product;
479
480 if ( ! $product instanceof WC_Product ) {
481 return '';
482 }
483
484 ob_start();
485 $this->all_badges_for_product();
486
487 return ob_get_clean();
488 }
489
490 /**
491 * Image badge styles.
492 *
493 * @param array $parent_selector Selectors.
494 * @param array $badge Badges array.
495 *
496 * @return string
497 */
498 private function print_images_style( $parent_selector, $badge ) {
499 $style = '';
500
501 if ( empty( $parent_selector ) ) {
502 return '';
503 }
504
505 $selectors = [
506 'width' => '.index-' . sanitize_title( $badge['title'] ) . ':not(.rtsb-badge-manual-style)',
507 'height' => '.index-' . sanitize_title( $badge['title'] ) . ':not(.rtsb-badge-manual-style)',
508 ];
509
510 foreach ( $selectors as $key => $selector ) {
511 $selectors[ $key ] = $this->generate_selector( $parent_selector, $selector );
512 }
513
514 if ( ! empty( $badge['badge_width'] ) ) {
515 $width = 'width:' . $badge['badge_width'] . ';';
516 $style .= $selectors['width'] . '{' . $width . 'max-width: initial;}';
517 }
518
519 if ( ! empty( $badge['badge_height'] ) ) {
520 $height = 'height:' . $badge['badge_height'] . ';';
521 $style .= $selectors['height'] . '{' . $height . '}';
522 }
523
524 return $style;
525 }
526
527 /**
528 * Text badge styles.
529 *
530 * @param array $parent_selector Selectors.
531 * @param array $badge Badges array.
532 *
533 * @return string
534 */
535 private function print_text_style( $parent_selector, $badge ) {
536 $style = '';
537 if ( empty( $parent_selector ) ) {
538 return '';
539 }
540
541 $selectors = [
542 'color' => '.index-' . sanitize_title( $badge['title'] ) . ':not(.rtsb-badge-manual-style) span',
543 'bg_color' => '.index-' . sanitize_title( $badge['title'] ) . ':not(.rtsb-badge-manual-style) span',
544 'border_color' => '.index-' . sanitize_title( $badge['title'] ) . ':not(.rtsb-badge-manual-style) span, .index-' . sanitize_title( $badge['title'] ) . ':not(.rtsb-badge-manual-style) .rtsb-tag-outline.angle-right::after',
545 'width' => '.index-' . sanitize_title( $badge['title'] ) . ':not(.rtsb-badge-manual-style) span',
546 'height' => '.index-' . sanitize_title( $badge['title'] ) . ':not(.rtsb-badge-manual-style) span',
547 'padding' => '.index-' . sanitize_title( $badge['title'] ) . ':not(.rtsb-badge-manual-style) span',
548 'border_radius' => '.index-' . sanitize_title( $badge['title'] ) . ':not(.rtsb-badge-manual-style) span',
549 'badge_font_size' => '.index-' . sanitize_title( $badge['title'] ) . ':not(.rtsb-badge-manual-style) span',
550 'badge_font_width' => '.index-' . sanitize_title( $badge['title'] ) . ':not(.rtsb-badge-manual-style) span',
551 ];
552
553 foreach ( $selectors as $key => $selector ) {
554 $selectors[ $key ] = $this->generate_selector( $parent_selector, $selector );
555 }
556
557 if ( ! empty( $badge['badge_text_color'] ) ) {
558 $color = 'color:' . $badge['badge_text_color'] . ';';
559 $style .= $selectors['color'] . '{' . $color . '}';
560 }
561
562 if ( ! empty( $badge['badge_bg_color'] ) && in_array( ( $badge['badge_preset'] ?? '' ), [ 'preset1', 'preset2' ], true ) ) {
563 $color = 'background-color:' . $badge['badge_bg_color'] . ';';
564 $color .= 'border-color:' . $badge['badge_bg_color'] . ';';
565 $style .= $selectors['bg_color'] . '{' . $color . '}';
566 }
567 if ( ! empty( $badge['badge_border_color'] ) && in_array( ( $badge['badge_preset'] ?? '' ), [ 'preset3', 'preset4' ], true ) ) {
568 $color = 'border-color:' . $badge['badge_border_color'] . ';';
569 $style .= $selectors['border_color'] . '{' . $color . '}';
570 }
571 // badge_border_color.
572 if ( ! empty( $badge['badge_width'] ) ) {
573 $width = 'width:' . $badge['badge_width'] . ';';
574 $style .= $selectors['width'] . '{' . $width . 'max-width: initial;}';
575 }
576
577 if ( ! empty( $badge['badge_height'] ) ) {
578 $height = 'height:' . $badge['badge_height'] . ';';
579 $style .= $selectors['height'] . '{' . $height . '}';
580 }
581
582 if ( ! empty( $badge['badge_padding'] ) ) {
583 $padding = 'padding:' . $badge['badge_padding'] . ';';
584 $style .= $selectors['padding'] . '{' . $padding . '}';
585 }
586
587 if ( ! empty( $badge['border_radius'] ) ) {
588 $border_radius = 'border-radius:' . $badge['border_radius'] . ';';
589 $style .= $selectors['border_radius'] . '{' . $border_radius . '}';
590 }
591
592 if ( ! empty( $badge['badge_font_size'] ) ) {
593 $font_size = 'font-size:' . $badge['badge_font_size'] . ';';
594 $style .= $selectors['badge_font_size'] . '{' . $font_size . '}';
595 }
596
597 if ( ! empty( $badge['badge_font_width'] ) ) {
598 $font_width = 'font-weight:' . $badge['badge_font_width'] . ';';
599 $style .= $selectors['badge_font_width'] . '{' . $font_width . '}';
600 }
601
602 return $style;
603 }
604
605 /**
606 * Selector generator.
607 *
608 * @param array $parent_selector Selectors.
609 * @param string $suffix Suffix.
610 *
611 * @return string
612 */
613 private function generate_selector( $parent_selector, $suffix ) {
614 $selectors = '';
615
616 foreach ( $parent_selector as $selector ) {
617 if ( ! empty( $selectors ) ) {
618 $selectors .= ',' . $selector . ' ' . $suffix;
619 } else {
620 $selectors = $selector . ' ' . $suffix;
621 }
622 }
623
624 return $selectors;
625 }
626
627 /**
628 * Badge global CSS.
629 *
630 * @return string
631 */
632 private function global_badge_css() {
633 $global_css = '';
634 $styles = [
635 'gap' => [
636 'selector' => '.rtsb-badge-container .rtsb-badge-group-style',
637 'style' => 'gap',
638 'value' => $this->options['group_badge_gap'] ?? '',
639 ],
640 ];
641
642 foreach ( $styles as $style ) {
643 if ( ! empty( $style['value'] ) ) {
644 $global_css .= $style['selector'] . '{';
645 $global_css .= $style['style'] . ': ' . $style['value'] . ';';
646 $global_css .= '}';
647 }
648 }
649
650 return $global_css;
651 }
652
653 /**
654 * Hide Woocommerce on sale badge.
655 *
656 * @param string $onsale Text.
657 *
658 * @return string
659 */
660 public function woocommerce_sale_flash( $onsale ) {
661 $hide_default_badge = $this->options['hide_woocommerce_badge'] ?? false;
662
663 if ( ! $hide_default_badge ) {
664 return $onsale;
665 }
666
667 $hide_on_sale = ( $this->options['hide_on_sale'] ?? 'all_products' );
668
669 switch ( $hide_on_sale ) {
670 case 'where_custom_badge_applied':
671 if ( BadgesFns::is_allowed_badge_showing() ) {
672 $onsale = '';
673 }
674 break;
675 case 'all_products':
676 $onsale = '';
677 break;
678 default:
679 }
680
681 return $onsale;
682 }
683
684 /**
685 * Is allowed badge.
686 *
687 * @return bool
688 */
689 private function is_allowed_badge_showing() {
690 $badge_applied = BadgesFns::get_product_badges_for_current_product();
691
692 return is_array( $badge_applied ) && count( $badge_applied );
693 }
694
695 /**
696 * Get Badge Html
697 *
698 * @param array $group_badges badge array.
699 * @param array $group_classes badges classes.
700 *
701 * @return string
702 */
703 private function group_badges_html( $group_badges, $group_classes = [] ) {
704 if ( empty( $group_badges ) ) {
705 return '';
706 }
707
708 ob_start();
709 ?>
710 <div class="rtsb-badge-group-style rtsb-promotion <?php echo esc_attr( implode( ' ', $group_classes ) ); ?>">
711 <?php
712 foreach ( $group_badges as $badge_applied ) {
713 $badges_type = ( $badge_applied['badges_type'] ?? 'text' );
714 $badge_classes = [];
715 if ( ! empty( $badge_applied['title'] ) ) {
716 $badge_classes[] = 'index-' . sanitize_title( $badge_applied['title'] );
717 }
718
719 $this->generate_badges( $badge_applied, $badges_type, $badge_classes );
720 }
721 ?>
722 </div>
723 <?php
724 return ob_get_clean();
725 }
726
727 /**
728 * Show badge on product thumbnail
729 *
730 * @param string $image_html The image HTML.
731 *
732 * @return string
733 */
734 public function show_badge_on_loop_product_thumbnail( $image_html ) {
735 if ( ! apply_filters( 'rtsb/module/badges/show', true ) ) {
736 return $image_html;
737 }
738
739 global $product;
740
741 if ( ! ( $product && $product instanceof WC_Product ) ) {
742 return $image_html;
743 }
744
745 if ( ! $this->is_allowed_badge_showing() ) {
746 return $image_html;
747 }
748
749 if ( empty( $this->above_image_loop ) ) {
750 return $image_html;
751 }
752
753 $group_classes = [];
754 $group_position = ( $this->options['group_badge_position'] ?? 'top-left' );
755 // group_badge_position.
756 if ( ! empty( $this->options['group_badge_display_as'] ) ) {
757 $group_classes[] = 'rtsb-group-display-as-' . $this->options['group_badge_display_as'];
758 }
759
760 $group_classes[] = 'rtsb-group-position-' . $group_position;
761
762 return $this->image_badges_html( $image_html, $this->group_badges_html( $this->above_image_loop, $group_classes ) );
763 }
764
765
766 /**
767 * Show badge on product thumbnail
768 *
769 * @param string $image_html The image HTML.
770 *
771 * @return string
772 */
773 public function show_badge_on_product_page_thumbnail( $image_html ) {
774 if ( ! apply_filters( 'rtsb/module/badges/show', true ) ) {
775 return $image_html;
776 }
777
778 global $product;
779
780 if ( ! ( $product && $product instanceof WC_Product && ! did_action( 'woocommerce_product_thumbnails' ) ) ) {
781 return $image_html;
782 }
783
784 if ( ! $this->is_allowed_badge_showing() ) {
785 return $image_html;
786 }
787
788 if ( empty( $this->above_image_product ) ) {
789 return $image_html;
790 }
791
792 $group_classes = [];
793 if ( ! empty( $this->options['group_badge_display_as'] ) ) {
794 $group_classes[] = 'rtsb-group-display-as-' . $this->options['group_badge_display_as'];
795 }
796
797 $group_position = ( $this->options['product_page_group_badge_position'] ?? 'top-left' );
798 // group_badge_position.
799 $group_classes[] = 'rtsb-group-position-' . $group_position;
800
801 return $this->image_badges_html( $image_html, $this->group_badges_html( $this->above_image_product, $group_classes ) );
802 }
803
804 /**
805 * Badge HTML.
806 *
807 * @param string $image_html HTML.
808 * @param string $badge_html HTML.
809 *
810 * @return string
811 */
812 private function image_badges_html( $image_html, $badge_html ) {
813 if ( empty( $badge_html ) ) {
814 return $image_html;
815 }
816
817 $print_badges_directly = false;
818 $div_close = '</div>';
819
820 if ( get_theme_support( 'wc-product-gallery-slider' ) ) {
821 $content = rtrim( $image_html );
822
823 if ( strrpos( $content, $div_close ) === ( strlen( $content ) - strlen( $div_close ) ) ) {
824 $print_badges_directly = true;
825 $image_html = $content;
826 }
827 }
828
829 if ( $print_badges_directly ) {
830 $image_html = substr( $image_html, 0, - strlen( $div_close ) );
831 $image_html .= $badge_html;
832 $image_html .= $div_close;
833 } else {
834 $image_html = "<div class='rtsb-badge-container'>" . $image_html . $badge_html . '</div>';
835 }
836
837 return $image_html;
838 }
839 }
840