From e6deec4b9af9ef61253281b603bd0690e4339384 Mon Sep 17 00:00:00 2001 From: William Earnhardt Date: Thu, 4 May 2023 11:29:33 -0400 Subject: [PATCH 1/3] Split out filters into separate methods --- includes/Application.php | 4 ++-- includes/LoginRedirect.php | 32 ++++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/includes/Application.php b/includes/Application.php index 9561cccea..598d9238c 100644 --- a/includes/Application.php +++ b/includes/Application.php @@ -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' ) diff --git a/includes/LoginRedirect.php b/includes/LoginRedirect.php index 31129db8e..50e6a7fee 100644 --- a/includes/LoginRedirect.php +++ b/includes/LoginRedirect.php @@ -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 + * @param string $requested_original_redirect + * @param WP_User|WP_Error $user + * @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; From 28a60931760e60371a8c612b8dd7e9ee96fed311 Mon Sep 17 00:00:00 2001 From: William Earnhardt Date: Thu, 4 May 2023 11:50:18 -0400 Subject: [PATCH 2/3] Fix whitespace --- includes/LoginRedirect.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/includes/LoginRedirect.php b/includes/LoginRedirect.php index 50e6a7fee..f93f9739c 100644 --- a/includes/LoginRedirect.php +++ b/includes/LoginRedirect.php @@ -14,32 +14,32 @@ class LoginRedirect { * @param string $original_redirect The requested redirect URL * @return string The filtered url to redirect to */ - public static function sso( $original_redirect ) { - return self::filter_redirect( $original_redirect, wp_get_current_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 - * @param string $requested_original_redirect - * @param WP_User|WP_Error $user + * @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 ) { + 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 ); - } + 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 + * @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 ) { From 122215206b3915818ae256e8a447bf78c1648745 Mon Sep 17 00:00:00 2001 From: William Earnhardt Date: Thu, 4 May 2023 11:51:55 -0400 Subject: [PATCH 3/3] Fix whitespace in docblock --- includes/LoginRedirect.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/LoginRedirect.php b/includes/LoginRedirect.php index f93f9739c..e69c7d76b 100644 --- a/includes/LoginRedirect.php +++ b/includes/LoginRedirect.php @@ -21,9 +21,9 @@ public static function sso( $original_redirect ) { /** * Redirect hook for direct WP Logins * - * @param string $original_redirect The requested redirect URL + * @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 + * @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 ) {