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