class-redirect-after-login.php
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | /** |
| 6 | * Class for Redirect After Login module |
| 7 | * |
| 8 | * @since 6.9.5 |
| 9 | */ |
| 10 | class Redirect_After_Login { |
| 11 | /** |
| 12 | * Redirect to custom internal URL after login for user roles |
| 13 | * |
| 14 | * @param string $username Username. |
| 15 | * @param object $user Logged-in user's data. |
| 16 | * @since 1.5.0 |
| 17 | */ |
| 18 | public function redirect_after_login( $username, $user ) { |
| 19 | $redirect_url = $this->get_redirect_url_for_user( $user ); |
| 20 | if ( !empty( $redirect_url ) ) { |
| 21 | wp_safe_redirect( $redirect_url ); |
| 22 | exit; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Apply custom redirect URL after login completes (e.g. after 2FA validation). |
| 28 | * |
| 29 | * @param string $redirect_to The redirect destination URL. |
| 30 | * @param string $requested_redirect_to The requested redirect destination URL passed as a parameter. |
| 31 | * @param WP_User|object $user WP_User object if login was successful. |
| 32 | * @return string |
| 33 | * @since 8.2.4 |
| 34 | */ |
| 35 | public function filter_login_redirect( $redirect_to, $requested_redirect_to, $user ) { |
| 36 | $custom_redirect_url = $this->get_redirect_url_for_user( $user ); |
| 37 | if ( !empty( $custom_redirect_url ) ) { |
| 38 | return $custom_redirect_url; |
| 39 | } |
| 40 | return $redirect_to; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Redirect all applicable user roles to a single URL |
| 45 | * |
| 46 | * @param string $username Username. |
| 47 | * @param object $user Logged-in user's data. |
| 48 | * @since 7.3.3 |
| 49 | */ |
| 50 | public function redirect_to_single_url( $username, $user ) { |
| 51 | $redirect_url = $this->get_redirect_url_for_user( $user ); |
| 52 | if ( !empty( $redirect_url ) ) { |
| 53 | wp_safe_redirect( $redirect_url ); |
| 54 | exit; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get the custom redirect URL for a user based on module settings and role. |
| 60 | * |
| 61 | * @param WP_User|object $user Logged-in user's data. |
| 62 | * @return string Custom redirect URL, or empty string if no redirect applies. |
| 63 | * @since 8.2.4 |
| 64 | */ |
| 65 | public function get_redirect_url_for_user( $user ) { |
| 66 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 67 | if ( !isset( $user->roles ) || !is_array( $user->roles ) ) { |
| 68 | return ''; |
| 69 | } |
| 70 | $current_user_roles = $user->roles; |
| 71 | $redirect_after_login_to_slug_raw = ( isset( $options['redirect_after_login_to_slug'] ) ? $options['redirect_after_login_to_slug'] : '' ); |
| 72 | $relative_path = $this->get_redirect_relative_path( $redirect_after_login_to_slug_raw ); |
| 73 | $redirect_after_login_for = ( isset( $options['redirect_after_login_for'] ) ? $options['redirect_after_login_for'] : array() ); |
| 74 | if ( !isset( $redirect_after_login_for ) || count( $redirect_after_login_for ) <= 0 ) { |
| 75 | return ''; |
| 76 | } |
| 77 | $roles_for_custom_redirect = array(); |
| 78 | foreach ( $redirect_after_login_for as $role_slug => $custom_redirect ) { |
| 79 | if ( $custom_redirect ) { |
| 80 | $roles_for_custom_redirect[] = $role_slug; |
| 81 | } |
| 82 | } |
| 83 | foreach ( $current_user_roles as $role ) { |
| 84 | if ( in_array( $role, $roles_for_custom_redirect, true ) ) { |
| 85 | return home_url( $relative_path ); |
| 86 | } |
| 87 | } |
| 88 | return ''; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get the relative path to redirect to based on the raw redirect slug |
| 93 | * |
| 94 | * @since 7.3.3 |
| 95 | */ |
| 96 | public function get_redirect_relative_path( $redirect_after_login_to_slug_raw ) { |
| 97 | if ( !empty( $redirect_after_login_to_slug_raw ) ) { |
| 98 | $redirect_after_login_to_slug = trim( trim( $redirect_after_login_to_slug_raw ), '/' ); |
| 99 | if ( false !== strpos( $redirect_after_login_to_slug, '#' ) || false !== strpos( $redirect_after_login_to_slug, '?' ) || false !== strpos( $redirect_after_login_to_slug, '.php' ) || false !== strpos( $redirect_after_login_to_slug, '.html' ) ) { |
| 100 | $slug_suffix = ''; |
| 101 | } else { |
| 102 | $slug_suffix = '/'; |
| 103 | } |
| 104 | $relative_path = $redirect_after_login_to_slug . $slug_suffix; |
| 105 | } else { |
| 106 | $relative_path = ''; |
| 107 | } |
| 108 | return $relative_path; |
| 109 | } |
| 110 | |
| 111 | } |
| 112 |