PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 11.0.3
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v11.0.3
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 / Controllers / Mails / Edition.php

Edition.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 11.0.3, at back/Controllers/Mails/Edition.php

1,187 lines 47.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AcyMailing\Controllers\Mails;
4
5 use AcyMailing\Classes\CampaignClass;
6 use AcyMailing\Classes\FollowupClass;
7 use AcyMailing\Classes\ListClass;
8 use AcyMailing\Classes\MailClass;
9 use AcyMailing\Classes\TagClass;
10 use AcyMailing\Controllers\CampaignsController;
11 use AcyMailing\Helpers\EditorHelper;
12 use AcyMailing\Helpers\MailerHelper;
13 use AcyMailing\Helpers\PaginationHelper;
14 use AcyMailing\Types\UploadFileType;
15
16 trait Edition
17 {
18 public function edit(): void
19 {
20 if (!acym_isAdmin()) {
21 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
22 }
23
24 $tempId = acym_getVar('int', 'id');
25 $mailClass = new MailClass();
26 $typeEditor = acym_getVar('string', 'type_editor');
27 $notification = acym_getVar('cmd', 'notification');
28 $return = acym_getVar('string', 'return', '');
29 $followupId = acym_getVar('int', 'followup_id', 0);
30 $followupClass = new FollowupClass();
31
32 $campaignController = new CampaignsController();
33
34 if (base64_decode($return, true) === false) {
35 $return = empty($return) ? '' : $return;
36 } else {
37 $return = empty($return) ? '' : urldecode(base64_decode($return));
38 }
39
40 $type = acym_getVar('string', 'type', '');
41 $fromId = acym_getVar('int', 'from');
42 $listIds = acym_getVar('int', 'list_id', []);
43
44 if (in_array($type, [MailClass::TYPE_WELCOME, MailClass::TYPE_UNSUBSCRIBE])) {
45 array_pop($this->breadcrumb);
46 $this->breadcrumb[acym_translation('ACYM_LISTS')] = acym_completeLink('lists');
47 $campaignController->setTaskListing($type);
48
49 if (!empty($listIds)) {
50 $listIds = [$listIds];
51 }
52 }
53
54
55 if (!empty($notification)) {
56 $mail = $mailClass->getOneByName($notification);
57 if (!empty($mail->id)) {
58 $tempId = $mail->id;
59 }
60 }
61
62 $isAutomationAdmin = false;
63 $fromMail = '';
64
65 if (!empty($fromId)) {
66 $fromMail = $mailClass->getOneById($fromId);
67 }
68
69 if ($type === 'automation_admin') {
70 $type = MailClass::TYPE_AUTOMATION;
71 $isAutomationAdmin = true;
72 }
73
74 // new mails
75 if (empty($tempId)) {
76 if (empty($fromId)) {
77 $mail = new \stdClass();
78 $mail->name = '';
79 $mail->subject = '';
80 $mail->preheader = '';
81 $mail->tags = [];
82 $mail->type = $type;
83 $mail->body = '';
84 $mail->editor = in_array($type, [MailClass::TYPE_AUTOMATION, MailClass::TYPE_FOLLOWUP, MailClass::TYPE_SCENARIO]) ? 'acyEditor' : $typeEditor;
85 $mail->headers = '';
86 $mail->thumbnail = null;
87 $mail->links_language = '';
88 $mail->from_email = '';
89 $mail->from_name = '';
90 $mail->reply_to_email = '';
91 $mail->reply_to_name = '';
92 } else {
93 $mail = $fromMail;
94 $mail->id = 0;
95 $mail->from_email = $fromMail->from_email;
96 $mail->from_name = $fromMail->from_name;
97 $mail->reply_to_email = $fromMail->reply_to_email;
98 $mail->reply_to_name = $fromMail->reply_to_name;
99 if (0 == $mail->drag_editor) {
100 $mail->editor = 'html';
101 } else {
102 $mail->editor = !empty($typeEditor) ? $typeEditor : 'acyEditor';
103 }
104 }
105 $mail->access = [];
106 $mail->delay = 0;
107 $mail->delay_unit = FollowupClass::DEFAULT_DELAY_UNIT;
108
109 if (!empty($type)) $mail->type = $type;
110
111 if (MailClass::TYPE_AUTOMATION != $type || empty($fromId)) $mail->id = 0;
112
113 switch ($type) {
114 case MailClass::TYPE_WELCOME:
115 $breadcrumbTitle = 'ACYM_CREATE_WELCOME_MAIL';
116 break;
117 case MailClass::TYPE_UNSUBSCRIBE:
118 $breadcrumbTitle = 'ACYM_CREATE_UNSUBSCRIBE_MAIL';
119 break;
120 case MailClass::TYPE_AUTOMATION:
121 $breadcrumbTitle = 'ACYM_NEW_EMAIL';
122 break;
123 case MailClass::TYPE_FOLLOWUP:
124 $breadcrumbTitle = 'ACYM_NEW_FOLLOW_UP_EMAIL';
125 break;
126 case MailClass::TYPE_SCENARIO:
127 $breadcrumbTitle = 'ACYM_SCENARIO_EMAIL';
128 break;
129 default:
130 $breadcrumbTitle = 'ACYM_CREATE_TEMPLATE';
131 }
132
133 $breadcrumbTitle = acym_translation($breadcrumbTitle);
134 $breadcrumbUrl = 'mails&task=edit&type_editor='.$typeEditor.(!empty($fromId) ? '&from='.$fromId : '').'&type='.$type;
135 } else {
136 // Existing mails and notifications
137 if (!$mailClass->hasUserAccess($tempId)) {
138 die('Access denied for this email');
139 }
140
141 $mail = $mailClass->getOneById($tempId);
142
143 if (!empty($fromMail)) {
144 $mail->drag_editor = $fromMail->drag_editor;
145 $mail->body = $fromMail->body;
146 $mail->stylesheet = $fromMail->stylesheet;
147 $mail->settings = $fromMail->settings;
148 $mail->from_email = $fromMail->from_email;
149 $mail->from_name = $fromMail->from_name;
150 $mail->reply_to_email = $fromMail->reply_to_email;
151 $mail->reply_to_name = $fromMail->reply_to_name;
152 }
153
154 if (!empty($followupId)) $followupClass->getDelaySettingToMail($mail, $followupId);
155
156 $mail->editor = $mail->drag_editor == 0 ? 'html' : 'acyEditor';
157 if (!empty($typeEditor)) $mail->editor = $typeEditor;
158
159 if (empty($notification)) {
160 if (in_array($mail->type, [MailClass::TYPE_WELCOME, MailClass::TYPE_UNSUBSCRIBE])) {
161 array_pop($this->breadcrumb);
162 $this->breadcrumb[acym_translation('ACYM_LISTS')] = acym_completeLink('lists');
163 }
164
165 if ($mail->type === MailClass::TYPE_OVERRIDE) {
166 array_pop($this->breadcrumb);
167 $this->breadcrumb[acym_translation('ACYM_EMAILS_OVERRIDE')] = acym_completeLink('override');
168 acym_loadLanguageFile('plg_user_joomla', ACYM_BASE);
169 acym_loadLanguageFile('com_users');
170 acym_loadLanguageFile('com_contact');
171 $breadcrumbTitle = acym_translationSprintf(
172 preg_replace(
173 '#^{trans:([A-Z_]+)(|.+)*}$#',
174 '$1',
175 $mail->subject
176 ),
177 '{param1}',
178 '{param2}'
179 );
180 } else {
181 $breadcrumbTitle = $mail->name;
182 }
183
184 $breadcrumbUrl = 'mails&task=edit&id='.$mail->id;
185 } else {
186 if (empty($return)) {
187 $httpReferer = acym_getVar('string', 'HTTP_REFERER', '', 'SERVER');
188 $return = empty($httpReferer) ? '' : $httpReferer;
189 }
190
191 $notifName = acym_translation('ACYM_NOTIFICATIION_'.strtoupper(substr($mail->name, 4)));
192 if (strpos($notifName, 'ACYM_NOTIFICATIION_') !== false) {
193 array_pop($this->breadcrumb);
194 $this->breadcrumb[acym_translation('ACYM_CONFIGURATION')] = acym_completeLink('configuration');
195
196 $notifName = $mail->name;
197
198 if ($notifName === 'acy_confirm') {
199 $notifName = acym_translation('ACYM_CONFIRMATION_EMAIL');
200 } elseif ($notifName === 'acy_notification_create') {
201 $notifName = acym_translation('ACYM_NOTIFICATION_CREATE_EMAIL');
202 } elseif ($notifName === 'acy_notification_unsub') {
203 $notifName = acym_translation('ACYM_NOTIFICATION_UNSUB_EMAIL');
204 } elseif ($notifName === 'acy_notification_unsuball') {
205 $notifName = acym_translation('ACYM_NOTIFICATION_UNSUBALL_EMAIL');
206 } elseif ($notifName === 'acy_notification_subform') {
207 $notifName = acym_translation('ACYM_NOTIFICATION_SUBFORM_EMAIL');
208 } elseif ($notifName === 'acy_notification_profile') {
209 $notifName = acym_translation('ACYM_NOTIFICATION_PROFILE_EMAIL');
210 } elseif ($notifName === 'acy_notification_confirm') {
211 $notifName = acym_translation('ACYM_NOTIFICATION_CONFIRM_EMAIL');
212 }
213 }
214
215 $breadcrumbTitle = $notifName;
216 $breadcrumbUrl = 'mails&task=edit&notification='.$mail->name;
217 }
218
219 if (!empty($mail->stylesheet) && strpos($mail->stylesheet, '[class="') !== false) {
220 acym_enqueueMessage(acym_translation('ACYM_WARNING_STYLESHEET_NOT_CORRECT'), 'warning');
221 }
222 }
223
224 if (!empty($return)) {
225 $breadcrumbUrl .= '&return='.urlencode(base64_encode($return));
226 }
227 $this->breadcrumb[$breadcrumbTitle] = acym_completeLink($breadcrumbUrl);
228
229 $lists = [];
230
231 if (in_array($mail->type, [MailClass::TYPE_WELCOME, MailClass::TYPE_UNSUBSCRIBE])) {
232 $listClass = new ListClass();
233 $lists = $listClass->getAllWithIdName();
234
235 if (empty($listIds) && !empty($mail->id)) $listIds = $listClass->getListIdsByWelcomeUnsub($mail->id, $mail->type == MailClass::TYPE_WELCOME);
236 if (!is_array($listIds)) {
237 $listIds = [$listIds];
238 }
239 }
240
241 if (!empty($mail->attachments) && !is_array($mail->attachments)) {
242 $mail->attachments = json_decode($mail->attachments, true);
243 } elseif (empty($mail->attachments)) {
244 $mail->attachments = [];
245 }
246
247 $mail->thumbnail = $mail->thumbnail ?? '';
248
249 $tagClass = new TagClass();
250 $languages = acym_getLanguages(true, true);
251 $data = [
252 'mail' => $mail,
253 'allTags' => $tagClass->getAllTagsByType(TagClass::TYPE_MAIL),
254 'isAutomationAdmin' => $isAutomationAdmin,
255 'social_icons' => $this->config->get('social_icons', '{}'),
256 'fromId' => $fromId,
257 'langChoice' => count($languages) < 2
258 ? []
259 : [
260 'links' => $mail->links_language,
261 'name' => 'mail[links_language]',
262 ],
263 'list_id' => $listIds,
264 'lists' => $lists,
265 'delay_unit' => $followupClass->getDelayUnits(),
266 'default_delay_unit' => FollowupClass::DEFAULT_DELAY_UNIT,
267 'followup_id' => $followupId,
268 'uploadFileType' => new UploadFileType(),
269 'mailClass' => $mailClass,
270 ];
271 $followup = $followupClass->getOneById($data['followup_id']);
272
273 if (!empty($followup)) {
274 $data['followup_trigger'] = $followup->trigger;
275 }
276
277 $this->prepareMailMultilingual($data);
278 $this->prepareEditorEdit($data);
279
280 $campaignController->prepareMaxUpload($data);
281
282 if (!empty($return)) {
283 $data['return'] = $return;
284 }
285
286 acym_setVar('layout', 'edit');
287 $data['menuClass'] = $this->menuClass;
288
289 parent::display($data);
290 }
291
292 private function prepareMailMultilingual(array &$data): void
293 {
294 $mail = $data['mail'];
295 if (in_array($mail->type, [MailClass::TYPE_WELCOME, MailClass::TYPE_UNSUBSCRIBE, MailClass::TYPE_NOTIFICATION]) && acym_isMultilingual()) {
296 $data['multilingual'] = true;
297 }
298 }
299
300 private function prepareEditorEdit(array &$data): void
301 {
302 $data['editor'] = new EditorHelper();
303 $data['editor']->content = $data['mail']->body;
304 $data['editor']->autoSave = empty($data['mail']->autosave) ? '' : $data['mail']->autosave;
305 $data['editor']->editor = empty($data['mail']->editor) ? '' : $data['mail']->editor;
306 if (!empty($data['mail']->id)) $data['editor']->mailId = $data['mail']->id;
307 if (!empty($data['mail']->type)) $data['editor']->automation = $data['isAutomationAdmin'];
308 if (!empty($data['mail']->settings)) $data['editor']->settings = $data['mail']->settings;
309 if (!empty($data['mail']->stylesheet)) $data['editor']->stylesheet = $data['mail']->stylesheet;
310
311 $data['editor']->data = [
312 'mail' => $data['mail'],
313 'mailClass' => new MailClass(),
314 ];
315
316 if (!empty($data['multilingual'])) {
317 $data['editor']->data['tagClass'] = new TagClass();
318 $data['editor']->data['main_language'] = $this->config->get('multilingual_default');
319 $data['editor']->data['languages'] = explode(',', $this->config->get('multilingual_languages'));
320 $data['editor']->data['multilingual'] = true;
321
322 $allLanguages = acym_getLanguages();
323
324 $mainLang = new \stdClass();
325 $mainLang->code = $data['editor']->data['main_language'];
326 $mainLang->name = empty($allLanguages[$data['editor']->data['main_language']]) ? $data['editor']->data['main_language']
327 : $allLanguages[$data['editor']->data['main_language']]->name;
328 $data['editor']->data['main_language'] = $mainLang;
329 $mailClass = new MailClass();
330 $mailId = empty($data['mail']->id) ? -1 : $data['mail']->id;
331 $translations = $mailClass->getTranslationsById($mailId, true);
332 foreach ($data['editor']->data['languages'] as $i => $oneLangCode) {
333 $data['editor']->data['languages'][$i] = new \stdClass();
334 $data['editor']->data['languages'][$i]->code = $oneLangCode;
335 $data['editor']->data['languages'][$i]->name = empty($allLanguages[$oneLangCode]) ? $oneLangCode : $allLanguages[$oneLangCode]->name;
336 $data['editor']->data['languages'][$i]->subject = empty($translations[$oneLangCode]->subject) ? '' : $translations[$oneLangCode]->subject;
337 $data['editor']->data['languages'][$i]->preview = empty($translations[$oneLangCode]->preheader) ? '' : $translations[$oneLangCode]->preheader;
338 $data['editor']->data['languages'][$i]->content = empty($translations[$oneLangCode]->body) ? '' : $translations[$oneLangCode]->body;
339 $data['editor']->data['languages'][$i]->autosave = empty($translations[$oneLangCode]->autosave) ? '' : $translations[$oneLangCode]->autosave;
340 $data['editor']->data['languages'][$i]->settings = empty($translations[$oneLangCode]->settings) ? '' : $translations[$oneLangCode]->settings;
341 $data['editor']->data['languages'][$i]->stylesheet = empty($translations[$oneLangCode]->stylesheet) ? '' : $translations[$oneLangCode]->stylesheet;
342 }
343 $data['editor']->data['editor'] = new \stdClass();
344 $data['editor']->data['editor']->editor = 'acyEditor';
345 }
346
347 if ($data['editor']->isDragAndDrop()) {
348 $this->loadScripts['edit'][] = 'editor-wysid';
349 $this->loadScripts['edit']['vue-applications'] = ['custom_view'];
350 } else {
351 $this->loadScripts['edit'][] = 'dtextPicker';
352 }
353 }
354
355 /**
356 * Spaghetti code, needs refactoring.
357 *
358 * 1 - Save the main mail
359 * 2 - Save the alternate versions if any
360 */
361 public function store(bool $ajax = false): int
362 {
363 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
364
365 $mailClass = new MailClass();
366 $formData = acym_getVar('array', 'mail', []);
367 $versions = acym_getVar('array', 'versions', [], 'REQUEST', ACYM_ALLOWRAW);
368 $versionType = acym_getVar('string', 'version_type', '');
369 $currentVersion = acym_getVar('string', 'current_version', 'main');
370 $allowedFields = array_diff(acym_getColumns('mail'), ['creator_id']);
371 $fromId = acym_getVar('int', 'fromId', 0);
372 $return = acym_getVar('string', 'return');
373
374 $fromAutomation = false;
375 if (!empty($return) && strpos($return, 'automation') !== false) {
376 $fromAutomation = true;
377 }
378
379 $mail = new \stdClass();
380 foreach ($formData as $name => $data) {
381 if (!in_array($name, $allowedFields)) {
382 continue;
383 }
384
385 $mail->{$name} = $data;
386 }
387
388 if (!empty($mail->id)) {
389 if (!$mailClass->hasUserAccess($mail->id, true)) {
390 die('Cannot save this mail');
391 }
392
393 $previousMail = $mailClass->getOneById($mail->id);
394 }
395
396 $saveAsTmpl = acym_getVar('int', 'saveAsTmpl', 0);
397 if (!empty($versions)) {
398 $saveAsTmplVersion = $saveAsTmpl === 1 ? acym_getVar('string', 'saveAsTmplVersion', 'main') : 'main';
399 if (!empty($versions[$saveAsTmplVersion]['subject'])) $mail->subject = $versions[$saveAsTmplVersion]['subject'];
400 if (!empty($versions[$saveAsTmplVersion]['preview'])) $mail->preheader = $versions[$saveAsTmplVersion]['preview'];
401 if (!empty($versions[$saveAsTmplVersion]['content'])) $mail->body = $versions[$saveAsTmplVersion]['content'];
402 if (!empty($versions[$saveAsTmplVersion]['settings'])) $mail->settings = $versions[$saveAsTmplVersion]['settings'];
403 if (!empty($versions[$saveAsTmplVersion]['stylesheet'])) $mail->stylesheet = $versions[$saveAsTmplVersion]['stylesheet'];
404
405 if ($versionType === 'multilingual') {
406 $mail->links_language = $this->config->get('multilingual_default');
407 }
408 }
409
410 if ($saveAsTmpl === 1) {
411 unset($mail->id);
412 $mail->type = MailClass::TYPE_TEMPLATE;
413 }
414
415 if ($fromAutomation) {
416 acym_setVar('from', $mail->id);
417 acym_setVar('type', MailClass::TYPE_AUTOMATION);
418 acym_setVar('type_editor', 'acyEditor');
419 }
420
421 if (empty($mail->subject) && !empty($mail->type) && $mail->type !== MailClass::TYPE_TEMPLATE) {
422 $mail->subject = acym_translation('ACYM_EMAIL_SUBJECT');
423 }
424
425 $inputNameBody = $saveAsTmpl === 1 ? 'editor_content_template' : 'editor_content';
426 $inputNameSettings = $saveAsTmpl === 1 ? 'editor_settings_template' : 'editor_settings';
427 $inputNameStylesheet = $saveAsTmpl === 1 ? 'editor_stylesheet_template' : 'editor_stylesheet';
428 $inputNameColors = $saveAsTmpl === 1 ? 'main_colors_template' : 'main_colors';
429
430 $mail->tags = acym_getVar('array', 'template_tags', []);
431 $mail->body = acym_getVar('string', $inputNameBody, '', 'REQUEST', ACYM_ALLOWRAW);
432 $mail->settings = acym_getVar('string', $inputNameSettings, '', 'REQUEST', ACYM_ALLOWRAW);
433 $mail->stylesheet = acym_getVar('string', $inputNameStylesheet, '', 'REQUEST', ACYM_ALLOWRAW);
434 $mail->headers = acym_getVar('string', 'editor_headers', '', 'REQUEST', ACYM_ALLOWRAW);
435 $mail->drag_editor = strpos($mail->body, 'acym__wysid__template') === false ? 0 : 1;
436 $mail->thumbnail = '';
437
438 if ($fromAutomation) {
439 $mail->type = MailClass::TYPE_AUTOMATION;
440 } else {
441 $thumbnailName = acym_getVar('string', 'editor_thumbnail', '', 'REQUEST', ACYM_ALLOWRAW);
442
443 if (preg_match('#^thumbnail_([0-9]*)\.png$#', $thumbnailName)) {
444 $mail->thumbnail = $thumbnailName;
445 }
446 }
447
448 if (empty($mail->id)) {
449 $mail->creation_date = acym_date('now', 'Y-m-d H:i:s', false);
450 }
451
452 $uploadedThumbnail = $this->setThumbnailFromInput();
453 if (!empty($uploadedThumbnail)) {
454 if (!empty($previousMail->thumbnail)) {
455 acym_deleteFile(ACYM_UPLOAD_FOLDER_THUMBNAIL.$previousMail->thumbnail);
456 }
457
458 $mail->thumbnail = $uploadedThumbnail;
459 } elseif (acym_getVar('bool', 'custom_thumbnail_reset', false)) {
460 if (!empty($previousMail->thumbnail)) {
461 acym_deleteFile(ACYM_UPLOAD_FOLDER_THUMBNAIL.$previousMail->thumbnail);
462 }
463
464 $mail->thumbnail = null;
465 }
466
467 if (!empty($previousMail->mail_settings)) {
468 $mailSettings = json_decode($previousMail->mail_settings, false);
469 } else {
470 $mailSettings = new \stdClass();
471 }
472
473 $mailSettings->mainColors = acym_getVar('string', $inputNameColors, '', 'REQUEST', ACYM_ALLOWRAW);
474 $mail->mail_settings = json_encode($mailSettings);
475
476 // Use the thumbnail of the source mail if not modified
477 if (!empty($fromId) && empty($mail->thumbnail) && !$fromAutomation) {
478 $thumbnail = $this->setThumbnailFrom($fromId);
479 if (!empty($thumbnail)) {
480 if (!empty($previousMail->thumbnail)) {
481 acym_deleteFile(ACYM_UPLOAD_FOLDER_THUMBNAIL.$previousMail->thumbnail);
482 }
483
484 $mail->thumbnail = $thumbnail;
485 }
486 }
487
488 if (empty($mail->name) && !in_array($mail->type, MailClass::TYPES_NO_NAME) && empty($mail->id)) {
489 $mail->name = empty($mail->subject) ? acym_translation('ACYM_TEMPLATE_NAME') : $mail->subject;
490 }
491
492 $this->setAttachmentToMail($mail);
493
494 if ($versionType === 'multilingual' && $currentVersion !== 'main') {
495 if (empty($mail->id)) {
496 $mail->subject = $versions['main']['subject'];
497 $mail->body = $versions['main']['content'];
498 $mail->preheader = $versions['main']['preview'];
499 $mail->settings = $versions['main']['settings'];
500 $mail->stylesheet = $versions['main']['stylesheet'];
501
502 $mailID = $mailClass->save($mail);
503 } else {
504 $mailID = $mail->id;
505 }
506 } else {
507 $mailID = $mailClass->save($mail);
508 }
509
510 if (empty($mailID)) {
511 if (!$ajax) {
512 acym_enqueueMessage(acym_translation('ACYM_ERROR_SAVING'), 'error');
513 }
514
515 if (!empty($mailClass->errors)) {
516 if (!$ajax) acym_enqueueMessage($mailClass->errors, 'error');
517 }
518
519 return 0;
520 }
521
522 if (!empty($mail->type) && in_array($mail->type, [MailClass::TYPE_WELCOME, MailClass::TYPE_UNSUBSCRIBE])) {
523 $listIds = acym_getVar('array', 'list_ids', []);
524 $listClass = new ListClass();
525 $listClass->setWelcomeUnsubEmail($listIds, (int)$mailID, $mail->type);
526 } elseif (!empty($mail->type) && $mail->type === MailClass::TYPE_FOLLOWUP) {
527 // Pass the new email ID in the return URL to ask user if we should add it to the queue
528 acym_setVar('return', acym_getVar('string', 'return').'&newEmailId='.$mailID);
529
530 $followupData = acym_getVar('array', 'followup', []);
531 $followupClass = new FollowupClass();
532 if (!$followupClass->saveDelaySettings($followupData, $mailID)) {
533 acym_enqueueMessage(acym_translation('ACYM_COULD_NOT_SAVE_DELAY_SETTINGS'), 'error');
534 }
535 if (!empty($followupData['id'])) {
536 acym_setVar('followup_id', $followupData['id']);
537 }
538 }
539
540 if (!$ajax) {
541 acym_enqueueMessage(acym_translation('ACYM_SUCCESSFULLY_SAVED'));
542 }
543
544 if ($fromAutomation) {
545 acym_setVar('type', MailClass::TYPE_AUTOMATION);
546 acym_setVar('type_editor', 'acyEditor');
547 } else {
548 acym_setVar('mailID', $mailID);
549 }
550
551 if (!empty($versions) && in_array($versionType, ['multilingual', 'abtest']) && $saveAsTmpl !== 1) {
552 $abTestSendingParams = [];
553
554 unset($versions['main']);
555 foreach ($versions as $code => $version) {
556 if (empty($version['subject'])) {
557 if ($versionType === 'multilingual') {
558 $mailClass->delete([$mailClass->getTranslationId($mailID, $code)]);
559 } elseif (!empty($abTestSendingParams[$code])) {
560 $mailClass->delete([$abTestSendingParams[$code]]);
561 }
562 continue;
563 }
564
565 unset($mail->id);
566 $versionId = null;
567 if ($versionType === 'multilingual') {
568 $versionId = $mailClass->getTranslationId($mailID, $code);
569 } elseif (!empty($abTestSendingParams[$code])) {
570 $versionId = $abTestSendingParams[$code];
571 }
572
573 if (!empty($versionId)) {
574 $mail->id = $versionId;
575 }
576
577 $mail->subject = $version['subject'];
578 $mail->preheader = $version['preview'];
579 $mail->body = $version['content'];
580 $mail->parent_id = $mailID;
581 $mail->settings = $version['settings'];
582 $mail->stylesheet = $version['stylesheet'];
583
584 if ($versionType === 'multilingual') {
585 $mail->links_language = $code;
586 $mail->language = $code;
587 }
588
589 $abTestSendingParams[$code] = $mailClass->save($mail);
590 }
591 }
592
593 return (int)$mailID;
594 }
595
596 private function setThumbnailFromInput(): string
597 {
598 $thumbnailFile = acym_getVar('array', 'custom_thumbnail', [], 'FILES');
599 if (empty($thumbnailFile['name'])) {
600 return '';
601 }
602
603 $extension = acym_fileGetExt($thumbnailFile['name']);
604
605 $thumbNb = $this->config->get('numberThumbnail', 2);
606 $filename = 'thumbnail_custom_'.($thumbNb + 1).'.'.$extension;
607 $this->config->saveConfig(['numberThumbnail' => $thumbNb + 1]);
608 $thumbnailFile['name'] = $filename;
609
610 ob_start();
611 $uploaded = acym_importFile($thumbnailFile, ACYM_UPLOAD_FOLDER_THUMBNAIL, true);
612 ob_end_clean();
613 if (!empty($uploaded)) {
614 return $uploaded;
615 }
616
617 acym_enqueueMessage(acym_translation('ACYM_UPLOADED_FILE_IS_NOT_IMAGE'), 'error');
618
619 return '';
620 }
621
622 public function setAttachmentToMail(object $mail): void
623 {
624 if (!empty($mail->id)) {
625 $mailClass = new MailClass();
626 $mail->attachments = $mailClass->getMailAttachments($mail->id);
627 }
628
629 if (!empty($mail->attachments)) {
630 if (!is_array($mail->attachments)) {
631 $mail->attachments = json_decode($mail->attachments, true);
632 }
633 } else {
634 $mail->attachments = [];
635 }
636
637 // Attachments
638 $newAttachments = [];
639 $attachments = acym_getVar('array', 'attachments', []);
640 if (!empty($attachments)) {
641 foreach ($attachments as $filepath) {
642 if (empty($filepath)) continue;
643
644 $attachment = [
645 'filename' => $filepath,
646 'size' => filesize(ACYM_ROOT.$filepath),
647 ];
648
649 //We will never allow some files to be uploaded...
650 if (preg_match('#\.(php.?|.?htm.?|pl|py|jsp|asp|sh|cgi)#Ui', $attachment['filename'])) {
651 acym_enqueueMessage(
652 acym_translationSprintf(
653 'ACYM_ACCEPTED_TYPE',
654 substr($attachment['filename'], strrpos($attachment['filename'], '.') + 1),
655 $this->config->get('allowed_files')
656 ),
657 'notice'
658 );
659 continue;
660 }
661
662 if (in_array($attachment, $mail->attachments)) continue;
663
664 $newAttachments[] = $attachment;
665 }
666 // Add to previous attachments
667 if (!empty($mail->attachments)) {
668 $newAttachments = array_merge($mail->attachments, $newAttachments);
669 }
670 $mail->attachments = $newAttachments;
671 }
672
673 if (empty($mail->attachments)) {
674 unset($mail->attachments);
675 } else {
676 $mail->attachments = json_encode($mail->attachments);
677 }
678 }
679
680 private function setThumbnailFrom(int $fromId): string
681 {
682 $thumbNb = $this->config->get('numberThumbnail', 2);
683 $fileName = 'thumbnail_'.($thumbNb + 1).'.png';
684 $this->config->saveConfig(['numberThumbnail' => $thumbNb + 1]);
685
686 $mailClass = new MailClass();
687 $fromMail = $mailClass->getOneById($fromId);
688 $fromThumbnail = $fromMail->thumbnail;
689
690 $ret = acym_createFolder(ACYM_UPLOAD_FOLDER_THUMBNAIL);
691 if (!$ret) {
692 return '';
693 }
694
695 $fromThumbnailSource = acym_fileGetContent(acym_getMailThumbnail($fromThumbnail));
696 if (empty($fromThumbnailSource)) {
697 return '';
698 }
699
700 file_put_contents(ACYM_UPLOAD_FOLDER_THUMBNAIL.$fileName, $fromThumbnailSource);
701
702 return $fileName;
703 }
704
705 public function apply(): void
706 {
707 $mailId = $this->store();
708 acym_setVar('id', $mailId);
709 $this->edit();
710 }
711
712 public function save(): void
713 {
714 $mailId = $this->store();
715
716 // When saving notifications, we return to page where we clicked the "Edit email" button
717 $return = str_replace(
718 '{mailid}',
719 empty($mailId) ? '' : $mailId,
720 acym_getVar('string', 'return', '')
721 );
722
723 if (empty($return)) {
724 $this->listing();
725 } else {
726 acym_redirect($return);
727 }
728 }
729
730 public function autoSave(): void
731 {
732 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
733
734 $mailClass = new MailClass();
735 $mail = new \stdClass();
736
737 $language = acym_getVar('string', 'language', 'main');
738 $mail->id = acym_getVar('int', 'mailId', 0);
739 $mail->autosave = base64_decode(acym_getVar('string', 'autoSave', '', 'REQUEST', ACYM_ALLOWRAW));
740
741 if (empty($mail->id) || !$mailClass->hasUserAccess($mail->id, true) || !$mailClass->autoSave($mail, $language)) {
742 acym_sendAjaxResponse('', [], false);
743 } else {
744 acym_sendAjaxResponse();
745 }
746 }
747
748 public function ajaxCheckVideoUrl(): void
749 {
750 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
751 $videoUrl = acym_getVar('string', 'url', '');
752
753 if (!acym_isValidUrl($videoUrl)) {
754 acym_sendAjaxResponse('', [], false);
755 }
756
757 $image = '';
758 $imageName = '';
759
760 $youtubeMatch = '';
761 $vimeoMatch = '';
762 $dailymotionMatch = '';
763
764 preg_match('/(?:https?:\/{2})?(?:w{3}\.)?youtu(?:be)?\.(?:com|be)(?:\/watch\?v=|\/)([^\s&]+)/', $videoUrl, $youtubeMatch);
765 preg_match('/^.*(vimeo\.com\/)((channels\/[A-z]+\/)|(groups\/[A-z]+\/videos\/))?([0-9]+)/', $videoUrl, $vimeoMatch);
766 preg_match('/^(?:(?:http|https):\/\/)?(?:www.)?(dailymotion\.com|dai\.ly)\/((video\/([^_]+))|(hub\/([^_]+)|([^\/_]+)))$/', $videoUrl, $dailymotionMatch);
767
768 if (!empty($youtubeMatch)) {
769 $image = 'https://img.youtube.com/vi/'.$youtubeMatch[1].'/0.jpg';
770 $imageName = $youtubeMatch[1];
771 } elseif (!empty($dailymotionMatch)) {
772 $dailymotionImage = $dailymotionMatch[4] ?? $dailymotionMatch[2];
773
774 $image = 'https://www.dailymotion.com/thumbnail/video/'.$dailymotionImage;
775 $imageName = $dailymotionMatch[2];
776 } elseif (!empty($vimeoMatch)) {
777 $image = unserialize(file_get_contents('https://vimeo.com/api/v2/video/'.$vimeoMatch[5].'.php'));
778 $image = $image[0]['thumbnail_large'];
779 $imageName = $vimeoMatch[5];
780 }
781
782 if (empty($image) || !acym_isValidUrl($image)) {
783 acym_sendAjaxResponse('', [], false);
784 }
785
786 acym_sendAjaxResponse('', ['new_image_name' => $this->saveVideoPreview($image, urlencode($imageName).'.jpg')]);
787 }
788
789 private function saveVideoPreview(string $image, string $fileName): string
790 {
791 if (!$this->config->get('add_play_button_video', 1)) {
792 return $image;
793 }
794
795 $imageVideo = imagecreatefromjpeg($image);
796 $playButton = @imagecreatefrompng(ACYM_ROOT.ACYM_MEDIA_FOLDER.'images'.DS.'editor'.DS.'video_insertion'.DS.'play_button.png');
797 if ($playButton === false || $imageVideo === false) {
798 return $image;
799 }
800 $imageWidth = imagesx($imageVideo);
801 $imageHeight = imagesy($imageVideo);
802 $logoWidth = imagesx($playButton);
803 $logoHeight = imagesy($playButton);
804
805 $left = round(($imageWidth - $logoWidth) / 2);
806 $top = round(($imageHeight - $logoHeight) / 2);
807 imagecopy($imageVideo, $playButton, $left, $top, 0, 0, $logoWidth, $logoHeight);
808 $tmpFilePath = ACYM_TMP_FOLDER.'tmp.jpg';
809 acym_createFolder(ACYM_TMP_FOLDER);
810 imagepng($imageVideo, $tmpFilePath, 9);
811 $input = imagecreatefrompng($tmpFilePath);
812 $output = imagecreatetruecolor($imageWidth, $imageHeight);
813 $white = imagecolorallocate($output, 255, 255, 255);
814
815 imagefilledrectangle($output, 0, 0, $imageWidth, $imageHeight, $white);
816 imagecopy($output, $input, 0, 0, 0, 0, $imageWidth, $imageHeight);
817
818 ob_start();
819 $status = imagejpeg($output, null, 95);
820 $imageContent = ob_get_clean();
821 if ($status && acym_writeFile(ACYM_ROOT.ACYM_UPLOAD_FOLDER.$fileName, $imageContent)) {
822 acym_deleteFile($tmpFilePath);
823
824 return ACYM_UPLOADS_URL.$fileName;
825 }
826
827 acym_deleteFile($tmpFilePath);
828
829 return '';
830 }
831
832 public function getTemplateAjax(): void
833 {
834 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
835
836 $pagination = new PaginationHelper();
837 $id = acym_getVar('int', 'id');
838 $id = empty($id) ? '' : '&id='.$id;
839 $searchFilter = acym_getVar('string', 'search', '');
840 $tagFilter = acym_getVar('string', 'tag', 0);
841 $ordering = 'creation_date';
842 $orderingSortOrder = 'DESC';
843 $type = acym_getVar('string', 'type', 'custom');
844 $automation = acym_getVar('boolean', 'automation', false);
845 $returnUrl = acym_getVar('string', 'return');
846 $returnUrl = empty($returnUrl) || 'undefined' == $returnUrl ? '' : '&return='.urlencode(base64_encode($returnUrl));
847 $fromMultilingual = acym_getVar('int', 'is_multilingual_edition', 0);
848
849 $mailsPerPage = $pagination->getListLimit();
850 $page = acym_getVar('int', 'pagination_page_ajax', 1);
851 if ($page === 'undefined') {
852 $page = 1;
853 }
854
855 $mailClass = new MailClass();
856 $matchingMails = $mailClass->getMatchingElements(
857 [
858 'ordering' => $ordering,
859 'ordering_sort_order' => $orderingSortOrder,
860 'search' => $searchFilter,
861 'elementsPerPage' => $mailsPerPage,
862 'offset' => ($page - 1) * $mailsPerPage,
863 'tag' => $tagFilter,
864 'automation' => $automation,
865 'onlyStandard' => true,
866 'creator_id' => $this->setFrontEndParamsForTemplateChoose(),
867 'drag_editor' => !empty($fromMultilingual),
868 'gettingTemplates' => !$automation,
869 ]
870 );
871
872 $return = '<div class="grid-x grid-padding-x grid-padding-y grid-margin-x grid-margin-y xxlarge-up-6 large-up-4 medium-up-3 small-up-1 cell acym__template__choose__list margin-top-1">';
873
874 $followup_id = '';
875 if ($type == MailClass::TYPE_FOLLOWUP) {
876 $followup_id = acym_getVar('int', 'followup_id', 0);
877 }
878 $listId = 0;
879 if (in_array($type, [MailClass::TYPE_WELCOME, MailClass::TYPE_UNSUBSCRIBE])) {
880 $listId = acym_getVar('int', 'list_id', 0);
881 }
882 foreach ($matchingMails['elements'] as $oneTemplate) {
883 $return .= '<div class="cell grid-x acym__templates__oneTpl acym__listing__block" id="'.esc_attr($oneTemplate->id).'">
884 <div class="cell acym__templates__pic text-center">';
885
886 $url = acym_getVar('cmd', 'ctrl').'&task=edit&step=editEmail&from='.intval($oneTemplate->id).$returnUrl.'&type='.$type.$id.'&'.acym_getFormToken();
887 if (!empty($followup_id)) {
888 $url .= '&followup_id='.$followup_id;
889 }
890 if (!empty($listId)) {
891 $url .= '&list_id='.$listId;
892 }
893 if (!empty($this->data['campaignInformation'])) {
894 $url .= '&id='.intval($this->data['campaignInformation']);
895 }
896 if (!$automation || !empty($returnUrl)) {
897 $return .= '<a href="'.esc_url(acym_completeLink($url, false, false, true)).'">';
898 }
899
900 $return .= '<img src="'.esc_url(acym_getMailThumbnail($oneTemplate->thumbnail)).'" alt="template thumbnail"/>';
901 if (!$automation || !empty($returnUrl)) {
902 $return .= '</a>';
903 }
904
905 if ($oneTemplate->drag_editor) {
906 $return .= '<div class="acym__templates__choose__ribbon acyeditor">'.esc_html(acym_translation('ACYM_DD_EDITOR')).'</div>';
907 } else {
908 $return .= '<div class="acym__templates__choose__ribbon htmleditor">'.esc_html(acym_translation('ACYM_HTML_EDITOR')).'</div>';
909 }
910
911 if (strlen($oneTemplate->name) > 55) {
912 $oneTemplate->name = substr($oneTemplate->name, 0, 50).'...';
913 }
914 $return .= '</div>
915 <div class="cell grid-x acym__templates__footer text-center">
916 <div class="cell acym__templates__footer__title acym_text_ellipsis" title="'.esc_attr($oneTemplate->name).'">';
917 $return .= esc_html($oneTemplate->name).'</div>
918 <div class="cell">'.esc_html(acym_date($oneTemplate->creation_date, 'ACYM_DATE_FORMAT_LC3')).'</div>
919 </div>
920 </div>';
921 }
922
923 $return .= '</div>';
924
925 $pagination->setStatus((int)$matchingMails['total']->total, $page, $mailsPerPage);
926
927 ob_start();
928 $pagination->display('', '__ajax');
929 $return .= ob_get_clean();
930
931 acym_sendAjaxResponse('', ['pagination' => $return]);
932 exit;
933 }
934
935 public function setNewThumbnail(): void
936 {
937 if (!acym_isAdmin() || (!acym_isAllowed('mails') && !acym_isAllowed('campaigns'))) {
938 die('Access denied for thumbnail creation');
939 }
940
941 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
942 $contentThumbnail = acym_getVar('string', 'content', '');
943 if (strpos($contentThumbnail, 'data:image/png') !== 0) {
944 acym_sendAjaxResponse('This file is not allowed.', [], false);
945 }
946
947 $mailId = acym_getVar('int', 'mailId', 0);
948 if (!empty($mailId)) {
949 $mailClass = new MailClass();
950 $mail = $mailClass->getOneById($mailId);
951 if (!empty($mail)) {
952 $file = $mail->thumbnail;
953 }
954 }
955
956 if (empty($file) || strpos($file, 'http') === 0) {
957 $thumbNb = $this->config->get('numberThumbnail', 2);
958 $file = 'thumbnail_'.($thumbNb + 1).'.png';
959 $this->config->saveConfig(['numberThumbnail' => $thumbNb + 1]);
960 }
961
962 $extension = acym_fileGetExt($file);
963 if (strpos($file, 'thumbnail_') === false || !in_array($extension, ['png', 'jpeg', 'jpg', 'gif', 'webp'])) {
964 acym_sendAjaxResponse('This file is not allowed.', [], false);
965 }
966
967 acym_createFolder(ACYM_UPLOAD_FOLDER_THUMBNAIL);
968 file_put_contents(ACYM_UPLOAD_FOLDER_THUMBNAIL.$file, base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $contentThumbnail)));
969
970 acym_sendAjaxResponse('', ['fileName' => $file]);
971 }
972
973 public function setNewIconShare(): void
974 {
975 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
976 $socialName = acym_getVar('string', 'social', '');
977 $socialMedias = acym_getSocialMedias();
978 if (!in_array($socialName, $socialMedias)) {
979 acym_sendAjaxResponse(acym_translationSprintf('ACYM_UNKNOWN_SOCIAL', $socialName), [], false);
980 }
981
982 $file = acym_getVar('array', 'file', [], 'FILES');
983 $extension = pathinfo($file['name'], PATHINFO_EXTENSION);
984 $newPath = ACYM_UPLOAD_FOLDER.'socials'.DS.$socialName;
985 $newPathComplete = $newPath.'.'.$extension;
986
987 $allowedExtensions = acym_getImageFileExtensions(true);
988 if (!in_array($extension, $allowedExtensions)) {
989 $errorMessage = acym_translationSprintf('ACYM_ACCEPTED_TYPE', $extension, implode(', ', $allowedExtensions));
990 } elseif (!acym_uploadFile($file['tmp_name'], ACYM_ROOT.$newPathComplete)) {
991 $errorMessage = acym_translationSprintf('ACYM_ERROR_UPLOADING_FILE_X', $newPathComplete);
992 } elseif (strtolower($extension) === 'svg' && !acym_isSvgFileSafe(ACYM_ROOT.$newPathComplete)) {
993 acym_deleteFile(ACYM_ROOT.$newPathComplete);
994 $errorMessage = acym_translationSprintf('ACYM_ERROR_UPLOADING_FILE_X', $newPathComplete);
995 }
996
997 if (!empty($errorMessage)) {
998 acym_sendAjaxResponse($errorMessage, [], false);
999 }
1000
1001 $newImg = acym_rootURI().$newPathComplete;
1002 $newImgWithoutExtension = acym_rootURI().$newPath;
1003
1004 $socialIcons = json_decode($this->config->get('social_icons', '{}'), true);
1005 $socialIcons[$socialName] = $newImg;
1006 $this->config->saveConfig(['social_icons' => json_encode($socialIcons)]);
1007
1008 acym_sendAjaxResponse(
1009 acym_translation('ACYM_ICON_IMPORTED'),
1010 [
1011 'url' => $newImgWithoutExtension,
1012 'extension' => $extension,
1013 ]
1014 );
1015 }
1016
1017 public function saveAjax(): void
1018 {
1019 $mailId = $this->store(true);
1020
1021 if (!empty($mailId)) {
1022 acym_sendAjaxResponse('', ['result' => $mailId]);
1023 } else {
1024 acym_sendAjaxResponse(acym_translation('ACYM_ERROR_SAVING'), [], false);
1025 }
1026 }
1027
1028 public function loadCSS(): void
1029 {
1030 header('Content-Type: text/css');
1031 $idMail = acym_getVar('int', 'id', 0);
1032 if (empty($idMail)) {
1033 exit;
1034 }
1035
1036 $mailClass = new MailClass();
1037 $mail = $mailClass->getOneById($idMail);
1038
1039 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Returning content as text/css, need to output raw content.
1040 echo $mailClass->buildCSS($mail->stylesheet);
1041 exit;
1042 }
1043
1044 /*
1045 * Method used to send a test when editing a notification email
1046 */
1047 public function test(): void
1048 {
1049 $mailId = $this->store();
1050 $return = acym_getVar('string', 'return', '');
1051 acym_setVar('return', $return);
1052 acym_setVar('id', $mailId);
1053
1054 $mailClass = new MailClass();
1055 $mail = $mailClass->getOneById($mailId);
1056
1057 if (empty($mail)) {
1058 acym_enqueueMessage(acym_translation('ACYM_CAMPAIGN_NOT_FOUND'), 'error');
1059 $this->edit();
1060
1061 return;
1062 }
1063
1064 $mailerHelper = new MailerHelper();
1065 $mailerHelper->autoAddUser = true;
1066 $mailerHelper->report = false;
1067
1068 $currentEmail = acym_currentUserEmail();
1069 $options = ['isTest' => true];
1070 if ($mailerHelper->sendOne($mailId, $currentEmail, $options)) {
1071 acym_enqueueMessage(acym_translationSprintf('ACYM_SEND_SUCCESS', $mail->name, $currentEmail), 'info');
1072 } else {
1073 $notification = [
1074 'name' => 'send_error',
1075 'removable' => 1,
1076 ];
1077 acym_enqueueMessage(acym_translationSprintf('ACYM_SEND_ERROR', $mail->name, $currentEmail), 'error', true, [$notification]);
1078 }
1079
1080 $this->edit();
1081 }
1082
1083 public function sendTest(): void
1084 {
1085 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
1086 $controller = acym_getVar('string', 'controller', 'mails');
1087 $level = 'info';
1088
1089 $testNote = acym_getVar('string', 'test_note', '');
1090 $mailClass = new MailClass();
1091 $mailerHelper = new MailerHelper();
1092 $mailerHelper->autoAddUser = true;
1093 $mailerHelper->report = false;
1094
1095 if (in_array($controller, ['mails', 'frontmails'])) {
1096 $mailId = acym_getVar('int', 'id', 0);
1097 } else {
1098 $campaignId = acym_getVar('int', 'id', 0);
1099 $campaignClass = new CampaignClass();
1100 $campaign = $campaignClass->getOneById($campaignId);
1101 if (empty($campaign)) {
1102 acym_sendAjaxResponse('', ['level' => 'error', 'message' => acym_translation('ACYM_CAMPAIGN_NOT_FOUND')], false);
1103 }
1104 if (!$campaignClass->hasUserAccess($campaignId)) {
1105 die('A test of this campaign cannot be sent');
1106 }
1107
1108 $mailId = $campaign->mail_id;
1109 $mailerHelper->isAbTest = $campaignClass->isAbTestMail($mailId);
1110
1111 $campaignVersion = acym_getVar('string', 'lang_version', 'main');
1112 if (!empty($campaignVersion) && $campaignVersion !== 'main') {
1113 if ($campaignVersion === 'B') {
1114 $versions = $mailClass->getVersionsById($mailId);
1115 if (!empty($versions)) {
1116 $mailIds = array_keys($versions);
1117 $mailId = array_pop($mailIds);
1118 }
1119 } else {
1120 $translationId = $mailClass->getTranslationId($mailId, $campaignVersion);
1121 if (!empty($translationId)) {
1122 $mailId = $translationId;
1123 }
1124 }
1125 }
1126 }
1127
1128 $mail = $mailClass->getOneById($mailId);
1129
1130 if (empty($mail) || !$mailClass->hasUserAccess($mailId)) {
1131 acym_sendAjaxResponse('', ['level' => 'error', 'message' => acym_translation('ACYM_EMAIL_NOT_FOUND')], false);
1132 }
1133
1134 $report = [];
1135
1136 $testEmails = explode(',', acym_getVar('string', 'test_emails'));
1137 $options = [
1138 'isTest' => true,
1139 'testNote' => $testNote,
1140 ];
1141 foreach ($testEmails as $oneAddress) {
1142 if (!$mailerHelper->sendOne($mail->id, $oneAddress, $options)) {
1143 $level = 'error';
1144 }
1145
1146 if (!empty($mailerHelper->reportMessage)) {
1147 $report[] = $mailerHelper->reportMessage;
1148 }
1149 }
1150
1151 acym_sendAjaxResponse('', ['level' => $level, 'message' => implode('<br/>', $report)], $level !== 'error');
1152 }
1153
1154 public function getMailContent(): void
1155 {
1156 $from = acym_getVar('int', 'from', 0);
1157
1158 if (empty($from)) {
1159 acym_sendAjaxResponse(acym_translation('ACYM_EMAIL_NOT_FOUND'), [], false);
1160 }
1161
1162 $mailClass = new MailClass();
1163 $mail = $mailClass->getOneById($from);
1164
1165 if (empty($mail)) {
1166 acym_sendAjaxResponse(acym_translation('ACYM_EMAIL_NOT_FOUND'), [], false);
1167 }
1168
1169 if ($mail->drag_editor == 0) {
1170 acym_sendAjaxResponse('Only available for the drag and drop editor.', [], false);
1171 }
1172
1173 acym_sendAjaxResponse();
1174 }
1175
1176 protected function setFrontEndParamsForTemplateChoose(): int
1177 {
1178 return 0;
1179 }
1180
1181 public function saveAsTmplAjax(): void
1182 {
1183 $mailId = $this->store(true);
1184 acym_sendAjaxResponse(!empty($mailId) ? '' : acym_translation('ACYM_ERROR_SAVING'), ['result' => $mailId], !empty($mailId));
1185 }
1186 }
1187