- Fixes sending limits not being enforced
This commit is contained in:
@@ -5,6 +5,7 @@ use MailPoet\Cron\CronHelper;
|
||||
use MailPoet\Cron\Workers\SendingQueue\Tasks\Mailer as MailerTask;
|
||||
use MailPoet\Cron\Workers\SendingQueue\Tasks\Newsletter as NewsletterTask;
|
||||
use MailPoet\Cron\Workers\SendingQueue\Tasks\Subscribers as SubscribersTask;
|
||||
use MailPoet\Mailer\MailerLog;
|
||||
use MailPoet\Models\SendingQueue as SendingQueueModel;
|
||||
use MailPoet\Models\StatisticsNewsletters as StatisticsNewslettersModel;
|
||||
use MailPoet\Models\Subscriber as SubscriberModel;
|
||||
@@ -22,14 +23,14 @@ class SendingQueue {
|
||||
$this->mailer_task = new MailerTask();
|
||||
$this->newsletter_task = new NewsletterTask();
|
||||
$this->timer = ($timer) ? $timer : microtime(true);
|
||||
// abort if sending limit is reached
|
||||
// abort if execution or sending limit are reached
|
||||
CronHelper::enforceExecutionLimit($this->timer);
|
||||
}
|
||||
|
||||
function process() {
|
||||
foreach(self::getRunningQueues() as $queue) {
|
||||
// abort if sending limit is reached
|
||||
$this->mailer_task->enforceSendingLimit();
|
||||
MailerLog::enforceSendingLimit();
|
||||
// get and pre-process newsletter (render, replace shortcodes/links, etc.)
|
||||
$newsletter = $this->newsletter_task->getAndPreProcess($queue->asArray());
|
||||
if(!$newsletter) {
|
||||
@@ -157,16 +158,17 @@ class SendingQueue {
|
||||
);
|
||||
// log statistics
|
||||
StatisticsNewslettersModel::createMultiple($statistics);
|
||||
// keep track of sent items
|
||||
$this->mailer_task->updateMailerLog();
|
||||
// update the sent count
|
||||
$this->mailer_task->updateSentCount();
|
||||
$queue = $this->updateQueue($queue);
|
||||
// enforce sending limit if there are still subscribers left to process
|
||||
if($queue->count_to_process) {
|
||||
$this->mailer_task->enforceSendingLimit();
|
||||
MailerLog::enforceSendingLimit();
|
||||
}
|
||||
}
|
||||
return $queue;
|
||||
}
|
||||
|
||||
static function getRunningQueues() {
|
||||
return SendingQueueModel::orderByDesc('priority')
|
||||
->whereNull('deleted_at')
|
||||
|
@@ -8,11 +8,9 @@ if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Mailer {
|
||||
public $mailer;
|
||||
public $mailer_log;
|
||||
|
||||
function __construct() {
|
||||
$this->mailer = $this->configureMailer();
|
||||
$this->mailer_log = $this->getMailerLog();
|
||||
}
|
||||
|
||||
function configureMailer(array $newsletter = null) {
|
||||
@@ -42,9 +40,8 @@ class Mailer {
|
||||
return MailerLog::getMailerLog();
|
||||
}
|
||||
|
||||
function updateMailerLog() {
|
||||
$this->mailer_log['sent']++;
|
||||
return MailerLog::updateMailerLog($this->mailer_log);
|
||||
function updateSentCount() {
|
||||
return MailerLog::incrementSentCount();
|
||||
}
|
||||
|
||||
function getProcessingMethod() {
|
||||
@@ -63,10 +60,4 @@ class Mailer {
|
||||
$prepared_subscribers
|
||||
);
|
||||
}
|
||||
|
||||
function enforceSendingLimit() {
|
||||
if(MailerLog::isSendingLimitReached()) {
|
||||
throw new \Exception(__('Sending frequency limit has been reached'));
|
||||
}
|
||||
}
|
||||
}
|
@@ -11,12 +11,12 @@ class MailerLog {
|
||||
static function getMailerLog() {
|
||||
$mailer_log = Setting::getValue(self::SETTING_NAME);
|
||||
if(!$mailer_log) {
|
||||
$mailer_log = self::createOrResetMailerLog();
|
||||
$mailer_log = self::createMailerLog();
|
||||
}
|
||||
return $mailer_log;
|
||||
}
|
||||
|
||||
static function createOrResetMailerLog() {
|
||||
static function createMailerLog() {
|
||||
$mailer_log = array(
|
||||
'sent' => 0,
|
||||
'started' => time()
|
||||
@@ -25,23 +25,36 @@ class MailerLog {
|
||||
return $mailer_log;
|
||||
}
|
||||
|
||||
static function resetMailerLog() {
|
||||
return self::createMailerLog();
|
||||
}
|
||||
|
||||
static function updateMailerLog($mailer_log) {
|
||||
Setting::setValue(self::SETTING_NAME, $mailer_log);
|
||||
return $mailer_log;
|
||||
}
|
||||
|
||||
static function incrementSentCount($mailer_log = false) {
|
||||
$mailer_log = ($mailer_log) ? $mailer_log : self::getMailerLog();
|
||||
(int)$mailer_log['sent']++;
|
||||
return self::updateMailerLog($mailer_log);
|
||||
}
|
||||
|
||||
static function isSendingLimitReached() {
|
||||
$mailer_config = Mailer::getMailerConfig();
|
||||
$mailer_log = self::getMailerLog();
|
||||
$elapsed_time = time() - (int)$mailer_log['started'];
|
||||
if($mailer_log['sent'] === $mailer_config['frequency_limit'] &&
|
||||
$elapsed_time <= $mailer_config['frequency_interval']
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
if($elapsed_time > $mailer_config['frequency_interval']) {
|
||||
self::createOrResetMailerLog();
|
||||
if($mailer_log['sent'] === $mailer_config['frequency_limit']) {
|
||||
if($elapsed_time <= $mailer_config['frequency_interval']) return true;
|
||||
// reset mailer log if enough time has passed since the limit was reached
|
||||
self::resetMailerLog();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static function enforceSendingLimit() {
|
||||
if(self::isSendingLimitReached()) {
|
||||
throw new \Exception(__('Sending frequency limit has been reached'));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user