Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| LoginRedirect | |
0.00% |
0 / 34 |
|
0.00% |
0 / 7 |
342 | |
0.00% |
0 / 1 |
| sso | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| wplogin | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| filter_redirect | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
72 | |||
| disable_redirect | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| enable_redirect | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| remove_handle_redirect_action | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| handle_redirect_param | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | namespace NewfoldLabs\WP\Module\Onboarding; |
| 3 | |
| 4 | use NewfoldLabs\WP\Module\Onboarding\Data\Data; |
| 5 | use NewfoldLabs\WP\Module\Onboarding\Data\Options; |
| 6 | |
| 7 | use function WP_Forge\Helpers\dataGet; |
| 8 | |
| 9 | /** |
| 10 | * Contains functionalities that redirect users to onboarding upon logging into WordPress. |
| 11 | */ |
| 12 | class LoginRedirect { |
| 13 | /** |
| 14 | * Redirect hook for Single Sign-On (SSO) logins. |
| 15 | * |
| 16 | * @param string $original_redirect The requested redirect URL. |
| 17 | * @return string The filtered URL to redirect to. |
| 18 | */ |
| 19 | public static function sso( $original_redirect ) { |
| 20 | // Handle the redirect to onboarding query parameter. |
| 21 | self::handle_redirect_param(); |
| 22 | return self::filter_redirect( $original_redirect, wp_get_current_user() ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Redirect hook for direct WordPress logins. |
| 27 | * |
| 28 | * @param string $original_redirect The requested redirect URL. |
| 29 | * @param string $requested_original_redirect The requested redirect URL from the parameter. |
| 30 | * @param WP_User|WP_Error $user The current logged-in user or WP_Error on login failure. |
| 31 | * @return string The filtered URL to redirect to. |
| 32 | */ |
| 33 | public static function wplogin( $original_redirect, $requested_original_redirect, $user ) { |
| 34 | // Handle the redirect to onboarding query parameter. |
| 35 | self::handle_redirect_param(); |
| 36 | |
| 37 | // wp-login.php runs this filter upon loading and during login failures. |
| 38 | // We should perform a redirect only upon a successful user login. |
| 39 | if ( ! ( $user instanceof \WP_User ) ) { |
| 40 | return $original_redirect; |
| 41 | } |
| 42 | return self::filter_redirect( $original_redirect, $user ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Evaluate whether the redirect should point to onboarding. |
| 47 | * |
| 48 | * @param string $original_redirect The requested redirect URL. |
| 49 | * @param WP_User $user The logged in user. |
| 50 | * @return string The filtered URL to redirect to. |
| 51 | */ |
| 52 | public static function filter_redirect( $original_redirect, $user ) { |
| 53 | // Only administrators should receive the onboarding redirect. |
| 54 | if ( ! user_can( $user, 'manage_options' ) ) { |
| 55 | return $original_redirect; |
| 56 | } |
| 57 | |
| 58 | // Handle the redirect to onboarding WordPress option. |
| 59 | $redirect_option_name = Options::get_option_name( 'redirect' ); |
| 60 | $redirect_option = get_option( $redirect_option_name ); |
| 61 | if ( '0' === $redirect_option ) { |
| 62 | return $original_redirect; |
| 63 | } elseif ( '1' === $redirect_option ) { |
| 64 | return admin_url( '/index.php?page=' . WP_Admin::$slug ); |
| 65 | } |
| 66 | |
| 67 | // Don't redirect to onboarding if onboarding was exited or completed. |
| 68 | $flow_data = get_option( Options::get_option_name( 'flow' ), false ); |
| 69 | if ( dataGet( $flow_data, 'hasExited' ) || dataGet( $flow_data, 'isComplete' ) ) { |
| 70 | return $original_redirect; |
| 71 | } |
| 72 | |
| 73 | // Don't redirect to onboarding if the site is not a fresh installation. |
| 74 | if ( false === Data::is_fresh_installation() ) { |
| 75 | return $original_redirect; |
| 76 | } |
| 77 | |
| 78 | // Don't redirect to onboarding if the 'coming_soon' mode is off. The user has launched their site. |
| 79 | if ( ! Data::coming_soon() ) { |
| 80 | return $original_redirect; |
| 81 | } |
| 82 | |
| 83 | // Redirect to onboarding |
| 84 | return admin_url( '/index.php?page=' . WP_Admin::$slug ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Sets an option that disables the redirect to onboarding on login. |
| 89 | * |
| 90 | * @return boolean |
| 91 | */ |
| 92 | public static function disable_redirect() { |
| 93 | return update_option( Options::get_option_name( 'redirect' ), '0' ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Sets an option that enables the redirect to onboarding on login. |
| 98 | * |
| 99 | * @return boolean |
| 100 | */ |
| 101 | public static function enable_redirect() { |
| 102 | return update_option( Options::get_option_name( 'redirect' ), '1' ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Removes the onboarding login redirect action. |
| 107 | * |
| 108 | * @return boolean |
| 109 | */ |
| 110 | public static function remove_handle_redirect_action() { |
| 111 | return remove_action( 'login_redirect', array( __CLASS__, 'handle_redirect' ) ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Sets a WordPress option corresponding to the redirect parameter value. |
| 116 | * |
| 117 | * @return boolean |
| 118 | */ |
| 119 | private static function handle_redirect_param() { |
| 120 | $redirect_option_name = Options::get_option_name( 'redirect' ); |
| 121 | if ( ! isset( $_GET[ $redirect_option_name ] ) ) { |
| 122 | return false; |
| 123 | } |
| 124 | switch ( $_GET[ $redirect_option_name ] ) { |
| 125 | case 'true': |
| 126 | return self::enable_redirect(); |
| 127 | case 'false': |
| 128 | return self::disable_redirect(); |
| 129 | } |
| 130 | |
| 131 | return false; |
| 132 | } |
| 133 | } |