This commit is contained in:
Javier Casares 2026-06-05 14:07:16 +00:00
commit d405d8587d

View file

@ -45,11 +45,6 @@ class Profile_Settings {
*/
private const OTP_CODE_FIELD = 'robotstxt_2fa_otp_code';
/**
* Form field used to request a new OTP secret.
*/
private const OTP_REGENERATE_FIELD = 'robotstxt_2fa_regenerate_otp';
/**
* Form field used to confirm recovery codes.
*/
@ -354,7 +349,7 @@ class Profile_Settings {
<?php esc_html_e( 'Scan the QR code or enter the setup key below in your authenticator app.', 'robotstxt-2fa' ); ?>
</p>
<?php if ( '' !== $otp_qr_data_uri ) : ?>
<img src="<?php echo esc_url( $otp_qr_data_uri ); ?>" alt="<?php esc_attr_e( 'Authenticator app QR code', 'robotstxt-2fa' ); ?>" class="robotstxt-2fa-otp-qr" />
<img src="<?php echo esc_url( $otp_qr_data_uri, array_merge( wp_allowed_protocols(), array( 'data' ) ) ); ?>" alt="<?php esc_attr_e( 'Authenticator app QR code', 'robotstxt-2fa' ); ?>" class="robotstxt-2fa-otp-qr" />
<?php else : ?>
<p class="description error">
<?php esc_html_e( 'Install the QR code library and enable the XMLWriter extension to display the authenticator code image. Run the following command inside this plugin directory:', 'robotstxt-2fa' ); ?>
@ -374,13 +369,7 @@ class Profile_Settings {
<?php esc_html_e( 'Authenticator code', 'robotstxt-2fa' ); ?>
</label>
<input type="text" name="<?php echo esc_attr( self::OTP_CODE_FIELD ); ?>" id="robotstxt-2fa-otp-code" pattern="\\d{<?php echo absint( $otp_digits ); ?>}" inputmode="numeric" autocomplete="one-time-code" />
<p class="description">
<?php esc_html_e( 'Need a different pairing? Use the button below to generate a new QR code.', 'robotstxt-2fa' ); ?>
</p>
<?php endif; ?>
<button type="submit" class="button button-secondary" name="<?php echo esc_attr( self::OTP_REGENERATE_FIELD ); ?>" value="1">
<?php esc_html_e( 'Generate new secret', 'robotstxt-2fa' ); ?>
</button>
</div>
<?php $recovery_required = in_array( 'recovery_codes', $required_methods, true ); ?>
<?php if ( $recovery_required ) : ?>
@ -426,42 +415,33 @@ class Profile_Settings {
</p>
<?php endif; ?>
<?php if ( $has_preview ) : ?>
<div class="notice notice-success inline robotstxt-2fa-recovery-preview" data-robotstxt-2fa-codes-scope="1">
<div class="robotstxt-2fa-recovery-preview" data-robotstxt-2fa-codes-scope="1">
<p class="description">
<?php esc_html_e( 'Copy these recovery codes and store them somewhere safe. Each code works only once.', 'robotstxt-2fa' ); ?>
</p>
<div class="robotstxt-2fa-recovery-code-grid" data-robotstxt-2fa-codes-list>
<?php
$code_pairs = array_chunk( $preview_codes, 2 );
foreach ( $code_pairs as $pair ) :
?>
<div class="robotstxt-2fa-recovery-code-row">
<?php foreach ( $pair as $preview_code ) : ?>
<div class="robotstxt-2fa-recovery-code-item">
<code data-robotstxt-2fa-code><?php echo esc_html( $preview_code ); ?></code>
<?php endforeach; ?>
<?php if ( 1 === count( $pair ) ) : ?>
<div class="robotstxt-2fa-recovery-code-item">&nbsp;</div>
<?php endif; ?>
</div>
<ul class="robotstxt-2fa-recovery-code-list" data-robotstxt-2fa-codes-list>
<?php foreach ( $preview_codes as $preview_code ) : ?>
<li><code data-robotstxt-2fa-code><?php echo esc_html( $preview_code ); ?></code></li>
<?php endforeach; ?>
</div>
<button type="button" class="button button-secondary robotstxt-2fa-recovery-copy" data-robotstxt-2fa-copy-button="1">
<?php esc_html_e( 'Copy codes', 'robotstxt-2fa' ); ?>
</button>
<p class="description robotstxt-2fa-recovery-copy-feedback" data-robotstxt-2fa-copy-feedback="1" hidden>
<?php esc_html_e( 'The recovery codes have been copied to your clipboard.', 'robotstxt-2fa' ); ?>
</ul>
<p>
<button type="button" class="button button-secondary robotstxt-2fa-recovery-copy" data-robotstxt-2fa-copy-button="1">
<?php esc_html_e( 'Copy codes', 'robotstxt-2fa' ); ?>
</button>
<span class="robotstxt-2fa-recovery-copy-feedback" data-robotstxt-2fa-copy-feedback="1" hidden>
<?php esc_html_e( '✓ Copied to clipboard', 'robotstxt-2fa' ); ?>
</span>
</p>
<?php if ( ! $recovery_enabled ) : ?>
<div class="robotstxt-2fa-recovery-confirm">
<label class="robotstxt-2fa-recovery-confirm-label" for="robotstxt-2fa-recovery-confirm">
<?php esc_html_e( 'Enter one of the codes above to confirm you saved them.', 'robotstxt-2fa' ); ?>
<label for="robotstxt-2fa-recovery-confirm">
<?php esc_html_e( 'Enter one of the codes above to confirm you saved them:', 'robotstxt-2fa' ); ?>
</label>
<input type="text" name="<?php echo esc_attr( self::RECOVERY_CONFIRM_FIELD ); ?>" id="robotstxt-2fa-recovery-confirm" pattern="\\d{<?php echo absint( $recovery_code_length ); ?>}" inputmode="numeric" autocomplete="one-time-code" />
<p class="description">
<?php esc_html_e( 'Once confirmed, the codes disappear from this page but remain valid until you use them.', 'robotstxt-2fa' ); ?>
</p>
</div>
<p class="description">
<?php esc_html_e( 'Once confirmed, the codes disappear from this page but remain valid until you use them.', 'robotstxt-2fa' ); ?>
</p>
<?php endif; ?>
</div>
<?php elseif ( ! $recovery_enabled ) : ?>
@ -540,7 +520,16 @@ class Profile_Settings {
}
}
toggle.addEventListener( 'change', applyState );
toggle.addEventListener( 'change', function () {
applyState();
// Pre-check email when enabling 2FA for the first time.
if ( toggle.checked ) {
var emailCheckbox = document.getElementById( 'robotstxt-2fa-method-email' );
if ( emailCheckbox && ! emailCheckbox.checked ) {
emailCheckbox.checked = true;
}
}
} );
applyState();
}());
</script>
@ -717,12 +706,6 @@ class Profile_Settings {
$otp_was_enabled = in_array( 'otp', $previous_methods, true );
$recovery_was_enabled = in_array( 'recovery_codes', $previous_methods, true );
$otp_regenerate_requested = isset( $_POST[ self::OTP_REGENERATE_FIELD ] );
if ( $otp_regenerate_requested ) {
$this->focus_section = true;
}
$otp_code = '';
if ( isset( $_POST[ self::OTP_CODE_FIELD ] ) && is_string( $_POST[ self::OTP_CODE_FIELD ] ) ) {
@ -746,21 +729,8 @@ class Profile_Settings {
$otp_requested = in_array( 'otp', $requested_methods, true );
$otp_should_remain_enabled = false;
if ( $otp_regenerate_requested ) {
$this->otp_manager->delete_secret( $user_id );
$this->otp_manager->ensure_secret( $user );
$otp_was_enabled = false;
$otp_requested = true;
add_settings_error(
'robotstxt-2fa',
'robotstxt-2fa-otp-reset',
__( 'A new authenticator secret is ready. Scan the QR code and confirm it with a fresh code to finish reconnecting.', 'robotstxt-2fa' ),
'updated'
);
}
if ( $otp_was_enabled ) {
if ( $otp_requested && ! $otp_regenerate_requested ) {
if ( $otp_requested ) {
$otp_should_remain_enabled = true;
} else {
$this->otp_manager->delete_secret( $user_id );
@ -865,6 +835,11 @@ class Profile_Settings {
$selected_methods = array_values( array_unique( $selected_methods ) );
// When enabling for the first time without selecting a method, default to email.
if ( $enabled && empty( $selected_methods ) && in_array( 'email', $available_keys, true ) ) {
$selected_methods[] = 'email';
}
if ( ! $is_forced && empty( $selected_methods ) ) {
$enabled = false;
}