patched simplenews_mailjet_subscriptions to use mailjet api through mailjet module

This commit is contained in:
Bachir Soussi Chiadmi 2022-06-09 11:43:06 +02:00
parent 212f471ec1
commit c133f71e50
3 changed files with 284 additions and 12 deletions

View File

@ -151,7 +151,7 @@
"drupal/select_translation": "^1.x-dev",
"drupal/session_limit": "^2.0@beta",
"drupal/simplenews": "3.x-dev@dev",
"drupal/simplenews_mailjet_subscriptions": "^1.2",
"drupal/simplenews_mailjet_subscriptions": "1.x-dev@dev",
"drupal/social_media_links": "^2.8",
"drupal/string_translation_ui": "^1.2",
"drupal/strings_i18n_json_export": "^1.x-dev@dev",
@ -303,6 +303,14 @@
},
"drupal/simplenews": {
"Spamassassin points because of using X-MSMail-Priority header without X-MimeOLE":"https://www.drupal.org/files/issues/2022-05-04/msmail-priority-header-without-x-mimeole_2072779%239.patch"
},
"drupal/simplenews_mailjet_subscriptions": {
"var_dump https://www.drupal.org/project/simplenews_mailjet_subscriptions/issues/3280822#comment-14517416":"https://www.drupal.org/files/issues/2022-05-17/3280822-vardump%231.patch",
"missing argument https://www.drupal.org/project/simplenews_mailjet_subscriptions/issues/3280825": "https://www.drupal.org/files/issues/2022-05-17/3280825-missing-argument%232.patch",
"using mailjet module":"./patches/simplenews_mailjet_subscriptions__usingmailjetmodule.patch"
},
"drupal/symfony_mailer":{
"AssertionError: assert($subscriber != NULL) https://www.drupal.org/project/symfony_mailer/issues/3280919": "https://www.drupal.org/files/issues/2022-05-17/assertionerror.patch"
}
},
"drupal-scaffold": {

20
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "37949e67c842c709dfa915af08bb2925",
"content-hash": "c0a6d527b742211bd7eae3923a33555b",
"packages": [
{
"name": "alchemy/zippy",
@ -12989,17 +12989,11 @@
},
{
"name": "drupal/simplenews_mailjet_subscriptions",
"version": "1.2.0",
"version": "dev-1.x",
"source": {
"type": "git",
"url": "https://git.drupalcode.org/project/simplenews_mailjet_subscriptions.git",
"reference": "8.x-1.2"
},
"dist": {
"type": "zip",
"url": "https://ftp.drupal.org/files/projects/simplenews_mailjet_subscriptions-8.x-1.2.zip",
"reference": "8.x-1.2",
"shasum": "ba295d052e87253973d962acd7915e0f4aef80d8"
"reference": "4e64ff5fb58611e0c9cd8d5208ed18dd33226ce6"
},
"require": {
"drupal/core": "^8 || ^9",
@ -13007,9 +13001,12 @@
},
"type": "drupal-module",
"extra": {
"branch-alias": {
"dev-1.x": "1.x-dev"
},
"drupal": {
"version": "8.x-1.2",
"datestamp": "1614341278",
"version": "8.x-1.1+1-dev",
"datestamp": "1614340780",
"security-coverage": {
"status": "not-covered",
"message": "Project has not opted into security advisory coverage!"
@ -25571,6 +25568,7 @@
"drupal/select_translation": 20,
"drupal/session_limit": 10,
"drupal/simplenews": 20,
"drupal/simplenews_mailjet_subscriptions": 20,
"drupal/strings_i18n_json_export": 20,
"drupal/symfony_mailer": 15,
"drupal/telephone_formatter": 10,

View File

@ -0,0 +1,266 @@
diff --git a/simplenews_mailjet_subscriptions.info.yml b/simplenews_mailjet_subscriptions.info.yml
index 2512a39..d4b0440 100644
--- a/simplenews_mailjet_subscriptions.info.yml
+++ b/simplenews_mailjet_subscriptions.info.yml
@@ -7,3 +7,4 @@ package: Mail
configure: simplenews_mailjet_subscription.list
dependencies:
- simplenews:simplenews
+ - mailjet:mailjet
diff --git a/simplenews_mailjet_subscriptions.module b/simplenews_mailjet_subscriptions.module
index 09abe13..4b2ee58 100644
--- a/simplenews_mailjet_subscriptions.module
+++ b/simplenews_mailjet_subscriptions.module
@@ -7,7 +7,7 @@
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\simplenews\Entity\Subscriber;
-use Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsUtilities;
+// use Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsUtilities;
use Drupal\simplenews_mailjet_subscriptions\Entity\SimplenewsMailjetSubscriptionEntity;
use Drupal\Component\Utility\Html;
@@ -24,18 +24,18 @@ use Drupal\Component\Utility\Html;
function simplenews_mailjet_subscriptions_simplenews_subscribe(Subscriber $subscriber, string $subscription) {
$entities = SimplenewsMailjetSubscriptionEntity::loadMultiple();
foreach ((array) $entities as $entity) {
- $api_key = $entity->getApiKey();
- $secret_key = $entity->getSecretkey();
$listID = $entity->getID();
$table = $entity->get('mapping_table');
if (is_array($table)) {
foreach ($table as $category) {
if ($category['simplenews_news'] == $subscription) {
- SimplenewsMailjetSubscriptionsUtilities::subscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID);
+ // SimplenewsMailjetSubscriptionsUtilities::subscribeUser($subscriber, $category);
+ \Drupal::service('simplenews_mailjet_subscriptions.utilities')->subscribeUser($listID, $subscriber, $category);
}
}
}
}
+ // \Drupal::service('simplenews_mailjet_subscriptions.utilities')->subscribeUser($subscriber, $subscription);
}
/**
@@ -51,19 +51,19 @@ function simplenews_mailjet_subscriptions_simplenews_subscribe(Subscriber $subsc
function simplenews_mailjet_subscriptions_simplenews_unsubscribe(Subscriber $subscriber, string $subscription) {
$entities = SimplenewsMailjetSubscriptionEntity::loadMultiple();
foreach ((array) $entities as $entity) {
- $api_key = $entity->getApiKey();
- $secret_key = $entity->getSecretkey();
$listID = $entity->getID();
$table = $entity->get('mapping_table');
if (is_array($table)) {
foreach ($table as $category) {
if ($category['simplenews_news'] == $subscription) {
// Unsubscribe.
- SimplenewsMailjetSubscriptionsUtilities::unsubscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID);
+ // SimplenewsMailjetSubscriptionsUtilities::unsubscribeUser($subscriber, $category);
+ \Drupal::service('simplenews_mailjet_subscriptions.utilities')->unsubscribeUser($listID, $subscriber, $category);
}
}
}
}
+ // \Drupal::service('simplenews_mailjet_subscriptions.utilities')->unsubscribeUser($subscriber, $subscription);
}
/**
diff --git a/simplenews_mailjet_subscriptions.services.yml b/simplenews_mailjet_subscriptions.services.yml
new file mode 100644
index 0000000..cdd4137
--- /dev/null
+++ b/simplenews_mailjet_subscriptions.services.yml
@@ -0,0 +1,4 @@
+services:
+ simplenews_mailjet_subscriptions.utilities:
+ class: Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsUtilities
+ arguments: [ '@mailjet.handler' ]
diff --git a/src/SimplenewsMailjetSubscriptionsUtilities.php b/src/SimplenewsMailjetSubscriptionsUtilities.php
index 950ff8a..05601ca 100644
--- a/src/SimplenewsMailjetSubscriptionsUtilities.php
+++ b/src/SimplenewsMailjetSubscriptionsUtilities.php
@@ -2,141 +2,79 @@
namespace Drupal\simplenews_mailjet_subscriptions;
+use Drupal\mailjet\MailjetHandlerInterface;
+
/**
- * Provides a list of Simplenews Mailjet Subscription entities.
+ * Utilisties
*/
class SimplenewsMailjetSubscriptionsUtilities {
/**
- * Check Subscriber.
+ * MailjetHandler service.
+ *
+ * @var \Drupal\mailjet\MailjetHandlerInterface
*/
- private static function checkSubscriber($api_key, $secret_key, $encoded_mail) {
-
- // Check if contact exist
- // verifica se o contacto ja tem opções.
- $endpoint = 'https://api.mailjet.com/v3/REST/contactdata/' . $encoded_mail;
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
- curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($ch, CURLOPT_USERPWD, $api_key . ':' . $secret_key);
- curl_setopt($ch, CURLOPT_URL, $endpoint);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $response = curl_exec($ch);
- curl_close($ch);
- $json = json_decode($response, TRUE);
+ protected $mailjetHandler;
- return $json;
+/**
+ * SubscribeEmailForm constructor.
+ *
+ * @param \Drupal\mailjet\MailjetHandlerInterface $mailjetHandler
+ * The mailjet handler service.
+ */
+ public function __construct(MailjetHandlerInterface $mailjetHandler) {
+ $this->mailjetHandler = $mailjetHandler;
}
/**
- * Update Subscriber.
+ * {@inheritDoc}
*/
- private static function updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, $action) {
-
- // Add contact to mailjet.
- $endpoint = 'https://api.mailjet.com/v3/REST/contactslist/' . $listID . '/managecontact';
- $message = [
- "Properties" => [
- $contact_property => $newsletter_property_name,
- ],
- "Action" => $action,
- "Email" => $subscriber_email,
- ];
- $payload = json_encode($message);
-
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($ch, CURLOPT_USERPWD, $api_key . ':' . $secret_key);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
- curl_setopt($ch, CURLOPT_URL, $endpoint);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $response = curl_exec($ch);
- curl_close($ch);
- $json = json_decode($response, TRUE);
-
- return $json;
+ public static function create(ContainerInterface $container) {
+ return new static(
+ $container->get('mailjet.handler')
+ );
}
/**
* Subscribe User.
*/
- public static function subscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID) {
+ public function subscribeUser($listID, $subscriber, $category) {
$contact_property = $category['contact_property'];
$newsletter_property_name = $category['newsletter_property_name'];
$subscriber_email = $subscriber->getMail();
- $encoded_mail = urlencode($subscriber_email);
- $response = '';
-
- $contact_info = self::checkSubscriber($api_key, $secret_key, $encoded_mail);
-
- $options = $contact_info['Data'];
-
- // Se existe - acrescenta a opções.
- if ($options[0]['Data']) {
- foreach ($options[0]['Data'] as $property) {
- if ($property["Name"] == $contact_property) {
-
- if (is_int(strpos($property['Value'], $newsletter_property_name))) {
- $newsletter_property_name = $property['Value'];
- }
- else {
- $newsletter_property_name = $property['Value'] . ';' . $newsletter_property_name;
- }
- }
- }
- }
+ $contact = [
+ 'Email' => $subscriber_email,
+ 'Properties' => [
+ $contact_property => $newsletter_property_name
+ ]
+ ];
- $response = self::updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, 'addnoforce');
- $contact_id = $response['Data'][0]['ContactID'];
- \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $contact_id . ' subscribed to the ' . $contact_property . ' properties with the ' . $newsletter_property_name . ' values in the list ' . $listID . '.');
- return $response;
+ // $response = $this->updateSubscriber($listID, $contact, 'addnoforce');
+ $response = $this->mailjetHandler->syncMailjetContact($listID, $contact, 'addnoforce');
+ // $contact_id = $response['Data'][0]['ContactID'];
+ \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' subscribed to the ' . $listID . ' list .');
}
/**
* Unsubscribe User.
*/
- public static function unsubscribeUser($subscriber, $subscription, $category, $api_key, $secret_key, $listID) {
+ public function unsubscribeUser($listID, $subscriber, $category) {
$contact_property = $category['contact_property'];
$newsletter_property_name = $category['newsletter_property_name'];
$subscriber_email = $subscriber->getMail();
- $encoded_mail = urlencode($subscriber_email);
- $response = '';
-
- $contact_info = self::checkSubscriber($api_key, $secret_key, $encoded_mail);
- $contact_id = $contact_info['ContactID'];
- $options = $contact_info['Data'];
-
- if ($options[0]['Data']) {
- foreach ($options[0]['Data'] as $property) {
- if ($property["Name"] == $contact_property) {
- if (substr_count($property['Value'], ';') == 0) {
- $newsletter_property_name = '';
- $response = self::updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, 'remove');
- $contact_id = $response['Data'][0]['ContactID'];
- \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $contact_id . ' unsubscribed to the list ' . $listID . '.');
- }
- else {
- $pos = strpos($property['Value'], $newsletter_property_name);
- \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $contact_id . ' unsubscribed to the ' . $contact_property . ' properties with the ' . $newsletter_property_name . ' values in the list ' . $listID . '.');
- if ($pos === 0) {
- $newsletter_property_name = str_replace($newsletter_property_name . ';', '', $property['Value']);
- }
- else {
- $newsletter_property_name = str_replace(';' . $newsletter_property_name, '', $property['Value']);
- }
- $response = self::updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, 'addnoforce');
- $contact_id = $response['Data'][0]['ContactID'];
- \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $contact_id . 'remains subscribed to the ' . $contact_property . ' properties with the ' . $newsletter_property_name . ' values in the list ' . $listID . '.');
-
- }
- }
- }
- }
+ $contact = [
+ 'Email' => $subscriber_email,
+ 'Properties' => [
+ $contact_property => $newsletter_property_name
+ ]
+ ];
- return $response;
+ // $response = $this->updateSubscriber($subscriber_email, $newsletter_property_name, 'remove');
+ $response = $this->mailjetHandler->syncMailjetContact($listID, $contact, 'remove');
+ // $contact_id = $response['Data'][0]['ContactID'];
+ \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' removed from the ' . $listID . ' list ');
+ // return $response;
}
}