Jake Vanderwerf
2025-11-25 2a2303d1dccc120dd7aa5f6b6ade0f89e0064850
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
<?php
namespace JVBase\managers;
 
use WP_User;
 
/**
 * Edmonton Ink Email Customization
 *
 * This file customizes all default WordPress emails to match the Edmonton Ink brand aesthetic.
 * - Sets emails to HTML format
 * - Applies consistent branding (logo, colors, typography)
 * - Customizes content for each email type
 *
 */
 
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
 
/**
 * Hooks into wp_mail to ensure a consistent look in our automatic emails
 */
 
class EmailManager
{
 
   private array $colours = JVB_EMAIL['colours'];
   private string $title = JVB_EMAIL['content']['title'];
   private string $prefix = JVB_EMAIL['content']['subjectPrefix'];
   private string $signature = JVB_EMAIL['content']['signature'];
   private string $footer;
 
    // Site info
    private string $site_name;
    private string $site_url;
 
    /**
     * Constructor - sets up all filters
     */
    public function __construct()
    {
        $this->site_name = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
        $this->site_url = get_site_url();
        $this->footer = (is_array(JVB_EMAIL['content']['footer'])) ? implode('', JVB_EMAIL['content']['footer']) : JVB_EMAIL['content']['footer'];
 
        add_filter('wp_mail_content_type', [$this, 'setHtmlContentType']);
        // User registration emails
        add_filter('wp_new_user_notification_email', [$this, 'customizeNewUserEmail'], 10, 3);
        add_filter('wp_new_user_notification_email_admin', [$this, 'customizeNewUserEmailAdmin'], 10, 3);
 
        // Password reset emails
        add_filter('retrieve_password_message', [$this, 'customizePasswordResetEmail'], 10, 4);
        add_filter('retrieve_password_title', [$this, 'customizePasswordResetTitle'], 10, 3);
 
        // User email change emails
        add_filter('email_change_email', [$this, 'customizeEmailChangeEmail'], 10, 3);
        add_filter('new_user_email_content', [$this, 'customizeNewUserEmailContent'], 10, 2);
 
        // Password change notification
        add_filter('password_change_email', [$this, 'customizePasswordChangeEmail'], 10, 3);
 
        // User request/export data emails
        add_filter('user_request_action_email_content', [$this, 'customizeUserRequestEmail'], 10, 2);
        add_filter('wp_privacy_personal_data_email_content', [$this, 'customizePersonalDataEmail'], 10, 3);
    }
 
    /**
     * Helper to set content type to HTML
     * @return string
     */
    public function setHtmlContentType():string
    {
        return 'text/html';
    }
 
    /**
     * Send a styled email using the common template
     *
     * @param string $to Recipient email address
     * @param string $subject Email subject line
     * @param string $message Email body content (can contain HTML)
     * @param string $header Optional header text for the template
     * @return bool Whether the email was sent successfully
     */
    public function sendEmail(string $to, string $subject, string $message, string $header = ''):bool
    {
        // Make sure the content type is set to HTML
        add_filter('wp_mail_content_type', [$this, 'setHtmlContentType']);
 
        // Format the message with our template
        $formatted_message = $this->getEmailTemplate($message, $header);
 
        // Send the email
        $result = wp_mail($to, $subject, $formatted_message);
 
        // Reset content type filter to avoid affecting other emails
        remove_filter('wp_mail_content_type', [$this, 'setHtmlContentType']);
 
        return $result;
    }
    /**
     * Common email wrapper template
     * @param string $content
     * @param string $header
     *
     * @return string
     */
    private function getEmailTemplate(string $content, string $header = ''):string
    {
        $logo = get_custom_logo();
 
        // Default header if none provided
        if (empty($header)) {
            $header = $this->title;
        }
 
        return '<!DOCTYPE html>
        <html>
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>' . $this->title . ' | ' . $this->site_name .'</title>
            <style>
                body, table, td, p, a, li, blockquote {
                    line-height: 1.5;
                }
 
                body {
                    background-color: ' . $this->colours['light'] . ';
                    margin: 0;
                    padding: 0;
                    color: ' . $this->colours['dark'] . ';
                }
 
                .email-container {
                    max-width: 600px;
                    margin: 0 auto;
                    padding: 20px;
                }
 
                .header img {
                    max-width: 120px;
                    height: auto;
                }
 
                .header {
                    background-color: ' . $this->colours['dark'] . ';
                    padding: 5px;
                    text-align: center;
                    color: ' . $this->colours['light'] . ';
                    font-size: 24px;
                    font-weight: 900;
                    letter-spacing: 1.5px;
                    text-transform: uppercase;
                    border-top-left-radius: 5px;
                    border-top-right-radius: 5px;
                }
                .header p,
                .header a {
                    display: inline-block;
                    vertical-align: middle;
                }
                .header p {
                    margin: 0;
                    margin-left: auto;
                }
 
                .content {
                    background-color: ' . $this->colours['light-50'] . ';
                    padding: 30px;
                    border-bottom-left-radius: 5px;
                    border-bottom-right-radius: 5px;
                }
 
                .button {
                    display: inline-block;
                    padding: 12px 24px;
                    background-color: ' . $this->colours['action-0'] . ';
                    color: ' . $this->colours['action-contrast'] . ' !important;
                    text-decoration: none;
                    border-radius: 3px;
                    font-weight: bold;
                    margin: 15px 0;
                    letter-spacing: 1px;
                    text-transform: uppercase;
                }
 
                .text-link {
                    color: ' . $this->colours['action-0'] . ';
                    text-decoration: underline;
                }
 
                .footer {
                    text-align: center;
                    margin-top: 20px;
                    font-size: 12px;
                    backgorund-color: ' . $this->colours['light-200'] . ';
                    color: ' . $this->colours['dark-200'] . ';
                    padding: 10px;
                    line-height: 1.6;
                }
 
                .divider {
                    border-top: 1px solid ' . $this->colours['dark-200'] . ';
                    margin: 25px 0;
                }
 
                @media screen and (max-width: 600px) {
                    .email-container {
                        width: 100% !important;
                        padding: 10px !important;
                    }
 
                    .content, .header {
                        padding: 15px !important;
                    }
                }
 
                @media screen and (max-width: 600px) {
    /* Table adjustments for content rows */
    table, tbody, tr, td {
        display: block !important;
        width: 100% !important;
        clear: both !important;
    }
 
    /* Make images responsive */
    img {
        max-width: 100% !important;
        height: auto !important;
    }
 
    /* Increase tap target sizes */
    .button {
        display: block !important;
        text-align: center !important;
        padding: 16px 10px !important;
        margin: 15px auto !important;
        width: 80% !important;
    }
 
    /* Typography adjustments */
    h1 {
        font-size: 24px !important;
    }
    h2 {
        font-size: 20px !important;
    }
    h3 {
        font-size: 18px !important;
    }
 
    /* Content spacing */
    .content {
        padding: 15px 10px !important;
    }
 
    /* Stack list items better */
    li {
        margin-bottom: 10px !important;
    }
 
    /* Adjust content cells for better stacking */
    td {
        padding: 10px 0 !important;
        text-align: center !important;
    }
 
    /* Header adjustments */
    .header img {
        max-width: 100px !important;
    }
 
    .header p {
        font-size: 18px !important;
        margin: 5px 0 !important;
        display: block !important; /* Force stacking */
    }
}
 
/* Dark mode support for clients that support it */
@media (prefers-color-scheme: dark) {
    .body {
        background-color: ' . $this->colours['dark'] . ';
        color: ' . $this->colours['light'] . ';
    }
 
    .header {
        background-color: ' . $this->colours['light'] . ';
        color: ' . $this->colours['dark'] . ';
    }
 
    .content {
        background-color: ' . $this->colours['dark-50'] . ';
    }
 
    .footer {
        background-color: ' . $this->colours['dark-200'] . ';
        color: ' . $this->colours['light-200'] . ';
    }
}
            </style>
        </head>
        <body>
            <div class="email-container">
 
                <div class="header">
                    <a href="' . $this->site_url . '">
                        ' . $logo . '
                    </a>
                    <p>' . $header . '</p>
                </div>
                <div class="content">
                    ' . $content . '
                </div>
                <div class="footer">
                    ' . $this->footer . '
                </div>
            </div>
        </body>
        </html>';
    }
 
    /**
     * New user registration email to user
     */
    public function customizeNewUserEmail($wp_new_user_notification_email, $user, $blogname)
    {
        $message = sprintf(
            '<p>Hi %s!</p>
                    <p>Thanks for signing up for an account on %s.</p>
                    <p><b>Your username:</b> %s</p>
                    <p><b>Your password:</b> <i>Your chosen password.</i></p>',
            $user->display_name,
            get_bloginfo('name'),
            $user->user_login,
        );
        $message = apply_filters('jvbNewUserEmail', $message, $user);
 
        $message .= $this->signature;
        $wp_new_user_notification_email['message'] = $this->getEmailTemplate($message, JVB_EMAIL['types']['newUser']['subject']?:'New User');
 
        // Change the subject line
        $prefix = JVB_EMAIL['types']['newUser']['showPrefix']??true;
        $prefix = ($prefix) ? $this->prefix : '';
        $wp_new_user_notification_email['subject'] = $prefix.JVB_EMAIL['types']['newUser']['subject']?:'New User';
 
        return $wp_new_user_notification_email;
    }
 
    /**
     * New user registration email to admin
     * @param array $emailData
     * @param WP_User $user
     * @param string $blogname
     *
     * @return array
     */
    public function customizeNewUserEmailAdmin(array $emailData, WP_User $user, string $blogname):array
    {
        $message = '<p>A new user has registered on <strong>' . $this->site_name . '</strong>:</p>';
        $message .= '<p><strong>Username:</strong> ' . $user->user_login . '</p>';
        $message .= '<p><strong>Email:</strong> ' . $user->user_email . '</p>';
        $message = apply_filters('jvbNewUserAdminEmail', $message, $user);
        $message .= $this->signature;
        $emailData['message'] = $this->getEmailTemplate($message, 'New User Registration');
        $emailData['subject'] = $this->prefix .'New ' . str_replace(BASE, '', array_values($user->roles)[0]).': '.$user->display_name;
 
        return $emailData;
    }
 
    /**
     * Password reset email
     * @param string $message
     * @param string $key
     * @param string $user_login
     * @param WP_User $user_data
     *
     * @return string
     */
    public function customizePasswordResetEmail(string $message, string $key, string $user_login, WP_User $user):string
    {
        $reset_url = network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login');
        $content = sprintf(
            '<p>Hi %s!</p>
            <p>We received a request to reset the password for an account associated with this email:</p>
            <p><strong>Username:</strong> %s</p>
            <p>If you didn\'t make this request, you can safely ignore this email and nothing will happen to your account.</p>
            <p>To reset your password, click the button below:</p>
            %s
            <p>Or copy and paste this link into your browser:</p>
            %s
            <div class="divider"></div>
            <p>This password reset link is only valid for 24 hours.</p>',
            $user->display_name,
            $user_login,
            jvbMailButton($reset_url,'Reset Password'),
            jvbEmailLink($reset_url)
        );
        $content = apply_filters('jvbPasswordResetEmail', $content, $user_login, $user, $reset_url);
        $content .= $this->signature;
        return $this->getEmailTemplate($content, 'Password Reset');
    }
 
    /**
     * Customize the password reset email title
     * @param string $title
     * @param string $user_login
     * @param WP_User $user_data
     * @return string
     */
    public function customizePasswordResetTitle(string $title, string $user_login, WP_User $user_data):string
    {
        $prefix = JVB_EMAIL['types']['resetPass']['showPrefix']??true;
        $prefix = ($prefix) ? $this->prefix : '';
        return $prefix.JVB_EMAIL['types']['resetPass']['subject']?:'Password Reset';
 
    }
 
    /**
     * Email change notification to admin
     * @param array $email_change_email
     * @param array $oldUser
     * @param array $newUser
     *
     * @return array
     */
    public function customizeEmailChangeEmail(array $email_change_email, array $oldUser, array $newUser):array
    {
        $content = sprintf(
            '<p>Hi %s,</p>
        <p>Ideally you already know this: someone asked to change the email for your account.</p>
        <p><strong>Old Email:</strong> %s</p>
        <p><strong>New Email:</strong> %s</p>
        <div class="divider"></div>
        <p>If this is news to you, or you did not request this - please contact us immediately. You can <a href="sms:+18258239916">text us</a> or reply to this email."></a></p>
        %s',
            $newUser['first_name'],
            $oldUser['user_email'],
            $newUser['user_email'],
            jvbMailButton(wp_login_url(), 'Log In To Your Account')
        );
        $content = apply_filters('jvbEmailChangeRequestEmail', $content, $oldUser, $newUser);
        $content .= $this->signature;
        $email_change_email['message'] = $this->getEmailTemplate($content, 'Email Address Changed');
        $prefix = JVB_EMAIL['types']['emailChange']['showPrefix']??true;
        $prefix = ($prefix) ? $this->prefix : '';
        $email_change_email['subject'] = $prefix.JVB_EMAIL['types']['emailChange']['subject']?:'Email Address Changed';
 
 
        return $email_change_email;
    }
 
    /**
     * New email address confirmation
     * @param string $email_text
     * @param array $user
     *
     * @return string
     */
    public function customizeNewUserEmailContent(string $email_text, array $new_user_email):string
    {
        $confirm_url = esc_url( add_query_arg('newuseremail', $new_user_email['hash'], self_admin_url('profile.php')));
        $content = sprintf(
            '<p>Hey,</p>
            <p>There was a request to change the email address associated with your account to this one.</p>
            <p>This is just a friendly email to ensure you would like this change.</p>
            <p>You can confirm this change by clicking the button below:</p>
            %s
            <p>Or copy and paste this link into your browser:</p>
            %s',
            jvbMailButton($confirm_url, 'Confirm this Email'),
            jvbEmailLink($confirm_url)
        );
 
        $content = apply_filters('jvbEmailChangedEmail', $content, $confirm_url);
 
        $content .= '<div class="divider"></div>';
        $content .= '<p>If you did not request this change, you can safely ignore this email and nothing will change.</p>';
        $content .= $this->signature;
 
        return $this->getEmailTemplate($content, 'Confirm Email Change');
    }
 
        /**
         * Password change notification
         * @param array $pass_change_email
         * @param array $oldUser
         * @param array $newUser
         *
         * @return array
     */
    public function customizePasswordChangeEmail(array $pass_change_email, array $oldUser, array $newUser):array
    {
        $content = sprintf(
            '<p>Hi %s,</p>
            <p>This is a confirmation email to let you know your password has successfully been changed.</p>
            <p>If you\'re not expecting this email, and did not change your password - please <strong>contact us immediately</strong></p>
            <p>You can <a href="sms:+18259257398">text us</a>, or reply to this email.</p>
            %s',
            $oldUser['first_name'],
            jvbMailButton(wp_login_url(), 'Log In to Your Account')
        );
        $content = apply_filters('jvbPasswordChangeEmail', $content, $oldUser, $newUser);
        $content .= $this->signature;
 
        $pass_change_email['message'] = $this->getEmailTemplate($content, 'Password Changed');
        $prefix = JVB_EMAIL['types']['passwordChange']['showPrefix']??true;
        $prefix = ($prefix) ? $this->prefix : '';
        $pass_change_email['subject'] = $prefix.JVB_EMAIL['types']['passwordChange']['subject']?:'Password Changed';
 
        return $pass_change_email;
    }
 
    /**
     * User data request confirmation email
     * @param string $content
     * @param array $request_data
     *
     * @return string
     */
    public function customizeUserRequestEmail(string $content, array $email_data):string
    {
 
        $confirm_url = $email_data['confirm_url'];
        $request_type = $email_data['action_name'];
 
        switch ($request_type) {
            case 'export_personal_data':
                $request_name = 'Export Personal Data';
                break;
            case 'remove_personal_data':
                $request_name = 'Erase Personal Data';
                break;
            default:
                $request_name = 'Data Request';
        }
 
 
        $message = sprintf(
            '<p>Hi,</p>
            <p>You\'re receiving this email because a request has been made to <strong>%s</strong></p>
            <p>If you\'re the one who made this request, you can confirm it by clicking the button below:</p>
            %s
            <p>Or copy and paste this link into your browser:</p>
            %s',
            $request_name,
            jvbMailButton($confirm_url, 'Confirm'),
            jvbEmailLink($confirm_url)
        );
        $message = apply_filters('jvbPersonalDataExport', $message, $request_type, $confirm_url, $email_data);
 
        $message .= '<div class="divider"></div>';
        $message .= '<p>If you did not make this request, you can safely ignore this email.</p>';
        $message .= $this->signature;
 
        return $this->getEmailTemplate($message, 'Action Confirmation');
    }
 
    /**
     * Personal data export email
     * @param string $content
     * @param int $request_id
     * @param array $email_data
     *
     * @return string
     */
    public function customizePersonalDataEmail(string $content, int $request_id, array $email_data):string
    {
        $download_url = $email_data['export_file_url'];
        $expiresAt = $email_data['expiration_date'];
 
        $message = sprintf(
            '<p>Hi,</p>
            <p>You\'re receiving this email because you requested an export of your personal data.</p>
            <p>You can download your personal data by clicking the button below:</p>
            %s
            <p>Or you can copy and paste this link into your browser:</p>
            %s
            <div class="divider"></div>
            <p><strong>Important:</strong> For privacy and security, this link will expire at %s.</p>',
            jvbMailButton($download_url, 'Download Your Data'),
            jvbEmailLink($download_url),
            $expiresAt
        );
        $message = apply_filters('jvbPersonalDataExported', $message, $download_url, $expiresAt, $email_data);
        $message .= $this->signature;
 
        return $this->getEmailTemplate($message, 'Your Personal Data Export');
    }
}
 
new EmailManager();