Updated shortcodes for unsubscribe/manage/browser links

- fixed all issues in #387 except the custom mailpoet pages
This commit is contained in:
Jonathan Labreuille
2016-03-16 12:33:45 +01:00
parent e28451d410
commit f11de2f1ad
26 changed files with 180 additions and 106 deletions

View File

@ -16,7 +16,7 @@ define([
defaults: function() {
return this._getDefaults({
type: 'footer',
text: '<a href="[unsubscribeUrl]">Unsubscribe</a> | <a href="[manageSubscriptionUrl]">Manage subscription</a><br /><b>Add your postal address here!</b>',
text: '<a href="[subscription:unsubscribe_url]">Unsubscribe</a> | <a href="[subscription:manage_url]">Manage subscription</a><br /><b>Add your postal address here!</b>',
styles: {
block: {
backgroundColor: 'transparent',

View File

@ -16,7 +16,7 @@ define([
defaults: function() {
return this._getDefaults({
type: 'header',
text: 'Display problems? <a href="[viewInBrowserUrl]">View it in your browser</a>',
text: 'Display problems? <a href="[newsletter:view_in_browser_url]">View it in your browser</a>',
styles: {
block: {
backgroundColor: 'transparent',

View File

@ -284,7 +284,7 @@ define([
}
if (App.getConfig().get('validation.validateUnsubscribeLinkPresent') &&
JSON.stringify(jsonObject).indexOf("[unsubscribeUrl]") < 0) {
JSON.stringify(jsonObject).indexOf("[subscription:unsubscribe_url]") < 0) {
this.showValidationError(App.getConfig().get('translations.unsubscribeLinkMissing'));
return;
}

View File

@ -74,8 +74,9 @@ class Populator {
$mailpoet_page_id = $page->ID;
}
Setting::setValue('subscription.page', $mailpoet_page_id);
Setting::setValue('signup_confirmation.page', $mailpoet_page_id);
Setting::setValue('subscription.unsubscribe_page', $mailpoet_page_id);
Setting::setValue('subscription.manage_page', $mailpoet_page_id);
Setting::setValue('subscription.confirmation_page', $mailpoet_page_id);
}
private function createDefaultSettings() {

View File

@ -142,7 +142,7 @@ class BlankTemplate {
"blocks" => array(
array(
"type" => "footer",
"text" => "<a href=\"[unsubscribeUrl]\">Unsubscribe</a> | <a href=\"[manageSubscriptionUrl]\">Manage subscription</a><br /><b>Add your postal address here!</b>",
"text" => "<a href=\"[subscription:unsubscribe_url]\">Unsubscribe</a> | <a href=\"[subscription:manage_url]\">Manage subscription</a><br /><b>Add your postal address here!</b>",
"styles" => array(
"block" => array(
"backgroundColor" => "transparent"

View File

@ -46,7 +46,7 @@ class FranksRoastHouseTemplate {
"blocks" => array(
array(
"type" => "header",
"text" => __("Display problems?&nbsp;<a href=\"[viewInBrowserUrl]\">View it in your browser</a>"),
"text" => __("Display problems?&nbsp;<a href=\"[newsletter:view_in_browser_url]\">View it in your browser</a>"),
"styles" => array(
"block" => array(
"backgroundColor" => "#ccc6c6"
@ -280,7 +280,7 @@ class FranksRoastHouseTemplate {
"blocks" => array(
array(
"type" => "footer",
"text" => __("<p><a href=\"[unsubscribeUrl]\">Unsubscribe</a> | <a href=\"[manageSubscriptionUrl]\">Manage subscription</a><br />12345 MailPoet Drive, EmailVille, 76543</p>"),
"text" => __("<p><a href=\"[subscription:unsubscribe_url]\">Unsubscribe</a> | <a href=\"[subscription:manage_url]\">Manage subscription</a><br />12345 MailPoet Drive, EmailVille, 76543</p>"),
"styles" => array(
"block" => array(
"backgroundColor" => "#a9a7a7"

View File

@ -242,7 +242,7 @@ class PostNotificationsBlankTemplate {
"blocks" => array(
array(
"type" => "footer",
"text" => __("<a href=\"[unsubscribeUrl]\">Unsubscribe</a> | <a href=\"[manageSubscriptionUrl]\">Manage subscription</a><br /><b>Add your postal address here!</b>"),
"text" => __("<a href=\"[subscription:unsubscribe_url]\">Unsubscribe</a> | <a href=\"[subscription:manage_url]\">Manage subscription</a><br /><b>Add your postal address here!</b>"),
"styles" => array(
"block" => array(
"backgroundColor" => "transparent"

View File

@ -46,7 +46,7 @@ class WelcomeTemplate {
"blocks" => array(
array(
"type" => "header",
"text" => __("Display problems?&nbsp;<a href=\"[viewInBrowserUrl]\">View it in your browser</a>"),
"text" => __("Display problems?&nbsp;<a href=\"[newsletter:view_in_browser_url]\">View it in your browser</a>"),
"styles" => array(
"block" => array(
"backgroundColor" => "transparent"
@ -224,7 +224,7 @@ class WelcomeTemplate {
"blocks" => array(
array(
"type" => "footer",
"text" => __("<a href=\"[unsubscribeUrl]\">Unsubscribe</a> | <a href=\"[manageSubscriptionUrl]\">Manage subscription</a><br /><b>Add your postal address here!</b>"),
"text" => __("<a href=\"[subscription:unsubscribe_url]\">Unsubscribe</a> | <a href=\"[subscription:manage_url]\">Manage subscription</a><br /><b>Add your postal address here!</b>"),
"styles" => array(
"block" => array(
"backgroundColor" => "transparent"

View File

@ -203,7 +203,9 @@ class SendingQueue {
$queue->subscribers->failed
)
);
$queue->subscribers->to_process = array_values($queue->subscribers->to_process);
$queue->subscribers->to_process = array_values(
$queue->subscribers->to_process
);
$queue->count_processed =
count($queue->subscribers->processed) + count($queue->subscribers->failed);
$queue->count_to_process = count($queue->subscribers->to_process);

View File

@ -112,4 +112,17 @@ abstract class Base {
&& strlen(trim($block['params']['value'])) > 0)
? esc_attr(trim($block['params']['value'])) : '';
}
protected static function getInputModifiers($block = array()) {
$modifiers = array();
if(isset($block['params']['readonly'])) {
$modifiers[] = 'readonly';
}
if(isset($block['params']['disabled'])) {
$modifiers[] = 'disabled';
}
return join(' ', $modifiers);
}
}

View File

@ -6,7 +6,7 @@ class Text extends Base {
static function render($block) {
$type = 'text';
if($block['id'] === 'email') {
$type = 'email';
$type = 'email';
}
$html = '';
@ -27,6 +27,8 @@ class Text extends Base {
$html .= static::getInputValidation($block);
$html .= static::getInputModifiers($block);
$html .= '/>';
$html .= '</p>';

View File

@ -19,6 +19,8 @@ class Textarea extends Base {
$html .= static::getInputValidation($block);
$html .= static::getInputModifiers($block);
$html .= '></textarea>';
$html .= '</p>';

View File

@ -35,4 +35,8 @@ class SendingQueue extends Model {
$this->save();
return ($this->getErrors() === false && $this->id() > 0);
}
function stop() {
return $this->delete();
}
}

View File

@ -336,6 +336,9 @@ class Subscriber extends Model {
static function createOrUpdate($data = array()) {
$subscriber = false;
if(is_array($data) && !empty($data)) {
$data = stripslashes_deep($data);
}
if(isset($data['id']) && (int)$data['id'] > 0) {
$subscriber = self::findOne((int)$data['id']);
@ -361,16 +364,25 @@ class Subscriber extends Model {
foreach($data as $key => $value) {
if(strpos($key, 'cf_') === 0) {
if(is_array($value)) {
$value = array_filter($value);
$value = reset($value);
}
$custom_fields[(int)substr($key, 3)] = $value;
unset($data[$key]);
}
}
$old_status = false;
$new_status = false;
if($subscriber === false) {
$subscriber = self::create();
$subscriber->hydrate($data);
} else {
$old_status = $subscriber->status;
$subscriber->set($data);
$new_status = $subscriber->status;
}
if($subscriber->save()) {
@ -379,8 +391,19 @@ class Subscriber extends Model {
$subscriber->setCustomField($custom_field_id, $value);
}
}
if($segment_ids !== false) {
SubscriberSegment::setSubscriptions($subscriber, $segment_ids);
// check for status change
if(
($old_status === self::STATUS_SUBSCRIBED)
&&
($new_status === self::STATUS_UNSUBSCRIBED)
) {
// make sure we unsubscribe the user from all lists
SubscriberSegment::setSubscriptions($subscriber, array());
} else {
if($segment_ids !== false) {
SubscriberSegment::setSubscriptions($subscriber, $segment_ids);
}
}
}
return $subscriber;

View File

@ -16,18 +16,19 @@ class SubscriberSegment extends Model {
if($subscriber->id > 0) {
// unsubscribe from current subscriptions
SubscriberSegment::where('subscriber_id', $subscriber->id)
->whereNotIn('segment_id', $segment_ids)
->findResultSet()
->set('status', Subscriber::STATUS_UNSUBSCRIBED)
->save();
// subscribe to segments
foreach($segment_ids as $segment_id) {
self::createOrUpdate(array(
'subscriber_id' => $subscriber->id,
'segment_id' => $segment_id,
'status' => Subscriber::STATUS_SUBSCRIBED
));
if((int)$segment_id > 0) {
self::createOrUpdate(array(
'subscriber_id' => $subscriber->id,
'segment_id' => $segment_id,
'status' => Subscriber::STATUS_SUBSCRIBED
));
}
}
}

View File

@ -1,46 +0,0 @@
<?php
namespace MailPoet\Newsletter\Shortcodes\Categories;
use MailPoet\Subscription;
require_once(ABSPATH . 'wp-includes/pluggable.php');
class Link {
/*
{
text: '<%= __('Unsubscribe link') %>',
shortcode: 'global:unsubscribe',
},
{
text: '<%= __('Edit subscription page link') %>',
shortcode: 'global:manage',
},
{
text: '<%= __('View in browser link') %>',
shortcode: 'global:browser',
}
*/
static function process(
$action,
$default_value,
$newsletter = false,
$subscriber = false
) {
$actions = array(
'unsubscribe' =>
'<a
target="_blank"
href="'.esc_attr(Subscription\Url::getUnsubscribeUrl($subscriber)).'">'.
__('Unsubscribe').
'</a>',
'manage' =>
'<a
target="_blank"
href="'.esc_attr(Subscription\Url::getManageUrl($subscriber)).'">'.
__('Manage subscription').
'</a>',
'browser' => 'TODO'
);
return (isset($actions[$action])) ? $actions[$action] : false;
}
}

View File

@ -18,6 +18,14 @@ class Newsletter {
{
text: '<%= __('Issue number') %>',
shortcode: 'newsletter:number',
},
{
text: '<%= __('Issue number') %>',
shortcode: 'newsletter:number',
},
{
text: '<%= __('View in browser link') %>',
shortcode: 'newsletter:view_in_browser',
}
*/
static function process($action, $default_value = false, $newsletter) {
@ -27,17 +35,34 @@ class Newsletter {
switch($action) {
case 'subject':
return ($newsletter) ? $newsletter['subject'] : false;
break;
case 'total':
$posts = wp_count_posts();
return $posts->publish;
break;
case 'post_title':
$post = wp_get_recent_posts(array('numberposts' => 1));
return (isset($post[0])) ? $post[0]['post_title'] : false;
break;
case 'number':
// TODO: implement
return;
return 1;
break;
case 'view_in_browser':
return '<a href="#TODO">'.__('View in your browser').'</a>';
break;
case 'view_in_browser_url':
return '#TODO';
break;
default:
return false;
break;
}
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace MailPoet\Newsletter\Shortcodes\Categories;
use MailPoet\Subscription\Url as SubscriptionUrl;
class Subscription {
/*
{
text: '<%= __('Unsubscribe') %>',-
shortcode: 'subscription:unsubscribe',
},
{
text: '<%= __('Manage subscriptions') %>',
shortcode: 'subscription:manage',
},
*/
static function process(
$action,
$default_value = false,
$newsletter = false,
$subscriber = false
) {
switch($action) {
case 'unsubscribe':
return '<a target="_blank" href="'.
esc_attr(SubscriptionUrl::getUnsubscribeUrl($subscriber))
.'">'.__('Unsubscribe').'</a>';
break;
case 'unsubscribe_url':
return SubscriptionUrl::getUnsubscribeUrl($subscriber);
break;
case 'manage':
return '<a target="_blank" href="'.
esc_attr(SubscriptionUrl::getManageUrl($subscriber))
.'">'.__('Manage subscription').'</a>';
break;
case 'manage_url':
return SubscriptionUrl::getManageUrl($subscriber);
break;
default:
return false;
break;
}
}
}

View File

@ -35,20 +35,31 @@ class User {
switch($action) {
case 'firstname':
return ($subscriber) ? $subscriber['first_name'] : $default_value;
break;
case 'lastname':
return ($subscriber) ? $subscriber['last_name'] : $default_value;
break;
case 'email':
return ($subscriber) ? $subscriber['email'] : false;
break;
case 'displayname':
if($subscriber && $subscriber['wp_user_id']) {
$wp_user = get_userdata($subscriber['wp_user_id']);
return $wp_user->user_login;
}
return $default_value;
break;
case 'count':
return Subscriber::filter('subscribed')->count();
break;
default:
return false;
break;
}
}
}

View File

@ -9,7 +9,8 @@ class Shortcodes {
function __construct(
$rendered_newsletter,
$newsletter = false,
$subscriber = false) {
$subscriber = false
) {
$this->rendered_newsletter = $rendered_newsletter;
$this->newsletter = $newsletter;
$this->subscriber = $subscriber;
@ -29,14 +30,6 @@ class Shortcodes {
$shortcode_details
);
// TODO: discuss renaming "global". It is a reserved name in PHP.
if(
isset($shortcode_details['type'])
&& $shortcode_details['type'] === 'global'
) {
$shortcode_details['type'] = 'link';
}
$shortcode_class =
__NAMESPACE__ . '\\Categories\\' . ucfirst($shortcode_details['type']);
if(!class_exists($shortcode_class)) return false;

View File

@ -43,7 +43,6 @@ class Pages {
$_POST,
array_flip($reserved_keywords)
);
$subscriber = Subscriber::createOrUpdate($subscriber_data);
$errors = $subscriber->getErrors();
@ -214,7 +213,8 @@ class Pages {
'params' => array(
'label' => __('Email'),
'required' => true,
'value' => $subscriber->email
'value' => $subscriber->email,
'readonly' => true
)
),
array(
@ -235,7 +235,7 @@ class Pages {
),
array(
'id' => 'status',
'type' => 'radio',
'type' => 'select',
'params' => array(
'label' => __('Status'),
'values' => array(

View File

@ -6,17 +6,17 @@ use \MailPoet\Models\Setting;
class Url {
static function getConfirmationUrl($subscriber = false) {
$post = get_post(Setting::getValue('signup_confirmation.page'));
$post = get_post(Setting::getValue('subscription.confirmation_page'));
return self::getSubscriptionUrl($post, 'confirm', $subscriber);
}
static function getManageUrl($subscriber = false) {
$post = get_post(Setting::getValue('subscription.page'));
$post = get_post(Setting::getValue('subscription.manage_page'));
return self::getSubscriptionUrl($post, 'manage', $subscriber);
}
static function getUnsubscribeUrl($subscriber = false) {
$post = get_post(Setting::getValue('subscription.page'));
$post = get_post(Setting::getValue('subscription.unsubscribe_page'));
return self::getSubscriptionUrl($post, 'unsubscribe', $subscriber);
}
@ -28,10 +28,15 @@ class Url {
$url = get_permalink($post);
if($subscriber !== false) {
if(is_object($subscriber)) {
$subscriber = $subscriber->asArray();
}
$params = array(
'mailpoet_action='.$action,
'mailpoet_token='.Subscriber::generateToken($subscriber->email),
'mailpoet_email='.$subscriber->email
'mailpoet_token='.Subscriber::generateToken($subscriber['email']),
'mailpoet_email='.$subscriber['email']
);
} else {
$params = array(

View File

@ -22,7 +22,7 @@
"blocks": [
{
"type": "header",
"text": "Display problems?&nbsp;<a href=\"[viewInBrowserUrl]\">View it in your browser</a>",
"text": "Display problems?&nbsp;<a href=\"[newsletter:view_in_browser_url]\">View it in your browser</a>",
"styles": {
"block": {
"backgroundColor": "#ffffff"
@ -1154,7 +1154,7 @@
"blocks": [
{
"type": "footer",
"text": "<p>You are receiving this email because you opted in on our website. <a href=\"[manageSubscriptionUrl]\">Update your preferences</a> or <a href=\"[unsubscribeUrl]\">Unsubscribe</a><a href=\"[manageSubscriptionUrl]\"> </a><br />123 Maple Avenue<br />93102<br />Oakland, California </p>",
"text": "<p>You are receiving this email because you opted in on our website. <a href=\"[subscription:manage_url]\">Update your preferences</a> or <a href=\"[subscription:unsubscribe_url]\">Unsubscribe</a><a href=\"[subscription:manage_url]\"> </a><br />123 Maple Avenue<br />93102<br />Oakland, California </p>",
"styles": {
"block": {
"backgroundColor": "transparent"

View File

@ -1022,7 +1022,7 @@
},
},
footer: {
text: '<a href="[unsubscribeUrl]"><%= __('Unsubscribe') %></a> | <a href="[manageSubscriptionUrl]"><%= __('Manage subscription') %></a><br /><b><%= __('Add your postal address here!') %></b>',
text: '<a href="[subscription:unsubscribe_url]"><%= __('Unsubscribe') %></a> | <a href="[subscription:manage_url]"><%= __('Manage subscription') %></a><br /><b><%= __('Add your postal address here!') %></b>',
styles: {
block: {
backgroundColor: 'transparent',
@ -1147,7 +1147,7 @@
},
header: {
text: '<%= __('Display problems?') %>&nbsp;'+
'<a href="[viewInBrowserUrl]"><%= __('View it in your browser') %></a>',
'<a href="[newsletter:view_in_browser_url]"><%= __('View it in your browser') %></a>',
styles: {
block: {
backgroundColor: 'transparent',
@ -1237,15 +1237,15 @@
'<%= __('Links') %>': [
{
text: '<%= __('Unsubscribe link') %>',
shortcode: 'global:unsubscribe',
shortcode: 'subscription:unsubscribe',
},
{
text: '<%= __('Edit subscription page link') %>',
shortcode: 'global:manage',
shortcode: 'subscription:manage',
},
{
text: '<%= __('View in browser link') %>',
shortcode: 'global:browser',
shortcode: 'newsletter:view_in_browser',
}
],
<% if customFields %>

View File

@ -270,7 +270,6 @@
<option
value="<%= page.id %>"
data-preview-url="<%= page.preview_url|raw %>&amp;mailpoet_action=unsubscribe"
data-edit-url="<%= page.edit_url|raw %>"
<% if(page.id == settings.subscription.unsubscribe_page) %>
selected="selected"
<% endif %>
@ -281,11 +280,7 @@
class="mailpoet_page_preview"
href="javascript:;"
title="<%= __('Preview page') %>"
><%= __('Preview') %></a>&nbsp;|&nbsp;<a
class="mailpoet_page_edit"
href="javascript:;"
title="<%= __('Edit page') %>"
><%= __('Edit') %></a>
><%= __('Preview') %></a>
</p>
</td>
</tr>

View File

@ -151,14 +151,13 @@
<p>
<select
class="mailpoet_page_selection"
name="signup_confirmation[page]"
name="subscription[confirmation_page]"
>
<% for page in pages %>
<option
value="<%= page.id %>"
data-preview-url="<%= page.preview_url|raw %>&amp;mailpoet_action=confirm"
data-edit-url="<%= page.edit_url|raw %>"
<% if(page.id == settings.signup_confirmation.page) %>
<% if(page.id == settings.subscription.confirmation_page) %>
selected="selected"
<% endif %>
><%= page.title %></option>
@ -168,11 +167,7 @@
class="mailpoet_page_preview"
href="javascript:;"
title="<%= __('Preview page') %>"
><%= __('Preview') %></a>&nbsp;|&nbsp;<a
class="mailpoet_page_edit"
href="javascript:;"
title="<%= __('Edit page') %>"
><%= __('Edit') %></a>
><%= __('Preview') %></a>
</p>
</td>
</tr>