*/ private const AVAILABLE_METHODS = array( 'email' => 'Email code', 'otp' => 'Authenticator app (OTP)', 'recovery_codes' => 'Recovery codes', ); /** * Register admin-related hooks. * * @return void */ public function register_hooks(): void { add_action( 'admin_menu', array( $this, 'register_menu' ) ); if ( is_multisite() ) { add_action( 'network_admin_menu', array( $this, 'register_network_menu' ) ); add_action( 'network_admin_edit_robotstxt_2fa_network_save', array( $this, 'handle_network_save' ) ); } add_action( 'admin_init', array( $this, 'register_settings' ) ); } /** * Register plugin settings page on single-site dashboards. * * @return void */ public function register_menu(): void { $this->register_admin_page( false ); } /** * Register plugin settings page on the network dashboard. * * @return void */ public function register_network_menu(): void { $this->register_admin_page( true ); } /** * Render plugin settings page. * * @return void */ public function render_page(): void { $capability = $this->get_required_capability(); if ( ! current_user_can( $capability ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to access this page.', 'robotstxt-2fa' ) ); } settings_errors( self::OPTION_NAME ); $form_action = $this->is_network_context() ? network_admin_url( 'edit.php?action=robotstxt_2fa_network_save' ) : 'options.php'; ?>

is_network_context() ) { wp_nonce_field( 'robotstxt_2fa_network_save', 'robotstxt_2fa_network_nonce' ); } else { settings_fields( self::OPTION_GROUP ); } do_settings_sections( self::PAGE_SLUG ); submit_button(); ?>
'array', 'sanitize_callback' => array( $this, 'sanitize_settings' ), 'default' => $this->get_default_settings(), 'autoload' => false, ) ); add_settings_section( 'robotstxt_2fa_general', __( 'General options', 'robotstxt-2fa' ), array( $this, 'render_general_section' ), self::PAGE_SLUG ); add_settings_field( 'robotstxt_2fa_role_methods', __( 'Required methods per role', 'robotstxt-2fa' ), array( $this, 'render_role_methods_field' ), self::PAGE_SLUG, 'robotstxt_2fa_general' ); add_settings_field( 'robotstxt_2fa_frequency', __( 'Verification frequency', 'robotstxt-2fa' ), array( $this, 'render_frequency_field' ), self::PAGE_SLUG, 'robotstxt_2fa_general' ); add_settings_field( 'robotstxt_2fa_force_frequency', __( 'Enforce frequency', 'robotstxt-2fa' ), array( $this, 'render_force_frequency_field' ), self::PAGE_SLUG, 'robotstxt_2fa_general' ); add_settings_section( 'robotstxt_2fa_data', __( 'Data management', 'robotstxt-2fa' ), array( $this, 'render_data_section' ), self::PAGE_SLUG ); add_settings_field( 'robotstxt_2fa_delete_on_uninstall', __( 'Delete data on uninstall', 'robotstxt-2fa' ), array( $this, 'render_delete_on_uninstall_field' ), self::PAGE_SLUG, 'robotstxt_2fa_data' ); } /** * Render general settings section description. * * @return void */ public function render_general_section(): void { echo '

' . esc_html__( 'Control how two-factor authentication is enforced for user roles.', 'robotstxt-2fa' ) . '

'; if ( $this->has_network_settings() && ! $this->is_network_context() ) { echo '

' . esc_html__( 'These settings are locked by a network administrator. Any changes made here will be ignored.', 'robotstxt-2fa' ) . '

'; } } /** * Render the per-role method matrix field. * * Each row represents a role; each column represents a verification method. * Checking a method for a role forces users of that role to verify with it. * Leaving all methods unchecked for a role makes 2FA optional for that role. * Row and column "select all" controls are provided via JavaScript. * * @return void */ public function render_role_methods_field(): void { $settings = $this->get_settings(); $role_methods = $settings['role_methods']; $roles = get_editable_roles(); $locked = $this->has_network_settings() && ! $this->is_network_context(); $disabled = $locked ? disabled( true, true, false ) : ''; if ( empty( $roles ) ) { echo '

' . esc_html__( 'No editable roles were found.', 'robotstxt-2fa' ) . '

'; return; } $methods = array( 'email' => __( 'Email code', 'robotstxt-2fa' ), 'otp' => __( 'Authenticator app', 'robotstxt-2fa' ), 'recovery_codes' => __( 'Recovery codes', 'robotstxt-2fa' ), ); ?> $method_label ) : ?> $role_details ) : ?> $method_label ) : ?>

>, frequency: string, force_frequency: bool, delete_on_uninstall: bool} */ public function sanitize_settings( $raw_settings ): array { $capability = $this->get_required_capability(); if ( ! current_user_can( $capability ) ) { add_settings_error( self::OPTION_NAME, 'robotstxt_2fa_capability', __( 'You are not allowed to update these settings.', 'robotstxt-2fa' ) ); return $this->get_settings(); } $raw_settings = is_array( $raw_settings ) ? $raw_settings : array(); $sanitized = $this->get_default_settings(); $current_settings = $this->get_settings(); $editable_roles = array_keys( get_editable_roles() ); $valid_methods = array_keys( self::AVAILABLE_METHODS ); $sanitized['role_methods'] = array(); if ( isset( $raw_settings['role_methods'] ) && is_array( $raw_settings['role_methods'] ) ) { foreach ( $raw_settings['role_methods'] as $role_key => $raw_methods ) { $role_key = sanitize_key( is_string( $role_key ) ? $role_key : '' ); if ( '' === $role_key || ! in_array( $role_key, $editable_roles, true ) ) { continue; } if ( ! is_array( $raw_methods ) ) { continue; } $methods = array(); foreach ( $raw_methods as $method ) { $method = sanitize_key( is_string( $method ) ? $method : '' ); if ( in_array( $method, $valid_methods, true ) ) { $methods[] = $method; } } if ( ! empty( $methods ) ) { $sanitized['role_methods'][ $role_key ] = array_values( array_unique( $methods ) ); } } } $frequency_value = $current_settings['frequency']; if ( isset( $raw_settings['frequency'] ) && is_string( $raw_settings['frequency'] ) ) { $frequency_value = $raw_settings['frequency']; } $sanitized['frequency'] = Frequency_Options::sanitize( $frequency_value ); if ( $this->has_network_settings() && ! $this->is_network_context() ) { $sanitized['force_frequency'] = $current_settings['force_frequency']; } elseif ( array_key_exists( 'force_frequency', $raw_settings ) ) { $sanitized['force_frequency'] = '1' === ( is_scalar( $raw_settings['force_frequency'] ) ? (string) $raw_settings['force_frequency'] : '' ); } if ( array_key_exists( 'delete_on_uninstall', $raw_settings ) ) { $sanitized['delete_on_uninstall'] = '1' === ( is_scalar( $raw_settings['delete_on_uninstall'] ) ? (string) $raw_settings['delete_on_uninstall'] : '' ); } return $sanitized; } /** * Retrieve plugin settings, migrating from legacy format when necessary. * * @return array{role_methods: array>, frequency: string, force_frequency: bool, delete_on_uninstall: bool} */ public function get_settings(): array { $options = array(); if ( is_multisite() ) { $network_options = get_site_option( self::OPTION_NAME, false ); if ( is_array( $network_options ) ) { $options = $network_options; } } if ( empty( $options ) ) { $raw = get_option( self::OPTION_NAME, array() ); if ( is_array( $raw ) ) { $options = $raw; } } 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'] ), ); } /** * Retrieve default settings. * * @return array{role_methods: array>, frequency: string, force_frequency: bool, delete_on_uninstall: bool} */ private function get_default_settings(): array { return array( 'role_methods' => array(), 'frequency' => Frequency_Options::FREQUENCY_SESSION, 'force_frequency' => false, 'delete_on_uninstall' => false, ); } /** * Resolve role_methods from stored options, migrating legacy force_roles + default_method if needed. * * @param array $options Raw stored options. * * @return array> */ private function resolve_role_methods( array $options ): array { $valid_methods = array_keys( self::AVAILABLE_METHODS ); if ( isset( $options['role_methods'] ) && is_array( $options['role_methods'] ) ) { $role_methods = array(); foreach ( $options['role_methods'] as $role => $methods ) { $role = is_string( $role ) ? sanitize_key( $role ) : ''; if ( '' === $role || ! is_array( $methods ) ) { continue; } $clean = array(); foreach ( $methods as $m ) { $m = is_string( $m ) ? sanitize_key( $m ) : ''; if ( in_array( $m, $valid_methods, true ) ) { $clean[] = $m; } } if ( ! empty( $clean ) ) { $role_methods[ $role ] = array_values( array_unique( $clean ) ); } } return $role_methods; } // Migrate legacy force_roles + default_method to role_methods. if ( isset( $options['force_roles'] ) && is_array( $options['force_roles'] ) ) { $default_method = isset( $options['default_method'] ) && is_string( $options['default_method'] ) ? sanitize_key( $options['default_method'] ) : 'email'; if ( ! in_array( $default_method, $valid_methods, true ) ) { $default_method = 'email'; } $role_methods = array(); foreach ( $options['force_roles'] as $role ) { $role = is_string( $role ) ? sanitize_key( $role ) : ''; if ( '' !== $role ) { $role_methods[ $role ] = array( $default_method ); } } return $role_methods; } return array(); } /** * Render verification frequency field. * * @return void */ public function render_frequency_field(): void { $settings = $this->get_settings(); $selected = $settings['frequency']; $disabled_attr = ''; if ( $this->has_network_settings() && ! $this->is_network_context() ) { $disabled_attr = disabled( true, true, false ); } $options = Frequency_Options::get_options(); echo ''; if ( isset( $options[ $selected ]['description'] ) && '' !== $options[ $selected ]['description'] ) { echo '

' . esc_html( $options[ $selected ]['description'] ) . '

'; } } /** * Render frequency enforcement checkbox. * * @return void */ public function render_force_frequency_field(): void { $settings = $this->get_settings(); $is_checked = $settings['force_frequency']; $disabled_attr = ''; if ( $this->has_network_settings() && ! $this->is_network_context() ) { $disabled_attr = disabled( true, true, false ); } if ( '' === $disabled_attr ) { printf( '', esc_attr( self::OPTION_NAME ) ); } printf( '', esc_attr( self::OPTION_NAME ), checked( $is_checked, true, false ), $disabled_attr, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Value comes from WordPress disabled() function. esc_html__( 'Prevent users from overriding the global verification frequency.', 'robotstxt-2fa' ) ); echo '

' . esc_html__( 'When enabled, users will see the profile frequency control disabled and must follow the global schedule.', 'robotstxt-2fa' ) . '

'; } /** * Render the data management section description. * * @return void */ public function render_data_section(): void { echo '

' . esc_html__( 'Control what happens to plugin data when the plugin is deleted.', 'robotstxt-2fa' ) . '

'; } /** * Render the delete-on-uninstall checkbox. * * @return void */ public function render_delete_on_uninstall_field(): void { $settings = $this->get_settings(); $is_checked = $settings['delete_on_uninstall']; printf( '', esc_attr( self::OPTION_NAME ) ); printf( '', esc_attr( self::OPTION_NAME ), checked( $is_checked, true, false ), esc_html__( 'Remove all plugin data when the plugin is deleted.', 'robotstxt-2fa' ) ); echo '

' . esc_html__( 'When checked, deleting this plugin will permanently remove all settings, per-user 2FA preferences, stored secrets, recovery codes, and verification history. This action cannot be undone. Leave unchecked (default) to preserve all data if you reinstall later.', 'robotstxt-2fa' ) . '

'; } /** * Handle the network admin settings form submission. * * WordPress's Settings API does not work in network admin; this handler * validates the nonce, sanitizes input, and saves via update_site_option(). * * @return void */ public function handle_network_save(): void { check_admin_referer( 'robotstxt_2fa_network_save', 'robotstxt_2fa_network_nonce' ); if ( ! current_user_can( 'manage_network_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to access this page.', 'robotstxt-2fa' ) ); } $raw = isset( $_POST[ self::OPTION_NAME ] ) && is_array( $_POST[ self::OPTION_NAME ] ) ? wp_unslash( $_POST[ self::OPTION_NAME ] ) : array(); $sanitized = $this->sanitize_settings( $raw ); update_site_option( self::OPTION_NAME, $sanitized ); wp_safe_redirect( add_query_arg( array( 'page' => 'robotstxt-2fa-settings', 'updated' => 'true', ), network_admin_url( 'admin.php' ) ) ); exit; } /** * Determine the capability required to manage settings. * * @return string */ private function get_required_capability(): string { return $this->is_network_context() ? 'manage_network_options' : 'manage_options'; } /** * Check if the current request runs in a network admin context. * * @return bool */ private function is_network_context(): bool { return is_multisite() && is_network_admin(); } /** * Determine if network-level settings have been stored. * * @return bool */ private function has_network_settings(): bool { if ( ! is_multisite() ) { return false; } return false !== get_site_option( self::OPTION_NAME, false ); } /** * Register the plugin admin page in the desired context. * * @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' ); } }