This commit is contained in:
Javier Casares 2026-06-05 16:40:56 +00:00
commit 17078a91bd
3 changed files with 123 additions and 49 deletions

View file

@ -62,6 +62,16 @@ class Frontend_Profile {
* @return void
*/
public function maybe_handle_frontend_save(): void {
// On wp-admin pages the profile form is already handled by the
// personal_options_update / edit_user_profile_update hooks that fire
// later in the admin bootstrap (when all admin includes are loaded).
// Running here too would call save_profile_settings() twice and
// trigger a fatal because add_settings_error() is not yet available
// at init time in admin context.
if ( is_admin() ) {
return;
}
if ( 'POST' !== $_SERVER['REQUEST_METHOD'] ) {
return;
}

View file

@ -383,7 +383,7 @@ class Profile_Settings {
</p>
<?php endif; ?>
<p class="description">
<?php esc_html_e( 'Enter the current six-digit code from your authenticator app to activate this method.', 'robotstxt-2fa' ); ?>
<?php esc_html_e( 'Enter the current six-digit code from your authenticator app to activate this method. Submitting a valid code activates the authenticator app even if the Enable checkbox above is not checked.', 'robotstxt-2fa' ); ?>
</p>
<label class="screen-reader-text" for="robotstxt-2fa-otp-code">
<?php esc_html_e( 'Authenticator code', 'robotstxt-2fa' ); ?>
@ -532,7 +532,10 @@ class Profile_Settings {
function applyState() {
var on = toggle.checked;
methods.querySelectorAll( 'input, select, button' ).forEach( function ( el ) {
// Only disable checkboxes and select elements — text inputs (OTP code,
// recovery confirmation) must remain submittable so entering a code
// activates the method even when the Enable toggle is off.
methods.querySelectorAll( 'input[type="checkbox"], select' ).forEach( function ( el ) {
var isRequired = el.closest( '[data-2fa-required]' );
if ( ! isRequired ) {
el.disabled = ! on;
@ -588,8 +591,18 @@ class Profile_Settings {
$raw_fmt = get_option( 'date_format' );
$fmt = is_string( $raw_fmt ) ? $raw_fmt : 'Y-m-d';
?>
<td><?php echo esc_html( wp_date( $fmt, $device['created_at'] ) ?: '' ); ?></td>
<td><?php echo esc_html( wp_date( $fmt, $device['expires_at'] ) ?: '' ); ?></td>
<td>
<?php
$created = wp_date( $fmt, $device['created_at'] );
echo esc_html( false !== $created ? $created : '' );
?>
</td>
<td>
<?php
$expires = wp_date( $fmt, $device['expires_at'] );
echo esc_html( false !== $expires ? $expires : '' );
?>
</td>
<td>
<button type="submit" class="button button-small" name="<?php echo esc_attr( self::REVOKE_DEVICE_FIELD ); ?>" value="<?php echo esc_attr( $token_hash ); ?>">
<?php esc_html_e( 'Revoke', 'robotstxt-2fa' ); ?>
@ -717,6 +730,14 @@ class Profile_Settings {
* @return void
*/
public function save_profile_settings( int $user_id ): void {
// add_settings_error() is defined in wp-admin/includes/template.php which
// may not be loaded when this method is called outside of a standard admin
// request (e.g. via the [robotstxt_2fa_profile] shortcode on the frontend
// or during the init hook on multisite). Load it on demand.
if ( ! function_exists( 'add_settings_error' ) ) {
require_once ABSPATH . 'wp-admin/includes/template.php';
}
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return;
}
@ -795,7 +816,7 @@ class Profile_Settings {
$recovery_was_enabled = in_array( 'recovery_codes', $previous_methods, true );
// Handle recovery code regeneration request — replaces the full batch.
if ( isset( $_POST[ self::RECOVERY_REGENERATE_FIELD ] ) && $recovery_was_enabled ) {
if ( '1' === ( isset( $_POST[ self::RECOVERY_REGENERATE_FIELD ] ) && is_string( $_POST[ self::RECOVERY_REGENERATE_FIELD ] ) ? $_POST[ self::RECOVERY_REGENERATE_FIELD ] : '' ) && $recovery_was_enabled ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified above.
try {
$result = $this->recovery_codes->regenerate_codes_for_user( $user );
} catch ( \Throwable $exception ) {
@ -812,6 +833,19 @@ class Profile_Settings {
}
$this->remember_recovery_preview( $user->ID, $result['codes'], $result['generated_at'] );
// Temporarily remove recovery_codes from active methods so the confirmation
// field is shown, exactly as in the initial-activation flow.
$methods_without_recovery = array_values( array_diff( $previous_methods, array( 'recovery_codes' ) ) );
$this->user_settings_repository->save_user_settings(
$user_id,
array(
'enabled' => $is_forced || ! empty( $methods_without_recovery ),
'methods' => $methods_without_recovery,
'frequency' => $user_settings['frequency'],
)
);
$this->focus_section = true;
add_settings_error( 'robotstxt-2fa', 'robotstxt-2fa-recovery-regenerated', __( 'New recovery codes generated. Copy them and confirm one to activate.', 'robotstxt-2fa' ), 'updated' );
return;
@ -837,7 +871,13 @@ class Profile_Settings {
$selected_methods[] = 'email';
}
$otp_requested = in_array( 'otp', $requested_methods, true );
$otp_requested = in_array( 'otp', $requested_methods, true );
// If the user entered a code without checking the checkbox, treat it as an activation attempt.
if ( ! $otp_was_enabled && ! $otp_requested && '' !== $otp_code ) {
$otp_requested = true;
}
$otp_should_remain_enabled = false;
if ( $otp_was_enabled ) {
@ -868,6 +908,7 @@ class Profile_Settings {
);
} elseif ( $this->otp_manager->verify_code( $user, $otp_code ) ) {
$selected_methods[] = 'otp';
$enabled = true;
$this->focus_section = true;
add_settings_error(
'robotstxt-2fa',
@ -933,7 +974,8 @@ class Profile_Settings {
);
}
} elseif ( $recovery_confirmation_success ) {
$selected_methods[] = 'recovery_codes';
$selected_methods[] = 'recovery_codes';
$enabled = true;
} elseif ( $recovery_requested ) {
$this->focus_section = true;
add_settings_error(

View file

@ -92,10 +92,7 @@ class Two_Factor_Config {
* @param array<int, string> $required Method slugs required by the user's roles.
* @param \WP_User $user User being authenticated.
*/
/**
* @var array<int, string> $filtered
* Note: at runtime a callback may return any type; the map below sanitizes it.
*/
// @phpstan-var array<int, string> $filtered — apply_filters returns mixed; sanitized below.
$filtered = apply_filters( 'robotstxt_2fa_required_methods_for_user', $required, $user );
return array_values( array_filter( $filtered ) );