Adds method to subscribe to single or multiple lists
This commit is contained in:
@ -2,6 +2,9 @@
|
|||||||
namespace MailPoet\API\MP\v1;
|
namespace MailPoet\API\MP\v1;
|
||||||
|
|
||||||
use MailPoet\Models\CustomField;
|
use MailPoet\Models\CustomField;
|
||||||
|
use MailPoet\Models\Segment;
|
||||||
|
use MailPoet\Models\Subscriber;
|
||||||
|
use MailPoet\Models\SubscriberSegment;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
@ -32,4 +35,39 @@ class API {
|
|||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function subscribeToList($subscriber_id, $segment_id) {
|
||||||
|
return $this->subscribeToLists($subscriber_id, array($segment_id));
|
||||||
|
}
|
||||||
|
|
||||||
|
function subscribeToLists($subscriber_id, array $segments_ids) {
|
||||||
|
$subscriber = Subscriber::findOne((int)$subscriber_id);
|
||||||
|
// throw exception when subscriber does not exist
|
||||||
|
if(!$subscriber) {
|
||||||
|
throw new \Exception(__('This subscriber does not exist.', 'mailpoet'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// throw exception when none of the segments exist
|
||||||
|
$found_segments = Segment::whereIn('id', $segments_ids)->findMany();
|
||||||
|
if(!$found_segments) {
|
||||||
|
throw new \Exception(__('These lists do not exists.', 'mailpoet'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// throw exception when trying to subscribe to a WP Users segment
|
||||||
|
$found_segments_ids = array();
|
||||||
|
foreach($found_segments as $found_segment) {
|
||||||
|
if($found_segment->type === Segment::TYPE_WP_USERS) {
|
||||||
|
throw new \Exception(__(sprintf("Can't subscribe to a WordPress Users list with ID %d.", $found_segment->id), 'mailpoet'));
|
||||||
|
}
|
||||||
|
$found_segments_ids[] = $found_segment->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// throw an exception when one or more segments do not exist
|
||||||
|
if(count($found_segments_ids) !== count($segments_ids)) {
|
||||||
|
$missing_ids = array_values(array_diff($segments_ids, $found_segments_ids));
|
||||||
|
throw new \Exception(__(sprintf('Lists with ID %s do not exist.', implode(', ', $missing_ids)), 'mailpoet'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return SubscriberSegment::subscribeToSegments($subscriber, $found_segments_ids);
|
||||||
|
}
|
||||||
}
|
}
|
@ -5,6 +5,8 @@ if(!defined('ABSPATH')) exit;
|
|||||||
|
|
||||||
class Segment extends Model {
|
class Segment extends Model {
|
||||||
static $_table = MP_SEGMENTS_TABLE;
|
static $_table = MP_SEGMENTS_TABLE;
|
||||||
|
const TYPE_WP_USERS = 'wp_users';
|
||||||
|
const TYPE_DEFAULT = 'default';
|
||||||
|
|
||||||
function __construct() {
|
function __construct() {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
@ -103,7 +105,7 @@ class Segment extends Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static function getWPSegment() {
|
static function getWPSegment() {
|
||||||
$wp_segment = self::where('type', 'wp_users')->findOne();
|
$wp_segment = self::where('type', self::TYPE_WP_USERS)->findOne();
|
||||||
|
|
||||||
if($wp_segment === false) {
|
if($wp_segment === false) {
|
||||||
// create the wp users segment
|
// create the wp users segment
|
||||||
@ -148,7 +150,7 @@ class Segment extends Model {
|
|||||||
return $orm;
|
return $orm;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function getSegmentsWithSubscriberCount($type = 'default') {
|
static function getSegmentsWithSubscriberCount($type = self::TYPE_DEFAULT) {
|
||||||
$query = self::selectMany(array(self::$_table.'.id', self::$_table.'.name'))
|
$query = self::selectMany(array(self::$_table.'.id', self::$_table.'.name'))
|
||||||
->selectExpr(
|
->selectExpr(
|
||||||
self::$_table.'.*, ' .
|
self::$_table.'.*, ' .
|
||||||
@ -227,7 +229,7 @@ class Segment extends Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static function getPublic() {
|
static function getPublic() {
|
||||||
return self::getPublished()->where('type', 'default')->orderByAsc('name');
|
return self::getPublished()->where('type', self::TYPE_DEFAULT)->orderByAsc('name');
|
||||||
}
|
}
|
||||||
|
|
||||||
static function bulkTrash($orm) {
|
static function bulkTrash($orm) {
|
||||||
@ -236,7 +238,7 @@ class Segment extends Model {
|
|||||||
'UPDATE `' . Segment::$_table . '`',
|
'UPDATE `' . Segment::$_table . '`',
|
||||||
'SET `deleted_at` = NOW()',
|
'SET `deleted_at` = NOW()',
|
||||||
'WHERE `id` IN ('.rtrim(str_repeat('?,', count($ids)), ',').')',
|
'WHERE `id` IN ('.rtrim(str_repeat('?,', count($ids)), ',').')',
|
||||||
'AND `type` = "default"'
|
'AND `type` = "' . Segment::TYPE_DEFAULT . '"'
|
||||||
)), $ids);
|
)), $ids);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -247,7 +249,7 @@ class Segment extends Model {
|
|||||||
$count = parent::bulkAction($orm, function($ids) {
|
$count = parent::bulkAction($orm, function($ids) {
|
||||||
// delete segments (only default)
|
// delete segments (only default)
|
||||||
Segment::whereIn('id', $ids)
|
Segment::whereIn('id', $ids)
|
||||||
->where('type', 'default')
|
->where('type', Segment::TYPE_DEFAULT)
|
||||||
->deleteMany();
|
->deleteMany();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user