Refactor mailpoet_get_segment_filters to direct calls
[MAILPOET-3077]
This commit is contained in:
committed by
Veljko V
parent
bf053edbfb
commit
814686e8d2
@ -2,7 +2,6 @@
|
||||
|
||||
namespace MailPoet\Config;
|
||||
|
||||
use MailPoet\DynamicSegments\DynamicSegmentHooks;
|
||||
use MailPoet\Form\DisplayFormInWPContent;
|
||||
use MailPoet\Mailer\WordPress\WordpressMailerReplacer;
|
||||
use MailPoet\Newsletter\Scheduler\PostNotificationScheduler;
|
||||
@ -55,9 +54,6 @@ class Hooks {
|
||||
/** @var WordpressMailerReplacer */
|
||||
private $wordpressMailerReplacer;
|
||||
|
||||
/** @var DynamicSegmentHooks */
|
||||
private $dynamicSegmentHooks;
|
||||
|
||||
/** @var DisplayFormInWPContent */
|
||||
private $displayFormInWPContent;
|
||||
|
||||
@ -74,8 +70,7 @@ class Hooks {
|
||||
WooCommercePurchases $woocommercePurchases,
|
||||
PostNotificationScheduler $postNotificationScheduler,
|
||||
WordpressMailerReplacer $wordpressMailerReplacer,
|
||||
DisplayFormInWPContent $displayFormInWPContent,
|
||||
DynamicSegmentHooks $dynamicSegmentHooks
|
||||
DisplayFormInWPContent $displayFormInWPContent
|
||||
) {
|
||||
$this->subscriptionForm = $subscriptionForm;
|
||||
$this->subscriptionComment = $subscriptionComment;
|
||||
@ -89,7 +84,6 @@ class Hooks {
|
||||
$this->woocommercePurchases = $woocommercePurchases;
|
||||
$this->postNotificationScheduler = $postNotificationScheduler;
|
||||
$this->wordpressMailerReplacer = $wordpressMailerReplacer;
|
||||
$this->dynamicSegmentHooks = $dynamicSegmentHooks;
|
||||
$this->displayFormInWPContent = $displayFormInWPContent;
|
||||
}
|
||||
|
||||
@ -103,7 +97,6 @@ class Hooks {
|
||||
$this->setupWooCommerceSubscriptionEvents();
|
||||
$this->setupPostNotifications();
|
||||
$this->setupWooCommerceSettings();
|
||||
$this->dynamicSegmentHooks->init();
|
||||
$this->setupFooter();
|
||||
}
|
||||
|
||||
|
@ -130,7 +130,6 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
$container->autowire(\MailPoet\PostEditorBlocks\PostEditorBlock::class);
|
||||
$container->autowire(\MailPoet\PostEditorBlocks\SubscriptionFormBlock::class);
|
||||
// Dynamic segments
|
||||
$container->autowire(\MailPoet\DynamicSegments\DynamicSegmentHooks::class);
|
||||
$container->autowire(\MailPoet\DynamicSegments\FreePluginConnectors\AddToNewslettersSegments::class);
|
||||
$container->autowire(\MailPoet\DynamicSegments\FreePluginConnectors\SendingNewslettersSubscribersFinder::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\DynamicSegments\FreePluginConnectors\AddToSubscribersFilters::class);
|
||||
|
@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\DynamicSegments;
|
||||
|
||||
use MailPoet\DynamicSegments\Mappers\DBMapper;
|
||||
use MailPoet\DynamicSegments\Persistence\Loading\SingleSegmentLoader;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class DynamicSegmentHooks {
|
||||
/** @var WPFunctions */
|
||||
private $wp;
|
||||
|
||||
public function __construct(WPFunctions $wp) {
|
||||
$this->wp = $wp;
|
||||
}
|
||||
|
||||
public function init() {
|
||||
$this->wp->addAction(
|
||||
'mailpoet_get_segment_filters',
|
||||
[$this, 'getSegmentFilters']
|
||||
);
|
||||
}
|
||||
|
||||
public function getSegmentFilters($segmentId) {
|
||||
$singleSegmentLoader = new SingleSegmentLoader(new DBMapper());
|
||||
return $singleSegmentLoader->load($segmentId)->getFilters();
|
||||
}
|
||||
}
|
@ -2,9 +2,10 @@
|
||||
|
||||
namespace MailPoet\Subscribers\ImportExport\Export;
|
||||
|
||||
use MailPoet\DI\ContainerWrapper;
|
||||
use MailPoet\DynamicSegments\Persistence\Loading\SingleSegmentLoader;
|
||||
use MailPoet\Models\Segment;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
/**
|
||||
* Gets batches of subscribers from dynamic segments.
|
||||
@ -13,15 +14,15 @@ class DynamicSubscribersGetter extends SubscribersGetter {
|
||||
|
||||
protected $segmentIndex = 0;
|
||||
|
||||
/** @var WPFunctions */
|
||||
private $wp;
|
||||
/** @var SingleSegmentLoader */
|
||||
private $dynamicSegmentsLoader;
|
||||
|
||||
public function __construct($segmentsIds, $batchSize, WPFunctions $wp = null) {
|
||||
public function __construct($segmentsIds, $batchSize, SingleSegmentLoader $dynamicSegmentsLoader = null) {
|
||||
parent::__construct($segmentsIds, $batchSize);
|
||||
if ($wp == null) {
|
||||
$wp = new WPFunctions;
|
||||
if ($dynamicSegmentsLoader === null) {
|
||||
$dynamicSegmentsLoader = ContainerWrapper::getInstance()->get(SingleSegmentLoader::class);
|
||||
}
|
||||
$this->wp = $wp;
|
||||
$this->dynamicSegmentsLoader = $dynamicSegmentsLoader;
|
||||
}
|
||||
|
||||
public function reset() {
|
||||
@ -32,10 +33,7 @@ class DynamicSubscribersGetter extends SubscribersGetter {
|
||||
protected function filter($subscribers) {
|
||||
$segmentId = $this->segmentsIds[$this->segmentIndex];
|
||||
|
||||
$filters = $this->wp->applyFilters(
|
||||
'mailpoet_get_segment_filters',
|
||||
$segmentId
|
||||
);
|
||||
$filters = $this->dynamicSegmentsLoader->load($segmentId)->getFilters();
|
||||
|
||||
if (!is_array($filters) || empty($filters)) {
|
||||
return [];
|
||||
|
@ -2,13 +2,15 @@
|
||||
|
||||
namespace MailPoet\Test\Subscribers\ImportExport\Export;
|
||||
|
||||
use MailPoet\DynamicSegments\Persistence\Loading\SingleSegmentLoader;
|
||||
use MailPoet\Models\CustomField;
|
||||
use MailPoet\Models\DynamicSegment;
|
||||
use MailPoet\Models\Segment;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Models\SubscriberCustomField;
|
||||
use MailPoet\Subscribers\ImportExport\Export\DynamicSubscribersGetter;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
use MailPoetVendor\Idiorm\ORM;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
|
||||
class DynamicSubscribersGetterTest extends \MailPoetTest {
|
||||
public $segmentsData;
|
||||
@ -16,6 +18,9 @@ class DynamicSubscribersGetterTest extends \MailPoetTest {
|
||||
public $subscribersData;
|
||||
public $subscriberFields;
|
||||
|
||||
/** @var SingleSegmentLoader & MockObject */
|
||||
private $singleSegmentLoader;
|
||||
|
||||
public function _before() {
|
||||
parent::_before();
|
||||
$this->subscriberFields = [
|
||||
@ -92,19 +97,19 @@ class DynamicSubscribersGetterTest extends \MailPoetTest {
|
||||
$entity->customFieldId = 1;
|
||||
$entity->value = $this->subscribersData[1][1];
|
||||
$entity->save();
|
||||
$wp = new WPFunctions;
|
||||
$wp->removeAllFilters('mailpoet_get_segment_filters');
|
||||
$wp->addAction(
|
||||
'mailpoet_get_segment_filters',
|
||||
function($segmentId) {
|
||||
|
||||
$this->singleSegmentLoader = $this->createMock(SingleSegmentLoader::class);
|
||||
$this->singleSegmentLoader->method('load')
|
||||
->willReturnCallback(function($segmentId) {
|
||||
$segment = DynamicSegment::create();
|
||||
$segment->name = 'Dynamic';
|
||||
if ($segmentId == 1) {
|
||||
return [new \DynamicSegmentFilter([1, 2])];
|
||||
$segment->setFilters([new \DynamicSegmentFilter([1, 2])]);
|
||||
} else if ($segmentId == 2) {
|
||||
return [new \DynamicSegmentFilter([1, 3, 4])];
|
||||
$segment->setFilters([new \DynamicSegmentFilter([1, 3, 4])]);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
);
|
||||
return $segment;
|
||||
});
|
||||
}
|
||||
|
||||
protected function filterSubscribersData($subscribers) {
|
||||
@ -122,7 +127,7 @@ class DynamicSubscribersGetterTest extends \MailPoetTest {
|
||||
}
|
||||
|
||||
public function testItGetsSubscribersInOneSegment() {
|
||||
$getter = new DynamicSubscribersGetter([1], 10);
|
||||
$getter = new DynamicSubscribersGetter([1], 10, $this->singleSegmentLoader);
|
||||
$subscribers = $getter->get();
|
||||
expect($this->filterSubscribersData($subscribers))->equals([
|
||||
[
|
||||
@ -151,7 +156,7 @@ class DynamicSubscribersGetterTest extends \MailPoetTest {
|
||||
}
|
||||
|
||||
public function testItGetsSubscribersInMultipleSegments() {
|
||||
$getter = new DynamicSubscribersGetter([1, 2], 10);
|
||||
$getter = new DynamicSubscribersGetter([1, 2], 10, $this->singleSegmentLoader);
|
||||
expect($this->filterSubscribersData($getter->get()))->equals([
|
||||
[
|
||||
'first_name' => 'Adam',
|
||||
@ -212,7 +217,7 @@ class DynamicSubscribersGetterTest extends \MailPoetTest {
|
||||
}
|
||||
|
||||
public function testItGetsSubscribersInBatches() {
|
||||
$getter = new DynamicSubscribersGetter([1, 2], 2);
|
||||
$getter = new DynamicSubscribersGetter([1, 2], 2, $this->singleSegmentLoader);
|
||||
expect($this->filterSubscribersData($getter->get()))->equals([
|
||||
[
|
||||
'first_name' => 'Adam',
|
||||
|
@ -1,7 +1,9 @@
|
||||
<?php
|
||||
|
||||
use Codeception\Util\Fixtures;
|
||||
use MailPoet\DynamicSegments\Filters\Filter;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoetVendor\Idiorm\ORM;
|
||||
|
||||
$newsletterBodyText =
|
||||
|
||||
@ -171,14 +173,18 @@ Fixtures::add(
|
||||
* Simple class mocking dynamic segment filter.
|
||||
*/
|
||||
// phpcs:ignore PSR1.Classes.ClassDeclaration, Squiz.Classes.ClassFileName
|
||||
class DynamicSegmentFilter {
|
||||
class DynamicSegmentFilter implements Filter {
|
||||
protected $ids;
|
||||
|
||||
public function __construct($ids) {
|
||||
$this->ids = $ids;
|
||||
}
|
||||
|
||||
public function toSql($orm) {
|
||||
public function toSql(ORM $orm) {
|
||||
return $orm->whereIn(Subscriber::$_table . '.id', $this->ids);
|
||||
}
|
||||
|
||||
public function toArray() {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user