settings = Plugin::get_settings( 'security' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ], 20 ); add_filter( 'adminease_validate_settings', [ $this, 'validate_settings' ] ); $this->slug = self::active_slug( $this->settings ); if( null === $this->slug ) { return; } remove_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 ); add_filter( 'site_url', [ $this, 'rewrite_url' ], 100 ); add_filter( 'network_site_url', [ $this, 'rewrite_url' ], 100 ); add_filter( 'wp_redirect', [ $this, 'rewrite_url' ], 100 ); add_action( 'wp_loaded', [ $this, 'block_admin_for_visitors' ], 1 ); add_action( 'wp_loaded', [ $this, 'route' ], PHP_INT_MAX ); } /** * The slug that is in force, or null when the feature is off or cannot work on this site. * * @param array $settings The security settings. * * @return string|null */ public static function active_slug( array $settings ): ?string { if( empty( $settings['custom_login_url_enabled'] ) || ( defined( 'ADMINEASE_DISABLE_LOGIN_URL' ) && ADMINEASE_DISABLE_LOGIN_URL ) || is_multisite() || self::hosts_differ() ) { return null; } if( '' === (string) get_option( 'permalink_structure' ) ) { return null; } $slug = sanitize_title( (string) ( $settings['custom_login_url_slug'] ?? '' ) ); if( !preg_match( '/^[a-z0-9-]+$/', $slug ) || in_array( $slug, self::RESERVED, true ) ) { return null; } return $slug; } /** * Whether the site address and the WordPress address are on different hosts (a headless front end for example), * where the login address cannot be worked out. * * @return bool */ private static function hosts_differ(): bool { return wp_parse_url( home_url(), PHP_URL_HOST ) !== wp_parse_url( site_url(), PHP_URL_HOST ); } /** * The full address of the new login page. * * @param string $slug The login slug. * * @return string */ private static function login_address( string $slug ): string { return home_url( '/' . $slug . '/' ); } /** * Adds the switch and the slug to the Security tab. * * @param array $fields The settings fields keyed by tab. * * @return array The modified settings fields. */ public function adminease_settings_fields( array $fields ): array { $slug = sanitize_title( (string) ( $this->settings['custom_login_url_slug'] ?? '' ) ); $fields['security']['fields'][] = [ 'type' => 'switch', 'id' => 'custom-login-url-enabled', 'name' => 'adminease[security][custom_login_url_enabled]', 'value' => $this->settings['custom_login_url_enabled'] ?? '', 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'label' => __( 'Custom Login URL', 'adminease' ), 'risk' => 'careful', 'breaks' => __( 'The old /wp-login.php address stops working. Anyone who has it bookmarked gets a not found page.', 'adminease' ), 'description' => __( 'Moves the login page from /wp-login.php to an address you choose, so bots that try thousands of passwords find nothing. Bookmark the new address first. If you forget it, add define( \'ADMINEASE_DISABLE_LOGIN_URL\', true ); to wp-config.php, or rename the AdminEase plugin folder. Purge your page cache after switching it on. Needs pretty permalinks and does not run on multisite.', 'adminease' ), 'child_fields' => [ [ 'type' => 'text', 'id' => 'custom-login-url-slug', 'name' => 'adminease[security][custom_login_url_slug]', 'value' => $slug, 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'Login Address', 'adminease' ), 'field_description' => '' !== $slug /* translators: %s: the full login address */ ? sprintf( __( 'Letters a to z, numbers and dashes only. Your login page is now: %s', 'adminease' ), '' . esc_html( self::login_address( $slug ) ) . '' ) /* translators: %s: the site address */ : sprintf( __( 'Letters, numbers and dashes only, for example team-entrance. The login page becomes %s', 'adminease' ), '' . esc_html( home_url( '/team-entrance/' ) ) . '' ), 'attributes' => [ 'data-parent' => 'custom-login-url-enabled', 'maxlength' => 60, ], ], ], ]; return $fields; } /** * Refuses a slug that would break the site or lock the owner out, and stores it in its clean form. * * @param array|WP_Error $sanitized_settings Sanitized settings array, or an error from an earlier check. * * @return array|WP_Error */ public function validate_settings( $sanitized_settings ) { if( !is_array( $sanitized_settings ) ) { return $sanitized_settings; } $security = $sanitized_settings['security'] ?? null; if( !is_array( $security ) || empty( $security['custom_login_url_enabled'] ) ) { return $sanitized_settings; } if( is_multisite() ) { return new WP_Error( 'custom_login_url_multisite', esc_html__( 'Custom Login URL does not run on multisite.', 'adminease' ) ); } if( '' === (string) get_option( 'permalink_structure' ) ) { return new WP_Error( 'custom_login_url_permalinks', esc_html__( 'Custom Login URL needs pretty permalinks. Choose any structure other than Plain in Settings > Permalinks first.', 'adminease' ) ); } if( self::hosts_differ() ) { return new WP_Error( 'custom_login_url_hosts', esc_html__( 'Custom Login URL needs the WordPress Address and the Site Address to be on the same host.', 'adminease' ) ); } $slug = sanitize_title( (string) ( $security['custom_login_url_slug'] ?? '' ) ); if( '' === $slug ) { return new WP_Error( 'custom_login_url_empty', esc_html__( 'Login Address cannot be empty', 'adminease' ) ); } if( !preg_match( '/^[a-z0-9-]+$/', $slug ) ) { return new WP_Error( 'custom_login_url_characters', esc_html__( 'Login Address can only use the letters a to z, numbers and dashes.', 'adminease' ) ); } if( in_array( $slug, self::RESERVED, true ) ) { return new WP_Error( 'custom_login_url_reserved', esc_html__( 'That Login Address is already used by WordPress. Choose another one.', 'adminease' ) ); } if( get_page_by_path( $slug, OBJECT, get_post_types( [ 'public' => true ] ) ) ) { return new WP_Error( 'custom_login_url_taken', esc_html__( 'A page or post already uses that address. Choose another one.', 'adminease' ) ); } $sanitized_settings['security']['custom_login_url_slug'] = $slug; return $sanitized_settings; } /** * The request path below the site address, lowercase, without slashes at the ends. * * @return string */ private function request_path(): string { $uri = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Only compared, never output. $path = (string) wp_parse_url( rawurldecode( $uri ), PHP_URL_PATH ); $base = (string) wp_parse_url( home_url(), PHP_URL_PATH ); if( '' !== $base && ( $path === $base || 0 === strpos( $path, $base . '/' ) ) ) { $path = substr( $path, strlen( $base ) ); } return strtolower( trim( $path, '/' ) ); } /** * Shows the login page at the new address and hides the old one. * * @return void */ public function route(): void { $slug = $this->slug; if( null === $slug || ( defined( 'WP_CLI' ) && WP_CLI ) || wp_doing_cron() ) { return; } $path = $this->request_path(); if( $path === $slug ) { global $pagenow; $pagenow = 'wp-login.php'; global $user_login, $error, $interim_login, $action, $wp_error, $user_identity; require_once ABSPATH . 'wp-login.php'; exit; } global $pagenow; if( 'wp-login.php' === $pagenow && self::is_postpass_request() ) { return; } if( ( in_array( $pagenow, [ 'wp-login.php', 'wp-signup.php', 'wp-activate.php' ], true ) || in_array( $path, [ 'wp-login', 'wp-signup', 'wp-activate' ], true ) ) && !apply_filters( 'adminease_custom_login_url_allow_default', false ) ) { $this->not_found(); } } /** * Whether this request is the password form of a protected post: a POST with action=postpass. Core reads $_REQUEST, where a posted value beats the address, so both must say postpass. * * @return bool */ private static function is_postpass_request(): bool { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only compares the action name, nothing is read or saved. return isset( $_SERVER['REQUEST_METHOD'], $_GET['action'], $_REQUEST['action'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] && 'postpass' === $_GET['action'] && 'postpass' === $_REQUEST['action']; } /** * Answers 404 to wp-admin for visitors who are not logged in, so it does not send them to the new login address. * admin-ajax.php and admin-post.php stay open, front end features use them, and so do the install and upgrade screens and the application password approval page. * * @return void */ public function block_admin_for_visitors(): void { global $pagenow; if( !defined( 'WP_ADMIN' ) || !WP_ADMIN || is_user_logged_in() || wp_doing_ajax() || wp_doing_cron() ) { return; } if( in_array( $pagenow, [ 'admin-ajax.php', 'admin-post.php', 'upgrade.php', 'install.php', 'load-styles.php', 'load-scripts.php', 'authorize-application.php' ], true ) || apply_filters( 'adminease_custom_login_url_allow_default', false ) ) { return; } $this->not_found(); } /** * Ends the request with a plain 404 page. * * @return void */ private function not_found(): void { nocache_headers(); wp_die( esc_html__( 'Not found.', 'adminease' ), esc_html__( 'Not found', 'adminease' ), [ 'response' => 404 ] ); } /** * Points every link to wp-login.php at the new address, keeping its query string. * * @param string $url The address WordPress built. * * @return string */ public function rewrite_url( $url ) { if( !is_string( $url ) || false === strpos( $url, 'wp-login.php' ) || null === $this->slug ) { return $url; } $slug = $this->slug; $parts = explode( '?', $url, 2 ); if( isset( $parts[1] ) ) { wp_parse_str( $parts[1], $query ); if( isset( $query['action'] ) && 'postpass' === $query['action'] ) { return $url; } } if( !preg_match( '#^((https?:)?//[^/]+(/[^?]*)?/|/?)wp-login\.php$#i', $parts[0] ) ) { return $url; } $host = wp_parse_url( $parts[0], PHP_URL_HOST ); if( $host && strtolower( $host ) !== strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ) && strtolower( $host ) !== strtolower( (string) wp_parse_url( site_url(), PHP_URL_HOST ) ) ) { return $url; } $scheme = wp_parse_url( $parts[0], PHP_URL_SCHEME ); $new = self::login_address( $slug ); if( $scheme ) { $new = set_url_scheme( $new, $scheme ); } return isset( $parts[1] ) ? $new . '?' . $parts[1] : $new; } }