This commit is contained in:
Javier Casares 2026-06-05 19:45:19 +00:00
commit 467560d420

View file

@ -37,6 +37,11 @@ class Settings_Page {
*/
private const DOCS_PAGE_SLUG = 'robotstxt-2fa-docs';
/**
* Dashboard page slug (single-site parent menu, owned by Dashboard_Page).
*/
private const DASHBOARD_PAGE_SLUG = 'robotstxt-2fa-dashboard';
/**
* Verification methods available for per-role configuration.
*
@ -226,6 +231,76 @@ class Settings_Page {
self::PAGE_SLUG,
'robotstxt_2fa_data'
);
add_settings_section(
'robotstxt_2fa_access',
__( 'Access control', 'robotstxt-2fa' ),
array( $this, 'render_access_section' ),
self::PAGE_SLUG
);
add_settings_field(
'robotstxt_2fa_app_passwords_exempt',
__( 'Application Passwords', 'robotstxt-2fa' ),
array( $this, 'render_app_passwords_exempt_field' ),
self::PAGE_SLUG,
'robotstxt_2fa_access'
);
add_settings_field(
'robotstxt_2fa_ip_allow',
__( 'IP allow list', 'robotstxt-2fa' ),
array( $this, 'render_ip_allow_field' ),
self::PAGE_SLUG,
'robotstxt_2fa_access'
);
add_settings_field(
'robotstxt_2fa_ip_deny',
__( 'IP deny list', 'robotstxt-2fa' ),
array( $this, 'render_ip_deny_field' ),
self::PAGE_SLUG,
'robotstxt_2fa_access'
);
add_settings_section(
'robotstxt_2fa_notifications',
__( 'Email notifications', 'robotstxt-2fa' ),
array( $this, 'render_notifications_section' ),
self::PAGE_SLUG
);
add_settings_field(
'robotstxt_2fa_notif_on_enable',
__( 'On method activation', 'robotstxt-2fa' ),
array( $this, 'render_notif_on_enable_field' ),
self::PAGE_SLUG,
'robotstxt_2fa_notifications'
);
add_settings_field(
'robotstxt_2fa_notif_on_new_ip',
__( 'New location', 'robotstxt-2fa' ),
array( $this, 'render_notif_on_new_ip_field' ),
self::PAGE_SLUG,
'robotstxt_2fa_notifications'
);
add_settings_field(
'robotstxt_2fa_notif_on_recovery',
__( 'Recovery code used', 'robotstxt-2fa' ),
array( $this, 'render_notif_on_recovery_field' ),
self::PAGE_SLUG,
'robotstxt_2fa_notifications'
);
add_settings_field(
'robotstxt_2fa_notif_digest',
__( 'Activity digest', 'robotstxt-2fa' ),
array( $this, 'render_notif_digest_field' ),
self::PAGE_SLUG,
'robotstxt_2fa_notifications'
);
}
/**
@ -429,7 +504,7 @@ class Settings_Page {
*
* @param mixed $raw_settings Raw settings submitted by the user.
*
* @return array{role_methods: array<string, array<int, string>>, frequency: string, force_frequency: bool, delete_on_uninstall: bool, max_attempts: int, lockout_duration: int, trust_device_days: int, grace_period_days: int, grace_period_action: string}
* @return array{role_methods: array<string, array<int, string>>, frequency: string, force_frequency: bool, delete_on_uninstall: bool, max_attempts: int, lockout_duration: int, trust_device_days: int, grace_period_days: int, grace_period_action: string, app_passwords_exempt: bool, ip_allow: string, ip_deny: string, notifications: array{on_enable: bool, on_new_ip: bool, on_recovery_used: bool, on_recovery_used_admin: bool, digest_enabled: bool, digest_frequency: string}}
*/
public function sanitize_settings( $raw_settings ): array {
$capability = $this->get_required_capability();
@ -518,13 +593,36 @@ class Settings_Page {
$sanitized['grace_period_action'] = $raw_settings['grace_period_action'];
}
if ( array_key_exists( 'app_passwords_exempt', $raw_settings ) ) {
$sanitized['app_passwords_exempt'] = '1' === ( is_scalar( $raw_settings['app_passwords_exempt'] ) ? (string) $raw_settings['app_passwords_exempt'] : '' );
}
if ( isset( $raw_settings['ip_allow'] ) && is_string( $raw_settings['ip_allow'] ) ) {
$sanitized['ip_allow'] = sanitize_textarea_field( $raw_settings['ip_allow'] );
}
if ( isset( $raw_settings['ip_deny'] ) && is_string( $raw_settings['ip_deny'] ) ) {
$sanitized['ip_deny'] = sanitize_textarea_field( $raw_settings['ip_deny'] );
}
$raw_notif = isset( $raw_settings['notifications'] ) && is_array( $raw_settings['notifications'] ) ? $raw_settings['notifications'] : array();
$sanitized['notifications'] = array(
'on_enable' => ! empty( $raw_notif['on_enable'] ),
'on_new_ip' => ! empty( $raw_notif['on_new_ip'] ),
'on_recovery_used' => ! empty( $raw_notif['on_recovery_used'] ),
'on_recovery_used_admin' => ! empty( $raw_notif['on_recovery_used_admin'] ),
'digest_enabled' => ! empty( $raw_notif['digest_enabled'] ),
'digest_frequency' => isset( $raw_notif['digest_frequency'] ) && 'monthly' === $raw_notif['digest_frequency'] ? 'monthly' : 'weekly',
);
return $sanitized;
}
/**
* Retrieve plugin settings, migrating from legacy format when necessary.
*
* @return array{role_methods: array<string, array<int, string>>, frequency: string, force_frequency: bool, delete_on_uninstall: bool, max_attempts: int, lockout_duration: int, trust_device_days: int, grace_period_days: int, grace_period_action: string}
* @return array{role_methods: array<string, array<int, string>>, frequency: string, force_frequency: bool, delete_on_uninstall: bool, max_attempts: int, lockout_duration: int, trust_device_days: int, grace_period_days: int, grace_period_action: string, app_passwords_exempt: bool, ip_allow: string, ip_deny: string, notifications: array{on_enable: bool, on_new_ip: bool, on_recovery_used: bool, on_recovery_used_admin: bool, digest_enabled: bool, digest_frequency: string}}
*/
public function get_settings(): array {
$options = array();
@ -544,37 +642,61 @@ class Settings_Page {
}
}
$defaults = $this->get_default_settings();
$defaults = $this->get_default_settings();
$raw_notif = isset( $options['notifications'] ) && is_array( $options['notifications'] ) ? $options['notifications'] : array();
$def_notif = $defaults['notifications'];
return array(
'role_methods' => $this->resolve_role_methods( $options ),
'frequency' => Frequency_Options::sanitize( isset( $options['frequency'] ) && is_string( $options['frequency'] ) ? $options['frequency'] : '' ),
'force_frequency' => ! empty( $options['force_frequency'] ),
'delete_on_uninstall' => ! empty( $options['delete_on_uninstall'] ),
'max_attempts' => isset( $options['max_attempts'] ) && is_numeric( $options['max_attempts'] ) ? max( 0, absint( $options['max_attempts'] ) ) : $defaults['max_attempts'],
'lockout_duration' => isset( $options['lockout_duration'] ) && is_numeric( $options['lockout_duration'] ) ? max( 1, absint( $options['lockout_duration'] ) ) : $defaults['lockout_duration'],
'trust_device_days' => isset( $options['trust_device_days'] ) && is_numeric( $options['trust_device_days'] ) ? max( 0, absint( $options['trust_device_days'] ) ) : $defaults['trust_device_days'],
'grace_period_days' => isset( $options['grace_period_days'] ) && is_numeric( $options['grace_period_days'] ) ? max( 0, absint( $options['grace_period_days'] ) ) : $defaults['grace_period_days'],
'grace_period_action' => isset( $options['grace_period_action'] ) && in_array( $options['grace_period_action'], array( 'block', 'wizard' ), true ) ? $options['grace_period_action'] : $defaults['grace_period_action'],
'role_methods' => $this->resolve_role_methods( $options ),
'frequency' => Frequency_Options::sanitize( isset( $options['frequency'] ) && is_string( $options['frequency'] ) ? $options['frequency'] : '' ),
'force_frequency' => ! empty( $options['force_frequency'] ),
'delete_on_uninstall' => ! empty( $options['delete_on_uninstall'] ),
'max_attempts' => isset( $options['max_attempts'] ) && is_numeric( $options['max_attempts'] ) ? max( 0, absint( $options['max_attempts'] ) ) : $defaults['max_attempts'],
'lockout_duration' => isset( $options['lockout_duration'] ) && is_numeric( $options['lockout_duration'] ) ? max( 1, absint( $options['lockout_duration'] ) ) : $defaults['lockout_duration'],
'trust_device_days' => isset( $options['trust_device_days'] ) && is_numeric( $options['trust_device_days'] ) ? max( 0, absint( $options['trust_device_days'] ) ) : $defaults['trust_device_days'],
'grace_period_days' => isset( $options['grace_period_days'] ) && is_numeric( $options['grace_period_days'] ) ? max( 0, absint( $options['grace_period_days'] ) ) : $defaults['grace_period_days'],
'grace_period_action' => isset( $options['grace_period_action'] ) && in_array( $options['grace_period_action'], array( 'block', 'wizard' ), true ) ? $options['grace_period_action'] : $defaults['grace_period_action'],
'app_passwords_exempt' => isset( $options['app_passwords_exempt'] ) ? (bool) $options['app_passwords_exempt'] : $defaults['app_passwords_exempt'],
'ip_allow' => isset( $options['ip_allow'] ) && is_string( $options['ip_allow'] ) ? $options['ip_allow'] : $defaults['ip_allow'],
'ip_deny' => isset( $options['ip_deny'] ) && is_string( $options['ip_deny'] ) ? $options['ip_deny'] : $defaults['ip_deny'],
'notifications' => array(
'on_enable' => isset( $raw_notif['on_enable'] ) ? (bool) $raw_notif['on_enable'] : $def_notif['on_enable'],
'on_new_ip' => isset( $raw_notif['on_new_ip'] ) ? (bool) $raw_notif['on_new_ip'] : $def_notif['on_new_ip'],
'on_recovery_used' => isset( $raw_notif['on_recovery_used'] ) ? (bool) $raw_notif['on_recovery_used'] : $def_notif['on_recovery_used'],
'on_recovery_used_admin' => isset( $raw_notif['on_recovery_used_admin'] ) ? (bool) $raw_notif['on_recovery_used_admin'] : $def_notif['on_recovery_used_admin'],
'digest_enabled' => isset( $raw_notif['digest_enabled'] ) ? (bool) $raw_notif['digest_enabled'] : $def_notif['digest_enabled'],
'digest_frequency' => isset( $raw_notif['digest_frequency'] ) && 'monthly' === $raw_notif['digest_frequency'] ? 'monthly' : $def_notif['digest_frequency'],
),
);
}
/**
* Retrieve default settings.
*
* @return array{role_methods: array<string, array<int, string>>, frequency: string, force_frequency: bool, delete_on_uninstall: bool, max_attempts: int, lockout_duration: int, trust_device_days: int, grace_period_days: int, grace_period_action: string}
* @return array{role_methods: array<string, array<int, string>>, frequency: string, force_frequency: bool, delete_on_uninstall: bool, max_attempts: int, lockout_duration: int, trust_device_days: int, grace_period_days: int, grace_period_action: string, app_passwords_exempt: bool, ip_allow: string, ip_deny: string, notifications: array{on_enable: bool, on_new_ip: bool, on_recovery_used: bool, on_recovery_used_admin: bool, digest_enabled: bool, digest_frequency: string}}
*/
private function get_default_settings(): array {
return array(
'role_methods' => array(),
'frequency' => Frequency_Options::FREQUENCY_SESSION,
'force_frequency' => false,
'delete_on_uninstall' => false,
'max_attempts' => 3,
'lockout_duration' => 15,
'trust_device_days' => 30,
'grace_period_days' => 7,
'grace_period_action' => 'wizard',
'role_methods' => array(),
'frequency' => Frequency_Options::FREQUENCY_SESSION,
'force_frequency' => false,
'delete_on_uninstall' => false,
'max_attempts' => 3,
'lockout_duration' => 15,
'trust_device_days' => 30,
'grace_period_days' => 7,
'grace_period_action' => 'wizard',
'app_passwords_exempt' => true,
'ip_allow' => '',
'ip_deny' => '',
'notifications' => array(
'on_enable' => false,
'on_new_ip' => false,
'on_recovery_used' => false,
'on_recovery_used_admin' => false,
'digest_enabled' => false,
'digest_frequency' => 'weekly',
),
);
}
@ -906,42 +1028,233 @@ class Settings_Page {
/**
* Register the plugin admin page in the desired context.
*
* On the network dashboard the Settings_Page creates its own top-level menu.
* On single-site installs the top-level menu is owned by Dashboard_Page; this
* method only registers the Settings and Documentation submenus under it.
*
* @param bool $network Whether the menu is registered for the network dashboard.
*
* @return void
*/
private function register_admin_page( bool $network ): void {
unset( $network );
$capability = $this->get_required_capability();
add_menu_page(
__( '2FA (by ROBOTSTXT)', 'robotstxt-2fa' ),
__( '2FA', 'robotstxt-2fa' ),
$capability,
self::PAGE_SLUG,
array( $this, 'render_page' ),
'dashicons-shield'
if ( $network ) {
add_menu_page(
__( '2FA (by ROBOTSTXT)', 'robotstxt-2fa' ),
__( '2FA', 'robotstxt-2fa' ),
$capability,
self::PAGE_SLUG,
array( $this, 'render_page' ),
'dashicons-shield'
);
// Rename the first auto-created submenu entry to "Settings".
add_submenu_page(
self::PAGE_SLUG,
__( 'Settings — 2FA', 'robotstxt-2fa' ),
__( 'Settings', 'robotstxt-2fa' ),
$capability,
self::PAGE_SLUG,
array( $this, 'render_page' )
);
add_submenu_page(
self::PAGE_SLUG,
__( 'Documentation — 2FA', 'robotstxt-2fa' ),
__( 'Documentation', 'robotstxt-2fa' ),
$capability,
self::DOCS_PAGE_SLUG,
array( $this, 'render_docs_page' )
);
} else {
// Single-site: Dashboard_Page owns the top-level menu. Register submenus only.
add_submenu_page(
self::DASHBOARD_PAGE_SLUG,
__( 'Settings — 2FA', 'robotstxt-2fa' ),
__( 'Settings', 'robotstxt-2fa' ),
$capability,
self::PAGE_SLUG,
array( $this, 'render_page' )
);
add_submenu_page(
self::DASHBOARD_PAGE_SLUG,
__( 'Documentation — 2FA', 'robotstxt-2fa' ),
__( 'Documentation', 'robotstxt-2fa' ),
$capability,
self::DOCS_PAGE_SLUG,
array( $this, 'render_docs_page' )
);
}
}
/**
* Render the Access control section description.
*
* @return void
*/
public function render_access_section(): void {
echo '<p>' . esc_html__( 'Application Passwords, IP allow lists and deny lists.', 'robotstxt-2fa' ) . '</p>';
}
/**
* Render the Application Passwords exemption checkbox.
*
* @return void
*/
public function render_app_passwords_exempt_field(): void {
$settings = $this->get_settings();
$is_checked = $settings['app_passwords_exempt'];
printf(
'<input type="hidden" name="%1$s[app_passwords_exempt]" value="0" />',
esc_attr( self::OPTION_NAME )
);
// Rename the first auto-created submenu entry to "Settings".
add_submenu_page(
self::PAGE_SLUG,
__( 'Settings — 2FA', 'robotstxt-2fa' ),
__( 'Settings', 'robotstxt-2fa' ),
$capability,
self::PAGE_SLUG,
array( $this, 'render_page' )
printf(
'<label><input type="checkbox" name="%1$s[app_passwords_exempt]" value="1" %2$s /> %3$s</label>',
esc_attr( self::OPTION_NAME ),
checked( $is_checked, true, false ),
esc_html__( 'Exempt Application Password requests from the 2FA challenge', 'robotstxt-2fa' )
);
add_submenu_page(
self::PAGE_SLUG,
__( 'Documentation — 2FA', 'robotstxt-2fa' ),
__( 'Documentation', 'robotstxt-2fa' ),
$capability,
self::DOCS_PAGE_SLUG,
array( $this, 'render_docs_page' )
echo '<p class="description">' . esc_html__( 'Application Password requests cannot complete browser-based 2FA challenges. Enable to let REST API and WP-CLI clients using Application Passwords skip the verification step.', 'robotstxt-2fa' ) . '</p>';
}
/**
* Render the IP allow list textarea field.
*
* @return void
*/
public function render_ip_allow_field(): void {
$settings = $this->get_settings();
printf(
'<textarea name="%1$s[ip_allow]" rows="5" cols="40">%2$s</textarea>',
esc_attr( self::OPTION_NAME ),
esc_textarea( $settings['ip_allow'] )
);
echo '<p class="description">' . esc_html__( 'One IP address or CIDR range per line (e.g. 192.168.1.0/24). Requests from these IPs skip the 2FA challenge.', 'robotstxt-2fa' ) . '</p>';
}
/**
* Render the IP deny list textarea field.
*
* @return void
*/
public function render_ip_deny_field(): void {
$settings = $this->get_settings();
printf(
'<textarea name="%1$s[ip_deny]" rows="5" cols="40">%2$s</textarea>',
esc_attr( self::OPTION_NAME ),
esc_textarea( $settings['ip_deny'] )
);
echo '<p class="description">' . esc_html__( 'One IP address or CIDR range per line. Login is blocked outright for these IPs.', 'robotstxt-2fa' ) . '</p>';
}
/**
* Render the Email notifications section description.
*
* @return void
*/
public function render_notifications_section(): void {
echo '<p>' . esc_html__( 'Automatic email alerts for security-relevant events.', 'robotstxt-2fa' ) . '</p>';
}
/**
* Render the on-method-enable notification checkbox.
*
* @return void
*/
public function render_notif_on_enable_field(): void {
$settings = $this->get_settings();
printf(
'<label><input type="checkbox" name="%1$s[notifications][on_enable]" value="1" %2$s /> %3$s</label>',
esc_attr( self::OPTION_NAME ),
checked( $settings['notifications']['on_enable'], true, false ),
esc_html__( 'Notify the user when an administrator enables 2FA for their account.', 'robotstxt-2fa' )
);
}
/**
* Render the new-IP notification checkbox.
*
* @return void
*/
public function render_notif_on_new_ip_field(): void {
$settings = $this->get_settings();
printf(
'<label><input type="checkbox" name="%1$s[notifications][on_new_ip]" value="1" %2$s /> %3$s</label>',
esc_attr( self::OPTION_NAME ),
checked( $settings['notifications']['on_new_ip'], true, false ),
esc_html__( 'Notify the user on login from an unrecognised browser or IP.', 'robotstxt-2fa' )
);
}
/**
* Render the recovery-code-used notification checkboxes.
*
* @return void
*/
public function render_notif_on_recovery_field(): void {
$settings = $this->get_settings();
printf(
'<label><input type="checkbox" name="%1$s[notifications][on_recovery_used]" value="1" %2$s /> %3$s</label>',
esc_attr( self::OPTION_NAME ),
checked( $settings['notifications']['on_recovery_used'], true, false ),
esc_html__( 'Notify user when a recovery code is used.', 'robotstxt-2fa' )
);
echo '<br />';
printf(
'<label style="margin-top:6px;display:inline-block;"><input type="checkbox" name="%1$s[notifications][on_recovery_used_admin]" value="1" %2$s /> %3$s</label>',
esc_attr( self::OPTION_NAME ),
checked( $settings['notifications']['on_recovery_used_admin'], true, false ),
esc_html__( 'Also notify site administrator.', 'robotstxt-2fa' )
);
}
/**
* Render the activity digest checkbox and frequency radio buttons.
*
* @return void
*/
public function render_notif_digest_field(): void {
$settings = $this->get_settings();
$frequency = $settings['notifications']['digest_frequency'];
printf(
'<label><input type="checkbox" name="%1$s[notifications][digest_enabled]" value="1" %2$s /> %3$s</label>',
esc_attr( self::OPTION_NAME ),
checked( $settings['notifications']['digest_enabled'], true, false ),
esc_html__( 'Send a periodic activity digest to site administrators.', 'robotstxt-2fa' )
);
echo '<fieldset style="margin-top:8px;">';
printf(
'<label><input type="radio" name="%1$s[notifications][digest_frequency]" value="weekly" %2$s /> %3$s</label><br />',
esc_attr( self::OPTION_NAME ),
checked( 'weekly', $frequency, false ),
esc_html__( 'Weekly', 'robotstxt-2fa' )
);
printf(
'<label><input type="radio" name="%1$s[notifications][digest_frequency]" value="monthly" %2$s /> %3$s</label>',
esc_attr( self::OPTION_NAME ),
checked( 'monthly', $frequency, false ),
esc_html__( 'Monthly (every 30 days)', 'robotstxt-2fa' )
);
echo '</fieldset>';
}
/**