| @@ -1,39 +1,50 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace AcyMailing\WpInit; |
| 4 | 4 | |
| 5 | +defined('ABSPATH') || die('Restricted Access'); | |
| 6 | + | |
| 7 | +use AcyMailing\Controllers\ConfigurationController; | |
| 8 | +use AcyMailing\Helpers\CronHelper; | |
| 9 | + | |
| 5 | 10 | class Cron |
| 6 | 11 | { |
| 7 | 12 | public function __construct() |
| 8 | 13 | { |
| 9 | - $ctrl = acym_getVar('string', 'ctrl', ''); | |
| 10 | - if (acym_level(ACYM_ESSENTIAL) && !acym_isNoTemplate() && $ctrl !== 'cron') { | |
| 11 | - $this->callAcyCron(); | |
| 12 | - } | |
| 14 | + add_filter('cron_schedules', [$this, 'addCronIntervals'], 100); | |
| 15 | + add_action(ConfigurationController::CRON_TASK_NAME, [$this, 'triggerAutomatedTasks']); | |
| 13 | 16 | } |
| 14 | 17 | |
| 15 | - public function callAcyCron() | |
| 18 | + public function addCronIntervals(array $schedules): array | |
| 16 | 19 | { |
| 17 | - $config = acym_config(); | |
| 20 | + $schedules['every_minute'] = [ | |
| 21 | + 'interval' => 60, | |
| 22 | + 'display' => 'Every minute', | |
| 23 | + ]; | |
| 18 | 24 | |
| 19 | - $activeCron = $config->get('active_cron', 0); | |
| 20 | - if (empty($activeCron)) return; | |
| 25 | + return $schedules; | |
| 26 | + } | |
| 21 | 27 | |
| 22 | - $queueType = $config->get('queue_type', 'manual'); | |
| 23 | - $cronNext = $config->get('cron_next', 0); | |
| 24 | - if (empty($cronNext) || $cronNext > time() || $queueType === 'manual') return; | |
| 28 | + public function triggerAutomatedTasks() | |
| 29 | + { | |
| 25 | 30 | |
| 26 | - $cronFrequency = $config->get('cron_frequency', 0); | |
| 27 | - $cronBatches = $config->get('queue_batch_auto', 1); | |
| 31 | + // Starter versions shouldn't have access to the cron | |
| 32 | + if (!acym_level(ACYM_ESSENTIAL)) { | |
| 33 | + acym_deleteScheduledTask(['name' => ConfigurationController::CRON_TASK_NAME]); | |
| 28 | 34 | |
| 29 | - if (empty($cronFrequency)) return; | |
| 35 | + return; | |
| 36 | + } | |
| 30 | 37 | |
| 31 | - if (intval($cronFrequency) >= 900 && intval($cronBatches) < 2) return; | |
| 38 | + //removeIf(development) | |
| 39 | + $httpReferer = acym_getVar('string', 'HTTP_REFERER', '', 'SERVER'); | |
| 40 | + if (!acym_isLicenseValidWeekly() && (empty($httpReferer) || strpos($httpReferer, 'api.acymailing.com') === false)) { | |
| 41 | + acym_deleteScheduledTask(['name' => ConfigurationController::CRON_TASK_NAME]); | |
| 32 | 42 | |
| 33 | - $cronKey = ''; | |
| 34 | - if (!empty($config->get('cron_security', 0)) && !empty($config->get('cron_key'))) { | |
| 35 | - $cronKey = '&cronKey='.$config->get('cron_key'); | |
| 43 | + return; | |
| 36 | 44 | } |
| 37 | - acym_asyncCurlCall([acym_frontendLink('cron&task=cron'.$cronKey)]); | |
| 45 | + //endRemoveIf(development) | |
| 46 | + | |
| 47 | + $cronHelper = new CronHelper(); | |
| 48 | + $cronHelper->cron(); | |
| 38 | 49 | } |
| 39 | 50 | } |