Load subscribers from dynamic segments
[PREMIUM-38]
This commit is contained in:
@ -10,7 +10,9 @@ use MailPoet\Form\Util\FieldNameObfuscator;
|
||||
use MailPoet\Models\Form;
|
||||
use MailPoet\Models\StatisticsForms;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Segments\SubscribersListings;
|
||||
use MailPoet\Subscription\Throttling as SubscriptionThrottling;
|
||||
use MailPoet\WP\Hooks;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
@ -40,12 +42,15 @@ class Subscribers extends APIEndpoint {
|
||||
}
|
||||
|
||||
function listing($data = array()) {
|
||||
$listing = new Listing\Handler(
|
||||
'\MailPoet\Models\Subscriber',
|
||||
$data
|
||||
);
|
||||
|
||||
if(!isset($data['filter']['segment'])) {
|
||||
$listing = new Listing\Handler('\MailPoet\Models\Subscriber', $data);
|
||||
|
||||
$listing_data = $listing->get();
|
||||
} else {
|
||||
$listings = new SubscribersListings();
|
||||
$listing_data = $listings->getListingsInSegment($data);
|
||||
}
|
||||
|
||||
$data = array();
|
||||
foreach($listing_data['items'] as $subscriber) {
|
||||
|
39
lib/Segments/SubscribersListings.php
Normal file
39
lib/Segments/SubscribersListings.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Segments;
|
||||
|
||||
use MailPoet\Listing\Handler;
|
||||
use MailPoet\Models\Segment;
|
||||
use MailPoet\WP\Hooks;
|
||||
|
||||
class SubscribersListings {
|
||||
|
||||
function getListingsInSegment($data) {
|
||||
if(!isset($data['filter']['segment'])) {
|
||||
throw new \InvalidArgumentException('Missing segment id');
|
||||
}
|
||||
$segment = Segment::find_one($data['filter']['segment']);
|
||||
if($segment) {
|
||||
$segment = $segment->asArray();
|
||||
}
|
||||
return $this->getListings($segment, $data);
|
||||
|
||||
}
|
||||
|
||||
private function getListings($segment, $data) {
|
||||
if(!$segment || $segment['type'] === Segment::TYPE_DEFAULT || $segment['type'] === Segment::TYPE_WP_USERS) {
|
||||
$listing = new Handler('\MailPoet\Models\Subscriber', $data);
|
||||
|
||||
return $listing_data = $listing->get();
|
||||
}
|
||||
$handlers = Hooks::applyFilters('mailpoet_get_subscribers_listings_in_segment_handlers', array());
|
||||
foreach($handlers as $handler) {
|
||||
$listings = $handler->get($segment, $data);
|
||||
if($listings) {
|
||||
return $listings;
|
||||
}
|
||||
}
|
||||
throw new \InvalidArgumentException('No handler found for segment');
|
||||
}
|
||||
|
||||
}
|
@ -220,6 +220,20 @@ class SubscribersTest extends \MailPoetTest {
|
||||
expect($response->data[0]['email'])->equals($this->subscriber_2->email);
|
||||
}
|
||||
|
||||
function testItCanAddSegmentsUsingHooks() {
|
||||
$add_segment = function() {
|
||||
return 'segment';
|
||||
};
|
||||
add_filter('mailpoet_subscribers_listings_filters_segments', $add_segment);
|
||||
$router = new Subscribers();
|
||||
$response = $router->listing(array(
|
||||
'filter' => array(
|
||||
'segment' => $this->segment_2->id
|
||||
)
|
||||
));
|
||||
expect($response->meta['filters']['segment'])->equals('segment');
|
||||
}
|
||||
|
||||
function testItCanSearchListing() {
|
||||
$new_subscriber = Subscriber::createOrUpdate(array(
|
||||
'email' => 'search.me@find.me',
|
||||
|
11
tests/unit/Segments/DynamicListingsHandlerMock.php
Normal file
11
tests/unit/Segments/DynamicListingsHandlerMock.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Test\Segments;
|
||||
|
||||
class DynamicListingsHandlerMock {
|
||||
|
||||
function get() {
|
||||
|
||||
}
|
||||
|
||||
}
|
85
tests/unit/Segments/SubscribersListingsTest.php
Normal file
85
tests/unit/Segments/SubscribersListingsTest.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Segments;
|
||||
|
||||
require_once('DynamicListingsHandlerMock.php');
|
||||
|
||||
use Codeception\Util\Stub;
|
||||
use MailPoet\Models\Segment;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Models\SubscriberSegment;
|
||||
use MailPoet\WP\Hooks;
|
||||
|
||||
class SubscribersListingsTest extends \MailPoetTest {
|
||||
|
||||
function _before() {
|
||||
\ORM::raw_execute('TRUNCATE ' . Segment::$_table);
|
||||
\ORM::raw_execute('TRUNCATE ' . SubscriberSegment::$_table);
|
||||
\ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||
$this->segment_1 = Segment::createOrUpdate(array('name' => 'Segment 1', 'type' => 'default'));
|
||||
$this->segment_2 = Segment::createOrUpdate(array('name' => 'Segment 3', 'type' => 'not default'));
|
||||
$this->subscriber_1 = Subscriber::createOrUpdate(array(
|
||||
'email' => 'john@mailpoet.com',
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'status' => Subscriber::STATUS_SUBSCRIBED,
|
||||
'segments' => array(
|
||||
$this->segment_1->id,
|
||||
),
|
||||
));
|
||||
$this->subscriber_2 = Subscriber::createOrUpdate(array(
|
||||
'email' => 'jake@mailpoet.com',
|
||||
'first_name' => 'Jake',
|
||||
'last_name' => 'Doe',
|
||||
'status' => Subscriber::STATUS_SUBSCRIBED,
|
||||
'segments' => array(
|
||||
$this->segment_2->id,
|
||||
),
|
||||
));
|
||||
SubscriberSegment::resubscribeToAllSegments($this->subscriber_1);
|
||||
SubscriberSegment::resubscribeToAllSegments($this->subscriber_2);
|
||||
}
|
||||
|
||||
function testTryToGetListingsWithoutPassingSegment() {
|
||||
$finder = new SubscribersListings();
|
||||
$this->setExpectedException('InvalidArgumentException');
|
||||
$finder->getListingsInSegment(array());
|
||||
}
|
||||
|
||||
function testGetListingsForDefaultSegment() {
|
||||
$finder = new SubscribersListings();
|
||||
$listings = $finder->getListingsInSegment(array('filter'=> array('segment' => $this->segment_1->id)));
|
||||
expect($listings['items'])->count(1);
|
||||
}
|
||||
|
||||
function testGetListingsForNonExistingSegmen() {
|
||||
$finder = new SubscribersListings();
|
||||
$listings = $finder->getListingsInSegment(array('filter'=> array('segment' => 'non-existing-id')));
|
||||
expect($listings['items'])->notEmpty();
|
||||
}
|
||||
|
||||
function testGetListingsUsingFilter() {
|
||||
$mock = Stub::makeEmpty('MailPoet\Test\Segments\DynamicListingsHandlerMock', array('get'));
|
||||
$mock
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->will($this->returnValue('dynamic listings'));
|
||||
|
||||
remove_all_filters('mailpoet_get_subscribers_listings_in_segment_handlers');
|
||||
Hooks::addFilter('mailpoet_get_subscribers_listings_in_segment_handlers', function () use ($mock) {
|
||||
return array($mock);
|
||||
});
|
||||
|
||||
$finder = new SubscribersListings();
|
||||
$listings = $finder->getListingsInSegment(array('filter'=> array('segment' => $this->segment_2->id)));
|
||||
expect($listings)->equals('dynamic listings');
|
||||
}
|
||||
|
||||
function testTryToGetListingsForSegmentWithout() {
|
||||
$finder = new SubscribersListings();
|
||||
$this->setExpectedException('InvalidArgumentException');
|
||||
remove_all_filters('mailpoet_get_subscribers_listings_in_segment_handlers');
|
||||
$finder->getListingsInSegment(array('filter'=> array('segment' => $this->segment_2->id)));
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user