Add inactive subscribers status job scheduling on settings change
[MAILPOET-2003]
This commit is contained in:
committed by
M. Shull
parent
f5d154d680
commit
bfcc499b10
@ -2,9 +2,12 @@
|
||||
|
||||
namespace MailPoet\API\JSON\v1;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
||||
use MailPoet\API\JSON\Error as APIError;
|
||||
use MailPoet\Config\AccessControl;
|
||||
use MailPoet\Cron\Workers\InactiveSubscribers;
|
||||
use MailPoet\Models\ScheduledTask;
|
||||
use MailPoet\Services\Bridge;
|
||||
use MailPoet\Settings\SettingsController;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
@ -36,12 +39,32 @@ class Settings extends APIEndpoint {
|
||||
WPFunctions::get()->__('You have not specified any settings to be saved.', 'mailpoet')
|
||||
));
|
||||
} else {
|
||||
$original_inactivation_interval = (int)$this->settings->get('deactivate_subscriber_after_inactive_days');
|
||||
foreach ($settings as $name => $value) {
|
||||
$this->settings->set($name, $value);
|
||||
}
|
||||
if (isset($settings['deactivate_subscriber_after_inactive_days'])
|
||||
&& $original_inactivation_interval !== (int)$settings['deactivate_subscriber_after_inactive_days']
|
||||
) {
|
||||
$this->onInactiveSubscribersIntervalChange();
|
||||
}
|
||||
$bridge = new Bridge();
|
||||
$bridge->onSettingsSave($settings);
|
||||
return $this->successResponse($this->settings->getAll());
|
||||
}
|
||||
}
|
||||
|
||||
private function onInactiveSubscribersIntervalChange() {
|
||||
$task = ScheduledTask::where('type', InactiveSubscribers::TASK_TYPE)
|
||||
->whereRaw('status = ?', [ScheduledTask::STATUS_SCHEDULED])
|
||||
->findOne();
|
||||
if (!$task) {
|
||||
$task = ScheduledTask::create();
|
||||
$task->type = InactiveSubscribers::TASK_TYPE;
|
||||
$task->status = ScheduledTask::STATUS_SCHEDULED;
|
||||
}
|
||||
$datetime = Carbon::createFromTimestamp(WPFunctions::get()->currentTime('timestamp'));
|
||||
$task->scheduled_at = $datetime->subMinute();
|
||||
$task->save();
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,12 @@
|
||||
<?php
|
||||
namespace MailPoet\Test\API\JSON\v1;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use MailPoet\API\JSON\Response as APIResponse;
|
||||
use MailPoet\API\JSON\Error as APIError;
|
||||
use MailPoet\API\JSON\v1\Settings;
|
||||
use MailPoet\Cron\Workers\InactiveSubscribers;
|
||||
use MailPoet\Models\ScheduledTask;
|
||||
use MailPoet\Models\Setting;
|
||||
use MailPoet\Settings\SettingsController;
|
||||
|
||||
@ -17,6 +20,7 @@ class SettingsTest extends \MailPoetTest {
|
||||
|
||||
function _before() {
|
||||
parent::_before();
|
||||
\ORM::raw_execute('TRUNCATE ' . ScheduledTask::$_table);
|
||||
$this->settings = new SettingsController();
|
||||
$this->settings->set('some.setting.key', true);
|
||||
$this->endpoint = new Settings($this->settings);
|
||||
@ -60,6 +64,23 @@ class SettingsTest extends \MailPoetTest {
|
||||
expect($response->data['some']['new_setting'])->true();
|
||||
}
|
||||
|
||||
function testItSchedulesInactiveSubscribersCheckIfIntervalSettingChanges() {
|
||||
$this->settings->set('deactivate_subscriber_after_inactive_days', 30);
|
||||
$settings = ['deactivate_subscriber_after_inactive_days' => 30];
|
||||
$this->endpoint->set($settings);
|
||||
$task = ScheduledTask::where('type', InactiveSubscribers::TASK_TYPE)
|
||||
->whereRaw('status = ?', [ScheduledTask::STATUS_SCHEDULED])
|
||||
->findOne();
|
||||
expect($task)->false();
|
||||
|
||||
$settings = ['deactivate_subscriber_after_inactive_days' => 0];
|
||||
$this->endpoint->set($settings);
|
||||
$task = ScheduledTask::where('type', InactiveSubscribers::TASK_TYPE)
|
||||
->whereRaw('status = ?', [ScheduledTask::STATUS_SCHEDULED])
|
||||
->findOne();
|
||||
expect($task->scheduled_at)->lessThan(Carbon::now());
|
||||
}
|
||||
|
||||
function _after() {
|
||||
\ORM::forTable(Setting::$_table)->deleteMany();
|
||||
}
|
||||
|
Reference in New Issue
Block a user