Sends welcome notifications when subscribing to lists via MP's API
[MAILPOET-1295]
This commit is contained in:
@@ -37,11 +37,13 @@ class API {
|
|||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
function subscribeToList($subscriber_id, $segment_id) {
|
function subscribeToList($subscriber_id, $segment_id, $options = array()) {
|
||||||
return $this->subscribeToLists($subscriber_id, array($segment_id));
|
return $this->subscribeToLists($subscriber_id, array($segment_id), $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
function subscribeToLists($subscriber_id, array $segments_ids) {
|
function subscribeToLists($subscriber_id, array $segments_ids, $options = array()) {
|
||||||
|
$schedule_welcome_email = (isset($options['schedule_welcome_email']) && $options['schedule_welcome_email'] === false) ? false : true;
|
||||||
|
|
||||||
if(empty($segments_ids)) {
|
if(empty($segments_ids)) {
|
||||||
throw new \Exception(__('At least one segment ID is required.', 'mailpoet'));
|
throw new \Exception(__('At least one segment ID is required.', 'mailpoet'));
|
||||||
}
|
}
|
||||||
@@ -79,6 +81,12 @@ class API {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SubscriberSegment::subscribeToSegments($subscriber, $found_segments_ids);
|
SubscriberSegment::subscribeToSegments($subscriber, $found_segments_ids);
|
||||||
|
|
||||||
|
// schedule welcome email
|
||||||
|
if($schedule_welcome_email) {
|
||||||
|
$this->_scheduleWelcomeNotification($subscriber, $found_segments_ids);
|
||||||
|
}
|
||||||
|
|
||||||
return $subscriber->withCustomFields()->withSubscriptions()->asArray();
|
return $subscriber->withCustomFields()->withSubscriptions()->asArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -129,7 +129,8 @@ class APITest extends \MailPoetTest {
|
|||||||
1,
|
1,
|
||||||
array(
|
array(
|
||||||
2
|
2
|
||||||
)
|
),
|
||||||
|
array()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -174,6 +175,45 @@ class APITest extends \MailPoetTest {
|
|||||||
expect($result['subscriptions'][0]['segment_id'])->equals($segment->id);
|
expect($result['subscriptions'][0]['segment_id'])->equals($segment->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItSchedulesWelcomeNotificationByDefaultAfterSubscriberSubscriberToLists() {
|
||||||
|
$API = Stub::makeEmptyExcept(
|
||||||
|
new \MailPoet\API\MP\v1\API(),
|
||||||
|
'subscribeToLists',
|
||||||
|
array(
|
||||||
|
'_scheduleWelcomeNotification' => Stub::once()
|
||||||
|
), $this);
|
||||||
|
$subscriber = Subscriber::create();
|
||||||
|
$subscriber->hydrate(Fixtures::get('subscriber_template'));
|
||||||
|
$subscriber->save();
|
||||||
|
$segment = Segment::createOrUpdate(
|
||||||
|
array(
|
||||||
|
'name' => 'Default',
|
||||||
|
'type' => Segment::TYPE_DEFAULT
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$API->subscribeToLists($subscriber->id, array($segment->id));
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDoesNotScheduleWelcomeNotificationAfterSubscribingSubscriberToListsWhenDisabledByOption() {
|
||||||
|
$API = Stub::makeEmptyExcept(
|
||||||
|
new \MailPoet\API\MP\v1\API(),
|
||||||
|
'subscribeToLists',
|
||||||
|
array(
|
||||||
|
'_scheduleWelcomeNotification' => Stub::never()
|
||||||
|
), $this);
|
||||||
|
$subscriber = Subscriber::create();
|
||||||
|
$subscriber->hydrate(Fixtures::get('subscriber_template'));
|
||||||
|
$subscriber->save();
|
||||||
|
$segment = Segment::createOrUpdate(
|
||||||
|
array(
|
||||||
|
'name' => 'Default',
|
||||||
|
'type' => Segment::TYPE_DEFAULT
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$options = array('schedule_welcome_email' => false);
|
||||||
|
$API->subscribeToLists($subscriber->id, array($segment->id), $options);
|
||||||
|
}
|
||||||
|
|
||||||
function testItGetsSegments() {
|
function testItGetsSegments() {
|
||||||
$segment = Segment::createOrUpdate(
|
$segment = Segment::createOrUpdate(
|
||||||
array(
|
array(
|
||||||
|
Reference in New Issue
Block a user