'per_user', // 'per_user' or 'flat_total' 'referrer_reward_amount' => 25.00, 'referrer_reward_type' => 'fixed', 'referee_reward_type' => 'percentage', // 'percentage' or 'fixed' 'referee_reward_amount' => 20, // 20% or $20 'referee_reward_applies_to' => 'first_order', // 'first_order' or 'all_orders' ]; protected string $role; protected array $settings; public function __construct() { $this->defineTables(); $this->role = Site::getDefaultReferralRole(); $this->default_settings['referral_role'] = $this->role; global $wpdb; $this->wpdb = $wpdb; $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); if (JVB_TESTING) { $this->cache->flush(); $this->requestCache->flush(); $this->statsCache->flush(); } $this->referrals_table = $wpdb->prefix . BASE . 'referrals'; $this->rewards_table = $wpdb->prefix . BASE . 'referral_rewards'; $this->referralPage = $this->getReferralPageId(); $this->settings = $this->getRewardSettings(); add_action('jvbUserRegistered', [$this, 'processRegistrationToken'], 10, 3); add_action('jvb_add_token_inputs', [$this, 'addLoginInputs'], 10, 1); add_action('user_register', [$this, 'processReferral'], 10, 1); // Add meta boxes for admin to manage referrals add_action('show_user_profile', [$this, 'displayUserReferralInfo']); add_action('edit_user_profile', [$this, 'displayUserReferralInfo']); // Save referral code changes add_action('personal_options_update', [$this, 'saveUserReferralCode']); add_action('edit_user_profile_update', [$this, 'saveUserReferralCode']); add_filter('jvbNewUserEmail', [$this, 'addReferralToWelcomeEmail'], 99, 2); add_filter('jvbAdditionalActions', [$this, 'outputShareWidget']); add_action('wp_enqueue_scripts', [$this, 'enqueueScripts']); // Schedule cron jobs for reports $this->registerCronJobs(); // Add admin bar label for referral page add_action('admin_bar_menu', [$this, 'addReferralPageLabel'], 999); // Add admin notice to referral page edit screen add_action('admin_notices', [$this, 'showReferralPageNotice']); add_filter('jvbDashboardPage', [$this, 'renderDashPage'], 10, 2); // Handle settings save add_action('admin_init', [$this, 'registerSettings']); // Handle admin page form submission add_filter('jvb_admin_page_submission', [$this, 'handleAdminSubmission'], 10, 3); } protected function defineTables():void { $this->defineReferralsTable(); $this->defineCodeTable(); $this->defineJaneClientsTable(); $this->defineRewardsTable(); $this->defineTreatmentsTable(); } protected function defineReferralsTable():void { $table = CustomTable::for('referrals'); $table->setColumns([ 'id' => 'bigint(20) unsigned NOT NULL AUTO_INCREMENT', 'from_user' => "{$table->getUserIDType()} NOT NULL", 'to_user' => "{$table->getUserIDType()} NOT NULL", 'to_name' => 'varchar(255) NOT NULL', 'to_email' => 'varchar(255) NOT NULL', 'to_phone' => 'varchar(50) NOT NULL', 'referral_code'=> 'varchar(50) NOT NULL', 'status' => "ENUM('pending', 'consulted', 'treated', 'cancelled') DEFAULT 'pending'", 'created_at'=> 'datetime DEFAULT CURRENT_TIMESTAMP', 'consulted_at'=> 'datetime DEFAULT NULL', 'treated_at'=> 'datetime DEFAULT NULL', 'treatment_count' => 'int DEFAULT 0', 'notes' => 'text DEFAULT NULL', ]); $table->setKeys([ ['key' => 'PRIMARY', 'value' => 'id'], ['key' => 'UNIQUE', 'value' => '(`to_user`)'], 'from_user (`from_user`)', 'status (`status`)', 'code (`referral_code`)', 'date (`created_at`)', 'consult (`consulted_at`)' ]); $base = BASE; $table->setConstraints([ "CONSTRAINT `{$base}referral_from_user_fk` FOREIGN KEY (`from_user`) REFERENCES `{$table->getUserTable()}` (`ID`) ON DELETE CASCADE", "CONSTRAINT `{$base}referral_to_user_fk` FOREIGN KEY (`to_user`) REFERENCES `{$table->getUserTable()}` (`ID`) ON DELETE CASCADE" ]); $table->defineTable(); $this->referrals = $table; } protected function defineCodeTable():void { $table = CustomTable::for('referrals_codes'); $table->setColumns([ 'id' => 'bigint(20) unsigned NOT NULL AUTO_INCREMENT', 'user_id' => "{$table->getUserIDType()} NOT NULL", 'code' => 'varchar(50) NOT NULL', 'created_at'=> 'datetime DEFAULT CURRENT_TIMESTAMP', ]); $table->setKeys([ ['key' => 'PRIMARY', 'value' => '(`id`)'], ['key' => 'UNIQUE', 'value' => '(`code`)'], 'user (`user_id`)', ]); $base = BASE; $table->setConstraints([ "CONSTRAINT `{$base}referral_code_user_fk` FOREIGN KEY (`user_id`) REFERENCES `{$table->getUserTable()}` (`ID`) ON DELETE CASCADE", ]); $table->defineTable(); $this->codes = $table; } protected function defineJaneClientsTable():void { $table = CustomTable::for('referrals_jane_clients'); $table->setColumns([ 'id' => 'bigint(20) unsigned NOT NULL AUTO_INCREMENT', 'patient_guid' => 'varchar(50) NOT NULL', 'user_id' => "{$table->getUserIDType()} NOT NULL", 'first_name' => 'varchar(100) NOT NULL', 'last_name' => 'varchar(100) NOT NULL', 'email' => 'varchar(255) NOT NULL', 'imported_at' => 'datetime DEFAULT CURRENT_TIMESTAMP', 'updated_at' => 'datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', ]); $table->setKeys([ ['key' => 'PRIMARY', 'value' => '(`id`)'], ['key' => 'UNIQUE', 'value' => '(`patient_guid`)'], 'user (`user_id`)', 'email (`email`)', ]); $base = BASE; $table->setConstraints([ "CONSTRAINT `{$base}jane_clients_user` FOREIGN KEY (`user_id`) REFERENCES `{$table->getUserTable()}` (`ID`) ON DELETE CASCADE", ]); $table->defineTable(); $this->janeClients = $table; } protected function defineRewardsTable():void { $table = CustomTable::for('referrals_rewards'); $table->setColumns([ 'id' => 'bigint(20) unsigned NOT NULL AUTO_INCREMENT', 'referral_id' => 'bigint(20) unsigned NOT NULL', 'user_id' => "{$table->getUserIDType()} NOT NULL", 'reward_type' => "ENUM('referrer', 'referee') NOT NULL", 'amount' => 'decimal(10,2) NOT NULL', 'reward_calculation'=> "ENUM('percentage', 'fixed')", 'status' => "ENUM('available', 'redeemed', 'expired', 'cancelled') DEFAULT 'available'", 'created_at' => 'datetime NOT NULL DEFAULT CURRENT_TIMESTAMP', 'updated_at' => 'datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', 'redeemed_at' => 'datetime DEFAULT NULL', 'expires_at' => 'datetime DEFAULT NULL', 'notes' => 'text DEFAULT NULL', ]); $table->setKeys([ ['key' => 'PRIMARY', 'value' => '(`id`)'], 'referral (`referral_id`)', 'user (`user_id`)', 'status (`status`)', 'type (`reward_type`)' ]); $base = BASE; $table->setConstraints([ "CONSTRAINT `{$base}reward_referral` FOREIGN KEY (`referral_id`) REFERENCES `{$this->referrals->getFullTableName()}` (`id`) ON DELETE CASCADE", "CONSTRAINT `{$base}reward_user` FOREIGN KEY (`user_id`) REFERENCES `{$table->getUserTable()}` (`ID`) ON DELETE CASCADE" ]); $table->defineTable(); $this->rewards = $table; } protected function defineTreatmentsTable():void { $table = CustomTable::for('referral_treatments'); $table->setColumns([ 'id' => 'bigint(20) unsigned NOT NULL AUTO_INCREMENT', 'referral_id' => 'bigint(20) unsigned NOT NULL', 'user_id' => "{$table->getUserIDType()} NOT NULL", 'treatment_type'=> 'varchar(100) NOT NULL', //Tier 1-6, Brows, etc 'treatment_date'=> 'datetime NOT NULL', 'invoice_number'=> 'varchar(50) DEFAULT NULL', 'amount' => 'decimal(10,2) DEFAULT NULL', 'status' => "ENUM('completed', 'no_show', 'cancelled') DEFAULT 'completed'", 'imported_at' => 'datetime DEFAULT CURRENT_TIMESTAMP', ]); $table->setKeys([ ['key' => 'PRIMARY', 'value' => '(`id`)'], 'referral (`referral_id`)', 'user (`user_id`)', 'date (`treatment_date`)', 'type (`treatment_type`)', ]); $base = BASE; $table->setConstraints([ "CONSTRAINT `{$base}treatment_referral` FOREIGN KEY (`referral_id`) REFERENCES `{$this->referrals->getFullTableName()}` (`id`) ON DELETE CASCADE", "CONSTRAINT `{$base}treatment_user` FOREIGN KEY (`user_id`) REFERENCES `{$table->getUserTable()}` (`ID`) ON DELETE CASCADE" ]); $table->defineTable(); $this->treatments = $table; } public function getSettings():array { return $this->settings; } public function getRole():string { return $this->role; } public function addLoginInputs(string $action):void { if (array_key_exists('ref', $_GET)) { echo ''; } } public function modifyLoginText(array $defaults):array { if (!array_key_exists('ref', $_GET)){ return $defaults; } $code = $_GET['ref']; $user = $this->getUserByReferralCode($code); $desc = ($user) ? strtok($user->display_name, ' ') . ' invited you ' : 'You\'ve been invited '; $defaults['title'] = 'Register your Account. Get Your Reward.'; $defaults['description'] = [ $desc.' to see the difference with us.', 'Oh, and you\'ll get 20% off your first treatment!', 'Finish this account creation (you\'ll still need to make an account on Jane App to book), or let us know when you come in for your appointment.' ]; $defaults['extra'] = [ 'Once you\'re in, you\'ll get your own code you can share.', 'Get a $25 credit for each person you send our way who comes in for their first treatment!' ]; return $defaults; } public function enqueueScripts():void { $requirements = [ 'jvb-utility', 'jvb-a11y', 'jvb-popup', 'jvb-tabs', 'jvb-data-store', ]; if (Site::hasIntegration('cloudflare') && JVB()->connect('cloudflare')->isSetUp()) { JVB()->connect('cloudflare')->enqueueTurnstileScripts(); } if (is_singular(BASE.'dash')) { $requirements[] = 'jvb-form'; $requirements[] = 'jvb-view'; wp_enqueue_script('jvb-referral-admin', JVB_URL.'assets/js/min/referralAdmin.min.js', ['jvb-referral'], '1.0.0', true); } wp_enqueue_script( 'jvb-referral', JVB_URL . 'assets/js/min/referral.min.js', $requirements, '1.0.0', true ); } /** * Register cron jobs for automated reporting */ protected function registerCronJobs(): void { add_action(BASE . 'referral_daily_report', [$this, 'sendDailyReport']); add_action(BASE . 'referral_weekly_report', [$this, 'sendWeeklyReport']); // Schedule if not already scheduled if (!wp_next_scheduled(BASE . 'referral_daily_report')) { wp_schedule_event(strtotime('tomorrow 9:00'), 'daily', BASE . 'referral_daily_report'); } if (!wp_next_scheduled(BASE . 'referral_weekly_report')) { wp_schedule_event(strtotime('next Monday 9:00'), 'weekly', BASE . 'referral_weekly_report'); } } public function createCode(int $user_id, string $code):string|false { $code = sanitize_title($code); $existing = $this->codes->get(['code' => $code]); if ($existing) { if ($existing['user_id'] !== $user_id) { return false; } return $code; } $success = $this->codes->insert([ 'user_id' => $user_id, 'code' => $code ]); if ($success) { return $code; } return false; } /** * Generate or get existing referral code for a user * * @param int $user_id * @param string|null $custom_code Optional custom code * @return string|WP_Error */ public function getUserReferralCode(int $user_id, ?string $custom_code = null):string|false { $user = get_userdata($user_id); if (!$user) { return false; } $existing = $this->codes->pluck('code', ['user_id' => $user_id],'created_at', 'DESC'); if (!empty($existing) && !$custom_code) { return $existing[0]; } if ($custom_code && !empty($existing) && !in_array($custom_code, $existing)) { $test = $this->createCode($user_id, $custom_code); if ($test) { return $this->codes->pluck('code', ['user_id' => $user_id], 'created_at', 'DESC')[0]; } else { return $existing[0]; } } // Generate new code if custom provided or none exists $code = $this->generateReferralCode($user); $success = $this->createCode($user_id, $code); if ($success) { return $this->codes->pluck('code', ['user_id' => $user_id], 'created_at', 'DESC')[0]; } return false; } /** * Generate a referral code based on user info * * @param WP_User $user * @return string */ protected function generateReferralCode(WP_User $user): string { // Create code from first and last name $first = sanitize_title($user->first_name ?: 'user'); $last = sanitize_title($user->last_name ?: wp_generate_password(4, false)); $base_code = strtoupper(substr($first, 0, 4) . substr($last, 0, 4)); // Ensure uniqueness $code = $base_code; $suffix = 1; while ($this->isCodeTaken($code)) { $code = $base_code . $suffix; $suffix++; } return $code; } /** * Check if a referral code is already taken * * @param string $code * @return bool */ protected function isCodeTaken(string $code): bool { return (bool) $this->codes->get(['code' => $code]); } public function processRegistrationToken(int $user_id, string $email, array $data): void { // Check for referral code in data $code = $data['referral_code'] ?? ''; if (empty($code)) { return; } // Store in session/cookie for processReferral to pick up if (session_status() === PHP_SESSION_NONE) { session_start(); } $_SESSION[BASE . 'referral_code'] = sanitize_text_field($code); setcookie( BASE . 'referral_code', sanitize_text_field($code), time() + (86400 * 30), '/' ); } /** * Track a new referral when user registers * * @param int $user_id * @param array $userData * @return bool; */ public function processReferral(int $user_id, array $userData): bool { $referral = $this->referrals->get(['to_user' => $user_id]); if (empty($referral)) { $referral = $this->referrals->get(['to_email' => $userData['email']]); } if (empty($referral)) { // Check session/cookie if not in meta if (session_status() === PHP_SESSION_NONE) { session_start(); } $referral_code = $_SESSION[BASE . 'referral_code'] ?? $_COOKIE[BASE . 'referral_code'] ?? ''; if (!empty ($referral_code)) { $referral = [ 'to_user' => $user_id, 'referral_code' => $referral_code, 'to_email' => $userData['user_email'] ]; } } if (empty($referral)) { return false; // No referral code - regular registration } // Find the referrer $referrer = $this->codes->pluck('user_id', ['code' => $referral['referral_code']]); if (empty($referrer)) { //This should not happen, but whatever return false; } $referrer = $referrer[0]; $record = $this->referrals->findOrCreate([ 'to_user' => $user_id, 'referral_code' => $referral['referral_code'], ], [ 'from_user' => $referrer, 'to_email' => $referral['to_email'], 'to_name' => $userData['first_name'], // 'to_phone' => 'status' => 'pending' ]); if (!$record) { error_log('[ReferralManager]::processReferral Could not update record for user: '.print_r($referral, true)); return false; } // Clean up temp data delete_user_meta($user_id, BASE . 'pending_referral_code'); if (isset($_SESSION[BASE . 'referral_code'])) { unset($_SESSION[BASE . 'referral_code']); } if (isset($_COOKIE[BASE . 'referral_code'])) { setcookie(BASE . 'referral_code', '', time() - 3600, '/'); } // Clear caches $this->cache->flush(); // Fire action for tracking do_action('jvb_referral_processed', $user_id, $referrer->ID, $referral['referral_code']); // Send notification to referrer $this->sendReferrerNotification($referrer->ID, $userData['display_name']); return true; } /** * Create a referral record in the database * * @param int $referrer_id * @param int $referee_id * @param string $code * @return int|false */ public function createReferral(int $referrer_id, int $referee_id, string $code) { $user = get_user_by('ID', $referee_id); return $this->wpdb->insert( $this->referrals_table, [ 'referrer_id' => $referrer_id, 'referee_id' => $referee_id, 'referee_name' => $user->display_name, 'referee_email' => $user->user_email, 'referee_phone' => get_user_meta($referee_id, BASE . 'phone', true) ?: '', 'referral_code' => $code, 'status' => 'pending', 'referred_at' => current_time('mysql') ], ['%d', '%d', '%s', '%s', '%s', '%s', '%s', '%s'] ); } /** * Get user by referral code * * @param string $code * @return WP_User|null */ public function getUserByReferralCode(string $code): ?WP_User { return $this->cache->remember( $code, function () use ($code) { $users = get_users([ 'meta_key' => BASE . 'referral_code', 'meta_value' => $code, 'number' => 1 ]); return $users[0] ?? null; } ); } /** * Get referral record by referee ID * * @param int $referee_id * @return object|null */ public function getReferralByReferee(int $referee_id): ?object { $result = $this->wpdb->get_row($this->wpdb->prepare( "SELECT * FROM {$this->referrals_table} WHERE referee_id = %d", $referee_id )); return $result ?: null; } /** * Mark a referral as treated/rewarded * * @param int $referral_id * @param bool $treated * @return bool */ public function markAsTreated(int $referral_id, bool $treated = true): bool { $status = $treated ? 'treated' : 'pending'; $result = $this->wpdb->update( $this->referrals_table, [ 'status' => $status, 'treated_at' => $treated ? current_time('mysql') : null ], ['id' => $referral_id], ['%s', '%s'], ['%d'] ); if ($result && $treated) { // Create reward records when marking as treated $this->createRewardRecords($referral_id); } return $result !== false; } /** * Create reward records for both referrer and referee * * @param int $referral_id */ protected function createRewardRecords(int $referral_id): void { $referral = $this->wpdb->get_row($this->wpdb->prepare( "SELECT * FROM {$this->referrals_table} WHERE id = %d", $referral_id )); if (!$referral) { return; } // Create referrer reward $this->wpdb->insert( $this->rewards_table, [ 'referral_id' => $referral_id, 'user_id' => $referral->referrer_id, 'reward_type' => 'referrer', 'amount' => $this->settings['referrer_reward_amount'], 'status' => 'available', 'created_at' => current_time('mysql') ], ['%d', '%d', '%s', '%f', '%s', '%s'] ); // Create referee reward $referee_amount = $this->settings['referee_reward_type'] === 'percentage' ? $this->settings['referee_reward_amount'] // Store as percentage : $this->settings['referee_reward_amount']; // Store as fixed amount $this->wpdb->insert( $this->rewards_table, [ 'referral_id' => $referral_id, 'user_id' => $referral->referee_id, 'reward_type' => 'referee', 'amount' => $referee_amount, 'reward_calculation' => $this->settings['referee_reward_type'], 'status' => 'available', 'created_at' => current_time('mysql') ], ['%d', '%d', '%s', '%f', '%s', '%s', '%s'] ); } /** * Get referrals for a user * * @param int $user_id * @param array $args * @return array */ public function getUserReferrals(int $user_id, array $args = []): array { $defaults = [ 'status' => 'all', 'limit' => 100, 'offset' => 0, 'orderby' => 'referred_at', 'order' => 'DESC' ]; $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') { $where .= $this->wpdb->prepare(" AND status = %s", $args['status']); } $query = "SELECT * FROM {$this->referrals_table} {$where} ORDER BY {$args['orderby']} {$args['order']} LIMIT {$args['limit']} OFFSET {$args['offset']}"; $results = $this->wpdb->get_results($query); return array_map(function($referral) { $last_invite = get_transient('referral_last_invite_' . md5($referral->referee_email)); $can_resend = !$last_invite || (time() - $last_invite) > WEEK_IN_SECONDS; $status = match($referral->status) { 'consulted' => 'Awaiting Treatment', 'treated' => 'Rewarded!', default => 'Pending', }; return [ 'id' => $referral->id, 'referee_name' => $referral->referee_name, 'referee_email' => $referral->referee_email, 'referred_at' => JVB()->routes('referral')->formatTimestamp($referral->referred_at), 'referral_status'=> $status, 'can_resend' => $can_resend ]; }, $results); } ); } /** * Get referral statistics for a user * * @param int $user_id * @return array */ public function getUserStats(int $user_id): array { return $this->statsCache->remember( $user_id, function() use ($user_id) { $stats = $this->wpdb->get_row($this->wpdb->prepare( "SELECT COUNT(*) as code_used, SUM(CASE WHEN status IN ('consulted', 'treated') THEN 1 ELSE 0 END) as consultations, SUM(CASE WHEN status = 'treated' THEN 1 ELSE 0 END) as treatments, SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending FROM {$this->referrals_table} WHERE referrer_id = %d", $user_id ), ARRAY_A); // Get total rewards earned (available + redeemed) $rewards = $this->wpdb->get_var($this->wpdb->prepare( "SELECT SUM(amount) FROM {$this->rewards_table} WHERE user_id = %d AND reward_type = 'referrer'", $user_id )); $stats['total_rewards'] = floatval($rewards ?? 0); $stats['user_id'] = $user_id; return $stats; } ); } /** * Get top referrers for a time period * * @param int $limit * @param string $period 'day'|'week'|'month'|'all' * @return array */ 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') { $date_where = match($period) { 'day' => "referred_at >= DATE_SUB(NOW(), INTERVAL 1 DAY)", 'week' => "referred_at >= DATE_SUB(NOW(), INTERVAL 1 WEEK)", 'month' => "referred_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH)", default => "1=1" }; $where = "WHERE {$date_where}"; } $query = "SELECT referrer_id, COUNT(*) as referral_count, SUM(CASE WHEN status = 'treated' THEN 1 ELSE 0 END) as treated_count FROM {$this->referrals_table} {$where} GROUP BY referrer_id ORDER BY referral_count DESC LIMIT {$limit}"; $results = $this->wpdb->get_results($query); // Enrich with user data foreach ($results as &$result) { $user = get_user_by('ID', $result->referrer_id); $result->user_name = $user ? $user->display_name : 'Unknown'; $result->user_email = $user ? $user->user_email : ''; } return $results; } ); } /** * Send daily report if there are new referrals */ public function sendDailyReport(): void { $yesterday = date('Y-m-d', strtotime('-1 day')); $new_referrals = $this->wpdb->get_results($this->wpdb->prepare( "SELECT r.*, u.display_name as referrer_name FROM {$this->referrals_table} r JOIN {$this->wpdb->users} u ON r.referrer_id = u.ID WHERE DATE(r.referred_at) = %s ORDER BY r.referred_at DESC", $yesterday )); if (empty($new_referrals)) { return; } $content = JVB()->email()->h1('Daily Referral Report'); $content .= JVB()->email()->stat( count($new_referrals), count($new_referrals) === 1 ? 'New Referral' : 'New Referrals', 'From ' . $yesterday ); $content .= JVB()->email()->spacer(20); $content .= JVB()->email()->h2('New Referrals'); // Build list of referrals foreach ($new_referrals as $ref) { $cardContent = sprintf( '
%s (%s)
', esc_html($ref->referee_name), esc_html($ref->referee_email) ); $cardContent .= sprintf( 'Referred by: %s | Code: %s
', JVB()->email()->colours['dark-200'], esc_html($ref->referrer_name), JVB()->email()->badge($ref->referral_code, 'info') ); $content .= JVB()->email()->card($cardContent); } $to = get_option('admin_email'); $subject = sprintf( '[%s] %d New Referral%s', get_bloginfo('name'), count($new_referrals), count($new_referrals) !== 1 ? 's' : '' ); JVB()->email()->sendEmail($to, $subject, $content, 'DAILY REPORT'); } /** * Send weekly report with top referrers */ public function sendWeeklyReport(): void { $top_referrers = $this->getTopReferrers(10, 'week'); $total_referrals = $this->wpdb->get_var( "SELECT COUNT(*) FROM {$this->referrals_table} WHERE referred_at >= DATE_SUB(NOW(), INTERVAL 1 WEEK)" ); if ($total_referrals == 0) { return; } $content = JVB()->email()->h1('Weekly Referral Summary'); $content .= JVB()->email()->stat( $total_referrals, 'Total Referrals', 'This week' ); $content .= JVB()->email()->spacer(30); $content .= JVB()->email()->h2('Top 10 Referrers'); // Leaderboard style $rank = 1; foreach ($top_referrers as $referrer) { $rankBadge = $rank <= 3 ? JVB()->email()->badge('#' . $rank, $rank === 1 ? 'success' : 'info') : '#' . $rank . ''; $cardContent = sprintf( '%s %s
', $rankBadge, esc_html($referrer->user_name) ); $stats = [ JVB()->email()->stat($referrer->referral_count, 'Total Referrals'), JVB()->email()->stat($referrer->treated_count, 'Treated') ]; $cardContent .= JVB()->email()->grid($stats, 2); $content .= JVB()->email()->card($cardContent); $rank++; } $to = get_option('admin_email'); $subject = '[' . get_bloginfo('name') . '] Weekly Referral Summary - ' . date('F j, Y'); JVB()->email()->sendEmail($to, $subject, $content, 'WEEKLY SUMMARY'); } /** * Generate CSV content from referrals * * @param array $referrals * @return string */ 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) { $csv .= sprintf( '"%s","%s","%s","%s","%s","%s","%s","%s"' . "\n", $referral->referrer_name ?? 'Unknown', $referral->referee_name, $referral->referee_email, $referral->referee_phone, $referral->referral_code, $referral->status, $referral->referred_at, $referral->treated_at ?? 'Not yet' ); } return $csv; } ); } /** * Generate HTML email for weekly report * * @param array $top_referrers * @param int $total_referrals * @return string */ protected function generateWeeklyReportEmail(array $top_referrers, int $total_referrals): string { $content = sprintf( 'This week you had %d total referral%s.
', $total_referrals, $total_referrals !== 1 ? 's' : '' ); $referrers = []; $rank = 1; foreach ($top_referrers as $referrer) { $referrers[] = [ 'label' => '#' . $rank++ . ' - ' . esc_html($referrer->user_name), 'value' => sprintf( 'Total Referrals: %d | Treated: %d', $referrer->referral_count, $referrer->treated_count ) ]; } $content .= JVB()->email()->table($referrers, 'Top 10 Referrers This Week'); return $content; } /** * Get reward settings * * @return array */ public function getRewardSettings(): array { $saved = get_option(BASE . 'referral_settings', []); return wp_parse_args($saved, $this->default_settings); } /** * Display referral info in user profile * * @param WP_User $user */ public function displayUserReferralInfo(WP_User $user): void { if (!current_user_can('edit_user', $user->ID)) { return; } $referral_code = get_user_meta($user->ID, BASE . 'referral_code', true); $stats = $this->getUserStats($user->ID); $referrals = $this->getUserReferrals($user->ID, ['limit' => 10]); ?>|
Users can sign up with this code. Share link: |
| Total Referrals: | |
|---|---|
| Treated: | |
| Pending: | |
| Available Rewards: | $ |
| Redeemed Rewards: | $ |
| Name | Status | Referred At | Actions | |
|---|---|---|---|---|
| referee_name); ?> | referee_email); ?> | status)); ?> | referred_at); ?> | status === 'pending'): ?> |
Enter your referral code to get started!
', jvbIcon('confetti'), esc_html($reward_text), ($referrer_name ? '' . esc_html($referrer_name) . ' invited you to join us
' : '') ); $codeForm = sprintf( 'We\'ve sent you a magic link to complete your registration. Click the link to activate your account and claim your reward!
Can\'t find it? Check your spam folder.
Already have an account?
Log in to see your rewards!
We\'ve sent you a magic link to log in - no password required! Click the link in your email to log in.
Can\'t find it? Check your spam folder.
You'll receive:
your first treatment!
Your referral code has been applied. Book your free consultation now to claim your reward!
Book Your Free ConsultationInvite friends. Earn rewards.
= esc_url($share_url) ?>
Quickest and easiest: autofills your code.
= esc_html($referral_code) ?>
Manually copy and paste the code
Bulk-invite your friends via email - the link will pre-fill their name, email, and code!
Since you were referred by a friend, you\'ve earned %s your first booking!
Your reward will be automatically applied when you book.
', $reward_text, ); $code = $this->getUserReferralCode($user->ID); $yourCode = ''; if ($code) { $share_url = $this->getShareURL($code); $yourCode = sprintf( 'If you find you love what we can do for you, you can share your own code!
Your Referral Code: %s
Or click the button below:
%s%s
Click the button below to register and claim your reward:
%sThis invitation expires in 30 days.
', esc_html($referrer->display_name), esc_html(get_bloginfo('name')), nl2br(esc_html($message)), esc_html($reward_text), JVB()->email()->button($registration_url, 'Register & Get Your Reward') ); // Send email $sent = JVB()->email()->sendEmail( $invitee_email, $subject, $email_content ); if (!$sent) { return new WP_Error('email_failed', 'Failed to send invitation email'); } return [ 'success' => true, 'message' => 'Invitation sent successfully', 'email' => $invitee_email, 'name' => $invitee_name ]; } /** * Send multiple referral invitations * * @param int $user_id Referrer's user ID * @param array $invitations Array of ['email' => '', 'name' => ''] * @return array Results with success/failed arrays */ public function sendBatchReferralInvitations(int $user_id, array $invitations, string $subject, string $message): array { $results = [ 'success' => [], 'failed' => [] ]; foreach ($invitations as $invite) { $email = $invite['email'] ?? ''; $name = $invite['name'] ?? ''; if (empty($email) || empty($name)) { $results['failed'][] = [ 'email' => $email, 'name' => $name, 'reason' => 'Missing email or name' ]; continue; } $result = $this->sendReferralInvitation($user_id, $email, $name, $subject, $message); if (is_wp_error($result)) { $results['failed'][] = [ 'email' => $email, 'name' => $name, 'reason' => $result->get_error_message() ]; } else { $results['success'][] = [ 'email' => $email, 'name' => $name ]; } // Small delay between sends to be respectful usleep(100000); // 0.1 seconds } return [ 'success' => !empty($results['success']), 'result' => $results, 'summary' => sprintf( 'Sent %d invitations, %d failed', count($results['success']), count($results['failed']) ) ]; } /** * Check email invitation rate limit (15 per hour) * * @param int $user_id * @return true|string True if allowed, error message if limited */ protected function checkEmailRateLimit(int $user_id):bool|string { $hourly_key = 'referral_invites_hour_' . $user_id; $count = (int) get_transient($hourly_key); if ($count >= 15) { return 'hourly_limit_reached'; } set_transient($hourly_key, $count + 1, HOUR_IN_SECONDS); return true; } /** * Check if an email has already been invited * * @param string $email * @return bool */ protected function isEmailInvited(string $email): bool { // Check invitation tracking table $invitation_key = 'referral_invite_' . md5($email); $invited = get_transient($invitation_key); if ($invited) { return true; } // Check if there's a pending referral for this email $existing = $this->wpdb->get_var($this->wpdb->prepare( "SELECT id FROM {$this->referrals_table} WHERE referee_email = %s", $email )); return !empty($existing); } /** * Record invitation attempt (for tracking and preventing duplicates) * * @param int $user_id * @param string $email * @param string $name */ protected function recordInvitationAttempt(int $user_id, string $email, string $name): void { // Store for 30 days (same as magic link invitation validity) $invitation_key = 'referral_invite_' . md5($email); $data = [ 'inviter_id' => $user_id, 'email' => $email, 'name' => $name, 'sent_at' => current_time('mysql') ]; set_transient($invitation_key, $data, 30 * DAY_IN_SECONDS); // Also log in user meta for tracking $sent_invites = get_user_meta($user_id, BASE . 'referral_invites_sent', true) ?: []; $sent_invites[] = [ 'email' => $email, 'name' => $name, 'sent_at' => current_time('mysql') ]; update_user_meta($user_id, BASE . 'referral_invites_sent', $sent_invites); } /** * Get user's invitation stats * * @param int $user_id * @return array */ public function getUserInvitationStats(int $user_id): array { $sent_invites = get_user_meta($user_id, BASE . 'referral_invites_sent', true) ?: []; // Count invites sent in last hour $one_hour_ago = strtotime('-1 hour'); $recent_count = 0; foreach ($sent_invites as $invite) { if (strtotime($invite['sent_at']) > $one_hour_ago) { $recent_count++; } } return [ 'total_sent' => count($sent_invites), 'sent_last_hour' => $recent_count, 'remaining_this_hour' => max(0, 15 - $recent_count), 'can_send_more' => $recent_count < 15 ]; } /** * Export referrals for Jane App cross-reference * * @param string $start_date Y-m-d format * @param string $end_date Y-m-d format * @return string CSV content */ public function exportReferrals(string $start_date, string $end_date): string { $referrals = $this->wpdb->get_results($this->wpdb->prepare( "SELECT r.id, r.referee_name, r.referee_email, r.referee_phone, r.referral_code, r.referred_at, r.status, r.treated_at, u.display_name as referrer_name, u.user_email as referrer_email FROM {$this->referrals_table} r JOIN {$this->wpdb->users} u ON r.referrer_id = u.ID WHERE DATE(r.referred_at) BETWEEN %s AND %s ORDER BY r.referred_at DESC", $start_date, $end_date )); // Build CSV $csv_lines = []; // Headers $csv_lines[] = [ 'Referral ID', 'Referee Name', 'Referee Email', 'Referee Phone', 'Referral Code', 'Referred Date', 'Status', 'Treated Date', 'Referrer Name', 'Referrer Email' ]; // Data rows foreach ($referrals as $ref) { $csv_lines[] = [ $ref->id, $ref->referee_name, $ref->referee_email, $ref->referee_phone ?: 'N/A', $ref->referral_code, $ref->referred_at, ucfirst($ref->status), $ref->treated_at ?: 'N/A', $ref->referrer_name, $ref->referrer_email ]; } // Convert to CSV string $output = fopen('php://temp', 'r+'); foreach ($csv_lines as $line) { fputcsv($output, $line); } rewind($output); $csv_content = stream_get_contents($output); fclose($output); return $csv_content; } /** * Add referral settings subpage to admin menu * Add referral settings subpage to admin menu * * @param array $subpages * @return array */ public static function addSubpage():void { $subpage = [ 'page_title' => 'Referral System', 'menu_title' => 'Referrals', 'capability' => 'manage_options', 'menu_slug' => BASE . 'referral-admin', 'callback' => [self::class, 'renderAdminPageStatic'] ]; AdminPages::addSubPage(BASE.'referral-admin', $subpage); } /** * Static wrapper for renderAdminPage * Called by WordPress when admin page is rendered */ public static function renderAdminPageStatic(): void { // Get the properly initialized instance from JVB singleton JVB()->referrals()->renderAdminPage(); } /** * Register settings */ public function registerSettings(): void { register_setting( BASE . 'referral_settings', BASE . 'referral_page_id', [ 'type' => 'integer', 'sanitize_callback' => 'absint', 'default' => 0 ] ); register_setting( BASE . 'referral_settings', BASE . 'referral_reward_settings', [ 'type' => 'array', 'sanitize_callback' => [$this, 'sanitizeRewardSettings'], 'default' => $this->default_settings ] ); } /** * Sanitize reward settings */ public function sanitizeRewardSettings(array $settings): array { return [ 'referrer_reward_applies_to' => in_array($settings['referrer_reward_applies_to'] ?? '', ['per_user', 'flat_total']) ? $settings['referrer_reward_applies_to'] : 'per_user', 'referrer_reward_amount' => floatval($settings['referrer_reward_amount'] ?? 25.00), 'referrer_reward_type' => in_array($settings['referrer_reward_type'] ?? '', ['fixed', 'percentage']) ? $settings['referrer_reward_type'] : 'fixed', 'referee_reward_type' => in_array($settings['referee_reward_type'] ?? '', ['percentage', 'fixed']) ? $settings['referee_reward_type'] : 'percentage', 'referee_reward_amount' => floatval($settings['referee_reward_amount'] ?? 20), 'referee_reward_applies_to' => in_array($settings['referee_reward_applies_to'] ?? '', ['first_order', 'all_orders']) ? $settings['referee_reward_applies_to'] : 'first_order', ]; } /** * Render the admin settings page */ public function renderAdminPage(): void { ?>Upload your exported CSV files from Jane App to sync client and sales data.
| Total Referrals | = esc_html($total_referrals ?? 0) ?> |
|---|---|
| Pending | = esc_html($pending_referrals ?? 0) ?> |
| Treated | = esc_html($treated_referrals ?? 0) ?> |
' . __('This page is designated as the Referral Page.', 'jvbase') . '
'; echo '= home_url('/?ref=' . $referral_code) ?>
= esc_html($referral_code) ?>
People who used your code
Completed first treatment
Earned from referrals
Start sharing your referral code to earn rewards!
Share your code using the "Share" tab below.