@@ -37,6 +37,18 @@ public function alterLoginRedirectUrl($redirect_to, $intentRedirectTo, $user)
3737 return $ redirect_to ;
3838 }
3939
40+ // check if we have value from cookie _fls_redirect_to
41+ if (isset ($ _COOKIE ['_fls_redirect_to ' ]) && filter_var ($ _COOKIE ['_fls_redirect_to ' ], FILTER_VALIDATE_URL )) {
42+ $ redirect_to = sanitize_url ($ _COOKIE ['_fls_redirect_to ' ]);
43+ $ validatedRedirectUrl = Helper::getValidatedRedirectUrl ($ redirect_to , admin_url ());
44+ unset($ _COOKIE ['_fls_redirect_to ' ]);
45+ setcookie ('_fls_redirect_to ' , '' , time () - 3600 , COOKIEPATH , COOKIE_DOMAIN );
46+ if ($ validatedRedirectUrl == $ redirect_to ) {
47+ return $ redirect_to ;
48+ }
49+
50+ }
51+
4052 if (apply_filters ('fluent_auth/respect_front_login_url ' , true ) && strpos ($ redirect_to , '/wp-admin ' ) === false ) {
4153 // it's a frontend redirect url
4254 } else if ($ url = $ this ->getDefaultLoginRedirectUrl ($ user )) {
@@ -74,6 +86,11 @@ public function loginForm($attributes, $headerContent = '')
7486 $ message = apply_filters ('fluent_auth/already_logged_in_message ' ,
7587 sprintf (__ ('You are already logged in. <a href="%s">Go to Home Page</a> ' , 'fluent-security ' ), site_url ())
7688 );
89+
90+ if (!$ message ) {
91+ return '' ;
92+ }
93+
7794 return '<p> ' . $ message . '</p> ' ;
7895 }
7996
@@ -88,9 +105,11 @@ public function loginForm($attributes, $headerContent = '')
88105 }
89106
90107 $ redirect = '' ;
108+ $ requestedRedirectTo = '' ;
91109
92110 if (!empty ($ attributes ['redirect_to ' ]) && filter_var ($ attributes ['redirect_to ' ], FILTER_VALIDATE_URL )) {
93111 $ redirect = $ attributes ['redirect_to ' ];
112+ $ requestedRedirectTo = $ redirect ;
94113 add_filter ('fluent_auth/social_redirect_to ' , function ($ url ) use ($ redirect ) {
95114 return $ redirect ;
96115 });
@@ -104,11 +123,12 @@ public function loginForm($attributes, $headerContent = '')
104123 * @param array $loginArgs
105124 */
106125 $ loginArgs = apply_filters ('fluent_auth/login_form_args ' , [
107- 'echo ' => false ,
108- 'redirect ' => $ redirect ,
109- 'remember ' => true ,
110- 'value_remember ' => true ,
111- 'action_url ' => site_url ('/ ' )
126+ 'echo ' => false ,
127+ 'redirect ' => $ redirect ,
128+ 'force_redirect_to ' => $ requestedRedirectTo ,
129+ 'remember ' => true ,
130+ 'value_remember ' => true ,
131+ 'action_url ' => site_url ('/ ' )
112132 ]);
113133
114134 $ return .= $ this ->nativeLoginForm ($ loginArgs );
@@ -672,6 +692,11 @@ public function handleLoginAjax()
672692 }
673693
674694 $ data = $ _REQUEST ;
695+ if (empty ($ _REQUEST ['_is_fls_form ' ])) {
696+ wp_send_json ([
697+ 'message ' => __ ('Invalid request ' , 'fluent-security ' )
698+ ], 422 );
699+ }
675700
676701 if (empty ($ data ['pwd ' ]) || empty ($ data ['log ' ])) {
677702 wp_send_json ([
@@ -682,13 +707,20 @@ public function handleLoginAjax()
682707 $ redirectUrl = admin_url ();
683708 if (isset ($ data ['redirect_to ' ]) && filter_var ($ data ['redirect_to ' ], FILTER_VALIDATE_URL )) {
684709 $ userRedirect = sanitize_url ($ data ['redirect_to ' ]);
685- $ redirectUrl = wp_validate_redirect ($ userRedirect , $ redirectUrl );
710+ $ redirectUrl = Helper::getValidatedRedirectUrl ($ userRedirect , $ redirectUrl );
711+ }
712+
713+ $ isForced = false ;
714+ if (!empty ($ data ['force_redirect_to ' ]) && filter_var ($ data ['force_redirect_to ' ], FILTER_VALIDATE_URL )) {
715+ $ redirectUrl = Helper::getValidatedRedirectUrl (sanitize_url ($ data ['force_redirect_to ' ]), $ redirectUrl );
716+ $ isForced = true ;
686717 }
687718
688719 if ($ currentUserId = get_current_user_id ()) { // user already logged in
689720 $ user = get_user_by ('ID ' , $ currentUserId );
690- $ redirectUrl = apply_filters ('login_redirect ' , $ redirectUrl , false , $ user );
691-
721+ if (!$ isForced ) {
722+ $ redirectUrl = apply_filters ('login_redirect ' , $ redirectUrl , false , $ user );
723+ }
692724 wp_send_json ([
693725 'redirect ' => $ redirectUrl
694726 ], 200 );
@@ -712,15 +744,21 @@ public function handleLoginAjax()
712744 ], 422 );
713745 }
714746
747+
715748 $ user = wp_signon ();
749+
716750 if (is_wp_error ($ user )) {
717751 wp_send_json ([
718752 'message ' => $ user ->get_error_message ()
719753 ], 422 );
720754 }
721755
722- $ filteredRedirectUrl = apply_filters ('login_redirect ' , $ redirectUrl , false , $ user );
723- $ filteredRedirectUrl = apply_filters ('fluent_auth/login_redirect_url ' , $ filteredRedirectUrl , $ user , $ _REQUEST );
756+ if (!$ isForced ) {
757+ $ filteredRedirectUrl = apply_filters ('login_redirect ' , $ redirectUrl , false , $ user );
758+ $ filteredRedirectUrl = apply_filters ('fluent_auth/login_redirect_url ' , $ filteredRedirectUrl , $ user , $ _REQUEST );
759+ } else {
760+ $ filteredRedirectUrl = $ redirectUrl ;
761+ }
724762
725763 wp_send_json ([
726764 'redirect ' => $ filteredRedirectUrl
@@ -851,7 +889,7 @@ public function handleSignupAjax()
851889 if ($ isAutoLogin ) {
852890 $ this ->login ($ userId );
853891 $ redirectUrl = Arr::get ($ formData , 'redirect_to ' , admin_url ());
854- $ redirectUrl = wp_validate_redirect ($ redirectUrl , admin_url ());
892+ $ redirectUrl = Helper:: getValidatedRedirectUrl ($ redirectUrl , admin_url ());
855893 $ redirectUrl = apply_filters ('login_redirect ' , $ redirectUrl , false , $ user );
856894 $ redirectUrl = apply_filters ('fluent_auth/login_redirect_url ' , $ redirectUrl , $ user , $ formData );
857895 $ message = __ ('Successfully registered to the site. ' , 'fluent-security ' );
@@ -1092,6 +1130,10 @@ protected function nativeLoginForm($args = array())
10921130 $ actionUrl = esc_url ($ args ['action_url ' ]);
10931131 }
10941132
1133+ if (!empty ($ args ['force_redirect_to ' ])) {
1134+ $ login_form_top .= '<input type="hidden" name="force_redirect_to" value=" ' . esc_url ($ args ['force_redirect_to ' ]) . '" /> ' ;
1135+ }
1136+
10951137 $ form = \sprintf (
10961138 '<form name="%1$s" id="%1$s" action="%2$s" method="post"> ' ,
10971139 esc_attr ($ args ['form_id ' ]),
0 commit comments