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