PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 11.1.0
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v11.1.0
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
← All changes | back/dynamics/sendinblue/users.php +289 -0 10.2.0 → 11.1.0 View file →
@@ -1,0 +1,289 @@
1 +<?php
2 +
3 +use AcyMailing\Classes\UserClass;
4 +
5 +class SendinblueUsers extends SendinblueClass
6 +{
7 + const MAX_USERS_IMPORT_NB = 5000;
8 +
9 + private SendinblueList $list;
10 +
11 + public function __construct(&$plugin, $headers, $list)
12 + {
13 + parent::__construct($plugin, $headers);
14 + $this->list = $list;
15 + }
16 +
17 + public function createUser($user)
18 + {
19 + $sendingMethod = $this->config->get('mailer_method', 'phpmail');
20 + if ($sendingMethod != plgAcymSendinblue::SENDING_METHOD_ID) return;
21 +
22 + $userName = empty($user->name) ? '' : $user->name;
23 +
24 + $nameParts = explode(' ', $userName, 2);
25 + $userData = [
26 + 'email' => acym_strtolower($user->email),
27 + 'attributes' => [
28 + 'LASTNAME' => empty($nameParts[1]) ? '' : $nameParts[1],
29 + 'FIRSTNAME' => $nameParts[0],
30 + ],
31 + 'updateEnabled' => true,
32 + ];
33 + $this->callApiSendingMethod('contacts', $userData, $this->headers, 'POST');
34 + }
35 +
36 + public function createFromImportedSource($source)
37 + {
38 + $userClass = new UserClass();
39 + $importedUsers = $userClass->getByColumnValue('source', $source);
40 + $this->importUsers($importedUsers);
41 + }
42 +
43 + public function importUsers($users, $ajax = false)
44 + {
45 + $sendingMethod = $this->config->get('mailer_method', 'phpmail');
46 + if ($sendingMethod != plgAcymSendinblue::SENDING_METHOD_ID) return;
47 +
48 + if (!acym_createFolder(ACYM_TMP_FOLDER)) {
49 + $message = acym_translation('ACYM_ERROR_CREATING_EXPORT_FILE');
50 + acym_logError($message, 'sendinblue');
51 + if ($ajax) {
52 + acym_sendAjaxResponse($message, [], false);
53 + $notification = [
54 + 'name' => 'error_creating_export_file',
55 + 'removable' => 1,
56 + ];
57 + acym_enqueueMessage($message, 'error', false, [$notification]);
58 + }
59 +
60 + return;
61 + }
62 +
63 + static $listId = null;
64 +
65 + if (empty($listId)) {
66 + $listId = $this->list->createList('Import '.time());
67 + }
68 +
69 + $this->errors = [];
70 + $filePath = ACYM_TMP_FOLDER.plgAcymSendinblue::SENDING_METHOD_ID.'.txt';
71 + file_put_contents($filePath, "LASTNAME;FIRSTNAME;EMAIL\n");
72 + $limit = self::MAX_USERS_IMPORT_NB;
73 + $buffer = '';
74 +
75 + foreach ($users as $oneUser) {
76 + $nameParts = explode(' ', $oneUser->name, 2);
77 + $lastName = str_replace('"', '', empty($nameParts[1]) ? '' : $nameParts[1]);
78 + $firstName = str_replace('"', '', $nameParts[0]);
79 + $buffer .= '"'.$lastName.'";"'.$firstName.'";"'.acym_strtolower($oneUser->email)."\"\n";
80 + $limit--;
81 +
82 + if ($limit === 0) {
83 + file_put_contents($filePath, $buffer, FILE_APPEND);
84 + $this->sendUsers($listId);
85 + file_put_contents($filePath, "LASTNAME;FIRSTNAME;EMAIL\n");
86 + $limit = self::MAX_USERS_IMPORT_NB;
87 + $buffer = '';
88 + }
89 + }
90 + if (!empty($buffer)) {
91 + file_put_contents($filePath, $buffer, FILE_APPEND);
92 + $this->sendUsers($listId);
93 + }
94 +
95 + if (empty($this->errors)) {
96 + $message = acym_translation('ACYM_USERS_SUNCHRONIZED');
97 + acym_logError($message, 'sendinblue');
98 + if ($ajax) {
99 + acym_sendAjaxResponse($message);
100 + }
101 + } else {
102 + $message = implode('<br />', $this->errors);
103 + acym_logError($message, 'sendinblue');
104 + if ($ajax) {
105 + acym_sendAjaxResponse($message, [], false);
106 + } else {
107 + acym_enqueueMessage($message, 'error');
108 + }
109 + }
110 + }
111 +
112 + private function sendUsers($listId)
113 + {
114 + // Call API to import
115 + $data = [
116 + 'fileUrl' => ACYM_TMP_URL.plgAcymSendinblue::SENDING_METHOD_ID.'.txt',
117 + 'listIds' => [$listId],
118 + 'updateExistingContacts' => true,
119 + ];
120 +
121 + $response = $this->callApiSendingMethod('contacts/import', $data, $this->headers, 'POST');
122 + //TODO delete the import file to avoid leaking user data
123 +
124 + if (!empty($response['error_curl'])) {
125 + $this->errors[] = acym_translationSprintf('ACYM_ERROR_OCCURRED_WHILE_CALLING_API', $response['error_curl']);
126 + } elseif (!empty($response['code']) && !empty($response['message'])) {
127 + $this->errors[] = acym_translationSprintf('ACYM_API_RETURN_THIS_ERROR', $response['message']);
128 + }
129 + }
130 +
131 + public function deleteUsers($users)
132 + {
133 + $sendingMethod = $this->config->get('mailer_method', 'phpmail');
134 + if ($sendingMethod != plgAcymSendinblue::SENDING_METHOD_ID) return;
135 +
136 + $deleteUrl = 'contacts/';
137 +
138 + $userClass = new UserClass();
139 +
140 + foreach ($users as $oneUser) {
141 + $userToDelete = $userClass->getOneById($oneUser);
142 + $this->callApiSendingMethod($deleteUrl.urlencode(acym_strtolower($userToDelete->email)), [], $this->headers, 'DELETE');
143 + }
144 + }
145 +
146 + public function createAttribute($mailId)
147 + {
148 + $existingAttributes = $this->config->get('sendinblue_attributes', '{}');
149 + $existingAttributes = json_decode($existingAttributes, true);
150 +
151 + $subjectAttributeName = $this->getSubjectAttributeName($mailId);
152 + $contentAttributeName = $this->getAttributeName($mailId);
153 +
154 + $added = $this->addAttribute($existingAttributes, $subjectAttributeName);
155 + $added = $this->addAttribute($existingAttributes, $contentAttributeName) || $added;
156 +
157 + if ($added) {
158 + $this->config->saveConfig(['sendinblue_attributes' => json_encode($existingAttributes)]);
159 + }
160 + }
161 +
162 + private function addAttribute(&$existingAttributes, $attributeName): bool
163 + {
164 + if (!empty($existingAttributes[$attributeName])) return false;
165 + $this->callApiSendingMethod(
166 + 'contacts/attributes/normal/'.$attributeName,
167 + ['type' => 'text'],
168 + $this->headers,
169 + 'POST'
170 + );
171 + $existingAttributes[$attributeName] = true;
172 +
173 + return true;
174 + }
175 +
176 + public function addUserToList($email, $mailId, &$warnings): bool
177 + {
178 + $listId = 0;
179 + $this->list->getListExternalSendingMethod($listId, $mailId);
180 +
181 + if (empty($listId)) return false;
182 +
183 + $email = acym_strtolower($email);
184 + $data = [
185 + 'emails' => [$email],
186 + ];
187 +
188 + $response = $this->callApiSendingMethod('contacts/lists/'.$listId.'/contacts/add', $data, $this->headers, 'POST');
189 + $success = !empty($response['contacts']['success']) && in_array($email, $response['contacts']['success']);
190 + $alreadyInList = !empty($response['message']) && strpos($response['message'], 'Contact already in list') !== false;
191 +
192 + if (!$success && !empty($response['message']) && !$alreadyInList) {
193 + $warnings .= $response['message'];
194 + acym_logError('Error trying to add user '.$email.' to list '.$listId.' for mail ID '.$mailId.': '.$response['message'], 'sendinblue');
195 + } else {
196 + if (!$success) {
197 + if (!empty($response['message'])) {
198 + acym_logError('Error trying to add user '.$email.' to list '.$listId.' for mail ID '.$mailId.': '.$response['message'], 'sendinblue');
199 + } else {
200 + acym_logError('Error trying to add user '.$email.' to list '.$listId.' for mail ID '.$mailId.' - unknown error: '.json_encode($response), 'sendinblue');
201 + }
202 + }
203 + }
204 +
205 + return $success || $alreadyInList;
206 + }
207 +
208 + public function removeUserFromList($mailId)
209 + {
210 + $listId = 0;
211 + $this->list->getListExternalSendingMethod($listId, $mailId);
212 +
213 + if (empty($listId)) return;
214 +
215 + $this->callApiSendingMethod('contacts/lists/'.$listId.'/contacts/remove', ['all' => true], $this->headers, 'POST');
216 + }
217 +
218 + public function addAttributeToUser($email, $subjectContent, $htmlContent, $mailId)
219 + {
220 + $subjectAttribute = $this->getSubjectAttributeName($mailId);
221 + $contentAttribute = $this->getAttributeName($mailId);
222 +
223 + if (strpos($htmlContent, 'acym__wysid__template') === false || strpos($htmlContent, '<body') === false) {
224 + $personalContent = $htmlContent;
225 + } else {
226 + $personalContent = preg_replace('#^.*<body[^>]*>(.*)</body>.*$#Uis', '$1', $htmlContent);
227 + }
228 +
229 + $data = [
230 + 'attributes' => [
231 + $subjectAttribute => $subjectContent,
232 + $contentAttribute => $personalContent,
233 + ],
234 + 'email' => acym_strtolower($email),
235 + 'updateEnabled' => true,
236 + ];
237 + $this->callApiSendingMethod('contacts', $data, $this->headers, 'POST');
238 + }
239 +
240 + public function getAttributeName($mailId): string
241 + {
242 + return 'HTML_CONTENT_'.$mailId;
243 + }
244 +
245 + public function getSubjectAttributeName($mailId): string
246 + {
247 + return 'SUBJECT_'.$mailId;
248 + }
249 +
250 + public function deleteAttribute($mailId)
251 + {
252 + $subjectAttributeName = $this->getSubjectAttributeName($mailId);
253 + $contentAttributeName = $this->getAttributeName($mailId);
254 +
255 + $existingAttributes = $this->config->get('sendinblue_attributes', '{}');
256 + $existingAttributes = json_decode($existingAttributes, true);
257 +
258 + $removedAnAttribute = $this->removeAttribute($existingAttributes, $subjectAttributeName);
259 + $removedAnAttribute = $this->removeAttribute($existingAttributes, $contentAttributeName) || $removedAnAttribute;
260 +
261 + if ($removedAnAttribute) {
262 + $this->config->saveConfig(['sendinblue_attributes' => json_encode($existingAttributes)]);
263 + }
264 + }
265 +
266 + private function removeAttribute(&$existingAttributes, $attributeName): bool
267 + {
268 + $this->callApiSendingMethod(plgAcymSendinblue::SENDING_METHOD_API_URL.'contacts/attributes/normal/'.$attributeName, [], $this->headers, 'DELETE');
269 +
270 + if (empty($existingAttributes[$attributeName])) return false;
271 +
272 + unset($existingAttributes[$attributeName]);
273 +
274 + return true;
275 + }
276 +
277 + public function synchronizeExistingUsers()
278 + {
279 + // Generate file with user to import
280 + $userClass = new UserClass();
281 + $users = $userClass->getAllSimpleData();
282 + if (empty($users)) {
283 + acym_sendAjaxResponse(acym_translation('ACYM_NO_USER_TO_SYNCHRONIZE'));
284 + acym_logError(acym_translation('ACYM_NO_USER_TO_SYNCHRONIZE'), 'sendinblue');
285 + }
286 +
287 + $this->importUsers($users, true);
288 + }
289 +}