Use only Listing repository in subscribers listing API
[MAILPOET-3077]
This commit is contained in:
committed by
Veljko V
parent
a98f627dbf
commit
c952ef9542
@ -19,7 +19,6 @@ use MailPoet\Models\Subscriber;
|
|||||||
use MailPoet\Models\SubscriberSegment;
|
use MailPoet\Models\SubscriberSegment;
|
||||||
use MailPoet\Newsletter\Scheduler\WelcomeScheduler;
|
use MailPoet\Newsletter\Scheduler\WelcomeScheduler;
|
||||||
use MailPoet\Segments\BulkAction;
|
use MailPoet\Segments\BulkAction;
|
||||||
use MailPoet\Segments\SubscribersListings;
|
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
use MailPoet\Statistics\Track\Unsubscribes;
|
use MailPoet\Statistics\Track\Unsubscribes;
|
||||||
use MailPoet\Subscribers\ConfirmationEmailMailer;
|
use MailPoet\Subscribers\ConfirmationEmailMailer;
|
||||||
@ -45,8 +44,6 @@ class Subscribers extends APIEndpoint {
|
|||||||
/** @var Listing\BulkActionController */
|
/** @var Listing\BulkActionController */
|
||||||
private $bulkActionController;
|
private $bulkActionController;
|
||||||
|
|
||||||
/** @var SubscribersListings */
|
|
||||||
private $subscribersListings;
|
|
||||||
|
|
||||||
/** @var SubscriberActions */
|
/** @var SubscriberActions */
|
||||||
private $subscriberActions;
|
private $subscriberActions;
|
||||||
@ -95,7 +92,6 @@ class Subscribers extends APIEndpoint {
|
|||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
Listing\BulkActionController $bulkActionController,
|
Listing\BulkActionController $bulkActionController,
|
||||||
SubscribersListings $subscribersListings,
|
|
||||||
SubscriberActions $subscriberActions,
|
SubscriberActions $subscriberActions,
|
||||||
RequiredCustomFieldValidator $requiredCustomFieldValidator,
|
RequiredCustomFieldValidator $requiredCustomFieldValidator,
|
||||||
Listing\Handler $listingHandler,
|
Listing\Handler $listingHandler,
|
||||||
@ -113,7 +109,6 @@ class Subscribers extends APIEndpoint {
|
|||||||
AddToSubscribersFilters $dynamicSegmentsFiltersLoader
|
AddToSubscribersFilters $dynamicSegmentsFiltersLoader
|
||||||
) {
|
) {
|
||||||
$this->bulkActionController = $bulkActionController;
|
$this->bulkActionController = $bulkActionController;
|
||||||
$this->subscribersListings = $subscribersListings;
|
|
||||||
$this->subscriberActions = $subscriberActions;
|
$this->subscriberActions = $subscriberActions;
|
||||||
$this->requiredCustomFieldValidator = $requiredCustomFieldValidator;
|
$this->requiredCustomFieldValidator = $requiredCustomFieldValidator;
|
||||||
$this->listingHandler = $listingHandler;
|
$this->listingHandler = $listingHandler;
|
||||||
@ -143,43 +138,23 @@ class Subscribers extends APIEndpoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function listing($data = []) {
|
public function listing($data = []) {
|
||||||
if (!isset($data['filter']['segment'])) {
|
$definition = $this->listingHandler->getListingDefinition($data);
|
||||||
$definition = $this->listingHandler->getListingDefinition($data);
|
$items = $this->subscriberListingRepository->getData($definition);
|
||||||
$items = $this->subscriberListingRepository->getData($definition);
|
$count = $this->subscriberListingRepository->getCount($definition);
|
||||||
$count = $this->subscriberListingRepository->getCount($definition);
|
$filters = $this->subscriberListingRepository->getFilters($definition);
|
||||||
$filters = $this->subscriberListingRepository->getFilters($definition);
|
$groups = $this->subscriberListingRepository->getGroups($definition);
|
||||||
$groups = $this->subscriberListingRepository->getGroups($definition);
|
$filters['segment'] = $this->dynamicSegmentsFiltersLoader->add($filters['segment'] ?? []);
|
||||||
$filters['segment'] = $this->dynamicSegmentsFiltersLoader->add($filters['segment'] ?? []);
|
$subscribers = $this->subscribersResponseBuilder->buildForListing($items);
|
||||||
return $this->successResponse($this->subscribersResponseBuilder->buildForListing($items), [
|
if ($data['filter']['segment'] ?? false) {
|
||||||
'count' => $count,
|
foreach ($subscribers as $key => $subscriber) {
|
||||||
'filters' => $filters,
|
$subscribers[$key] = $this->preferUnsubscribedStatusFromSegment($subscriber, $data['filter']['segment']);
|
||||||
'groups' => $groups,
|
|
||||||
]);
|
|
||||||
} else {
|
|
||||||
// this branch is here temporarily until we refactor dynamic segments to doctrine [MAILPOET-3077]
|
|
||||||
$listingData = $this->subscribersListings->getListingsInSegment($data);
|
|
||||||
|
|
||||||
$result = [];
|
|
||||||
foreach ($listingData['items'] as $subscriber) {
|
|
||||||
$subscriberResult = $subscriber
|
|
||||||
->withSubscriptions()
|
|
||||||
->asArray();
|
|
||||||
if (isset($data['filter']['segment'])) {
|
|
||||||
$subscriberResult = $this->preferUnsubscribedStatusFromSegment($subscriberResult, $data['filter']['segment']);
|
|
||||||
}
|
|
||||||
$result[] = $subscriberResult;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$listingData['filters']['segment'] = $this->dynamicSegmentsFiltersLoader->add($listingData['filters']['segment'] ?? []);
|
|
||||||
|
|
||||||
return $this->successResponse(
|
|
||||||
$result, [
|
|
||||||
'count' => $listingData['count'],
|
|
||||||
'filters' => $listingData['filters'],
|
|
||||||
'groups' => $listingData['groups'],
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
return $this->successResponse($subscribers, [
|
||||||
|
'count' => $count,
|
||||||
|
'filters' => $filters,
|
||||||
|
'groups' => $groups,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function preferUnsubscribedStatusFromSegment(array $subscriber, $segmentId) {
|
private function preferUnsubscribedStatusFromSegment(array $subscriber, $segmentId) {
|
||||||
|
@ -251,7 +251,6 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\Subscribers\SubscriberCustomFieldRepository::class);
|
$container->autowire(\MailPoet\Subscribers\SubscriberCustomFieldRepository::class);
|
||||||
$container->autowire(\MailPoet\Subscribers\Statistics\SubscriberStatisticsRepository::class);
|
$container->autowire(\MailPoet\Subscribers\Statistics\SubscriberStatisticsRepository::class);
|
||||||
// Segments
|
// Segments
|
||||||
$container->autowire(\MailPoet\Segments\SubscribersListings::class)->setPublic(true);
|
|
||||||
$container->autowire(\MailPoet\Segments\WooCommerce::class)->setPublic(true);
|
$container->autowire(\MailPoet\Segments\WooCommerce::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Segments\SubscribersFinder::class);
|
$container->autowire(\MailPoet\Segments\SubscribersFinder::class);
|
||||||
$container->autowire(\MailPoet\Segments\SegmentsRepository::class);
|
$container->autowire(\MailPoet\Segments\SegmentsRepository::class);
|
||||||
|
@ -790,46 +790,6 @@ class Subscriber extends Model {
|
|||||||
->findArray();
|
->findArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function groups($data) {
|
|
||||||
return [
|
|
||||||
[
|
|
||||||
'name' => 'all',
|
|
||||||
'label' => WPFunctions::get()->__('All', 'mailpoet'),
|
|
||||||
'count' => self::getPublished()->count(),
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'name' => self::STATUS_SUBSCRIBED,
|
|
||||||
'label' => WPFunctions::get()->__('Subscribed', 'mailpoet'),
|
|
||||||
'count' => self::filter(self::STATUS_SUBSCRIBED)->count(),
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'name' => self::STATUS_UNCONFIRMED,
|
|
||||||
'label' => WPFunctions::get()->__('Unconfirmed', 'mailpoet'),
|
|
||||||
'count' => self::filter(self::STATUS_UNCONFIRMED)->count(),
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'name' => self::STATUS_UNSUBSCRIBED,
|
|
||||||
'label' => WPFunctions::get()->__('Unsubscribed', 'mailpoet'),
|
|
||||||
'count' => self::filter(self::STATUS_UNSUBSCRIBED)->count(),
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'name' => self::STATUS_INACTIVE,
|
|
||||||
'label' => WPFunctions::get()->__('Inactive', 'mailpoet'),
|
|
||||||
'count' => self::filter(self::STATUS_INACTIVE)->count(),
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'name' => self::STATUS_BOUNCED,
|
|
||||||
'label' => WPFunctions::get()->__('Bounced', 'mailpoet'),
|
|
||||||
'count' => self::filter(self::STATUS_BOUNCED)->count(),
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'name' => 'trash',
|
|
||||||
'label' => WPFunctions::get()->__('Trash', 'mailpoet'),
|
|
||||||
'count' => self::getTrashed()->count(),
|
|
||||||
],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is here only for BC fix of 3rd party plugin integration.
|
* This method is here only for BC fix of 3rd party plugin integration.
|
||||||
* @see https://kb.mailpoet.com/article/195-add-subscribers-through-your-own-form-or-plugin
|
* @see https://kb.mailpoet.com/article/195-add-subscribers-through-your-own-form-or-plugin
|
||||||
|
@ -1,38 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace MailPoet\Segments;
|
|
||||||
|
|
||||||
use MailPoet\Entities\SegmentEntity;
|
|
||||||
use MailPoet\Listing\Handler;
|
|
||||||
use MailPoet\Models\Segment;
|
|
||||||
|
|
||||||
class SubscribersListings {
|
|
||||||
|
|
||||||
/** @var Handler */
|
|
||||||
private $handler;
|
|
||||||
|
|
||||||
public function __construct(Handler $handler) {
|
|
||||||
$this->handler = $handler;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getListingsInSegment($data) {
|
|
||||||
if (!isset($data['filter']['segment'])) {
|
|
||||||
throw new \InvalidArgumentException('Missing segment id');
|
|
||||||
}
|
|
||||||
$segment = Segment::findOne($data['filter']['segment']);
|
|
||||||
return $this->getListings($data, $segment instanceof Segment ? $segment : null);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getListings($data, Segment $segment = null) {
|
|
||||||
if (!$segment
|
|
||||||
|| in_array($segment->type, [SegmentEntity::TYPE_DEFAULT, SegmentEntity::TYPE_WP_USERS, SegmentEntity::TYPE_WC_USERS], true)
|
|
||||||
) {
|
|
||||||
return $listingData = $this->handler->get('\MailPoet\Models\Subscriber', $data);
|
|
||||||
}
|
|
||||||
if ($segment->type === SegmentEntity::TYPE_DYNAMIC) {
|
|
||||||
return $this->handler->get('\MailPoet\Models\SubscribersInDynamicSegment', $data);
|
|
||||||
}
|
|
||||||
throw new \InvalidArgumentException('No handler found for segment');
|
|
||||||
}
|
|
||||||
}
|
|
@ -30,7 +30,6 @@ use MailPoet\Models\SendingQueue;
|
|||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
use MailPoet\Models\SubscriberIP;
|
use MailPoet\Models\SubscriberIP;
|
||||||
use MailPoet\Models\SubscriberSegment;
|
use MailPoet\Models\SubscriberSegment;
|
||||||
use MailPoet\Segments\SubscribersListings;
|
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
use MailPoet\Settings\SettingsRepository;
|
use MailPoet\Settings\SettingsRepository;
|
||||||
use MailPoet\Statistics\Track\Unsubscribes;
|
use MailPoet\Statistics\Track\Unsubscribes;
|
||||||
@ -88,7 +87,6 @@ class SubscribersTest extends \MailPoetTest {
|
|||||||
$obfuscator = new FieldNameObfuscator($wp);
|
$obfuscator = new FieldNameObfuscator($wp);
|
||||||
$this->endpoint = new Subscribers(
|
$this->endpoint = new Subscribers(
|
||||||
$container->get(BulkActionController::class),
|
$container->get(BulkActionController::class),
|
||||||
$container->get(SubscribersListings::class),
|
|
||||||
$container->get(SubscriberActions::class),
|
$container->get(SubscriberActions::class),
|
||||||
$container->get(RequiredCustomFieldValidator::class),
|
$container->get(RequiredCustomFieldValidator::class),
|
||||||
$container->get(Handler::class),
|
$container->get(Handler::class),
|
||||||
|
@ -1,104 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace MailPoet\Segments;
|
|
||||||
|
|
||||||
require_once(ABSPATH . 'wp-admin/includes/user.php');
|
|
||||||
|
|
||||||
use MailPoet\DI\ContainerWrapper;
|
|
||||||
use MailPoet\Models\DynamicSegmentFilter;
|
|
||||||
use MailPoet\Models\Segment;
|
|
||||||
use MailPoet\Models\Subscriber;
|
|
||||||
use MailPoet\Models\SubscriberSegment;
|
|
||||||
use MailPoet\Test\DataFactories\DynamicSegment;
|
|
||||||
use MailPoetVendor\Idiorm\ORM;
|
|
||||||
|
|
||||||
class SubscribersListingsTest extends \MailPoetTest {
|
|
||||||
public $subscriber2;
|
|
||||||
public $subscriber1;
|
|
||||||
public $segment2;
|
|
||||||
public $segment1;
|
|
||||||
public $dynamicSegment;
|
|
||||||
|
|
||||||
/** @var SubscribersListings */
|
|
||||||
private $finder;
|
|
||||||
|
|
||||||
public function _before() {
|
|
||||||
parent::_before();
|
|
||||||
$this->finder = ContainerWrapper::getInstance()->get(SubscribersListings::class);
|
|
||||||
$this->cleanData();
|
|
||||||
wp_insert_user([
|
|
||||||
'user_login' => 'user-role-test1',
|
|
||||||
'user_email' => 'user-role-test1@example.com',
|
|
||||||
'role' => 'editor',
|
|
||||||
'user_pass' => '12123154',
|
|
||||||
]);
|
|
||||||
$this->segment1 = Segment::createOrUpdate(['name' => 'Segment 1', 'type' => 'default']);
|
|
||||||
$this->segment2 = Segment::createOrUpdate(['name' => 'Segment 3', 'type' => 'not default']);
|
|
||||||
$dynamicSegmentFactory = new DynamicSegment();
|
|
||||||
$this->dynamicSegment = $dynamicSegmentFactory
|
|
||||||
->withName('Dynamic')
|
|
||||||
->withUserRoleFilter('editor')
|
|
||||||
->create();
|
|
||||||
$this->subscriber1 = Subscriber::createOrUpdate([
|
|
||||||
'email' => 'john@mailpoet.com',
|
|
||||||
'first_name' => 'John',
|
|
||||||
'last_name' => 'Doe',
|
|
||||||
'status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'segments' => [
|
|
||||||
$this->segment1->id,
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
$this->subscriber2 = Subscriber::createOrUpdate([
|
|
||||||
'email' => 'jake@mailpoet.com',
|
|
||||||
'first_name' => 'Jake',
|
|
||||||
'last_name' => 'Doe',
|
|
||||||
'status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'segments' => [
|
|
||||||
$this->segment2->id,
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
SubscriberSegment::resubscribeToAllSegments($this->subscriber1);
|
|
||||||
SubscriberSegment::resubscribeToAllSegments($this->subscriber2);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function _after() {
|
|
||||||
$this->cleanData();
|
|
||||||
}
|
|
||||||
|
|
||||||
private function cleanData() {
|
|
||||||
ORM::raw_execute('TRUNCATE ' . Segment::$_table);
|
|
||||||
ORM::raw_execute('TRUNCATE ' . SubscriberSegment::$_table);
|
|
||||||
ORM::raw_execute('TRUNCATE ' . DynamicSegmentFilter::$_table);
|
|
||||||
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
|
||||||
$user = get_user_by('email', 'user-role-test1@example.com');
|
|
||||||
if ($user) {
|
|
||||||
\wp_delete_user($user->ID);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testTryToGetListingsWithoutPassingSegment() {
|
|
||||||
$this->expectException('InvalidArgumentException');
|
|
||||||
$this->finder->getListingsInSegment([]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetListingsForDefaultSegment() {
|
|
||||||
$listings = $this->finder->getListingsInSegment(['filter' => ['segment' => $this->segment1->id]]);
|
|
||||||
expect($listings['items'])->count(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetListingsForNonExistingSegmen() {
|
|
||||||
$listings = $this->finder->getListingsInSegment(['filter' => ['segment' => 'non-existing-id']]);
|
|
||||||
expect($listings['items'])->notEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetListingsForDynamicSegment() {
|
|
||||||
$listings = $this->finder->getListingsInSegment(['filter' => ['segment' => $this->dynamicSegment->id]]);
|
|
||||||
expect($listings['items'])->count(1);
|
|
||||||
expect($listings['items'][0]->email)->equals('user-role-test1@example.com');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testTryToGetListingsForSegmentWithoutHandler() {
|
|
||||||
$this->expectException('InvalidArgumentException');
|
|
||||||
$this->finder->getListingsInSegment(['filter' => ['segment' => $this->segment2->id]]);
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user