*/ 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'; ?>
' . 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 ) : ?> |
|
|
|---|---|---|
| $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
' . 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' ); } }