| | |
| | | <?php |
| | | namespace JVBase\managers; |
| | | |
| | | use Exception; |
| | | use JVBase\Instance; |
| | | use JVBase\integrations\services\Cloudflare; |
| | | use JVBase\meta\Form; |
| | |
| | | $this->role = Site::getReferralRole(); |
| | | $this->default_settings['referral_role'] = $this->role; |
| | | |
| | | $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)->user(); |
| | | if (JVB_TESTING) { |
| | | $this->cache->flush(); |
| | | // $this->requestCache->flush(); |
| | | $this->statsCache->flush(); |
| | | } |
| | | $this->cache = Cache::for('referrals'); |
| | | |
| | | $this->statsCache = Cache::for('referral_stats', null, WEEK_IN_SECONDS)->user(); |
| | | |
| | | $this->referralPage = $this->getReferralPageId(); |
| | | $this->settings = $this->getRewardSettings(); |
| | |
| | | public function getTopReferrers(int $limit = 10, string $period = 'all'): array |
| | | { |
| | | return $this->statsCache->remember( |
| | | $this->statsCache->generateKey(['per_page' => $limit, 'period' => $period]), |
| | | ['per_page' => $limit, 'period' => $period], |
| | | function() use ($limit, $period) { |
| | | $where = ''; |
| | | if ($period !== 'all') { |
| | |
| | | 'From ' . $yesterday |
| | | ); |
| | | |
| | | $content .= Emails::getInstance()->spacer(20); |
| | | $content .= Emails::getInstance()->spacer(); |
| | | $content .= Emails::getInstance()->h2('New Referrals'); |
| | | |
| | | // Build list of referrals |
| | |
| | | Emails::getInstance()->stat($from_user->referral_count, 'Total Referrals'), |
| | | Emails::getInstance()->stat($from_user->treated_count, 'Treated') |
| | | ]; |
| | | $cardContent .= Emails::getInstance()->grid($stats, 2); |
| | | $cardContent .= Emails::getInstance()->grid($stats); |
| | | |
| | | $content .= Emails::getInstance()->card($cardContent); |
| | | $rank++; |
| | |
| | | */ |
| | | protected function generateCSV(array $referrals): string |
| | | { |
| | | $cache = Cache::for('referralCSV', HOUR_IN_SECONDS)->connect('referrals'); |
| | | $cache = Cache::for('referralCSV'); |
| | | return $cache->remember( |
| | | 'csv', |
| | | function () use ($referrals) { |
| | |
| | | } |
| | | |
| | | // Check if code is unique |
| | | if ($this->isCodeTaken($code, $user_id)) { |
| | | if ($this->isCodeTaken($code)) { |
| | | add_action('user_profile_update_errors', function($errors) { |
| | | $errors->add('referral_code_taken', |
| | | 'This referral code is already in use.'); |
| | |
| | | Cloudflare::getInstance()->renderTurnstile(); |
| | | $turnstile = ob_get_clean(); |
| | | |
| | | $reward_text = $this->getRewardText(true); |
| | | $reward_text = $this->getRewardText(); |
| | | |
| | | // Pre-fill code if from referral link |
| | | $prefill_code = $_GET['ref'] ?? ''; |
| | |
| | | 'message' => 'Referral settings saved successfully!' |
| | | ]; |
| | | |
| | | } catch (\Exception $e) { |
| | | } catch (Exception $e) { |
| | | return [ |
| | | 'success' => false, |
| | | 'message' => 'Failed to save settings: ' . $e->getMessage() |