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