ownsLock( 'notifications', $lease ) && $store->get( $claim['_id'] ) === $claim && self::recipient( Store::freshOption( 'antivirus_ai_settings', array() ) ) === $recipient && $currentBody() === $body; if ( ! $allowed || $writeObserved || $site !== get_current_blog_id() ) { $stop( 'Notification permission, recipient, evidence or attempt changed before mail handoff. No automatic resend is scheduled.' ); } } finally { remove_filter( 'query', $observer, PHP_INT_MAX ); } }; $arguments = static function ( array $atts ) use ( $recipient, $subject, $body, $stop ): void { $to = $atts['to'] ?? null; if ( $to !== $recipient && $to !== array( $recipient ) ) { $stop( 'A mail filter changed the notification recipient. Delivery was stopped.' ); } if ( ( $atts['subject'] ?? null ) !== $subject || ( $atts['message'] ?? null ) !== $body || ! empty( $atts['headers'] ) || ! empty( $atts['attachments'] ) || ! empty( $atts['embeds'] ) ) { $stop( 'A mail filter changed the notification content or added recipients or attachments. Delivery was stopped.' ); } }; $mailGuard = static function ( $atts ) use ( $own, $current, $arguments, $stop ) { if ( ! $own( 'wp_mail' ) ) { return $atts; } if ( ! is_array( $atts ) ) { $stop( 'The notification mail arguments are invalid.' ); } $current(); $arguments( $atts ); return $atts; }; $preGuard = static function ( $short, $atts ) use ( $own, $current, $arguments, $stop ) { if ( ! $own( 'pre_wp_mail' ) ) { return $short; } if ( ! is_array( $atts ) ) { $stop( 'The notification mail arguments are invalid.' ); } $current(); $arguments( $atts ); return $short; }; $mailerGuard = static function ( $mailer ) use ( $own, $current, $recipient, $subject, $body, $stop ): void { if ( ! $own( 'phpmailer_init' ) ) { return; } $current(); if ( ! $mailer instanceof \PHPMailer\PHPMailer\PHPMailer || $mailer->getToAddresses() !== array( array( $recipient, '' ) ) || $mailer->getCcAddresses() || $mailer->getBccAddresses() || $mailer->getReplyToAddresses() || $mailer->getAttachments() || $mailer->Subject !== $subject || $mailer->Body !== $body || '' !== $mailer->AltBody || 'text/plain' !== $mailer->ContentType ) { $stop( 'Mail preparation changed notification content, recipients or attachments. Delivery was stopped.' ); } }; foreach ( array( -PHP_INT_MAX, PHP_INT_MAX ) as $priority ) { add_filter( 'wp_mail', $mailGuard, $priority ); add_filter( 'pre_wp_mail', $preGuard, $priority, 2 ); } add_action( 'phpmailer_init', $mailerGuard, PHP_INT_MAX ); try { $current(); $accepted = wp_mail( $recipient, $subject, $body ); return array( 'accepted' => true === $accepted, 'stopped' => false, 'message' => true === $accepted ? '' : 'The mail outcome is unconfirmed. No automatic resend is scheduled; inspect your mail provider before taking further action.' ); } catch ( \Throwable $error ) { return array( 'accepted' => false, 'stopped' => $stopped, 'message' => $stopped ? $reason : 'Mail processing was interrupted and its outcome is unconfirmed. No automatic resend is scheduled.' ); } finally { foreach ( array( -PHP_INT_MAX, PHP_INT_MAX ) as $priority ) { remove_filter( 'wp_mail', $mailGuard, $priority ); remove_filter( 'pre_wp_mail', $preGuard, $priority ); } remove_action( 'phpmailer_init', $mailerGuard, PHP_INT_MAX ); } } }