Skip to content

Commit

Permalink
Custom Login: only run code when needed
Browse files Browse the repository at this point in the history
* No need to process login redirect on rest requests

* Avoid loading SSO settings when the user is logged in

* fix logging out without sso enabled.
  • Loading branch information
corsacca authored Dec 13, 2023
1 parent d4a138c commit 929704f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
25 changes: 18 additions & 7 deletions dt-login/login-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,39 @@
// LOGIN PAGE REDIRECT
add_action( 'init', 'dt_login_redirect_login_page' );
function dt_login_redirect_login_page() {
if ( dt_is_rest() ) {
return;
}

$login_page_enabled = DT_Login_Fields::get( 'login_enabled' ) === 'on';

if ( isset( $_SERVER['REQUEST_URI'] ) && !empty( $_SERVER['REQUEST_URI'] ) ) {
$page_viewed = substr( basename( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 0, 12 );
$parsed_request_uri = ( new DT_URL( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) )->parsed_url;
$page_viewed = ltrim( $parsed_request_uri['path'], '/' );

//this section only applies to the login pages (ones that have the action query param)
if ( $page_viewed !== 'wp-login.php' && !isset( $_GET['action'] ) ){
return;
}

if ( $page_viewed == 'wp-login.php' && isset( $_GET['action'] ) && $_GET['action'] === 'register' && !dt_can_users_register() ) {
wp_redirect( wp_login_url() );
exit;
}

$login_page_enabled = DT_Login_Fields::get( 'login_enabled' ) === 'on';
if ( !$login_page_enabled ) {
return;
}

if ( is_user_logged_in() ){
if ( $page_viewed == 'wp-login.php' && isset( $_GET['action'] ) && $_GET['action'] === 'logout' ) {
wp_redirect( dt_login_url( 'logout' ) );
exit;
} else {
return;
}
}

//if ( $page_viewed == 'wp-login.php' && isset( $_GET['action'] ) && $_GET['action'] === 'rp' ) {
// return;
//}
Expand All @@ -32,11 +48,6 @@ function dt_login_redirect_login_page() {
exit;
}

if ( $page_viewed == 'wp-login.php' && isset( $_GET['action'] ) && $_GET['action'] === 'logout' ) {
wp_redirect( dt_login_url( 'logout' ) );
exit;
}

$login_url = DT_Login_Fields::get( 'login_url' );
$login_url = apply_filters( 'dt_login_url', $login_url );
if ( $page_viewed == $login_url && isset( $_GET['action'] ) && $_GET['action'] === 'register' && !dt_can_users_register() ) {
Expand Down
4 changes: 4 additions & 0 deletions dt-login/login-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public function __construct() {
parent::__construct();

$url = dt_get_url_path();
if ( is_user_logged_in() && strpos( $url, 'action' ) === false ) {
return;
}

$login_page_enabled = DT_Login_Fields::get( 'login_enabled' ) === 'on';

$login_url = DT_Login_Fields::get( 'login_url' );
Expand Down

0 comments on commit 929704f

Please sign in to comment.