| @@ -1,407 +1,0 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace AcyMailing\Controllers\Campaigns; | |
| 4 | - | |
| 5 | -use AcyMailing\Classes\CampaignClass; | |
| 6 | -use AcyMailing\Classes\FollowupClass; | |
| 7 | -use AcyMailing\Classes\MailArchiveClass; | |
| 8 | -use AcyMailing\Classes\MailClass; | |
| 9 | -use stdClass; | |
| 10 | - | |
| 11 | - | |
| 12 | -trait Actions | |
| 13 | -{ | |
| 14 | - public function duplicate(): void | |
| 15 | - { | |
| 16 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 17 | - | |
| 18 | - //We get the id of campaign checked | |
| 19 | - $campaignsSelected = acym_getVar('array', 'elements_checked', []); | |
| 20 | - | |
| 21 | - $campaignClass = new CampaignClass(); | |
| 22 | - $mailClass = new MailClass(); | |
| 23 | - $campaignId = 0; | |
| 24 | - | |
| 25 | - if (!acym_isAdmin()) { | |
| 26 | - $campaignClass->onlyManageableCampaigns($campaignsSelected); | |
| 27 | - } | |
| 28 | - | |
| 29 | - foreach ($campaignsSelected as $campaignSelected) { | |
| 30 | - //we get the campaign | |
| 31 | - $campaign = $campaignClass->getOneById($campaignSelected); | |
| 32 | - | |
| 33 | - //remove id and set to draft and not sent | |
| 34 | - unset($campaign->id); | |
| 35 | - unset($campaign->sending_date); | |
| 36 | - $campaign->draft = 1; | |
| 37 | - $campaign->sent = 0; | |
| 38 | - $campaign->active = 0; | |
| 39 | - if (!empty($campaign->sending_params['resendTarget'])) { | |
| 40 | - unset($campaign->sending_params['resendTarget']); | |
| 41 | - } | |
| 42 | - | |
| 43 | - //We get the mail to duplicate it | |
| 44 | - $mail = $mailClass->getOneById($campaign->mail_id); | |
| 45 | - $oldMailId = $mail->id; | |
| 46 | - unset($mail->id); | |
| 47 | - $mail->creation_date = acym_date('now', 'Y-m-d H:i:s', false); | |
| 48 | - $mail->name .= '_copy'; | |
| 49 | - $mail->creator_id = acym_currentUserId(); | |
| 50 | - $idNewMail = $mailClass->save($mail); | |
| 51 | - | |
| 52 | - if (isset($campaign->sending_params['abtest']) && !empty($campaign->sending_params['abtest']['B'])) { | |
| 53 | - unset($campaign->sending_params['abtest']['abtest_finished']); | |
| 54 | - unset($campaign->sending_params['abtest']['final']); | |
| 55 | - | |
| 56 | - $mailVersion = $mailClass->getOneById($campaign->sending_params['abtest']['B']); | |
| 57 | - if (!empty($mailVersion)) { | |
| 58 | - unset($mailVersion->id); | |
| 59 | - $mailVersion->creation_date = acym_date('now', 'Y-m-d H:i:s', false); | |
| 60 | - $mailVersion->name .= '_copy'; | |
| 61 | - $mailVersion->parent_id = $idNewMail; | |
| 62 | - $mailVersion->id = $mailClass->save($mailVersion); | |
| 63 | - $campaign->sending_params['abtest']['B'] = $mailVersion->id; | |
| 64 | - } | |
| 65 | - } else { | |
| 66 | - $translations = $mailClass->getTranslationsById($oldMailId, true); | |
| 67 | - foreach ($translations as $oneTranslation) { | |
| 68 | - unset($oneTranslation->id); | |
| 69 | - $oneTranslation->creation_date = acym_date('now', 'Y-m-d H:i:s', false); | |
| 70 | - $oneTranslation->name .= '_copy'; | |
| 71 | - $oneTranslation->parent_id = $idNewMail; | |
| 72 | - $mailClass->save($oneTranslation); | |
| 73 | - } | |
| 74 | - } | |
| 75 | - | |
| 76 | - //we set the new mail id and save campaign | |
| 77 | - $campaign->mail_id = $idNewMail; | |
| 78 | - $campaignId = $campaignClass->save($campaign); | |
| 79 | - | |
| 80 | - //We get the lists | |
| 81 | - $allLists = $campaignClass->getListsByMailId($oldMailId); | |
| 82 | - | |
| 83 | - $campaignClass->manageListsToCampaign($allLists, $idNewMail); | |
| 84 | - } | |
| 85 | - | |
| 86 | - acym_enqueueMessage(acym_translation('ACYM_CAMPAIGN_DUPLICATED_SUCCESS')); | |
| 87 | - | |
| 88 | - if (count($campaignsSelected) == 1 && acym_getVar('string', 'step', '') === 'summary') { | |
| 89 | - acym_setVar('campaignId', $campaignId); | |
| 90 | - $this->editEmail(); | |
| 91 | - } else { | |
| 92 | - $this->listing(); | |
| 93 | - } | |
| 94 | - } | |
| 95 | - | |
| 96 | - public function duplicateFollowup(): void | |
| 97 | - { | |
| 98 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 99 | - | |
| 100 | - //We get the id of follow-ups checked | |
| 101 | - $followupsSelected = acym_getVar('int', 'elements_checked'); | |
| 102 | - | |
| 103 | - $followupClass = new FollowupClass(); | |
| 104 | - $mailClass = new MailClass(); | |
| 105 | - | |
| 106 | - foreach ($followupsSelected as $oneFollowupId) { | |
| 107 | - // Duplicate the follow-up + list associated | |
| 108 | - $followUp = $followupClass->getOneByIdWithMails($oneFollowupId); | |
| 109 | - $followupEmails = $followUp->mails; | |
| 110 | - | |
| 111 | - unset($followUp->id, $followUp->creation_date, $followUp->list_id, $followUp->mails); | |
| 112 | - $followUp->name .= '_copy'; | |
| 113 | - $followUp->active = 0; | |
| 114 | - $followUp->creation_date = acym_date('now', 'Y-m-d H:i:s', false); | |
| 115 | - $newFollowUpId = $followupClass->save($followUp); | |
| 116 | - if (empty($newFollowUpId)) { | |
| 117 | - acym_enqueueMessage(acym_translation('ACYM_FOLLOWUP_DUPLICATE_ERROR'), 'error'); | |
| 118 | - $this->listing(); | |
| 119 | - | |
| 120 | - return; | |
| 121 | - } | |
| 122 | - $followUp->id = $newFollowUpId; | |
| 123 | - | |
| 124 | - // Duplicate emails of the follow-up and attach them to the new followup | |
| 125 | - foreach ($followupEmails as $oneEmail) { | |
| 126 | - $mail = $mailClass->getOneById($oneEmail->id); | |
| 127 | - unset($mail->id, $mail->creation_date, $mail->creator_id, $mail->autosave); | |
| 128 | - $newMailId = $mailClass->save($mail); | |
| 129 | - if (empty($newMailId)) { | |
| 130 | - acym_enqueueMessage(acym_translation('ACYM_FOLLOWUP_DUPLICATE_ERROR'), 'error'); | |
| 131 | - continue; | |
| 132 | - } | |
| 133 | - $followupData = [ | |
| 134 | - 'id' => $newFollowUpId, | |
| 135 | - 'delay' => $oneEmail->delay, | |
| 136 | - 'delay_unit' => $oneEmail->delay_unit, | |
| 137 | - ]; | |
| 138 | - $followupClass->saveDelaySettings($followupData, $newMailId); | |
| 139 | - } | |
| 140 | - } | |
| 141 | - $this->listing(); | |
| 142 | - } | |
| 143 | - | |
| 144 | - public function unpause_campaign(): void | |
| 145 | - { | |
| 146 | - $id = acym_getVar('int', 'campaignId', 0); | |
| 147 | - if (empty($id)) { | |
| 148 | - acym_enqueueMessage(acym_translation('ACYM_CAMPAIGN_NOT_FOUND'), 'error'); | |
| 149 | - $this->listing(); | |
| 150 | - | |
| 151 | - return; | |
| 152 | - } | |
| 153 | - | |
| 154 | - acym_redirect( | |
| 155 | - acym_completeLink('queue', false, true) | |
| 156 | - .'&task=playPauseSending&acym__queue__play_pause__active__new_value=1&acym__queue__play_pause__campaign_id='.$id | |
| 157 | - .'&'.acym_getFormToken() | |
| 158 | - ); | |
| 159 | - } | |
| 160 | - | |
| 161 | - public function stopSending(): void | |
| 162 | - { | |
| 163 | - $this->stopAction('stopSendingCampaignId'); | |
| 164 | - } | |
| 165 | - | |
| 166 | - public function stopScheduled(): void | |
| 167 | - { | |
| 168 | - $this->stopAction('stopScheduledCampaignId'); | |
| 169 | - } | |
| 170 | - | |
| 171 | - private function stopAction(string $action): void | |
| 172 | - { | |
| 173 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 174 | - | |
| 175 | - $campaignID = acym_getVar('int', $action); | |
| 176 | - $campaignClass = new CampaignClass(); | |
| 177 | - | |
| 178 | - if (!empty($campaignID)) { | |
| 179 | - if (!$campaignClass->hasUserAccess($campaignID)) { | |
| 180 | - die('Access denied for campaign send modification'); | |
| 181 | - } | |
| 182 | - | |
| 183 | - $campaign = new stdClass(); | |
| 184 | - $campaign->id = $campaignID; | |
| 185 | - $campaign->active = 0; | |
| 186 | - $campaign->draft = 1; | |
| 187 | - | |
| 188 | - $campaignId = $campaignClass->save($campaign); | |
| 189 | - if (empty($campaignId)) { | |
| 190 | - acym_enqueueMessage(acym_translation('ACYM_CAMPAIGN_CANT_BE_SAVED'), 'error'); | |
| 191 | - } else { | |
| 192 | - acym_enqueueMessage(acym_translation('ACYM_SUCCESSFULLY_SAVED')); | |
| 193 | - } | |
| 194 | - } else { | |
| 195 | - acym_enqueueMessage(acym_translation('ACYM_CAMPAIGN_CANT_BE_SAVED'), 'error'); | |
| 196 | - } | |
| 197 | - | |
| 198 | - $this->listing(); | |
| 199 | - } | |
| 200 | - | |
| 201 | - public function confirmCampaign(): void | |
| 202 | - { | |
| 203 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 204 | - | |
| 205 | - $this->updateOpenAcymailerPopup(); | |
| 206 | - $campaignId = acym_getVar('int', 'campaignId'); | |
| 207 | - $campaignSendingDate = acym_getVar('string', 'sending_date'); | |
| 208 | - $resendTarget = acym_getVar('cmd', 'resend_target', ''); | |
| 209 | - $campaignClass = new CampaignClass(); | |
| 210 | - | |
| 211 | - if (!$campaignClass->hasUserAccess($campaignId)) { | |
| 212 | - die('Access denied for campaign confirmation'); | |
| 213 | - } | |
| 214 | - | |
| 215 | - $campaign = new stdClass(); | |
| 216 | - $campaign->id = $campaignId; | |
| 217 | - $campaign->draft = 0; | |
| 218 | - $campaign->active = 1; | |
| 219 | - $campaign->sent = 0; | |
| 220 | - | |
| 221 | - $currentCampaign = $campaignClass->getOneById($campaignId); | |
| 222 | - if (!empty($resendTarget)) { | |
| 223 | - $currentCampaign->sending_params['resendTarget'] = $resendTarget; | |
| 224 | - $campaign->sending_params = $currentCampaign->sending_params; | |
| 225 | - acym_trigger('onAcymResendCampaign', [$currentCampaign->mail_id]); | |
| 226 | - } | |
| 227 | - | |
| 228 | - $resultSave = $campaignClass->save($campaign); | |
| 229 | - | |
| 230 | - if (!empty($resultSave)) { | |
| 231 | - acym_enqueueMessage(acym_translationSprintf('ACYM_CONFIRMED_CAMPAIGN', acym_date($campaignSendingDate, 'j F Y H:i'))); | |
| 232 | - } else { | |
| 233 | - acym_enqueueMessage(acym_translation('ACYM_CANT_CONFIRM_CAMPAIGN').' : '.end($campaignClass->errors), 'error'); | |
| 234 | - } | |
| 235 | - | |
| 236 | - $this->listing(); | |
| 237 | - } | |
| 238 | - | |
| 239 | - public function activeAutoCampaign(): void | |
| 240 | - { | |
| 241 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 242 | - | |
| 243 | - $this->updateOpenAcymailerPopup(); | |
| 244 | - $campaignId = acym_getVar('int', 'campaignId'); | |
| 245 | - $campaignClass = new CampaignClass(); | |
| 246 | - | |
| 247 | - $campaign = new stdClass(); | |
| 248 | - $campaign->id = $campaignId; | |
| 249 | - $campaign->draft = 0; | |
| 250 | - $campaign->active = 1; | |
| 251 | - $campaign->sent = 0; | |
| 252 | - | |
| 253 | - $resultSave = $campaignClass->save($campaign); | |
| 254 | - | |
| 255 | - if (!empty($resultSave)) { | |
| 256 | - acym_enqueueMessage(acym_translation('ACYM_CAMPAIGN_IS_ACTIVE')); | |
| 257 | - } else { | |
| 258 | - acym_enqueueMessage(acym_translation('ACYM_ERROR_SAVING'), 'error'); | |
| 259 | - } | |
| 260 | - | |
| 261 | - $this->listing(); | |
| 262 | - } | |
| 263 | - | |
| 264 | - public function saveAsDraftCampaign(): void | |
| 265 | - { | |
| 266 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 267 | - | |
| 268 | - $campaignId = acym_getVar('int', 'campaignId'); | |
| 269 | - $campaignClass = new CampaignClass(); | |
| 270 | - | |
| 271 | - if (!$campaignClass->hasUserAccess($campaignId)) { | |
| 272 | - die('Access denied for campaign draft save'); | |
| 273 | - } | |
| 274 | - | |
| 275 | - $campaign = new stdClass(); | |
| 276 | - $campaign->id = $campaignId; | |
| 277 | - $campaign->draft = 1; | |
| 278 | - $campaign->active = 0; | |
| 279 | - | |
| 280 | - $resultSave = $campaignClass->save($campaign); | |
| 281 | - | |
| 282 | - if (!empty($resultSave)) { | |
| 283 | - acym_enqueueMessage(acym_translation('ACYM_CAMPAIGN_SUCCESSFULLY_SAVE_AS_DRAFT')); | |
| 284 | - } else { | |
| 285 | - acym_enqueueMessage(acym_translation('ACYM_CAMPAIGN_CANT_BE_SAVED').' : '.end($campaignClass->errors), 'error'); | |
| 286 | - } | |
| 287 | - | |
| 288 | - $this->listing(); | |
| 289 | - } | |
| 290 | - | |
| 291 | - public function toggleActivateColumnCampaign(): void | |
| 292 | - { | |
| 293 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 294 | - | |
| 295 | - $campaignId = acym_getVar('int', 'campaignId'); | |
| 296 | - $campaignClass = new CampaignClass(); | |
| 297 | - | |
| 298 | - $campaign = $campaignClass->getOneById($campaignId); | |
| 299 | - if (empty($campaign)) { | |
| 300 | - acym_enqueueMessage(acym_translation('ACYM_CAMPAIGN_CANT_BE_SAVED').' : '.end($campaignClass->errors), 'error'); | |
| 301 | - $this->listing(); | |
| 302 | - | |
| 303 | - return; | |
| 304 | - } | |
| 305 | - | |
| 306 | - $campaign->active = empty($campaign->active) ? 1 : 0; | |
| 307 | - | |
| 308 | - // Remove the next trigger when disabling the automatic campaign. It will be calculated again properly if reactivated | |
| 309 | - if ($campaign->active === 0 && $campaign->sending_type === CampaignClass::SENDING_TYPE_AUTO) { | |
| 310 | - $campaign->next_trigger = null; | |
| 311 | - } | |
| 312 | - | |
| 313 | - $resultSave = $campaignClass->save($campaign); | |
| 314 | - | |
| 315 | - if (!empty($resultSave)) { | |
| 316 | - acym_enqueueMessage(acym_translation('ACYM_CAMPAIGN_SUCCESSFULLY_SAVE_AS_DRAFT')); | |
| 317 | - } else { | |
| 318 | - acym_enqueueMessage(acym_translation('ACYM_CAMPAIGN_CANT_BE_SAVED').' : '.end($campaignClass->errors), 'error'); | |
| 319 | - } | |
| 320 | - | |
| 321 | - $this->listing(); | |
| 322 | - } | |
| 323 | - | |
| 324 | - public function addQueue(): void | |
| 325 | - { | |
| 326 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 327 | - $this->updateOpenAcymailerPopup(); | |
| 328 | - | |
| 329 | - $campaignClass = new CampaignClass(); | |
| 330 | - $campaignID = acym_getVar('int', 'campaignId', 0); | |
| 331 | - | |
| 332 | - if (empty($campaignID)) { | |
| 333 | - acym_enqueueMessage(acym_translation('ACYM_CAMPAIGN_NOT_FOUND'), 'error'); | |
| 334 | - } else { | |
| 335 | - if (!$campaignClass->hasUserAccess($campaignID)) { | |
| 336 | - die('Access denied to add campaign to the queue'); | |
| 337 | - } | |
| 338 | - $campaign = $campaignClass->getOneByIdWithMail($campaignID); | |
| 339 | - | |
| 340 | - $resendTarget = acym_getVar('cmd', 'resend_target', ''); | |
| 341 | - if (!empty($resendTarget)) { | |
| 342 | - $currentCampaign = $campaignClass->getOneById($campaignID); | |
| 343 | - $currentCampaign->sending_params['resendTarget'] = $resendTarget; | |
| 344 | - $campaignClass->save($currentCampaign); | |
| 345 | - acym_trigger('onAcymResendCampaign', [$currentCampaign->mail_id]); | |
| 346 | - } | |
| 347 | - | |
| 348 | - $status = $campaignClass->send($campaignID); | |
| 349 | - | |
| 350 | - if ($status) { | |
| 351 | - acym_enqueueMessage(acym_translationSprintf('ACYM_CAMPAIGN_ADDED_TO_QUEUE', $campaign->name, 'info')); | |
| 352 | - } else { | |
| 353 | - if (empty($campaignClass->errors)) { | |
| 354 | - $notification = [ | |
| 355 | - 'name' => 'queue_campaign_error', | |
| 356 | - 'removable' => 1, | |
| 357 | - ]; | |
| 358 | - acym_enqueueMessage(acym_translationSprintf('ACYM_ERROR_QUEUE_CAMPAIGN', $campaign->name), 'error', true, [$notification]); | |
| 359 | - } else { | |
| 360 | - acym_enqueueMessage($campaignClass->errors, 'error'); | |
| 361 | - } | |
| 362 | - } | |
| 363 | - } | |
| 364 | - | |
| 365 | - $this->redirectAfterQueued(); | |
| 366 | - } | |
| 367 | - | |
| 368 | - private function redirectAfterQueued(): void | |
| 369 | - { | |
| 370 | - if (acym_isAdmin() && (!acym_level(ACYM_ESSENTIAL) || $this->config->get('cron_last', 0) < (time() - 43200))) { | |
| 371 | - acym_redirect(acym_completeLink('queue&task=campaigns', false, true)); | |
| 372 | - } else { | |
| 373 | - $this->listing(); | |
| 374 | - } | |
| 375 | - } | |
| 376 | - | |
| 377 | - private function updateOpenAcymailerPopup(): void | |
| 378 | - { | |
| 379 | - if (acym_isAdmin() && $this->config->get('mailer_method') === 'acymailer' && intval($this->config->get('acymailer_popup', 0)) === 0) { | |
| 380 | - $this->config->saveConfig(['acymailer_popup' => '1']); | |
| 381 | - } | |
| 382 | - } | |
| 383 | - | |
| 384 | - public function updateArchive(): void | |
| 385 | - { | |
| 386 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 387 | - | |
| 388 | - $campaignId = acym_getVar('int', 'campaignId', 0); | |
| 389 | - if (empty($campaignId)) { | |
| 390 | - acym_sendAjaxResponse('', [], false); | |
| 391 | - } | |
| 392 | - | |
| 393 | - $campaignClass = new CampaignClass(); | |
| 394 | - $campaign = $campaignClass->getOneById($campaignId); | |
| 395 | - if (empty($campaign)) { | |
| 396 | - acym_sendAjaxResponse(acym_translation('ACYM_CAMPAIGN_NOT_FOUND'), [], false); | |
| 397 | - } | |
| 398 | - | |
| 399 | - $mailArchiveClass = new MailArchiveClass(); | |
| 400 | - $archive = $mailArchiveClass->getOneByMailId($campaign->mail_id); | |
| 401 | - if (!empty($archive)) { | |
| 402 | - $mailArchiveClass->delete([$archive->id]); | |
| 403 | - } | |
| 404 | - | |
| 405 | - acym_sendAjaxResponse(); | |
| 406 | - } | |
| 407 | -} | |