0

I have this three functions in my custom plugin. I have created a custom front-end and I'm using an SSO plugin that will get the registered data from usermeta table to pass them to magento if an user is registered and have an approved account.

    public function role_login_redirect( $url, $request, $user )
    {        
        if( isset($user->roles) && is_array($user->roles) && in_array('operator', $user->roles) ){
            $url = site_url('/gestione-clienti');
        }elseif( isset($user->roles) && is_array($user->roles) && in_array('customer', $user->roles) ){
            $usermeta = get_user_meta( $user->ID );
            if( $usermeta['stato_account'][0] === 'pending' || $usermeta['stato_account'][0] === 'rejected' ){
                $url = site_url('/pagina-errore');
                //CHECK TODO debug per fix redirect loop
            }elseif( $usermeta['stato_account'] === 'approved' && is_null($usermeta['last_password_reset'][0]) || $usermeta['stato_account'] === 'approved' && $this->lastPasswordResetCheck($usermeta['last_password_reset'][0]) ){
                $url = site_url('/password-reset');
            }else {
                $url = site_url('/profilo-utente');
            }
        }
        return $url;   
    }
    
    /*
    *
    */
    public function role_logout_redirect()
    {
        $redirect_to = site_url('/login');
        if( wp_safe_redirect( $redirect_to ) ){
            return $redirect_to;
        }
    }

    /*
    *
    */
    public function logged_users_redirect()
    {
        $user = wp_get_current_user();
        $usermeta = get_user_meta( $user->ID );
        
        if( is_user_logged_in() && current_user_can('administrator') && is_page('login') ){
            wp_safe_redirect( admin_url() );
            exit;
        }
        
        if( is_user_logged_in() && is_array($user->roles) && in_array('operator', $user->roles) && is_page('login') ){
            wp_safe_redirect( site_url('/gestione-clienti') );
            exit;
        }
        
        if( is_user_logged_in() && is_array($user->roles) && in_array('customer', $user->roles) && is_page('login') ) {
            if( $usermeta['stato_account'][0] === 'pending' || $usermeta['stato_account'][0] === 'rejected' ){
                wp_safe_redirect( site_url('/pagina-errore') );
                exit;
            } else {
                wp_safe_redirect( site_url('/profilo-utente') );
                exit;
            }
        }

        if( is_user_logged_in() && is_array($user->roles) && in_array('customer', $user->roles) && is_page('profilo-utente') ){
            if( $usermeta['stato_account'][0] === 'approved' && is_null($usermeta['last_password_reset'][0]) ){
                wp_safe_redirect( site_url('/password-reset') );
                exit;
            }elseif( $usermeta['stato_account'][0] === 'approved' && $this->lastPasswordResetCheck($usermeta['last_password_reset'][0]) ){
                wp_safe_redirect( site_url('/password-reset') );
                exit;
            }
        }
        
        if( is_user_logged_in() && is_array($user->roles) && in_array('customer', $user->roles) && is_page('password-reset') ){
            if( $usermeta['stato_account'][0] === 'approved' && !is_null($usermeta['last_password_reset'][0]) && !$this->lastPasswordResetCheck($usermeta['last_password_reset'][0]) ){
                wp_safe_redirect( site_url('/profilo-utente') );
                exit;
            }elseif( $usermeta['stato_account'][0] === 'pending' || $usermeta['stato_account'][0] === 'rejected' ){
                wp_safe_redirect( site_url('/pagina-errore') );
                exit;
            }   
        }

        if( !is_user_logged_in() && is_page('profilo-utente') ){
            wp_safe_redirect( site_url('/login') );
            exit;
        } elseif( !is_user_logged_in() && is_page('gestione-clienti') ){
            wp_safe_redirect( site_url('/login') );
            exit;
        } elseif( !is_user_logged_in() && is_page('password-reset') ){
            wp_safe_redirect( site_url('/login') );
            exit;
        }

    }

I've noticed that after the login if a customer user insert the wp-admin into the address bar it will see the default wordpress dashboard and this isn't what I want, I want that the user is always redirected to "profilo-utente" page or password reset if the password is expired or is the first login. Is there any filter I can use to do the check into the plugin and redirect the user correctly?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.