Adds support for list-unsubscribe header to the MSS method

[MAILPOET-956]
This commit is contained in:
mrcasual
2017-06-19 11:19:31 -04:00
committed by pavel-mailpoet
parent 01eaf1ef2d
commit cbcd9fb22f
3 changed files with 47 additions and 9 deletions

View File

@ -93,6 +93,7 @@ class SendingQueue {
$prepared_newsletters = array(); $prepared_newsletters = array();
$prepared_subscribers = array(); $prepared_subscribers = array();
$prepared_subscribers_ids = array(); $prepared_subscribers_ids = array();
$unsubscribe_urls = array();
$statistics = array(); $statistics = array();
foreach($subscribers as $subscriber) { foreach($subscribers as $subscriber) {
// render shortcodes and replace subscriber data in tracked links // render shortcodes and replace subscriber data in tracked links
@ -110,6 +111,8 @@ class SendingQueue {
$subscriber $subscriber
); );
$prepared_subscribers_ids[] = $subscriber->id; $prepared_subscribers_ids[] = $subscriber->id;
// save personalized unsubsribe link
$unsubscribe_urls[] = Links::getUnsubscribeUrl($queue, $subscriber->id);
// keep track of values for statistics purposes // keep track of values for statistics purposes
$statistics[] = array( $statistics[] = array(
'newsletter_id' => $newsletter->id, 'newsletter_id' => $newsletter->id,
@ -123,11 +126,12 @@ class SendingQueue {
$prepared_newsletters[0], $prepared_newsletters[0],
$prepared_subscribers[0], $prepared_subscribers[0],
$statistics, $statistics,
array('unsubscribe_url' => Links::getUnsubscribeUrl($queue, $prepared_subscribers_ids[0])) array('unsubscribe_url' => $unsubscribe_urls[0])
); );
$prepared_newsletters = array(); $prepared_newsletters = array();
$prepared_subscribers = array(); $prepared_subscribers = array();
$prepared_subscribers_ids = array(); $prepared_subscribers_ids = array();
$unsubscribe_urls = array();
$statistics = array(); $statistics = array();
} }
} }
@ -137,7 +141,8 @@ class SendingQueue {
$prepared_subscribers_ids, $prepared_subscribers_ids,
$prepared_newsletters, $prepared_newsletters,
$prepared_subscribers, $prepared_subscribers,
$statistics $statistics,
array('unsubscribe_url' => $unsubscribe_urls)
); );
} }
return $queue; return $queue;

View File

@ -27,7 +27,7 @@ class MailPoet {
return Mailer::formatMailerSendErrorResult($response); return Mailer::formatMailerSendErrorResult($response);
} }
$message_body = $this->getBody($newsletter, $subscriber); $message_body = $this->getBody($newsletter, $subscriber, $extra_params);
$result = $this->api->sendMessages($message_body); $result = $this->api->sendMessages($message_body);
switch($result['status']) { switch($result['status']) {
@ -57,9 +57,9 @@ class MailPoet {
); );
} }
function getBody($newsletter, $subscriber) { function getBody($newsletter, $subscriber, $extra_params = array()) {
$_this = $this; $_this = $this;
$composeBody = function($newsletter, $subscriber) use($_this) { $composeBody = function($newsletter, $subscriber, $unsubscribe_url) use($_this) {
$body = array( $body = array(
'to' => (array( 'to' => (array(
'address' => $subscriber['email'], 'address' => $subscriber['email'],
@ -81,6 +81,9 @@ class MailPoet {
if(!empty($newsletter['body']['text'])) { if(!empty($newsletter['body']['text'])) {
$body['text'] = $newsletter['body']['text']; $body['text'] = $newsletter['body']['text'];
} }
if($unsubscribe_url) {
$body['list_unsubscribe'] = $unsubscribe_url;
}
return $body; return $body;
}; };
if(is_array($newsletter) && is_array($subscriber)) { if(is_array($newsletter) && is_array($subscriber)) {
@ -88,11 +91,16 @@ class MailPoet {
for($record = 0; $record < count($newsletter); $record++) { for($record = 0; $record < count($newsletter); $record++) {
$body[] = $composeBody( $body[] = $composeBody(
$newsletter[$record], $newsletter[$record],
$this->processSubscriber($subscriber[$record]) $this->processSubscriber($subscriber[$record]),
(!empty($extra_params['unsubscribe_url'][$record])) ? $extra_params['unsubscribe_url'][$record] : false
); );
} }
} else { } else {
$body[] = $composeBody($newsletter, $this->processSubscriber($subscriber)); $body[] = $composeBody(
$newsletter,
$this->processSubscriber($subscriber),
(!empty($extra_params['unsubscribe_url'])) ? $extra_params['unsubscribe_url'] : false
);
} }
return $body; return $body;
} }

View File

@ -1,9 +1,8 @@
<?php <?php
use Codeception\Util\Stub; use Codeception\Util\Stub;
use MailPoet\Mailer\Methods\MailPoet;
use MailPoet\Config\ServicesChecker; use MailPoet\Config\ServicesChecker;
use MailPoet\Services\Bridge\API; use MailPoet\Mailer\Methods\MailPoet;
class MailPoetAPITest extends MailPoetTest { class MailPoetAPITest extends MailPoetTest {
function _before() { function _before() {
@ -69,6 +68,32 @@ class MailPoetAPITest extends MailPoetTest {
expect($body[0]['text'])->equals($this->newsletter['body']['text']); expect($body[0]['text'])->equals($this->newsletter['body']['text']);
} }
function testItCanAddExtraParametersToSingleMessage() {
$extra_params = array('unsubscribe_url' => 'http://example.com');
$body = $this->mailer->getBody($this->newsletter, $this->subscriber, $extra_params);
expect($body[0]['list_unsubscribe'])->equals($extra_params['unsubscribe_url']);
}
function testItCanAddExtraParametersToMultipleMessages() {
$extra_params = array('unsubscribe_url' => 'http://example.com');
$newsletters = array_fill(0, 10, $this->newsletter);
$subscribers = array_fill(0, 10, $this->subscriber);
$body = $this->mailer->getBody($newsletters, $subscribers, $extra_params);
expect($body[0]['list_unsubscribe'])->equals($extra_params['unsubscribe_url'][0]);
expect($body[9]['list_unsubscribe'])->equals($extra_params['unsubscribe_url'][9]);
}
function testItCanAddUnsubscribeUrlToMultipleMessages() {
$newsletters = array_fill(0, 10, $this->newsletter);
$subscribers = array_fill(0, 10, $this->subscriber);
$extra_params = array('unsubscribe_url' => array_fill(0, 10, 'http://example.com'));
$body = $this->mailer->getBody($newsletters, $subscribers, $extra_params);
expect(count($body))->equals(10);
expect($body[0]['list_unsubscribe'])->equals($extra_params['unsubscribe_url'][0]);
expect($body[9]['list_unsubscribe'])->equals($extra_params['unsubscribe_url'][9]);
}
function testItCanProcessSubscriber() { function testItCanProcessSubscriber() {
expect($this->mailer->processSubscriber('test@test.com')) expect($this->mailer->processSubscriber('test@test.com'))
->equals( ->equals(