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(); $subscriptionUrlFactory = SubscriptionUrlFactory::getInstance();
switch ($shortcodeDetails['action']) { switch ($shortcodeDetails['action']) {
case 'subscription_unsubscribe_url': case 'subscription_unsubscribe_url':
return self::processUrl(
$shortcodeDetails['action'],
$subscriptionUrlFactory->getConfirmUnsubscribeUrl($wpUserPreview ? null : $subscriber),
$queue,
$wpUserPreview
);
case 'subscription_instant_unsubscribe_url':
return self::processUrl( return self::processUrl(
$shortcodeDetails['action'], $shortcodeDetails['action'],
$subscriptionUrlFactory->getUnsubscribeUrl($wpUserPreview ? null : $subscriber), $subscriptionUrlFactory->getUnsubscribeUrl($wpUserPreview ? null : $subscriber),
@ -76,12 +84,11 @@ class Link {
$subscriptionUrlFactory = SubscriptionUrlFactory::getInstance(); $subscriptionUrlFactory = SubscriptionUrlFactory::getInstance();
switch ($shortcodeAction) { switch ($shortcodeAction) {
case 'subscription_unsubscribe_url': case 'subscription_unsubscribe_url':
$settings = SettingsController::getInstance(); self::trackUnsubscribe($newsletter, $subscriber, $queue, $wpUserPreview);
// track unsubscribe event $url = $subscriptionUrlFactory->getConfirmUnsubscribeUrl($subscriber);
if ((boolean)$settings->get('tracking.enabled') && !$wpUserPreview) { break;
$unsubscribeEvent = new Unsubscribes(); case 'subscription_instant_unsubscribe_url':
$unsubscribeEvent->track($newsletter->id, $subscriber->id, $queue->id); self::trackUnsubscribe($newsletter, $subscriber, $queue, $wpUserPreview);
}
$url = $subscriptionUrlFactory->getUnsubscribeUrl($subscriber); $url = $subscriptionUrlFactory->getUnsubscribeUrl($subscriber);
break; break;
case 'subscription_manage_url': case 'subscription_manage_url':
@ -114,4 +121,13 @@ class Link {
private static function getFullShortcode($action) { private static function getFullShortcode($action) {
return sprintf('[link:%s]', $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->click('Unsubscribe');
$i->switchToNextTab(); $i->switchToNextTab();
$confirmUnsubscribeLink = '[data-automation-id="confirm-unsubscribe"]';
$i->waitForElement($confirmUnsubscribeLink);
$i->click($confirmUnsubscribeLink);
$i->waitForText('You are now unsubscribed'); $i->waitForText('You are now unsubscribed');
$i->click('Manage your subscription'); $i->click('Manage your subscription');
$i->seeOptionIsSelected($formStatusElement, 'Unsubscribed'); $i->seeOptionIsSelected($formStatusElement, 'Unsubscribed');

View File

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

View File

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

View File

@ -2,6 +2,6 @@
<p class="mailpoet_confirm_unsubscribe"> <p class="mailpoet_confirm_unsubscribe">
<%= __('Simply click on this link to stop receiving emails from us.') %> <%= __('Simply click on this link to stop receiving emails from us.') %>
<br> <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> </p>
<% endblock %> <% endblock %>