<?php if ( ! defined( 'ABSPATH' ) ) exit; class GT_BOGO_Countdown_Timer { private $id = 'countdown_timer'; public function __construct() { if ( is_admin() && ! defined('DOING_AJAX') ) return; if ( ! $this->enabled() ) return; add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] ); } private function enabled() { return (bool) get_option( 'gtbogo_sp_' . $this->id . '_enabled', 0 ) && get_option( 'gtbogo_sp_' . $this->id . '_intensity', 'subtle' ) !== 'off'; } private function intensity() { return get_option( 'gtbogo_sp_' . $this->id . '_intensity', 'subtle' ); } private function styles() { $map = [ 'subtle' => [ 'font_size' => '0.8em', 'padding' => '10px 16px', 'opacity' => '0.75' ], 'moderate' => [ 'font_size' => '0.88em', 'padding' => '12px 20px', 'opacity' => '0.88' ], 'bold' => [ 'font_size' => '0.95em', 'padding' => '14px 24px', 'opacity' => '1.0' ], ]; return $map[ $this->intensity() ] ?? $map['subtle']; } private function get_end_time() { if ( function_exists( 'gt_bogo_has_pro' ) && gt_bogo_has_pro() ) { global $wpdb; $rule = $wpdb->get_row( "SELECT end_date,name FROM {$wpdb->prefix}gtbogo_rules WHERE status='active' AND end_date IS NOT NULL AND end_date!='' ORDER BY priority ASC LIMIT 1", ARRAY_A ); if ( $rule && strtotime( $rule['end_date'] ) > time() ) { return [ 'ts' => strtotime( $rule['end_date'] ), 'label' => $rule['name'] ?? '' ]; } } $midnight = strtotime( 'tomorrow midnight' ); return [ 'ts' => $midnight, 'label' => '' ]; } public function enqueue() { $timer = $this->get_end_time(); $s = $this->styles(); wp_add_inline_style( 'woocommerce-general', ' .gtbogo-countdown{width:100%;box-sizing:border-box;font-family:inherit;color:inherit;border-top:1px solid currentColor;border-bottom:1px solid currentColor;text-align:center;margin-bottom:16px} .gtbogo-cd-inner{display:inline-flex;align-items:center;gap:20px;flex-wrap:wrap;justify-content:center} .gtbogo-cd-label{font-weight:600;text-transform:uppercase;letter-spacing:0.08em;font-size:0.78em;opacity:0.6} .gtbogo-cd-units{display:flex;gap:6px;align-items:center} .gtbogo-cd-unit{display:flex;flex-direction:column;align-items:center;min-width:2.8em} .gtbogo-cd-num{font-size:1.5em;font-weight:800;line-height:1;font-variant-numeric:tabular-nums} .gtbogo-cd-sub{font-size:0.52em;opacity:0.45;text-transform:uppercase;letter-spacing:0.1em;margin-top:1px} .gtbogo-cd-sep{font-size:1.2em;opacity:0.35;font-weight:300;padding-bottom:6px} ' ); $msg = get_option( 'gtbogo_sp_countdown_timer_message', '' ); wp_add_inline_script( 'wc-add-to-cart', ' (function(){ var end = ' . (int)$timer['ts'] . ' * 1000; function tick(){ var d = end - Date.now(); if(d <= 0){ document.querySelectorAll(".gtbogo-countdown").forEach(function(el){ el.style.display="none"; }); return; } var h = Math.floor(d/3600000), m = Math.floor((d%3600000)/60000), s = Math.floor((d%60000)/1000); document.querySelectorAll("#gtbogo-cd-h").forEach(function(el){ el.textContent=("0"+h).slice(-2); }); document.querySelectorAll("#gtbogo-cd-m").forEach(function(el){ el.textContent=("0"+m).slice(-2); }); document.querySelectorAll("#gtbogo-cd-s").forEach(function(el){ el.textContent=("0"+s).slice(-2); }); } tick(); setInterval(tick,1000); })(); ', 'after' ); } public function render() { $timer = $this->get_end_time(); $s = $this->styles(); $msg = $timer['label'] ?: get_option( 'gtbogo_sp_countdown_timer_message', GT_BOGO_Copy::get( 'countdown_label', $this->intensity() ) ); ?> <div class="gtbogo-countdown" style="padding:<?php echo esc_attr($s['padding']); ?>;font-size:<?php echo esc_attr($s['font_size']); ?>;opacity:<?php echo esc_attr($s['opacity']); ?>"> <div class="gtbogo-cd-inner"> <?php if ( $msg ) : ?><span class="gtbogo-cd-label"><?php echo esc_html( $msg ); ?></span><?php endif; ?> <div class="gtbogo-cd-units"> <div class="gtbogo-cd-unit"><span class="gtbogo-cd-num" id="gtbogo-cd-h">00</span><span class="gtbogo-cd-sub"><?php esc_html_e('hrs','gt-bogo-engine'); ?></span></div> <span class="gtbogo-cd-sep">:</span> <div class="gtbogo-cd-unit"><span class="gtbogo-cd-num" id="gtbogo-cd-m">00</span><span class="gtbogo-cd-sub"><?php esc_html_e('min','gt-bogo-engine'); ?></span></div> <span class="gtbogo-cd-sep">:</span> <div class="gtbogo-cd-unit"><span class="gtbogo-cd-num" id="gtbogo-cd-s">00</span><span class="gtbogo-cd-sub"><?php esc_html_e('sec','gt-bogo-engine'); ?></span></div> </div> </div> </div> <?php } }