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