| | |
| | | { |
| | | protected $wpdb; |
| | | protected MagicLinkManager $magic_link; |
| | | protected CacheManager $cache; |
| | | protected Cache $cache; |
| | | protected Cache $requestCache; |
| | | protected Cache $statsCache; |
| | | protected string $referrals_table; |
| | | protected ?int $referralPage = null; |
| | | protected string $rewards_table; |
| | |
| | | { |
| | | global $wpdb; |
| | | $this->wpdb = $wpdb; |
| | | $this->cache = CacheManager::for('referrals', WEEK_IN_SECONDS); |
| | | $this->cache = Cache::for('referrals', WEEK_IN_SECONDS); |
| | | $this->requestCache = Cache::for('referral_requests', WEEK_IN_SECONDS)->connect('referrals', true); |
| | | $this->statsCache = Cache::for('referral_stats', WEEK_IN_SECONDS)->connect('referrals', true); |
| | | |
| | | $this->referrals_table = $wpdb->prefix . BASE . 'referrals'; |
| | | $this->rewards_table = $wpdb->prefix . BASE . 'referral_rewards'; |
| | | |
| | |
| | | } |
| | | |
| | | // Clear caches |
| | | $this->cache->clear(); |
| | | $this->cache->flush(); |
| | | |
| | | // Fire action for tracking |
| | | do_action('jvb_referral_processed', $user_id, $referrer->ID, $referral_code); |
| | |
| | | */ |
| | | public function getUserReferrals(int $user_id, array $args = []): array |
| | | { |
| | | return $this->cache->remember( |
| | | $user_id, |
| | | function() use ($user_id, $args) { |
| | | $defaults = [ |
| | | 'status' => 'all', |
| | | 'limit' => 100, |
| | |
| | | |
| | | $args = wp_parse_args($args, $defaults); |
| | | |
| | | return $this->requestCache->remember( |
| | | $this->requestCache->generateKey(array_merge(['user'=>$user_id], $args)), |
| | | function() use ($user_id, $args) { |
| | | $where = $this->wpdb->prepare("WHERE referrer_id = %d", $user_id); |
| | | |
| | | if ($args['status'] !== 'all') { |
| | |
| | | */ |
| | | public function getUserStats(int $user_id): array |
| | | { |
| | | $cache_key = 'stats_' . $user_id; |
| | | $cached = $this->cache->get($cache_key); |
| | | |
| | | if ($cached !== false) { |
| | | return $cached; |
| | | } |
| | | |
| | | return $this->statsCache->remember( |
| | | $user_id, |
| | | function() use ($user_id) { |
| | | $stats = $this->wpdb->get_row($this->wpdb->prepare( |
| | | "SELECT |
| | | COUNT(*) as code_used, |
| | |
| | | |
| | | $stats['total_rewards'] = floatval($rewards ?? 0); |
| | | $stats['user_id'] = $user_id; |
| | | $this->cache->set($cache_key, $stats, HOUR_IN_SECONDS); |
| | | |
| | | return $stats; |
| | | } |
| | | ); |
| | | } |
| | | |
| | | /** |
| | | * Get top referrers for a time period |
| | |
| | | */ |
| | | public function getTopReferrers(int $limit = 10, string $period = 'all'): array |
| | | { |
| | | return $this->statsCache->remember( |
| | | $this->statsCache->generateKey(['limit'=>$limit, 'period' => $period]), |
| | | function() use ($limit, $period) { |
| | | $where = ''; |
| | | |
| | | if ($period !== 'all') { |
| | |
| | | |
| | | return $results; |
| | | } |
| | | ); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * Send daily report if there are new referrals |
| | |
| | | */ |
| | | protected function generateCSV(array $referrals): string |
| | | { |
| | | $cache = Cache::for('referralCSV', HOUR_IN_SECONDS)->connect('referrals'); |
| | | return $cache->remember( |
| | | 'csv', |
| | | function () use ($referrals) { |
| | | $csv = "Referred By,Referee Name,Referee Email,Referee Phone,Referral Code,Status,Referred At,Treated At\n"; |
| | | |
| | | foreach ($referrals as $referral) { |
| | |
| | | |
| | | return $csv; |
| | | } |
| | | ); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * Generate HTML email for weekly report |