Jake Vanderwerf
2026-01-25 b38f03c0e7218762d90fa5092696b127f24f36db
inc/managers/ReferralManager.php
@@ -24,7 +24,9 @@
{
   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;
@@ -48,7 +50,10 @@
   {
      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';
@@ -355,7 +360,7 @@
      }
      // Clear caches
      $this->cache->clear();
      $this->cache->flush();
      // Fire action for tracking
      do_action('jvb_referral_processed', $user_id, $referrer->ID, $referral_code);
@@ -519,9 +524,6 @@
    */
   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,
@@ -532,6 +534,9 @@
            $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') {
@@ -575,13 +580,9 @@
    */
   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,
@@ -603,10 +604,10 @@
      $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
@@ -617,6 +618,9 @@
    */
   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') {
@@ -651,6 +655,9 @@
      return $results;
   }
      );
   }
   /**
    * Send daily report if there are new referrals
@@ -772,6 +779,10 @@
    */
   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) {
@@ -790,6 +801,9 @@
      return $csv;
   }
      );
   }
   /**
    * Generate HTML email for weekly report