Handle bulk operations on subscribers filtered by dynamic segments
[PREMIUM-69]
This commit is contained in:
@ -12,6 +12,7 @@ use MailPoet\Models\Setting;
|
|||||||
use MailPoet\Models\StatisticsForms;
|
use MailPoet\Models\StatisticsForms;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
use MailPoet\Newsletter\Scheduler\Scheduler;
|
use MailPoet\Newsletter\Scheduler\Scheduler;
|
||||||
|
use MailPoet\Segments\BulkAction;
|
||||||
use MailPoet\Segments\SubscribersListings;
|
use MailPoet\Segments\SubscribersListings;
|
||||||
use MailPoet\Subscription\Throttling as SubscriptionThrottling;
|
use MailPoet\Subscription\Throttling as SubscriptionThrottling;
|
||||||
use MailPoet\WP\Hooks;
|
use MailPoet\WP\Hooks;
|
||||||
@ -246,12 +247,12 @@ class Subscribers extends APIEndpoint {
|
|||||||
|
|
||||||
function bulkAction($data = array()) {
|
function bulkAction($data = array()) {
|
||||||
try {
|
try {
|
||||||
$bulk_action = new Listing\BulkAction(
|
if(!isset($data['listing']['filter']['segment'])) {
|
||||||
'\MailPoet\Models\Subscriber',
|
$bulk_action = new Listing\BulkAction('\MailPoet\Models\Subscriber', $data);
|
||||||
$data
|
} else {
|
||||||
);
|
$bulk_action = new BulkAction($data);
|
||||||
$meta = $bulk_action->apply();
|
}
|
||||||
return $this->successResponse(null, $meta);
|
return $this->successResponse(null, $bulk_action->apply());
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
return $this->errorResponse(array(
|
return $this->errorResponse(array(
|
||||||
$e->getCode() => $e->getMessage()
|
$e->getCode() => $e->getMessage()
|
||||||
|
@ -184,7 +184,7 @@ class Model extends \Sudzy\ValidModel {
|
|||||||
return (int)$model['id'];
|
return (int)$model['id'];
|
||||||
}, $rows);
|
}, $rows);
|
||||||
|
|
||||||
if($callback !== false) {
|
if(is_callable($callback)) {
|
||||||
$callback($ids);
|
$callback($ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
57
lib/Segments/BulkAction.php
Normal file
57
lib/Segments/BulkAction.php
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
105
tests/unit/Segments/BulkActionTest.php
Normal file
105
tests/unit/Segments/BulkActionTest.php
Normal 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');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
11
tests/unit/Segments/SubscribersBulkActionHandlerMock.php
Normal file
11
tests/unit/Segments/SubscribersBulkActionHandlerMock.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Test\Segments;
|
||||||
|
|
||||||
|
class SubscribersBulkActionHandlerMock {
|
||||||
|
|
||||||
|
function apply() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user