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 '
'; foreach ( AIEO_Login_Security::selectable_roles() as $slug => $name ) { $on = in_array( $slug, (array) $s['roles'], true ); $n = isset( $avail[ $slug ] ) ? (int) $avail[ $slug ] : 0; echo ''; } echo '
'; echo $this->card_form( $anchor ); $why = __( 'The number beside each role is how many accounts currently have it.', 'ai-eshop-optimizer' ); echo class_exists( '\Rooster\Admin\Guidance' ) ? \Rooster\Admin\Guidance::text( $why ) : '

' . 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 '
'; echo self::row( array( 'id' => 'aieo-ls-2fa-enabled', 'group' => true, 'label' => __( 'Require it', 'ai-eshop-optimizer' ), 'control' => AIEO_Kit::check( '2fa[enabled]', __( 'Ask the roles ticked above for a code as well as a password.', 'ai-eshop-optimizer' ), ! empty( $c['enabled'] ), array( 'id' => 'aieo-ls-2fa-enabled' ) ), ) ); $line = ''; $since = isset( $c['enabled_at'] ) ? (int) $c['enabled_at'] : 0; if ( ! empty( $c['enabled'] ) && $since > 0 ) { $fmt = (string) get_option( 'date_format' ); $line = sprintf( esc_html__( 'In force since %s.', 'ai-eshop-optimizer' ), esc_html( wp_date( $fmt, $since ) ) ); if ( (int) $c['grace_days'] > 0 ) { $line .= ' ' . sprintf( esc_html__( 'Accounts that already existed then have until %s to set it up.', 'ai-eshop-optimizer' ), esc_html( wp_date( $fmt, $since + ( (int) $c['grace_days'] * DAY_IN_SECONDS ) ) ) ); } } echo self::row( array( 'id' => 'aieo-ls-grace-days', 'label' => __( 'Give them', 'ai-eshop-optimizer' ), 'control' => AIEO_Kit::input( '2fa[grace_days]', (string) (int) $c['grace_days'], array( 'type' => 'number', 'id' => 'aieo-ls-grace-days', 'min' => 0, 'max' => 90, 'xs' => true, 'unit' => __( 'days to set it up', 'ai-eshop-optimizer' ) ) ), 'help_html' => esc_html__( 'Counted from the day you switch this on, so the deadline is one date for the whole shop rather than a fresh countdown each person starts at their own next sign-in. Somebody whose account is created later gets the full run from the day it is created. Until it runs out they are reminded on every admin page and can carry on working. After it runs out, setting it up happens at sign-in.', 'ai-eshop-optimizer' ) . ' ' . esc_html__( 'Set 0 if you have already told your staff and want it in force from their next sign-in.', 'ai-eshop-optimizer' ) . ( '' !== $line ? '
' . $line . '' : '' ), ) ); echo self::row( array( 'id' => 'aieo-ls-remember-days', 'label' => __( 'Trusted browsers', 'ai-eshop-optimizer' ), 'control' => AIEO_Kit::input( '2fa[remember_days]', (string) (int) $c['remember_days'], array( 'type' => 'number', 'id' => 'aieo-ls-remember-days', 'min' => 0, 'max' => 90, 'xs' => true, 'unit' => __( 'days before asking again on the same browser', 'ai-eshop-optimizer' ) ) ), 'help' => __( 'Each person decides at sign-in: the code screen offers a box to trust that browser for this many days, unticked, and only a browser whose box was ticked is trusted. 0 asks every single time and offers no box. Nobody should tick it on a shared or public computer: the shop cannot tell the difference, it only knows the browser.', 'ai-eshop-optimizer' ), ) ); echo self::row( array( 'id' => 'aieo-ls-allow-optional', 'group' => true, 'label' => __( 'Everybody else', 'ai-eshop-optimizer' ), 'control' => AIEO_Kit::check( '2fa[allow_optional]', __( 'Let accounts outside those roles switch it on for themselves.', 'ai-eshop-optimizer' ), ! empty( $c['allow_optional'] ), array( 'id' => 'aieo-ls-allow-optional' ) ), 'help' => __( 'Costs nothing and is never worse. Each person sets it up from their own profile page.', 'ai-eshop-optimizer' ), ) ); echo '
'; echo $this->card_form( $anchor ); $this->render_enrolment_status(); echo $close; } private function render_enrolment_status() { $roles = AIEO_Login_Security::protected_roles(); if ( empty( $roles ) ) { return; } $users = get_users( array( 'role__in' => $roles, 'fields' => array( 'ID', 'user_login', 'display_name' ), 'number' => 200, ) ); if ( empty( $users ) ) { return; } $done = array(); $todo = array(); foreach ( $users as $u ) { if ( AIEO_TOTP::is_active( $u->ID ) ) { $done[] = $u; } else { $todo[] = $u; } } echo rooster_admin_card_section_html(); echo '
'; 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 '
'; } private function render_login_url_card( array $s ) { $c = $s['login_url']; $current = AIEO_Login_URL::private_url(); $anchor = 'login-security-private-sign-in-address'; list( $open, $close ) = $this->card( $anchor, 'door-open', __( 'A private address for signing in', 'ai-eshop-optimizer' ), __( 'Serve the sign-in page from a private address of your choosing instead of the one every bot already knows.', 'ai-eshop-optimizer' ), array( 'col' => 2, 'footer' => self::save_footer( $anchor ) ) ); echo $open; $this->render_login_url_verdict(); $why = __( 'Every WordPress site has its sign-in page at the same well-known address, and automated scanners hammer it around the clock. Moving it stops that noise, which matters mostly because it makes a real attempt visible in your logs instead of buried under thousands of fake ones.', 'ai-eshop-optimizer' ); echo class_exists( '\Rooster\Admin\Guidance' ) ? \Rooster\Admin\Guidance::text( $why ) : '

' . esc_html( $why ) . '

'; ob_start(); echo '
'; echo self::row( array( 'id' => 'aieo-ls-url-enabled', 'group' => true, 'label' => __( 'Use a private address', 'ai-eshop-optimizer' ), 'control' => AIEO_Kit::check( 'login_url[enabled]', __( 'Serve the sign-in page from the address below instead.', 'ai-eshop-optimizer' ), ! empty( $c['enabled'] ), array( 'id' => 'aieo-ls-url-enabled' ) ), ) ); $aieo_ls_slug = '' . 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 ) . '

'; } echo self::row( array( 'id' => 'aieo-ls-slug', 'label' => __( 'The address', 'ai-eshop-optimizer' ), 'control' => $aieo_ls_slug, ) ); $modes = array( 'my_account' => __( 'My Account page', 'ai-eshop-optimizer' ), 'home' => __( 'The home page', 'ai-eshop-optimizer' ), '404' => __( 'A "page not found"', 'ai-eshop-optimizer' ), ); echo self::row( array( 'id' => 'aieo-ls-on-hit', 'label' => __( 'Anyone who finds the old address', 'ai-eshop-optimizer' ), 'control' => AIEO_Kit::select( 'login_url[on_hit]', $modes, (string) $c['on_hit'], array( 'id' => 'aieo-ls-on-hit' ) ), 'help_html' => esc_html__( 'My Account is the right answer on a shop: somebody who typed the old address is far more likely to be a customer looking for their orders than an attacker.', 'ai-eshop-optimizer' ) . '
' . esc_html__( 'Password-reset links, logout links and the "session expired" box keep working at their published addresses whichever you pick — recalling those would break emails your customers already have.', 'ai-eshop-optimizer' ), ) ); $reg_modes = array( 'follow' => __( 'At the private address, with the sign-in form', 'ai-eshop-optimizer' ), 'public' => __( 'At the usual public address', 'ai-eshop-optimizer' ), 'private' => __( 'At its own private address', 'ai-eshop-optimizer' ), ); $reg_mode = isset( $c['reg_mode'] ) ? (string) $c['reg_mode'] : 'follow'; $aieo_ls_reg = AIEO_Kit::select( 'login_url[reg_mode]', $reg_modes, $reg_mode, array( 'id' => 'aieo-ls-reg-mode' ) ) . '' . 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__( 'At the private address', 'ai-eshop-optimizer' ) . ' ' . esc_html__( 'suits a site whose accounts are handed out rather than requested: nobody signs up without already knowing the way in.', 'ai-eshop-optimizer' ) . '
' . esc_html__( 'At the usual public address', 'ai-eshop-optimizer' ) . ' ' . esc_html__( 'suits a shop with open sign-ups. Your sign-up page is public by definition — every "Register" link on the site points at it — so there is nothing left to hide, and hiding it only costs you the sign-ups.', 'ai-eshop-optimizer' ) . '
' . esc_html__( 'At its own private address', 'ai-eshop-optimizer' ) . ' ' . esc_html__( 'lets you hand out a sign-up link without handing out the way in. The sign-up page never shows the sign-in address, not even in its own form.', 'ai-eshop-optimizer' ), ) ); echo self::row( array( 'id' => 'aieo-ls-bar-myaccount', 'group' => true, 'label' => __( 'The other door', 'ai-eshop-optimizer' ), 'control' => AIEO_Kit::check( 'login_url[bar_myaccount]', __( 'Stop the protected roles signing in on the customer My Account form.', 'ai-eshop-optimizer' ), ! empty( $c['bar_myaccount'] ), array( 'id' => 'aieo-ls-bar-myaccount' ) ), 'help_html' => '' . esc_html__( 'Leave this ticked.', 'ai-eshop-optimizer' ) . ' ' . esc_html__( 'Your shop already has a second, fully working sign-in form on the My Account page, and it accepts an administrator password exactly as the old page did. Without this, moving the sign-in address hides one door and leaves the other wide open. Customers are unaffected — they keep using My Account as normal.', 'ai-eshop-optimizer' ) . '
' . esc_html__( 'If the private address ever stops showing the sign-in form, this stands aside until it works again, so a broken address never locks your staff out.', 'ai-eshop-optimizer' ) . '
' . esc_html__( 'Shop integrations that sign in over the API — your ERP bridge, courier connections — are not affected, because they do not use a form.', 'ai-eshop-optimizer' ), ) ); echo '
'; echo $this->card_form( $anchor ); echo $close; } private function render_login_url_verdict() { if ( ! AIEO_Login_Security::feature_active( 'login_url' ) ) { return; } $fresh = false; if ( isset( $_GET['ls_retest'] ) && check_admin_referer( 'aieo_ls_retest' ) ) { $fresh = true; } $test = AIEO_Login_URL::self_test( $fresh ); $url = AIEO_Login_URL::private_url(); if ( 'ok' === $test['state'] ) { $aieo_ls_verdict = \Rooster\Admin\Ui::notice( esc_html( $url ), 'success', array( 'title' => __( 'The private address answers with the sign-in form.', 'ai-eshop-optimizer' ), 'inline' => true, ) ); echo $aieo_ls_verdict; } elseif ( 'redirected' === $test['state'] || 'missing' === $test['state'] ) { ob_start(); if ( 'redirected' === $test['state'] ) { printf( esc_html__( 'Something on this site sends %1$s to %2$s instead of showing the sign-in form.', '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' ) . '

'; echo '

' . 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' ) . '

'; } $aieo_ls_verdict = \Rooster\Admin\Ui::notice( $aieo_ls_what, 'destructive', array( 'title' => __( 'Your private sign-in address does not work.', 'ai-eshop-optimizer' ), 'inline' => true, 'detail' => (string) ob_get_clean(), ) ); echo $aieo_ls_verdict; } elseif ( 'unknown' === $test['state'] ) { $aieo_ls_verdict = \Rooster\Admin\Ui::notice( esc_html__( 'That is not the same as it being broken, but do open it in a private browser window and confirm you get the sign-in form before you sign out.', 'ai-eshop-optimizer' ) . ' ' . esc_html( $test['detail'] ), 'warning', array( 'title' => __( 'The private address could not be checked from this server.', 'ai-eshop-optimizer' ), 'inline' => true, ) ); echo $aieo_ls_verdict; } echo '

' . 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 self::row( array( 'id' => 'aieo-ls-lockout-enabled', 'group' => true, 'label' => __( 'Switch it on', 'ai-eshop-optimizer' ), 'control' => AIEO_Kit::check( 'lockout[enabled]', __( 'Pause sign-in from an address that keeps failing.', 'ai-eshop-optimizer' ), ! empty( $c['enabled'] ), array( 'id' => 'aieo-ls-lockout-enabled' ) ), ) ); ob_start(); echo ' ' . esc_html__( 'failures within', 'ai-eshop-optimizer' ) . ' '; echo ' ' . esc_html__( 'minutes, then pause for', 'ai-eshop-optimizer' ) . ' '; echo ' ' . esc_html__( 'minutes', 'ai-eshop-optimizer' ); echo self::row( array( 'id' => 'aieo-ls-lockout', 'group' => true, 'label' => __( 'Allow', 'ai-eshop-optimizer' ), 'control' => '' . (string) ob_get_clean() . '', 'help' => __( 'An office on one internet connection looks like a single address to us, so five people mistyping their passwords on a Monday can trip this. Failures are counted per account as well as per address to soften that, but if your staff share a connection, be generous here.', 'ai-eshop-optimizer' ), ) ); ob_start(); echo esc_html__( 'After', 'ai-eshop-optimizer' ) . ' '; echo ' ' . esc_html__( 'pauses in a day, pause for', 'ai-eshop-optimizer' ) . ' '; echo ' ' . esc_html__( 'hours instead', 'ai-eshop-optimizer' ); echo self::row( array( 'id' => 'aieo-ls-long-lock', 'group' => true, 'label' => __( 'Repeat offenders', 'ai-eshop-optimizer' ), 'control' => '' . (string) ob_get_clean() . '', 'help' => __( 'Somebody who comes back a fourth time after being told to stop is not a customer who forgot their password. Set 0 to treat every pause the same.', 'ai-eshop-optimizer' ), ) ); echo self::row( array( 'id' => 'aieo-ls-notify', 'group' => true, 'label' => __( 'Tell the account holder', 'ai-eshop-optimizer' ), 'control' => AIEO_Kit::check( 'lockout[notify]', __( 'Email the person whose account is being guessed at.', 'ai-eshop-optimizer' ), ! empty( $c['notify'] ), array( 'id' => 'aieo-ls-notify' ) ), 'help' => __( 'Only for the protected roles, and at most once an hour per account however heavy the attack.', 'ai-eshop-optimizer' ), ) ); echo '
'; echo $this->card_form( $anchor ); echo $close; } private function render_log() { $summary = AIEO_Login_Lockout::summary(); $rows = AIEO_Login_Lockout::recent( 100 ); $nonce = wp_create_nonce( self::NONCE ); $foot = ''; $dialog = ''; if ( $summary['active'] > 0 ) { $asks = array(); if ( class_exists( '\Rooster\Admin\States' ) ) { $dialog = \Rooster\Admin\States::confirm( 'aieo-ls-release-confirm', 'off', __( 'Lift every pause now', 'ai-eshop-optimizer' ), __( 'Lift every pause now? Any address currently blocked will be able to try again immediately.', 'ai-eshop-optimizer' ), array( 'confirm' => __( 'Lift every pause now', 'ai-eshop-optimizer' ) ) ); $asks = \Rooster\Admin\States::confirm_attrs( 'aieo-ls-release-confirm' ); } $foot = AIEO_Kit::button( __( 'Lift every pause now', 'ai-eshop-optimizer' ), array( 'attrs' => array( 'id' => 'aieo-ls-release', 'data-nonce' => $nonce, 'data-aieo-action' => 'login_security.release_locks', 'data-aieo-action-destructive' => '1', ) + ( is_array( $asks ) ? $asks : array() ), ) ) . ' ' . esc_html__( 'Use if your own office has been caught by it.', 'ai-eshop-optimizer' ) . ''; } rooster_admin_band_open( array( 'cols' => '2:1' ) ); list( $open, $close ) = $this->card( 'login-security-last-24-hours', 'clock', __( 'The last 24 hours', 'ai-eshop-optimizer' ), __( 'How many sign-ins failed in the last day, how many pauses were applied and how many addresses are paused now.', 'ai-eshop-optimizer' ), array( 'col' => 2, 'footer' => $foot ) ); echo $open; echo $dialog; 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 ) { ?>