Problem/Motivation
We encountered a problem using simplenews subscriptions. Subscribers ended up as active subscribers without running through desired confirmation process. This worked on D9 with simplenews v3.x but not with simplenews v4.x as the implementation of the subcribe function in the SubscriptionManager class has changed.
Steps to reproduce
Subscribe a user and check the status afterwards, it's stored as active without prior confirmation email
Proposed resolution
Change subscribe function. We've tested the solution in our environment and with the changes the solution works as expected.
Original code:
public function subscribe(string $mail, string $newsletter_id, string $preferred_langcode = NULL) {
if (func_num_args() > 3) {
throw new \LogicException('Only 3 arguments are supported.');
}
// Get/create subscriber entity.
$preferred_langcode = $preferred_langcode ?? $this->languageManager->getCurrentLanguage()->getId();
$subscriber = Subscriber::loadByMail($mail, 'create', $preferred_langcode);
//$subscriber->setStatus(SubscriberInterface::UNCONFIRMED);
$subscriber->subscribe($newsletter_id)->save();
return $this;
}
Proposed code:
public function subscribe(string $mail, string $newsletter_id, string $preferred_langcode = NULL) {
if (func_num_args() > 3) {
throw new \LogicException('Only 3 arguments are supported.');
}
// Get/create subscriber entity.
$preferred_langcode = $preferred_langcode ?? $this->languageManager->getCurrentLanguage()->getId();
// require trusted check
$subscriber = Subscriber::loadByMail($mail, 'create', $preferred_langcode, true);
//$subscriber->setStatus(SubscriberInterface::UNCONFIRMED);
$subscriber->subscribe($newsletter_id)->save();
// call sendConfirmation function which won't send confirmation email if unnecessary
$subscriber->sendConfirmation();
return $this;
}
Remaining tasks
Testing