Jake Vanderwerf
2026-01-25 b38f03c0e7218762d90fa5092696b127f24f36db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?php
namespace JVBase\managers;
 
use WP_User;
use WP_Error;
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * Magic Link Authentication Manager
 *
 * NOTE: Login form integration is now handled by LoginManager.php
 * This class focuses solely on magic link generation and verification
 */
class MagicLinkManager
{
    protected Cache $cache;
    protected Cache $referral_cache;
 
    // Token settings
    protected int $token_expiry = 900; // 15 minutes in seconds
    protected int $rate_limit_window = 3600; // 1 hour
    protected int $max_attempts_per_hour = 5;
 
    // Link types
    const TYPE_LOGIN = 'login';
    const TYPE_SIGNUP = 'signup';
    const TYPE_REFERRAL = 'referral';
    const TYPE_RESET = 'reset';
 
    public function __construct()
    {
        $this->cache = Cache::for('magic_links', $this->token_expiry);
        $this->referral_cache = Cache::for('referral_magic_links', 14 * DAY_IN_SECONDS);
 
        // Hook into WordPress auth flow
        add_action('template_redirect', [$this, 'handleMagicLinkClick']);
        add_action('wp_login_failed', [$this, 'handleFailedLogin']);
        add_action('jvb_process_login_tokens', [$this, 'processRegistrationToken'], 10, 3);
    }
 
    /**
     * Generate and send a magic link
     *
     * @param string $email User's email address
     * @param string $type Type of magic link (login, signup, referral, reset)
     * @param array $context Additional context (referral_code, redirect_url, etc.)
     * @return true|WP_Error
     */
    public function sendMagicLink(string $email, string $type = self::TYPE_LOGIN, array $context = [])
    {
        // Validate email
        $email = sanitize_email($email);
        if (!is_email($email)) {
            return new WP_Error('invalid_email', 'Invalid email address');
        }
 
        // Check rate limiting
        $rate_check = $this->checkRateLimit($email);
        if (is_wp_error($rate_check)) {
            return $rate_check;
        }
 
        // Handle different link types
        switch ($type) {
            case self::TYPE_LOGIN:
                return $this->sendLoginLink($email, $context);
 
            case self::TYPE_SIGNUP:
                return $this->sendSignupLink($email, $context);
 
            case self::TYPE_REFERRAL:
                return $this->sendReferralLink($email, $context);
 
            case self::TYPE_RESET:
                return $this->sendResetLink($email, $context);
 
            default:
                return new WP_Error('invalid_type', 'Invalid magic link type');
        }
    }
 
    /**
     * Generate a secure token
     */
    protected function generateToken(string $email, string $type, array $data = []): string
    {
        $token = wp_generate_password(32, false);
 
        $token_data = array_merge([
            'email' => $email,
            'type' => $type,
            'created' => time()
        ], $data);
 
        // Use longer expiry for referral tokens
        if ($type === self::TYPE_REFERRAL) {
            $this->referral_cache->set($token, $token_data);
        } else {
            $this->cache->set($token, $token_data);
        }
 
        return $token;
    }
 
    /**
     * Verify a token
     */
    public function verifyToken(string $token, string $email): array|WP_Error
    {
        // Try regular cache first, then referral cache
        $token_data = $this->cache->get($token);
 
        if (!$token_data) {
            $token_data = $this->referral_cache->get($token);
        }
 
        if (!$token_data) {
            error_log('Token not found. Checking cache stats...');
            return new WP_Error('invalid_token', 'Invalid or expired token');
        }
 
        if ($token_data['email'] !== $email) {
            return new WP_Error('email_mismatch', 'Token does not match email');
        }
 
        // Delete token after verification (single use)
        // Check which cache it's in and delete from the correct one
        if ($token_data['type'] === 'referral') {
            $this->referral_cache->forget($token);
        } else {
            $this->cache->forget($token);
        }
 
        return $token_data;
    }
 
    /**
     * Check rate limiting for sending magic links
     */
    protected function checkRateLimit(string $email): bool|WP_Error
    {
        $cache_key = 'rate_limit_' . md5($email);
        $attempts = $this->cache->get($cache_key);
 
        if (!$attempts) {
            $attempts = ['count' => 0, 'timestamp' => time()];
        }
 
        // Reset counter if window has passed
        if (time() - $attempts['timestamp'] > $this->rate_limit_window) {
            $attempts = ['count' => 0, 'timestamp' => time()];
        }
 
        // Check if limit exceeded
        if ($attempts['count'] >= $this->max_attempts_per_hour) {
            return new WP_Error(
                'rate_limit_exceeded',
                'Too many magic link requests. Please try again in an hour.'
            );
        }
 
        // Increment counter
        $attempts['count']++;
        $this->cache->set($cache_key, $attempts, $this->rate_limit_window);
 
        return true;
    }
 
    /**
     * Send login magic link to existing user
     */
    protected function sendLoginLink(string $email, array $context): bool|WP_Error
    {
        $user = get_user_by('email', $email);
        if (!$user) {
            return new WP_Error('user_not_found', 'No account found with this email');
        }
 
        $token = $this->generateToken($email, self::TYPE_LOGIN, [
            'user_id' => $user->ID
        ]);
 
        $magic_url = add_query_arg([
            'magic_token' => $token,
            'email' => rawurlencode($email),
            'action' => 'magic_login'
        ], home_url('/'));
 
        if (!empty($context['redirect_to'])) {
            $magic_url = add_query_arg('redirect_to', urlencode($context['redirect_to']), $magic_url);
        }
 
        $subject = 'Sign in to ' . get_bloginfo('name');
        $message = $this->getLoginEmailTemplate($user->display_name, $magic_url);
 
        $sent = JVB()->email()->sendEmail($email, $subject, $message, 'Log in to '. get_bloginfo('name'));
 
        return $sent ? true : new WP_Error('email_failed', 'Failed to send magic link');
    }
 
    /**
     * Send signup magic link for new user registration
     */
    protected function sendSignupLink(string $email, array $context): bool|WP_Error
    {
        // Check if user already exists
        if (email_exists($email)) {
            return $this->sendLoginLink($email, $context);
        }
 
        $token_data = [
            'name' => $context['name'] ?? '',
            'role' => $context['role'] ?? 'subscriber',
            'meta' => $context['meta'] ?? []
        ];
 
        $token = $this->generateToken($email, self::TYPE_SIGNUP, $token_data);
 
        $magic_url = add_query_arg([
            'magic_token' => $token,
            'email' => rawurlencode($email),
            'action' => 'magic_signup'
        ], home_url('/'));
 
        $subject = 'Complete your ' . get_bloginfo('name') . ' registration';
        $message = $this->getSignupEmailTemplate($context['name'] ?? '', $magic_url);
 
        $sent = JVB()->email()->sendEmail($email, $subject, $message, 'Complete Registration');
 
        return $sent ? true : new WP_Error('email_failed', 'Failed to send signup link');
    }
 
    /**
     * Send referral signup link
     */
    protected function sendReferralLink(string $email, array $context): bool|WP_Error
    {
        if (empty($context['referral_code'])) {
            return new WP_Error('missing_referral', 'Referral code is required');
        }
 
        $token_data = [
            'referral_code' => $context['referral_code'],
            'name' => $context['name'] ?? '',
            'role' => $context['role'] ?? 'subscriber',
            'email' => $email
        ];
 
        $token = $this->generateToken($email, self::TYPE_REFERRAL, $token_data);
 
        $magic_url = add_query_arg([
            'magic_token' => $token,
            'email' => rawurlencode($email),
            'action' => 'magic_referral'
        ], home_url('/'));
 
        $referrer_name = $context['referrer_name'] ?? 'A friend';
        $reward_text = $context['reward_text'] ?? '';
 
        $subject = (array_key_exists('subject', $context) && $context['subject'] !== '') ? $context['subject'] : $referrer_name . ' invited you to join ' . get_bloginfo('name');
        $message = $this->getReferralEmailTemplate($context['name'] ?? '', $referrer_name, $magic_url, $reward_text, $context);
 
        $sent = JVB()->email()->sendEmail($email, $subject, $message, 'Accept Invitation');
 
        return $sent ? true : new WP_Error('email_failed', 'Failed to send referral link');
    }
 
    /**
     * Send password reset magic link
     */
    protected function sendResetLink(string $email, array $context): bool|WP_Error
    {
        $user = get_user_by('email', $email);
        if (!$user) {
            return new WP_Error('user_not_found', 'No account found with this email');
        }
 
        $token = $this->generateToken($email, self::TYPE_RESET, [
            'user_id' => $user->ID
        ]);
 
        $magic_url = add_query_arg([
            'magic_token' => $token,
            'email' => rawurlencode($email),
            'action' => 'magic_reset'
        ], home_url('/'));
 
        $subject = 'Reset your password';
        $message = $this->getResetEmailTemplate($user->display_name, $magic_url);
 
        $sent = JVB()->email()->sendEmail($email, $subject, $message, 'Reset Password');
 
        return $sent ? true : new WP_Error('email_failed', 'Failed to send reset link');
    }
 
    /**
     * Handle magic link click
     */
    public function handleMagicLinkClick(): void
    {
        if (!isset($_GET['action']) || !isset($_GET['magic_token']) || !isset($_GET['email'])) {
            return;
        }
 
        $action = sanitize_text_field($_GET['action']);
        $token = sanitize_text_field($_GET['magic_token']);
        $email = sanitize_email(rawurldecode($_GET['email']));
 
        if (!in_array($action, ['magic_login', 'magic_signup', 'magic_referral', 'magic_reset'])) {
            return;
        }
 
        $token_data = $this->verifyToken($token, $email);
 
        if (is_wp_error($token_data)) {
            $this->handleInvalidToken($token_data);
            return;
        }
 
        switch ($action) {
            case 'magic_login':
                $this->processLogin($token_data);
                break;
 
            case 'magic_signup':
                $this->processSignup($token_data);
                break;
 
            case 'magic_referral':
                $this->processReferralSignup($token_data);
                break;
 
            case 'magic_reset':
                $this->processPasswordReset($token_data);
                break;
        }
    }
 
    /**
     * Process login via magic link
     */
    protected function processLogin(array $token_data): void
    {
        $user = get_user_by('ID', $token_data['user_id']);
 
        if (!$user) {
            wp_die('Invalid user');
        }
 
        wp_clear_auth_cookie();
        wp_set_current_user($user->ID);
        wp_set_auth_cookie($user->ID, true);
 
        do_action('wp_login', $user->user_login, $user);
 
        $redirect = isset($_GET['redirect_to']) ? esc_url_raw($_GET['redirect_to']) : home_url('/dash');
 
        wp_safe_redirect($redirect);
        exit;
    }
 
    /**
     * Process signup via magic link
     */
    protected function processSignup(array $token_data): void
    {
        if (!array_key_exists('email', $token_data) || !array_key_exists('name', $token_data)) {
            JVB()->error()->log('[MagicLinkManager]Could not process Signup');
            return;
        }
        $user_id = wp_create_user(
            $token_data['email'],
            wp_generate_password(20, true, true),
            $token_data['email']
        );
 
        if (is_wp_error($user_id)) {
            wp_die('Failed to create account: ' . $user_id->get_error_message());
        }
 
        $user = get_user_by('ID', $user_id);
        $user->set_role($token_data['role']);
 
        if (!empty($token_data['name'])) {
            wp_update_user([
                'ID' => $user_id,
                'display_name' => $token_data['name'],
                'first_name' => $token_data['name']
            ]);
        }
 
        if (!empty($token_data['meta'])) {
            foreach ($token_data['meta'] as $key => $value) {
                update_user_meta($user_id, BASE . $key, $value);
            }
        }
 
        wp_set_current_user($user_id);
        wp_set_auth_cookie($user_id, true);
 
        do_action('user_register', $user_id);
        do_action('wp_login', $user->user_login, $user);
 
        wp_safe_redirect(home_url('/dash?welcome=1'));
        exit;
    }
 
    /**
     * Process referral signup via magic link
     */
    /**
     * Process referral signup via magic link
     */
    protected function processReferralSignup(array $token_data): void
    {
        if (!array_key_exists('email', $token_data) || !array_key_exists('name', $token_data)) {
            JVB()->error()->log('[MagicLinkManager]Could not process Referral Signup');
            return;
        }
 
        $email = sanitize_email($token_data['email']);
        if (email_exists($email)) {
            wp_die('Looks like you already have an account!');
        }
        $role = JVB()->referrals()->getRole();
        $pass = wp_generate_password(20, true, true);
        $name = sanitize_text_field($token_data['name']);
        $user_id = wp_insert_user([
            'user_login'    => $email,
            'user_email'    => $email,
            'user_pass'     => $pass,
            'display_name'  => $name,
            'role'          => $role
        ]);
        if (!is_wp_error($user_id)) {
            $response = JVB()->routes('login')->login($email, $pass, true);
            if ($response) {
                wp_safe_redirect(home_url('/dash?welcome=1&referral=1'));
                exit;
            }
        } else {
            JVB()->error()->log(
                '[MagicLinkManager]',
                $user_id->get_error_message(),
                $token_data
            );
        }
    }
 
    /**
     * Process password reset via magic link
     */
    protected function processPasswordReset(array $token_data): void
    {
        $user = get_user_by('ID', $token_data['user_id']);
 
        if (!$user) {
            wp_die('Invalid user');
        }
 
        // Log user in and redirect to password change page
        wp_set_current_user($user->ID);
        wp_set_auth_cookie($user->ID, true);
 
        wp_safe_redirect(admin_url('profile.php?password_reset=1'));
        exit;
    }
 
    /**
     * Handle invalid token
     */
    protected function handleInvalidToken(WP_Error $error): void
    {
        wp_die($error->get_error_message());
    }
 
    /**
     * Handle failed login - offer magic link option
     */
    public function handleFailedLogin(string $username): void
    {
        // Could add logic here to automatically offer magic link
        // after multiple failed attempts
    }
 
    /**
     * Optionally block standard password auth for magic-link-only users
     */
    public function blockStandardAuth($user, $username, $password)
    {
        if ($user instanceof WP_User) {
            $magic_only = get_user_meta($user->ID, BASE . 'magic_link_only', true);
            if ($magic_only) {
                return new WP_Error('magic_link_required', 'Please use the login link sent to your email');
            }
        }
 
        return $user;
    }
 
    // ========================================
    // EMAIL TEMPLATES
    // ========================================
 
    protected function getLoginEmailTemplate(string $name, string $magic_url): string
    {
        $content = JVB()->email()->h2('Hey ' . esc_html($name) . '!');
        $content .= '<p>Click the button below to sign in to your account instantly - no password needed!</p>';
        $content .= JVB()->email()->button($magic_url, 'Sign In Now');
        $content .= '<p>Or copy and paste this link into your browser:</p>';
        $content .= JVB()->email()->link($magic_url);
        $content .= JVB()->email()->divider();
        $content .= '<p>If you didn\'t request this, you can safely ignore this email. This link expires in 15 minutes.</p>';
        return $content;
    }
 
    protected function getSignupEmailTemplate(string $name, string $magic_url): string
    {
        $content = JVB()->email()->h2('Welcome' . ($name ? ', ' . esc_html($name) : '') . '!');
        $content .= '<p>Click the button below to complete your registration and access your account!</p>';
        $content .= JVB()->email()->button($magic_url, 'Complete Registration');
        $content .= '<p>Or copy and paste this link:</p>';
        $content .= JVB()->email()->link($magic_url);
        $content .= JVB()->email()->spacer(10);
        $content .= '<p><small>This link expires in 24 hours.</small></p>';
        return $content;
    }
 
    protected function getReferralEmailTemplate(string $name, string $referrer_name, string $magic_url, string $reward_text, array $context): string
    {
        $content = JVB()->email()->h2('Hey' . ($name ? ' ' . esc_html($name) : '') . '!');
        $content .= sprintf(
            '<p><strong>%s</strong> thinks you\'d love %s!</p>',
            esc_html($referrer_name),
            esc_html(get_bloginfo('name'))
        );
 
        if (!empty($context['message'])) {
            $content .= JVB()->email()->callout(nl2br(esc_html($context['message'])));
        }
 
        if ($reward_text) {
            $content .= JVB()->email()->alert(
                '<strong>Special Welcome Offer:</strong> ' . esc_html($reward_text),
                'success'
            );
        }
 
        $content .= JVB()->email()->button($magic_url, 'Join Now');
        $content .= '<p>Or copy and paste this link:</p>';
        $content .= JVB()->email()->link($magic_url);
        $content .= JVB()->email()->spacer(10);
        $content .= '<p><small>This invitation expires in 14 days.</small></p>';
 
        return $content;
    }
}