PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.1.4
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.1.4
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 / elasticemail / plugin.php

plugin.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 10.1.4, at back/dynamics/elasticemail/plugin.php

307 lines 10.5 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\CampaignClass;
4 use AcyMailing\Classes\MailClass;
5 use AcyMailing\Helpers\MailerHelper;
6 use AcyMailing\Core\AcymPlugin;
7
8 class plgAcymElasticemail extends AcymPlugin
9 {
10 const SENDING_METHOD_ID = 'elasticemail';
11 const SENDING_METHOD_NAME = 'Elastic Email';
12 const SENDING_METHOD_API_URL = 'https://api.elasticemail.com/v4/';
13
14 private $mailClass;
15 private $campaignClass;
16 private $attachmentsFetched = false;
17 private $uploadedAttachments = [];
18
19 public function __construct()
20 {
21 parent::__construct();
22 $this->pluginDescription->name = self::SENDING_METHOD_NAME;
23 }
24
25 public function onAcymGetSendingMethods(&$data, $isMailer = false)
26 {
27 $data['sendingMethods'][self::SENDING_METHOD_ID] = [
28 'name' => $this->pluginDescription->name,
29 'image' => ACYM_IMAGES.'mailers/elasticemail.png',
30 ];
31 }
32
33 public function onAcymGetSendingMethodsHtmlSetting(&$data)
34 {
35 ob_start();
36 ?>
37 <div class="send_settings grid-x cell" id="<?php echo self::SENDING_METHOD_ID; ?>_settings">
38 <div class="cell grid-x acym_vcenter acym__sending__methods__one__settings">
39 <label for="<?php echo self::SENDING_METHOD_ID; ?>_password" class="cell shrink margin-right-1">
40 <?php echo acym_translation('ACYM_API_KEY'); ?>
41 </label>
42 <?php
43 echo $this->getLinks(
44 'https://elasticemail.com/referral-reward?r=7b884a0b-b979-4473-8803-06ae39d76599',
45 'https://elasticemail.com/email-api-pricing?r=7b884a0b-b979-4473-8803-06ae39d76599'
46 );
47 ?>
48 <input id="<?php echo self::SENDING_METHOD_ID; ?>_password"
49 class="cell"
50 type="text"
51 name="config[<?php echo self::SENDING_METHOD_ID; ?>_password]"
52 value="<?php echo str_repeat('*', strlen($this->config->get(self::SENDING_METHOD_ID.'_password'))); ?>">
53 <?php echo $this->getTestCredentialsSendingMethodButton(self::SENDING_METHOD_ID); ?>
54 </div>
55 </div>
56 <?php
57 $data['sendingMethodsHtmlSettings'][self::SENDING_METHOD_ID] = ob_get_clean();
58 }
59
60 public function onAcymTestCredentialSendingMethod($sendingMethod, $credentials)
61 {
62 if ($sendingMethod !== self::SENDING_METHOD_ID) {
63 return;
64 }
65
66 $response = $this->callApiSendingMethod(
67 self::SENDING_METHOD_API_URL.'files?limit=1',
68 [],
69 $this->getAuthenticationHeaders($credentials),
70 );
71
72 if (!empty($response['error_curl'])) {
73 acym_sendAjaxResponse(acym_translationSprintf('ACYM_ERROR_OCCURRED_WHILE_CALLING_API', $response['error_curl']), [], false);
74 } elseif (!empty($response['Error'])) {
75 $message = $response['Error'] === 'APIKey Expired'
76 ? acym_translation('ACYM_AUTHENTICATION_FAILS_WITH_API_KEY')
77 : acym_translationSprintf(
78 'ACYM_API_RETURN_THIS_ERROR',
79 $response['Error']
80 );
81 acym_sendAjaxResponse($message, [], false);
82 } else {
83 acym_sendAjaxResponse(acym_translation('ACYM_API_KEY_CORRECT'));
84 }
85 }
86
87 public function onAcymSendEmail(
88 array &$response,
89 MailerHelper $mailerHelper,
90 array $to,
91 array $from,
92 array $reply_to,
93 array $bcc = [],
94 array $attachments = [],
95 $sendingMethodListParams = []
96 ): void {
97 if ($mailerHelper->externalMailer !== self::SENDING_METHOD_ID) {
98 return;
99 }
100
101 $data = [
102 'recipients' => new stdClass(),
103 'content' => new stdClass(),
104 ];
105
106 $this->handleAddresses($data, $from, $reply_to, $to, $bcc);
107 $this->handleEmailContent($data, $mailerHelper);
108 $this->handleAttachments($data, $mailerHelper, $attachments);
109 $this->handleHeaders($data, $mailerHelper);
110 $this->handleGA($data, $mailerHelper);
111
112 $responseMailer = $this->callApiSendingMethod(
113 self::SENDING_METHOD_API_URL.'emails/transactional',
114 $data,
115 $this->getAuthenticationHeaders(),
116 'POST'
117 );
118
119 if (!empty($responseMailer['Error'])) {
120 $response['error'] = true;
121 $response['message'] = $responseMailer['Error'];
122 } else {
123 $response['error'] = false;
124 }
125 }
126
127 public function onAcymSendingMethodOptions(&$data)
128 {
129 $data['embedImage'][self::SENDING_METHOD_ID] = false;
130 }
131
132 private function handleAddresses(array &$data, array $from, array $reply_to, array $to, array $bcc): void
133 {
134 $data['recipients']->to = [$to['email']];
135
136 if (!empty($bcc)) {
137 $data['recipients']->bcc = [$bcc[0][0]];
138 }
139
140 $data['content']->From = empty($from['name']) ? $from['email'] : $from['name'].' <'.$from['email'].'>';
141 $data['content']->ReplyTo = empty($reply_to['name']) ? $reply_to['email'] : $reply_to['name'].' <'.$reply_to['email'].'>';
142
143 $bounceAddress = $this->config->get('bounce_email');
144 if (!empty($bounceAddress)) {
145 $data['content']->EnvelopeFrom = $bounceAddress;
146 }
147 }
148
149 private function handleEmailContent(array &$data, MailerHelper $mailerHelper): void
150 {
151 $data['content']->Subject = $mailerHelper->Subject;
152 $data['content']->Body = [
153 (object)[
154 'ContentType' => 'HTML',
155 'Content' => $mailerHelper->Body,
156 'Charset' => 'utf-8',
157 ],
158 ];
159
160 if (!empty($mailerHelper->AltBody)) {
161 $data['content']->Body[] = (object)[
162 'ContentType' => 'PlainText',
163 'Content' => $mailerHelper->AltBody,
164 'Charset' => 'utf-8',
165 ];
166 }
167 }
168
169 private function handleAttachments(array &$data, MailerHelper $mailerHelper, array $attachments): void
170 {
171 if (empty($attachments)) {
172 return;
173 }
174
175 if (!$this->attachmentsFetched) {
176 $this->attachmentsFetched = true;
177
178 $uploadedAttachments = $this->callApiSendingMethod(
179 self::SENDING_METHOD_API_URL.'files?limit=1000',
180 [],
181 $this->getAuthenticationHeaders()
182 );
183
184 if (empty($uploadedAttachments['Error'])) {
185 foreach ($uploadedAttachments as $oneAttachment) {
186 $this->uploadedAttachments[] = $oneAttachment['FileName'];
187 }
188 }
189 }
190
191 $data['content']->AttachFiles = [];
192 foreach ($attachments as $attachment) {
193 $fileName = $this->cleanFileName($attachment[1]);
194
195 if (!in_array($fileName, $this->uploadedAttachments)) {
196 $uploadedAttachment = $this->callApiSendingMethod(
197 self::SENDING_METHOD_API_URL.'files',
198 [
199 'BinaryContent' => $attachment['contentEncoded'],
200 'Name' => $fileName,
201 'ContentType' => $attachment[4],
202 ],
203 $this->getAuthenticationHeaders(),
204 'POST'
205 );
206
207 if (empty($uploadedAttachment['FileName'])) {
208 continue;
209 }
210
211 $this->uploadedAttachments[] = $fileName;
212 }
213
214 $data['content']->AttachFiles[] = $fileName;
215 }
216 }
217
218 private function handleHeaders(array &$data, MailerHelper $mailerHelper): void
219 {
220 if (empty($mailerHelper->CustomHeader)) {
221 return;
222 }
223
224 $data['content']->Headers = new stdClass();
225 foreach ($mailerHelper->CustomHeader as $oneHeader) {
226 $data['content']->Headers->{$oneHeader[0]} = $oneHeader[1];
227 }
228 }
229
230 private function handleGA(array &$data, MailerHelper $mailerHelper): void
231 {
232 $trackingSystem = $this->config->get('trackingsystem', 'acymailing');
233 if (strpos($trackingSystem, 'google') === false || empty($mailerHelper->mailId)) {
234 return;
235 }
236 $data['content']->Utm = new stdClass();
237 $data['content']->Utm->Source = 'newsletter';
238 $data['content']->Utm->Medium = 'email';
239 $data['content']->Utm->Campaign = acym_getAlias($data['content']->Subject);
240
241 if (empty($mailerHelper->mailId)) {
242 return;
243 }
244
245 if (empty($this->mailClass)) {
246 $this->mailClass = new MailClass();
247 }
248 $campaignId = $this->mailClass->getCampaignIdByMailId($mailerHelper->mailId);
249
250 if (empty($campaignId)) {
251 $data['content']->Utm->Source .= '_'.$mailerHelper->mailId;
252 } else {
253 if (empty($this->campaignClass)) {
254 $this->campaignClass = new CampaignClass();
255 }
256 $campaign = $this->campaignClass->getOneById($campaignId);
257
258 if (empty($campaign)) {
259 return;
260 }
261
262 $data['content']->Utm->Source .= '_'.$campaignId;
263
264 if (!empty($campaign->sending_params['utm_source'])) {
265 $data['content']->Utm->Source = $campaign->sending_params['utm_source'];
266 }
267
268 if (!empty($campaign->sending_params['utm_medium'])) {
269 $data['content']->Utm->Medium = $campaign->sending_params['utm_medium'];
270 }
271
272 if (!empty($campaign->sending_params['utm_campaign'])) {
273 $data['content']->Utm->Campaign = $campaign->sending_params['utm_campaign'];
274 }
275 }
276 }
277
278 private function getAuthenticationHeaders(array $credentials = []): array
279 {
280 if (!isset($credentials[self::SENDING_METHOD_ID.'_password']) || (!empty($credentials[self::SENDING_METHOD_ID.'_password']) && empty(
281 trim(
282 $credentials[self::SENDING_METHOD_ID.'_password'],
283 '*'
284 )
285 ))) {
286 return [
287 'X-ElasticEmail-ApiKey: '.$this->config->get(self::SENDING_METHOD_ID.'_password'),
288 'Content-Type: application/json',
289 ];
290 }
291
292 return [
293 'X-ElasticEmail-ApiKey: '.$credentials[self::SENDING_METHOD_ID.'_password'],
294 'Content-Type: application/json',
295 ];
296 }
297
298 private function cleanFileName(string $fileName): string
299 {
300 if (function_exists('iconv')) {
301 $fileName = iconv('UTF-8', 'ASCII//TRANSLIT', $fileName);
302 }
303
304 return preg_replace('#[^a-z0-9\.\-\_]#i', '_', $fileName);
305 }
306 }
307