From 46d681c6b825d21b3f698d793c4e630c687d90ad Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Thu, 21 May 2026 21:41:53 +0000
Subject: [PATCH] =Major CustomBlocks.php overhaul, expanding block support and customization from the editor. theme.json should now be updated on new themes to set brand colours, etc. Also note: major change to .col vs .row alignment: simplifying it to .top .bottom vs the confusion of the differences for .col/.row .start and .a-start
---
inc/managers/ReferralManager.php | 220 +++++++++++++++++++++++++++++++-----------------------
1 files changed, 126 insertions(+), 94 deletions(-)
diff --git a/inc/managers/ReferralManager.php b/inc/managers/ReferralManager.php
index af9f0b2..c521c15 100644
--- a/inc/managers/ReferralManager.php
+++ b/inc/managers/ReferralManager.php
@@ -1,12 +1,10 @@
<?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;
@@ -45,21 +43,27 @@
'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'
- 'referral_role' => BASE.'client'
];
- protected string $role = BASE.'client';
+ 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';
@@ -134,7 +138,7 @@
$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`)',
@@ -165,8 +169,8 @@
]);
$table->setKeys([
- ['key' => 'PRIMARY', 'value' => 'id'],
- ['key' => 'UNIQUE', 'value' => 'code (`code`)'],
+ ['key' => 'PRIMARY', 'value' => '(`id`)'],
+ ['key' => 'UNIQUE', 'value' => '(`code`)'],
'user (`user_id`)',
]);
@@ -195,7 +199,7 @@
$table->setKeys([
['key' => 'PRIMARY', 'value' => '(`id`)'],
- ['key' => 'UNIQUE', 'value' => 'patient_guid (`patient_guid`)'],
+ ['key' => 'UNIQUE', 'value' => '(`patient_guid`)'],
'user (`user_id`)',
'email (`email`)',
]);
@@ -238,9 +242,9 @@
$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;
@@ -329,7 +333,7 @@
'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')) {
@@ -396,24 +400,24 @@
* @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');
+ return $this->codes->pluck('code', ['user_id' => $user_id], 'created_at', 'DESC')[0];
+ } else {
+ return $existing[0];
}
- } else {
- return $existing;
}
// Generate new code if custom provided or none exists
@@ -421,10 +425,10 @@
$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;
}
/**
@@ -1252,28 +1256,54 @@
$referrer_name = $referrer ? strtok($referrer->display_name, ' ') : '';
}
- $codeForm = '<div class="referral-reward-banner">
- '.jvbIcon('confetti').'
- <h4>Get ' . esc_html($reward_text) . '!</h4>
- ' . ($referrer_name ? '<p>' . esc_html($referrer_name) . ' invited you to join us</p>' : '') . '
- </div>
- <form id="referral-code-form">
- '.jvbFormStatus(). '
- <input type="hidden" name="user_select" value="' . esc_attr(get_option(BASE.'referral_role','client')) . '">
- ' .Form::render('referral_name', '', [
+ $header = sprintf(
+ '<header><h2>%sGet %s.</h2></header><h3>Have a code?</h3>%s<p>Enter your referral code to get started!</p>',
+ jvbIcon('confetti'),
+ esc_html($reward_text),
+ ($referrer_name ? '<p>' . esc_html($referrer_name) . ' invited you to join us</p>' : '')
+ );
+
+ $codeForm = sprintf(
+ '<form id="referral-code-form">
+ %s
+ <input type="hidden" name="user_select" value="%s">
+ %s%s%s%s
+ <div class="row">
+ <button type="button" class="button-secondary check-code-btn">
+ %s Verify Code
+ </button>
+ <button type="submit">
+ Get Started
+ </button>
+ </div>
+
+ <div class="code-status" hidden></div>
+
+ <p class="hint">
+ We\'ll send you a link to complete your registration.
+ </p>
+ </form>
+ <div class="success-content" hidden>
+ <h3>Check Your Email!</h3>
+ <p>We\'ve sent you a magic link to complete your registration. Click the link to activate your account and claim your reward!</p>
+ <p class="hint">Can\'t find it? Check your spam folder.</p>
+ </div>',
+ jvbFormStatus(),
+ esc_attr(get_option(BASE.'referral_role','client')),
+ Form::render('referral_name', '', [
'required' => true,
'type' => 'text',
'label' => 'Your Name',
'placeholder'=> 'Mister Meeseeks',
'autocomplete'=>'name'
- ]).
+ ]),
Form::render('referral_email', '', [
'required' => true,
'type' => 'email',
'label' => 'Your Email',
'placeholder'=> 'look@me.com',
'autocomplete'=> 'email'
- ]).
+ ]),
Form::render('referral_code', $prefill_code, [
'required' => true,
'type' => 'text',
@@ -1282,54 +1312,53 @@
'maxLength' => 20,
'autocomplete'=>'off',
'data-referrer' => $referrer_name
- ]).'
- '.$turnstile.'
- <button type="button" class="button-secondary check-code-btn">
- '.jvbIcon('check-circle', ['size' => 16]).' Verify Code
- </button>
- <div class="code-status" hidden></div>
- <button type="submit">
- Get Started
- </button>
+ ]),
+ $turnstile,
+ jvbIcon('check-circle')
+ );
- <p class="helper-text">
- We\'ll send you a link to complete your registration.
- </p>
- </form>
- <div class="success-content" hidden>
- <h3>Check Your Email!</h3>
- <p>We\'ve sent you a magic link to complete your registration. Click the link to activate your account and claim your reward!</p>
- <p class="hint">Can\'t find it? Check your spam folder.</p>
- </div>';
+ $loginHeader = sprintf(
+ '<header><h2>%sLogin</h2></header><p>Already have an account?<br>Log in to see your rewards!</p>',
+ jvbIcon('sign-in')
+ );
+ $loginForm = sprintf(
+ '<form id="login-form">%s%s%s
+ <button type="submit">%sLogin With Magic Link</button>
+ </form>
+ <div class="success-content" hidden>
+ <h3>Check Your Email!</h3>
+ <p>We\'ve sent you a magic link to log in - no password required! Click the link in your email to log in.</p>
+ <p class="hint">Can\'t find it? Check your spam folder.</p>
+ </div>',
+ jvbFormStatus(),
+ Form::render('login_email', null, [
+ 'required' => true,
+ 'type' => 'email',
+ 'label' => 'Your Email',
+ 'autocomplete'=>'email'
+ ]),
+ $turnstile,
+ jvbIcon('magic-wand')
+ );
- $loginForm = '<form id="login-form">
- '.jvbFormStatus().Form::render('login_email', null, [
- 'required' => true,
- 'type' => 'email',
- 'label' => 'Your Email',
- 'autocomplete'=>'email'
- ]).'
- '.$turnstile.'
- <button type="submit">Login With Magic Link</button>
-</form>
-<div class="success-content" hidden>
- <h3>Check Your Email!</h3>
- <p>We\'ve sent you a magic link to log in - no password required! Click the link in your email to log in.</p>
- <p class="hint">Can\'t find it? Check your spam folder.</p>
-</div>';
- $footer = '<div class="referral-footer">
- <a href="' . wp_login_url() . '" class="text-link">Prefer to use a password?</a>
- </div>';
+ $footer = sprintf(
+ '<p class="hint">
+ <a href="%s" class="text-link">Prefer to use a password?</a>
+ </p>',
+ wp_login_url()
+ );
$tabs = [
'enterCode' => [
'title' => 'Have a Code?',
'description' => [
'Enter your referral code to get started'
],
+ 'header' => $header,
'content' => $codeForm
],
'login' => [
+ 'header' => $loginHeader,
'title' => 'Login',
'description' => [
'Already have an account? Log in to see your rewards'
@@ -1395,13 +1424,14 @@
public function getLoggedInReferral(int $user_id): string
{
$referral_code = $this->getUserReferralCode($user_id);
- if (is_wp_error($referral_code)) {
+ if (!$referral_code) {
return '';
}
$share_url = $this->getShareURL($referral_code);
ob_start();
?>
+ <div class="wrap">
<header>
<h3>Share the ♡</h3>
<p>Invite friends. Earn rewards.</p>
@@ -1409,9 +1439,9 @@
<?php $this->getShareButtons($user_id); ?>
- <div class="copy-section">
+ <section class="copy">
<h4>Your Referral Link</h4>
- <div class="copy-group row btw nowrap">
+ <div class="copy-group row x-btw nowrap">
<code id="referral-link" class="copy-target"><?= esc_url($share_url) ?></code>
<button type="button" class="copy-btn" data-target="referral-link" title="Copy referral link">
<?= jvbIcon('copy'); ?>
@@ -1422,7 +1452,7 @@
<h4>Your Code</h4>
- <div class="copy-group row btw nowrap">
+ <div class="copy-group row x-btw nowrap">
<code id="referral-code" class="copy-target"><?= esc_html($referral_code) ?></code>
<button type="button" class="copy-btn" data-target="referral-code" title="Copy referral code">
<?= jvbIcon('copy'); ?>
@@ -1431,39 +1461,39 @@
</div>
<p class="hint">Manually copy and paste the code</p>
- </div>
+ </section>
- <div class="recent-referrals-section">
+ <section class="recent-referrals">
<h4>Recent Referrals</h4>
<div class="recent-referrals-list" data-user-id="<?= $user_id ?>">
<div class="loading">Loading...</div>
</div>
- </div>
+ </section>
- <div class="stats-summary">
- <div class="stat-row">
+ <section class="stats-summary">
+ <div class="row x-btw">
<span class="stat-label">Total Referrals</span>
<span class="stat-value" data-stat="total">-</span>
</div>
- <div class="stat-row">
+ <div class="row x-btw">
<span class="stat-label">Successful</span>
<span class="stat-value" data-stat="treated">-</span>
</div>
- <div class="stat-row">
+ <div class="row x-btw">
<span class="stat-label">Pending</span>
<span class="stat-value" data-stat="pending">-</span>
</div>
- <div class="stat-row highlight">
+ <div class="row x-btw highlight">
<span class="stat-label">Available Rewards</span>
<span class="stat-value" data-stat="rewards">$0.00</span>
</div>
- </div>
+ </section>
<a href="<?= get_home_url(null, '/dash/referrals')?>" class="view-dashboard-btn">
Dashboard <?= jvbIcon('arrow-right', ['size' => 16]); ?>
</a>
<p class="hint">Bulk-invite your friends via email - the link will pre-fill their name, email, and code!</p>
-
+ </div>
<?php
return ob_get_clean();
}
@@ -1494,7 +1524,7 @@
$code = $this->getUserReferralCode($user->ID);
$yourCode = '';
- if (!is_wp_error($code)) {
+ if ($code) {
$share_url = $this->getShareURL($code);
$yourCode = sprintf(
'<div class="callout">
@@ -1556,7 +1586,7 @@
$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;
}
@@ -2621,10 +2651,11 @@
// Regular users get their referral dashboard
$user_id = get_current_user_id();
- $referral_code = get_user_meta($user_id, BASE . 'referral_code', true);
+ $referral_code = $this->getUserReferralCode($user_id);
if (!$referral_code) {
- $referral_code = $this->getUserReferralCode($user_id);
+ $user = get_userdata($user_id);
+ $referral_code = $this->generateReferralCode($user);
}
$referrals = $this->getUserReferrals($user_id, ['limit' => 20]);
@@ -2658,9 +2689,10 @@
<?php $this->getShareButtons($user_id); ?>
<!-- Referral Code Card -->
- <div class="card">
+ <details open>
+ <summary>Your Code</summary>
<h3>Share Link</h3>
- <div class="row btw nowrap">
+ <div class="row x-btw nowrap">
<code id="referral-link" class="copy-target"><?= home_url('/?ref=' . $referral_code) ?></code>
<button type="button" class="copy-btn" data-target="referral-link" title="Copy referral link">
<?= jvbIcon('copy'); ?>
@@ -2668,14 +2700,14 @@
</button>
</div>
<h3>Share Code</h3>
- <div class="row btw nowrap">
+ <div class="row x-btw nowrap">
<code id="referral-code" class="copy-target"><?= esc_html($referral_code) ?></code>
<button type="button" class="copy-btn" data-target="referral-code" title="Copy referral code">
<?= jvbIcon('copy'); ?>
<?= jvbIcon('check-circle'); ?>
</button>
</div>
- </div>
+ </details>
<form class="invite">
<h2>Invite your Friends</h2>
<p>Or, if you prefer, enter your friends name(s) and email(s), and we'll send off some emails.</p>
@@ -2839,7 +2871,7 @@
update_option(BASE . 'referral_page_id', $page_id);
// Save client import role
- $import_role = sanitize_text_field($post_data[BASE . 'referral_role'] ?? JVB_USER);
+ $import_role = sanitize_text_field($post_data[BASE . 'referral_role'] ?? Site::getDefaultReferralRole());
update_option(BASE . 'referral_role', $import_role);
// Save reward settings
@@ -2892,7 +2924,7 @@
public function getShareButtons(int $user_id):void
{
$referral_code = $this->getUserReferralCode($user_id);
- if (is_wp_error($referral_code)) {
+ if (!$referral_code) {
return;
}
@@ -2908,7 +2940,7 @@
?>
<nav class="share">
<h4>Quick Share</h4>
- <ul class="share-buttons-grid">
+ <ul>
<a href="mailto:?subject=<?php echo urlencode('Check out ' . get_bloginfo('name')); ?>&body=<?php echo urlencode($share_message . ' ' . $share_url); ?>"
class="button" title="Email">
<?php echo jvbIcon('envelope'); ?>
--
Gitblit v1.10.0