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 / elasticemail / plugin.php

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