Export lists for email
[MAILPOET-1354]
This commit is contained in:
committed by
pavel-mailpoet
parent
f8e443ed3c
commit
8e13eb50bf
@ -130,6 +130,7 @@ class Initializer {
|
|||||||
$this->setupMenu();
|
$this->setupMenu();
|
||||||
$this->setupShortcodes();
|
$this->setupShortcodes();
|
||||||
$this->setupImages();
|
$this->setupImages();
|
||||||
|
$this->setupPersonalDataExporters();
|
||||||
|
|
||||||
$this->setupChangelog();
|
$this->setupChangelog();
|
||||||
$this->setupCronTrigger();
|
$this->setupCronTrigger();
|
||||||
@ -264,6 +265,11 @@ class Initializer {
|
|||||||
$hooks->init();
|
$hooks->init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setupPersonalDataExporters() {
|
||||||
|
$exporters = new PersonalDataExporters();
|
||||||
|
$exporters->init();
|
||||||
|
}
|
||||||
|
|
||||||
function handleFailedInitialization($exception) {
|
function handleFailedInitialization($exception) {
|
||||||
// check if we are able to add pages at this point
|
// check if we are able to add pages at this point
|
||||||
if(function_exists('wp_get_current_user')) {
|
if(function_exists('wp_get_current_user')) {
|
||||||
|
21
lib/Config/PersonalDataExporters.php
Normal file
21
lib/Config/PersonalDataExporters.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Config;
|
||||||
|
|
||||||
|
use MailPoet\Subscribers\ImportExport\PersonalDataExporters\SegmentsExporter;
|
||||||
|
|
||||||
|
class PersonalDataExporters {
|
||||||
|
|
||||||
|
function init() {
|
||||||
|
add_filter('wp_privacy_personal_data_exporters', array($this, 'registerSegmentsExporter'));
|
||||||
|
}
|
||||||
|
|
||||||
|
function registerSegmentsExporter($exporters) {
|
||||||
|
$exporters[] = array(
|
||||||
|
'exporter_friendly_name' => __('MailPoet Lists'),
|
||||||
|
'callback' => array(new SegmentsExporter(), 'export'),
|
||||||
|
);
|
||||||
|
return $exporters;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -883,4 +883,20 @@ class Subscriber extends Model {
|
|||||||
}
|
}
|
||||||
return array($data, $custom_fields);
|
return array($data, $custom_fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getAllSegmentNamesWithStatus() {
|
||||||
|
return Segment::table_alias('segment')
|
||||||
|
->select('name')
|
||||||
|
->select('subscriber_segment.segment_id', 'segment_id')
|
||||||
|
->select('subscriber_segment.status', 'status')
|
||||||
|
->select('subscriber_segment.updated_at', 'updated_at')
|
||||||
|
->join(
|
||||||
|
SubscriberSegment::$_table,
|
||||||
|
array('subscriber_segment.segment_id', '=', 'segment.id'),
|
||||||
|
'subscriber_segment'
|
||||||
|
)
|
||||||
|
->where('subscriber_segment.subscriber_id', $this->id)
|
||||||
|
->orderByAsc('name')
|
||||||
|
->findArray();
|
||||||
|
}
|
||||||
}
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Subscribers\ImportExport\PersonalDataExporters;
|
||||||
|
|
||||||
|
use MailPoet\Models\Subscriber;
|
||||||
|
|
||||||
|
class SegmentsExporter {
|
||||||
|
|
||||||
|
function export($email) {
|
||||||
|
return array(
|
||||||
|
'data' => $this->exportSubscriber(Subscriber::findOne(trim($email))),
|
||||||
|
'done' => true,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function exportSubscriber($subscriber) {
|
||||||
|
if(!$subscriber) return array();
|
||||||
|
|
||||||
|
$result = array();
|
||||||
|
$segments = $subscriber->getAllSegmentNamesWithStatus();
|
||||||
|
|
||||||
|
foreach($segments as $segment) {
|
||||||
|
$result[] = $this->exportSegment($segment);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function exportSegment($segment) {
|
||||||
|
$segment_data = array();
|
||||||
|
$segment_data[] = array(
|
||||||
|
'name' => __('List name'),
|
||||||
|
'value' => $segment['name'],
|
||||||
|
);
|
||||||
|
$segment_data[] = array(
|
||||||
|
'name' => __('Subscription status'),
|
||||||
|
'value' => $segment['status'],
|
||||||
|
);
|
||||||
|
$segment_data[] = array(
|
||||||
|
'name' => __('Timestamp of the subscription (or last change of the subscription status)'),
|
||||||
|
'value' => $segment['updated_at'],
|
||||||
|
);
|
||||||
|
return array(
|
||||||
|
'group_id' => 'mailpoet-lists',
|
||||||
|
'group_label' => __('MailPoet Mailing Lists'),
|
||||||
|
'item_id' => 'list-' . $segment['segment_id'],
|
||||||
|
'data' => $segment_data,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,97 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Subscribers\ImportExport\PersonalDataExporters;
|
||||||
|
|
||||||
|
use MailPoet\Models\Segment;
|
||||||
|
use MailPoet\Models\Subscriber;
|
||||||
|
use MailPoet\Models\SubscriberSegment;
|
||||||
|
|
||||||
|
class SegmentsExporterTest extends \MailPoetTest {
|
||||||
|
|
||||||
|
/** @var SegmentsExporter */
|
||||||
|
private $exporter;
|
||||||
|
|
||||||
|
function _before() {
|
||||||
|
$this->exporter = new SegmentsExporter();
|
||||||
|
}
|
||||||
|
|
||||||
|
function testExportWorksWhenSubscriberNotFound() {
|
||||||
|
$result = $this->exporter->export('email.that@doesnt.exists');
|
||||||
|
expect($result)->internalType('array');
|
||||||
|
expect($result)->hasKey('data');
|
||||||
|
expect($result['data'])->equals(array());
|
||||||
|
expect($result)->hasKey('done');
|
||||||
|
expect($result['done'])->equals(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testExportWorksForSubscriberWithNoSegments() {
|
||||||
|
Subscriber::createOrUpdate(array(
|
||||||
|
'email' => 'email.that@has.no.segments',
|
||||||
|
));
|
||||||
|
$result = $this->exporter->export('email.that@has.no.segments');
|
||||||
|
expect($result)->internalType('array');
|
||||||
|
expect($result)->hasKey('data');
|
||||||
|
expect($result['data'])->equals(array());
|
||||||
|
expect($result)->hasKey('done');
|
||||||
|
expect($result['done'])->equals(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testExportWorksForSubscriberWithSegments() {
|
||||||
|
$subscriber = Subscriber::createOrUpdate(array(
|
||||||
|
'email' => 'email.that@has.some.segments',
|
||||||
|
));
|
||||||
|
$segment1 = Segment::createOrUpdate(array('name' => 'List 1'));
|
||||||
|
$segment2 = Segment::createOrUpdate(array('name' => 'List 2'));
|
||||||
|
SubscriberSegment::createOrUpdate(array(
|
||||||
|
'subscriber_id' => $subscriber->id(),
|
||||||
|
'segment_id' => $segment1->id(),
|
||||||
|
'status' => Subscriber::STATUS_SUBSCRIBED,
|
||||||
|
'updated_at' => '2018-05-02 15:26:52',
|
||||||
|
));
|
||||||
|
SubscriberSegment::createOrUpdate(array(
|
||||||
|
'subscriber_id' => $subscriber->id(),
|
||||||
|
'segment_id' => $segment2->id(),
|
||||||
|
'status' => Subscriber::STATUS_UNSUBSCRIBED,
|
||||||
|
'updated_at' => '2018-05-02 15:26:00',
|
||||||
|
));
|
||||||
|
$result = $this->exporter->export('email.that@has.some.segments');
|
||||||
|
expect($result)->internalType('array');
|
||||||
|
expect($result)->hasKey('data');
|
||||||
|
expect($result)->hasKey('done');
|
||||||
|
$expected = array (
|
||||||
|
array (
|
||||||
|
'group_id' => 'mailpoet-lists',
|
||||||
|
'group_label' => 'MailPoet Mailing Lists',
|
||||||
|
'item_id' => 'list-' . $segment1->id(),
|
||||||
|
'data' => array (
|
||||||
|
array ('name' => 'List name', 'value' => 'List 1'),
|
||||||
|
array ('name' => 'Subscription status', 'value' => 'subscribed'),
|
||||||
|
array ('name' => 'Timestamp of the subscription (or last change of the subscription status)', 'value' => '2018-05-02 15:26:52'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
array (
|
||||||
|
'group_id' => 'mailpoet-lists',
|
||||||
|
'group_label' => 'MailPoet Mailing Lists',
|
||||||
|
'item_id' => 'list-' . $segment2->id(),
|
||||||
|
'data' => array (
|
||||||
|
array ('name' => 'List name', 'value' => 'List 2'),
|
||||||
|
array ('name' => 'Subscription status', 'value' => 'unsubscribed'),
|
||||||
|
array ('name' => 'Timestamp of the subscription (or last change of the subscription status)', 'value' => '2018-05-02 15:26:00'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
expect($result['data'])->internalType('array');
|
||||||
|
expect($result['data'])->count(2);
|
||||||
|
expect($result['done'])->equals(true);
|
||||||
|
expect($result['data'][0])->hasKey('group_id');
|
||||||
|
expect($result['data'][0])->hasKey('group_label');
|
||||||
|
expect($result['data'][0])->hasKey('item_id');
|
||||||
|
expect($result['data'][0])->hasKey('data');
|
||||||
|
expect($result['data'][1])->hasKey('group_id');
|
||||||
|
expect($result['data'][1])->hasKey('group_label');
|
||||||
|
expect($result['data'][1])->hasKey('item_id');
|
||||||
|
expect($result['data'][1])->hasKey('data');
|
||||||
|
expect($result['data'])->equals($expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user