<?php if (!defined('ABSPATH')) exit; class GT_BOGO_Exit_Intent { private $id = 'exit_intent'; public function __construct() { if (!$this->enabled()) return; add_action('wp_footer', [$this, 'render']); add_action('wp_enqueue_scripts', [$this, 'enqueue']); } private function get_intensity_styles() { $i = get_option('gtbogo_sp_' . $this->id . '_intensity', 'subtle'); $map = [ 'subtle' => ['opacity'=>'0.75', 'font_size'=>'12px', 'padding'=>'6px 12px', 'border_width'=>'1px'], 'moderate' => ['opacity'=>'0.90', 'font_size'=>'13px', 'padding'=>'8px 16px', 'border_width'=>'1px'], 'bold' => ['opacity'=>'1.0', 'font_size'=>'14px', 'padding'=>'10px 20px', 'border_width'=>'2px'], ]; return $map[$i] ?? $map['subtle']; } private function animate() { return (bool) get_option('gtbogo_sp_' . $this->id . '_anim', 0); } private function enabled() { if (get_option('gtbogo_sp_' . $this->id . '_intensity', 'subtle') === 'off') return false; return (bool) get_option('gtbogo_sp_' . $this->id . '_enabled', 0); } public function enqueue() { $anim = $this->animate() ? 'gtbogo-exit-anim' : ''; wp_add_inline_style('woocommerce-general', $this->get_css($anim)); wp_add_inline_script('jquery', $this->get_js()); } public function render() { $settings = get_option('gt_bogo_engine_settings', []); if (empty($settings['enabled'])) return; $intensity = $this->get_intensity_styles(); $discount = $settings['discount_percent'] ?? 50; $buy = $settings['buy_quantity'] ?? 1; $get = $settings['get_quantity'] ?? 1; ?> <div id="gtbogo-exit-popup" role="dialog" aria-modal="true" aria-label="Exclusive Offer" style="display:none"> <div class="gtbogo-exit-backdrop"></div> <div class="gtbogo-exit-inner" style="font-size:<?php echo esc_attr($intensity['font_size']); ?>;opacity:<?php echo esc_attr($intensity['opacity']); ?>"> <button class="gtbogo-exit-close" aria-label="Close">&times;</button> <div class="gtbogo-exit-body"> <p class="gtbogo-exit-eyebrow"><?php esc_html_e('Before you go', 'gt-bogo-engine'); ?></p> <h2 class="gtbogo-exit-headline"> <?php printf( esc_html__( 'Buy %1$d Get %2$d at %3$d%% off', 'gt-bogo-engine' ), intval($buy), intval($get), intval($discount) ); ?> </h2> <p class="gtbogo-exit-sub"><?php esc_html_e('Applied automatically at checkout. No code needed.', 'gt-bogo-engine'); ?></p> <a href="<?php echo esc_url(wc_get_page_permalink('shop')); ?>" class="gtbogo-exit-cta"> <?php esc_html_e('Shop the Deal', 'gt-bogo-engine'); ?> </a> </div> </div> </div> <?php } private function get_css($anim_class) { return ' #gtbogo-exit-popup{position:fixed;inset:0;z-index:99999;display:flex;align-items:center;justify-content:center} .gtbogo-exit-backdrop{position:absolute;inset:0;background:rgba(0,0,0,0.5);backdrop-filter:blur(2px)} .gtbogo-exit-inner{position:relative;background:inherit;color:inherit;font-family:inherit;border:var(--gtbogo-border-w,1px) solid currentColor;border-radius:8px;max-width:480px;width:90%;padding:40px 32px;text-align:center;background-color:var(--color-base,#fff)} .gtbogo-exit-close{position:absolute;top:12px;right:16px;background:none;border:none;font-size:20px;cursor:pointer;color:inherit;opacity:0.6;line-height:1} .gtbogo-exit-close:hover{opacity:1} .gtbogo-exit-eyebrow{font-size:11px;font-weight:600;letter-spacing:1.5px;text-transform:uppercase;opacity:0.6;margin:0 0 12px} .gtbogo-exit-headline{font-size:1.6em;font-weight:700;margin:0 0 12px;color:inherit;font-family:inherit} .gtbogo-exit-sub{font-size:0.9em;opacity:0.7;margin:0 0 24px} .gtbogo-exit-cta{display:inline-block;padding:12px 28px;background:currentColor;color:var(--color-base,#fff);font-family:inherit;font-weight:600;text-decoration:none;border-radius:4px;transition:opacity 0.2s} .gtbogo-exit-cta:hover{opacity:0.85} ' . ($anim_class ? ' #gtbogo-exit-popup .gtbogo-exit-inner{animation:gtbogo-exit-in 0.35s cubic-bezier(0.34,1.56,0.64,1) both} @keyframes gtbogo-exit-in{from{transform:translateY(-20px) scale(0.96);opacity:0}to{transform:none;opacity:1}} ' : ''); } private function get_js() { return ' (function(){ var shown = sessionStorage.getItem("gtbogo_exit_shown"); if (shown) return; var popup = document.getElementById("gtbogo-exit-popup"); if (!popup) return; var fired = false; document.addEventListener("mouseleave", function(e){ if (e.clientY <= 0 && !fired) { fired = true; sessionStorage.setItem("gtbogo_exit_shown","1"); popup.style.display = "flex"; } }); popup.querySelector(".gtbogo-exit-close").addEventListener("click", function(){ popup.style.display = "none"; }); popup.querySelector(".gtbogo-exit-backdrop").addEventListener("click", function(){ popup.style.display = "none"; }); })(); '; } }