Switch unsubscribe url shortcode to the confirm page

[MAILPOET-2736]
This commit is contained in:
Rostislav Wolny
2020-04-28 16:06:32 +02:00
committed by Veljko V
parent 602b156e54
commit 1f744453d4
5 changed files with 41 additions and 11 deletions

View File

@ -22,6 +22,14 @@ class Link {
$subscriptionUrlFactory = SubscriptionUrlFactory::getInstance();
switch ($shortcodeDetails['action']) {
case 'subscription_unsubscribe_url':
return self::processUrl(
$shortcodeDetails['action'],
$subscriptionUrlFactory->getConfirmUnsubscribeUrl($wpUserPreview ? null : $subscriber),
$queue,
$wpUserPreview
);
case 'subscription_instant_unsubscribe_url':
return self::processUrl(
$shortcodeDetails['action'],
$subscriptionUrlFactory->getUnsubscribeUrl($wpUserPreview ? null : $subscriber),
@ -76,12 +84,11 @@ class Link {
$subscriptionUrlFactory = SubscriptionUrlFactory::getInstance();
switch ($shortcodeAction) {
case 'subscription_unsubscribe_url':
$settings = SettingsController::getInstance();
// track unsubscribe event
if ((boolean)$settings->get('tracking.enabled') && !$wpUserPreview) {
$unsubscribeEvent = new Unsubscribes();
$unsubscribeEvent->track($newsletter->id, $subscriber->id, $queue->id);
}
self::trackUnsubscribe($newsletter, $subscriber, $queue, $wpUserPreview);
$url = $subscriptionUrlFactory->getConfirmUnsubscribeUrl($subscriber);
break;
case 'subscription_instant_unsubscribe_url':
self::trackUnsubscribe($newsletter, $subscriber, $queue, $wpUserPreview);
$url = $subscriptionUrlFactory->getUnsubscribeUrl($subscriber);
break;
case 'subscription_manage_url':
@ -114,4 +121,13 @@ class Link {
private static function getFullShortcode($action) {
return sprintf('[link:%s]', $action);
}
private static function trackUnsubscribe($newsletter, $subscriber, $queue, $wpUserPreview) {
$settings = SettingsController::getInstance();
// track unsubscribe event
if ((boolean)$settings->get('tracking.enabled') && !$wpUserPreview) {
$unsubscribeEvent = new Unsubscribes();
$unsubscribeEvent->track($newsletter->id, $subscriber->id, $queue->id);
}
}
}

View File

@ -78,6 +78,9 @@ class ManageSubscriptionLinkCest {
);
$i->click('Unsubscribe');
$i->switchToNextTab();
$confirmUnsubscribeLink = '[data-automation-id="confirm-unsubscribe"]';
$i->waitForElement($confirmUnsubscribeLink);
$i->click($confirmUnsubscribeLink);
$i->waitForText('You are now unsubscribed');
$i->click('Manage your subscription');
$i->seeOptionIsSelected($formStatusElement, 'Unsubscribed');

View File

@ -885,7 +885,7 @@ class NewslettersTest extends \MailPoetTest {
Mailer::class,
[
'send' => function ($newsletter, $subscriber, $extraParams) {
$unsubscribeLink = $this->subscriptionUrlFactory->getUnsubscribeUrl(null);
$unsubscribeLink = $this->subscriptionUrlFactory->getConfirmUnsubscribeUrl(null);
$manageLink = $this->subscriptionUrlFactory->getManageUrl(null);
$viewInBrowserLink = Url::getViewInBrowserUrl($this->newsletter);
$mailerMetaInfo = new MetaInfo;

View File

@ -11,6 +11,7 @@ use MailPoet\Models\SendingQueue;
use MailPoet\Models\Subscriber;
use MailPoet\Models\SubscriberCustomField;
use MailPoet\Newsletter\Shortcodes\Categories\Date;
use MailPoet\Newsletter\Shortcodes\Shortcodes;
use MailPoet\Newsletter\Url as NewsletterUrl;
use MailPoet\Referrals\ReferralDetector;
use MailPoet\Settings\SettingsController;
@ -24,12 +25,13 @@ require_once(ABSPATH . 'wp-admin/includes/user.php');
class ShortcodesTest extends \MailPoetTest {
public $newsletterId;
public $shortcodesObject;
public $wPPost;
public $wPUser;
public $renderedNewsletter;
public $newsletter;
public $subscriber;
/** @var Shortcodes */
private $shortcodesObject;
/** @var SettingsController */
private $settings;
/** @var SubscriptionUrlFactory */
@ -46,7 +48,7 @@ class ShortcodesTest extends \MailPoetTest {
$this->wPPost = $this->_createWPPost();
$this->subscriber = $this->_createSubscriber();
$this->newsletter = $this->_createNewsletter();
$this->shortcodesObject = new \MailPoet\Newsletter\Shortcodes\Shortcodes(
$this->shortcodesObject = new Shortcodes(
$this->newsletter,
$this->subscriber
);
@ -257,6 +259,9 @@ class ShortcodesTest extends \MailPoetTest {
$shortcodesObject = $this->shortcodesObject;
$result =
$shortcodesObject->process(['[link:subscription_unsubscribe_url]']);
expect($result['0'])->regExp('/^http.*?action=confirm_unsubscribe/');
$result =
$shortcodesObject->process(['[link:subscription_instant_unsubscribe_url]']);
expect($result['0'])->regExp('/^http.*?action=unsubscribe/');
$result =
$shortcodesObject->process(['[link:subscription_manage_url]']);
@ -269,18 +274,22 @@ class ShortcodesTest extends \MailPoetTest {
public function testItReturnsShortcodeWhenTrackingEnabled() {
$shortcodesObject = $this->shortcodesObject;
// Returns URL when tracking is not enabled
$shortcode = '[link:subscription_unsubscribe_url]';
$result =
$shortcodesObject->process([$shortcode]);
expect($result['0'])->regExp('/^http.*?action=unsubscribe/');
expect($result['0'])->regExp('/^http.*?action=confirm_unsubscribe/');
// Returns shortcodes when tracking enabled
$this->settings->set('tracking.enabled', true);
$initialShortcodes = [
'[link:subscription_unsubscribe_url]',
'[link:subscription_instant_unsubscribe_url]',
'[link:subscription_manage_url]',
'[link:newsletter_view_in_browser_url]',
];
$expectedTransformedShortcodes = [
'[link:subscription_unsubscribe_url]',
'[link:subscription_instant_unsubscribe_url]',
'[link:subscription_manage_url]',
'[link:newsletter_view_in_browser_url]',
];
@ -303,10 +312,12 @@ class ShortcodesTest extends \MailPoetTest {
$shortcodesObject->wpUserPreview = true;
$shortcodes = [
'[link:subscription_unsubscribe_url]',
'[link:subscription_instant_unsubscribe_url]',
'[link:subscription_manage_url]',
'[link:newsletter_view_in_browser_url]',
];
$links = [
$this->subscriptionUrlFactory->getConfirmUnsubscribeUrl(null),
$this->subscriptionUrlFactory->getUnsubscribeUrl(null),
$this->subscriptionUrlFactory->getManageUrl(null),
NewsletterUrl::getViewInBrowserUrl($this->newsletter),

View File

@ -2,6 +2,6 @@
<p class="mailpoet_confirm_unsubscribe">
<%= __('Simply click on this link to stop receiving emails from us.') %>
<br>
<a href="<%= unsubscribeUrl %>"><%= _x('Yes, unsubscribe me', 'Text in unsubscribe link') %></a>
<a data-automation-id="confirm-unsubscribe" href="<%= unsubscribeUrl %>"><%= _x('Yes, unsubscribe me', 'Text in unsubscribe link') %></a>
</p>
<% endblock %>