'string', 'sanitize_callback' => 'sanitize_title', 'default' => 'secure-login', ] ); register_setting( 'adlhpti_settings_group', 'adlhpti_enabled', [ 'type' => 'boolean', 'sanitize_callback' => 'rest_sanitize_boolean', 'default' => false, ] ); } public function adlhpti_render_settings_page() { $slug = get_option('adlhpti_custom_login_slug', 'secure-login'); $custom_login_url = site_url($slug); // No nonce for public URL $template = plugin_dir_path(__FILE__) . '../templates/admin-template.php'; if (file_exists($template)) { include $template; } else { echo '
' . esc_html__('Settings page template not found.', 'admin-login-hide-pti') . '
'; } } public function adlhpti_admin_enqueue_assets($hook) { if ($hook !== 'settings_page_adlhpti-settings') { return; } $base_url = plugin_dir_url(__FILE__) . '../assets/'; $base_path = plugin_dir_path(__FILE__) . '../assets/'; $css_path = $base_path . 'css/admin.css'; if (file_exists($css_path)) { wp_enqueue_style( 'adlhpti-admin-css', $base_url . 'css/admin.css', [], filemtime($css_path) ); } $js_path = $base_path . 'js/admin.js'; if (file_exists($js_path)) { wp_enqueue_script( 'adlhpti-admin-js', $base_url . 'js/admin.js', ['jquery'], filemtime($js_path), true ); } } public function adlhpti_custom_login_redirect() { $is_enabled = get_option('adlhpti_enabled', false); if (!$is_enabled) { return; } $custom_slug = trim(get_option('adlhpti_custom_login_slug', 'secure-login'), '/'); $request_uri_raw = isset($_SERVER['REQUEST_URI']) ? sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])) : ''; $parsed_request_uri = wp_parse_url($request_uri_raw); $request_path = isset($parsed_request_uri['path']) ? trim($parsed_request_uri['path'], '/') : ''; $parsed_site_url = wp_parse_url(site_url()); $site_path = isset($parsed_site_url['path']) ? trim($parsed_site_url['path'], '/') : ''; if (!empty($site_path) && strpos($request_path, $site_path) === 0) { $request_path = trim(substr($request_path, strlen($site_path)), '/'); } // Prevent direct wp-login.php unless POST or valid action if (strpos($request_path, 'wp-login.php') === 0) { $allowed_actions = ['logout', 'lostpassword', 'rp', 'resetpass', 'register']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only use of $_GET for display logic, not modifying data $action = isset($_GET['action']) ? sanitize_text_field(wp_unslash($_GET['action'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only use of $_GET for display logic $logged_out = isset($_GET['loggedout']) ? sanitize_text_field(wp_unslash($_GET['loggedout'])) : ''; $method = strtoupper(sanitize_text_field(wp_unslash($_SERVER['REQUEST_METHOD'] ?? ''))); if ($method !== 'POST' && !in_array($action, $allowed_actions, true) && !$logged_out) { wp_die( esc_html__('Access denied. Please use the custom login URL.', 'admin-login-hide-pti'), '', ['response' => 403] ); } if ($method === 'GET' && in_array($action, ['lostpassword', 'rp', 'resetpass'], true)) { $redirect_url = site_url($custom_slug); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only use of $_GET for redirection logic. $redirect_url = add_query_arg($_GET, $redirect_url); wp_safe_redirect($redirect_url); exit; } } if ($request_path === $custom_slug) { if ($_SERVER['REQUEST_METHOD'] === 'POST') { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is verified for custom actions (lostpassword, resetpass) below; default login is handled by WordPress core. $post_action = isset($_GET['action']) ? sanitize_text_field(wp_unslash($_GET['action'])) : 'login'; if ($post_action === 'lostpassword' || $post_action === 'retrievepassword') { if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_key(wp_unslash($_POST['_wpnonce'])), 'adlhpti_lostpassword_nonce')) { wp_die(esc_html__('Nonce verification failed.', 'admin-login-hide-pti')); } $errors = retrieve_password(); if (is_wp_error($errors)) { wp_safe_redirect(add_query_arg('error', $errors->get_error_code(), site_url($custom_slug . '?action=lostpassword'))); exit; } else { wp_safe_redirect(site_url($custom_slug . '?checkemail=confirm')); exit; } } if ($post_action === 'resetpass' || $post_action === 'rp') { if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_key(wp_unslash($_POST['_wpnonce'])), 'adlhpti_resetpass_nonce')) { wp_die(esc_html__('Nonce verification failed.', 'admin-login-hide-pti')); } // Logic from wp-login.php for resetpass $rp_key = isset($_POST['rp_key']) ? sanitize_text_field(wp_unslash($_POST['rp_key'])) : ''; $rp_login = isset($_POST['rp_login']) ? sanitize_text_field(wp_unslash($_POST['rp_login'])) : ''; $user = check_password_reset_key($rp_key, $rp_login); if (!is_wp_error($user)) { if (isset($_POST['pass1'], $_POST['pass2']) && $_POST['pass1'] !== $_POST['pass2']) { wp_safe_redirect(add_query_arg(['action' => 'rp', 'key' => $rp_key, 'login' => $rp_login, 'error' => 'password_reset_mismatch'], site_url($custom_slug))); exit; } if (isset($_POST['pass1']) && !empty($_POST['pass1'])) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Passwords should not be sanitized as they can contain special characters. reset_password($user, wp_unslash($_POST['pass1'])); wp_safe_redirect(site_url($custom_slug . '?resetpass=complete')); exit; } } else { wp_safe_redirect(add_query_arg('error', $user->get_error_code(), site_url($custom_slug . '?action=lostpassword'))); exit; } } } if (is_user_logged_in() && !isset($_GET['action'])) { wp_safe_redirect(admin_url()); exit; } $action = isset($_GET['action']) ? sanitize_text_field(wp_unslash($_GET['action'])) : 'login'; $errors = new WP_Error(); ?> > >' . esc_html__('Lost your password?', 'admin-login-hide-pti') . '
'; } echo '' . esc_html($error_msg) . ''; } $custom_slug = trim(get_option('adlhpti_custom_login_slug', 'secure-login'), '/'); ?>