PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.1.4
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.1.4
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 / Helpers / CronHelper.php

CronHelper.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 10.1.4, at back/Helpers/CronHelper.php

702 lines 24.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AcyMailing\Helpers;
4
5 use AcyMailing\Classes\AutomationClass;
6 use AcyMailing\Classes\CampaignClass;
7 use AcyMailing\Classes\FollowupClass;
8 use AcyMailing\Classes\MailArchiveClass;
9 use AcyMailing\Classes\MailboxClass;
10 use AcyMailing\Classes\MailClass;
11 use AcyMailing\Classes\QueueClass;
12 use AcyMailing\Classes\UserClass;
13 use AcyMailing\Classes\UserStatClass;
14 use AcyMailing\Core\AcymObject;
15
16 class CronHelper extends AcymObject
17 {
18 const STEP_SCHEDULE = 'schedule';
19 const STEP_CLEAN_QUEUE = 'cleanqueue';
20 const STEP_SEND = 'send';
21 const STEP_BOUNCE = 'bounce';
22 const STEP_AUTOMATION = 'automation';
23 const STEP_CAMPAIGN = 'campaign';
24 const STEP_SPECIFIC = 'specific';
25 const STEP_FOLLOWUP = 'followup';
26 const STEP_DELETE_HISTORY = 'delete_history';
27 const STEP_CLEAN_DATA_EXTERNAL_SENDING_METHOD = 'clean_data_external_sending_method';
28 const STEP_CLEAN_EXPORT_CHANGES = 'clean_export_changes';
29 const STEP_MAILBOX_ACTION = 'mailbox_action';
30 const STEP_ABTEST = 'abtest';
31 const STEP_SCENARIO = 'scenario';
32
33 const ALL_STEPS = [
34 self::STEP_SCHEDULE,
35 self::STEP_CLEAN_QUEUE,
36 self::STEP_SEND,
37 self::STEP_BOUNCE,
38 self::STEP_AUTOMATION,
39 self::STEP_CAMPAIGN,
40 self::STEP_SPECIFIC,
41 self::STEP_FOLLOWUP,
42 self::STEP_DELETE_HISTORY,
43 self::STEP_CLEAN_DATA_EXTERNAL_SENDING_METHOD,
44 self::STEP_CLEAN_EXPORT_CHANGES,
45 self::STEP_MAILBOX_ACTION,
46 self::STEP_ABTEST,
47 self::STEP_SCENARIO,
48 ];
49
50 private array $messages = [];
51 private array $detailMessages = [];
52 private array $emailTypes = [];
53 private array $skip = [];
54
55 private int $cronTimeLimit = 0;
56 private bool $cronTimeLimitReached = false;
57 private int $startQueue;
58
59 private string $mainMessage = '';
60
61 private bool $processed = false;
62
63 private bool $errorDetected = false;
64
65 private bool $externalSendingActivated = false;
66 private bool $externalSendingRepeat;
67 private bool $externalSendingNotFinished = false;
68
69 public function __construct()
70 {
71 parent::__construct();
72 $this->startQueue = acym_getVar('int', 'startqueue', 0);
73
74 acym_trigger('onAcymProcessQueueExternalSendingCampaign', [&$this->externalSendingActivated]);
75
76 $this->externalSendingRepeat = !empty(acym_getVar('int', 'external_sending_repeat', 0));
77 if (!empty($this->startQueue) || !empty($this->externalSendingRepeat)) {
78 $this->skip = array_diff(self::ALL_STEPS, [self::STEP_SEND]);
79 }
80 }
81
82 public function addSkipFromString(string $skipVar): void
83 {
84 if (empty($skipVar)) {
85 return;
86 }
87
88 $skipVar = explode(',', $skipVar);
89
90 if (empty($skipVar)) {
91 return;
92 }
93
94 $this->skip = array_unique(array_merge($this->skip, $skipVar));
95 }
96
97 public function cron(): void
98 {
99
100 $this->handleUnlinkLicenseCalls();
101 $this->triggeredMessage();
102 if (!$this->checkCronFrequency()) {
103 return;
104 }
105
106 $this->freeUpCall();
107 $this->queueScheduledCampaigns();
108 $this->cleanQueue();
109 $this->sendQueuedEmails();
110 $this->checkTimeRemaining();
111 $this->handleBounceMessages();
112 $this->checkTimeRemaining();
113 $this->handleAutomations();
114 $this->checkTimeRemaining();
115 $this->handleAutomaticCampaigns();
116 $this->checkTimeRemaining();
117 $this->handleSpecificEmails();
118 $this->checkTimeRemaining();
119 $this->handleFollowups();
120 $this->checkTimeRemaining();
121 $this->handleMailboxActions();
122 $this->checkTimeRemaining();
123 $this->cleanData();
124 $this->handleABTestCampaigns();
125 $this->handleScenario();
126 $this->continueCronCall();
127 $this->handleCronReport();
128 }
129
130 public function saveReport(array $messages = [], array $detailMessages = []): void
131 {
132 $saveReport = $this->config->get('cron_savereport');
133 $reportPath = $this->config->get('cron_savepath');
134
135 if (empty($saveReport) || empty($reportPath)) {
136 return;
137 }
138
139 if (!(($saveReport == 2 && $this->processed) || $saveReport == 1 || ($saveReport == 3 && $this->errorDetected))) {
140 return;
141 }
142
143 if (!empty($messages)) {
144 $this->messages = $messages;
145 }
146
147 if (!empty($detailMessages)) {
148 $this->detailMessages = $detailMessages;
149 }
150
151 $reportPath = str_replace(['{year}', '{month}'], [date('Y'), date('m')], $reportPath);
152 $reportPath = acym_cleanPath(ACYM_ROOT.trim(html_entity_decode($reportPath)));
153 acym_createDir(dirname($reportPath), true, true);
154
155 $lr = "\r\n";
156 ob_start();
157 file_put_contents(
158 $reportPath,
159 $lr.$lr.'******************** '.acym_getDate(time()).' UTC ********************'.$lr.implode($lr, $this->messages),
160 FILE_APPEND
161 );
162
163 if ($saveReport == 2 && !empty($this->detailMessages)) {
164 file_put_contents(
165 $reportPath,
166 $lr.'---- Details ----'.$lr.implode($lr, $this->detailMessages),
167 FILE_APPEND
168 );
169 }
170 $potentialWarnings = ob_get_clean();
171
172 if (!empty($potentialWarnings)) {
173 $this->messages[] = $potentialWarnings;
174 }
175 }
176
177 public function setEmailTypes(array $emailTypes): void
178 {
179 $this->emailTypes = $emailTypes;
180 }
181
182 public function addMessage(string $message): void
183 {
184 $this->messages[] = $message;
185 }
186
187 public function handleCronReport(): void
188 {
189 $sendReport = $this->config->get('cron_sendreport');
190
191 if (($sendReport == 2 && $this->processed) || $sendReport == 1 || ($sendReport == 3 && $this->errorDetected)) {
192 $mailerHelper = new MailerHelper();
193 $mailerHelper->report = false;
194 $mailerHelper->autoAddUser = true;
195 $mailerHelper->addParam('report', implode('<br />', $this->messages));
196 $mailerHelper->addParam('mainreport', $this->mainMessage);
197 $mailerHelper->addParam('detailreport', implode('<br />', $this->detailMessages));
198
199 $receiverString = $this->config->get('cron_sendto');
200 $receivers = [];
201 if (substr_count($receiverString, '@') > 1) {
202 $receivers = explode(' ', trim(preg_replace('# +#', ' ', str_replace([';', ','], ' ', $receiverString))));
203 } else {
204 $receivers[] = trim($receiverString);
205 }
206
207 if (!empty($receivers)) {
208 $mailClass = new MailClass();
209 foreach ($receivers as $oneReceiver) {
210 if (empty($oneReceiver)) {
211 continue;
212 }
213
214 try {
215 $reportEmail = $mailClass->getOneByName('acy_report');
216 if (!empty($reportEmail)) {
217 $mailerHelper->sendOne($reportEmail->id, $oneReceiver);
218 }
219 } catch (\Exception $e) {
220 acym_logError('Error while sending the cron report to '.$oneReceiver.' : '.$e->getMessage());
221 }
222 }
223 }
224 }
225
226 $this->saveReport();
227
228 $newConfig = new \stdClass();
229 $newConfig->cron_report = implode("\n", $this->messages);
230
231 if (strlen($newConfig->cron_report) > 800) {
232 $newConfig->cron_report = substr($newConfig->cron_report, 0, 795).'...';
233 }
234
235 $this->config->save($newConfig);
236 }
237
238 private function handleUnlinkLicenseCalls(): void
239 {
240 if (acym_getVar('int', 'unlink', 0) !== 1) {
241 return;
242 }
243
244 $callLicenseKey = acym_getVar('string', 'licenseKey');
245 $configLicenseKey = $this->config->get('license_key');
246
247 if (!empty($configLicenseKey) && $configLicenseKey === $callLicenseKey) {
248 $this->config->save(['license_key' => '', 'active_cron' => 0]);
249 }
250
251 exit;
252 }
253
254 private function triggeredMessage(): void
255 {
256 $firstMessage = acym_translationSprintf('ACYM_CRON_TRIGGERED', acym_date('now', 'd F Y H:i'));
257 $this->messages[] = $firstMessage;
258 acym_display($firstMessage, 'info');
259 }
260
261 private function checkCronFrequency(): bool
262 {
263 if (!empty($this->startQueue)) {
264 return true;
265 }
266
267 $time = time();
268 $nextCronTime = $this->config->get('cron_next', 0);
269 $cronFrequency = $this->config->get('cron_frequency', 900);
270
271 if ($nextCronTime > $time) {
272 if ($nextCronTime > ($time + $cronFrequency)) {
273 $newConfig = new \stdClass();
274 $newConfig->cron_next = $time + $cronFrequency;
275 $this->config->save($newConfig);
276 }
277
278 $notTimeMessage = acym_translationSprintf('ACYM_CRON_NEXT', acym_date($this->config->get('cron_next'), 'd F Y H:i'));
279 $this->messages[] = $notTimeMessage;
280 acym_display($notTimeMessage, 'info');
281
282 return false;
283 }
284
285 $newConfig = new \stdClass();
286 $newConfig->cron_last = $time;
287 $newConfig->cron_fromip = acym_getIP();
288 $newConfig->cron_next = $nextCronTime + $cronFrequency;
289
290 if ($newConfig->cron_next <= $time || $newConfig->cron_next > $time + $cronFrequency) {
291 $newConfig->cron_next = $time + $cronFrequency;
292 }
293
294 $this->config->save($newConfig);
295
296 return true;
297 }
298
299 private function freeUpCall(): void
300 {
301 if (function_exists('fastcgi_finish_request')) {
302 fastcgi_finish_request();
303 }
304 }
305
306 private function queueScheduledCampaigns(): void
307 {
308 if (in_array(self::STEP_SCHEDULE, $this->skip)) {
309 return;
310 }
311
312 $queueClass = new QueueClass();
313 $nbScheduled = $queueClass->scheduleReady();
314 if ($nbScheduled) {
315 $this->messages[] = acym_translationSprintf('ACYM_NB_SCHEDULED', $nbScheduled);
316 $this->detailMessages = array_merge($this->detailMessages, $queueClass->messages);
317 $this->processed = true;
318 }
319 }
320
321 private function cleanQueue(): void
322 {
323 if (in_array(self::STEP_CLEAN_QUEUE, $this->skip)) {
324 return;
325 }
326
327 $queueClass = new QueueClass();
328 $deletedNb = $queueClass->cleanQueue();
329
330 if (!empty($deletedNb)) {
331 $this->messages[] = acym_translationSprintf('ACYM_EMAILS_REMOVED_QUEUE_CLEAN', $deletedNb);
332 $this->processed = true;
333 }
334 }
335
336 private function sendQueuedEmails(): void
337 {
338 if (in_array(self::STEP_SEND, $this->skip) || $this->config->get('queue_type') === 'manual') {
339 return;
340 }
341
342 $fromHour = $this->config->get('queue_send_from_hour', '00');
343 $fromMinute = $this->config->get('queue_send_from_minute', '00');
344 $toHour = $this->config->get('queue_send_to_hour', '23');
345 $toMinute = $this->config->get('queue_send_to_minute', '59');
346 $time = time();
347
348 if ($fromHour != '00' || $fromMinute != '00' || $toHour != '23' || $toMinute != '59') {
349 $dayBasedOnCMSTimezone = acym_date('now', 'Y-m-d');
350 $fromBasedOnCMSTimezoneAtSpecifiedHour = acym_getTimeFromCMSDate($dayBasedOnCMSTimezone.' '.$fromHour.':'.$fromMinute);
351 $toBasedOnCMSTimezoneAtSpecifiedHour = acym_getTimeFromCMSDate($dayBasedOnCMSTimezone.' '.$toHour.':'.$toMinute);
352 if ($fromBasedOnCMSTimezoneAtSpecifiedHour > $toBasedOnCMSTimezoneAtSpecifiedHour) {
353 if ($time > $fromBasedOnCMSTimezoneAtSpecifiedHour) {
354 $toBasedOnCMSTimezoneAtSpecifiedHour = acym_getTimeFromCMSDate(acym_date('tomorrow', 'Y-m-d').' '.$toHour.':'.$toMinute);
355 } elseif ($time < $toBasedOnCMSTimezoneAtSpecifiedHour) {
356 $fromBasedOnCMSTimezoneAtSpecifiedHour = acym_getTimeFromCMSDate(acym_date('yesterday', 'Y-m-d').' '.$fromHour.':'.$fromMinute);
357 }
358 }
359
360 if ($time < $fromBasedOnCMSTimezoneAtSpecifiedHour || $time > $toBasedOnCMSTimezoneAtSpecifiedHour) {
361 return;
362 }
363 }
364
365 $dayOfWeek = acym_date('now', 'N');
366 if ($this->config->get('queue_stop_weekend', 0) && $dayOfWeek >= 6) {
367 return;
368 }
369
370 $this->handleMultiCron();
371 $queueHelper = new QueueHelper();
372 $queueHelper->send_limit = (int)$this->config->get('queue_nbmail_auto');
373 $queueHelper->report = false;
374 $queueHelper->emailTypes = $this->emailTypes;
375 $queueHelper->process();
376
377 if (!empty($queueHelper->messages)) {
378 $this->detailMessages = array_merge($this->detailMessages, $queueHelper->messages);
379 }
380
381 if (!empty($queueHelper->nbprocess)) {
382 $this->processed = true;
383
384 if (!$queueHelper->finish && $this->externalSendingActivated) {
385 $this->externalSendingNotFinished = true;
386 }
387 }
388
389 $this->mainMessage = acym_translationSprintf('ACYM_CRON_PROCESS', $queueHelper->nbprocess, $queueHelper->successSend, $queueHelper->errorSend);
390 $this->messages[] = $this->mainMessage;
391
392 if (!empty($queueHelper->errorSend)) {
393 $this->errorDetected = true;
394 }
395
396 $this->cronTimeLimit = $queueHelper->stoptime;
397 }
398
399 private function checkTimeRemaining(): void
400 {
401 $time = time();
402 if (empty($this->cronTimeLimit) || $time < $this->cronTimeLimit) {
403 return;
404 }
405
406 $this->messages[] = acym_translation('ACYM_CRON_TIME_LIMIT_REACHED');
407
408 $this->cronTimeLimitReached = true;
409 }
410
411 private function handleMultiCron(): void
412 {
413 if (!empty($this->startQueue) || !acym_level(ACYM_ENTERPRISE)) {
414 return;
415 }
416
417 $emailsBatches = $this->config->get('queue_batch_auto', 1);
418 $emailsBatches = intval($emailsBatches);
419 $emailsPerBatches = $this->config->get('queue_nbmail_auto', 70);
420 if ($emailsBatches < 2 || empty($emailsPerBatches)) {
421 return;
422 }
423
424 $cronKey = '';
425 if (!empty($this->config->get('cron_security', 0)) && !empty($this->config->get('cron_key'))) {
426 $cronKey = '&cronKey='.$this->config->get('cron_key');
427 }
428
429 $urls = [];
430 for ($i = 1 ; $i <= $emailsBatches - 1 ; $i++) {
431 $urls[] = acym_frontendLink('cron&task=cron&startqueue='.($emailsPerBatches * $i).'&t='.time().$cronKey);
432 }
433
434 acym_asyncCurlCall($urls);
435 }
436
437 private function handleBounceMessages(): void
438 {
439 $time = time();
440 $autoBounceHandlingActive = (int)$this->config->get('auto_bounce', 0) === 1;
441 $autoBounceHandlingNextTime = (int)$this->config->get('auto_bounce_next', 0);
442 $autoBounceHandlingFrequency = (int)$this->config->get('auto_bounce_frequency', 0);
443 $isEnterprise = acym_level(ACYM_ENTERPRISE);
444
445 if (in_array(self::STEP_BOUNCE, $this->skip) || $this->cronTimeLimitReached || !$isEnterprise || !$autoBounceHandlingActive || $time < $autoBounceHandlingNextTime) {
446 return;
447 }
448
449 $newConfig = new \stdClass();
450 $newConfig->auto_bounce_last = $time;
451 $newConfig->auto_bounce_next = $time + $autoBounceHandlingFrequency;
452 $this->config->save($newConfig);
453
454 $bounceHelper = new BounceHelper();
455 $bounceHelper->report = false;
456 $bounceHelper->stoptime = $this->cronTimeLimit;
457
458 $newConfig = new \stdClass();
459 if ($bounceHelper->init() && $bounceHelper->connect()) {
460 $nbMessages = $bounceHelper->getNBMessages();
461 $nbMessagesReport = acym_translationSprintf('ACYM_NB_MAIL_MAILBOX', $nbMessages);
462 $this->messages[] = $nbMessagesReport;
463 $newConfig->auto_bounce_report = $nbMessagesReport;
464 $this->detailMessages[] = $nbMessagesReport;
465 if (!empty($nbMessages)) {
466 $bounceHelper->handleMessages();
467 $this->processed = true;
468 }
469 $this->detailMessages = array_merge($this->detailMessages, $bounceHelper->messages);
470 } else {
471 $bounceErrors = $bounceHelper->getErrors();
472 $newConfig->auto_bounce_report = implode('<br />', $bounceErrors);
473 if (!empty($bounceErrors[0])) {
474 $bounceErrors[0] = acym_translation('ACYM_BOUNCE_HANDLING').' : '.$bounceErrors[0];
475 }
476 $this->messages = array_merge($this->messages, $bounceErrors);
477 $this->processed = true;
478 $this->errorDetected = true;
479 }
480
481 $this->config->save($newConfig);
482 }
483
484 private function handleAutomations(): void
485 {
486 if (in_array(self::STEP_AUTOMATION, $this->skip) || $this->cronTimeLimitReached || !acym_level(ACYM_ENTERPRISE)) {
487 return;
488 }
489
490 $automationClass = new AutomationClass();
491 $automationClass->trigger('classic');
492
493 $userStatusCheckTriggers = [];
494 acym_trigger('onAcymDefineUserStatusCheckTriggers', [&$userStatusCheckTriggers]);
495 $automationClass->trigger($userStatusCheckTriggers);
496
497 if (!empty($automationClass->report)) {
498 if ($automationClass->didAnAction) {
499 $this->processed = true;
500 }
501 $this->messages = array_merge($this->messages, $automationClass->report);
502 }
503 }
504
505 private function handleAutomaticCampaigns(): void
506 {
507 if (in_array(self::STEP_CAMPAIGN, $this->skip) || $this->cronTimeLimitReached || !acym_level(ACYM_ENTERPRISE)) {
508 return;
509 }
510
511 $campaignClass = new CampaignClass();
512 $campaignClass->triggerAutoCampaign();
513
514 if (!empty($campaignClass->messages)) {
515 $this->messages = array_merge($this->messages, $campaignClass->messages);
516 $this->processed = true;
517 }
518 }
519
520 private function handleSpecificEmails(): void
521 {
522 if (in_array(self::STEP_SPECIFIC, $this->skip) || $this->cronTimeLimitReached) {
523 return;
524 }
525
526 $campaignClass = new CampaignClass();
527 $specialTypes = [];
528
529 acym_trigger('getCampaignTypes', [&$specialTypes]);
530
531 $specialMail = $campaignClass->getCampaignsByTypes($specialTypes, true);
532 $time = time();
533
534 acym_trigger('filterSpecificMailsToSend', [&$specialMail, $time]);
535
536 foreach ($specialMail as $onespecialMail) {
537 $campaignClass->send($onespecialMail->id);
538 }
539 }
540
541 private function handleFollowups(): void
542 {
543 if (in_array(self::STEP_FOLLOWUP, $this->skip) || $this->cronTimeLimitReached) {
544 return;
545 }
546
547 $followupClass = new FollowupClass();
548 $followups = $followupClass->getFollowupDailyBases();
549
550 $dailyHour = $this->config->get('daily_hour', '12');
551 $dailyMinute = $this->config->get('daily_minute', '00');
552 $dayBasedOnCMSTimezone = acym_date('now', 'Y-m-d');
553 $dayBasedOnCMSTimezoneAtSpecifiedHour = acym_getTimeFromCMSDate($dayBasedOnCMSTimezone.' '.$dailyHour.':'.$dailyMinute);
554 $time = time();
555
556 foreach ($followups as $followup) {
557 $lastTrigger = empty($followup->last_trigger) ? '' : acym_date($followup->last_trigger, 'Y-m-d');
558 if ($time >= $dayBasedOnCMSTimezoneAtSpecifiedHour && $lastTrigger != $dayBasedOnCMSTimezone) {
559 if (!empty($followup->condition)) {
560 $followup->condition = json_decode($followup->condition, true);
561 }
562
563 acym_trigger('onAcymFollowupDailyBasesNeedToBeTriggered', [$followup]);
564 }
565 }
566 }
567
568 private function handleMailboxActions(): void
569 {
570 if (in_array(self::STEP_MAILBOX_ACTION, $this->skip) || $this->cronTimeLimitReached || !acym_level(ACYM_ENTERPRISE)) {
571 return;
572 }
573
574 $mailboxActionClass = new MailboxClass();
575 $mailboxes = $mailboxActionClass->getAllActiveReadyWithActions();
576
577 if (empty($mailboxes)) {
578 return;
579 }
580
581 $mailboxHelper = new MailboxHelper();
582 $mailboxHelper->report = false;
583 foreach ($mailboxes as $oneMailboxAction) {
584 $this->processed = true;
585
586 if (!$mailboxHelper->isConnectionValid($oneMailboxAction, false)) {
587 $this->messages[] = acym_translationSprintf('ACYM_CONNECTION_FAILED_MAILBOX_X', $oneMailboxAction->id.'-'.$oneMailboxAction->name);
588 $this->errorDetected = true;
589 continue;
590 }
591
592 $nbMessages = $mailboxHelper->getNBMessages();
593 if (!$nbMessages) {
594 $this->messages[] = acym_translationSprintf('ACYM_NO_MESSAGE_IN_MAILBOX_X', $oneMailboxAction->id.'-'.$oneMailboxAction->name);
595 $mailboxHelper->close();
596 continue;
597 }
598
599 $mailboxHelper->handleAction();
600 $mailboxHelper->close();
601
602
603 $oneMailboxAction->nextdate = time() + $oneMailboxAction->frequency;
604 unset($oneMailboxAction->conditions);
605 unset($oneMailboxAction->actions);
606 $mailboxActionClass->save($oneMailboxAction);
607 }
608 }
609
610 private function cleanData(): void
611 {
612 if (!in_array(self::STEP_DELETE_HISTORY, $this->skip) && $this->isDailyCron()) {
613 $userStatClass = new UserStatClass();
614 $userDetailedStatsDeleted = $userStatClass->deleteDetailedStatsPeriod();
615 if (!empty($userDetailedStatsDeleted['message'])) {
616 $this->messages[] = $userDetailedStatsDeleted['message'];
617 $this->processed = true;
618 }
619
620 $userClass = new UserClass();
621 $userHistoryDeleted = $userClass->deleteHistoryPeriod();
622 if (!empty($userHistoryDeleted['message'])) {
623 $this->messages[] = $userHistoryDeleted['message'];
624 $this->processed = true;
625 }
626
627 $mailArchiveClass = new MailArchiveClass();
628 $archiveDeleted = $mailArchiveClass->deleteArchivePeriod();
629 if (!empty($archiveDeleted['message'])) {
630 $this->messages[] = $archiveDeleted['message'];
631 $this->processed = true;
632 }
633 }
634
635 if (!in_array(self::STEP_CLEAN_DATA_EXTERNAL_SENDING_METHOD, $this->skip) && $this->isDailyCron()) {
636 acym_trigger('onAcymCleanDataExternalSendingMethod');
637 }
638
639 if (!in_array(self::STEP_CLEAN_EXPORT_CHANGES, $this->skip) && $this->isDailyCron()) {
640 $exportHelper = new ExportHelper();
641 $exportHelper->cleanExportChangesFile();
642 }
643
644 if ($this->isDailyCron()) {
645 $time = time();
646 $this->config->save(['cron_last_daily' => $time]);
647 }
648 }
649
650 private function isDailyCron(): bool
651 {
652 $dailyHour = $this->config->get('daily_hour', '12');
653 $dailyMinute = $this->config->get('daily_minute', '00');
654 $dayBasedOnCMSTimezone = acym_date('now', 'Y-m-d');
655 $dayBasedOnCMSTimezoneAtSpecifiedHour = acym_getTimeFromCMSDate($dayBasedOnCMSTimezone.' '.$dailyHour.':'.$dailyMinute);
656
657 $time = time();
658
659 $lastCronDayBasedOnCMSTimezone = acym_date($this->config->get('cron_last_daily', $time - 86400), 'Y-m-d');
660
661 return $time >= $dayBasedOnCMSTimezoneAtSpecifiedHour && $lastCronDayBasedOnCMSTimezone != $dayBasedOnCMSTimezone;
662 }
663
664 private function handleABTestCampaigns(): void
665 {
666 if (in_array(self::STEP_ABTEST, $this->skip) || $this->cronTimeLimitReached) {
667 return;
668 }
669
670 $campaignClass = new CampaignClass();
671 $abTestCampaigns = $campaignClass->getAllAbTestCampaignsToFinishSending();
672 foreach ($abTestCampaigns as $campaign) {
673 if ($campaignClass->finishAbTestCampaign($campaign)) {
674 $this->messages[] = acym_translationSprintf('ACYM_ABTEST_CAMPAIGN_X_FINAL_VERSION_SENT', $campaign->id);
675 $this->processed = true;
676 }
677 }
678 }
679
680 private function handleScenario(): void
681 {
682 if (in_array(self::STEP_SCENARIO, $this->skip) || $this->cronTimeLimitReached) {
683 return;
684 }
685
686 $scenarioHelper = new ScenarioHelper();
687 $scenarioHelper->executeAvailableSteps();
688 $scenarioHelper->triggerTimeScenarios();
689 }
690
691 private function continueCronCall(): void
692 {
693 if ($this->externalSendingNotFinished) {
694 $cronKey = '';
695 if (!empty($this->config->get('cron_security', 0)) && !empty($this->config->get('cron_key'))) {
696 $cronKey = '&cronKey='.$this->config->get('cron_key');
697 }
698 acym_makeCurlCall(acym_frontendLink('cron&task=cron&external_sending_repeat=1&t='.time().$cronKey), ['verifySsl' => false]);
699 }
700 }
701 }
702