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