This commit is contained in:
Javier Casares 2026-11-04 14:36:03 +00:00
commit 1ff471299e

View file

@ -17,6 +17,13 @@ if ( ! defined( 'ABSPATH' ) ) {
* Handles generation and delivery of email-based verification codes.
*/
class Email_Code_Sender {
/**
* Option key recording the last failed delivery attempt. Kept while mail
* delivery fails so administrators can see why forced-email users cannot
* sign in; cleared automatically once a code is delivered successfully.
*/
public const MAIL_FAILURE_OPTION = 'robotstxt_2fa_last_mail_failure';
/**
* Default length of the generated numeric code.
*/
@ -187,14 +194,57 @@ class Email_Code_Sender {
$headers = array( 'Content-Type: text/plain; charset=UTF-8' );
if ( ! wp_mail( $user->user_email, $email_subject, $message, $headers ) ) {
$this->record_mail_failure( $user->user_email );
return false;
}
$this->clear_mail_failure();
$this->user_settings_repository->store_email_challenge( $user->ID, $code, $expires );
return true;
}
/**
* Record a failed verification-code delivery for administrator visibility.
*
* When a role forces email verification, a failing wp_mail() locks users
* out of their accounts with no site-side signal. Store the failure so an
* admin notice can explain the root cause.
*
* @since 1.6.8
*
* @param string $to Intended recipient.
*
* @return void
*/
private function record_mail_failure( string $to ): void {
$existing = get_option( self::MAIL_FAILURE_OPTION, array() );
$record = is_array( $existing ) ? $existing : array();
update_option(
self::MAIL_FAILURE_OPTION,
array(
'at' => time(),
'to' => $to,
'count' => ( isset( $record['count'] ) && is_numeric( $record['count'] ) ? (int) $record['count'] : 0 ) + 1,
),
false
);
}
/**
* Clear the delivery failure marker after a successful delivery.
*
* @since 1.6.8
*
* @return void
*/
private function clear_mail_failure(): void {
delete_option( self::MAIL_FAILURE_OPTION );
}
/**
* Verify a submitted code against the stored challenge.
*