PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.11.1
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.11.1
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
acymailing / back / dynamics / sendinblue / users.php

users.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 10.11.1, at back/dynamics/sendinblue/users.php

287 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $data = [
115 'fileUrl' => ACYM_TMP_URL.plgAcymSendinblue::SENDING_METHOD_ID.'.txt',
116 'listIds' => [$listId],
117 'updateExistingContacts' => true,
118 ];
119
120 $response = $this->callApiSendingMethod('contacts/import', $data, $this->headers, 'POST');
121
122 if (!empty($response['error_curl'])) {
123 $this->errors[] = acym_translationSprintf('ACYM_ERROR_OCCURRED_WHILE_CALLING_API', $response['error_curl']);
124 } elseif (!empty($response['code']) && !empty($response['message'])) {
125 $this->errors[] = acym_translationSprintf('ACYM_API_RETURN_THIS_ERROR', $response['message']);
126 }
127 }
128
129 public function deleteUsers($users)
130 {
131 $sendingMethod = $this->config->get('mailer_method', 'phpmail');
132 if ($sendingMethod != plgAcymSendinblue::SENDING_METHOD_ID) return;
133
134 $deleteUrl = 'contacts/';
135
136 $userClass = new UserClass();
137
138 foreach ($users as $oneUser) {
139 $userToDelete = $userClass->getOneById($oneUser);
140 $this->callApiSendingMethod($deleteUrl.urlencode(acym_strtolower($userToDelete->email)), [], $this->headers, 'DELETE');
141 }
142 }
143
144 public function createAttribute($mailId)
145 {
146 $existingAttributes = $this->config->get('sendinblue_attributes', '{}');
147 $existingAttributes = json_decode($existingAttributes, true);
148
149 $subjectAttributeName = $this->getSubjectAttributeName($mailId);
150 $contentAttributeName = $this->getAttributeName($mailId);
151
152 $added = $this->addAttribute($existingAttributes, $subjectAttributeName);
153 $added = $this->addAttribute($existingAttributes, $contentAttributeName) || $added;
154
155 if ($added) {
156 $this->config->saveConfig(['sendinblue_attributes' => json_encode($existingAttributes)]);
157 }
158 }
159
160 private function addAttribute(&$existingAttributes, $attributeName): bool
161 {
162 if (!empty($existingAttributes[$attributeName])) return false;
163 $this->callApiSendingMethod(
164 'contacts/attributes/normal/'.$attributeName,
165 ['type' => 'text'],
166 $this->headers,
167 'POST'
168 );
169 $existingAttributes[$attributeName] = true;
170
171 return true;
172 }
173
174 public function addUserToList($email, $mailId, &$warnings): bool
175 {
176 $listId = 0;
177 $this->list->getListExternalSendingMethod($listId, $mailId);
178
179 if (empty($listId)) return false;
180
181 $email = acym_strtolower($email);
182 $data = [
183 'emails' => [$email],
184 ];
185
186 $response = $this->callApiSendingMethod('contacts/lists/'.$listId.'/contacts/add', $data, $this->headers, 'POST');
187 $success = !empty($response['contacts']['success']) && in_array($email, $response['contacts']['success']);
188 $alreadyInList = !empty($response['message']) && strpos($response['message'], 'Contact already in list') !== false;
189
190 if (!$success && !empty($response['message']) && !$alreadyInList) {
191 $warnings .= $response['message'];
192 acym_logError('Error trying to add user '.$email.' to list '.$listId.' for mail ID '.$mailId.': '.$response['message'], 'sendinblue');
193 } else {
194 if (!$success) {
195 if (!empty($response['message'])) {
196 acym_logError('Error trying to add user '.$email.' to list '.$listId.' for mail ID '.$mailId.': '.$response['message'], 'sendinblue');
197 } else {
198 acym_logError('Error trying to add user '.$email.' to list '.$listId.' for mail ID '.$mailId.' - unknown error: '.json_encode($response), 'sendinblue');
199 }
200 }
201 }
202
203 return $success || $alreadyInList;
204 }
205
206 public function removeUserFromList($mailId)
207 {
208 $listId = 0;
209 $this->list->getListExternalSendingMethod($listId, $mailId);
210
211 if (empty($listId)) return;
212
213 $this->callApiSendingMethod('contacts/lists/'.$listId.'/contacts/remove', ['all' => true], $this->headers, 'POST');
214 }
215
216 public function addAttributeToUser($email, $subjectContent, $htmlContent, $mailId)
217 {
218 $subjectAttribute = $this->getSubjectAttributeName($mailId);
219 $contentAttribute = $this->getAttributeName($mailId);
220
221 if (strpos($htmlContent, 'acym__wysid__template') === false || strpos($htmlContent, '<body') === false) {
222 $personalContent = $htmlContent;
223 } else {
224 $personalContent = preg_replace('#^.*<body[^>]*>(.*)</body>.*$#Uis', '$1', $htmlContent);
225 }
226
227 $data = [
228 'attributes' => [
229 $subjectAttribute => $subjectContent,
230 $contentAttribute => $personalContent,
231 ],
232 'email' => acym_strtolower($email),
233 'updateEnabled' => true,
234 ];
235 $this->callApiSendingMethod('contacts', $data, $this->headers, 'POST');
236 }
237
238 public function getAttributeName($mailId): string
239 {
240 return 'HTML_CONTENT_'.$mailId;
241 }
242
243 public function getSubjectAttributeName($mailId): string
244 {
245 return 'SUBJECT_'.$mailId;
246 }
247
248 public function deleteAttribute($mailId)
249 {
250 $subjectAttributeName = $this->getSubjectAttributeName($mailId);
251 $contentAttributeName = $this->getAttributeName($mailId);
252
253 $existingAttributes = $this->config->get('sendinblue_attributes', '{}');
254 $existingAttributes = json_decode($existingAttributes, true);
255
256 $removedAnAttribute = $this->removeAttribute($existingAttributes, $subjectAttributeName);
257 $removedAnAttribute = $this->removeAttribute($existingAttributes, $contentAttributeName) || $removedAnAttribute;
258
259 if ($removedAnAttribute) {
260 $this->config->saveConfig(['sendinblue_attributes' => json_encode($existingAttributes)]);
261 }
262 }
263
264 private function removeAttribute(&$existingAttributes, $attributeName): bool
265 {
266 $this->callApiSendingMethod(plgAcymSendinblue::SENDING_METHOD_API_URL.'contacts/attributes/normal/'.$attributeName, [], $this->headers, 'DELETE');
267
268 if (empty($existingAttributes[$attributeName])) return false;
269
270 unset($existingAttributes[$attributeName]);
271
272 return true;
273 }
274
275 public function synchronizeExistingUsers()
276 {
277 $userClass = new UserClass();
278 $users = $userClass->getAllSimpleData();
279 if (empty($users)) {
280 acym_sendAjaxResponse(acym_translation('ACYM_NO_USER_TO_SYNCHRONIZE'));
281 acym_logError(acym_translation('ACYM_NO_USER_TO_SYNCHRONIZE'), 'sendinblue');
282 }
283
284 $this->importUsers($users, true);
285 }
286 }
287