PluginProbe
Admin and Site Enhancements (ASE) / 6.9.1
Admin and Site Enhancements (ASE) v6.9.1
9.1.1 9.1.0 9.0.2 9.0.1 9.0.0 8.9.2 8.9.1 8.9.0 8.8.8 8.8.7 8.8.6 8.8.5 8.8.4 8.8.3 8.8.2 8.8.1 8.8.0 8.7.3 8.7.2 8.7.1 8.2.1 8.2.2 8.2.3 8.3.0 8.3.1 All 273 releases
admin-site-enhancements / classes / class-login-logout.php
class-login-logout.php
636 lines 23.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ASENHA\Classes;
4
5 use Walker_Nav_Menu_Checklist ;
6 use WP_Error ;
7 /**
8 * Class related to Log In Log Out features
9 *
10 * @since 3.6.0
11 */
12 class Login_Logout
13 {
14 /**
15 * Redirect to valid login URL when custom login slug is part of the request URL
16 *
17 * @link https://plugins.trac.wordpress.org/browser/admin-login-url-change/trunk/admin-login-url-change.php#L134
18 * @since 1.4.0
19 */
20 public function redirect_on_custom_login_url()
21 {
22 $options = get_option( ASENHA_SLUG_U );
23 $custom_login_slug = $options['custom_login_slug'];
24 $url_input = sanitize_text_field( $_SERVER['REQUEST_URI'] );
25 // Make sure $url_input ends with /
26 if ( false !== strpos( $url_input, $custom_login_slug ) ) {
27 if ( substr( $url_input, -1 ) != '/' ) {
28 $url_input = $url_input . '/';
29 }
30 }
31 // If URL contains the custom login slug, redirect to the dashboard
32
33 if ( false !== strpos( $url_input, '/' . $custom_login_slug . '/' ) ) {
34
35 if ( is_user_logged_in() ) {
36 // Redirect to dashboard
37 wp_safe_redirect( get_admin_url() );
38 } else {
39 // Redirect to the login URL with custom login slug in the query parameters
40 wp_safe_redirect( site_url( '/wp-login.php?' . $custom_login_slug . '&redirect=false' ) );
41 }
42
43 exit;
44 }
45
46 }
47
48 /**
49 * Customize login URL returned when calling wp_login_url(). Add the custom login slug.
50 *
51 * @since 5.8.0
52 */
53 public function customize_login_url( $lostpassword_url )
54 {
55 $options = get_option( ASENHA_SLUG_U );
56 $custom_login_slug = $options['custom_login_slug'];
57 return home_url( '/' . $custom_login_slug . '/' );
58 }
59
60 /**
61 * Customize lost password URL. Add the custom login slug.
62 *
63 * @since 5.8.0
64 */
65 public function customize_lost_password_url( $lostpassword_url )
66 {
67 $options = get_option( ASENHA_SLUG_U );
68 $custom_login_slug = $options['custom_login_slug'];
69 // return home_url( '/wp-login.php?manage&action=lostpassword' );
70 return $lostpassword_url . '&' . $custom_login_slug;
71 }
72
73 /**
74 * Customize registration URL. Add the custom login slug.
75 *
76 * @since 6.2.5
77 */
78 public function customize_register_url( $registration_url )
79 {
80 $options = get_option( ASENHA_SLUG_U );
81 $custom_login_slug = $options['custom_login_slug'];
82 // return home_url( '/wp-login.php?manage&action=lostpassword' );
83 return $registration_url . '&' . $custom_login_slug;
84 }
85
86 /**
87 * Redirect to /not_found when login URL does not contain the custom login slug
88 * This will redirect /wp-login.php and /wp-admin/ to /not_found/
89 *
90 * @link https://plugins.trac.wordpress.org/browser/admin-login-url-change/trunk/admin-login-url-change.php#L121
91 * @since 1.4.0
92 */
93 public function redirect_on_default_login_urls()
94 {
95 global $interim_login ;
96 $options = get_option( ASENHA_SLUG_U );
97 $custom_login_slug = $options['custom_login_slug'];
98 // e.g. manage
99 $url_input = sanitize_text_field( $_SERVER['REQUEST_URI'] );
100 $redirect_slug = 'not_found';
101 // When logging in
102
103 if ( isset( $_POST['log'] ) && isset( $_POST['pwd'] ) || isset( $_POST['post_password'] ) ) {
104 // Do nothing. i.e. do not redirect to /not_found/ as this contains a login POST request
105 // upon successful login, redirection to logged-in view of /wp-admin/ happens.
106 // Without this condition, login attempt will redirect to /not_found/
107 } else {
108 // When landing on the login page
109 // At this point /admin, /wp-admin/, /login redirects to /wp-login.php
110 if ( false !== strpos( $url_input, 'wp-login.php' ) ) {
111
112 if ( isset( $_GET['action'] ) && ('logout' == $_GET['action'] || 'rp' == $_GET['action'] || 'resetpass' == $_GET['action']) || isset( $_GET['checkemail'] ) && ('confirm' == $_GET['checkemail'] || 'registered' == $_GET['checkemail']) || isset( $_GET['interim-login'] ) && '1' == $_GET['interim-login'] || 'success' == $interim_login ) {
113 // When we're logging out, inside the reset password flow, inside the registration flow or within the interim login flow
114 // e.g. https://www.example.com/wp-login.php?action=logout&_wpnonce=49bb818269
115 // e.g. https://www.example.com/wp-login.php?action=rp --> reset password
116 // e.g. https://www.example.com/wp-login.php?action=resetpass --> reset password
117 // e.g. https://www.example.com/wp-login.php?checkmail=confirm --> reset password
118 // e.g. https://www.example.com/wp-login.php?checkmail=registered --> register account
119 // e.g. https://www.example.com/wp-login.php?interim-login=1&wp_lang=en_US
120 // Do nothing.. proceed...
121 } elseif ( isset( $_GET['action'] ) && ('lostpassword' == $_GET['action'] || 'register' == $_GET['action']) ) {
122 // When resetting password or registering an account
123
124 if ( isset( $_POST['user_login'] ) ) {
125 // Sending the form to reset password or register an account...
126 // Do nothing.. proceed with password reset or account registration
127 } else {
128 // When landing on the password reset or registration form
129 // ...and custom login slug is not in the URL
130
131 if ( false === strpos( $url_input, $custom_login_slug ) ) {
132 // Redirect to /not_found/
133 wp_safe_redirect( home_url( $redirect_slug . '/' ), 302 );
134 exit;
135 }
136
137 // or, custom login slug is in the url
138 // e.g. https://www.example.com/wp-login.php?action=lostpassword&customloginslug
139 // e.g. https://www.example.com/wp-login.php?action=register&customloginslug
140 // Do nothing... allow reset password or registration
141 }
142
143 } else {
144 // When landing on the login form
145 // ...and custom login slug is not in the URL
146
147 if ( false === strpos( $url_input, $custom_login_slug ) ) {
148 // Redirect to /not_found/
149 wp_safe_redirect( home_url( $redirect_slug . '/' ), 302 );
150 exit;
151 }
152
153 // ...or, custom login slug is in the URL
154 // e.g. https://www.example.com/wp-login.php?customloginslug&redirect=false
155 // Do nothing... allow login
156 }
157
158 }
159 }
160
161 }
162
163 /**
164 * Redirect to custom login URL on failed login
165 *
166 * @link https://plugins.trac.wordpress.org/browser/admin-login-url-change/trunk/admin-login-url-change.php#L148
167 * @since 1.4.0
168 */
169 public function redirect_to_custom_login_url_on_login_fail()
170 {
171 global $asenha_limit_login ;
172 $options = get_option( ASENHA_SLUG_U );
173 $custom_login_slug = $options['custom_login_slug'];
174
175 if ( isset( $asenha_limit_login ) && is_array( $asenha_limit_login ) && $asenha_limit_login['within_lockout_period'] ) {
176 // Do nothing. This prevents redirection loop.
177 } else {
178 $should_redirect = true;
179
180 if ( $should_redirect ) {
181 // Append 'failed_login=true' so we can output custom error message above the login form
182 wp_safe_redirect( home_url( 'wp-login.php?' . $custom_login_slug . '&redirect=false&failed_login=true' ) );
183 exit;
184 }
185
186 }
187
188 }
189
190 /**
191 * Add login error message on top of the login form.
192 * Only shown if there's a failed_login URL parameter, and Limit Login Attempts module is not enabled.
193 * If LLA module is enabled, the same custom login error message is handled there.
194 *
195 * @since 6.9.1
196 */
197 public function add_failed_login_message( $message )
198 {
199 global $asenha_limit_login ;
200 if ( isset( $_REQUEST['failed_login'] ) && $_REQUEST['failed_login'] == 'true' ) {
201 if ( is_null( $asenha_limit_login ) ) {
202 $message = '<div id="login_error" class="notice notice-error"><b>Error:</b> Invalid username/email or incorrect password.</div>';
203 }
204 }
205 return $message;
206 }
207
208 /**
209 * Redirect to custom login URL on successful logout
210 *
211 * @link https://plugins.trac.wordpress.org/browser/admin-login-url-change/trunk/admin-login-url-change.php#L148
212 * @since 1.4.0
213 */
214 public function redirect_to_custom_login_url_on_logout_success()
215 {
216 $options = get_option( ASENHA_SLUG_U );
217 $custom_login_slug = $options['custom_login_slug'];
218 // Redirect to the login URL with custom login slug in it
219 wp_safe_redirect( home_url( 'wp-login.php?' . $custom_login_slug . '&redirect=false' ) );
220 exit;
221 }
222
223 /**
224 * Change default label on login form
225 *
226 * @param array $defaults an array of default login form arguments
227 * @link https://plugins.trac.wordpress.org/browser/xo-security/tags/3.7.1/inc/class-xo-security.php
228 * @since 6.8.0
229 */
230 public function change_login_form_defaults( $defaults )
231 {
232 $defaults['label_username'] = 'Username';
233 return $defaults;
234 }
235
236 /**
237 * Filter for gettext.
238 *
239 * @param string $translation Translated text.
240 * @param string $text Text to translate.
241 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
242 * @link https://plugins.trac.wordpress.org/browser/xo-security/tags/3.7.1/inc/class-xo-security.php
243 * @since 6.8.0
244 */
245 public function gettext_login_id_username( $translation, $text, $domain )
246 {
247 global $pagenow ;
248 if ( 'wp-login.php' === $pagenow ) {
249 if ( 'default' === $domain && 'Username or Email Address' === $text ) {
250 $translation = 'Username';
251 }
252 }
253 return $translation;
254 }
255
256 /**
257 * Filter for authenticate.
258 *
259 * @param WP_User|Mixed $user user object if authenticated.
260 * @param String $username username.
261 * @return WP_User|Mixed authenticated user or error.
262 * @link https://plugins.trac.wordpress.org/browser/xo-security/tags/3.7.1/inc/class-xo-security.php
263 * @since 6.8.0
264 */
265 public function authenticate_email( $user, $username )
266 {
267 if ( null !== $user && !is_wp_error( $user ) && $user->user_email !== $username ) {
268 $user = new WP_Error( 'invalid_username', '<strong>Error:</strong> There is no account with that email address.' );
269 }
270 return $user;
271 }
272
273 /**
274 * Filter for gettext.
275 *
276 * @param string $translation Translated text.
277 * @param string $text Text to translate.
278 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
279 * @return WP_User|Mixed authenticated user or error.
280 * @link https://plugins.trac.wordpress.org/browser/xo-security/tags/3.7.1/inc/class-xo-security.php
281 */
282 public function gettext_login_id_email( $translation, $text, $domain )
283 {
284 global $pagenow ;
285 if ( 'wp-login.php' === $pagenow ) {
286 if ( 'default' === $domain && 'Username or Email Address' === $text ) {
287 $translation = 'Email';
288 }
289 }
290 return $translation;
291 }
292
293 /**
294 * Use site icon as the login page icon, the one on top of the login form
295 *
296 * @link https://plugins.trac.wordpress.org/browser/login-site-icon/trunk/login-site-icon.php
297 * @since 6.0.0
298 */
299 public function use_site_icon_on_login()
300 {
301
302 if ( has_site_icon() ) {
303 ?>
304 <style type="text/css">
305 .login h1 a {
306 background-image: url('<?php
307 site_icon_url( 180 );
308 ?>');
309 }
310 </style>
311 <?php
312 }
313
314 }
315
316 /**
317 * Use site icon URL as a link on the login page icon
318 *
319 * @link https://plugins.trac.wordpress.org/browser/login-site-icon/trunk/login-site-icon.php
320 * @since 6.0.0
321 */
322 public function use_site_url_on_login()
323 {
324 return get_site_url();
325 }
326
327 /**
328 * Add metabox to Appearance >> Menus page for the login logout menu items
329 *
330 * @since 3.4.0
331 */
332 public function add_login_logout_metabox()
333 {
334 add_meta_box(
335 'add-login-logout',
336 'Log In / Log Out',
337 array( $this, 'add_login_logout_menu_items' ),
338 'nav-menus',
339 'side',
340 'default'
341 );
342 }
343
344 /**
345 * Add menu items for the login logout metabox
346 *
347 * @since 3.4.0
348 */
349 public function add_login_logout_menu_items()
350 {
351 // The ID of the currently selected menu
352 global $nav_menu_selected_id ;
353 $menu_items = array(
354 'asenha-login' => array(
355 'title' => 'Log In',
356 'url' => '#asenha-login',
357 'classes' => array( 'asenha-login-menu-item' ),
358 ),
359 'asenha-logout' => array(
360 'title' => 'Log Out',
361 'url' => '#asenha-logout',
362 'classes' => array( 'asenha-logout-menu-item' ),
363 ),
364 'asenha-login-logout' => array(
365 'title' => 'Log In / Log Out',
366 'url' => '#asenha-login-logout',
367 'classes' => array( 'asenha-login-logout-menu-item' ),
368 ),
369 );
370 $item_details = array(
371 'db_id' => 0,
372 'object' => 'asenha',
373 'object_id' => '',
374 'menu_item_parent' => 0,
375 'type' => 'custom',
376 'title' => '',
377 'url' => '',
378 'target' => '',
379 'attr_title' => '',
380 'classes' => array(),
381 'xfn' => '',
382 );
383 $menu_items_object = array();
384 foreach ( $menu_items as $item_id => $details ) {
385 $menu_items_object[$details['title']] = (object) $item_details;
386 $menu_items_object[$details['title']]->object_id = $item_id;
387 $menu_items_object[$details['title']]->title = $details['title'];
388 $menu_items_object[$details['title']]->classes = $details['classes'];
389 $menu_items_object[$details['title']]->url = $details['url'];
390 }
391 $walker = new Walker_Nav_Menu_Checklist( array() );
392 ?>
393 <div id="login-logout-links" class="loginlinksdiv">
394 <div id="tabs-panel-login-logout-links-all" class="tabs-panel tabs-panel-view-all tabs-panel-active">
395 <ul id="login-logout-links-checklist" class="list:login-logout-links categorychecklist form-no-clear">
396 <?php
397 echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $menu_items_object ), 0, (object) array(
398 'walker' => $walker,
399 ) ) ;
400 ?>
401 </ul>
402 </div>
403 <p class="button-controls">
404 <span class="add-to-menu">
405 <input type="submit"<?php
406 disabled( $nav_menu_selected_id, 0 );
407 ?> class="button-secondary submit-add-to-menu right" value="<?php
408 echo esc_attr( 'Add to Menu' ) ;
409 ?>" name="add-login-logout-links-menu-item" id="submit-login-logout-links" />
410 <span class="spinner"></span>
411 </span>
412 </p>
413 </div>
414 <?php
415 }
416
417 /**
418 * Setup login logout URL based on login state
419 *
420 * @since 3.4.0
421 */
422 public function set_login_logout_menu_item_dynamic_url( $menu_item )
423 {
424 global $pagenow ;
425 $options = get_option( ASENHA_SLUG_U, array() );
426
427 if ( $pagenow != 'nav-menus.php' && !defined( 'DOING_AJAX' ) && isset( $menu_item->url ) && false !== strpos( $menu_item->url, 'asenha' ) ) {
428 // Define login URL based on whether
429
430 if ( array_key_exists( 'change_login_url', $options ) && $options['change_login_url'] ) {
431 if ( array_key_exists( 'custom_login_slug', $options ) && !empty($options['custom_login_slug']) ) {
432 $login_page_url = get_site_url() . '/' . $options['custom_login_slug'];
433 }
434 } else {
435 $login_page_url = wp_login_url();
436 }
437
438 $logout_redirect_url = home_url();
439 switch ( $menu_item->url ) {
440 case '#asenha-login':
441 $menu_item->url = $login_page_url;
442 break;
443 case '#asenha-logout':
444 $menu_item->url = wp_logout_url();
445 break;
446 case '#asenha-login-logout':
447 $menu_item->url = ( is_user_logged_in() ? wp_logout_url() : $login_page_url );
448 $menu_item->title = ( is_user_logged_in() ? 'Log Out' : 'Log In' );
449 break;
450 }
451 }
452
453 return $menu_item;
454 }
455
456 /**
457 * Conditionally remove login or logout menu item based on is_user_logged_in()
458 *
459 * @since 3.4.0
460 */
461 public function maybe_remove_login_or_logout_menu_item( $sorted_menu_items )
462 {
463 foreach ( $sorted_menu_items as $menu => $item ) {
464 $item_classes = $item->classes;
465 // Maybe remove Log In menu item
466 if ( in_array( 'asenha-login-menu-item', $item_classes ) ) {
467 if ( is_user_logged_in() ) {
468 unset( $sorted_menu_items[$menu] );
469 }
470 }
471 // Maybe remove Log Out menu item
472 if ( in_array( 'asenha-logout-menu-item', $item_classes ) ) {
473 if ( !is_user_logged_in() ) {
474 unset( $sorted_menu_items[$menu] );
475 }
476 }
477 }
478 return $sorted_menu_items;
479 }
480
481 /**
482 * Redirect to custom internal URL after login for user roles
483 *
484 * @param string $redirect_to_url URL to redirect to. Default is admin dashboard URL.
485 * @param string $origin_url URL the user is coming from.
486 * @param object $user logged-in user's data.
487 * @since 1.5.0
488 */
489 public function redirect_for_roles_after_login( $username, $user )
490 {
491 $options = get_option( ASENHA_SLUG_U, array() );
492 $redirect_after_login_to_slug = trim( trim( $options['redirect_after_login_to_slug'] ), '/' );
493
494 if ( false !== strpos( $redirect_after_login_to_slug, '.php' ) ) {
495 $slug_suffix = '';
496 } else {
497 $slug_suffix = '/';
498 }
499
500 $redirect_after_login_for = $options['redirect_after_login_for'];
501
502 if ( isset( $redirect_after_login_for ) && count( $redirect_after_login_for ) > 0 ) {
503 // Assemble single-dimensional array of roles for which custom URL redirection should happen
504 $roles_for_custom_redirect = array();
505 foreach ( $redirect_after_login_for as $role_slug => $custom_redirect ) {
506 if ( $custom_redirect ) {
507 $roles_for_custom_redirect[] = $role_slug;
508 }
509 }
510 // Does the user have roles data in array form?
511 if ( isset( $user->roles ) && is_array( $user->roles ) ) {
512 $current_user_roles = $user->roles;
513 }
514 // Set custom redirect URL for roles set in the settings. Otherwise, leave redirect URL to the default, i.e. admin dashboard.
515 foreach ( $current_user_roles as $role ) {
516
517 if ( in_array( $role, $roles_for_custom_redirect ) ) {
518 wp_safe_redirect( home_url( $redirect_after_login_to_slug . $slug_suffix ) );
519 exit;
520 }
521
522 }
523 }
524
525 }
526
527 /**
528 * Redirect to custom internal URL after login for user roles
529 *
530 * @param string $redirect_to_url URL to redirect to. Default is admin dashboard URL.
531 * @param string $origin_url URL the user is coming from.
532 * @param object $user logged-in user's data.
533 * @since 1.6.0
534 */
535 public function redirect_after_logout( $user_id )
536 {
537 $options = get_option( ASENHA_SLUG_U, array() );
538 $redirect_after_logout_to_slug = $options['redirect_after_logout_to_slug'];
539 $redirect_after_logout_for = $options['redirect_after_logout_for'];
540 $user = get_userdata( $user_id );
541
542 if ( isset( $redirect_after_logout_for ) && count( $redirect_after_logout_for ) > 0 ) {
543 // Assemble single-dimensional array of roles for which custom URL redirection should happen
544 $roles_for_custom_redirect = array();
545 foreach ( $redirect_after_logout_for as $role_slug => $custom_redirect ) {
546 if ( $custom_redirect ) {
547 $roles_for_custom_redirect[] = $role_slug;
548 }
549 }
550 // Does the user have roles data in array form?
551 if ( isset( $user->roles ) && is_array( $user->roles ) ) {
552 $current_user_roles = $user->roles;
553 }
554 // Redirect for roles set in the settings. Otherwise, leave redirect URL to the default, i.e. admin dashboard.
555 foreach ( $current_user_roles as $role ) {
556
557 if ( in_array( $role, $roles_for_custom_redirect ) ) {
558 wp_safe_redirect( home_url( $redirect_after_logout_to_slug . '/' ) );
559 exit;
560 }
561
562 }
563 }
564
565 }
566
567 /**
568 * Log date time when a user last logged in successfully
569 *
570 * @since 3.6.0
571 */
572 public function log_login_datetime( $user_login )
573 {
574 $user = get_user_by( 'login', $user_login );
575 // by username
576 if ( is_object( $user ) ) {
577 if ( property_exists( $user, 'ID' ) ) {
578 update_user_meta( $user->ID, 'asenha_last_login_on', time() );
579 }
580 }
581 }
582
583 /**
584 * Add Last Login column to users list table
585 *
586 * @since 3.6.0
587 */
588 public function add_last_login_column( $columns )
589 {
590 $columns['asenha_last_login'] = 'Last Login';
591 return $columns;
592 }
593
594 /**
595 * Show last login info in the last login column
596 *
597 * @since 3.6.0
598 */
599 public function show_last_login_info( $output, $column_name, $user_id )
600 {
601 if ( 'asenha_last_login' === $column_name ) {
602
603 if ( !empty(get_user_meta( $user_id, 'asenha_last_login_on', true )) ) {
604 $last_login_unixtime = (int) get_user_meta( $user_id, 'asenha_last_login_on', true );
605
606 if ( function_exists( 'wp_date' ) ) {
607 $output = wp_date( 'M j, Y H:i', $last_login_unixtime );
608 } else {
609 $output = date_i18n( 'M j, Y H:i', $last_login_unixtime );
610 }
611
612 } else {
613 $output = 'No data yet';
614 }
615
616 }
617 return $output;
618 }
619
620 /**
621 * Add custom CSS for the Last Login column
622 *
623 * @since 3.6.0
624 */
625 public function add_column_style()
626 {
627 ?>
628 <style>
629 .column-asenha_last_login {
630 width: 90px;
631 }
632 </style>
633 <?php
634 }
635
636 }