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

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