Add inactive subscribers deactivation
[MAILPOET-1971]
This commit is contained in:
committed by
M. Shull
parent
e660541b5b
commit
9edace78f0
88
lib/Subscribers/InactiveSubscribersController.php
Normal file
88
lib/Subscribers/InactiveSubscribersController.php
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Subscribers;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use MailPoet\Models\ScheduledTask;
|
||||||
|
use MailPoet\Models\ScheduledTaskSubscriber;
|
||||||
|
use MailPoet\Models\SendingQueue;
|
||||||
|
use MailPoet\Models\StatisticsOpens;
|
||||||
|
use MailPoet\Models\Subscriber;
|
||||||
|
|
||||||
|
if (!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
|
class InactiveSubscribersController {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $days_to_inactive
|
||||||
|
* @param int $batch_size
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
function markInactiveSubscribers($days_to_inactive, $batch_size) {
|
||||||
|
$threshold_date = $this->getThresholdDate($days_to_inactive);
|
||||||
|
return $this->deactivateSubscribers($threshold_date, $batch_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $days_to_inactive
|
||||||
|
* @return Carbon
|
||||||
|
*/
|
||||||
|
private function getThresholdDate($days_to_inactive) {
|
||||||
|
$now = new Carbon();
|
||||||
|
return $now->subDays($days_to_inactive);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Carbon $threshold_date
|
||||||
|
* @param int $batch_size
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
private function deactivateSubscribers(Carbon $threshold_date, $batch_size) {
|
||||||
|
$subscribers_table = Subscriber::$_table;
|
||||||
|
$scheduled_tasks_table = ScheduledTask::$_table;
|
||||||
|
$scheduled_task_subcribres_table = ScheduledTaskSubscriber::$_table;
|
||||||
|
$statistics_opens_table = StatisticsOpens::$_table;
|
||||||
|
$sending_queues_table = SendingQueue::$_table;
|
||||||
|
|
||||||
|
$threshold_date_iso = $threshold_date->toIso8601String();
|
||||||
|
$day_ago = new Carbon();
|
||||||
|
$day_ago_iso = $day_ago->subDay()->toIso8601String();
|
||||||
|
|
||||||
|
// We take into account only emails which have at least one opening tracked
|
||||||
|
// to ensure that tracking was enabled for the particular email
|
||||||
|
$scheduled_task_ids_query = sprintf("
|
||||||
|
SELECT task_id as id FROM $sending_queues_table as sq
|
||||||
|
JOIN (SELECT newsletter_id as id FROM $statistics_opens_table as so WHERE so.created_at > '%s' GROUP BY newsletter_id) newsletters_ids ON newsletters_ids.id = sq.newsletter_id
|
||||||
|
JOIN $scheduled_tasks_table as st ON sq.task_id = st.id AND st.processed_at > '%s' AND st.processed_at < '%s'
|
||||||
|
GROUP BY task_id",
|
||||||
|
$threshold_date_iso, $threshold_date_iso, $day_ago_iso
|
||||||
|
);
|
||||||
|
|
||||||
|
// Select subscribers who received a recent tracked email but didn't open it
|
||||||
|
$ids_to_deactivate = \ORM::forTable($subscribers_table)->rawQuery("
|
||||||
|
SELECT s.id FROM $subscribers_table as s
|
||||||
|
JOIN $scheduled_task_subcribres_table as sts ON s.id = sts.subscriber_id
|
||||||
|
JOIN ($scheduled_task_ids_query) task_ids ON task_ids.id = sts.task_id
|
||||||
|
LEFT OUTER JOIN $statistics_opens_table as so ON s.id = so.subscriber_id AND so.created_at > ?
|
||||||
|
WHERE s.created_at < ? AND s.status = ? AND so.id IS NULL
|
||||||
|
GROUP BY s.id LIMIT ?",
|
||||||
|
[$threshold_date_iso, $threshold_date_iso, Subscriber::STATUS_SUBSCRIBED, $batch_size]
|
||||||
|
)->findArray();
|
||||||
|
|
||||||
|
$ids_to_deactivate = array_map(
|
||||||
|
function ($id) {
|
||||||
|
return (int)$id['id'];
|
||||||
|
},
|
||||||
|
$ids_to_deactivate
|
||||||
|
);
|
||||||
|
if (!count($ids_to_deactivate)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
\ORM::rawExecute(sprintf(
|
||||||
|
"UPDATE %s SET status='" . Subscriber::STATUS_INACTIVE . "' WHERE id IN (%s);",
|
||||||
|
$subscribers_table,
|
||||||
|
implode(',', $ids_to_deactivate)
|
||||||
|
));
|
||||||
|
return count($ids_to_deactivate);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,190 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Subscribers;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use MailPoet\Models\Newsletter;
|
||||||
|
use MailPoet\Models\ScheduledTask;
|
||||||
|
use MailPoet\Models\ScheduledTaskSubscriber;
|
||||||
|
use MailPoet\Models\SendingQueue;
|
||||||
|
use MailPoet\Models\StatisticsOpens;
|
||||||
|
use MailPoet\Models\Subscriber;
|
||||||
|
use MailPoet\Tasks\Sending;
|
||||||
|
|
||||||
|
class InactiveSubscribersControllerTest extends \MailPoetTest {
|
||||||
|
|
||||||
|
/** @var InactiveSubscribersController */
|
||||||
|
private $controller;
|
||||||
|
|
||||||
|
/** @var Newsletter */
|
||||||
|
private $newsletter;
|
||||||
|
|
||||||
|
function _before() {
|
||||||
|
$this->controller = new InactiveSubscribersController();
|
||||||
|
\ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||||
|
\ORM::raw_execute('TRUNCATE ' . ScheduledTask::$_table);
|
||||||
|
\ORM::raw_execute('TRUNCATE ' . StatisticsOpens::$_table);
|
||||||
|
\ORM::raw_execute('TRUNCATE ' . ScheduledTaskSubscriber::$_table);
|
||||||
|
\ORM::raw_execute('TRUNCATE ' . SendingQueue::$_table);
|
||||||
|
\ORM::raw_execute('TRUNCATE ' . Newsletter::$_table);
|
||||||
|
$this->newsletter = Newsletter::createOrUpdate([
|
||||||
|
'subject' => "Subject ",
|
||||||
|
"type" => Newsletter::TYPE_STANDARD,
|
||||||
|
"status" => Newsletter::STATUS_SENT,
|
||||||
|
]);
|
||||||
|
$this->newsletter->save();
|
||||||
|
parent::_before();
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDeactivatesOldSubscribersWithUnopenedEmail() {
|
||||||
|
list($task) = $this->createCompletedSendingTaskWithOneOpen(3);
|
||||||
|
|
||||||
|
$subscriber1 = $this->createSubscriber('s1@email.com', 10);
|
||||||
|
$this->addSubcriberToTask($subscriber1, $task);
|
||||||
|
$subscriber2 = $this->createSubscriber('s2@email.com', 10);
|
||||||
|
$this->addSubcriberToTask($subscriber2, $task);
|
||||||
|
|
||||||
|
$result = $this->controller->markInactiveSubscribers(5, 100);
|
||||||
|
expect($result)->equals(2);
|
||||||
|
$subscriber1 = Subscriber::findOne($subscriber1->id);
|
||||||
|
$subscriber2 = Subscriber::findOne($subscriber2->id);
|
||||||
|
expect($subscriber1->status)->equals(Subscriber::STATUS_INACTIVE);
|
||||||
|
expect($subscriber2->status)->equals(Subscriber::STATUS_INACTIVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDeactivatesLimitedAmountOfSubscribers() {
|
||||||
|
list($task) = $this->createCompletedSendingTaskWithOneOpen(3);
|
||||||
|
|
||||||
|
$subscriber1 = $this->createSubscriber('s1@email.com', 10);
|
||||||
|
$this->addSubcriberToTask($subscriber1, $task);
|
||||||
|
$subscriber2 = $this->createSubscriber('s2@email.com', 10);
|
||||||
|
$this->addSubcriberToTask($subscriber2, $task);
|
||||||
|
|
||||||
|
$result = $this->controller->markInactiveSubscribers(5, 1);
|
||||||
|
expect($result)->equals(1);
|
||||||
|
$subscriber1 = Subscriber::findOne($subscriber1->id);
|
||||||
|
$subscriber2 = Subscriber::findOne($subscriber2->id);
|
||||||
|
expect($subscriber1->status === Subscriber::STATUS_INACTIVE || $subscriber2->status === Subscriber::STATUS_INACTIVE)->true();
|
||||||
|
expect($subscriber1->status === Subscriber::STATUS_SUBSCRIBED || $subscriber2->status === Subscriber::STATUS_SUBSCRIBED)->true();
|
||||||
|
|
||||||
|
$result = $this->controller->markInactiveSubscribers(5, 1);
|
||||||
|
expect($result)->equals(1);
|
||||||
|
$subscriber1 = Subscriber::findOne($subscriber1->id);
|
||||||
|
$subscriber2 = Subscriber::findOne($subscriber2->id);
|
||||||
|
expect($subscriber1->status)->equals(Subscriber::STATUS_INACTIVE);
|
||||||
|
expect($subscriber2->status)->equals(Subscriber::STATUS_INACTIVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDoesNotDeactivateNewSubscriberWithUnopenedEmail() {
|
||||||
|
list($task) = $this->createCompletedSendingTaskWithOneOpen(1);
|
||||||
|
|
||||||
|
$subscriber = $this->createSubscriber('s1@email.com', 2);
|
||||||
|
$this->addSubcriberToTask($subscriber, $task);
|
||||||
|
|
||||||
|
$result = $this->controller->markInactiveSubscribers(5, 100);
|
||||||
|
expect($result)->equals(0);
|
||||||
|
$subscriber = Subscriber::findOne($subscriber->id);
|
||||||
|
expect($subscriber->status)->equals(Subscriber::STATUS_SUBSCRIBED);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDoesNotDeactivateSubscriberWithoutSentEmail() {
|
||||||
|
$this->createCompletedSendingTaskWithOneOpen(1);
|
||||||
|
$subscriber = $this->createSubscriber('s1@email.com', 10);
|
||||||
|
$result = $this->controller->markInactiveSubscribers(5, 100);
|
||||||
|
expect($result)->equals(0);
|
||||||
|
$subscriber = Subscriber::findOne($subscriber->id);
|
||||||
|
expect($subscriber->status)->equals(Subscriber::STATUS_SUBSCRIBED);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDoesNotDeactivateSubscriberWhoRecentlyOpenedEmail() {
|
||||||
|
list($task, $queue) = $this->createCompletedSendingTaskWithOneOpen(2);
|
||||||
|
$subscriber = $this->createSubscriber('s1@email.com', 10);
|
||||||
|
$this->addSubcriberToTask($subscriber, $task);
|
||||||
|
$this->addEmailOpenedRecord($subscriber, $queue, 2);
|
||||||
|
list($task2) = $this->createCompletedSendingTaskWithOneOpen(2);
|
||||||
|
$this->addSubcriberToTask($subscriber, $task2);
|
||||||
|
$result = $this->controller->markInactiveSubscribers(5, 100);
|
||||||
|
expect($result)->equals(0);
|
||||||
|
$subscriber = Subscriber::findOne($subscriber->id);
|
||||||
|
expect($subscriber->status)->equals(Subscriber::STATUS_SUBSCRIBED);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDoesNotDeactivateSubscriberWhoReceivedEmailRecently() {
|
||||||
|
list($task) = $this->createCompletedSendingTaskWithOneOpen(0);
|
||||||
|
$subscriber = $this->createSubscriber('s1@email.com', 10);
|
||||||
|
$this->addSubcriberToTask($subscriber, $task);
|
||||||
|
$result = $this->controller->markInactiveSubscribers(5, 100);
|
||||||
|
expect($result)->equals(0);
|
||||||
|
$subscriber = Subscriber::findOne($subscriber->id);
|
||||||
|
expect($subscriber->status)->equals(Subscriber::STATUS_SUBSCRIBED);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDoesNotDeactivateSubscriberWhoReceivedEmailWhichWasNeverOpened() {
|
||||||
|
list($task) = $this->createCompletedSendingTask(2);
|
||||||
|
$subscriber = $this->createSubscriber('s1@email.com', 10);
|
||||||
|
$this->addSubcriberToTask($subscriber, $task);
|
||||||
|
$result = $this->controller->markInactiveSubscribers(5, 100);
|
||||||
|
expect($result)->equals(0);
|
||||||
|
$subscriber = Subscriber::findOne($subscriber->id);
|
||||||
|
expect($subscriber->status)->equals(Subscriber::STATUS_SUBSCRIBED);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $email
|
||||||
|
* @param int $created_days_ago
|
||||||
|
* @param string $status
|
||||||
|
* @return Subscriber
|
||||||
|
*/
|
||||||
|
private function createSubscriber($email, $created_days_ago = 0, $status = Subscriber::STATUS_SUBSCRIBED) {
|
||||||
|
$created_at = (new Carbon())->subDays($created_days_ago)->toDateTimeString();
|
||||||
|
$subscriber = Subscriber::createOrUpdate(['email' => $email, 'status' => $status]);
|
||||||
|
$subscriber->created_at = $created_at;
|
||||||
|
$subscriber->save();
|
||||||
|
return $subscriber;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $processed_days_ago
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function createCompletedSendingTask($processed_days_ago = 0) {
|
||||||
|
$processed_at = (new Carbon())->subDays($processed_days_ago)->toDateTimeString();
|
||||||
|
$task = ScheduledTask::createOrUpdate(['type' => Sending::TASK_TYPE, 'status' => ScheduledTask::STATUS_COMPLETED]);
|
||||||
|
$task->created_at = $processed_at;
|
||||||
|
$task->processed_at = $processed_at;
|
||||||
|
$task->save();
|
||||||
|
$queue = SendingQueue::createOrUpdate(['task_id' => $task->id, 'newsletter_id' => $this->newsletter->id]);
|
||||||
|
$queue->save();
|
||||||
|
return [$task, $queue];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $processed_days_ago
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function createCompletedSendingTaskWithOneOpen($processed_days_ago = 0) {
|
||||||
|
list($task, $queue) = $this->createCompletedSendingTask($processed_days_ago);
|
||||||
|
$subscriber0 = $this->createSubscriber('s0@email.com', 10);
|
||||||
|
$this->addSubcriberToTask($subscriber0, $task);
|
||||||
|
$this->addEmailOpenedRecord($subscriber0, $queue);
|
||||||
|
return [$task, $queue];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Subscriber $subscriber
|
||||||
|
* @param ScheduledTask $task
|
||||||
|
* @param int $days_ago
|
||||||
|
*/
|
||||||
|
private function addSubcriberToTask(Subscriber $subscriber, ScheduledTask $task, $days_ago = 0) {
|
||||||
|
$created_at = (new Carbon())->subDays($days_ago)->toDateTimeString();
|
||||||
|
$task_subscriber = ScheduledTaskSubscriber::createOrUpdate(['task_id' => $task->id, 'subscriber_id' => $subscriber->id]);
|
||||||
|
$task_subscriber->created_at = $created_at;
|
||||||
|
$task_subscriber->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function addEmailOpenedRecord(Subscriber $subscriber, SendingQueue $queue, $days_ago = 0) {
|
||||||
|
$opened = StatisticsOpens::createOrUpdate(['subscriber_id' => $subscriber->id, 'newsletter_id' => $queue->newsletter_id, 'queue_id' => $queue->id]);
|
||||||
|
$opened->created_at = (new Carbon())->subDays($days_ago)->toDateTimeString();
|
||||||
|
$opened->save();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user