Skip to content

Commit

Permalink
Merge pull request #234 from newfold-labs/login-sso-redirect
Browse files Browse the repository at this point in the history
Split out redirect filters into separate methods
  • Loading branch information
earnjam authored May 4, 2023
2 parents c1c2b5e + 1222152 commit c9a53da
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
4 changes: 2 additions & 2 deletions includes/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public function __construct( Container $container ) {
// Reset the stored Compatibility Status every time WP Core is updated.
\add_action( '_core_updated_successfully', array( Status::class, 'reset' ) );

\add_filter( 'login_redirect', array( LoginRedirect::class, 'handle_redirect' ), 10 );
\add_filter( 'newfold_sso_success_url', array( LoginRedirect::class, 'handle_redirect' ), 10 );
\add_filter( 'login_redirect', array( LoginRedirect::class, 'wplogin' ), 10, 3 );
\add_filter( 'newfold_sso_success_url', array( LoginRedirect::class, 'sso' ), 10 );
\add_filter(
Options::get_option_name( 'redirect' ) . '_disable',
array( LoginRedirect::class, 'remove_handle_redirect_action' )
Expand Down
32 changes: 26 additions & 6 deletions includes/LoginRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,40 @@
*/
class LoginRedirect {
/**
* Handles the redirect to onboarding
* Redirect hook for SSO Logins
*
* @param string $original_redirect The requested redirect URL
* @return string The filtered url to redirect to
*/
public static function handle_redirect( $original_redirect ) {
// Current user not always available from wp_get_current_user(), so must reference out of the global
global $user;
// Loading the login screen, or login failures set $user as a WP_Error object.
// We should only override the redirect param if we have a valid logged in user
public static function sso( $original_redirect ) {
return self::filter_redirect( $original_redirect, wp_get_current_user() );
}

/**
* Redirect hook for direct WP Logins
*
* @param string $original_redirect The requested redirect URL
* @param string $requested_original_redirect The requested redirect URL from parameter
* @param WP_User|WP_Error $user The current logged in user or WP_Error on login failure
* @return string The filtered URL to redirect to
*/
public static function wplogin( $original_redirect, $requested_original_redirect, $user ) {
// wp-login.php runs this filter on load and login failures
// We should only do a redirect with a succesful user login
if ( ! ( $user instanceof \WP_User ) ) {
return $original_redirect;
}
return self::filter_redirect( $original_redirect, $user );
}

/**
* Evaluate whether the redirect should point to onboarding
*
* @param string $original_redirect The requested redirect URL
* @param WP_User $user The logged in user
* @return string The filtered url to redirect to
*/
public static function filter_redirect( $original_redirect, $user ) {
// Only admins should get the onboarding redirect
if ( ! user_can( $user, 'manage_options' ) ) {
return $original_redirect;
Expand Down

0 comments on commit c9a53da

Please sign in to comment.