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

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