- Removes unused class import

- Fixes newsletter URL generation in archive shortcode
- Disables generation of subscription management/unsubscribe/view in
  browser shortcodes when newsletter is previewed
This commit is contained in:
Vlad
2016-12-07 15:18:49 -05:00
parent edcce542c3
commit eee22227b3
6 changed files with 65 additions and 31 deletions

View File

@@ -3,7 +3,6 @@ namespace MailPoet\Config;
use \MailPoet\Models\Newsletter;
use \MailPoet\Models\Subscriber;
use \MailPoet\Models\SubscriberSegment;
use \MailPoet\Subscription;
use MailPoet\Newsletter\Url as NewsletterUrl;
class Shortcodes {
@@ -113,12 +112,16 @@ class Shortcodes {
}
function renderArchiveSubject($newsletter) {
$preview_url = NewsletterUrl::getViewInBrowserUrl($newsletter);
$preview_url = NewsletterUrl::getViewInBrowserUrl(
$newsletter,
$subscriber = false,
$queue = false,
$wp_user_preview = true
);
return '<a href="'.esc_attr($preview_url).'" target="_blank" title="'
.esc_attr(__('Preview in a new tab', 'mailpoet')).'">'
.esc_attr($newsletter->subject).
'</a>';
}
}

View File

@@ -7,11 +7,14 @@ use MailPoet\Statistics\Track\Unsubscribes;
use MailPoet\Subscription\Url as SubscriptionUrl;
class Link {
static function process($action,
static function process(
$action,
$default_value,
$newsletter,
$subscriber,
$queue
$queue,
$content,
$wp_user_preview
) {
switch($action) {
case 'subscription_unsubscribe':
@@ -19,7 +22,8 @@ class Link {
$url = self::processUrl(
$action,
esc_attr(SubscriptionUrl::getUnsubscribeUrl($subscriber)),
$queue
$queue,
$wp_user_preview
);
return sprintf(
'<a target="_blank" href="%s">%s</a>',
@@ -31,14 +35,16 @@ class Link {
return self::processUrl(
$action,
SubscriptionUrl::getUnsubscribeUrl($subscriber),
$queue
$queue,
$wp_user_preview
);
case 'subscription_manage':
$url = self::processUrl(
$action = 'subscription_manage_url',
esc_attr(SubscriptionUrl::getManageUrl($subscriber)),
$queue
$queue,
$wp_user_preview
);
return sprintf(
'<a target="_blank" href="%s">%s</a>',
@@ -50,13 +56,14 @@ class Link {
return self::processUrl(
$action,
SubscriptionUrl::getManageUrl($subscriber),
$queue
$queue,
$wp_user_preview
);
case 'newsletter_view_in_browser':
$action = 'newsletter_view_in_browser_url';
$url = esc_attr(NewsletterUrl::getViewInBrowserUrl($newsletter, $subscriber, $queue));
$url = self::processUrl($action, $url, $queue);
$url = self::processUrl($action, $url, $queue, $wp_user_preview);
return sprintf(
'<a target="_blank" href="%s">%s</a>',
$url,
@@ -65,7 +72,7 @@ class Link {
case 'newsletter_view_in_browser_url':
$url = NewsletterUrl::getViewInBrowserUrl($newsletter, $subscriber, $queue);
return self::processUrl($action, $url, $queue);
return self::processUrl($action, $url, $queue, $wp_user_preview);
default:
$shortcode = self::getShortcode($action);
@@ -74,15 +81,17 @@ class Link {
$shortcode,
$newsletter,
$subscriber,
$queue
$queue,
$wp_user_preview
);
return ($url !== $shortcode) ?
self::processUrl($action, $url, $queue) :
self::processUrl($action, $url, $queue, $wp_user_preview) :
false;
}
}
static function processUrl($action, $url, $queue) {
static function processUrl($action, $url, $queue, $wp_user_preview = false) {
if($wp_user_preview) return '#';
return ($queue !== false && (boolean)Setting::getValue('tracking.enabled')) ?
self::getShortcode($action) :
$url;

View File

@@ -5,16 +5,19 @@ class Shortcodes {
public $newsletter;
public $subscriber;
public $queue;
public $wp_user_preview;
const SHORTCODE_CATEGORY_NAMESPACE = 'MailPoet\Newsletter\Shortcodes\Categories\\';
function __construct(
$newsletter = false,
$subscriber = false,
$queue = false
$queue = false,
$wp_user_preview = false
) {
$this->newsletter = $newsletter;
$this->subscriber = $subscriber;
$this->queue = $queue;
$this->wp_user_preview = $wp_user_preview;
}
function extract($content, $categories = false) {
@@ -64,7 +67,8 @@ class Shortcodes {
$_this->newsletter,
$_this->subscriber,
$_this->queue,
$content
$content,
$_this->wp_user_preview
);
return ($custom_shortcode === $shortcode) ?
false :
@@ -76,7 +80,8 @@ class Shortcodes {
$_this->newsletter,
$_this->subscriber,
$_this->queue,
$content
$content,
$_this->wp_user_preview
);
}, $shortcodes);
return $processed_shortcodes;

View File

@@ -27,8 +27,8 @@ class ViewInBrowser {
$shortcodes = new Shortcodes(
$newsletter,
$subscriber,
$queue
$queue,
$wp_user_preview
);
$rendered_newsletter = $shortcodes->replace($newsletter_body['html']);
if($queue && (boolean)Setting::getValue('tracking.enabled')) {

View File

@@ -269,6 +269,30 @@ class ShortcodesTest extends MailPoetTest {
}
}
function testItReturnsHashInsteadofLinksWhenInPreviewIsEnabled() {
$shortcodes_object = $this->shortcodes_object;
$shortcodes_object->wp_user_preview = true;
$shortcodes = array(
'[link:subscription_unsubscribe_url]',
'[link:subscription_manage_url]',
'[link:newsletter_view_in_browser_url]',
);
$result = $shortcodes_object->process($shortcodes);
// hash is returned
foreach($result as $index => $transformed_shortcode) {
expect($transformed_shortcode)->equals('#');
}
$shortcodes = array(
'[link:subscription_unsubscribe]',
'[link:subscription_manage]',
'[link:newsletter_view_in_browser]',
);
$result = $shortcodes_object->process($shortcodes);
foreach($result as $index => $transformed_shortcode) {
expect($transformed_shortcode)->regExp('/href="#"/');
}
}
function testItCanProcessCustomLinkShortcodes() {
$shortcodes_object = $this->shortcodes_object;
$shortcode = '[link:shortcode]';

View File

@@ -85,13 +85,6 @@ class ViewInBrowserTest extends MailPoetTest {
$queue->newsletter_rendered_body = $this->queue_rendered_newsletter_without_tracking;
$queue->subscribers = array('processed' => array($subscriber->id));
$this->queue = $queue->save();
// build browser preview data
$this->browser_preview_data = (object)array(
'queue' => $this->queue,
'subscriber' => $this->subscriber,
'newsletter' => $this->newsletter,
'preview' => false
);
}
function testItRendersNewsletter() {
@@ -99,7 +92,7 @@ class ViewInBrowserTest extends MailPoetTest {
$this->newsletter,
$this->subscriber,
$queue = false,
$preview = true
$preview = false
);
expect($rendered_body)->regExp('/Rendered newsletter/');
}
@@ -109,7 +102,7 @@ class ViewInBrowserTest extends MailPoetTest {
$this->newsletter,
$this->subscriber,
$this->queue,
$preview = true
$preview = false
);
expect($rendered_body)->regExp('/Newsletter from queue/');
}
@@ -120,7 +113,7 @@ class ViewInBrowserTest extends MailPoetTest {
$this->newsletter,
$this->subscriber,
$this->queue,
$preview = true
$preview = false
);
expect($rendered_body)->contains('Hello, First');
expect($rendered_body)->contains(Router::NAME . '&endpoint=view_in_browser');
@@ -134,7 +127,7 @@ class ViewInBrowserTest extends MailPoetTest {
$this->newsletter,
$this->subscriber,
$queue,
$preview = true
$preview = false
);
expect($rendered_body)->contains(Router::NAME . '&endpoint=track');
}