diff --git a/lib/Cron/Workers/Scheduler.php b/lib/Cron/Workers/Scheduler.php index d03805e315..2122e3b322 100644 --- a/lib/Cron/Workers/Scheduler.php +++ b/lib/Cron/Workers/Scheduler.php @@ -32,8 +32,7 @@ class Scheduler { if(!count($scheduled_queues)) return false; $this->updateTasks($scheduled_queues); foreach($scheduled_queues as $i => $queue) { - $newsletter = Newsletter::filter('filterWithOptions') - ->findOne($queue->newsletter_id); + $newsletter = Newsletter::filter('filterWithOptions')->findOne($queue->newsletter_id); if(!$newsletter || $newsletter->deleted_at !== null) { $queue->delete(); } elseif($newsletter->status !== Newsletter::STATUS_ACTIVE && $newsletter->status !== Newsletter::STATUS_SCHEDULED) { @@ -44,6 +43,8 @@ class Scheduler { $this->processPostNotificationNewsletter($newsletter, $queue); } elseif($newsletter->type === Newsletter::TYPE_STANDARD) { $this->processScheduledStandardNewsletter($newsletter, $queue); + } elseif($newsletter->type === Newsletter::TYPE_AUTOMATIC) { + $this->processScheduledAutomaticEmail($newsletter, $queue); } CronHelper::enforceExecutionLimit($this->timer); } @@ -101,6 +102,22 @@ class Scheduler { return true; } + function processScheduledAutomaticEmail($newsletter, $queue) { + if($newsletter->sendTo === 'segment') { + $segment = Segment::findOne($newsletter->segment)->asArray(); + $finder = new SubscribersFinder(); + $result = $finder->addSubscribersToTaskFromSegments($queue->task(), array($segment)); + if(empty($result)) { + $queue->delete(); + return false; + } + } + + $queue->status = null; + $queue->save(); + return true; + } + function processScheduledStandardNewsletter($newsletter, $queue) { $segments = $newsletter->segments()->findArray(); $finder = new SubscribersFinder(); diff --git a/lib/Newsletter/Scheduler/Scheduler.php b/lib/Newsletter/Scheduler/Scheduler.php index c02c0fcb5e..de50981fbf 100644 --- a/lib/Newsletter/Scheduler/Scheduler.php +++ b/lib/Newsletter/Scheduler/Scheduler.php @@ -47,6 +47,16 @@ class Scheduler { } } + static function scheduleAutomaticEmail($group, $event, $scheduling_condition = false, $subscriber_id = false, $meta = false) { + $newsletters = self::getNewsletters(Newsletter::TYPE_AUTOMATIC, $group); + if(empty($newsletters)) return false; + foreach($newsletters as $newsletter) { + if($newsletter->event !== $event) continue; + if(is_callable($scheduling_condition) && !$scheduling_condition($newsletter)) continue; + self::createAutomaticEmailQueue($newsletter, $subscriber_id, $meta); + } + } + static function scheduleWPUserWelcomeNotification( $subscriber_id, $wp_user, @@ -84,26 +94,30 @@ class Scheduler { $queue = SendingTask::create(); $queue->newsletter_id = $newsletter->id; $queue->setSubscribers(array($subscriber_id)); - $after_time_type = $newsletter->afterTimeType; - $after_time_number = $newsletter->afterTimeNumber; - $scheduled_at = null; - $current_time = Carbon::createFromTimestamp(WPFunctions::currentTime('timestamp')); - switch($after_time_type) { - case 'hours': - $scheduled_at = $current_time->addHours($after_time_number); - break; - case 'days': - $scheduled_at = $current_time->addDays($after_time_number); - break; - case 'weeks': - $scheduled_at = $current_time->addWeeks($after_time_number); - break; - default: - $scheduled_at = $current_time; - } $queue->status = SendingQueue::STATUS_SCHEDULED; $queue->priority = SendingQueue::PRIORITY_HIGH; - $queue->scheduled_at = $scheduled_at; + $queue->scheduled_at = self::getScheduledTimeForWelcomeOrAutomaticEmails( + $newsletter->afterTimeType, + $newsletter->afterTimeNumber + ); + return $queue->save(); + } + + static function createAutomaticEmailQueue($newsletter, $subscriber_id, $meta) { + $queue = SendingTask::create(); + $queue->newsletter_id = $newsletter->id; + if($newsletter->sendTo === 'user' && $subscriber_id) { + $queue->setSubscribers(array($subscriber_id)); + } + if($meta) { + $queue->__set('meta', $meta); + } + $queue->status = SendingQueue::STATUS_SCHEDULED; + $queue->priority = SendingQueue::PRIORITY_MEDIUM; + $queue->scheduled_at = self::getScheduledTimeForWelcomeNotificationOrAutomaticEmails( + $newsletter->afterTimeType, + $newsletter->afterTimeNumber + ); return $queue->save(); } @@ -195,9 +209,9 @@ class Scheduler { return $previous_run_date; } - static function getNewsletters($type) { + static function getNewsletters($type, $group = false) { return Newsletter::getPublished() - ->filter('filterType', $type) + ->filter('filterType', $type, $group) ->filter('filterStatus', Newsletter::STATUS_ACTIVE) ->filter('filterWithOptions') ->findMany(); @@ -206,4 +220,18 @@ class Scheduler { static function formatDatetimeString($datetime_string) { return Carbon::parse($datetime_string)->format('Y-m-d H:i:s'); } -} + + static function getScheduledTimeForWelcomeNotificationOrAutomaticEmails($after_time_type, $after_time_number) { + $current_time = Carbon::createFromTimestamp(WPFunctions::currentTime('timestamp')); + switch($after_time_type) { + case 'hours': + return $current_time->addHours($after_time_number); + case 'days': + return $current_time->addDays($after_time_number); + case 'weeks': + return $current_time->addWeeks($after_time_number); + default: + return $current_time; + } + } +} \ No newline at end of file