| | |
| | | <?php |
| | | namespace JVBase\managers; |
| | | |
| | | use JVBase\managers\MagicLinkManager; |
| | | use JVBase\integrations\Cloudflare; |
| | | use JVBase\meta\Form; |
| | | use JVBase\ui\CRUDSkeleton; |
| | | use JVBase\ui\Tabs; |
| | | use JVBase\utility\Features; |
| | | use JVBase\base\Site; |
| | | use WP_User; |
| | | use WP_Error; |
| | | |
| | |
| | | |
| | | $table->setKeys([ |
| | | ['key' => 'PRIMARY', 'value' => 'id'], |
| | | ['key' => 'UNIQUE', 'value' => 'to_user (`to_user`)'], |
| | | ['key' => 'UNIQUE', 'value' => '(`to_user`)'], |
| | | 'from_user (`from_user`)', |
| | | 'status (`status`)', |
| | | 'code (`referral_code`)', |
| | |
| | | ]); |
| | | |
| | | $table->setKeys([ |
| | | ['key' => 'PRIMARY', 'value' => 'id'], |
| | | ['key' => 'UNIQUE', 'value' => 'code (`code`)'], |
| | | ['key' => 'PRIMARY', 'value' => '(`id`)'], |
| | | ['key' => 'UNIQUE', 'value' => '(`code`)'], |
| | | 'user (`user_id`)', |
| | | ]); |
| | | |
| | |
| | | |
| | | $table->setKeys([ |
| | | ['key' => 'PRIMARY', 'value' => '(`id`)'], |
| | | ['key' => 'UNIQUE', 'value' => 'patient_guid (`patient_guid`)'], |
| | | ['key' => 'UNIQUE', 'value' => '(`patient_guid`)'], |
| | | 'user (`user_id`)', |
| | | 'email (`email`)', |
| | | ]); |
| | |
| | | $base = BASE; |
| | | $table->setConstraints([ |
| | | "CONSTRAINT `{$base}reward_referral` FOREIGN KEY (`referral_id`) |
| | | REFERENCES {$this->referrals->getFullTableName()} (`id`) ON DELETE CASCADE", |
| | | REFERENCES `{$this->referrals->getFullTableName()}` (`id`) ON DELETE CASCADE", |
| | | "CONSTRAINT `{$base}reward_user` FOREIGN KEY (`user_id`) |
| | | REFERENCES {$table->getUserTable()}` (`ID`) ON DELETE CASCADE" |
| | | REFERENCES `{$table->getUserTable()}` (`ID`) ON DELETE CASCADE" |
| | | ]); |
| | | $table->defineTable(); |
| | | $this->rewards = $table; |
| | |
| | | 'jvb-data-store', |
| | | ]; |
| | | |
| | | if (Features::hasIntegration('cloudflare') && JVB()->connect('cloudflare')->isSetUp()) { |
| | | if (Site::hasIntegration('cloudflare') && JVB()->connect('cloudflare')->isSetUp()) { |
| | | JVB()->connect('cloudflare')->enqueueTurnstileScripts(); |
| | | } |
| | | if (is_singular(BASE.'dash')) { |
| | |
| | | * @param string|null $custom_code Optional custom code |
| | | * @return string|WP_Error |
| | | */ |
| | | public function getUserReferralCode(int $user_id, ?string $custom_code = null):array|wp_error |
| | | public function getUserReferralCode(int $user_id, ?string $custom_code = null):string|false |
| | | { |
| | | $user = get_userdata($user_id); |
| | | if (!$user) { |
| | | return new WP_Error('invalid_user', 'User not found'); |
| | | return false; |
| | | } |
| | | |
| | | $existing = $this->codes->pluck('code', ['user_id' => $user_id],'created_at', 'DESC'); |
| | | if ($existing && !$custom_code) { |
| | | return $existing; |
| | | if (!empty($existing) && !$custom_code) { |
| | | return $existing[0]; |
| | | } |
| | | if ($custom_code && !in_array($custom_code, $existing)) { |
| | | 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'); |
| | | } |
| | | } else { |
| | | return $existing; |
| | | return $existing[0]; |
| | | } |
| | | } |
| | | |
| | | // Generate new code if custom provided or none exists |
| | |
| | | |
| | | $success = $this->createCode($user_id, $code); |
| | | if ($success) { |
| | | return $this->codes->pluck('code', ['user_id' => $user_id], 'created_at', 'DESC'); |
| | | return $this->codes->pluck('code', ['user_id' => $user_id], 'created_at', 'DESC')[0]; |
| | | } |
| | | |
| | | return $code; |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | |
| | | public function getLoggedInReferral(int $user_id): string |
| | | { |
| | | $referral_code = $this->getUserReferralCode($user_id); |
| | | if (is_wp_error($referral_code)) { |
| | | if (!$referral_code) { |
| | | return ''; |
| | | } |
| | | |
| | |
| | | |
| | | $code = $this->getUserReferralCode($user->ID); |
| | | $yourCode = ''; |
| | | if (!is_wp_error($code)) { |
| | | if ($code) { |
| | | $share_url = $this->getShareURL($code); |
| | | $yourCode = sprintf( |
| | | '<div class="callout"> |
| | |
| | | $referrer = get_user_by('ID', $user_id); |
| | | $referral_code = $this->getUserReferralCode($user_id); |
| | | |
| | | if (is_wp_error($referral_code)) { |
| | | if ($referral_code) { |
| | | return $referral_code; |
| | | } |
| | | |
| | |
| | | |
| | | // Regular users get their referral dashboard |
| | | $user_id = get_current_user_id(); |
| | | $referral_code = get_user_meta($user_id, BASE . 'referral_code', true); |
| | | |
| | | if (!$referral_code) { |
| | | $referral_code = $this->getUserReferralCode($user_id); |
| | | if (!$referral_code) { |
| | | $referral_code = $this->generateReferralCode($user_id); |
| | | } |
| | | |
| | | $referrals = $this->getUserReferrals($user_id, ['limit' => 20]); |
| | |
| | | public function getShareButtons(int $user_id):void |
| | | { |
| | | $referral_code = $this->getUserReferralCode($user_id); |
| | | if (is_wp_error($referral_code)) { |
| | | if (!$referral_code) { |
| | | return; |
| | | } |
| | | |