v0.2.0
This commit is contained in:
commit
f936aeb807
1 changed files with 15710 additions and 0 deletions
415
includes/admin/class-settings-page.php
Normal file
415
includes/admin/class-settings-page.php
Normal file
|
|
@ -0,0 +1,415 @@
|
|||
<?php
|
||||
/**
|
||||
* Admin settings management.
|
||||
*
|
||||
* @package Robotstxt_2FA
|
||||
*/
|
||||
|
||||
namespace Robotstxt\TwoFA\Admin;
|
||||
|
||||
use Robotstxt\TwoFA\Frequency_Options;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles plugin settings page and enforcement logic configuration.
|
||||
*/
|
||||
class Settings_Page {
|
||||
/**
|
||||
* Option group for settings registration.
|
||||
*/
|
||||
private const OPTION_GROUP = 'robotstxt_2fa_settings_group';
|
||||
|
||||
/**
|
||||
* Option name for storing settings.
|
||||
*/
|
||||
public const OPTION_NAME = 'robotstxt_2fa_settings';
|
||||
|
||||
/**
|
||||
* Settings page slug.
|
||||
*/
|
||||
private const PAGE_SLUG = 'robotstxt-2fa-settings';
|
||||
|
||||
/**
|
||||
* 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( '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 );
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1><?php esc_html_e( 'Two-Factor Authentication settings', 'robotstxt-2fa' ); ?></h1>
|
||||
<form action="options.php" method="post">
|
||||
<?php
|
||||
settings_fields( self::OPTION_GROUP );
|
||||
do_settings_sections( self::PAGE_SLUG );
|
||||
submit_button();
|
||||
?>
|
||||
</form>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Register settings and sections.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_settings(): void {
|
||||
register_setting(
|
||||
self::OPTION_GROUP,
|
||||
self::OPTION_NAME,
|
||||
array(
|
||||
'type' => 'array',
|
||||
'sanitize_callback' => array( $this, 'sanitize_settings' ),
|
||||
'default' => $this->get_default_settings(),
|
||||
)
|
||||
);
|
||||
|
||||
add_settings_section(
|
||||
'robotstxt_2fa_general',
|
||||
__( 'General options', 'robotstxt-2fa' ),
|
||||
array( $this, 'render_general_section' ),
|
||||
self::PAGE_SLUG
|
||||
);
|
||||
|
||||
add_settings_field(
|
||||
'robotstxt_2fa_force_roles',
|
||||
__( 'Forced roles', 'robotstxt-2fa' ),
|
||||
array( $this, 'render_force_roles_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'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render general settings section description.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function render_general_section(): void {
|
||||
echo '<p>' . esc_html__( 'Control how two-factor authentication is enforced for user roles.', 'robotstxt-2fa' ) . '</p>';
|
||||
|
||||
if ( $this->has_network_settings() && ! $this->is_network_context() ) {
|
||||
echo '<p class="description">' . esc_html__( 'These settings are locked by a network administrator. Any changes made here will be ignored.', 'robotstxt-2fa' ) . '</p>';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render forced roles field.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function render_force_roles_field(): void {
|
||||
$settings = $this->get_settings();
|
||||
$selected_roles = array();
|
||||
$disabled_attr = '';
|
||||
|
||||
if ( isset( $settings['force_roles'] ) && is_array( $settings['force_roles'] ) ) {
|
||||
$selected_roles = $settings['force_roles'];
|
||||
}
|
||||
|
||||
if ( $this->has_network_settings() && ! $this->is_network_context() ) {
|
||||
$disabled_attr = disabled( true, true, false );
|
||||
}
|
||||
|
||||
$roles = get_editable_roles();
|
||||
|
||||
if ( empty( $roles ) ) {
|
||||
echo '<p class="description">' . esc_html__( 'No editable roles were found.', 'robotstxt-2fa' ) . '</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( $roles as $role_key => $role_details ) {
|
||||
printf(
|
||||
'<label><input type="checkbox" name="%1$s[force_roles][]" value="%2$s" %3$s %4$s/> %5$s</label><br />',
|
||||
esc_attr( self::OPTION_NAME ),
|
||||
esc_attr( $role_key ),
|
||||
checked( in_array( $role_key, $selected_roles, true ), true, false ),
|
||||
$disabled_attr,
|
||||
esc_html( translate_user_role( $role_details['name'] ) )
|
||||
);
|
||||
}
|
||||
|
||||
echo '<p class="description">' . esc_html__( 'Users assigned to the selected roles must enable two-factor authentication. Leave all roles unchecked to allow each user to decide whether to activate 2FA.', 'robotstxt-2fa' ) . '</p>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize settings before saving them.
|
||||
*
|
||||
* @param mixed $raw_settings Raw settings submitted by the user.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
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();
|
||||
|
||||
$sanitized['force_roles'] = array();
|
||||
|
||||
if ( isset( $raw_settings['force_roles'] ) && is_array( $raw_settings['force_roles'] ) ) {
|
||||
$editable_roles = array_keys( get_editable_roles() );
|
||||
|
||||
foreach ( $raw_settings['force_roles'] as $role_key ) {
|
||||
$role_key = sanitize_key( $role_key );
|
||||
|
||||
if ( in_array( $role_key, $editable_roles, true ) ) {
|
||||
$sanitized['force_roles'][] = $role_key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$sanitized['force_roles'] = array_values( array_unique( $sanitized['force_roles'] ) );
|
||||
|
||||
$frequency_value = $sanitized['frequency'];
|
||||
|
||||
if ( isset( $current_settings['frequency'] ) ) {
|
||||
$frequency_value = (string) $current_settings['frequency'];
|
||||
}
|
||||
|
||||
if ( isset( $raw_settings['frequency'] ) ) {
|
||||
$frequency_value = (string) $raw_settings['frequency'];
|
||||
}
|
||||
|
||||
$sanitized['frequency'] = Frequency_Options::sanitize( $frequency_value );
|
||||
|
||||
if ( $this->has_network_settings() && ! $this->is_network_context() ) {
|
||||
$sanitized['force_frequency'] = ! empty( $current_settings['force_frequency'] ?? false );
|
||||
} elseif ( array_key_exists( 'force_frequency', $raw_settings ) ) {
|
||||
$sanitized['force_frequency'] = '1' === (string) $raw_settings['force_frequency'];
|
||||
}
|
||||
|
||||
return $sanitized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve plugin settings.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function get_settings(): array {
|
||||
$options = array();
|
||||
|
||||
if ( is_multisite() ) {
|
||||
/** @var array<string, mixed>|false $network_options */
|
||||
$network_options = get_site_option( self::OPTION_NAME, false );
|
||||
|
||||
if ( false !== $network_options && is_array( $network_options ) ) {
|
||||
$options = $network_options;
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty( $options ) ) {
|
||||
/** @var array<string, mixed> $options */
|
||||
$options = get_option( self::OPTION_NAME, array() );
|
||||
}
|
||||
|
||||
return wp_parse_args( $options, $this->get_default_settings() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve default settings.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function get_default_settings(): array {
|
||||
return array(
|
||||
'force_roles' => array(),
|
||||
'default_method' => 'email',
|
||||
'frequency' => Frequency_Options::FREQUENCY_SESSION,
|
||||
'force_frequency' => false,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render verification frequency field.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function render_frequency_field(): void {
|
||||
$settings = $this->get_settings();
|
||||
$selected = Frequency_Options::sanitize( (string) $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 '<select name="' . esc_attr( self::OPTION_NAME ) . '[frequency]" ' . $disabled_attr . '>';
|
||||
|
||||
foreach ( $options as $value => $labels ) {
|
||||
printf(
|
||||
'<option value="%1$s" %2$s>%3$s</option>',
|
||||
esc_attr( $value ),
|
||||
selected( $selected, $value, false ),
|
||||
esc_html( $labels['label'] )
|
||||
);
|
||||
}
|
||||
|
||||
echo '</select>';
|
||||
|
||||
if ( isset( $options[ $selected ]['description'] ) && '' !== $options[ $selected ]['description'] ) {
|
||||
echo '<p class="description">' . esc_html( $options[ $selected ]['description'] ) . '</p>';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render frequency enforcement checkbox.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function render_force_frequency_field(): void {
|
||||
$settings = $this->get_settings();
|
||||
$is_checked = ! empty( $settings['force_frequency'] );
|
||||
$disabled_attr = '';
|
||||
|
||||
if ( $this->has_network_settings() && ! $this->is_network_context() ) {
|
||||
$disabled_attr = disabled( true, true, false );
|
||||
}
|
||||
|
||||
if ( '' === $disabled_attr ) {
|
||||
printf(
|
||||
'<input type="hidden" name="%1$s[force_frequency]" value="0" />',
|
||||
esc_attr( self::OPTION_NAME )
|
||||
);
|
||||
}
|
||||
|
||||
printf(
|
||||
'<label><input type="checkbox" name="%1$s[force_frequency]" value="1" %2$s %3$s/> %4$s</label>',
|
||||
esc_attr( self::OPTION_NAME ),
|
||||
checked( $is_checked, true, false ),
|
||||
$disabled_attr,
|
||||
esc_html__( 'Prevent users from overriding the global verification frequency.', 'robotstxt-2fa' )
|
||||
);
|
||||
|
||||
echo '<p class="description">' . esc_html__( 'When enabled, users will see the profile frequency control disabled and must follow the global schedule.', 'robotstxt-2fa' ) . '</p>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the capability required to manage settings.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_required_capability(): string {
|
||||
return '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'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in a new issue