-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathclass-reader-activation-emails.php
More file actions
290 lines (281 loc) · 12.2 KB
/
Copy pathclass-reader-activation-emails.php
File metadata and controls
290 lines (281 loc) · 12.2 KB
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
<?php
/**
* Reader Activation related emails.
*
* @package Newspack
*/
namespace Newspack;
use Newspack\WooCommerce_My_Account;
defined( 'ABSPATH' ) || exit;
/**
* Reader Activation related emails.
*/
class Reader_Activation_Emails {
const EMAIL_TYPES = [
'VERIFICATION' => 'reader-activation-verification',
'MAGIC_LINK' => 'reader-activation-magic-link',
'OTP_AUTH' => 'reader-activation-otp-authentication',
'RESET_PASSWORD' => 'reader-activation-reset-password',
'DELETE_ACCOUNT' => 'reader-activation-delete-account',
'CHANGE_EMAIL' => 'reader-activation-change-email',
'CHANGE_EMAIL_CANCEL' => 'reader-activation-change-email-cancel',
'NON_READER' => 'reader-activation-non-reader-user',
];
/**
* Initialize.
*
* @codeCoverageIgnore
*/
public static function init() {
add_filter( 'newspack_email_configs', [ __CLASS__, 'add_email_configs' ] );
// Only override WC password reset emails when Reader Activation is enabled,
// since the My Account handlers that process the reset link require it.
if ( Reader_Activation::is_enabled() ) {
add_filter( 'woocommerce_email_enabled_customer_reset_password', '__return_false' );
add_action( 'woocommerce_reset_password_notification', [ __CLASS__, 'send_reset_password_email' ], 10, 2 );
add_action( 'woocommerce_customer_reset_password', [ __CLASS__, 'redirect_non_reader' ] );
}
}
/**
* Register email type.
*
* @param array $configs Email types.
*
* @return array Email configs.
*/
public static function add_email_configs( $configs ) {
$available_placeholders = [
[
'label' => __( 'the site base address', 'newspack-plugin' ),
'template' => '*SITE_ADDRESS*',
],
[
'label' => __( 'the site contact info, including site name and address', 'newspack-plugin' ),
'template' => '*SITE_CONTACT*',
],
[
'label' => __( 'the site title', 'newspack-plugin' ),
'template' => '*SITE_TITLE*',
],
[
'label' => __( 'the site url', 'newspack-plugin' ),
'template' => '*SITE_URL*',
],
];
$configs[ self::EMAIL_TYPES['VERIFICATION'] ] = [
'name' => self::EMAIL_TYPES['VERIFICATION'],
'category' => 'reader-activation',
'label' => __( 'Verification', 'newspack-plugin' ),
'description' => __( "Email sent to the reader after they've registered.", 'newspack-plugin' ),
'template' => dirname( NEWSPACK_PLUGIN_FILE ) . '/includes/templates/reader-activation-emails/verification.php',
'editor_notice' => __( 'This email will be sent to a reader after they\'ve registered.', 'newspack-plugin' ),
'trigger_description' => __( 'Sent when a reader needs to verify their email address.', 'newspack-plugin' ),
'recipient' => 'reader',
'recommended' => true,
'chip' => 'auth-account',
'available_placeholders' => array_merge(
$available_placeholders,
[
[
'label' => __( 'the verification link', 'newspack-plugin' ),
'template' => '*VERIFICATION_URL*',
],
]
),
];
$configs[ self::EMAIL_TYPES['MAGIC_LINK'] ] = [
'name' => self::EMAIL_TYPES['MAGIC_LINK'],
'category' => 'reader-activation',
'label' => __( 'Login link', 'newspack-plugin' ),
'description' => __( 'Email sent to users with a login link generated by Admin user or when one-time password is disabled.', 'newspack-plugin' ),
'template' => dirname( NEWSPACK_PLUGIN_FILE ) . '/includes/templates/reader-activation-emails/magic-link.php',
'editor_notice' => __( 'This email will be sent to a reader when they request a login link.', 'newspack-plugin' ),
'trigger_description' => __( 'Sent when a reader requests a magic login link.', 'newspack-plugin' ),
'recipient' => 'reader',
'recommended' => true,
'chip' => 'auth-account',
'available_placeholders' => array_merge(
$available_placeholders,
[
[
'label' => __( 'the one-time password', 'newspack-plugin' ),
'template' => '*MAGIC_LINK_OTP*',
],
]
),
];
$configs[ self::EMAIL_TYPES['OTP_AUTH'] ] = [
'name' => self::EMAIL_TYPES['OTP_AUTH'],
'category' => 'reader-activation',
'label' => __( 'Login one-time password', 'newspack-plugin' ),
'description' => __( 'Email sent to users with a one-time password and login link.', 'newspack-plugin' ),
'template' => dirname( NEWSPACK_PLUGIN_FILE ) . '/includes/templates/reader-activation-emails/otp.php',
'editor_notice' => __( 'This email will be sent to a reader when they request a login link and a one-time password is available.', 'newspack-plugin' ),
'trigger_description' => __( 'Sent when a reader logs in with a one-time password.', 'newspack-plugin' ),
'recipient' => 'reader',
'recommended' => true,
'chip' => 'auth-account',
'available_placeholders' => array_merge(
$available_placeholders,
[
[
'label' => __( 'the one-time password', 'newspack-plugin' ),
'template' => '*MAGIC_LINK_OTP*',
],
[
'label' => __( 'the login link', 'newspack-plugin' ),
'template' => '*MAGIC_LINK_URL*',
],
[
'label' => __( 'the password reset link', 'newspack-plugin' ),
'template' => '*SET_PASSWORD_LINK*',
],
]
),
];
$configs[ self::EMAIL_TYPES['RESET_PASSWORD'] ] = [
'name' => self::EMAIL_TYPES['RESET_PASSWORD'],
'category' => 'reader-activation',
'label' => __( 'Set a New Password', 'newspack-plugin' ),
'description' => __( 'Email with password reset link.', 'newspack-plugin' ),
'template' => dirname( NEWSPACK_PLUGIN_FILE ) . '/includes/templates/reader-activation-emails/password-reset.php',
'editor_notice' => __( 'This email will be sent to a reader when they request a password creation or reset.', 'newspack-plugin' ),
'trigger_description' => __( 'Sent when a reader requests a password reset.', 'newspack-plugin' ),
'recipient' => 'reader',
'recommended' => true,
'chip' => 'auth-account',
'available_placeholders' => array_merge(
$available_placeholders,
[
[
'label' => __( 'the password reset link', 'newspack-plugin' ),
'template' => '*PASSWORD_RESET_LINK*',
],
]
),
];
$configs[ self::EMAIL_TYPES['DELETE_ACCOUNT'] ] = [
'name' => self::EMAIL_TYPES['DELETE_ACCOUNT'],
'category' => 'reader-activation',
'label' => __( 'Delete Account', 'newspack-plugin' ),
'description' => __( 'Email with account deletion link.', 'newspack-plugin' ),
'template' => dirname( NEWSPACK_PLUGIN_FILE ) . '/includes/templates/reader-activation-emails/delete-account.php',
'editor_notice' => __( 'This email will be sent to a reader when they request an account deletion.', 'newspack-plugin' ),
'trigger_description' => __( 'Sent when a reader requests to delete their account.', 'newspack-plugin' ),
'recipient' => 'reader',
'recommended' => false,
'chip' => 'auth-account',
'available_placeholders' => [
[
'label' => __( 'the account deletion link', 'newspack-plugin' ),
'template' => '*DELETION_LINK*',
],
],
];
if ( WooCommerce_My_Account::is_email_change_enabled() ) {
$configs[ self::EMAIL_TYPES['CHANGE_EMAIL'] ] = [
'name' => self::EMAIL_TYPES['CHANGE_EMAIL'],
'category' => 'reader-activation',
'label' => __( 'Change Email Confirmation', 'newspack-plugin' ),
'description' => __( 'Email sent to the reader to confirm or cancel a request to update their email address.', 'newspack-plugin' ),
'template' => dirname( NEWSPACK_PLUGIN_FILE ) . '/includes/templates/reader-activation-emails/change-email.php',
'editor_notice' => __( 'This email will be sent to a reader\'s new email address after they update their email address.', 'newspack-plugin' ),
'trigger_description' => __( 'Sent to the new address to confirm an email change.', 'newspack-plugin' ),
'recipient' => 'reader',
'recommended' => false,
'chip' => 'auth-account',
'available_placeholders' => array_merge(
$available_placeholders,
[
[
'label' => __( 'the verification link', 'newspack-plugin' ),
'template' => '*EMAIL_VERIFICATION_URL*',
],
[
'label' => __( 'the email change cancellation link', 'newspack-plugin' ),
'template' => '*EMAIL_CANCELLATION_URL*',
],
]
),
];
$configs[ self::EMAIL_TYPES['CHANGE_EMAIL_CANCEL'] ] = [
'name' => self::EMAIL_TYPES['CHANGE_EMAIL_CANCEL'],
'category' => 'reader-activation',
'label' => __( 'Change Email Notification', 'newspack-plugin' ),
'description' => __( 'Email sent to notify the reader of a request to update their email addresses.', 'newspack-plugin' ),
'template' => dirname( NEWSPACK_PLUGIN_FILE ) . '/includes/templates/reader-activation-emails/change-email-cancel.php',
'editor_notice' => __( 'This email will be sent to a reader\'s existing email address after they update their email address.', 'newspack-plugin' ),
'trigger_description' => __( 'Sent to the old address when a reader changes their email.', 'newspack-plugin' ),
'recipient' => 'reader',
'recommended' => false,
'chip' => 'auth-account',
'available_placeholders' => array_merge(
$available_placeholders,
[
[
'label' => __( 'the email change cancellation link', 'newspack-plugin' ),
'template' => '*EMAIL_CANCELLATION_URL*',
],
[
'label' => __( 'the new account email address pending confirmation', 'newspack-plugin' ),
'template' => '*PENDING_EMAIL_ADDRESS*',
],
]
),
];
}
$configs[ self::EMAIL_TYPES['NON_READER'] ] = [
'name' => self::EMAIL_TYPES['NON_READER'],
'category' => 'reader-activation',
'label' => __( 'Non-reader account', 'newspack-plugin' ),
'description' => __( 'Email reminder sent to non-reader accounts instead of reader account-related emails.', 'newspack-plugin' ),
'template' => dirname( NEWSPACK_PLUGIN_FILE ) . '/includes/templates/reader-activation-emails/non-reader.php',
'editor_notice' => __( 'This email will be sent to non-reader WP user accounts as a reminder to use standard WP login flows.', 'newspack-plugin' ),
'trigger_description' => __( 'Sent when a non-reader WordPress user tries to log in as a reader.', 'newspack-plugin' ),
'recipient' => 'reader',
'recommended' => false,
'chip' => 'auth-account',
'available_placeholders' => array_merge(
$available_placeholders,
[
[
'label' => __( 'the standard WP login page', 'newspack-pliugin' ),
'template' => '*WP_LOGIN_URL*',
],
]
),
];
return $configs;
}
/**
* Redirect non reader to default wp-login.php.
*
* @param \WP_User $user User object.
*/
public static function redirect_non_reader( $user ) {
if ( ! \Newspack\Reader_Activation::is_user_reader( $user ) ) {
wp_safe_redirect( wp_login_url() );
exit;
}
}
/**
* Send password reset email.
*
* @param string $user_login User login.
* @param string $key Password reset key.
*/
public static function send_reset_password_email( $user_login, $key ) {
$user = get_user_by( 'login', $user_login );
Emails::send_email(
self::EMAIL_TYPES['RESET_PASSWORD'],
$user->data->user_email,
[
[
'template' => '*PASSWORD_RESET_LINK*',
'value' => Emails::get_password_reset_url( $user, $key ),
],
]
);
}
}
Reader_Activation_Emails::init();