Use only Listing repository in subscribers listing API

[MAILPOET-3077]
This commit is contained in:
Rostislav Wolny
2020-09-22 15:13:45 +02:00
committed by Veljko V
parent a98f627dbf
commit c952ef9542
6 changed files with 15 additions and 225 deletions

View File

@ -19,7 +19,6 @@ use MailPoet\Models\Subscriber;
use MailPoet\Models\SubscriberSegment;
use MailPoet\Newsletter\Scheduler\WelcomeScheduler;
use MailPoet\Segments\BulkAction;
use MailPoet\Segments\SubscribersListings;
use MailPoet\Settings\SettingsController;
use MailPoet\Statistics\Track\Unsubscribes;
use MailPoet\Subscribers\ConfirmationEmailMailer;
@ -45,8 +44,6 @@ class Subscribers extends APIEndpoint {
/** @var Listing\BulkActionController */
private $bulkActionController;
/** @var SubscribersListings */
private $subscribersListings;
/** @var SubscriberActions */
private $subscriberActions;
@ -95,7 +92,6 @@ class Subscribers extends APIEndpoint {
public function __construct(
Listing\BulkActionController $bulkActionController,
SubscribersListings $subscribersListings,
SubscriberActions $subscriberActions,
RequiredCustomFieldValidator $requiredCustomFieldValidator,
Listing\Handler $listingHandler,
@ -113,7 +109,6 @@ class Subscribers extends APIEndpoint {
AddToSubscribersFilters $dynamicSegmentsFiltersLoader
) {
$this->bulkActionController = $bulkActionController;
$this->subscribersListings = $subscribersListings;
$this->subscriberActions = $subscriberActions;
$this->requiredCustomFieldValidator = $requiredCustomFieldValidator;
$this->listingHandler = $listingHandler;
@ -143,43 +138,23 @@ class Subscribers extends APIEndpoint {
}
public function listing($data = []) {
if (!isset($data['filter']['segment'])) {
$definition = $this->listingHandler->getListingDefinition($data);
$items = $this->subscriberListingRepository->getData($definition);
$count = $this->subscriberListingRepository->getCount($definition);
$filters = $this->subscriberListingRepository->getFilters($definition);
$groups = $this->subscriberListingRepository->getGroups($definition);
$filters['segment'] = $this->dynamicSegmentsFiltersLoader->add($filters['segment'] ?? []);
return $this->successResponse($this->subscribersResponseBuilder->buildForListing($items), [
$subscribers = $this->subscribersResponseBuilder->buildForListing($items);
if ($data['filter']['segment'] ?? false) {
foreach ($subscribers as $key => $subscriber) {
$subscribers[$key] = $this->preferUnsubscribedStatusFromSegment($subscriber, $data['filter']['segment']);
}
}
return $this->successResponse($subscribers, [
'count' => $count,
'filters' => $filters,
'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'],
]
);
}
}
private function preferUnsubscribedStatusFromSegment(array $subscriber, $segmentId) {

View File

@ -251,7 +251,6 @@ class ContainerConfigurator implements IContainerConfigurator {
$container->autowire(\MailPoet\Subscribers\SubscriberCustomFieldRepository::class);
$container->autowire(\MailPoet\Subscribers\Statistics\SubscriberStatisticsRepository::class);
// Segments
$container->autowire(\MailPoet\Segments\SubscribersListings::class)->setPublic(true);
$container->autowire(\MailPoet\Segments\WooCommerce::class)->setPublic(true);
$container->autowire(\MailPoet\Segments\SubscribersFinder::class);
$container->autowire(\MailPoet\Segments\SegmentsRepository::class);

View File

@ -790,46 +790,6 @@ class Subscriber extends Model {
->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.
* @see https://kb.mailpoet.com/article/195-add-subscribers-through-your-own-form-or-plugin

View File

@ -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');
}
}

View File

@ -30,7 +30,6 @@ use MailPoet\Models\SendingQueue;
use MailPoet\Models\Subscriber;
use MailPoet\Models\SubscriberIP;
use MailPoet\Models\SubscriberSegment;
use MailPoet\Segments\SubscribersListings;
use MailPoet\Settings\SettingsController;
use MailPoet\Settings\SettingsRepository;
use MailPoet\Statistics\Track\Unsubscribes;
@ -88,7 +87,6 @@ class SubscribersTest extends \MailPoetTest {
$obfuscator = new FieldNameObfuscator($wp);
$this->endpoint = new Subscribers(
$container->get(BulkActionController::class),
$container->get(SubscribersListings::class),
$container->get(SubscriberActions::class),
$container->get(RequiredCustomFieldValidator::class),
$container->get(Handler::class),

View File

@ -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]]);
}
}