settings(); $settings->add_tab( self::TAB, __( 'Security', 'all-in-one-utilities' ), __( 'Reduce what your site exposes and slow down password guessing. None of these replace strong passwords, updates, and backups.', 'all-in-one-utilities' ), 28 ); $this->register_login_attempts( $settings ); $this->register_ip_address( $settings ); add_action( 'aiou_settings_render_field_' . self::LOCKOUTS_TYPE, array( LoginLimiter::class, 'render_lockouts' ) ); add_action( 'aiou_settings_render_field_' . self::IP_INFO_TYPE, array( $this, 'render_ip_info' ) ); ( new LoginLimiter() )->register(); } /** * Limit Login Attempts. * * @param SettingsRegistry $settings The settings registry. * @return void */ private function register_login_attempts( SettingsRegistry $settings ): void { $section = LoginLimiter::SECTION; $settings->add_section( self::TAB, $section, __( 'Limit Login Attempts', 'all-in-one-utilities' ), __( 'Lock a device out for a while after too many wrong passwords.', 'all-in-one-utilities' ), 20 ); $number = static fn( string $id, string $label, int $initial, int $min, int $max, string $description, int $priority ): Field => new Field( id: $id, label: $label, type: 'number', default: $initial, args: array( 'min' => $min, 'max' => $max, 'description' => $description, 'depends_on' => 'enabled', ), sanitize: static fn( $value ): int => max( $min, min( $max, (int) $value ) ), priority: $priority ); $fields = array( new Field( id: 'enabled', label: __( 'Limit login attempts', 'all-in-one-utilities' ), type: 'checkbox', default: 0, args: array( 'toggle_label' => __( 'Lock out a device after repeated failed logins', 'all-in-one-utilities' ) ), doc: new Doc( title: __( 'Limit login attempts', 'all-in-one-utilities' ), short: __( 'Counts failed logins per IP address. After too many, that address can\'t log in for a while, even with the right password. It covers the login page, WooCommerce\'s My Account login, XML-RPC, and application passwords.', 'all-in-one-utilities' ), detailed: __( 'Lockouts are per address, not per username, so nobody can lock a real user out by guessing their name. Visitors see how many attempts are left once only two remain.', 'all-in-one-utilities' ), warnings: array( __( 'Locked yourself out? Wait for the lockout to end, log in from another network, or add define( \'AIOU_DISABLE_LOGIN_LIMITS\', true ); to wp-config.php.', 'all-in-one-utilities' ) ) ), priority: 10 ), $number( 'max_attempts', __( 'Attempts allowed', 'all-in-one-utilities' ), 5, 1, 50, __( 'Failed logins in a row before a lockout. Failures older than 12 hours are forgotten.', 'all-in-one-utilities' ), 20 ), $number( 'lockout_minutes', __( 'Lockout length (minutes)', 'all-in-one-utilities' ), 20, 1, 1440, '', 30 ), $number( 'max_lockouts', __( 'Lockouts before a long lockout', 'all-in-one-utilities' ), 4, 1, 20, __( 'Counted over the last 24 hours.', 'all-in-one-utilities' ), 40 ), $number( 'long_lockout_hours', __( 'Long lockout length (hours)', 'all-in-one-utilities' ), 24, 1, 720, '', 50 ), new Field( id: 'allowlist', label: __( 'Never lock out', 'all-in-one-utilities' ), type: 'textarea', default: '', args: array( 'rows' => 4, 'placeholder' => "203.0.113.10\n198.51.100.0/24", 'description' => sprintf( /* translators: %s: the visitor's IP address */ __( 'One IP address or range per line (e.g. 198.51.100.0/24 or 10.0.*). Your address is %s.', 'all-in-one-utilities' ), '' === Ip::current() ? __( 'unknown', 'all-in-one-utilities' ) : Ip::current() ), 'depends_on' => 'enabled', ), sanitize: array( Ip::class, 'sanitize_list' ), priority: 60 ), new Field( id: 'lockouts', label: __( 'Locked out now', 'all-in-one-utilities' ), type: self::LOCKOUTS_TYPE, default: '', args: array( 'label_above' => true ), sanitize: static fn(): string => '', priority: 70 ), ); foreach ( $fields as $field ) { $settings->add_field( self::TAB, $section, $field ); } } /** * Visitor IP Address. * * @param SettingsRegistry $settings The settings registry. * @return void */ private function register_ip_address( SettingsRegistry $settings ): void { $settings->add_section( self::TAB, Ip::SECTION, __( 'Visitor IP Address', 'all-in-one-utilities' ), __( 'Where security features read a visitor\'s IP address from. Only change this if your site is behind a proxy or CDN such as Cloudflare.', 'all-in-one-utilities' ), 90 ); $settings->add_field( self::TAB, Ip::SECTION, new Field( id: 'source', label: __( 'Read the address from', 'all-in-one-utilities' ), type: 'select', default: 'REMOTE_ADDR', args: array( 'choices' => Ip::sources() ), doc: new Doc( title: __( 'Visitor IP address', 'all-in-one-utilities' ), short: __( 'Behind a proxy or CDN, every visitor appears to come from the proxy\'s address, so one lockout would lock everyone out. Pick the header your proxy sets instead.', 'all-in-one-utilities' ), warnings: array( __( 'Choose a header only if your site really is behind that service. Otherwise visitors can send a made-up address in that header and slip past lockouts.', 'all-in-one-utilities' ) ) ), priority: 10 ) ); $settings->add_field( self::TAB, Ip::SECTION, new Field( id: 'info', label: __( 'What each one reports', 'all-in-one-utilities' ), type: self::IP_INFO_TYPE, default: '', sanitize: static fn(): string => '', priority: 20 ) ); } /** * The address each source reports for the current request. * * @return void */ public function render_ip_info(): void { echo ''; echo '

' . esc_html__( 'The right choice shows your own public IP address.', 'all-in-one-utilities' ) . '

'; } /** * Move the Hardening (formerly Performance → Security & Access) and * Author Pages values from the Performance tab's option to this tab's. * "Disable author archives" became Author Pages with a 404 response. * * @return void */ public static function migrate(): void { $performance = get_option( 'aiou_settings_performance' ); if ( ! is_array( $performance ) || ! ( isset( $performance['security_access'] ) || isset( $performance['author_pages'] ) ) ) { return; } $security = get_option( 'aiou_settings_security', array() ); $security = is_array( $security ) ? $security : array(); if ( isset( $performance['security_access'] ) && ! isset( $security['security_access'] ) ) { $security['security_access'] = (array) $performance['security_access']; } if ( isset( $performance['author_pages'] ) && ! isset( $security['author_pages'] ) ) { $security['author_pages'] = (array) $performance['author_pages']; } if ( ! empty( $security['security_access']['author_archives'] ) && empty( $security['author_pages']['enabled'] ) ) { $security['author_pages'] = array_merge( (array) ( $security['author_pages'] ?? array() ), array( 'enabled' => 1, 'response' => '404', ) ); } unset( $security['security_access']['author_archives'], $performance['security_access'], $performance['author_pages'] ); update_option( 'aiou_settings_security', $security ); update_option( 'aiou_settings_performance', $performance ); } }