Handle bulk operations on subscribers filtered by dynamic segments

[PREMIUM-69]
This commit is contained in:
Pavel Dohnal
2018-02-28 17:12:56 +00:00
parent 8837c9e14c
commit 99edc35b08
5 changed files with 181 additions and 7 deletions

View File

@ -12,6 +12,7 @@ use MailPoet\Models\Setting;
use MailPoet\Models\StatisticsForms;
use MailPoet\Models\Subscriber;
use MailPoet\Newsletter\Scheduler\Scheduler;
use MailPoet\Segments\BulkAction;
use MailPoet\Segments\SubscribersListings;
use MailPoet\Subscription\Throttling as SubscriptionThrottling;
use MailPoet\WP\Hooks;
@ -246,12 +247,12 @@ class Subscribers extends APIEndpoint {
function bulkAction($data = array()) {
try {
$bulk_action = new Listing\BulkAction(
'\MailPoet\Models\Subscriber',
$data
);
$meta = $bulk_action->apply();
return $this->successResponse(null, $meta);
if(!isset($data['listing']['filter']['segment'])) {
$bulk_action = new Listing\BulkAction('\MailPoet\Models\Subscriber', $data);
} else {
$bulk_action = new BulkAction($data);
}
return $this->successResponse(null, $bulk_action->apply());
} catch(\Exception $e) {
return $this->errorResponse(array(
$e->getCode() => $e->getMessage()

View File

@ -184,7 +184,7 @@ class Model extends \Sudzy\ValidModel {
return (int)$model['id'];
}, $rows);
if($callback !== false) {
if(is_callable($callback)) {
$callback($ids);
}

View File

@ -0,0 +1,57 @@
<?php
namespace MailPoet\Segments;
use MailPoet\Models\Segment;
use MailPoet\WP\Hooks;
class BulkAction {
private $data = null;
function __construct($data) {
$this->data = $data;
}
/**
* @return array
* @throws \Exception
*/
function apply() {
if(!isset($this->data['listing']['filter']['segment'])) {
throw new \InvalidArgumentException('Missing segment id');
}
$segment = Segment::findOne($this->data['listing']['filter']['segment']);
if($segment) {
$segment = $segment->asArray();
}
return $this->applySegment($segment);
}
/**
* @param array $segment
*
* @return array
* @throws \Exception
*/
private function applySegment($segment) {
if(!$segment || $segment['type'] === Segment::TYPE_DEFAULT || $segment['type'] === Segment::TYPE_WP_USERS) {
$bulk_action = new \MailPoet\Listing\BulkAction(
'\MailPoet\Models\Subscriber',
$this->data
);
return $bulk_action->apply();
} else {
$handlers = Hooks::applyFilters('mailpoet_subscribers_in_segment_apply_bulk_action_handlers', array());
foreach($handlers as $handler) {
$meta = $handler->apply($segment, $this->data);
if($meta) {
return $meta;
}
}
throw new \InvalidArgumentException('No handler found for segment');
}
}
}

View File

@ -0,0 +1,105 @@
<?php
namespace MailPoet\Segments;
use Codeception\Util\Stub;
use MailPoet\Models\Segment;
use MailPoet\Models\Subscriber;
use MailPoet\Models\SubscriberSegment;
use MailPoet\WP\Hooks;
require_once('SubscribersBulkActionHandlerMock.php');
class BulkActionTest extends \MailPoetTest {
function _before() {
$this->cleanData();
$this->segment_1 = Segment::createOrUpdate(array('name' => 'Segment 1', 'type' => 'default'));
$this->segment_2 = Segment::createOrUpdate(array('name' => 'Segment 3', 'type' => 'not default'));
$this->subscriber_1 = Subscriber::createOrUpdate(array(
'email' => 'john@mailpoet.com',
'first_name' => 'John',
'last_name' => 'Doe',
'status' => Subscriber::STATUS_SUBSCRIBED,
'segments' => array(
$this->segment_1->id,
),
));
$this->subscriber_2 = Subscriber::createOrUpdate(array(
'email' => 'jake@mailpoet.com',
'first_name' => 'Jake',
'last_name' => 'Doe',
'status' => Subscriber::STATUS_SUBSCRIBED,
'segments' => array(
$this->segment_2->id,
),
));
SubscriberSegment::resubscribeToAllSegments($this->subscriber_1);
SubscriberSegment::resubscribeToAllSegments($this->subscriber_2);
}
function _after() {
$this->cleanData();
}
private function cleanData() {
\ORM::raw_execute('TRUNCATE ' . Segment::$_table);
\ORM::raw_execute('TRUNCATE ' . SubscriberSegment::$_table);
\ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
}
function testBulkActionWithoutSegment() {
$handler = new BulkAction(array());
$this->setExpectedException('InvalidArgumentException');
$handler->apply();
}
function testBulkActionForDefaultSegment() {
$handler = new BulkAction(array(
'listing' => array('filter'=> array('segment' => $this->segment_1->id)),
'action' => 'trash',
));
$result = $handler->apply();
expect($result['count'])->equals(1);
}
function testBulkActionForUnknownSegment() {
$handler = new BulkAction(array(
'listing' => array('filter'=> array('segment' => 'this-segment-doesnt-exist')),
'action' => 'trash',
));
$result = $handler->apply();
expect($result)->notEmpty();
}
function testForUnknownSegmentTypeWithoutHandler() {
$handler = new BulkAction(array(
'listing' => array('filter'=> array('segment' => $this->segment_2->id)),
'action' => 'trash',
));
$this->setExpectedException('InvalidArgumentException');
remove_all_filters('mailpoet_subscribers_in_segment_apply_bulk_action_handlers');
$handler->apply();
}
function testBulkActionUsingFilter() {
$mock = Stub::makeEmpty('\MailPoet\Test\Segments\SubscribersBulkActionHandlerMock', array('apply'));
$mock
->expects($this->once())
->method('apply')
->will($this->returnValue('result'));
remove_all_filters('mailpoet_subscribers_in_segment_apply_bulk_action_handlers');
Hooks::addFilter('mailpoet_subscribers_in_segment_apply_bulk_action_handlers', function () use ($mock) {
return array($mock);
});
$handler = new BulkAction(array(
'listing' => array('filter'=> array('segment' => $this->segment_2->id)),
'action' => 'trash',
));
$result = $handler->apply();
expect($result)->equals('result');
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace MailPoet\Test\Segments;
class SubscribersBulkActionHandlerMock {
function apply() {
}
}