<?php if (!defined('ABSPATH')) exit; class GT_BOGO_Referrals { public static function init() { add_action('woocommerce_account_dashboard', array(__CLASS__, 'display_referral_program')); add_action('template_redirect', array(__CLASS__, 'track_referral')); add_action('woocommerce_thankyou', array(__CLASS__, 'reward_referrer'), 10, 1); } public static function display_referral_program() { $user_id = get_current_user_id(); if (!$user_id) return; $referral_code = self::get_referral_code($user_id); $referral_url = home_url('/?ref=' . $referral_code); $referral_count = (int) get_user_meta($user_id, 'gtbogo_referral_count', true); echo '<div class="gtbogo-referral-card" style="padding: 24px; margin: 20px 0; color: inherit; border: 1px solid currentColor; border-radius: 4px;">'; echo '<h3 style="margin: 0 0 15px 0; font-size: 1.1em; font-weight: 600; letter-spacing: 0.05em;">Earn rewards by sharing</h3>'; echo '<p style="margin: 0 0 20px 0; opacity: 0.95;">Share your unique link. When friends make a purchase, you both get 10% extra BOGO discount!</p>'; echo '<div style="background: rgba(255,255,255,0.2); padding: 15px; border-radius: 8px; margin-bottom: 15px;">'; echo '<p style="margin: 0 0 8px 0; font-size: 12px; opacity: 0.9;">Your Referral Link:</p>'; echo '<div style="display: flex; gap: 10px;">'; echo '<input type="text" value="' . esc_attr($referral_url) . '" readonly style="flex: 1; padding: 10px; border: none; border-radius: 6px; font-family: monospace;">'; echo '<button onclick="navigator.clipboard.writeText(\'' . esc_js($referral_url) . '\'); this.textContent=\'Copied!\';" style="padding: 10px 20px; background: currentColor; color: var(--color-base,#fff); border: none; border-radius: 2px; cursor: pointer; font-weight: 600;">Copy</button>'; echo '</div>'; echo '</div>'; echo '<div style="display: flex; gap: 30px;">'; echo '<div>'; echo '<div style="font-size: 36px; font-weight: 700;">' . $referral_count . '</div>'; echo '<div style="opacity: 0.9; font-size: 14px;">Successful Referrals</div>'; echo '</div>'; echo '</div>'; echo '</div>'; } private static function get_referral_code($user_id) { $code = get_user_meta($user_id, 'gtbogo_referral_code', true); if (!$code) { $code = strtoupper(substr(md5($user_id . time()), 0, 8)); update_user_meta($user_id, 'gtbogo_referral_code', $code); } return $code; } public static function track_referral() { if (isset($_GET['ref'])) { $ref_code = sanitize_text_field($_GET['ref']); setcookie('gtbogo_ref', $ref_code, time() + (30 * 24 * 60 * 60), '/'); } } public static function reward_referrer($order_id) { if (!isset($_COOKIE['gtbogo_ref'])) return; $ref_code = sanitize_text_field($_COOKIE['gtbogo_ref']); $users = get_users(array( 'meta_key' => 'gtbogo_referral_code', 'meta_value' => $ref_code )); if (empty($users)) return; $referrer_id = $users[0]->ID; $count = (int) get_user_meta($referrer_id, 'gtbogo_referral_count', true); update_user_meta($referrer_id, 'gtbogo_referral_count', $count + 1); $bonuses = get_user_meta($referrer_id, 'gtbogo_referral_bonuses', true); if (!is_array($bonuses)) $bonuses = array(); $bonuses[] = array('bonus' => 10, 'expires' => time() + (30 * 24 * 60 * 60)); update_user_meta($referrer_id, 'gtbogo_referral_bonuses', $bonuses); $user = get_user_by('ID', $referrer_id); if ($user) { wp_mail( $user->user_email, 'Referral credit applied', '<h2>Someone used your referral link!</h2><p>You\'ve earned a 10% bonus on your next BOGO purchase. Valid for 30 days.</p>', array('Content-Type: text/html; charset=UTF-8') ); } setcookie('gtbogo_ref', '', time() - 3600, '/'); } } GT_BOGO_Referrals::init();