um-actions-login.php
15.6 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
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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Error processing hook for login.
*
* @param $submitted_data
*/
function um_submit_form_errors_hook_login( $submitted_data ) {
$user_password = $submitted_data['user_password'];
if ( isset( $submitted_data['username'] ) && $submitted_data['username'] == '' ) {
UM()->form()->add_error( 'username', __( 'Please enter your username or email', 'ultimate-member' ) );
}
if ( isset( $submitted_data['user_login'] ) && $submitted_data['user_login'] == '' ) {
UM()->form()->add_error( 'user_login', __( 'Please enter your username', 'ultimate-member' ) );
}
if ( isset( $submitted_data['user_email'] ) && $submitted_data['user_email'] == '' ) {
UM()->form()->add_error( 'user_email', __( 'Please enter your email', 'ultimate-member' ) );
}
if ( isset( $submitted_data['username'] ) ) {
$authenticate = $submitted_data['username'];
$field = 'username';
if ( is_email( $submitted_data['username'] ) ) {
$data = get_user_by('email', $submitted_data['username'] );
$user_name = isset( $data->user_login ) ? $data->user_login : null;
} else {
$user_name = $submitted_data['username'];
}
} elseif ( isset( $submitted_data['user_email'] ) ) {
$authenticate = $submitted_data['user_email'];
$field = 'user_email';
$data = get_user_by('email', $submitted_data['user_email'] );
$user_name = isset( $data->user_login ) ? $data->user_login : null;
} else {
$field = 'user_login';
$user_name = $submitted_data['user_login'];
$authenticate = $submitted_data['user_login'];
}
if ( $submitted_data['user_password'] == '' ) {
UM()->form()->add_error( 'user_password', __( 'Please enter your password', 'ultimate-member' ) );
}
$user = get_user_by( 'login', $user_name );
if ( $user && wp_check_password( $submitted_data['user_password'], $user->data->user_pass, $user->ID ) ) {
UM()->login()->auth_id = username_exists( $user_name );
} else {
UM()->form()->add_error( 'user_password', __( 'Password is incorrect. Please try again.', 'ultimate-member' ) );
}
// Integration with 3rd-party login handlers e.g. 3rd-party reCAPTCHA etc.
$third_party_codes = apply_filters( 'um_custom_authenticate_error_codes', array() );
// @since 4.18 replacement for 'wp_login_failed' action hook
// see WP function wp_authenticate()
$ignore_codes = array( 'empty_username', 'empty_password' );
$user = apply_filters( 'authenticate', null, $authenticate, $submitted_data['user_password'] );
if ( is_wp_error( $user ) && ! in_array( $user->get_error_code(), $ignore_codes ) ) {
if ( ! empty( $third_party_codes ) && in_array( $user->get_error_code(), $third_party_codes ) ) {
UM()->form()->add_error( $user->get_error_code(), $user->get_error_message() );
} else {
UM()->form()->add_error( 'user_password', __( 'Password is incorrect. Please try again.', 'ultimate-member' ) );
}
}
$user = apply_filters( 'wp_authenticate_user', $user, $submitted_data['user_password'] );
if ( is_wp_error( $user ) && ! in_array( $user->get_error_code(), $ignore_codes ) ) {
if ( ! empty( $third_party_codes ) && in_array( $user->get_error_code(), $third_party_codes ) ) {
UM()->form()->add_error( $user->get_error_code(), $user->get_error_message() );
} else {
UM()->form()->add_error( 'user_password', __( 'Password is incorrect. Please try again.', 'ultimate-member' ) );
}
}
// if there is an error notify wp
if ( UM()->form()->has_error( $field ) || UM()->form()->has_error( $user_password ) || UM()->form()->count_errors() > 0 ) {
do_action( 'wp_login_failed', $user_name, UM()->form()->get_wp_error() );
}
}
add_action( 'um_submit_form_errors_hook_login', 'um_submit_form_errors_hook_login' );
/**
* Display the login errors from other plugins
*
* @param $args
*/
function um_display_login_errors( $args ) {
if ( UM()->form()->count_errors() > 0 ) {
$errors = UM()->form()->errors;
// hook for other plugins to display error
$error_keys = array_keys( $errors );
}
if ( isset( $args['custom_fields'] ) ) {
$custom_fields = $args['custom_fields'];
}
if ( ! empty( $error_keys ) && ! empty( $custom_fields ) ) {
foreach ( $error_keys as $error ) {
if ( trim( $error ) && ! isset( $custom_fields[ $error ] ) && ! empty( $errors[ $error ] ) ) {
$error_message = apply_filters( 'login_errors', $errors[ $error ], $error );
if ( empty( $error_message ) ) {
return;
}
echo '<p class="um-notice err um-error-code-' . esc_attr( $error ) . '"><i class="um-icon-ios-close-empty" onclick="jQuery(this).parent().fadeOut();"></i>' . $error_message . '</p>';
}
}
}
}
add_action( 'um_before_login_fields', 'um_display_login_errors' );
/**
* Login checks through the frontend login
*
* @param array $submitted_data
* @param array $form_data
*/
function um_submit_form_errors_hook_logincheck( $submitted_data, $form_data ) {
// Logout if logged in
if ( is_user_logged_in() ) {
wp_logout();
}
$user_id = ( isset( UM()->login()->auth_id ) ) ? UM()->login()->auth_id : '';
um_fetch_user( $user_id );
$status = um_user( 'account_status' ); // account status
switch ( $status ) {
// If user can't log in to site...
case 'inactive':
case 'awaiting_admin_review':
case 'awaiting_email_confirmation':
case 'rejected':
um_reset_user();
// Not `um_safe_redirect()` because UM()->permalinks()->get_current_url() is situated on the same host.
wp_safe_redirect( add_query_arg( 'err', esc_attr( $status ), UM()->permalinks()->get_current_url() ) );
exit;
}
if ( isset( $form_data['form_id'] ) && absint( $form_data['form_id'] ) === absint( UM()->shortcodes()->core_login_form() ) && UM()->form()->errors && ! isset( $_POST[ UM()->honeypot ] ) ) {
// Not `um_safe_redirect()` because predefined login page is situated on the same host.
wp_safe_redirect( um_get_core_page( 'login' ) );
exit;
}
}
add_action( 'um_submit_form_errors_hook_logincheck', 'um_submit_form_errors_hook_logincheck', 9999, 2 );
/**
* Store last login timestamp
*
* @param $user_id
*/
function um_store_lastlogin_timestamp( $user_id ) {
update_user_meta( $user_id, '_um_last_login', current_time( 'timestamp' ) );
// Flush user cache after updating last_login timestamp.
UM()->user()->remove_cache( $user_id );
}
add_action( 'um_on_login_before_redirect', 'um_store_lastlogin_timestamp', 10, 1 );
/**
* @param $login
*/
function um_store_lastlogin_timestamp_( $login ) {
$user = get_user_by( 'login', $login );
if ( false !== $user ) {
um_store_lastlogin_timestamp( $user->ID );
$attempts = (int) get_user_meta( $user->ID, 'password_rst_attempts', true );
if ( $attempts ) {
//don't create meta but update if it's exists only
update_user_meta( $user->ID, 'password_rst_attempts', 0 );
}
}
}
add_action( 'wp_login', 'um_store_lastlogin_timestamp_' );
/**
* Login user process.
*
* @param array $submitted_data
*/
function um_user_login( $submitted_data ) {
// phpcs:disable WordPress.Security.NonceVerification -- already verified here
$rememberme = ( isset( $_REQUEST['rememberme'], $submitted_data['rememberme'] ) && 1 === (int) $submitted_data['rememberme'] ) ? 1 : 0;
// @todo check using the 'deny_admin_frontend_login' option
if ( false !== strrpos( um_user( 'wp_roles' ), 'administrator' ) && ( ! isset( $_GET['provider'] ) && UM()->options()->get( 'deny_admin_frontend_login' ) ) ) {
wp_die( esc_html__( 'This action has been prevented for security measures.', 'ultimate-member' ) );
}
UM()->user()->auto_login( um_user( 'ID' ), $rememberme );
/**
* Fires after successful login and before user is redirected.
*
* @since 1.3.x
* @hook um_on_login_before_redirect
*
* @param {int} $user_id User ID.
*
* @example <caption>Make any custom action after successful login and before user is redirected.</caption>
* function my_on_login_before_redirect( $user_id ) {
* // your code here
* }
* add_action( 'um_on_login_before_redirect', 'my_on_login_before_redirect', 10, 1 );
*/
do_action( 'um_on_login_before_redirect', um_user( 'ID' ) );
// Priority redirect from $_GET attribute.
if ( ! empty( $submitted_data['redirect_to'] ) ) {
um_safe_redirect( $submitted_data['redirect_to'] );
exit;
}
// Role redirect
$after_login = um_user( 'after_login' );
if ( empty( $after_login ) ) {
// Not `um_safe_redirect()` because predefined user profile page is situated on the same host.
wp_safe_redirect( um_user_profile_url() );
exit;
}
switch ( $after_login ) {
case 'redirect_admin':
// Not `um_safe_redirect()` because is redirected to wp-admin.
wp_safe_redirect( admin_url() );
exit;
case 'redirect_url':
/**
* Filters change redirect URL after successful login.
*
* @since 2.0
* @hook um_login_redirect_url
*
* @param {string} $can_view Redirect URL.
* @param {int} $user_id User ID.
*
* @return {string} Redirect URL.
*
* @example <caption>Change redirect URL.</caption>
* function my_login_redirect_url( $url, $id ) {
* // your code here
* return $url;
* }
* add_filter( 'um_login_redirect_url', 'my_login_redirect_url', 10, 2 );
*/
$redirect_url = apply_filters( 'um_login_redirect_url', um_user( 'login_redirect_url' ), um_user( 'ID' ) );
um_safe_redirect( $redirect_url );
exit;
case 'refresh':
// Not `um_safe_redirect()` because UM()->permalinks()->get_current_url() is situated on the same host.
wp_safe_redirect( UM()->permalinks()->get_current_url() );
exit;
case 'redirect_profile':
default:
// Not `um_safe_redirect()` because predefined user profile page is situated on the same host.
wp_safe_redirect( um_user_profile_url() );
exit;
}
// phpcs:enable WordPress.Security.NonceVerification -- already verified here
}
add_action( 'um_user_login', 'um_user_login' );
/**
* Form processing
*
* @param array $submitted_data
* @param array $form_data
*/
function um_submit_form_login( $submitted_data, $form_data ) {
if ( ! isset( UM()->form()->errors ) ) {
/**
* Fires after successful submit login form.
*
* Internal Ultimate Member callbacks (Priority -> Callback name -> Excerpt):
* * 10 - `um_user_login()` Login form main handler.
*
* @since 1.3.x
* @hook um_user_login
*
* @param {array} $submitted_data $_POST Submission array.
* @param {array} $form_data UM form data. Since 2.6.7
*
* @example <caption>Make any custom login action if submission is valid.</caption>
* function my_user_login( $submitted_data, $form_data ) {
* // your code here
* }
* add_action( 'um_user_login', 'my_user_login', 10, 2 );
*/
do_action( 'um_user_login', $submitted_data, $form_data );
}
/**
* Fires after submit login form.
*
* Internal Ultimate Member callbacks (Priority -> Callback name -> Excerpt):
* * 10 - um-messaging.
*
* @since 1.3.x
* @hook um_user_login_extra_hook
*
* @param {array} $submitted_data $_POST Submission array.
* @param {array} $form_data UM form data. Since 2.6.7
*
* @example <caption>Make any custom login action.</caption>
* function my_user_login_extra( $submitted_data, $form_data ) {
* // your code here
* }
* add_action( 'um_user_login_extra_hook', 'my_user_login_extra', 10, 2 );
*/
do_action( 'um_user_login_extra_hook', $submitted_data, $form_data );
}
add_action( 'um_submit_form_login', 'um_submit_form_login', 10, 2 );
/**
* Show the submit button
*
* @param $args
*/
function um_add_submit_button_to_login( $args ) {
/**
* UM hook
*
* @type filter
* @title um_login_form_button_one
* @description Change Login Form Primary button
* @input_vars
* [{"var":"$primary_btn_word","type":"string","desc":"Button text"},
* {"var":"$args","type":"array","desc":"Login Form arguments"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_login_form_button_one', 'function_name', 10, 2 ); ?>
* @example
* <?php
* add_filter( 'um_login_form_button_one', 'my_login_form_button_one', 10, 2 );
* function my_login_form_button_one( $primary_btn_word, $args ) {
* // your code here
* return $primary_btn_word;
* }
* ?>
*/
$primary_btn_word = apply_filters('um_login_form_button_one', $args['primary_btn_word'], $args );
if ( ! isset( $primary_btn_word ) || $primary_btn_word == '' ){
$primary_btn_word = UM()->options()->get( 'login_primary_btn_word' );
}
/**
* UM hook
*
* @type filter
* @title um_login_form_button_two
* @description Change Login Form Secondary button
* @input_vars
* [{"var":"$secondary_btn_word","type":"string","desc":"Button text"},
* {"var":"$args","type":"array","desc":"Login Form arguments"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_login_form_button_two', 'function_name', 10, 2 ); ?>
* @example
* <?php
* add_filter( 'um_login_form_button_two', 'my_login_form_button_two', 10, 2 );
* function my_login_form_button_two( $secondary_btn_word, $args ) {
* // your code here
* return $secondary_btn_word;
* }
* ?>
*/
$secondary_btn_word = apply_filters( 'um_login_form_button_two', $args['secondary_btn_word'], $args );
if ( ! isset( $secondary_btn_word ) || $secondary_btn_word == '' ){
$secondary_btn_word = UM()->options()->get( 'login_secondary_btn_word' );
}
$secondary_btn_url = ! empty( $args['secondary_btn_url'] ) ? $args['secondary_btn_url'] : um_get_core_page( 'register' );
/**
* UM hook
*
* @type filter
* @title um_login_form_button_two_url
* @description Change Login Form Secondary button URL
* @input_vars
* [{"var":"$secondary_btn_url","type":"string","desc":"Button URL"},
* {"var":"$args","type":"array","desc":"Login Form arguments"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_login_form_button_two_url', 'function_name', 10, 2 ); ?>
* @example
* <?php
* add_filter( 'um_login_form_button_two_url', 'my_login_form_button_two_url', 10, 2 );
* function my_login_form_button_two_url( $secondary_btn_url, $args ) {
* // your code here
* return $secondary_btn_url;
* }
* ?>
*/
$secondary_btn_url = apply_filters( 'um_login_form_button_two_url', $secondary_btn_url, $args ); ?>
<div class="um-col-alt">
<?php if ( ! empty( $args['show_rememberme'] ) ) {
UM()->fields()->checkbox( 'rememberme', __( 'Keep me signed in', 'ultimate-member' ), false ); ?>
<div class="um-clear"></div>
<?php }
if ( ! empty( $args['secondary_btn'] ) ) { ?>
<div class="um-left um-half">
<input type="submit" value="<?php esc_attr_e( wp_unslash( $primary_btn_word ), 'ultimate-member' ); ?>" class="um-button" id="um-submit-btn" />
</div>
<div class="um-right um-half">
<a href="<?php echo esc_url( $secondary_btn_url ); ?>" class="um-button um-alt">
<?php _e( wp_unslash( $secondary_btn_word ), 'ultimate-member' ); ?>
</a>
</div>
<?php } else { ?>
<div class="um-center">
<input type="submit" value="<?php esc_attr_e( wp_unslash( $primary_btn_word ), 'ultimate-member' ); ?>" class="um-button" id="um-submit-btn" />
</div>
<?php } ?>
<div class="um-clear"></div>
</div>
<?php
}
add_action( 'um_after_login_fields', 'um_add_submit_button_to_login', 1000 );
/**
* Display a forgot password link
*
* @param $args
*/
function um_after_login_submit( $args ) {
if ( empty( $args['forgot_pass_link'] ) ) {
return;
} ?>
<div class="um-col-alt-b">
<a href="<?php echo esc_url( um_get_core_page( 'password-reset' ) ); ?>" class="um-link-alt">
<?php _e( 'Forgot your password?', 'ultimate-member' ); ?>
</a>
</div>
<?php
}
add_action( 'um_after_login_fields', 'um_after_login_submit', 1001 );
/**
* Show Fields
*
* @param $args
*/
function um_add_login_fields( $args ) {
echo UM()->fields()->display( 'login', $args );
}
add_action( 'um_main_login_fields', 'um_add_login_fields', 100 );