-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemail-verify.php
executable file
·350 lines (299 loc) · 9.55 KB
/
email-verify.php
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
<?php
/**
* Plugin Name: Email Verification on Signups
* Description: Send a verification email to newly registered users.
* Version: 1.1.7
* Author: Am!n
* Author URI: http://www.dornaweb.com
* License: GPL-2.0+
* Text Domain: dwverify
* Domain Path: /lang
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
require __DIR__ . '/vendor/autoload.php';
load_plugin_textdomain( 'dwverify', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
class DWEmailVerify{
/**
* Version
*/
const PLUGIN_VERSION = '1.1.7';
/**
* Value of all user meta rows w/ `verify-lock` key AFTER email has been verified
*/
const UNLOCKED = 'unlocked';
/**
* Construct
*/
public function __construct(){
$this->includes();
add_action( 'user_register', [ $this, 'user_register' ] );
add_filter( 'authenticate', [ $this, 'check_active_user' ], 100, 2 );
add_action( 'wp_enqueue_scripts', [ $this, 'assets' ] );
add_action( 'login_enqueue_scripts', [ $this, 'assets' ] );
}
/**
* Include required fiels
*/
public function includes(){
include_once $this->path() . 'settings.php';
include_once $this->path() . 'shortcode.php';
include_once $this->path() . 'user-mods.php';
}
/**
* Instanciating
*/
public static function instance(){
return new self();
}
/**
* Triggers when plugin gets activated
*/
public static function plugin_activated() {
self::instance()->create_plugin_pages();
self::instance()->set_default_settings();
}
/**
* Plugin assets
*/
public function assets(){
wp_register_script( 'dw-verify-js', $this->url() . 'assets/js/verify-email.js', ['jquery'], null, true );
wp_localize_script( 'dw-verify-js', 'dwverify', [
'ajaxurl' => admin_url('admin-ajax.php'),
'confirm_text' => esc_html__('Are you sure you want to re-send verification link?', 'dwverify')
]);
wp_enqueue_script( 'dw-verify-js' );
}
/**
* Create default pages
*/
public function create_plugin_pages() {
$pages = [
'authorize' => [
'title' => esc_html__( 'Authorize', 'dwverify' ),
'content' => '[dw-verify-email]',
'option_id' => 'dw_verify_authorize_page'
]
];
$pages_option = array();
foreach( $pages as $slug => $page ) {
$query = new WP_Query( 'pagename=' . $slug );
if ( ! $query->have_posts() ) {
// Add the page using the data from the array above
update_option( $page['option_id'],
wp_insert_post(
[
'post_content' => $page['content'],
'post_name' => $slug,
'post_title' => $page['title'],
'post_status' => 'publish',
'post_type' => 'page',
'ping_status' => 'closed',
'comment_status' => 'closed',
]
)
);
}
}
}
/**
* Set default settings
*/
public function set_default_settings(){
update_option('dw_verify_max_resend_allowed', 5);
}
/**
* Creates a hash when new user registers and stores the hash as a meta value
*
* @param int $user_id
*/
public function user_register( $user_id ){
$skip_verification = filter_input(INPUT_POST, 'skip_verification', FILTER_SANITIZE_SPECIAL_CHARS);
if( is_admin() && current_user_can( 'create_users') && ! empty( $skip_verification ) ){
return; // ignore adding verify lock
}
$this->send_verification_link( $user_id );
/**
* Default behavior is to redirect to the authorize page and exit. However, that prevents other plugins that hook into the
* registration process from being run. For example, a plugin that, after registration, bestows special privileges on
* the new user, would be short-circuited if execution exits here. Therefore, make it possible to override the redirect
* via this filter.
*/
$should_redirect = true;
$should_redirect = apply_filters( 'dw_verify_should_redirect_after_register', $should_redirect );
if( $should_redirect ) {
wp_redirect( add_query_arg( 'awaiting-verification', 'true', $this->authorize_page_url() ) );
exit;
} else {
return;
}
}
/**
* Lock user's account, send a verification email and ask them to verify their email address
* @param int $user_id
*/
public function send_verification_link( $user_id ){
$user = get_user_by('id', $user_id);
$token = $this->lock_user( $user_id );
$this->send_email( $token, $user );
}
/**
* Lock user
* @param int $user_id
*/
public function lock_user( $user_id ){
$user = get_user_by('id', $user_id);
$token = $this->generate_token();
$hash_method = 'sha256';
//update_user_meta( $user_id, 'verify-lock', $token );
update_user_meta( $user_id, 'verify-lock', hash( $hash_method, $token ) );
update_user_meta( $user_id, 'verify-lock-hash-method', $hash_method );
return $token;
}
/**
* Unlock user
* @param int $user_id
*/
public function unlock_user( $user_id ){
update_user_meta( $user_id, 'verify-lock', self::UNLOCKED );
}
/**
* Generate a cryptographically-secure, url-friendly verification token
*
* @param str $email
*/
public function generate_token(){
$bytes = random_bytes( 16 );
return bin2hex( $bytes );
}
/**
* Prevents users from loggin in, if they have not verified their email address
*
* @param WP_User $user
* @param str $username
*/
public function check_active_user( $user, $username ){
$needs_verification = $this->needs_validation( $user->ID );
if( $needs_verification !== false ) {
$username = esc_attr($username);
return new WP_Error( 'email_not_verified', sprintf(
__('You have not verified your email address, please check your email and click on verification link we sent you, <a href="#resend" onClick="%s">Re-send the link</a>', 'dwverify'),
"resend_verify_link('{$username}'); return false;"
));
}
return $user;
}
/**
* Send verification email
*
* @param WP_User $user
*/
public function send_email( $token, $user = false ){
if( ! $user || ! $user instanceof WP_User )
return;
$lock = $this->needs_validation( $user->ID );
// Ignore if user is not locked
if( $lock === false )
return;
$user_email = $user->data->user_email;
/**
* Add support for localized templates, just append your locale code to your template file name
* - eg. verify-fa_IR.php
* verify-en-GB.php
*/
$template = file_exists( $this->path() .'tpl/emails/verify-'. get_locale() .'.php' ) ? $this->path() .'tpl/emails/verify-'. get_locale() .'.php' : $this->path() .'tpl/emails/verify.php';
$template = apply_filters( 'dw_verify_email_template_path', $template );
$email = (new WP_Mail)
->to( $user_email )
->subject( esc_html__('Verify your email address', 'dwverify') )
->template( $template, apply_filters( 'dw_verify_email_template_args', [
'name' => $user->data->display_name,
'link' => add_query_arg( ['user_id' => $user->ID, 'verify_email' => $token], $this->authorize_page_url() ),
]) )
->send();
}
/**
* Authorize page url
* This is a regular wordpress page that contains the [dw-verify-email] shortcode
*/
public function authorize_page_url(){
return apply_filters( 'dw_verify_authorize_page_url', get_permalink( $this->authorize_page_id() ) );
}
/**
* Authorize page ID
* This is a regular wordpress page that contains the [dw-verify-email] shortcode
*/
public function authorize_page_id(){
return get_option('dw_verify_authorize_page');
}
/**
* Return false if user doesn't need validation, stored hash if it does
*/
public function needs_validation( $user_id ){
$needs_verification = false;
// If the verification metadata doesn't even exist, assume token hasn't been issued. That would indicate that
// the user registered at a time when this plugin was not installed/active. Don't require verification in that case.
if( metadata_exists( 'user', $user_id, "verify-lock" ) ) {
$lock = get_user_meta( $user_id, "verify-lock", true );
if( $lock !== self::UNLOCKED ) {
$needs_verification = $lock;
}
}
return $needs_verification;
}
/**
* Validate hash
*/
public function hash_valid( $user_token, $user_id ){
// user already verified
$stored_hash = $this->needs_validation( $user_id );
if( $stored_hash === false )
return;
// If the token stored for this user was hashed, hash the received token using the same method
// Retrieving the method like this means the plugin will be backwards-compatible with versions that didn't hash the token
$hash_method = get_user_meta( $user_id, 'verify-lock-hash-method', true );
if( $hash_method ) {
$user_token = hash( $hash_method, $user_token );
}
return hash_equals( $stored_hash, $user_token );
}
/**
* Verify user's email
*/
public function verify_if_valid( $user_hash, $user_id, $signon = false ){
if( ! $this->hash_valid( $user_hash, $user_id ) ) return;
$user = get_user_by('id', $user_id);
// Unlock user from logging in
$this->unlock_user( $user_id );
if( get_option('dw_verify_autologin') ) {
wp_clear_auth_cookie();
wp_set_current_user ( $user->ID );
wp_set_auth_cookie ( $user->ID );
}
return true;
}
/**
* Redirect after verification
*/
public function redirect_url(){
return ( $red_page = get_option('dw_verify_redirect_page') ) ?
apply_filters( 'dw_verify_redirect_url', get_permalink( $red_page ) ) :
apply_filters( 'dw_verify_redirect_url', home_url() );
}
/**
* Return the plugin's path
*/
public function path(){
return plugin_dir_path( __FILE__ );
}
/**
* Return the plugin's url
*/
public function url(){
return plugins_url( '', __FILE__ ) . '/';
}
}
new DWEmailVerify();
// hook plugin activated!
register_activation_hook( __FILE__, [ 'DWEmailVerify', 'plugin_activated' ] );