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 | } |