745 lines
23 KiB
PHP
745 lines
23 KiB
PHP
<?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';
|
|
|
|
/**
|
|
* Verification methods available for per-role configuration.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
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';
|
|
?>
|
|
<div class="wrap">
|
|
<h1><?php esc_html_e( 'Two-Factor Authentication settings', 'robotstxt-2fa' ); ?></h1>
|
|
<form action="<?php echo esc_url( $form_action ); ?>" method="post">
|
|
<?php
|
|
if ( $this->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();
|
|
?>
|
|
</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(),
|
|
'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 '<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 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 '<p class="description">' . esc_html__( 'No editable roles were found.', 'robotstxt-2fa' ) . '</p>';
|
|
return;
|
|
}
|
|
|
|
$methods = array(
|
|
'email' => __( 'Email code', 'robotstxt-2fa' ),
|
|
'otp' => __( 'Authenticator app', 'robotstxt-2fa' ),
|
|
'recovery_codes' => __( 'Recovery codes', 'robotstxt-2fa' ),
|
|
);
|
|
?>
|
|
<table class="widefat fixed striped robotstxt-2fa-role-methods-table" id="robotstxt-2fa-role-methods-table" style="max-width:680px;">
|
|
<thead>
|
|
<tr>
|
|
<th><?php esc_html_e( 'Role', 'robotstxt-2fa' ); ?></th>
|
|
<?php foreach ( $methods as $method_key => $method_label ) : ?>
|
|
<th style="text-align:center;">
|
|
<?php echo esc_html( $method_label ); ?>
|
|
<?php if ( ! $locked ) : ?>
|
|
<br />
|
|
<label style="font-weight:normal;font-size:11px;cursor:pointer;">
|
|
<input
|
|
type="checkbox"
|
|
class="robotstxt-2fa-col-toggle"
|
|
data-2fa-col="<?php echo esc_attr( $method_key ); ?>"
|
|
style="margin-top:4px;"
|
|
/>
|
|
<?php esc_html_e( 'All', 'robotstxt-2fa' ); ?>
|
|
</label>
|
|
<?php endif; ?>
|
|
</th>
|
|
<?php endforeach; ?>
|
|
<?php if ( ! $locked ) : ?>
|
|
<th style="text-align:center;font-weight:normal;font-size:11px;">
|
|
<?php esc_html_e( 'All methods', 'robotstxt-2fa' ); ?>
|
|
</th>
|
|
<?php endif; ?>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ( $roles as $role_key => $role_details ) : ?>
|
|
<tr data-2fa-role="<?php echo esc_attr( $role_key ); ?>">
|
|
<td><?php echo esc_html( translate_user_role( $role_details['name'] ) ); ?></td>
|
|
<?php foreach ( $methods as $method_key => $method_label ) : ?>
|
|
<td style="text-align:center;">
|
|
<label>
|
|
<span class="screen-reader-text">
|
|
<?php
|
|
printf(
|
|
/* translators: 1: role name, 2: method name. */
|
|
esc_html__( '%1$s: require %2$s', 'robotstxt-2fa' ),
|
|
esc_html( translate_user_role( $role_details['name'] ) ),
|
|
esc_html( $method_label )
|
|
);
|
|
?>
|
|
</span>
|
|
<input
|
|
type="checkbox"
|
|
class="robotstxt-2fa-cell"
|
|
data-2fa-role="<?php echo esc_attr( $role_key ); ?>"
|
|
data-2fa-method="<?php echo esc_attr( $method_key ); ?>"
|
|
name="<?php echo esc_attr( self::OPTION_NAME ); ?>[role_methods][<?php echo esc_attr( $role_key ); ?>][]"
|
|
value="<?php echo esc_attr( $method_key ); ?>"
|
|
<?php checked( in_array( $method_key, $role_methods[ $role_key ] ?? array(), true ) ); ?>
|
|
<?php echo $disabled; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Value comes from WordPress disabled() function. ?>
|
|
/>
|
|
</label>
|
|
</td>
|
|
<?php endforeach; ?>
|
|
<?php if ( ! $locked ) : ?>
|
|
<td style="text-align:center;">
|
|
<label style="font-size:11px;cursor:pointer;">
|
|
<input
|
|
type="checkbox"
|
|
class="robotstxt-2fa-row-toggle"
|
|
data-2fa-row="<?php echo esc_attr( $role_key ); ?>"
|
|
/>
|
|
<?php esc_html_e( 'All', 'robotstxt-2fa' ); ?>
|
|
</label>
|
|
</td>
|
|
<?php endif; ?>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<p class="description">
|
|
<?php esc_html_e( 'Check which verification methods are required for each role. Users assigned to a role with at least one checked method must complete two-factor authentication using one of those methods. Roles with no methods checked leave the choice to the user.', 'robotstxt-2fa' ); ?>
|
|
</p>
|
|
<?php if ( ! $locked ) : ?>
|
|
<script>
|
|
(function () {
|
|
var table = document.getElementById( 'robotstxt-2fa-role-methods-table' );
|
|
if ( ! table ) { return; }
|
|
|
|
function cells() {
|
|
return table.querySelectorAll( '.robotstxt-2fa-cell' );
|
|
}
|
|
|
|
function updateToggle( toggle, checked, total ) {
|
|
if ( total === 0 ) { return; }
|
|
toggle.indeterminate = checked > 0 && checked < total;
|
|
toggle.checked = checked === total;
|
|
}
|
|
|
|
function syncRow( roleKey ) {
|
|
var rowCells = table.querySelectorAll( '.robotstxt-2fa-cell[data-2fa-role="' + roleKey + '"]' );
|
|
var rowToggle = table.querySelector( '.robotstxt-2fa-row-toggle[data-2fa-row="' + roleKey + '"]' );
|
|
if ( ! rowToggle ) { return; }
|
|
var checked = 0;
|
|
rowCells.forEach( function ( c ) { if ( c.checked ) { checked++; } } );
|
|
updateToggle( rowToggle, checked, rowCells.length );
|
|
}
|
|
|
|
function syncCol( methodKey ) {
|
|
var colCells = table.querySelectorAll( '.robotstxt-2fa-cell[data-2fa-method="' + methodKey + '"]' );
|
|
var colToggle = table.querySelector( '.robotstxt-2fa-col-toggle[data-2fa-col="' + methodKey + '"]' );
|
|
if ( ! colToggle ) { return; }
|
|
var checked = 0;
|
|
colCells.forEach( function ( c ) { if ( c.checked ) { checked++; } } );
|
|
updateToggle( colToggle, checked, colCells.length );
|
|
}
|
|
|
|
/* Sync all toggles on load */
|
|
var allRoles = {}, allMethods = {};
|
|
cells().forEach( function ( c ) {
|
|
allRoles[ c.dataset['2faRole'] ] = true;
|
|
allMethods[ c.dataset['2faMethod'] ] = true;
|
|
} );
|
|
Object.keys( allRoles ).forEach( syncRow );
|
|
Object.keys( allMethods ).forEach( syncCol );
|
|
|
|
/* Row toggle: check / uncheck all cells in that role's row */
|
|
table.querySelectorAll( '.robotstxt-2fa-row-toggle' ).forEach( function ( toggle ) {
|
|
toggle.addEventListener( 'change', function () {
|
|
var roleKey = toggle.dataset['2faRow'];
|
|
table.querySelectorAll( '.robotstxt-2fa-cell[data-2fa-role="' + roleKey + '"]' ).forEach( function ( c ) {
|
|
c.checked = toggle.checked;
|
|
} );
|
|
Object.keys( allMethods ).forEach( syncCol );
|
|
} );
|
|
} );
|
|
|
|
/* Column toggle: check / uncheck all cells in that method's column */
|
|
table.querySelectorAll( '.robotstxt-2fa-col-toggle' ).forEach( function ( toggle ) {
|
|
toggle.addEventListener( 'change', function () {
|
|
var methodKey = toggle.dataset['2faCol'];
|
|
table.querySelectorAll( '.robotstxt-2fa-cell[data-2fa-method="' + methodKey + '"]' ).forEach( function ( c ) {
|
|
c.checked = toggle.checked;
|
|
} );
|
|
Object.keys( allRoles ).forEach( syncRow );
|
|
} );
|
|
} );
|
|
|
|
/* Individual cell: keep toggles in sync */
|
|
cells().forEach( function ( c ) {
|
|
c.addEventListener( 'change', function () {
|
|
syncRow( c.dataset['2faRole'] );
|
|
syncCol( c.dataset['2faMethod'] );
|
|
} );
|
|
} );
|
|
}());
|
|
</script>
|
|
<?php endif; ?>
|
|
<?php
|
|
}
|
|
|
|
/**
|
|
* Sanitize settings before saving them.
|
|
*
|
|
* @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}
|
|
*/
|
|
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<string, array<int, string>>, 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<string, array<int, string>>, 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<string, mixed> $options Raw stored options.
|
|
*
|
|
* @return array<string, array<int, string>>
|
|
*/
|
|
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 '<select name="' . esc_attr( self::OPTION_NAME ) . '[frequency]" ' . $disabled_attr . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Value comes from WordPress disabled() function.
|
|
|
|
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 = $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, // 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 '<p class="description">' . esc_html__( 'When enabled, users will see the profile frequency control disabled and must follow the global schedule.', 'robotstxt-2fa' ) . '</p>';
|
|
}
|
|
|
|
/**
|
|
* Render the data management section description.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function render_data_section(): void {
|
|
echo '<p>' . esc_html__( 'Control what happens to plugin data when the plugin is deleted.', 'robotstxt-2fa' ) . '</p>';
|
|
}
|
|
|
|
/**
|
|
* 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(
|
|
'<input type="hidden" name="%1$s[delete_on_uninstall]" value="0" />',
|
|
esc_attr( self::OPTION_NAME )
|
|
);
|
|
|
|
printf(
|
|
'<label><input type="checkbox" name="%1$s[delete_on_uninstall]" value="1" %2$s /> %3$s</label>',
|
|
esc_attr( self::OPTION_NAME ),
|
|
checked( $is_checked, true, false ),
|
|
esc_html__( 'Remove all plugin data when the plugin is deleted.', 'robotstxt-2fa' )
|
|
);
|
|
|
|
echo '<p class="description">' . 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' ) . '</p>';
|
|
}
|
|
|
|
/**
|
|
* 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'
|
|
);
|
|
}
|
|
}
|