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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
449 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 $options = get_option( ASENHA_SLUG_U );
95 $custom_login_slug = $options['custom_login_slug'];
96
97 // Append 'failed_login=true' so we can output custom error message above the login form
98 wp_safe_redirect( home_url( 'wp-login.php?' . $custom_login_slug . '&redirect=false&failed_login=true' ) );
99 exit();
100
101 }
102
103 /**
104 * Redirect to custom login URL on successful logout
105 *
106 * @link https://plugins.trac.wordpress.org/browser/admin-login-url-change/trunk/admin-login-url-change.php#L148
107 * @since 1.4.0
108 */
109 public function redirect_to_custom_login_url_on_logout_success() {
110
111 $options = get_option( ASENHA_SLUG_U );
112 $custom_login_slug = $options['custom_login_slug'];
113
114 // Redirect to the login URL with custom login slug in it
115 wp_safe_redirect( home_url( 'wp-login.php?' . $custom_login_slug . '&redirect=false' ) );
116 exit();
117
118 }
119
120 /**
121 * Add metabox to Appearance >> Menus page for the login logout menu items
122 *
123 * @since 3.4.0
124 */
125 public function add_login_logout_metabox() {
126 add_meta_box(
127 'add-login-logout',
128 'Log In / Log Out',
129 array( $this, 'add_login_logout_menu_items' ),
130 'nav-menus',
131 'side',
132 'default'
133 );
134 }
135
136 /**
137 * Add menu items for the login logout metabox
138 *
139 * @since 3.4.0
140 */
141 public function add_login_logout_menu_items() {
142
143 // The ID of the currently selected menu
144 global $nav_menu_selected_id;
145
146 $menu_items = array(
147 'asenha-login' => array(
148 'title' => 'Log In',
149 'url' => '#asenha-login',
150 'classes' => array( 'asenha-login-menu-item' ),
151 ),
152 'asenha-logout' => array(
153 'title' => 'Log Out',
154 'url' => '#asenha-logout',
155 'classes' => array( 'asenha-logout-menu-item' ),
156 ),
157 'asenha-login-logout' => array(
158 'title' => 'Log In / Log Out',
159 'url' => '#asenha-login-logout',
160 'classes' => array( 'asenha-login-logout-menu-item' ),
161 ),
162 );
163
164 $item_details = array(
165 'db_id' => 0,
166 'object' => 'asenha',
167 'object_id' => '',
168 'menu_item_parent' => 0,
169 'type' => 'custom',
170 'title' => '',
171 'url' => '',
172 'target' => '',
173 'attr_title' => '',
174 'classes' => array(),
175 'xfn' => '',
176 );
177
178 $menu_items_object = array();
179
180 foreach ( $menu_items as $item_id => $details ) {
181 $menu_items_object[ $details['title'] ] = (object) $item_details;
182 $menu_items_object[ $details['title'] ]->object_id = $item_id;
183 $menu_items_object[ $details['title'] ]->title = $details['title'];
184 $menu_items_object[ $details['title'] ]->classes = $details['classes'];
185 $menu_items_object[ $details['title'] ]->url = $details['url'];
186 }
187
188 $walker = new Walker_Nav_Menu_Checklist( array() );
189
190 ?>
191 <div id="login-logout-links" class="loginlinksdiv">
192 <div id="tabs-panel-login-logout-links-all" class="tabs-panel tabs-panel-view-all tabs-panel-active">
193 <ul id="login-logout-links-checklist" class="list:login-logout-links categorychecklist form-no-clear">
194 <?php echo walk_nav_menu_tree(
195 array_map( 'wp_setup_nav_menu_item', $menu_items_object ),
196 0,
197 (object) array( 'walker' => $walker)
198 ); ?>
199 </ul>
200 </div>
201 <p class="button-controls">
202 <span class="add-to-menu">
203 <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" />
204 <span class="spinner"></span>
205 </span>
206 </p>
207 </div>
208 <?php
209
210 }
211
212 /**
213 * Setup login logout URL based on login state
214 *
215 * @since 3.4.0
216 */
217 public function set_login_logout_menu_item_dynamic_url( $menu_item ) {
218
219 global $pagenow;
220 $options = get_option( ASENHA_SLUG_U, array() );
221
222 if ( $pagenow != 'nav-menus.php' && !defined('DOING_AJAX') && isset( $menu_item->url ) && false !== strpos( $menu_item->url, 'asenha' ) ) {
223
224 // Define login URL based on whether
225 if ( array_key_exists( 'change_login_url', $options ) && $options['change_login_url'] ) {
226 if ( array_key_exists( 'custom_login_slug', $options ) && ! empty( $options['custom_login_slug'] ) ) {
227 $login_page_url = get_site_url() . '/' . $options['custom_login_slug'];
228 }
229 } else {
230 $login_page_url = wp_login_url();
231 }
232
233 $logout_redirect_url = home_url();
234
235 switch( $menu_item->url ) {
236 case '#asenha-login';
237 $menu_item->url = $login_page_url;
238 break;
239 case '#asenha-logout';
240 $menu_item->url = wp_logout_url();
241 break;
242 case '#asenha-login-logout';
243 $menu_item->url = ( is_user_logged_in() ) ? wp_logout_url() : $login_page_url;
244 $menu_item->title = ( is_user_logged_in() ) ? 'Log Out' : 'Log In';
245 break;
246 }
247
248 }
249
250 return $menu_item;
251
252 }
253
254 /**
255 * Conditionally remove login or logout menu item based on is_user_logged_in()
256 *
257 * @since 3.4.0
258 */
259 public function maybe_remove_login_or_logout_menu_item( $sorted_menu_items ) {
260
261 foreach( $sorted_menu_items as $menu => $item ) {
262
263 $item_classes = $item->classes;
264
265 // Maybe remove Log In menu item
266 if ( in_array( 'asenha-login-menu-item', $item_classes ) ) {
267 if ( is_user_logged_in() ) {
268 unset( $sorted_menu_items[$menu] );
269 }
270 }
271
272 // Maybe remove Log Out menu item
273 if ( in_array( 'asenha-logout-menu-item', $item_classes ) ) {
274 if ( ! is_user_logged_in() ) {
275 unset( $sorted_menu_items[$menu] );
276 }
277 }
278
279 }
280
281 return $sorted_menu_items;
282
283 }
284
285 /**
286 * Redirect to custom internal URL after login for user roles
287 *
288 * @param string $redirect_to_url URL to redirect to. Default is admin dashboard URL.
289 * @param string $origin_url URL the user is coming from.
290 * @param object $user logged-in user's data.
291 * @since 1.5.0
292 */
293 public function redirect_for_roles_after_login( $username, $user ) {
294
295 $options = get_option( ASENHA_SLUG_U, array() );
296 $redirect_after_login_to_slug = $options['redirect_after_login_to_slug'];
297 $redirect_after_login_for = $options['redirect_after_login_for'];
298
299 if ( isset( $redirect_after_login_for ) && ( count( $redirect_after_login_for ) > 0 ) ) {
300
301 // Assemble single-dimensional array of roles for which custom URL redirection should happen
302 $roles_for_custom_redirect = array();
303
304 foreach( $redirect_after_login_for as $role_slug => $custom_redirect ) {
305 if ( $custom_redirect ) {
306 $roles_for_custom_redirect[] = $role_slug;
307 }
308 }
309
310 // Does the user have roles data in array form?
311 if ( isset( $user->roles ) && is_array( $user->roles ) ) {
312
313 $current_user_roles = $user->roles;
314
315 }
316
317 // Set custom redirect URL for roles set in the settings. Otherwise, leave redirect URL to the default, i.e. admin dashboard.
318 foreach ( $current_user_roles as $role ) {
319 if ( in_array( $role, $roles_for_custom_redirect ) ) {
320
321 wp_safe_redirect( home_url( $redirect_after_login_to_slug . '/' ) );
322 exit();
323
324 }
325 }
326
327 }
328
329 }
330
331 /**
332 * Redirect to custom internal URL after login for user roles
333 *
334 * @param string $redirect_to_url URL to redirect to. Default is admin dashboard URL.
335 * @param string $origin_url URL the user is coming from.
336 * @param object $user logged-in user's data.
337 * @since 1.6.0
338 */
339 public function redirect_after_logout( $user_id ) {
340
341 $options = get_option( ASENHA_SLUG_U, array() );
342 $redirect_after_logout_to_slug = $options['redirect_after_logout_to_slug'];
343 $redirect_after_logout_for = $options['redirect_after_logout_for'];
344
345 $user = get_userdata( $user_id );
346
347 if ( isset( $redirect_after_logout_for ) && ( count( $redirect_after_logout_for ) > 0 ) ) {
348
349 // Assemble single-dimensional array of roles for which custom URL redirection should happen
350 $roles_for_custom_redirect = array();
351
352 foreach( $redirect_after_logout_for as $role_slug => $custom_redirect ) {
353 if ( $custom_redirect ) {
354 $roles_for_custom_redirect[] = $role_slug;
355 }
356 }
357
358 // Does the user have roles data in array form?
359 if ( isset( $user->roles ) && is_array( $user->roles ) ) {
360
361 $current_user_roles = $user->roles;
362
363 }
364
365 // Redirect for roles set in the settings. Otherwise, leave redirect URL to the default, i.e. admin dashboard.
366 foreach ( $current_user_roles as $role ) {
367 if ( in_array( $role, $roles_for_custom_redirect ) ) {
368
369 wp_safe_redirect( home_url( $redirect_after_logout_to_slug . '/' ) );
370 exit();
371
372 }
373 }
374
375 }
376
377 }
378
379 /**
380 * Log date time when a user last logged in successfully
381 *
382 * @since 3.6.0
383 */
384 public function log_login_datetime( $user_login ) {
385
386 $user = get_user_by( 'login', $user_login ); // by username
387 update_user_meta( $user->ID, 'asenha_last_login_on', time() );
388
389 }
390
391 /**
392 * Add Last Login column to users list table
393 *
394 * @since 3.6.0
395 */
396 public function add_last_login_column( $columns ) {
397
398 $columns['asenha_last_login'] = 'Last Login';
399 return $columns;
400
401 }
402
403 /**
404 * Show last login info in the last login column
405 *
406 * @since 3.6.0
407 */
408 public function show_last_login_info( $output, $column_name, $user_id ) {
409
410 if ( 'asenha_last_login' === $column_name ) {
411
412 if ( ! empty( get_user_meta( $user_id, 'asenha_last_login_on', true ) ) ) {
413
414 $last_login_unixtime = (int) get_user_meta( $user_id, 'asenha_last_login_on', true );
415
416 if ( function_exists( 'wp_date' ) ) {
417 $output = wp_date( 'M j, Y H:i', $last_login_unixtime );
418 } else {
419 $output = date_i18n( 'M j, Y H:i', $last_login_unixtime );
420 }
421
422 } else {
423
424 $output = 'No data yet';
425
426 }
427
428 }
429
430 return $output;
431
432 }
433
434 /**
435 * Add custom CSS for the Last Login column
436 *
437 * @since 3.6.0
438 */
439 public function add_column_style() {
440 ?>
441 <style>
442 .column-asenha_last_login {
443 width: 90px;
444 }
445 </style>
446 <?php
447 }
448
449 }