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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 3 years ago class-admin-interface.php 3 years ago class-common-methods.php 3 years ago class-content-management.php 3 years ago class-custom-code.php 3 years ago class-deactivation.php 3 years ago class-disable-components.php 3 years ago class-login-logout.php 3 years ago class-optimizations.php 3 years ago class-security.php 3 years ago class-settings-fields-render.php 3 years ago class-settings-sanitization.php 3 years ago class-settings-sections-fields.php 3 years ago class-utilities.php 3 years ago
class-login-logout.php
455 lines
1 <?php
2
3 namespace ASENHA\Classes;
4 use Walker_Nav_Menu_Checklist;
5
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
24 $url_input = sanitize_text_field( $_SERVER['REQUEST_URI'] );
25
26 // Exclude interim login URL, which is inside modal popup when user is logged out in the background
27 // URL looks like https://www.example.com/wp-login.php?interim-login=1&wp_lang=en_US
28 if ( false !== strpos( $url_input, 'interim-login=1' ) ) {
29 remove_action( 'login_head', [ $this, 'redirect_on_default_login_urls' ] );
30 }
31
32 // Exclude URLs in the "Lost your password" flow
33 // URL looks like https://www.example.com/wp-login.php?action=lostpassword
34 if ( false !== strpos( $url_input, 'action=lostpassword' )
35 || false !== strpos( $url_input, 'checkemail=confirm' )
36 || false !== strpos( $url_input, 'action=rp' )
37 || false !== strpos( $url_input, 'action=resetpass' )
38 ) {
39 remove_action( 'login_head', [ $this, 'redirect_on_default_login_urls' ] );
40 }
41
42 // Make sure $url_input ends with /
43 if ( false !== strpos( $url_input, $custom_login_slug ) ) {
44 if ( substr( $url_input, -1 ) != '/' ) {
45 $url_input = $url_input . '/';
46 }
47 }
48
49 // If URL contains the custom login slug, redirect to the login URL with custom login slug in the query parameters
50 if ( false !== strpos( $url_input, '/' . $custom_login_slug . '/' ) ) {
51 wp_safe_redirect( home_url( 'wp-login.php?' . $custom_login_slug . '&redirect=false' ) );
52 exit();
53 }
54
55 }
56
57 /**
58 * Redirect to /not_found when login URL does not contain the custom login slug
59 *
60 * @link https://plugins.trac.wordpress.org/browser/admin-login-url-change/trunk/admin-login-url-change.php#L121
61 * @since 1.4.0
62 */
63 public function redirect_on_default_login_urls() {
64
65 global $interim_login;
66
67 $options = get_option( ASENHA_SLUG_U );
68 $custom_login_slug = $options['custom_login_slug']; // e.g. manage
69 $url_input = sanitize_text_field( $_SERVER['REQUEST_URI'] );
70
71 // Custom login slug is not part of the login URL typed into the browser
72 // e.g. https://www.example.com/wp-admin/ or https://www.example.com/wp-login.php
73 if ( ! is_user_logged_in() && ( false !== strpos( $url_input, 'wp-login.php' ) ) && ( false === strpos( $url_input, $custom_login_slug ) ) ) {
74
75 if ( 'success' != $interim_login ) {
76
77 wp_safe_redirect( home_url( 'not_found/' ), 302 );
78 exit();
79
80 }
81
82 }
83
84 }
85
86 /**
87 * Redirect to custom login URL on failed login
88 *
89 * @link https://plugins.trac.wordpress.org/browser/admin-login-url-change/trunk/admin-login-url-change.php#L148
90 * @since 1.4.0
91 */
92 public function redirect_to_custom_login_url_on_login_fail() {
93
94 global $asenha_limit_login;
95
96 $options = get_option( ASENHA_SLUG_U );
97 $custom_login_slug = $options['custom_login_slug'];
98
99 if ( isset( $asenha_limit_login ) && is_array( $asenha_limit_login ) && $asenha_limit_login['within_lockout_period'] ) {
100 // Do nothing. This prevents redirection loop.
101 } else {
102 // Append 'failed_login=true' so we can output custom error message above the login form
103 wp_safe_redirect( home_url( 'wp-login.php?' . $custom_login_slug . '&redirect=false&failed_login=true' ) );
104 exit();
105 }
106
107 }
108
109 /**
110 * Redirect to custom login URL on successful logout
111 *
112 * @link https://plugins.trac.wordpress.org/browser/admin-login-url-change/trunk/admin-login-url-change.php#L148
113 * @since 1.4.0
114 */
115 public function redirect_to_custom_login_url_on_logout_success() {
116
117 $options = get_option( ASENHA_SLUG_U );
118 $custom_login_slug = $options['custom_login_slug'];
119
120 // Redirect to the login URL with custom login slug in it
121 wp_safe_redirect( home_url( 'wp-login.php?' . $custom_login_slug . '&redirect=false' ) );
122 exit();
123
124 }
125
126 /**
127 * Add metabox to Appearance >> Menus page for the login logout menu items
128 *
129 * @since 3.4.0
130 */
131 public function add_login_logout_metabox() {
132 add_meta_box(
133 'add-login-logout',
134 'Log In / Log Out',
135 array( $this, 'add_login_logout_menu_items' ),
136 'nav-menus',
137 'side',
138 'default'
139 );
140 }
141
142 /**
143 * Add menu items for the login logout metabox
144 *
145 * @since 3.4.0
146 */
147 public function add_login_logout_menu_items() {
148
149 // The ID of the currently selected menu
150 global $nav_menu_selected_id;
151
152 $menu_items = array(
153 'asenha-login' => array(
154 'title' => 'Log In',
155 'url' => '#asenha-login',
156 'classes' => array( 'asenha-login-menu-item' ),
157 ),
158 'asenha-logout' => array(
159 'title' => 'Log Out',
160 'url' => '#asenha-logout',
161 'classes' => array( 'asenha-logout-menu-item' ),
162 ),
163 'asenha-login-logout' => array(
164 'title' => 'Log In / Log Out',
165 'url' => '#asenha-login-logout',
166 'classes' => array( 'asenha-login-logout-menu-item' ),
167 ),
168 );
169
170 $item_details = array(
171 'db_id' => 0,
172 'object' => 'asenha',
173 'object_id' => '',
174 'menu_item_parent' => 0,
175 'type' => 'custom',
176 'title' => '',
177 'url' => '',
178 'target' => '',
179 'attr_title' => '',
180 'classes' => array(),
181 'xfn' => '',
182 );
183
184 $menu_items_object = array();
185
186 foreach ( $menu_items as $item_id => $details ) {
187 $menu_items_object[ $details['title'] ] = (object) $item_details;
188 $menu_items_object[ $details['title'] ]->object_id = $item_id;
189 $menu_items_object[ $details['title'] ]->title = $details['title'];
190 $menu_items_object[ $details['title'] ]->classes = $details['classes'];
191 $menu_items_object[ $details['title'] ]->url = $details['url'];
192 }
193
194 $walker = new Walker_Nav_Menu_Checklist( array() );
195
196 ?>
197 <div id="login-logout-links" class="loginlinksdiv">
198 <div id="tabs-panel-login-logout-links-all" class="tabs-panel tabs-panel-view-all tabs-panel-active">
199 <ul id="login-logout-links-checklist" class="list:login-logout-links categorychecklist form-no-clear">
200 <?php echo walk_nav_menu_tree(
201 array_map( 'wp_setup_nav_menu_item', $menu_items_object ),
202 0,
203 (object) array( 'walker' => $walker)
204 ); ?>
205 </ul>
206 </div>
207 <p class="button-controls">
208 <span class="add-to-menu">
209 <input type="submit"<?php disabled( $nav_menu_selected_id, 0 ); ?> class="button-secondary submit-add-to-menu right" value="<?php echo esc_attr( 'Add to Menu' ); ?>" name="add-login-logout-links-menu-item" id="submit-login-logout-links" />
210 <span class="spinner"></span>
211 </span>
212 </p>
213 </div>
214 <?php
215
216 }
217
218 /**
219 * Setup login logout URL based on login state
220 *
221 * @since 3.4.0
222 */
223 public function set_login_logout_menu_item_dynamic_url( $menu_item ) {
224
225 global $pagenow;
226 $options = get_option( ASENHA_SLUG_U, array() );
227
228 if ( $pagenow != 'nav-menus.php' && !defined('DOING_AJAX') && isset( $menu_item->url ) && false !== strpos( $menu_item->url, 'asenha' ) ) {
229
230 // Define login URL based on whether
231 if ( array_key_exists( 'change_login_url', $options ) && $options['change_login_url'] ) {
232 if ( array_key_exists( 'custom_login_slug', $options ) && ! empty( $options['custom_login_slug'] ) ) {
233 $login_page_url = get_site_url() . '/' . $options['custom_login_slug'];
234 }
235 } else {
236 $login_page_url = wp_login_url();
237 }
238
239 $logout_redirect_url = home_url();
240
241 switch( $menu_item->url ) {
242 case '#asenha-login';
243 $menu_item->url = $login_page_url;
244 break;
245 case '#asenha-logout';
246 $menu_item->url = wp_logout_url();
247 break;
248 case '#asenha-login-logout';
249 $menu_item->url = ( is_user_logged_in() ) ? wp_logout_url() : $login_page_url;
250 $menu_item->title = ( is_user_logged_in() ) ? 'Log Out' : 'Log In';
251 break;
252 }
253
254 }
255
256 return $menu_item;
257
258 }
259
260 /**
261 * Conditionally remove login or logout menu item based on is_user_logged_in()
262 *
263 * @since 3.4.0
264 */
265 public function maybe_remove_login_or_logout_menu_item( $sorted_menu_items ) {
266
267 foreach( $sorted_menu_items as $menu => $item ) {
268
269 $item_classes = $item->classes;
270
271 // Maybe remove Log In menu item
272 if ( in_array( 'asenha-login-menu-item', $item_classes ) ) {
273 if ( is_user_logged_in() ) {
274 unset( $sorted_menu_items[$menu] );
275 }
276 }
277
278 // Maybe remove Log Out menu item
279 if ( in_array( 'asenha-logout-menu-item', $item_classes ) ) {
280 if ( ! is_user_logged_in() ) {
281 unset( $sorted_menu_items[$menu] );
282 }
283 }
284
285 }
286
287 return $sorted_menu_items;
288
289 }
290
291 /**
292 * Redirect to custom internal URL after login for user roles
293 *
294 * @param string $redirect_to_url URL to redirect to. Default is admin dashboard URL.
295 * @param string $origin_url URL the user is coming from.
296 * @param object $user logged-in user's data.
297 * @since 1.5.0
298 */
299 public function redirect_for_roles_after_login( $username, $user ) {
300
301 $options = get_option( ASENHA_SLUG_U, array() );
302 $redirect_after_login_to_slug = $options['redirect_after_login_to_slug'];
303 $redirect_after_login_for = $options['redirect_after_login_for'];
304
305 if ( isset( $redirect_after_login_for ) && ( count( $redirect_after_login_for ) > 0 ) ) {
306
307 // Assemble single-dimensional array of roles for which custom URL redirection should happen
308 $roles_for_custom_redirect = array();
309
310 foreach( $redirect_after_login_for as $role_slug => $custom_redirect ) {
311 if ( $custom_redirect ) {
312 $roles_for_custom_redirect[] = $role_slug;
313 }
314 }
315
316 // Does the user have roles data in array form?
317 if ( isset( $user->roles ) && is_array( $user->roles ) ) {
318
319 $current_user_roles = $user->roles;
320
321 }
322
323 // Set custom redirect URL for roles set in the settings. Otherwise, leave redirect URL to the default, i.e. admin dashboard.
324 foreach ( $current_user_roles as $role ) {
325 if ( in_array( $role, $roles_for_custom_redirect ) ) {
326
327 wp_safe_redirect( home_url( $redirect_after_login_to_slug . '/' ) );
328 exit();
329
330 }
331 }
332
333 }
334
335 }
336
337 /**
338 * Redirect to custom internal URL after login for user roles
339 *
340 * @param string $redirect_to_url URL to redirect to. Default is admin dashboard URL.
341 * @param string $origin_url URL the user is coming from.
342 * @param object $user logged-in user's data.
343 * @since 1.6.0
344 */
345 public function redirect_after_logout( $user_id ) {
346
347 $options = get_option( ASENHA_SLUG_U, array() );
348 $redirect_after_logout_to_slug = $options['redirect_after_logout_to_slug'];
349 $redirect_after_logout_for = $options['redirect_after_logout_for'];
350
351 $user = get_userdata( $user_id );
352
353 if ( isset( $redirect_after_logout_for ) && ( count( $redirect_after_logout_for ) > 0 ) ) {
354
355 // Assemble single-dimensional array of roles for which custom URL redirection should happen
356 $roles_for_custom_redirect = array();
357
358 foreach( $redirect_after_logout_for as $role_slug => $custom_redirect ) {
359 if ( $custom_redirect ) {
360 $roles_for_custom_redirect[] = $role_slug;
361 }
362 }
363
364 // Does the user have roles data in array form?
365 if ( isset( $user->roles ) && is_array( $user->roles ) ) {
366
367 $current_user_roles = $user->roles;
368
369 }
370
371 // Redirect for roles set in the settings. Otherwise, leave redirect URL to the default, i.e. admin dashboard.
372 foreach ( $current_user_roles as $role ) {
373 if ( in_array( $role, $roles_for_custom_redirect ) ) {
374
375 wp_safe_redirect( home_url( $redirect_after_logout_to_slug . '/' ) );
376 exit();
377
378 }
379 }
380
381 }
382
383 }
384
385 /**
386 * Log date time when a user last logged in successfully
387 *
388 * @since 3.6.0
389 */
390 public function log_login_datetime( $user_login ) {
391
392 $user = get_user_by( 'login', $user_login ); // by username
393 update_user_meta( $user->ID, 'asenha_last_login_on', time() );
394
395 }
396
397 /**
398 * Add Last Login column to users list table
399 *
400 * @since 3.6.0
401 */
402 public function add_last_login_column( $columns ) {
403
404 $columns['asenha_last_login'] = 'Last Login';
405 return $columns;
406
407 }
408
409 /**
410 * Show last login info in the last login column
411 *
412 * @since 3.6.0
413 */
414 public function show_last_login_info( $output, $column_name, $user_id ) {
415
416 if ( 'asenha_last_login' === $column_name ) {
417
418 if ( ! empty( get_user_meta( $user_id, 'asenha_last_login_on', true ) ) ) {
419
420 $last_login_unixtime = (int) get_user_meta( $user_id, 'asenha_last_login_on', true );
421
422 if ( function_exists( 'wp_date' ) ) {
423 $output = wp_date( 'M j, Y H:i', $last_login_unixtime );
424 } else {
425 $output = date_i18n( 'M j, Y H:i', $last_login_unixtime );
426 }
427
428 } else {
429
430 $output = 'No data yet';
431
432 }
433
434 }
435
436 return $output;
437
438 }
439
440 /**
441 * Add custom CSS for the Last Login column
442 *
443 * @since 3.6.0
444 */
445 public function add_column_style() {
446 ?>
447 <style>
448 .column-asenha_last_login {
449 width: 90px;
450 }
451 </style>
452 <?php
453 }
454
455 }