Add method for sending confirmation email with duplicates check
[MAILPOET-2876]
This commit is contained in:
committed by
Veljko V
parent
2697bbc9fe
commit
c6bb38cc69
@ -32,6 +32,9 @@ class ConfirmationEmailMailer {
|
||||
/** @var SubscriptionUrlFactory */
|
||||
private $subscriptionUrlFactory;
|
||||
|
||||
/** @var array Cache for confirmation emails sent within a request */
|
||||
private $sentEmails = [];
|
||||
|
||||
public function __construct(Mailer $mailer, WPFunctions $wp, SettingsController $settings, SubscriptionUrlFactory $subscriptionUrlFactory) {
|
||||
$this->mailer = $mailer;
|
||||
$this->wp = $wp;
|
||||
@ -40,6 +43,18 @@ class ConfirmationEmailMailer {
|
||||
$this->subscriptionUrlFactory = $subscriptionUrlFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this method if you want to make sure the confirmation email
|
||||
* is not sent multiple times within a single request
|
||||
* e.g. if sending confirmation emails from hooks
|
||||
*/
|
||||
public function sendConfirmationEmailOnce(Subscriber $subscriber): bool {
|
||||
if (isset($this->sentEmails[$subscriber->id])) {
|
||||
return true;
|
||||
}
|
||||
return $this->sendConfirmationEmail($subscriber);
|
||||
}
|
||||
|
||||
public function sendConfirmationEmail(Subscriber $subscriber) {
|
||||
$signupConfirmation = $this->settings->get('signup_confirmation');
|
||||
if ((bool)$signupConfirmation['enabled'] === false) {
|
||||
@ -99,10 +114,12 @@ class ConfirmationEmailMailer {
|
||||
$subscriber->setError(__('Something went wrong with your subscription. Please contact the website owner.', 'mailpoet'));
|
||||
return false;
|
||||
};
|
||||
|
||||
if (!$this->wp->isUserLoggedIn()) {
|
||||
$subscriber->countConfirmations++;
|
||||
$subscriber->save();
|
||||
}
|
||||
$this->sentEmails[$subscriber->id] = true;
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$subscriber->setError(__('Something went wrong with your subscription. Please contact the website owner.', 'mailpoet'));
|
||||
|
Reference in New Issue
Block a user