65 lines
2.1 KiB
PHP
65 lines
2.1 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Materio Mailjet: webhook support and simplenews-to-Mailjet list sync.
|
|
*/
|
|
|
|
use Drupal\simplenews\Entity\Subscriber;
|
|
|
|
/**
|
|
* Invoked if a subscriber is subscribed to a newsletter.
|
|
*
|
|
* Pushes the subscription to every Mailjet list mapped to the newsletter
|
|
* in materio_mailjet.settings.
|
|
*/
|
|
function materio_mailjet_simplenews_subscribe(Subscriber $subscriber, string $subscription) {
|
|
\Drupal::service('materio_mailjet.sync')->subscribeToNewsletter($subscription, (string) $subscriber->getMail());
|
|
}
|
|
|
|
/**
|
|
* Invoked if a subscriber is unsubscribed from a newsletter.
|
|
*
|
|
* Removes the subscription from every Mailjet list mapped to the newsletter.
|
|
*/
|
|
function materio_mailjet_simplenews_unsubscribe(Subscriber $subscriber, string $subscription) {
|
|
\Drupal::service('materio_mailjet.sync')->unsubscribeFromNewsletter($subscription, (string) $subscriber->getMail());
|
|
}
|
|
|
|
/**
|
|
* Act after a subscriber is updated.
|
|
*
|
|
* When the email changes, the old email is removed from the mapped lists
|
|
* and the new one is added.
|
|
*/
|
|
function materio_mailjet_simplenews_subscriber_update(Subscriber $subscriber) {
|
|
$new_mail = $subscriber->getMail();
|
|
$original_mail = $subscriber->original->getMail();
|
|
if ($new_mail === $original_mail) {
|
|
return;
|
|
}
|
|
|
|
$new_subscriptions = $subscriber->getSubscribedNewsletterIds();
|
|
$original_subscriptions = $subscriber->original->getSubscribedNewsletterIds();
|
|
$sync = \Drupal::service('materio_mailjet.sync');
|
|
|
|
$mapping = \Drupal::config('materio_mailjet.settings')->get('list_mapping') ?? [];
|
|
foreach ($mapping as $list_id => $newsletter_id) {
|
|
if (in_array($newsletter_id, $original_subscriptions)) {
|
|
$sync->unsubscribeFromList((string) $list_id, (string) $original_mail);
|
|
}
|
|
if (in_array($newsletter_id, $new_subscriptions)) {
|
|
$sync->subscribeToList((string) $list_id, (string) $new_mail);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Act after a subscriber has been deleted.
|
|
*
|
|
* Removes the email from every mapped Mailjet list.
|
|
*/
|
|
function materio_mailjet_simplenews_subscriber_delete(Subscriber $subscriber) {
|
|
\Drupal::service('materio_mailjet.sync')->unsubscribeFromAllLists((string) $subscriber->getMail());
|
|
}
|