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