<?php if (!defined('ABSPATH')) { exit; } class GT_BOGO_License_Manager { private static $instance = null; private $api_url = 'https://graphictshirts.shop/bogo/api/license-verify.php'; private $license_option = 'gt_bogo_license_key'; private $cache_key = 'gt_bogo_license_status'; private $cache_duration = 300; public static function instance() { if (self::$instance === null) { self::$instance = new self(); } return self::$instance; } private function __construct() { add_action('admin_init', array($this, 'check_license')); add_action('admin_notices', array($this, 'license_notices')); } public function get_license_key() { return get_option($this->license_option, ''); } public function save_license_key($license_key) { $license_key = sanitize_text_field(trim($license_key)); update_option($this->license_option, $license_key); delete_transient($this->cache_key); return $license_key; } public function check_license($force_refresh = false) { $license_key = $this->get_license_key(); if (empty($license_key)) { return array( 'valid' => false, 'status' => 'no_key', 'message' => 'No license key entered' ); } if (!$force_refresh) { $cached = get_transient($this->cache_key); if ($cached !== false) { return $cached; } } $response = $this->verify_with_api($license_key); if ($response && isset($response['status'])) { set_transient($this->cache_key, $response, $this->cache_duration); } return $response; } private function verify_with_api($license_key) { $domain = parse_url(home_url(), PHP_URL_HOST); $args = array( 'timeout' => 15, 'sslverify' => true, 'body' => array( 'license' => $license_key, 'domain' => $domain ) ); $response = wp_remote_post($this->api_url, $args); if (is_wp_error($response)) { return array( 'valid' => false, 'status' => 'error', 'message' => 'Connection error: ' . $response->get_error_message() ); } $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); if (!$data) { return array( 'valid' => false, 'status' => 'error', 'message' => 'Invalid API response' ); } return $data; } public function is_valid() { $status = $this->check_license(); return isset($status['valid']) && $status['valid'] === true; } public function is_active() { $status = $this->check_license(); return isset($status['status']) && $status['status'] === 'active'; } public function is_expiring() { $status = $this->check_license(); return isset($status['status']) && $status['status'] === 'expiring'; } public function get_status_details() { return $this->check_license(); } public function license_notices() { $screen = get_current_screen(); if (!$screen || strpos($screen->id, 'gt-bogo') === false) { return; } $status = $this->check_license(); if (!isset($status['status'])) { return; } if ($status['status'] === 'no_key') { ?> <div class="notice notice-warning"> <p> <strong>GT BOGO Engine:</strong> Please enter your license key to unlock PRO features. <a href="<?php echo admin_url('admin.php?page=gt-bogo-settings#license'); ?>">Enter License Key</a> </p> </div> <?php return; } if ($status['status'] === 'expired') { ?> <div class="notice notice-error"> <p> <strong>GT BOGO Engine:</strong> Your license has expired. <?php if (isset($status['expires_human'])): ?> (Expired on <?php echo esc_html($status['expires_human']); ?>) <?php endif; ?> <a href="https://graphictshirts.shop/renew" target="_blank">Renew License</a> </p> </div> <?php return; } if ($status['status'] === 'expiring') { $days = isset($status['days_remaining']) ? intval($status['days_remaining']) : 0; ?> <div class="notice notice-warning"> <p> <strong>GT BOGO Engine:</strong> Your license expires in <?php echo $days; ?> day<?php echo $days !== 1 ? 's' : ''; ?>. <a href="https://graphictshirts.shop/renew" target="_blank">Renew Now</a> </p> </div> <?php return; } if (!$status['valid']) { ?> <div class="notice notice-error"> <p> <strong>GT BOGO Engine:</strong> <?php echo esc_html($status['message'] ?? 'Invalid license key'); ?> </p> </div> <?php } } public function has_pro_access() { return $this->is_valid() && ($this->is_active() || $this->is_expiring()); } public function get_renewal_url() { return 'https://graphictshirts.shop/renew'; } public function refresh() { delete_transient($this->cache_key); return $this->check_license(true); } } function gt_bogo_license_manager() { return GT_BOGO_License_Manager::instance(); } function gt_bogo_has_pro() { return gt_bogo_license_manager()->has_pro_access(); }