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