Refactor mailpoet_subscribers_in_segment_apply_bulk_action_handlers to direct calls
[MAILPOET-3077]
This commit is contained in:
committed by
Veljko V
parent
a42b3d6e3f
commit
bf053edbfb
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace MailPoet\DynamicSegments;
|
namespace MailPoet\DynamicSegments;
|
||||||
|
|
||||||
use MailPoet\DynamicSegments\FreePluginConnectors\SubscribersBulkActionHandler;
|
|
||||||
use MailPoet\DynamicSegments\Mappers\DBMapper;
|
use MailPoet\DynamicSegments\Mappers\DBMapper;
|
||||||
use MailPoet\DynamicSegments\Persistence\Loading\SingleSegmentLoader;
|
use MailPoet\DynamicSegments\Persistence\Loading\SingleSegmentLoader;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
@ -16,22 +15,12 @@ class DynamicSegmentHooks {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function init() {
|
public function init() {
|
||||||
$this->wp->addAction(
|
|
||||||
'mailpoet_subscribers_in_segment_apply_bulk_action_handlers',
|
|
||||||
[$this, 'applySubscriberBulkAction']
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->wp->addAction(
|
$this->wp->addAction(
|
||||||
'mailpoet_get_segment_filters',
|
'mailpoet_get_segment_filters',
|
||||||
[$this, 'getSegmentFilters']
|
[$this, 'getSegmentFilters']
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function applySubscriberBulkAction(array $handlers) {
|
|
||||||
$handlers[] = new SubscribersBulkActionHandler();
|
|
||||||
return $handlers;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getSegmentFilters($segmentId) {
|
public function getSegmentFilters($segmentId) {
|
||||||
$singleSegmentLoader = new SingleSegmentLoader(new DBMapper());
|
$singleSegmentLoader = new SingleSegmentLoader(new DBMapper());
|
||||||
return $singleSegmentLoader->load($segmentId)->getFilters();
|
return $singleSegmentLoader->load($segmentId)->getFilters();
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace MailPoet\DynamicSegments\FreePluginConnectors;
|
|
||||||
|
|
||||||
use MailPoet\Listing\BulkActionController;
|
|
||||||
use MailPoet\Listing\BulkActionFactory;
|
|
||||||
use MailPoet\Listing\Handler;
|
|
||||||
use MailPoet\Models\DynamicSegment;
|
|
||||||
|
|
||||||
class SubscribersBulkActionHandler {
|
|
||||||
/**
|
|
||||||
* @param array $segment
|
|
||||||
* @param array $data
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function apply(array $segment, array $data) {
|
|
||||||
if ($segment['type'] === DynamicSegment::TYPE_DYNAMIC) {
|
|
||||||
$bulkAction = new BulkActionController(new BulkActionFactory(), new Handler());
|
|
||||||
return $bulkAction->apply('\MailPoet\Models\SubscribersInDynamicSegment', $data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -3,18 +3,20 @@
|
|||||||
namespace MailPoet\Segments;
|
namespace MailPoet\Segments;
|
||||||
|
|
||||||
use MailPoet\DI\ContainerWrapper;
|
use MailPoet\DI\ContainerWrapper;
|
||||||
use MailPoet\Listing\Handler;
|
use MailPoet\Entities\SegmentEntity;
|
||||||
|
use MailPoet\Listing\BulkActionController;
|
||||||
use MailPoet\Models\Segment;
|
use MailPoet\Models\Segment;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
|
||||||
|
|
||||||
class BulkAction {
|
class BulkAction {
|
||||||
|
/** @var BulkActionController */
|
||||||
|
private $actionsController;
|
||||||
|
|
||||||
private $data = null;
|
/** @var array */
|
||||||
private $wp;
|
private $data;
|
||||||
|
|
||||||
public function __construct($data) {
|
public function __construct(array $data) {
|
||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
$this->wp = new WPFunctions;
|
$this->actionsController = ContainerWrapper::getInstance()->get(BulkActionController::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,23 +41,13 @@ class BulkAction {
|
|||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
private function applySegment($segment) {
|
private function applySegment($segment) {
|
||||||
if (!$segment
|
if (is_bool($segment)
|
||||||
|| in_array($segment['type'], [Segment::TYPE_DEFAULT, Segment::TYPE_WP_USERS, Segment::TYPE_WC_USERS], true)
|
|| in_array($segment['type'], [SegmentEntity::TYPE_DEFAULT, SegmentEntity::TYPE_WP_USERS, SegmentEntity::TYPE_WC_USERS], true)
|
||||||
) {
|
) {
|
||||||
$bulkAction = new \MailPoet\Listing\BulkActionController(
|
return $this->actionsController->apply('\MailPoet\Models\Subscriber', $this->data);
|
||||||
ContainerWrapper::getInstance()->get(\MailPoet\Listing\BulkActionFactory::class),
|
} elseif (isset($segment['type']) && $segment['type'] === SegmentEntity::TYPE_DYNAMIC) {
|
||||||
new Handler()
|
return $this->actionsController->apply('\MailPoet\Models\SubscribersInDynamicSegment', $this->data);
|
||||||
);
|
|
||||||
return $bulkAction->apply('\MailPoet\Models\Subscriber', $this->data);
|
|
||||||
} else {
|
|
||||||
$handlers = $this->wp->applyFilters('mailpoet_subscribers_in_segment_apply_bulk_action_handlers', []);
|
|
||||||
foreach ($handlers as $handler) {
|
|
||||||
$meta = $handler->apply($segment, $this->data);
|
|
||||||
if ($meta) {
|
|
||||||
return $meta;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new \InvalidArgumentException('No handler found for segment');
|
|
||||||
}
|
}
|
||||||
|
throw new \InvalidArgumentException('No handler found for segment');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,48 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace MailPoet\DynamicSegments\FreePluginConnectors;
|
|
||||||
|
|
||||||
use MailPoet\Models\DynamicSegment;
|
|
||||||
use MailPoetVendor\Idiorm\ORM;
|
|
||||||
|
|
||||||
class SubscribersBulkActionHandlerTest extends \MailPoetTest {
|
|
||||||
public function testItReturnsNullWithUnknownSegment() {
|
|
||||||
$segment = [
|
|
||||||
'name' => 'name',
|
|
||||||
'description' => 'desc',
|
|
||||||
'type' => 'unknown',
|
|
||||||
];
|
|
||||||
$handler = new SubscribersBulkActionHandler();
|
|
||||||
$result = $handler->apply($segment, [
|
|
||||||
'listing' => ['filter' => ['segment' => 5]],
|
|
||||||
'action' => 'trash',
|
|
||||||
]);
|
|
||||||
expect($result)->null();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItReturnsDataForDynamicSegment() {
|
|
||||||
$segment = DynamicSegment::createOrUpdate([
|
|
||||||
'name' => 'name',
|
|
||||||
'description' => 'desc',
|
|
||||||
'type' => DynamicSegment::TYPE_DYNAMIC,
|
|
||||||
]);
|
|
||||||
$handler = new SubscribersBulkActionHandler();
|
|
||||||
$result = $handler->apply($segment->asArray(), [
|
|
||||||
'listing' => ['filter' => ['segment' => $segment->id()]],
|
|
||||||
'action' => 'trash',
|
|
||||||
]);
|
|
||||||
expect($result)->notNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function _before() {
|
|
||||||
$this->cleanData();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function _after() {
|
|
||||||
$this->cleanData();
|
|
||||||
}
|
|
||||||
|
|
||||||
private function cleanData() {
|
|
||||||
ORM::raw_execute('TRUNCATE ' . DynamicSegment::$_table);
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,13 +2,10 @@
|
|||||||
|
|
||||||
namespace MailPoet\Segments;
|
namespace MailPoet\Segments;
|
||||||
|
|
||||||
use Codeception\Util\Stub;
|
|
||||||
use MailPoet\Models\Segment;
|
use MailPoet\Models\Segment;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
use MailPoet\Models\SubscriberSegment;
|
use MailPoet\Models\SubscriberSegment;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
|
||||||
use MailPoetVendor\Idiorm\ORM;
|
use MailPoetVendor\Idiorm\ORM;
|
||||||
use PHPUnit\Framework\MockObject\MockObject;
|
|
||||||
|
|
||||||
require_once('SubscribersBulkActionHandlerMock.php');
|
require_once('SubscribersBulkActionHandlerMock.php');
|
||||||
|
|
||||||
@ -88,25 +85,4 @@ class BulkActionTest extends \MailPoetTest {
|
|||||||
remove_all_filters('mailpoet_subscribers_in_segment_apply_bulk_action_handlers');
|
remove_all_filters('mailpoet_subscribers_in_segment_apply_bulk_action_handlers');
|
||||||
$handler->apply();
|
$handler->apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testBulkActionUsingFilter() {
|
|
||||||
/** @var MockObject $mock */
|
|
||||||
$mock = Stub::makeEmpty('\MailPoet\Test\Segments\SubscribersBulkActionHandlerMock', ['apply']);
|
|
||||||
$mock
|
|
||||||
->expects($this->once())
|
|
||||||
->method('apply')
|
|
||||||
->will($this->returnValue('result'));
|
|
||||||
|
|
||||||
remove_all_filters('mailpoet_subscribers_in_segment_apply_bulk_action_handlers');
|
|
||||||
(new WPFunctions)->addFilter('mailpoet_subscribers_in_segment_apply_bulk_action_handlers', function () use ($mock) {
|
|
||||||
return [$mock];
|
|
||||||
});
|
|
||||||
|
|
||||||
$handler = new BulkAction([
|
|
||||||
'listing' => ['filter' => ['segment' => $this->segment2->id]],
|
|
||||||
'action' => 'trash',
|
|
||||||
]);
|
|
||||||
$result = $handler->apply();
|
|
||||||
expect($result)->equals('result');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user