From f11de2f1ad852b3ba0a790115e502e93bb37d7ae Mon Sep 17 00:00:00 2001 From: Jonathan Labreuille Date: Wed, 16 Mar 2016 12:33:45 +0100 Subject: [PATCH] Updated shortcodes for unsubscribe/manage/browser links - fixed all issues in #387 except the custom mailpoet pages --- .../js/src/newsletter_editor/blocks/footer.js | 2 +- .../js/src/newsletter_editor/blocks/header.js | 2 +- .../src/newsletter_editor/components/save.js | 2 +- lib/Config/Populator.php | 5 +- .../PopulatorData/Templates/BlankTemplate.php | 2 +- .../Templates/FranksRoastHouseTemplate.php | 4 +- .../PostNotificationsBlankTemplate.php | 2 +- .../Templates/WelcomeTemplate.php | 4 +- lib/Cron/Workers/SendingQueue.php | 4 +- lib/Form/Block/Base.php | 13 +++++ lib/Form/Block/Text.php | 4 +- lib/Form/Block/Textarea.php | 2 + lib/Models/SendingQueue.php | 4 ++ lib/Models/Subscriber.php | 27 ++++++++++- lib/Models/SubscriberSegment.php | 13 ++--- lib/Newsletter/Shortcodes/Categories/Link.php | 46 ------------------ .../Shortcodes/Categories/Newsletter.php | 27 ++++++++++- .../Shortcodes/Categories/Subscription.php | 48 +++++++++++++++++++ lib/Newsletter/Shortcodes/Categories/User.php | 11 +++++ lib/Newsletter/Shortcodes/Shortcodes.php | 11 +---- lib/Subscription/Pages.php | 6 +-- lib/Subscription/Url.php | 15 ++++-- tests/unit/Newsletter/RendererTestData.json | 4 +- views/newsletter/editor.html | 10 ++-- views/settings/basics.html | 7 +-- views/settings/signup.html | 11 ++--- 26 files changed, 180 insertions(+), 106 deletions(-) delete mode 100644 lib/Newsletter/Shortcodes/Categories/Link.php create mode 100644 lib/Newsletter/Shortcodes/Categories/Subscription.php diff --git a/assets/js/src/newsletter_editor/blocks/footer.js b/assets/js/src/newsletter_editor/blocks/footer.js index 52deaa5e60..4aba80c7f2 100644 --- a/assets/js/src/newsletter_editor/blocks/footer.js +++ b/assets/js/src/newsletter_editor/blocks/footer.js @@ -16,7 +16,7 @@ define([ defaults: function() { return this._getDefaults({ type: 'footer', - text: 'Unsubscribe | Manage subscription
Add your postal address here!', + text: 'Unsubscribe | Manage subscription
Add your postal address here!', styles: { block: { backgroundColor: 'transparent', diff --git a/assets/js/src/newsletter_editor/blocks/header.js b/assets/js/src/newsletter_editor/blocks/header.js index 7349a51f63..e7f6aa4f2e 100644 --- a/assets/js/src/newsletter_editor/blocks/header.js +++ b/assets/js/src/newsletter_editor/blocks/header.js @@ -16,7 +16,7 @@ define([ defaults: function() { return this._getDefaults({ type: 'header', - text: 'Display problems? View it in your browser', + text: 'Display problems? View it in your browser', styles: { block: { backgroundColor: 'transparent', diff --git a/assets/js/src/newsletter_editor/components/save.js b/assets/js/src/newsletter_editor/components/save.js index 580b5a1f98..a0aa36456e 100644 --- a/assets/js/src/newsletter_editor/components/save.js +++ b/assets/js/src/newsletter_editor/components/save.js @@ -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; } diff --git a/lib/Config/Populator.php b/lib/Config/Populator.php index 5d155c7845..3a57f59d40 100644 --- a/lib/Config/Populator.php +++ b/lib/Config/Populator.php @@ -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() { diff --git a/lib/Config/PopulatorData/Templates/BlankTemplate.php b/lib/Config/PopulatorData/Templates/BlankTemplate.php index 4387d278bc..a6fc319635 100644 --- a/lib/Config/PopulatorData/Templates/BlankTemplate.php +++ b/lib/Config/PopulatorData/Templates/BlankTemplate.php @@ -142,7 +142,7 @@ class BlankTemplate { "blocks" => array( array( "type" => "footer", - "text" => "Unsubscribe | Manage subscription
Add your postal address here!", + "text" => "Unsubscribe | Manage subscription
Add your postal address here!", "styles" => array( "block" => array( "backgroundColor" => "transparent" diff --git a/lib/Config/PopulatorData/Templates/FranksRoastHouseTemplate.php b/lib/Config/PopulatorData/Templates/FranksRoastHouseTemplate.php index 9230cc3920..de13067712 100644 --- a/lib/Config/PopulatorData/Templates/FranksRoastHouseTemplate.php +++ b/lib/Config/PopulatorData/Templates/FranksRoastHouseTemplate.php @@ -46,7 +46,7 @@ class FranksRoastHouseTemplate { "blocks" => array( array( "type" => "header", - "text" => __("Display problems? View it in your browser"), + "text" => __("Display problems? View it in your browser"), "styles" => array( "block" => array( "backgroundColor" => "#ccc6c6" @@ -280,7 +280,7 @@ class FranksRoastHouseTemplate { "blocks" => array( array( "type" => "footer", - "text" => __("

Unsubscribe | Manage subscription
12345 MailPoet Drive, EmailVille, 76543

"), + "text" => __("

Unsubscribe | Manage subscription
12345 MailPoet Drive, EmailVille, 76543

"), "styles" => array( "block" => array( "backgroundColor" => "#a9a7a7" diff --git a/lib/Config/PopulatorData/Templates/PostNotificationsBlankTemplate.php b/lib/Config/PopulatorData/Templates/PostNotificationsBlankTemplate.php index 39a9615e68..6304704292 100644 --- a/lib/Config/PopulatorData/Templates/PostNotificationsBlankTemplate.php +++ b/lib/Config/PopulatorData/Templates/PostNotificationsBlankTemplate.php @@ -242,7 +242,7 @@ class PostNotificationsBlankTemplate { "blocks" => array( array( "type" => "footer", - "text" => __("Unsubscribe | Manage subscription
Add your postal address here!"), + "text" => __("Unsubscribe | Manage subscription
Add your postal address here!"), "styles" => array( "block" => array( "backgroundColor" => "transparent" diff --git a/lib/Config/PopulatorData/Templates/WelcomeTemplate.php b/lib/Config/PopulatorData/Templates/WelcomeTemplate.php index 327affca65..64d5f258ec 100644 --- a/lib/Config/PopulatorData/Templates/WelcomeTemplate.php +++ b/lib/Config/PopulatorData/Templates/WelcomeTemplate.php @@ -46,7 +46,7 @@ class WelcomeTemplate { "blocks" => array( array( "type" => "header", - "text" => __("Display problems? View it in your browser"), + "text" => __("Display problems? View it in your browser"), "styles" => array( "block" => array( "backgroundColor" => "transparent" @@ -224,7 +224,7 @@ class WelcomeTemplate { "blocks" => array( array( "type" => "footer", - "text" => __("Unsubscribe | Manage subscription
Add your postal address here!"), + "text" => __("Unsubscribe | Manage subscription
Add your postal address here!"), "styles" => array( "block" => array( "backgroundColor" => "transparent" diff --git a/lib/Cron/Workers/SendingQueue.php b/lib/Cron/Workers/SendingQueue.php index a81fc0c8ca..528234f48d 100644 --- a/lib/Cron/Workers/SendingQueue.php +++ b/lib/Cron/Workers/SendingQueue.php @@ -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); diff --git a/lib/Form/Block/Base.php b/lib/Form/Block/Base.php index 1cc96d2168..a7fb57259d 100644 --- a/lib/Form/Block/Base.php +++ b/lib/Form/Block/Base.php @@ -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); + } } \ No newline at end of file diff --git a/lib/Form/Block/Text.php b/lib/Form/Block/Text.php index 2de8edba92..34683a0fd0 100644 --- a/lib/Form/Block/Text.php +++ b/lib/Form/Block/Text.php @@ -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 .= '

'; diff --git a/lib/Form/Block/Textarea.php b/lib/Form/Block/Textarea.php index 8cd36ffda9..d9f9728f8e 100644 --- a/lib/Form/Block/Textarea.php +++ b/lib/Form/Block/Textarea.php @@ -19,6 +19,8 @@ class Textarea extends Base { $html .= static::getInputValidation($block); + $html .= static::getInputModifiers($block); + $html .= '>'; $html .= '

'; diff --git a/lib/Models/SendingQueue.php b/lib/Models/SendingQueue.php index c6fd1c7978..75ecdcaa34 100644 --- a/lib/Models/SendingQueue.php +++ b/lib/Models/SendingQueue.php @@ -35,4 +35,8 @@ class SendingQueue extends Model { $this->save(); return ($this->getErrors() === false && $this->id() > 0); } + + function stop() { + return $this->delete(); + } } \ No newline at end of file diff --git a/lib/Models/Subscriber.php b/lib/Models/Subscriber.php index 696095ce26..892266fbdd 100644 --- a/lib/Models/Subscriber.php +++ b/lib/Models/Subscriber.php @@ -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; diff --git a/lib/Models/SubscriberSegment.php b/lib/Models/SubscriberSegment.php index 558736e376..4555871e68 100644 --- a/lib/Models/SubscriberSegment.php +++ b/lib/Models/SubscriberSegment.php @@ -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 + )); + } } } diff --git a/lib/Newsletter/Shortcodes/Categories/Link.php b/lib/Newsletter/Shortcodes/Categories/Link.php deleted file mode 100644 index f5161977ab..0000000000 --- a/lib/Newsletter/Shortcodes/Categories/Link.php +++ /dev/null @@ -1,46 +0,0 @@ -', - 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' => - ''. - __('Unsubscribe'). - '', - 'manage' => - ''. - __('Manage subscription'). - '', - 'browser' => 'TODO' - ); - return (isset($actions[$action])) ? $actions[$action] : false; - } -} \ No newline at end of file diff --git a/lib/Newsletter/Shortcodes/Categories/Newsletter.php b/lib/Newsletter/Shortcodes/Categories/Newsletter.php index d4dae921c9..b837dfc3f0 100644 --- a/lib/Newsletter/Shortcodes/Categories/Newsletter.php +++ b/lib/Newsletter/Shortcodes/Categories/Newsletter.php @@ -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 ''.__('View in your browser').''; + break; + + case 'view_in_browser_url': + return '#TODO'; + break; + default: return false; + break; } } } \ No newline at end of file diff --git a/lib/Newsletter/Shortcodes/Categories/Subscription.php b/lib/Newsletter/Shortcodes/Categories/Subscription.php new file mode 100644 index 0000000000..ed3a10e959 --- /dev/null +++ b/lib/Newsletter/Shortcodes/Categories/Subscription.php @@ -0,0 +1,48 @@ +',- + 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 ''.__('Unsubscribe').''; + break; + + case 'unsubscribe_url': + return SubscriptionUrl::getUnsubscribeUrl($subscriber); + break; + + case 'manage': + return ''.__('Manage subscription').''; + break; + + case 'manage_url': + return SubscriptionUrl::getManageUrl($subscriber); + break; + + default: + return false; + break; + } + } +} \ No newline at end of file diff --git a/lib/Newsletter/Shortcodes/Categories/User.php b/lib/Newsletter/Shortcodes/Categories/User.php index 9c157f6247..27f234c982 100644 --- a/lib/Newsletter/Shortcodes/Categories/User.php +++ b/lib/Newsletter/Shortcodes/Categories/User.php @@ -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; } } } \ No newline at end of file diff --git a/lib/Newsletter/Shortcodes/Shortcodes.php b/lib/Newsletter/Shortcodes/Shortcodes.php index 9b3ab638d6..8e138701f6 100644 --- a/lib/Newsletter/Shortcodes/Shortcodes.php +++ b/lib/Newsletter/Shortcodes/Shortcodes.php @@ -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; diff --git a/lib/Subscription/Pages.php b/lib/Subscription/Pages.php index 997dd85190..4691485bf6 100644 --- a/lib/Subscription/Pages.php +++ b/lib/Subscription/Pages.php @@ -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( diff --git a/lib/Subscription/Url.php b/lib/Subscription/Url.php index 769df966cd..48c20a877b 100644 --- a/lib/Subscription/Url.php +++ b/lib/Subscription/Url.php @@ -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( diff --git a/tests/unit/Newsletter/RendererTestData.json b/tests/unit/Newsletter/RendererTestData.json index b36b8eefcf..14e148cc65 100644 --- a/tests/unit/Newsletter/RendererTestData.json +++ b/tests/unit/Newsletter/RendererTestData.json @@ -22,7 +22,7 @@ "blocks": [ { "type": "header", - "text": "Display problems? View it in your browser", + "text": "Display problems? View it in your browser", "styles": { "block": { "backgroundColor": "#ffffff" @@ -1154,7 +1154,7 @@ "blocks": [ { "type": "footer", - "text": "

You are receiving this email because you opted in on our website. Update your preferences or Unsubscribe
123 Maple Avenue
93102
Oakland, California

", + "text": "

You are receiving this email because you opted in on our website. Update your preferences or Unsubscribe
123 Maple Avenue
93102
Oakland, California

", "styles": { "block": { "backgroundColor": "transparent" diff --git a/views/newsletter/editor.html b/views/newsletter/editor.html index af2d61a26a..be52f9ad52 100644 --- a/views/newsletter/editor.html +++ b/views/newsletter/editor.html @@ -1022,7 +1022,7 @@ }, }, footer: { - text: '<%= __('Unsubscribe') %> | <%= __('Manage subscription') %>
<%= __('Add your postal address here!') %>', + text: '<%= __('Unsubscribe') %> | <%= __('Manage subscription') %>
<%= __('Add your postal address here!') %>', styles: { block: { backgroundColor: 'transparent', @@ -1147,7 +1147,7 @@ }, header: { text: '<%= __('Display problems?') %> '+ - '<%= __('View it in your browser') %>', + '<%= __('View it in your browser') %>', 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 %> diff --git a/views/settings/basics.html b/views/settings/basics.html index 6dd3b6329c..841ef70aa0 100644 --- a/views/settings/basics.html +++ b/views/settings/basics.html @@ -270,7 +270,6 @@