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