diff --git a/lib/Cron/Workers/SendingQueue.php b/lib/Cron/Workers/SendingQueue.php index 35e04898e3..3f2d200d83 100644 --- a/lib/Cron/Workers/SendingQueue.php +++ b/lib/Cron/Workers/SendingQueue.php @@ -44,7 +44,8 @@ class SendingQueue { $newsletter = $newsletter->asArray(); $newsletter['body'] = $this->getOrRenderNewsletterBody($queue, $newsletter); if ($newsletter['type'] === 'notification' && - strpos($newsletter['body']['html'], 'data-post-id') === false) { + strpos($newsletter['body']['html'], 'data-post-id') === false + ){ $queue->delete(); continue; } diff --git a/lib/Newsletter/Scheduler/Scheduler.php b/lib/Newsletter/Scheduler/Scheduler.php index 6692ba460e..7912ef19f6 100644 --- a/lib/Newsletter/Scheduler/Scheduler.php +++ b/lib/Newsletter/Scheduler/Scheduler.php @@ -56,6 +56,8 @@ class Scheduler { } $relation->value = $schedule; $relation->save(); + return Newsletter::filter('filterWithOptions') + ->findOne($newsletter_id)->asArray(); } static function schedulePostNotification($post_id) { diff --git a/lib/Router/SendingQueue.php b/lib/Router/SendingQueue.php index ae7eed8f9a..b1d4ae991d 100644 --- a/lib/Router/SendingQueue.php +++ b/lib/Router/SendingQueue.php @@ -32,6 +32,8 @@ class SendingQueue { 'result' => false, 'errors' => array(__('Newsletter does not exist.')) ); + } else { + $newsletter = $newsletter->asArray(); } // create or update newsletter options, including schedule time @@ -42,32 +44,33 @@ class SendingQueue { unset($data['options']); } if ($options && - ($newsletter->type === 'notification' || $newsletter->type === 'welcome') + ($newsletter['type'] === 'notification' || $newsletter['type'] === 'welcome') ) { $option_fields = NewsletterOptionField::where( - 'newsletter_type', $newsletter->type + 'newsletter_type', $newsletter['type'] )->findArray(); foreach($option_fields as $option_field) { if(isset($options[$option_field['name']])) { $relation = NewsletterOption::where('option_field_id', $option_field['id']) - ->where('newsletter_id', $newsletter->id) + ->where('newsletter_id', $newsletter['id']) ->findOne(); if (!$relation) { $relation = NewsletterOption::create(); - $relation->newsletter_id = $newsletter->id; + $relation->newsletter_id = $newsletter['id']; $relation->option_field_id = $option_field['id']; } $relation->value = $options[$option_field['name']]; $relation->save(); } } - if ($newsletter->type === 'notification') { - Scheduler::processPostNotificationSchedule($newsletter->id); + if ($newsletter['type'] === 'notification') { + // convert scheduling options into cron format and add to queue + $newsletter = Scheduler::processPostNotificationSchedule($newsletter['id']); + Scheduler::createPostNotificationQueue($newsletter); } - $newsletter = Newsletter::filter('filterWithOptions') - ->findOne($data['newsletter_id']); } - if($newsletter->type === 'welcome') { + + if($newsletter['type'] === 'welcome') { return array( 'result' => true, 'data' => array( @@ -77,7 +80,7 @@ class SendingQueue { } $queue = \MailPoet\Models\SendingQueue::whereNull('status') - ->where('newsletter_id', $newsletter->id) + ->where('newsletter_id', $newsletter['id']) ->findOne(); if(!empty($queue)) { return array( @@ -86,15 +89,15 @@ class SendingQueue { ); } - if($newsletter->type === 'notification') { + if($newsletter['type'] === 'notification') { $queue = \MailPoet\Models\SendingQueue::where('status', 'scheduled') - ->where('newsletter_id', $newsletter->id) + ->where('newsletter_id', $newsletter['id']) ->findOne(); if(!$queue) { $queue = \MailPoet\Models\SendingQueue::create(); - $queue->newsletter_id = $newsletter->id; + $queue->newsletter_id = $newsletter['id']; } - $schedule = Cron::factory($newsletter->schedule); + $schedule = Cron::factory($newsletter['schedule']); $queue->scheduled_at = $schedule->getNextRunDate(current_time('mysql'))->format('Y-m-d H:i:s'); $queue->status = 'scheduled'; @@ -108,7 +111,7 @@ class SendingQueue { } $queue = \MailPoet\Models\SendingQueue::create(); - $queue->newsletter_id = $newsletter->id; + $queue->newsletter_id = $newsletter['id']; $subscribers = Subscriber::getSubscribedInSegments($data['segments']) ->findArray();