Improve redirect handling
This commit is contained in:
@@ -16,24 +16,13 @@ final class Admin {
|
|||||||
use Singleton;
|
use Singleton;
|
||||||
|
|
||||||
protected function __construct() {
|
protected function __construct() {
|
||||||
add_action('wp', [$this, 'logout'], 1);
|
add_action('admin_init', [$this, 'maybe_logout'], 1);
|
||||||
add_action('wp', [$this, 'handle_redirect'], 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function logout():void {
|
public function maybe_logout():void {
|
||||||
if (!is_user_logged_in()) return;
|
if (!is_user_logged_in()) return;
|
||||||
wp_logout();
|
wp_logout();
|
||||||
$this->do_redirect();
|
Redirect::do_redirect();
|
||||||
}
|
|
||||||
|
|
||||||
public function handle_redirect():void {
|
|
||||||
if (is_front_page()) return;
|
|
||||||
$this->do_redirect();
|
|
||||||
}
|
|
||||||
|
|
||||||
private function do_redirect():void {
|
|
||||||
wp_redirect(get_site_url());
|
|
||||||
exit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
35
inc/class-redirect.php
Normal file
35
inc/class-redirect.php
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* @package ogre-suspension
|
||||||
|
* @author cleverogre
|
||||||
|
* @version 1.0.0
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Ogre\Suspension;
|
||||||
|
|
||||||
|
use Ogre\Singleton;
|
||||||
|
|
||||||
|
defined('ABSPATH') || exit;
|
||||||
|
|
||||||
|
final class Redirect {
|
||||||
|
use Singleton;
|
||||||
|
|
||||||
|
protected function __construct() {
|
||||||
|
add_action('wp', [$this, 'maybe_redirect'], 1);
|
||||||
|
add_action('login_init', [__CLASS__, 'do_redirect'], 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function maybe_redirect():void {
|
||||||
|
if (is_front_page()) return;
|
||||||
|
self::do_redirect();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function do_redirect():void {
|
||||||
|
wp_redirect(get_site_url());
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Redirect::instance();
|
||||||
@@ -17,7 +17,7 @@ final class Template {
|
|||||||
use Singleton;
|
use Singleton;
|
||||||
|
|
||||||
protected function __construct() {
|
protected function __construct() {
|
||||||
add_action('template_redirect', [$this, 'template_redirect']);
|
add_action('template_redirect', [$this, 'template_redirect'], 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function template_redirect():void {
|
public function template_redirect():void {
|
||||||
|
|||||||
@@ -23,8 +23,9 @@ final class Suspension extends Plugin {
|
|||||||
protected function __construct() {
|
protected function __construct() {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->add_files([
|
$this->add_files([
|
||||||
'inc/class-template.php',
|
'inc/class-redirect.php',
|
||||||
'inc/class-admin.php',
|
'inc/class-admin.php',
|
||||||
|
'inc/class-template.php',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user