<?php if ( ! defined( 'ABSPATH' ) ) exit; class GT_BOGO_Countdown_Timer { public function __construct() { $enabled = get_option( 'gtbogo_sp_countdown_timer_enabled', 0 ); $intensity = get_option( 'gtbogo_sp_countdown_timer_intensity', 'subtle' ); if ( ! $enabled || $intensity === 'off' ) return; add_action( 'woocommerce_before_shop_loop', [ $this, 'render_timer' ], 5 ); add_action( 'woocommerce_before_single_product', [ $this, 'render_timer' ], 5 ); add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_assets' ] ); } private function get_intensity_styles() { $intensity = get_option( 'gtbogo_sp_countdown_timer_intensity', 'subtle' ); $map = [ 'subtle' => [ 'font_size' => '0.8em', 'padding' => '10px 16px', 'opacity' => '0.75' ], 'moderate' => [ 'font_size' => '0.9em', 'padding' => '14px 20px', 'opacity' => '0.90' ], 'bold' => [ 'font_size' => '1em', 'padding' => '18px 24px', 'opacity' => '1.0' ], ]; return $map[ $intensity ] ?? $map['subtle']; } private function get_active_timer() { global $wpdb; $rules = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}gtbogo_rules WHERE status = 'active'", ARRAY_A ); if ( ! $rules ) return null; foreach ( $rules as $row ) { $rule = $row; if ( empty( $rule ) ) continue; $countdown = $rule['superpowers']['countdown_timer'] ?? []; if ( empty( $countdown['enabled'] ) ) continue; $end_date = $rule['scheduling']['end_date'] ?? ''; if ( $end_date && strtotime( $end_date ) > time() ) { return [ 'title' => esc_html( $rule['title'] ?? '' ), 'end_time' => (int) strtotime( $end_date ), 'message' => esc_html( $countdown['message'] ?? 'Available for a limited time' ), ]; } } return null; } public function enqueue_assets() { $timer = $this->get_active_timer(); if ( ! $timer ) return; $end_time = $timer['end_time']; wp_add_inline_script( 'jquery', " jQuery(document).ready(function($){ var endTime = {$end_time} * 1000; function updateCountdown(){ var now = new Date().getTime(); var d = endTime - now; if(d < 0){ $('.gtbogo-countdown-banner').fadeOut(600); return; } var h = Math.floor(d / 3600000); var m = Math.floor((d % 3600000) / 60000); var s = Math.floor((d % 60000) / 1000); $('#gtbogo-h').text(('0'+h).slice(-2)); $('#gtbogo-m').text(('0'+m).slice(-2)); $('#gtbogo-s').text(('0'+s).slice(-2)); } updateCountdown(); setInterval(updateCountdown, 1000); }); " ); wp_add_inline_style( 'woocommerce-general', $this->get_css() ); } private function get_css() { return ' .gtbogo-countdown-banner{width:100%;box-sizing:border-box;text-align:center;font-family:inherit;color:inherit;border-top:1px solid currentColor;border-bottom:1px solid currentColor;letter-spacing:0.04em} .gtbogo-countdown-inner{display:inline-flex;align-items:center;gap:1.5em;flex-wrap:wrap;justify-content:center} .gtbogo-countdown-message{font-weight:600;text-transform:uppercase;letter-spacing:0.1em;font-size:0.75em;opacity:0.7} .gtbogo-countdown-units{display:flex;gap:0.5em;align-items:center} .gtbogo-countdown-unit{display:inline-flex;flex-direction:column;align-items:center;min-width:2.5em;padding:4px 6px;font-family:inherit} .gtbogo-countdown-num{font-size:1.4em;font-weight:700;line-height:1;font-variant-numeric:tabular-nums} .gtbogo-countdown-label{font-size:0.55em;opacity:0.5;text-transform:uppercase;letter-spacing:0.1em;margin-top:2px} .gtbogo-countdown-sep{font-size:1.2em;opacity:0.4;font-weight:300;align-self:flex-start;padding-top:2px} '; } public function render_timer() { $timer = $this->get_active_timer(); if ( ! $timer ) return; $s = $this->get_intensity_styles(); ?> <div class="gtbogo-countdown-banner" style="padding:<?php echo esc_attr($s['padding']); ?>;opacity:<?php echo esc_attr($s['opacity']); ?>;font-size:<?php echo esc_attr($s['font_size']); ?>"> <div class="gtbogo-countdown-inner"> <?php if ( $timer['message'] ) : ?> <span class="gtbogo-countdown-message"><?php echo esc_html( $timer['message'] ); ?></span> <?php endif; ?> <div class="gtbogo-countdown-units"> <div class="gtbogo-countdown-unit"><span class="gtbogo-countdown-num" id="gtbogo-h">00</span><span class="gtbogo-countdown-label">hrs</span></div> <span class="gtbogo-countdown-sep">:</span> <div class="gtbogo-countdown-unit"><span class="gtbogo-countdown-num" id="gtbogo-m">00</span><span class="gtbogo-countdown-label">min</span></div> <span class="gtbogo-countdown-sep">:</span> <div class="gtbogo-countdown-unit"><span class="gtbogo-countdown-num" id="gtbogo-s">00</span><span class="gtbogo-countdown-label">sec</span></div> </div> </div> </div> <?php } }