plugin = $plugin; } private function slug() { $settings = $this->plugin->get_settings(); if ((defined('A360_CUSTOM_LOGIN_DISABLED') && A360_CUSTOM_LOGIN_DISABLED) || is_multisite() || ! get_option('permalink_structure') || empty($settings['custom_login_enabled'])) { return ''; } return self::validate_slug($settings['custom_login_slug'] ?? ''); } public static function validate_slug($value) { if (! is_string($value)) { return ''; } $slug = sanitize_title($value); $reserved = array('wp-admin', 'wp-login.php', 'wp-json', 'wp-content', 'wp-includes', 'admin', 'login', 'feed', 'robots.txt', 'xmlrpc.php', 'index.php', 'wp-signup.php', 'wp-activate.php'); if (! $slug || in_array($slug, array_map('sanitize_title', $reserved), true) || get_page_by_path($slug, OBJECT, array('page', 'post', 'attachment'))) { return ''; } return $slug; } private function url() { return home_url('/' . $this->slug() . '/'); } public function init() { if (! $this->slug()) { return; } // Run before wp-admin/admin.php calls auth_redirect(). admin_init is too late. add_action('init', array($this, 'block_guest_admin'), 0); // Dispatch before WordPress queries content or applies canonical redirects. add_action('parse_request', array($this, 'route'), 0); add_filter('wp_redirect', array($this, 'filter_register_url')); add_action( 'login_init', array($this, 'block_default_login'), 1 ); add_filter( 'site_url', array($this, 'filter_site_url'), 10, 4 ); add_filter( 'network_site_url', array($this, 'filter_network_site_url'), 10, 3 ); add_filter( 'login_url', array($this, 'filter_login_url'), 10, 3 ); add_filter( 'logout_url', array($this, 'filter_logout_url'), 10, 2 ); add_filter( 'lostpassword_url', array($this, 'filter_lostpassword_url'), 10, 2 ); add_filter( 'register_url', array($this, 'filter_register_url') ); } private function requested_path() { $request = isset($_SERVER['REQUEST_URI']) ? wp_unslash($_SERVER['REQUEST_URI']) : ''; return trim( (string) wp_parse_url($request, PHP_URL_PATH), '/' ); } private function custom_path() { return trim( (string) wp_parse_url($this->url(), PHP_URL_PATH), '/' ); } public function route() { if ($this->requested_path() !== $this->custom_path()) { return; } if (defined('A360_CUSTOM_LOGIN_ENTRY')) { return; } define('A360_CUSTOM_LOGIN_ENTRY', true); global $error, $interim_login, $action, $user_login, $pagenow; $pagenow = 'wp-login.php'; status_header(200); nocache_headers(); require ABSPATH . 'wp-login.php'; exit; } public function block_guest_admin() { if ((defined('WP_CLI') && WP_CLI) || ! is_admin() || is_user_logged_in()) { return; } global $pagenow; // These entry scripts serve public frontend requests. Core and registered // handlers still enforce their own action, capability and nonce checks. if (in_array($pagenow, array('admin-ajax.php', 'admin-post.php'), true)) { return; } status_header(404); nocache_headers(); exit; } public function block_default_login() { if ( defined('A360_CUSTOM_LOGIN_ENTRY') && A360_CUSTOM_LOGIN_ENTRY ) { return; } // WordPress recovery links and protected-post passwords retain core handling. $action = isset($_REQUEST['action']) && is_string($_REQUEST['action']) ? wp_unslash($_REQUEST['action']) : ''; if (in_array($action, array('postpass', 'enter_recovery_mode'), true)) { return; } status_header(404); nocache_headers(); exit; } private function rewrite_url($url) { if (! is_string($url) || ! $this->slug()) { return $url; } $parts = wp_parse_url($url); if (! is_array($parts) || empty($parts['path'])) { return $url; } if (! preg_match( '~(?:^|/)wp-login\.php$~i', $parts['path'] )) { return $url; } $home_host = wp_parse_url( home_url(), PHP_URL_HOST ); if ( ! empty($parts['host']) && strcasecmp( $parts['host'], (string) $home_host ) !== 0 ) { return $url; } $result = $this->url(); if (! empty($parts['scheme'])) { $result = set_url_scheme($result, $parts['scheme']); } if (isset($parts['query'])) { $result .= '?' . $parts['query']; } if (isset($parts['fragment'])) { $result .= '#' . $parts['fragment']; } return $result; } public function filter_site_url( $url, $path, $scheme, $blog_id ) { return $this->rewrite_url($url); } public function filter_network_site_url( $url, $path, $scheme ) { return $this->rewrite_url($url); } public function filter_login_url( $url, $redirect, $force_reauth ) { return $this->rewrite_url($url); } public function filter_logout_url($url, $redirect) { return $this->rewrite_url($url); } public function filter_lostpassword_url($url, $redirect) { return $this->rewrite_url($url); } public function filter_register_url($url) { return $this->rewrite_url($url); } }