array('label' => __( 'Settings', 'ai-eshop-optimizer' ), 'icon' => 'settings'), 'log' => array('label' => __( 'Sign-in attempts', 'ai-eshop-optimizer' ), 'icon' => 'scroll-text'), ); } public function get_tab_arg() { return 'ls_section'; } public function register_hooks() { add_action( 'wp_ajax_aieo_login_security_save', array( $this, 'ajax_save' ) ); add_action( 'wp_ajax_aieo_login_security_release', array( $this, 'ajax_release' ) ); } public function render_subtab() { $tabs = $this->get_tabs(); $section = isset( $_GET['ls_section'] ) ? sanitize_key( wp_unslash( $_GET['ls_section'] ) ) : 'settings'; if ( ! isset( $tabs[ $section ] ) ) { $section = 'settings'; } AIEO_DMM_UI::tab_strip( $this->get_id(), $tabs, $section, array( 'arg' => $this->get_tab_arg(), 'wrapper' => 'div', 'margin' => '14px', ) ); if ( 'log' === $section ) { $this->render_log(); } else { $this->render_settings(); } } public function enqueue_admin_assets() { $css = 'assets/css/aieo-login-security-admin.css'; wp_enqueue_style( 'aieo-login-security-admin', AIEO_URI . $css, array(), file_exists( AIEO_DIR . $css ) ? (string) filemtime( AIEO_DIR . $css ) : '1' ); } private static function row( array $field ) { if ( class_exists( '\Rooster\Admin\Ui' ) ) { return \Rooster\Admin\Ui::field_row( $field ); } return AIEO_Kit::row( (string) ( $field['label'] ?? '' ), (string) ( $field['control'] ?? '' ), (string) ( $field['help'] ?? '' ), array( 'id' => (string) ( $field['id'] ?? '' ), 'group' => ! empty( $field['group'] ) ) ); } private function render_settings() { $s = AIEO_Login_Security::settings(); $nonce = wp_create_nonce( self::NONCE ); $this->form_opts = array( 'nonce' => $nonce, 'nonce_name' => 'nonce', 'class' => 'aieo-ls-form', ); rooster_admin_band_open( array( 'cols' => 1 ) ); $this->render_roles_card( $s ); rooster_admin_band_close(); rooster_admin_band_open( array( 'cols' => 2 ) ); $this->render_2fa_card( $s ); $this->render_lockout_card( $s ); $this->render_login_url_card( $s ); rooster_admin_band_close(); $this->print_js( $nonce ); } private $form_opts = array(); private static function form_id( $anchor ) { return 'aieo-card-' . $anchor . '-form'; } private static function save_footer( $anchor ) { return class_exists( '\Rooster\Admin\States' ) ? \Rooster\Admin\States::save_button( self::form_id( $anchor ) ) : ''; } private function card_form( $anchor ) { return AIEO_DMM_UI::card_form( 'aieo_login_security_save', $this->get_id(), self::form_id( $anchor ), (string) ob_get_clean(), $this->form_opts ); } private function render_state_banner() { if ( defined( 'AIEO_LOGIN_SECURITY_BYPASS' ) && AIEO_LOGIN_SECURITY_BYPASS ) { $aieo_ls_banner = \Rooster\Admin\Ui::notice( esc_html__( 'AIEO_LOGIN_SECURITY_BYPASS is set in wp-config.php, which switches off every control on this page. Your settings are saved and will take effect the moment that line is removed.', 'ai-eshop-optimizer' ), 'destructive', array( 'title' => __( 'None of this is being enforced.', 'ai-eshop-optimizer' ), 'inline' => true ) ); echo $aieo_ls_banner; return; } if ( AIEO_Login_Security::is_local_host() ) { $aieo_ls_banner = \Rooster\Admin\Ui::notice( esc_html__( 'This looks like a development copy, so sign-in controls are switched off to stop a downloaded copy of a shop demanding a phone that is not in the room. Your settings are saved and apply on the live site.', 'ai-eshop-optimizer' ), 'warning', array( 'title' => __( 'Not enforced on this address.', 'ai-eshop-optimizer' ), 'inline' => true ) ); echo $aieo_ls_banner; } } private function card( $anchor, $icon, $title, $description = '', array $more = array() ) { return AIEO_DMM_UI::card( array_merge( array( 'anchor' => $anchor, 'icon' => $icon, 'title' => $title, 'description' => $description, ), $more ) ); } private function render_roles_card( array $s ) { $anchor = 'login-security-protected-accounts'; list( $open, $close ) = $this->card( $anchor, 'users', __( 'Which accounts are we protecting?', 'ai-eshop-optimizer' ), __( 'Tick the roles that can change the shop — prices, orders, plugins, customer data. Everything below applies to these accounts. Your customers are not affected by any of it.', 'ai-eshop-optimizer' ), array( 'col' => 1, 'footer' => self::save_footer( $anchor ) ) ); echo $open; $counts = count_users(); $avail = isset( $counts['avail_roles'] ) ? $counts['avail_roles'] : array(); $this->render_state_banner(); ob_start(); echo '
' . esc_html( $why ) . '
'; echo $close; } private function render_2fa_card( array $s ) { $c = $s['2fa']; $anchor = 'login-security-two-factor-authentication'; list( $open, $close ) = $this->card( $anchor, 'lock', __( 'Two-factor authentication', 'ai-eshop-optimizer' ), __( 'A six-digit code from the person\'s own phone, on top of their password. It is what stops a password that has leaked somewhere else from being enough to get into your shop. Works with Google Authenticator, Microsoft Authenticator, 1Password, Authy and any other app that scans a QR code.', 'ai-eshop-optimizer' ), array( 'col' => 1, 'footer' => self::save_footer( $anchor ) ) ); echo $open; ob_start(); echo '' . esc_html( sprintf( __( '%1$d of %2$d protected accounts have set it up.', 'ai-eshop-optimizer' ), count( $done ), count( $users ) ) ) . '
'; if ( ! empty( $todo ) ) { $names = array(); foreach ( array_slice( $todo, 0, 12 ) as $u ) { $names[] = $u->display_name ? $u->display_name : $u->user_login; } echo '' . esc_html__( 'Still to do:', 'ai-eshop-optimizer' ) . ' ' . esc_html( implode( ', ', $names ) ) . ( count( $todo ) > 12 ? esc_html( sprintf( __( ' and %d more', 'ai-eshop-optimizer' ), count( $todo ) - 12 ) ) : '' ) . '
'; } echo '' . esc_html( $why ) . '
'; ob_start(); echo '' . esc_html( trailingslashit( home_url() ) ) . ''
. AIEO_Kit::input( 'login_url[slug]', (string) $c['slug'], array( 'id' => 'aieo-ls-slug', 'medium' => true, 'code' => true, 'autocomplete' => 'off', 'placeholder' => __( 'e.g. front-door-7412', 'ai-eshop-optimizer' ), 'attrs' => array( 'aria-describedby' => 'aieo-ls-slug-help' ) ) )
. ''
. '' . esc_html__( 'Write it down before you save.', 'ai-eshop-optimizer' ) . ' ' . esc_html__( 'Bookmark it and give it to your staff. If it is ever lost, add this line to wp-config.php to switch everything on this page off again:', 'ai-eshop-optimizer' ) . '
' . 'define(\'AIEO_LOGIN_SECURITY_BYPASS\', true);'; if ( '' !== $current && ! empty( $c['enabled'] ) ) { $aieo_ls_slug .= '
' . esc_html__( 'Currently live at:', 'ai-eshop-optimizer' ) . ' ' . esc_html( $current ) . '
' . esc_html( trailingslashit( home_url() ) ) . ''
. AIEO_Kit::input( 'login_url[reg_slug]', isset( $c['reg_slug'] ) ? (string) $c['reg_slug'] : '', array( 'id' => 'aieo-ls-reg-slug', 'medium' => true, 'code' => true, 'autocomplete' => 'off', 'aria-label' => __( 'At its own private address', 'ai-eshop-optimizer' ), 'placeholder' => __( 'e.g. join-7412', 'ai-eshop-optimizer' ) ) )
. ''
. '';
echo self::row(
array(
'id' => 'aieo-ls-reg-mode',
'label' => __( 'Where people sign up', 'ai-eshop-optimizer' ),
'control' => $aieo_ls_reg,
'help_html' => esc_html__( 'Only matters when Settings → General has "Anyone can register" ticked. With it unticked WordPress refuses sign-ups anyway and this changes nothing.', 'ai-eshop-optimizer' )
. '' . esc_html( $url ) . '',
'' . esc_html( $test['location'] ) . ''
);
} else {
printf(
esc_html__( '%1$s does not return a sign-in form (the server answered %2$s).', 'ai-eshop-optimizer' ),
'' . esc_html( $url ) . '',
'' . esc_html( (string) $test['status'] ) . ''
);
}
$aieo_ls_what = (string) ob_get_clean();
ob_start();
echo '' . esc_html__( 'Until it works, the protected roles can still sign in on the My Account form, so nobody is locked out, but the private address protects nothing. Switch this feature off, or clear whatever is redirecting the page.', 'ai-eshop-optimizer' ) . '
'; $foreign = AIEO_Login_URL::foreign_login_hooks(); $replaces = array(); $others = array(); foreach ( $foreign as $hit ) { $label = isset( $hit['label'] ) ? (string) $hit['label'] : ''; if ( '' === $label ) { continue; } if ( ! empty( $hit['replaces'] ) ) { $replaces[ $label ] = true; } elseif ( ! isset( $replaces[ $label ] ) ) { $others[ $label ] = true; } } $others = array_diff_key( $others, $replaces ); if ( ! empty( $replaces ) ) { echo '' . esc_html__( 'One of these is replacing the sign-in page. Start here:', 'ai-eshop-optimizer' ) . '
' . esc_html( $label ) . '' . esc_html__( 'A theme that provides its own front-end sign-in page takes over this one the same way, and cannot be reasoned with from here. Where that is the case the two features do the same job, and the private address is the one to switch off.', 'ai-eshop-optimizer' ) . '
'; } if ( ! empty( $others ) ) { echo '' . esc_html__( 'These also run on the sign-in page, but can only add to it, not redirect it:', 'ai-eshop-optimizer' ) . '
' . esc_html( $label ) . '' . AIEO_Kit::button( __( 'Test the private address now', 'ai-eshop-optimizer' ), array( 'href' => wp_nonce_url( add_query_arg( 'ls_retest', '1', $this->get_url() ), 'aieo_ls_retest' ) ) ) . '
'; } private function render_lockout_card( array $s ) { $c = $s['lockout']; $anchor = 'login-security-repeated-failure-pause'; list( $open, $close ) = $this->card( $anchor, 'timer', __( 'Pause sign-in after repeated failures', 'ai-eshop-optimizer' ), __( 'WordPress will let a script guess passwords as fast as your server can answer. This refuses an address that keeps getting it wrong. Nothing happens to the account itself — no password is reset, nothing is disabled, and the pause lifts on its own.', 'ai-eshop-optimizer' ), array( 'col' => 1, 'footer' => self::save_footer( $anchor ) ) ); echo $open; ob_start(); echo ''; echo esc_html( sprintf( __( '%1$d failed sign-ins, %2$d pauses applied, %3$d addresses paused right now.', 'ai-eshop-optimizer' ), $summary['failed'], $summary['locked'], $summary['active'] ) ); echo '
'; echo $close; list( $open, $close ) = $this->card( 'login-security-recent-attempts', 'scroll-text', __( 'Recent sign-in attempts', 'ai-eshop-optimizer' ), __( 'Each failed sign-in of the last 30 days: when, from which address, for which account, and what happened.', 'ai-eshop-optimizer' ), array( 'col' => 1 ) ); echo $open; if ( empty( $rows ) ) { $empty = \Rooster\Admin\States::state( 'first-use', array( 'title' => __( 'Nothing recorded yet. Entries appear here when a sign-in fails, and are kept for 30 days.', 'ai-eshop-optimizer' ) ) ); echo $empty; echo $close; rooster_admin_band_close(); $this->print_log_js(); return; } $labels = array( 'failed' => __( 'Wrong password', 'ai-eshop-optimizer' ), 'cleared' => __( 'Wrong password, later signed in successfully', 'ai-eshop-optimizer' ), 'locked' => __( 'Paused', 'ai-eshop-optimizer' ), 'released' => __( 'Pause lifted by an administrator', 'ai-eshop-optimizer' ), ); $log_rows = array(); foreach ( $rows as $i => $row ) { $when = strtotime( $row->created_at . ' UTC' ); $user = $row->user_id ? get_userdata( (int) $row->user_id ) : null; $label = isset( $labels[ $row->outcome ] ) ? $labels[ $row->outcome ] : $row->outcome; if ( 'locked' === $row->outcome && $row->locked_until ) { $until = strtotime( $row->locked_until . ' UTC' ); $label .= ' — ' . ( $until > time() ? sprintf( __( 'until %s from now', 'ai-eshop-optimizer' ), human_time_diff( time(), $until ) ) : __( 'now expired', 'ai-eshop-optimizer' ) ); } $account = $user ? (string) $user->user_login : __( 'no such account', 'ai-eshop-optimizer' ); $log_rows[] = array( '_id' => (string) $i, 'when' => wp_date( 'j M, H:i', $when ), 'ip' => (string) $row->ip, 'account' => $user ? $account : array( 'html' => '' . esc_html( $account ) . '' ), 'what' => $label, '_attrs' => array( 'data-ra-s' => array( 'when' => (int) $when, 'ip' => (string) $row->ip, 'account' => $account, 'what' => $label ), 'data-ra-text' => implode( ' ', array( wp_date( 'j M, H:i', $when ), (string) $row->ip, $account, $label ) ), ), ); } echo \Rooster\Admin\Ui::table( array( array( 'key' => 'when', 'label' => __( 'When', 'ai-eshop-optimizer' ), 'sort_url' => add_query_arg( 'orderby', 'when' ), 'type' => 'date', 'sort' => 'none', 'sort_type' => 'number' ), array( 'key' => 'ip', 'label' => __( 'Address', 'ai-eshop-optimizer' ), 'sort_url' => add_query_arg( 'orderby', 'ip' ), 'type' => 'code', 'sort' => 'none', 'sort_type' => 'text' ), array( 'key' => 'account', 'label' => __( 'Account', 'ai-eshop-optimizer' ), 'sort_url' => add_query_arg( 'orderby', 'account' ), 'type' => 'text', 'sort' => 'none', 'sort_type' => 'text' ), array( 'key' => 'what', 'label' => __( 'What happened', 'ai-eshop-optimizer' ), 'sort_url' => add_query_arg( 'orderby', 'what' ), 'type' => 'text', 'sort' => 'none', 'sort_type' => 'text' ), ), $log_rows, array( 'id' => 'aieo-ls-attempts', 'search' => true, 'local' => array( 'size' => 25 ), ) ); echo $close; rooster_admin_band_close(); $this->print_log_js(); } private function verify_ajax() { if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'permission_denied' ), 403 ); } check_ajax_referer( self::NONCE, 'nonce' ); } public function ajax_save() { $this->verify_ajax(); $in = $this->posted_settings(); $old = AIEO_Login_Security::settings(); $roles_valid = array_keys( AIEO_Login_Security::selectable_roles() ); $roles = array_values( array_intersect( (array) $in['roles'], $roles_valid ) ); $enabled_at = ! empty( $in['2fa']['enabled'] ) ? ( (int) $old['2fa']['enabled_at'] > 0 ? (int) $old['2fa']['enabled_at'] : time() ) : 0; $new = array( 'roles' => $roles, '2fa' => array( 'enabled' => ! empty( $in['2fa']['enabled'] ), 'enabled_at' => $enabled_at, 'grace_days' => max( 0, min( 90, (int) $in['2fa']['grace_days'] ) ), 'remember_days' => max( 0, min( 90, (int) $in['2fa']['remember_days'] ) ), 'allow_optional' => ! empty( $in['2fa']['allow_optional'] ), ), 'login_url' => array( 'enabled' => ! empty( $in['login_url']['enabled'] ), 'slug' => AIEO_Login_URL::sanitize_slug( $in['login_url']['slug'] ), 'on_hit' => in_array( $in['login_url']['on_hit'], array( 'my_account', 'home', '404' ), true ) ? $in['login_url']['on_hit'] : 'my_account', 'bar_myaccount' => ! empty( $in['login_url']['bar_myaccount'] ), 'reg_mode' => in_array( isset( $in['login_url']['reg_mode'] ) ? $in['login_url']['reg_mode'] : '', array( 'follow', 'public', 'private' ), true ) ? $in['login_url']['reg_mode'] : 'follow', 'reg_slug' => AIEO_Login_URL::sanitize_slug( isset( $in['login_url']['reg_slug'] ) ? $in['login_url']['reg_slug'] : '' ), ), 'lockout' => array( 'enabled' => ! empty( $in['lockout']['enabled'] ), 'max_attempts' => max( 1, min( 50, (int) $in['lockout']['max_attempts'] ) ), 'window_min' => max( 1, min( 1440, (int) $in['lockout']['window_min'] ) ), 'lock_min' => max( 1, min( 1440, (int) $in['lockout']['lock_min'] ) ), 'long_lock_after' => max( 0, min( 20, (int) $in['lockout']['long_lock_after'] ) ), 'long_lock_hours' => max( 1, min( 168, (int) $in['lockout']['long_lock_hours'] ) ), 'notify' => ! empty( $in['lockout']['notify'] ), ), ); if ( ( $new['2fa']['enabled'] || $new['login_url']['enabled'] ) && empty( $new['roles'] ) ) { wp_send_json_error( array( 'message' => __( 'Tick at least one role first — otherwise these apply to nobody, and the screen would tell you they were on.', 'ai-eshop-optimizer' ) ) ); } if ( $new['login_url']['enabled'] ) { $conflict = AIEO_Login_URL::slug_conflict( $new['login_url']['slug'] ); if ( '' !== $conflict ) { wp_send_json_error( array( 'message' => $conflict ) ); } if ( 'private' === $new['login_url']['reg_mode'] ) { if ( '' === $new['login_url']['reg_slug'] ) { wp_send_json_error( array( 'message' => __( 'Give the sign-up page an address, or set sign-ups back to one of the other two options.', 'ai-eshop-optimizer' ) ) ); } if ( $new['login_url']['reg_slug'] === $new['login_url']['slug'] ) { wp_send_json_error( array( 'message' => __( 'The sign-up address has to differ from the sign-in address, or one of the two would be unreachable.', 'ai-eshop-optimizer' ) ) ); } $conflict = AIEO_Login_URL::slug_conflict( $new['login_url']['reg_slug'] ); if ( '' !== $conflict ) { wp_send_json_error( array( 'message' => $conflict ) ); } } } if ( $new['login_url']['enabled'] && $new['login_url']['bar_myaccount'] ) { $me = wp_get_current_user(); if ( $me && array_intersect( $new['roles'], (array) $me->roles ) && '' === $new['login_url']['slug'] ) { wp_send_json_error( array( 'message' => __( 'Set the private address before switching this on, or you will not be able to sign in again.', 'ai-eshop-optimizer' ) ) ); } } AIEO_Login_Security::save( $new ); $notes = array(); if ( $new['login_url']['enabled'] && ( empty( $old['login_url']['enabled'] ) || $old['login_url']['slug'] !== $new['login_url']['slug'] ) ) { $notes[] = sprintf( __( 'Sign in at %s from now on. Bookmark it now.', 'ai-eshop-optimizer' ), AIEO_Login_URL::private_url() ); } if ( $new['2fa']['enabled'] && empty( $old['2fa']['enabled'] ) ) { $notes[] = 0 === $new['2fa']['grace_days'] ? __( 'Two-factor authentication is in force from the next sign-in.', 'ai-eshop-optimizer' ) : sprintf( _n( 'Staff have %d day to set up their authenticator.', 'Staff have %d days to set up their authenticator.', $new['2fa']['grace_days'], 'ai-eshop-optimizer' ), $new['2fa']['grace_days'] ); } wp_send_json_success( array( 'message' => empty( $notes ) ? __( 'Saved.', 'ai-eshop-optimizer' ) : implode( ' ', $notes ), 'reload' => true, ) ); } private function posted_settings() { $defaults = AIEO_Login_Security::defaults(); $raw = wp_unslash( $_POST ); $out = array( 'roles' => isset( $raw['roles'] ) ? array_map( 'sanitize_key', (array) $raw['roles'] ) : array() ); foreach ( array( '2fa', 'login_url', 'lockout' ) as $group ) { $posted = isset( $raw[ $group ] ) && is_array( $raw[ $group ] ) ? $raw[ $group ] : array(); $out[ $group ] = array(); foreach ( $defaults[ $group ] as $key => $default ) { $out[ $group ][ $key ] = isset( $posted[ $key ] ) ? sanitize_text_field( (string) $posted[ $key ] ) : ( is_bool( $default ) ? false : $default ); } } return $out; } public function ajax_release() { $this->verify_ajax(); $n = AIEO_Login_Lockout::release_all(); wp_send_json_success( array( 'message' => sprintf( _n( '%d pause lifted.', '%d pauses lifted.', $n, 'ai-eshop-optimizer' ), $n ), ) ); } private function print_js( $nonce ) { ?>