PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / trunk
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress vtrunk
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 / CampaignsController.php

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

231 lines 7.7 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;
4
5 use AcyMailing\Classes\CampaignClass;
6 use AcyMailing\Classes\HistoryClass;
7 use AcyMailing\Classes\MailClass;
8 use AcyMailing\Classes\ListClass;
9 use AcyMailing\Classes\UserClass;
10 use AcyMailing\Helpers\MailerHelper;
11 use AcyMailing\Core\AcymController;
12 use AcyMailing\Controllers\Campaigns\Actions;
13 use AcyMailing\Controllers\Campaigns\AutoCampaigns;
14 use AcyMailing\Controllers\Campaigns\Edition;
15 use AcyMailing\Controllers\Campaigns\Followup;
16 use AcyMailing\Controllers\Campaigns\ListEmails;
17 use AcyMailing\Controllers\Campaigns\Listing;
18 use AcyMailing\Controllers\Campaigns\Tests;
19 use AcyMailing\Controllers\Shared\EditorActions;
20
21 class CampaignsController extends AcymController
22 {
23 use Listing;
24 use Followup;
25 use ListEmails;
26 use AutoCampaigns;
27 use Edition;
28 use Actions;
29 use Tests;
30 use EditorActions;
31
32 const TASK_TYPE_CAMPAIGN = 'campaigns';
33 const TASK_TYPE_CAMPAIGN_AUTO = 'campaigns_auto';
34 const TASK_TYPE_WELCOME = 'welcome';
35 const TASK_TYPE_UNSUBSCRIBE = 'unsubscribe';
36 const TASK_TYPE_FOLLOWUP = 'followup';
37 const TASK_TYPE_SPECIFIC_LISTING = 'specificListing';
38 const TASK_TYPE_MAILBOX_ACTION = 'mailbox_action';
39
40 const TASKS_TO_REMEMBER_FOR_LISTING = [
41 '',
42 self::TASK_TYPE_CAMPAIGN,
43 self::TASK_TYPE_CAMPAIGN_AUTO,
44 self::TASK_TYPE_WELCOME,
45 self::TASK_TYPE_UNSUBSCRIBE,
46 self::TASK_TYPE_FOLLOWUP,
47 self::TASK_TYPE_SPECIFIC_LISTING,
48 self::TASK_TYPE_MAILBOX_ACTION,
49 ];
50
51 const CAMPAIGN_TYPE_TO_TASK = [
52 'now' => self::TASK_TYPE_CAMPAIGN,
53 'scheduled' => self::TASK_TYPE_CAMPAIGN,
54 'auto' => self::TASK_TYPE_CAMPAIGN_AUTO,
55 'followup' => self::TASK_TYPE_FOLLOWUP,
56 'welcome' => self::TASK_TYPE_WELCOME,
57 'unsubscribe' => self::TASK_TYPE_UNSUBSCRIBE,
58 'birthday' => self::TASK_TYPE_SPECIFIC_LISTING,
59 'woocommerce_cart' => self::TASK_TYPE_SPECIFIC_LISTING,
60 ];
61
62 private string $stepContainerClass = '';
63
64 public function __construct()
65 {
66 parent::__construct();
67
68 $this->defaulttask = 'campaigns';
69 $this->breadcrumb[acym_translation('ACYM_EMAILS')] = acym_completeLink('campaigns');
70 $this->loadScripts = [
71 'recipients' => ['vue-applications' => ['entity_select']],
72 'segment' => ['datepicker', 'vue-applications' => ['modal_users_summary']],
73 'send_settings' => ['datepicker'],
74 'summary' => ['vue-applications' => ['modal_users_summary']],
75 ];
76
77 if (acym_isAdmin()) {
78 $this->stepContainerClass = 'xxlarge-9';
79 }
80
81 acym_setVar('edition', '1');
82 acym_header('X-XSS-Protection:0');
83 $this->storeRedirectListing();
84 }
85
86 public function ajaxCountNumberOfRecipients(): void
87 {
88 $listsSelected = acym_getVar('array', 'listsSelected', []);
89 if (empty($listsSelected)) {
90 acym_sendAjaxResponse('', ['recipients' => 0]);
91 }
92
93 $listClass = new ListClass();
94 acym_sendAjaxResponse('', ['recipients' => $listClass->getTotalSubCount($listsSelected)]);
95 }
96
97 public function deleteAttachmentAjax(): void
98 {
99 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
100
101 $mailId = acym_getVar('int', 'mail', 0);
102 $attachmentId = acym_getVar('int', 'id', 0);
103
104 if (!empty($mailId) && $attachmentId >= 0) {
105 $mailClass = new MailClass();
106
107 if (!$mailClass->hasUserAccess($mailId, true)) {
108 acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_DELETE_ATTACHMENT'), [], false);
109 }
110
111 if ($mailClass->deleteOneAttachment($mailId, $attachmentId)) {
112 acym_sendAjaxResponse(acym_translation('ACYM_ATTACHMENT_WELL_DELETED'));
113 }
114 }
115
116 acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_DELETE_ATTACHMENT'), [], false);
117 }
118
119 public function test(): void
120 {
121 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
122
123 $campaignId = acym_getVar('int', 'campaignId', 0);
124 $specificMailId = acym_getVar('int', 'mailId', 0);
125
126 $campaignClass = new CampaignClass();
127 $campaign = $campaignClass->getOneById($campaignId);
128
129 if (empty($campaign)) {
130 acym_sendAjaxResponse(acym_translation('ACYM_CAMPAIGN_NOT_FOUND'), [], false);
131 }
132
133 if (!$campaignClass->hasUserAccess($campaignId)) {
134 acym_sendAjaxResponse(acym_translation('ACYM_ACCESS_DENIED'), [], false);
135 }
136
137 $mailClassAccess = new MailClass();
138 if (!empty($specificMailId) && $mailClassAccess->getMainMailId($specificMailId) !== (int)$campaign->mail_id) {
139 acym_sendAjaxResponse(acym_translation('ACYM_ACCESS_DENIED'), [], false);
140 }
141
142 $mailerHelper = new MailerHelper();
143 $mailerHelper->report = false;
144 if (!empty($specificMailId)) {
145 $mailerHelper->isAbTest = $campaignClass->isAbTestMail($specificMailId);
146 }
147
148 $report = [];
149 $success = true;
150 $testNote = acym_getVar('string', 'test_note', '');
151
152 $userClass = new UserClass();
153 $mailClass = new mailClass();
154 $translatedMails = [];
155 if (acym_isMultilingual()) {
156 $translatedMails = $mailClass->getTranslationsById($campaign->mail_id, true, true);
157 }
158
159 $rawTestEmails = explode(',', acym_getVar('string', 'test_emails'));
160 $testEmails = [];
161 foreach ($rawTestEmails as $email) {
162 $email = trim($email);
163 if (acym_isValidEmail($email) || is_numeric($email)) {
164 $testEmails[] = $email;
165 }
166 }
167
168 if (empty($testEmails)) {
169 acym_sendAjaxResponse(acym_translation('ACYM_NO_VALID_TEST_EMAILS'), [], false);
170
171 return;
172 }
173
174 $historyClass = new HistoryClass();
175
176 foreach ($testEmails as $oneAddress) {
177 if (is_numeric($oneAddress)) {
178 $receiver = $userClass->getOneById($oneAddress);
179 } else {
180 $receiver = $userClass->getOneByEmail($oneAddress, true);
181 }
182
183 $mailId = $specificMailId;
184 if (empty($mailId)) {
185 if (acym_isMultilingual()) {
186 $mailId = empty($receiver) || empty($translatedMails[$receiver->language]) ? $campaign->mail_id : $translatedMails[$receiver->language]->id;
187 } else {
188 $mailId = $campaign->mail_id;
189 }
190 }
191
192 $options = [
193 'isTest' => true,
194 'testNote' => $testNote,
195 ];
196 if ($mailerHelper->sendOne($mailId, $receiver, $options)) {
197 $historyClass->insert($receiver->id, 'test_sent', [], $mailId);
198 } else {
199 $success = false;
200 }
201
202 if (!empty($mailerHelper->reportMessage)) {
203 $report[] = $mailerHelper->reportMessage;
204 }
205 }
206
207 acym_sendAjaxResponse(implode('<br/>', $report), [], $success);
208 }
209
210 public function saveAjax(): void
211 {
212 $result = $this->saveEditEmail(true);
213 if (!empty($result)) {
214 acym_sendAjaxResponse('', ['result' => $result]);
215 } else {
216 acym_sendAjaxResponse(acym_translation('ACYM_ERROR_SAVING'), [], false);
217 }
218 }
219
220 public function saveAsTmplAjax(): void
221 {
222 if (!acym_isAdmin() || (!acym_isAllowed('campaigns') && !acym_isAllowed('mails'))) {
223 die('Access denied');
224 }
225
226 $mailController = new MailsController();
227 $mailId = $mailController->store(true);
228 acym_sendAjaxResponse(!empty($mailId) ? '' : acym_translation('ACYM_ERROR_SAVING'), ['result' => $mailId], !empty($mailId));
229 }
230 }
231