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 7.6.8 7.6.9 7.7.0 7.8.0 7.8.1 7.8.10 7.8.11 7.8.12 7.8.13 7.8.14 7.8.15 7.8.16 7.8.17 7.8.18 7.8.2 7.8.3 7.8.4 7.8.5 7.8.5.1 7.8.6 7.8.7 7.8.8 7.8.9 7.9.0 7.9.1 7.9.10 7.9.11 7.9.2 7.9.3 7.9.4 7.9.5 7.9.6 7.9.7 7.9.8 7.9.9 8.0.0 8.0.1 8.0.2 8.0.3 8.0.4 8.0.5 8.0.6 8.0.7 8.0.8 8.1.0 8.1.1 8.1.2 8.1.3 8.1.4 8.2.0 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 2.0.0 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.2.0 3.3.0 3.4.0 3.5.0 3.6.1 3.7.0 3.8.0 3.9.0 3.9.1 3.9.2 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.9.0 4.9.1 4.9.2 4.9.3 5.0.0
admin-site-enhancements / classes / class-login-logout.php
admin-site-enhancements / classes Last commit date
class-activation.php 2 years ago class-admin-interface.php 2 years ago class-common-methods.php 2 years ago class-content-management.php 2 years ago class-custom-code.php 2 years ago class-deactivation.php 2 years ago class-disable-components.php 2 years ago class-login-logout.php 2 years ago class-optimizations.php 2 years ago class-security.php 2 years ago class-settings-fields-render.php 2 years ago class-settings-sanitization.php 2 years ago class-settings-sections-fields.php 2 years ago class-utilities.php 2 years ago
class-login-logout.php
474 lines
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 login URL with custom login slug in the query parameters
31
32 if ( false !== strpos( $url_input, '/' . $custom_login_slug . '/' ) ) {
33 wp_safe_redirect( home_url( 'wp-login.php?' . $custom_login_slug . '&redirect=false' ) );
34 exit;
35 }
36
37 }
38
39 /**
40 * Customize lost password URL. Add the custom login slug.
41 *
42 * @since 5.8.0
43 */
44 public function customize_lost_password_url( $lostpassword_url )
45 {
46 $options = get_option( ASENHA_SLUG_U );
47 $custom_login_slug = $options['custom_login_slug'];
48 // return home_url( '/wp-login.php?manage&action=lostpassword' );
49 return $lostpassword_url . '&' . $custom_login_slug;
50 }
51
52 /**
53 * Redirect to /not_found when login URL does not contain the custom login slug
54 * This will redirect /wp-login.php and /wp-admin/ to /not_found/
55 *
56 * @link https://plugins.trac.wordpress.org/browser/admin-login-url-change/trunk/admin-login-url-change.php#L121
57 * @since 1.4.0
58 */
59 public function redirect_on_default_login_urls()
60 {
61 global $interim_login ;
62 $options = get_option( ASENHA_SLUG_U );
63 $custom_login_slug = $options['custom_login_slug'];
64 // e.g. manage
65 $url_input = sanitize_text_field( $_SERVER['REQUEST_URI'] );
66
67 if ( isset( $_POST['log'] ) && isset( $_POST['pwd'] ) || isset( $_POST['post_password'] ) ) {
68 // Do nothing. i.e. do not redirect to /not_found/ as this contains a login POST request
69 // upon successful login, redirection to logged-in view of /wp-admin/ happens.
70 // Without this condition, login attempt will redirect to /not_found/
71 } else {
72 if ( false !== strpos( $url_input, 'wp-login.php' ) && false === strpos( $url_input, $custom_login_slug ) ) {
73 // At this point /wp-admin/ redirects to /wp-login.php
74 // So, the URL contains wp-login.php but the custom login slug is not part of the URL
75 // https://www.example.com/wp-login.php
76
77 if ( isset( $_GET['action'] ) && ('logout' == $_GET['action'] || 'lostpassword' == $_GET['action'] || 'rp' == $_GET['action'] || 'resetpass' == $_GET['action']) || isset( $_GET['checkemail'] ) && 'confirm' == $_GET['checkemail'] || isset( $_GET['interim-login'] ) && '1' == $_GET['interim-login'] || 'success' == $interim_login ) {
78 // Do nothing for when we're logging out, inside the reset password flow, or within the interim login flow
79 // e.g. https://www.example.com/wp-login.php?action=logout&_wpnonce=49bb818269
80 // e.g. https://www.example.com/wp-login.php?action=lostpassword&manage
81 // e.g. https://www.example.com/wp-login.php?interim-login=1&wp_lang=en_US
82 } else {
83 // Redirect to /not_found/
84 wp_safe_redirect( home_url( 'not_found/' ), 302 );
85 exit;
86 }
87
88 }
89 }
90
91 }
92
93 /**
94 * Redirect to custom login URL on failed login
95 *
96 * @link https://plugins.trac.wordpress.org/browser/admin-login-url-change/trunk/admin-login-url-change.php#L148
97 * @since 1.4.0
98 */
99 public function redirect_to_custom_login_url_on_login_fail()
100 {
101 global $asenha_limit_login ;
102 $options = get_option( ASENHA_SLUG_U );
103 $custom_login_slug = $options['custom_login_slug'];
104
105 if ( isset( $asenha_limit_login ) && is_array( $asenha_limit_login ) && $asenha_limit_login['within_lockout_period'] ) {
106 // Do nothing. This prevents redirection loop.
107 } else {
108 $should_redirect = true;
109
110 if ( $should_redirect ) {
111 // Append 'failed_login=true' so we can output custom error message above the login form
112 wp_safe_redirect( home_url( 'wp-login.php?' . $custom_login_slug . '&redirect=false&failed_login=true' ) );
113 exit;
114 }
115
116 }
117
118 }
119
120 /**
121 * Redirect to custom login URL on successful logout
122 *
123 * @link https://plugins.trac.wordpress.org/browser/admin-login-url-change/trunk/admin-login-url-change.php#L148
124 * @since 1.4.0
125 */
126 public function redirect_to_custom_login_url_on_logout_success()
127 {
128 $options = get_option( ASENHA_SLUG_U );
129 $custom_login_slug = $options['custom_login_slug'];
130 // Redirect to the login URL with custom login slug in it
131 wp_safe_redirect( home_url( 'wp-login.php?' . $custom_login_slug . '&redirect=false' ) );
132 exit;
133 }
134
135 /**
136 * Use site icon as the login page icon, the one on top of the login form
137 *
138 * @link https://plugins.trac.wordpress.org/browser/login-site-icon/trunk/login-site-icon.php
139 * @since 6.0.0
140 */
141 public function use_site_icon_on_login()
142 {
143
144 if ( has_site_icon() ) {
145 ?>
146 <style type="text/css">
147 .login h1 a {
148 background-image: url('<?php
149 site_icon_url( 180 );
150 ?>');
151 }
152 </style>
153 <?php
154 }
155
156 }
157
158 /**
159 * Use site icon URL as a link on the login page icon
160 *
161 * @link https://plugins.trac.wordpress.org/browser/login-site-icon/trunk/login-site-icon.php
162 * @since 6.0.0
163 */
164 public function use_site_url_on_login()
165 {
166 return get_site_url();
167 }
168
169 /**
170 * Add metabox to Appearance >> Menus page for the login logout menu items
171 *
172 * @since 3.4.0
173 */
174 public function add_login_logout_metabox()
175 {
176 add_meta_box(
177 'add-login-logout',
178 'Log In / Log Out',
179 array( $this, 'add_login_logout_menu_items' ),
180 'nav-menus',
181 'side',
182 'default'
183 );
184 }
185
186 /**
187 * Add menu items for the login logout metabox
188 *
189 * @since 3.4.0
190 */
191 public function add_login_logout_menu_items()
192 {
193 // The ID of the currently selected menu
194 global $nav_menu_selected_id ;
195 $menu_items = array(
196 'asenha-login' => array(
197 'title' => 'Log In',
198 'url' => '#asenha-login',
199 'classes' => array( 'asenha-login-menu-item' ),
200 ),
201 'asenha-logout' => array(
202 'title' => 'Log Out',
203 'url' => '#asenha-logout',
204 'classes' => array( 'asenha-logout-menu-item' ),
205 ),
206 'asenha-login-logout' => array(
207 'title' => 'Log In / Log Out',
208 'url' => '#asenha-login-logout',
209 'classes' => array( 'asenha-login-logout-menu-item' ),
210 ),
211 );
212 $item_details = array(
213 'db_id' => 0,
214 'object' => 'asenha',
215 'object_id' => '',
216 'menu_item_parent' => 0,
217 'type' => 'custom',
218 'title' => '',
219 'url' => '',
220 'target' => '',
221 'attr_title' => '',
222 'classes' => array(),
223 'xfn' => '',
224 );
225 $menu_items_object = array();
226 foreach ( $menu_items as $item_id => $details ) {
227 $menu_items_object[$details['title']] = (object) $item_details;
228 $menu_items_object[$details['title']]->object_id = $item_id;
229 $menu_items_object[$details['title']]->title = $details['title'];
230 $menu_items_object[$details['title']]->classes = $details['classes'];
231 $menu_items_object[$details['title']]->url = $details['url'];
232 }
233 $walker = new Walker_Nav_Menu_Checklist( array() );
234 ?>
235 <div id="login-logout-links" class="loginlinksdiv">
236 <div id="tabs-panel-login-logout-links-all" class="tabs-panel tabs-panel-view-all tabs-panel-active">
237 <ul id="login-logout-links-checklist" class="list:login-logout-links categorychecklist form-no-clear">
238 <?php
239 echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $menu_items_object ), 0, (object) array(
240 'walker' => $walker,
241 ) ) ;
242 ?>
243 </ul>
244 </div>
245 <p class="button-controls">
246 <span class="add-to-menu">
247 <input type="submit"<?php
248 disabled( $nav_menu_selected_id, 0 );
249 ?> class="button-secondary submit-add-to-menu right" value="<?php
250 echo esc_attr( 'Add to Menu' ) ;
251 ?>" name="add-login-logout-links-menu-item" id="submit-login-logout-links" />
252 <span class="spinner"></span>
253 </span>
254 </p>
255 </div>
256 <?php
257 }
258
259 /**
260 * Setup login logout URL based on login state
261 *
262 * @since 3.4.0
263 */
264 public function set_login_logout_menu_item_dynamic_url( $menu_item )
265 {
266 global $pagenow ;
267 $options = get_option( ASENHA_SLUG_U, array() );
268
269 if ( $pagenow != 'nav-menus.php' && !defined( 'DOING_AJAX' ) && isset( $menu_item->url ) && false !== strpos( $menu_item->url, 'asenha' ) ) {
270 // Define login URL based on whether
271
272 if ( array_key_exists( 'change_login_url', $options ) && $options['change_login_url'] ) {
273 if ( array_key_exists( 'custom_login_slug', $options ) && !empty($options['custom_login_slug']) ) {
274 $login_page_url = get_site_url() . '/' . $options['custom_login_slug'];
275 }
276 } else {
277 $login_page_url = wp_login_url();
278 }
279
280 $logout_redirect_url = home_url();
281 switch ( $menu_item->url ) {
282 case '#asenha-login':
283 $menu_item->url = $login_page_url;
284 break;
285 case '#asenha-logout':
286 $menu_item->url = wp_logout_url();
287 break;
288 case '#asenha-login-logout':
289 $menu_item->url = ( is_user_logged_in() ? wp_logout_url() : $login_page_url );
290 $menu_item->title = ( is_user_logged_in() ? 'Log Out' : 'Log In' );
291 break;
292 }
293 }
294
295 return $menu_item;
296 }
297
298 /**
299 * Conditionally remove login or logout menu item based on is_user_logged_in()
300 *
301 * @since 3.4.0
302 */
303 public function maybe_remove_login_or_logout_menu_item( $sorted_menu_items )
304 {
305 foreach ( $sorted_menu_items as $menu => $item ) {
306 $item_classes = $item->classes;
307 // Maybe remove Log In menu item
308 if ( in_array( 'asenha-login-menu-item', $item_classes ) ) {
309 if ( is_user_logged_in() ) {
310 unset( $sorted_menu_items[$menu] );
311 }
312 }
313 // Maybe remove Log Out menu item
314 if ( in_array( 'asenha-logout-menu-item', $item_classes ) ) {
315 if ( !is_user_logged_in() ) {
316 unset( $sorted_menu_items[$menu] );
317 }
318 }
319 }
320 return $sorted_menu_items;
321 }
322
323 /**
324 * Redirect to custom internal URL after login for user roles
325 *
326 * @param string $redirect_to_url URL to redirect to. Default is admin dashboard URL.
327 * @param string $origin_url URL the user is coming from.
328 * @param object $user logged-in user's data.
329 * @since 1.5.0
330 */
331 public function redirect_for_roles_after_login( $username, $user )
332 {
333 $options = get_option( ASENHA_SLUG_U, array() );
334 $redirect_after_login_to_slug = trim( trim( $options['redirect_after_login_to_slug'] ), '/' );
335
336 if ( false !== strpos( $redirect_after_login_to_slug, '.php' ) ) {
337 $slug_suffix = '';
338 } else {
339 $slug_suffix = '/';
340 }
341
342 $redirect_after_login_for = $options['redirect_after_login_for'];
343
344 if ( isset( $redirect_after_login_for ) && count( $redirect_after_login_for ) > 0 ) {
345 // Assemble single-dimensional array of roles for which custom URL redirection should happen
346 $roles_for_custom_redirect = array();
347 foreach ( $redirect_after_login_for as $role_slug => $custom_redirect ) {
348 if ( $custom_redirect ) {
349 $roles_for_custom_redirect[] = $role_slug;
350 }
351 }
352 // Does the user have roles data in array form?
353 if ( isset( $user->roles ) && is_array( $user->roles ) ) {
354 $current_user_roles = $user->roles;
355 }
356 // Set custom redirect URL for roles set in the settings. Otherwise, leave redirect URL to the default, i.e. admin dashboard.
357 foreach ( $current_user_roles as $role ) {
358
359 if ( in_array( $role, $roles_for_custom_redirect ) ) {
360 wp_safe_redirect( home_url( $redirect_after_login_to_slug . $slug_suffix ) );
361 exit;
362 }
363
364 }
365 }
366
367 }
368
369 /**
370 * Redirect to custom internal URL after login for user roles
371 *
372 * @param string $redirect_to_url URL to redirect to. Default is admin dashboard URL.
373 * @param string $origin_url URL the user is coming from.
374 * @param object $user logged-in user's data.
375 * @since 1.6.0
376 */
377 public function redirect_after_logout( $user_id )
378 {
379 $options = get_option( ASENHA_SLUG_U, array() );
380 $redirect_after_logout_to_slug = $options['redirect_after_logout_to_slug'];
381 $redirect_after_logout_for = $options['redirect_after_logout_for'];
382 $user = get_userdata( $user_id );
383
384 if ( isset( $redirect_after_logout_for ) && count( $redirect_after_logout_for ) > 0 ) {
385 // Assemble single-dimensional array of roles for which custom URL redirection should happen
386 $roles_for_custom_redirect = array();
387 foreach ( $redirect_after_logout_for as $role_slug => $custom_redirect ) {
388 if ( $custom_redirect ) {
389 $roles_for_custom_redirect[] = $role_slug;
390 }
391 }
392 // Does the user have roles data in array form?
393 if ( isset( $user->roles ) && is_array( $user->roles ) ) {
394 $current_user_roles = $user->roles;
395 }
396 // Redirect for roles set in the settings. Otherwise, leave redirect URL to the default, i.e. admin dashboard.
397 foreach ( $current_user_roles as $role ) {
398
399 if ( in_array( $role, $roles_for_custom_redirect ) ) {
400 wp_safe_redirect( home_url( $redirect_after_logout_to_slug . '/' ) );
401 exit;
402 }
403
404 }
405 }
406
407 }
408
409 /**
410 * Log date time when a user last logged in successfully
411 *
412 * @since 3.6.0
413 */
414 public function log_login_datetime( $user_login )
415 {
416 $user = get_user_by( 'login', $user_login );
417 // by username
418 update_user_meta( $user->ID, 'asenha_last_login_on', time() );
419 }
420
421 /**
422 * Add Last Login column to users list table
423 *
424 * @since 3.6.0
425 */
426 public function add_last_login_column( $columns )
427 {
428 $columns['asenha_last_login'] = 'Last Login';
429 return $columns;
430 }
431
432 /**
433 * Show last login info in the last login column
434 *
435 * @since 3.6.0
436 */
437 public function show_last_login_info( $output, $column_name, $user_id )
438 {
439 if ( 'asenha_last_login' === $column_name ) {
440
441 if ( !empty(get_user_meta( $user_id, 'asenha_last_login_on', true )) ) {
442 $last_login_unixtime = (int) get_user_meta( $user_id, 'asenha_last_login_on', true );
443
444 if ( function_exists( 'wp_date' ) ) {
445 $output = wp_date( 'M j, Y H:i', $last_login_unixtime );
446 } else {
447 $output = date_i18n( 'M j, Y H:i', $last_login_unixtime );
448 }
449
450 } else {
451 $output = 'No data yet';
452 }
453
454 }
455 return $output;
456 }
457
458 /**
459 * Add custom CSS for the Last Login column
460 *
461 * @since 3.6.0
462 */
463 public function add_column_style()
464 {
465 ?>
466 <style>
467 .column-asenha_last_login {
468 width: 90px;
469 }
470 </style>
471 <?php
472 }
473
474 }