From 3eb6a219804c3d35634f03773dc481fdf3db96f9 Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 31 May 2016 11:02:08 -0400 Subject: [PATCH 1/9] - Centralizes a list of all shortcodes - Returns all shortcodes with custom fields --- .../Categories/{User.php => Subscriber.php} | 132 +++++++++--------- .../Shortcodes/ShortcodesHelper.php | 113 +++++++++++++++ 2 files changed, 179 insertions(+), 66 deletions(-) rename lib/Newsletter/Shortcodes/Categories/{User.php => Subscriber.php} (95%) create mode 100644 lib/Newsletter/Shortcodes/ShortcodesHelper.php diff --git a/lib/Newsletter/Shortcodes/Categories/User.php b/lib/Newsletter/Shortcodes/Categories/Subscriber.php similarity index 95% rename from lib/Newsletter/Shortcodes/Categories/User.php rename to lib/Newsletter/Shortcodes/Categories/Subscriber.php index 10cef17d34..bcbe28e61d 100644 --- a/lib/Newsletter/Shortcodes/Categories/User.php +++ b/lib/Newsletter/Shortcodes/Categories/Subscriber.php @@ -1,67 +1,67 @@ -', - shortcode: 'user:firstname | default:reader', - }, - { - text: '<%= __('Last Name') %>', - shortcode: 'user:lastname | default:reader', - }, - { - text: '<%= __('Email Address') %>', - shortcode: 'user:email', - }, - { - text: '<%= __('Wordpress user display name') %>', - shortcode: 'user:displayname | default:member', - }, - { - text: '<%= __('Total of subscribers') %>', - shortcode: 'user:count', - } - */ - static function process( - $action, - $default_value, - $newsletter = false, - $subscriber - ) { - 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; - } - } +', + shortcode: 'user:firstname | default:reader', + }, + { + text: '<%= __('Last Name') %>', + shortcode: 'user:lastname | default:reader', + }, + { + text: '<%= __('Email Address') %>', + shortcode: 'user:email', + }, + { + text: '<%= __('Wordpress user display name') %>', + shortcode: 'user:displayname | default:member', + }, + { + text: '<%= __('Total of subscribers') %>', + shortcode: 'user:count', + } + */ + static function process( + $action, + $default_value, + $newsletter = false, + $subscriber + ) { + 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/ShortcodesHelper.php b/lib/Newsletter/Shortcodes/ShortcodesHelper.php new file mode 100644 index 0000000000..9768ebeccc --- /dev/null +++ b/lib/Newsletter/Shortcodes/ShortcodesHelper.php @@ -0,0 +1,113 @@ + array( + array( + 'text' => __('First Name'), + 'shortcode' => 'subscriber:firstname | default:reader', + ), + array( + 'text' => __('Last Name'), + 'shortcode' => 'subscriber:lastname | default:reader', + ), + array( + 'text' => __('Email Address'), + 'shortcode' => 'subscriber:email', + ), + array( + 'text' => __('Wordpress user display name'), + 'shortcode' => 'subscriber:displayname | default:member', + ), + array( + 'text' => __('Total of subscribers'), + 'shortcode' => 'subscriber:count', + ) + ), + __('Newsletter') => array( + array( + 'text' => __('Newsletter Subject'), + 'shortcode' => 'newsletter:subject', + ) + ), + __('Post Notifications') => array( + array( + 'text' => __('Total number of posts or pages'), + 'shortcode' => 'newsletter:total', + ), + array( + 'text' => __('Latest post title'), + 'shortcode' => 'newsletter:post_title', + ), + array( + 'text' => __('Issue number'), + 'shortcode' => 'newsletter:number', + ) + ), + __('Date') => array( + array( + 'text' => __('Current day of the month number'), + 'shortcode' => 'date:d', + ), + array( + 'text' => __('Current day of the month in ordinal, ie. 2nd, 3rd, etc.'), + 'shortcode' => 'date:dordinal', + ), + array( + 'text' => __('Full name of current day'), + 'shortcode' => 'date:dtext', + ), + array( + 'text' => __('Current month number'), + 'shortcode' => 'date:m', + ), + array( + 'text' => __('Full name of current month'), + 'shortcode' => 'date:mtext', + ), + array( + 'text' => __('Year'), + 'shortcode' => 'date:y', + ) + ), + __('Links') => array( + array( + 'text' => __('Unsubscribe link'), + 'shortcode' => 'link:subscription_unsubscribe', + ), + array( + 'text' => __('Edit subscription page link'), + 'shortcode' => 'link:subscription_manage', + ), + array( + 'text' => __('View in browser link'), + 'shortcode' => 'link:newsletter_view_in_browser', + ) + ) + ); + $custom_fields = self::getCustomFields(); + if($custom_fields) { + $shortcodes[__('Subscriber')] = array_merge( + $shortcodes[__('Subscriber')], + $custom_fields + ); + } + return $shortcodes; + } + + static function getCustomFields() { + $custom_fields = CustomField::findMany(); + if(!$custom_fields) return false; + return array_map(function ($custom_field) { + return array( + 'text' => $custom_field->name, + 'shortcode' => 'subscriber:cf_' . $custom_field->id + ); + }, $custom_fields); + } +} \ No newline at end of file From da7615ba4c3d2fbcff970c7e917781e52f998bba Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 31 May 2016 11:03:04 -0400 Subject: [PATCH 2/9] - Removes redundant shortcode description - Implements shortcode processing for custom fields --- lib/Newsletter/Shortcodes/Categories/Date.php | 26 ------------- lib/Newsletter/Shortcodes/Categories/Link.php | 14 ------- .../Shortcodes/Categories/Newsletter.php | 22 ----------- .../Shortcodes/Categories/Subscriber.php | 39 +++++-------------- 4 files changed, 10 insertions(+), 91 deletions(-) diff --git a/lib/Newsletter/Shortcodes/Categories/Date.php b/lib/Newsletter/Shortcodes/Categories/Date.php index f066c1d510..dfa7ef9dda 100644 --- a/lib/Newsletter/Shortcodes/Categories/Date.php +++ b/lib/Newsletter/Shortcodes/Categories/Date.php @@ -2,32 +2,6 @@ namespace MailPoet\Newsletter\Shortcodes\Categories; class Date { - /* - { - text: '<%= __('Current day of the month number') %>', - shortcode: 'date:d', - }, - { - text: '<%= __('Current day of the month in ordinal, ie. 2nd, 3rd, etc.') %>', - shortcode: 'date:dordinal', - }, - { - text: '<%= __('Full name of current day') %>', - shortcode: 'date:dtext', - }, - { - text: '<%= __('Current month number') %>', - shortcode: 'date:m', - }, - { - text: '<%= __('Full name of current month') %>', - shortcode: 'date:mtext', - }, - { - text: '<%= __('Year') %>', - shortcode: 'date:y', - } - */ static function process($action) { $date = new \DateTime('now'); $actions = array( diff --git a/lib/Newsletter/Shortcodes/Categories/Link.php b/lib/Newsletter/Shortcodes/Categories/Link.php index a223e65d24..f22a425c4f 100644 --- a/lib/Newsletter/Shortcodes/Categories/Link.php +++ b/lib/Newsletter/Shortcodes/Categories/Link.php @@ -7,20 +7,6 @@ use MailPoet\Statistics\Track\Unsubscribes; use MailPoet\Subscription\Url as SubscriptionUrl; class Link { - /* - { - text: '<%= __('Unsubscribe') %>',- - shortcode: 'subscription:unsubscribe', - }, - { - text: '<%= __('Manage subscription') %>', - shortcode: 'subscription:manage', - }, - { - text: '<%= __('View in browser link') %>', - shortcode: 'newsletter:view_in_browser', - } - */ static function process($action, $default_value = false, $newsletter, diff --git a/lib/Newsletter/Shortcodes/Categories/Newsletter.php b/lib/Newsletter/Shortcodes/Categories/Newsletter.php index 1aae2059c6..576369b777 100644 --- a/lib/Newsletter/Shortcodes/Categories/Newsletter.php +++ b/lib/Newsletter/Shortcodes/Categories/Newsletter.php @@ -7,28 +7,6 @@ use MailPoet\Newsletter\Shortcodes\ShortcodesHelper; require_once( ABSPATH . "wp-includes/pluggable.php" ); class Newsletter { - /* - { - text: '<%= __('Newsletter Subject') %>',- - shortcode: 'newsletter:subject', - }, - { - text: '<%= __('Total number of posts or pages') %>', - shortcode: 'newsletter:total', - }, - { - text: '<%= __('Latest post title') %>', - shortcode: 'newsletter:post_title', - }, - { - text: '<%= __('Issue number') %>', - shortcode: 'newsletter:number', - }, - { - text: '<%= __('Issue number') %>', - shortcode: 'newsletter:number', - } - */ static function process($action, $default_value = false, $newsletter, diff --git a/lib/Newsletter/Shortcodes/Categories/Subscriber.php b/lib/Newsletter/Shortcodes/Categories/Subscriber.php index bcbe28e61d..dbbef54ebf 100644 --- a/lib/Newsletter/Shortcodes/Categories/Subscriber.php +++ b/lib/Newsletter/Shortcodes/Categories/Subscriber.php @@ -1,33 +1,11 @@ ', - shortcode: 'user:firstname | default:reader', - }, - { - text: '<%= __('Last Name') %>', - shortcode: 'user:lastname | default:reader', - }, - { - text: '<%= __('Email Address') %>', - shortcode: 'user:email', - }, - { - text: '<%= __('Wordpress user display name') %>', - shortcode: 'user:displayname | default:member', - }, - { - text: '<%= __('Total of subscribers') %>', - shortcode: 'user:count', - } - */ +class Subscriber { static function process( $action, $default_value, @@ -38,15 +16,12 @@ class User { 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']); @@ -54,11 +29,17 @@ class User { } return $default_value; break; - case 'count': return Subscriber::filter('subscribed')->count(); break; - + case preg_match('/cf_(\d+)/', $action, $custom_field) ? true : false: + if(empty($subscriber['id'])) return false; + $custom_field = SubscriberCustomField + ::where('subscriber_id', $subscriber['id']) + ->where('custom_field_id', $custom_field[1]) + ->findOne(); + return ($custom_field) ? $custom_field->value : false; + break; default: return false; break; From aed60e69055061e0545323a908264a2cbdae449b Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 31 May 2016 11:04:10 -0400 Subject: [PATCH 3/9] - Updates menu/editor view to work with the refactored shortcodes logic --- lib/Config/Menu.php | 10 +--- views/newsletter/editor.html | 88 +----------------------------------- 2 files changed, 3 insertions(+), 95 deletions(-) diff --git a/lib/Config/Menu.php b/lib/Config/Menu.php index 93bb7caf58..0d00431dc3 100644 --- a/lib/Config/Menu.php +++ b/lib/Config/Menu.php @@ -7,6 +7,7 @@ use MailPoet\Models\CustomField; use MailPoet\Models\Form; use MailPoet\Models\Segment; use MailPoet\Models\Setting; +use MailPoet\Newsletter\Shortcodes\ShortcodesHelper; use MailPoet\Settings\Charsets; use MailPoet\Settings\Hosts; use MailPoet\Settings\Pages; @@ -419,15 +420,8 @@ class Menu { } function newletterEditor() { - $custom_fields = array_map(function($field) { - return array( - 'text' => $field['name'], - 'shortcode' => 'field:' . $field['id'], - ); - }, CustomField::findArray()); - $data = array( - 'customFields' => $custom_fields, + 'shortcodes' => ShortcodesHelper::getShortcodes(), 'settings' => Setting::getAll(), 'sub_menu' => 'mailpoet-newsletters' ); diff --git a/views/newsletter/editor.html b/views/newsletter/editor.html index 77c815a80c..c2dc0dd548 100644 --- a/views/newsletter/editor.html +++ b/views/newsletter/editor.html @@ -1168,93 +1168,7 @@ }, }, }, - customFields: { - '<%= __('Subscriber') %>': [ - { - text: '<%= __('First Name') %>', - shortcode: 'user:firstname | default:reader', - }, - { - text: '<%= __('Last Name') %>', - shortcode: 'user:lastname | default:reader', - }, - { - text: '<%= __('Email Address') %>', - shortcode: 'user:email', - }, - { - text: '<%= __('Wordpress user display name') %>', - shortcode: 'user:displayname | default:member', - }, - { - text: '<%= __('Total of subscribers') %>', - shortcode: 'user:count', - } - ], - '<%= __('Newsletter') %>': [ - { - text: '<%= __('Newsletter Subject') %>', - shortcode: 'newsletter:subject', - }, - ], - '<%= __('Post Notifications') %>': [ - { - text: '<%= __('Total number of posts or pages') %>', - shortcode: 'newsletter:total', - }, - { - text: '<%= __('Latest post title') %>', - shortcode: 'newsletter:post_title', - }, - { - text: '<%= __('Issue number') %>', - shortcode: 'newsletter:number', - } - ], - '<%= __('Date') %>': [ - { - text: '<%= __('Current day of the month number') %>', - shortcode: 'date:d', - }, - { - text: '<%= __('Current day of the month in ordinal, ie. 2nd, 3rd, etc.') %>', - shortcode: 'date:dordinal', - }, - { - text: '<%= __('Full name of current day') %>', - shortcode: 'date:dtext', - }, - { - text: '<%= __('Current month number') %>', - shortcode: 'date:m', - }, - { - text: '<%= __('Full name of current month') %>', - shortcode: 'date:mtext', - }, - { - text: '<%= __('Year') %>', - shortcode: 'date:y', - } - ], - '<%= __('Links') %>': [ - { - text: '<%= __('Unsubscribe link') %>', - shortcode: 'link:subscription_unsubscribe', - }, - { - text: '<%= __('Edit subscription page link') %>', - shortcode: 'link:subscription_manage', - }, - { - text: '<%= __('View in browser link') %>', - shortcode: 'link:newsletter_view_in_browser', - } - ], - <% if customFields %> - '<%= __('Custom fields') %>': <%= json_encode(customFields) %>, - <% endif %> - }, + shortcodes: <%= json_encode(shortcodes) %>, sidepanelWidth: '331px', validation: { validateUnsubscribeLinkPresent: true, // TODO: Add validation based on whether Mailpoet MTA is used or not From 9642d3e672183d4c3cbde86463e562487fce2387 Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 31 May 2016 11:25:16 -0400 Subject: [PATCH 4/9] - Renames all references of "custom fields" to "shortcodes" --- .../newsletter_editor/libraryOverrides.styl | 6 +- .../js/src/newsletter_editor/blocks/footer.js | 8 +-- .../js/src/newsletter_editor/blocks/header.js | 8 +-- .../js/src/newsletter_editor/blocks/text.js | 8 +-- .../tinymce/mailpoet_custom_fields/plugin.js | 58 ------------------- .../tinymce/mailpoet_shortcodes/plugin.js | 58 +++++++++++++++++++ .../plugin.min.js | 0 views/newsletter/editor.html | 4 +- 8 files changed, 75 insertions(+), 75 deletions(-) delete mode 100644 assets/js/src/newsletter_editor/tinymce/mailpoet_custom_fields/plugin.js create mode 100644 assets/js/src/newsletter_editor/tinymce/mailpoet_shortcodes/plugin.js rename assets/js/src/newsletter_editor/tinymce/{mailpoet_custom_fields => mailpoet_shortcodes}/plugin.min.js (100%) diff --git a/assets/css/src/newsletter_editor/libraryOverrides.styl b/assets/css/src/newsletter_editor/libraryOverrides.styl index 78491b2929..12c509df0d 100644 --- a/assets/css/src/newsletter_editor/libraryOverrides.styl +++ b/assets/css/src/newsletter_editor/libraryOverrides.styl @@ -31,7 +31,7 @@ div.mce-toolbar-grp.mce-container box-shadow(0px 0px 3px 1px rgba(0, 0, 0, 0.05)) .mce-window - /* Fix TinyMCE mailpoet_custom_fields window lack of hiding overflow */ + /* Fix TinyMCE mailpoet_shortcodes window lack of hiding overflow */ div.mce-container-body.mce-abs-layout overflow: hidden @@ -40,8 +40,8 @@ div.mce-toolbar-grp.mce-container width: -webkit-calc( 100% - 36px ) width: calc( 100% - 36px ) -/* TinyMCE mailpoet_custom_fields toolbar icon */ -.mce-i-mailpoet_custom_fields:before +/* TinyMCE mailpoet_shortcodes toolbar icon */ +.mce-i-mailpoet_shortcodes:before font: 400 20px/1 dashicons!important content: "\f307" diff --git a/assets/js/src/newsletter_editor/blocks/footer.js b/assets/js/src/newsletter_editor/blocks/footer.js index 7effb371d4..dfa81dd561 100644 --- a/assets/js/src/newsletter_editor/blocks/footer.js +++ b/assets/js/src/newsletter_editor/blocks/footer.js @@ -56,13 +56,13 @@ define([ inline: true, menubar: false, - toolbar: "bold italic link unlink forecolor mailpoet_custom_fields", + toolbar: "bold italic link unlink forecolor mailpoet_shortcodes", valid_elements: "p[class|style],span[class|style],a[href|class|title|target|style],strong[class|style],em[class|style],strike,br", invalid_elements: "script", block_formats: 'Paragraph=p', - plugins: "link textcolor colorpicker mailpoet_custom_fields", + plugins: "link textcolor colorpicker mailpoet_shortcodes", setup: function(editor) { editor.on('change', function(e) { @@ -78,8 +78,8 @@ define([ }); }, - mailpoet_custom_fields: App.getConfig().get('customFields').toJSON(), - mailpoet_custom_fields_window_title: MailPoet.I18n.t('customFieldsWindowTitle'), + mailpoet_shortcodes: App.getConfig().get('shortcodes').toJSON(), + mailpoet_shortcodes_window_title: MailPoet.I18n.t('shortcodesWindowTitle'), }); }, }); diff --git a/assets/js/src/newsletter_editor/blocks/header.js b/assets/js/src/newsletter_editor/blocks/header.js index 3f5ed47d50..0bf5c23460 100644 --- a/assets/js/src/newsletter_editor/blocks/header.js +++ b/assets/js/src/newsletter_editor/blocks/header.js @@ -56,13 +56,13 @@ define([ inline: true, menubar: false, - toolbar: "bold italic link unlink forecolor mailpoet_custom_fields", + toolbar: "bold italic link unlink forecolor mailpoet_shortcodes", valid_elements: "p[class|style],span[class|style],a[href|class|title|target|style],strong[class|style],em[class|style],strike,br", invalid_elements: "script", block_formats: 'Paragraph=p', - plugins: "link textcolor colorpicker mailpoet_custom_fields", + plugins: "link textcolor colorpicker mailpoet_shortcodes", setup: function(editor) { editor.on('change', function(e) { @@ -78,8 +78,8 @@ define([ }); }, - mailpoet_custom_fields: App.getConfig().get('customFields').toJSON(), - mailpoet_custom_fields_window_title: MailPoet.I18n.t('customFieldsWindowTitle'), + mailpoet_shortcodes: App.getConfig().get('shortcodes').toJSON(), + mailpoet_shortcodes_window_title: MailPoet.I18n.t('shortcodesWindowTitle'), }); }, }); diff --git a/assets/js/src/newsletter_editor/blocks/text.js b/assets/js/src/newsletter_editor/blocks/text.js index 2810eb3b62..44499eddcb 100644 --- a/assets/js/src/newsletter_editor/blocks/text.js +++ b/assets/js/src/newsletter_editor/blocks/text.js @@ -53,14 +53,14 @@ define([ menubar: false, toolbar1: "formatselect bold italic forecolor | link unlink", - toolbar2: "alignleft aligncenter alignright alignjustify | bullist numlist blockquote | code mailpoet_custom_fields", + toolbar2: "alignleft aligncenter alignright alignjustify | bullist numlist blockquote | code mailpoet_shortcodes", //forced_root_block: 'p', valid_elements: "p[class|style],span[class|style],a[href|class|title|target|style],h1[class|style],h2[class|style],h3[class|style],ol[class|style],ul[class|style],li[class|style],strong[class|style],em[class|style],strike,br,blockquote[class|style],table[class|style],tr[class|style],th[class|style],td[class|style]", invalid_elements: "script", block_formats: 'Heading 1=h1;Heading 2=h2;Heading 3=h3;Paragraph=p', - plugins: "link code textcolor colorpicker mailpoet_custom_fields", + plugins: "link code textcolor colorpicker mailpoet_shortcodes", setup: function(editor) { editor.on('change', function(e) { @@ -76,8 +76,8 @@ define([ }); }, - mailpoet_custom_fields: App.getConfig().get('customFields').toJSON(), - mailpoet_custom_fields_window_title: MailPoet.I18n.t('customFieldsWindowTitle'), + mailpoet_shortcodes: App.getConfig().get('shortcodes').toJSON(), + mailpoet_shortcodes_window_title: MailPoet.I18n.t('shortcodesWindowTitle'), }); } }, diff --git a/assets/js/src/newsletter_editor/tinymce/mailpoet_custom_fields/plugin.js b/assets/js/src/newsletter_editor/tinymce/mailpoet_custom_fields/plugin.js deleted file mode 100644 index ca7e5fe434..0000000000 --- a/assets/js/src/newsletter_editor/tinymce/mailpoet_custom_fields/plugin.js +++ /dev/null @@ -1,58 +0,0 @@ -/** - * wysija_custom_fields/plugin.js - * - * TinyMCE plugin for adding dynamic data placeholders to newsletters. - * - * This adds a button to the editor toolbar which displays a modal window of - * available dynamic data placeholder buttons. On click each button inserts - * its placeholder into editor text. - */ - -/*jshint unused:false */ -/*global tinymce:true */ -tinymce.PluginManager.add('mailpoet_custom_fields', function(editor, url) { - var appendLabelAndClose = function(text) { - editor.insertContent('[' + text + ']'); - editor.windowManager.close(); - }, - generateOnClickFunc = function(id) { - return function() { - appendLabelAndClose(id); - }; - }; - - editor.addButton('mailpoet_custom_fields', { - icon: 'mailpoet_custom_fields', - onclick: function() { - var customFields = [], - configCustomFields = editor.settings.mailpoet_custom_fields; - - for (var segment in configCustomFields) { - if (configCustomFields.hasOwnProperty(segment)) { - customFields.push({ - type: 'label', - text: segment, - }); - - for (var i = 0; i < configCustomFields[segment].length; i += 1) { - customFields.push({ - type: 'button', - text: configCustomFields[segment][i].text, - onClick: generateOnClickFunc(configCustomFields[segment][i].shortcode) - }); - } - } - } - - // Open window - editor.windowManager.open({ - height: parseInt(editor.getParam("plugin_mailpoet_custom_fields_height", 400)), - width: parseInt(editor.getParam("plugin_mailpoet_custom_fields_width", 450)), - autoScroll: true, - title: editor.settings.mailpoet_custom_fields_window_title, - body: customFields, - buttons: [], - }); - }, - }); -}); diff --git a/assets/js/src/newsletter_editor/tinymce/mailpoet_shortcodes/plugin.js b/assets/js/src/newsletter_editor/tinymce/mailpoet_shortcodes/plugin.js new file mode 100644 index 0000000000..41609f9c3f --- /dev/null +++ b/assets/js/src/newsletter_editor/tinymce/mailpoet_shortcodes/plugin.js @@ -0,0 +1,58 @@ +/** + * wysija_shortcodes/plugin.js + * + * TinyMCE plugin for adding dynamic data placeholders to newsletters. + * + * This adds a button to the editor toolbar which displays a modal window of + * available dynamic data placeholder buttons. On click each button inserts + * its placeholder into editor text. + */ + +/*jshint unused:false */ +/*global tinymce:true */ +tinymce.PluginManager.add('mailpoet_shortcodes', function(editor, url) { + var appendLabelAndClose = function(text) { + editor.insertContent('[' + text + ']'); + editor.windowManager.close(); + }, + generateOnClickFunc = function(id) { + return function() { + appendLabelAndClose(id); + }; + }; + + editor.addButton('mailpoet_shortcodes', { + icon: 'mailpoet_shortcodes', + onclick: function() { + var shortcodes = [], + configshortcodes = editor.settings.mailpoet_shortcodes; + + for (var segment in configshortcodes) { + if (configshortcodes.hasOwnProperty(segment)) { + shortcodes.push({ + type: 'label', + text: segment, + }); + + for (var i = 0; i < configshortcodes[segment].length; i += 1) { + shortcodes.push({ + type: 'button', + text: configshortcodes[segment][i].text, + onClick: generateOnClickFunc(configshortcodes[segment][i].shortcode) + }); + } + } + } + + // Open window + editor.windowManager.open({ + height: parseInt(editor.getParam("plugin_mailpoet_shortcodes_height", 400)), + width: parseInt(editor.getParam("plugin_mailpoet_shortcodes_width", 450)), + autoScroll: true, + title: editor.settings.mailpoet_shortcodes_window_title, + body: shortcodes, + buttons: [], + }); + }, + }); +}); diff --git a/assets/js/src/newsletter_editor/tinymce/mailpoet_custom_fields/plugin.min.js b/assets/js/src/newsletter_editor/tinymce/mailpoet_shortcodes/plugin.min.js similarity index 100% rename from assets/js/src/newsletter_editor/tinymce/mailpoet_custom_fields/plugin.min.js rename to assets/js/src/newsletter_editor/tinymce/mailpoet_shortcodes/plugin.min.js diff --git a/views/newsletter/editor.html b/views/newsletter/editor.html index c2dc0dd548..fac7cf6537 100644 --- a/views/newsletter/editor.html +++ b/views/newsletter/editor.html @@ -317,7 +317,7 @@ <%= localize({ 'failedToFetchAvailablePosts': __('Failed to fetch available posts'), 'failedToFetchRenderedPosts': __('Failed to fetch rendered posts'), - 'customFieldsWindowTitle': __('Select a shortcode'), + 'shortcodesWindowTitle': __('Select a shortcode'), 'unsubscribeLinkMissing': __('All newsletters must include an "unsubscribe" link. Add a footer widget to your newsletter to continue.'), 'newsletterPreviewEmailMissing': __('Please enter an email where newsletter preview should be sent to.'), 'newsletterPreviewFailed': __('Preview failed. Pleae check console log.'), @@ -340,7 +340,7 @@ <%= javascript( 'lib/tinymce/tinymce.jquery.min.js', 'lib/tinymce/jquery.tinymce.min.js', - 'lib/mailpoet_custom_fields/plugin.js', + 'lib/mailpoet_shortcodes/plugin.js', 'lib/wplink/plugin.js', 'newsletter_editor.js' ) %> From 16cb91990b13a3cc45e72ba3cb15aa83baef389d Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 31 May 2016 12:44:46 -0400 Subject: [PATCH 5/9] - Updates unit test --- lib/Newsletter/Shortcodes/Categories/Link.php | 4 +- .../Shortcodes/Categories/Subscriber.php | 3 +- tests/unit/Newsletter/ShortcodesTest.php | 38 ++++++++++++++++--- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/lib/Newsletter/Shortcodes/Categories/Link.php b/lib/Newsletter/Shortcodes/Categories/Link.php index f22a425c4f..c26f987ec4 100644 --- a/lib/Newsletter/Shortcodes/Categories/Link.php +++ b/lib/Newsletter/Shortcodes/Categories/Link.php @@ -2,7 +2,7 @@ namespace MailPoet\Newsletter\Shortcodes\Categories; use MailPoet\Models\Setting; -use MailPoet\Models\Subscriber; +use MailPoet\Models\Subscriber as SubscriberModel; use MailPoet\Statistics\Track\Unsubscribes; use MailPoet\Subscription\Url as SubscriptionUrl; @@ -102,7 +102,7 @@ class Link { $subscriber['id'] : $subscriber, 'subscriber_token' => (isset($subscriber['id'])) ? - Subscriber::generateToken($subscriber['email']) : + SubscriberModel::generateToken($subscriber['email']) : false, 'queue' => (isset($queue['id'])) ? $queue['id'] : diff --git a/lib/Newsletter/Shortcodes/Categories/Subscriber.php b/lib/Newsletter/Shortcodes/Categories/Subscriber.php index dbbef54ebf..f533ea5ca6 100644 --- a/lib/Newsletter/Shortcodes/Categories/Subscriber.php +++ b/lib/Newsletter/Shortcodes/Categories/Subscriber.php @@ -2,6 +2,7 @@ namespace MailPoet\Newsletter\Shortcodes\Categories; use MailPoet\Models\SubscriberCustomField; +use MailPoet\Models\Subscriber as SubscriberModel; require_once(ABSPATH . 'wp-includes/pluggable.php'); @@ -30,7 +31,7 @@ class Subscriber { return $default_value; break; case 'count': - return Subscriber::filter('subscribed')->count(); + return SubscriberModel::filter('subscribed')->count(); break; case preg_match('/cf_(\d+)/', $action, $custom_field) ? true : false: if(empty($subscriber['id'])) return false; diff --git a/tests/unit/Newsletter/ShortcodesTest.php b/tests/unit/Newsletter/ShortcodesTest.php index 6dce3a37ca..50c394a1d4 100644 --- a/tests/unit/Newsletter/ShortcodesTest.php +++ b/tests/unit/Newsletter/ShortcodesTest.php @@ -1,9 +1,11 @@ equals(2); } - function testItCanProcessUserShortcodes() { + function testItCanProcessSubscriberShortcodes() { $shortcodes_object = $this->shortcodes_object; $result = - $shortcodes_object->process(array('[user:firstname]')); + $shortcodes_object->process(array('[subscriber:firstname]')); expect($result[0])->equals($this->subscriber->first_name); $result = - $shortcodes_object->process(array('[user:lastname]')); + $shortcodes_object->process(array('[subscriber:lastname]')); expect($result[0])->equals($this->subscriber->last_name); $result = - $shortcodes_object->process(array('[user:displayname]')); + $shortcodes_object->process(array('[subscriber:displayname]')); expect($result[0])->equals($this->WP_user->user_login); $subscribers = Subscriber::where('status', 'subscribed') ->findMany(); $subscriber_count = count($subscribers); $result = - $shortcodes_object->process(array('[user:count]')); + $shortcodes_object->process(array('[subscriber:count]')); expect($result[0])->equals($subscriber_count); $this->subscriber->status = 'unsubscribed'; $this->subscriber->save(); $result = - $shortcodes_object->process(array('[user:count]')); + $shortcodes_object->process(array('[subscriber:count]')); expect($result[0])->equals(--$subscriber_count); } + function testItCanProcessSubscriberCustomFieldShortcodes() { + $shortcodes_object = $this->shortcodes_object; + $subscriber = $this->subscriber; + $custom_field = CustomField::create(); + $custom_field->name = 'custom_field_name'; + $custom_field->type = 'text'; + $custom_field->save(); + $result = $shortcodes_object->process( + array('[subscriber:cf_' . $custom_field->id . ']') + ); + expect($result[0])->false(); + $subscriber_custom_field = SubscriberCustomField::create(); + $subscriber_custom_field->subscriber_id = $subscriber->id; + $subscriber_custom_field->custom_field_id = $custom_field->id; + $subscriber_custom_field->value = 'custom_field_value'; + $subscriber_custom_field->save(); + $result = $shortcodes_object->process( + array('[subscriber:cf_' . $custom_field->id . ']') + ); + expect($result[0])->equals($subscriber_custom_field->value); + } + function testItCanProcessLinkShortcodes() { $shortcodes_object = $this->shortcodes_object; $result = @@ -245,6 +269,8 @@ class ShortcodesTest extends MailPoetTest { function _after() { ORM::raw_execute('TRUNCATE ' . Subscriber::$_table); ORM::raw_execute('TRUNCATE ' . SendingQueue::$_table); + ORM::raw_execute('TRUNCATE ' . CustomField::$_table); + ORM::raw_execute('TRUNCATE ' . SubscriberCustomField::$_table); wp_delete_post($this->WP_post, true); wp_delete_user($this->WP_user->ID); } From 26d9b915a2536c392aaa3d2a6874feeb3a954f0c Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 31 May 2016 21:24:05 -0400 Subject: [PATCH 6/9] - Adds unit test for shortcodes helper class --- .../unit/Newsletter/ShortcodesHelperTest.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/unit/Newsletter/ShortcodesHelperTest.php diff --git a/tests/unit/Newsletter/ShortcodesHelperTest.php b/tests/unit/Newsletter/ShortcodesHelperTest.php new file mode 100644 index 0000000000..1fd0edca4a --- /dev/null +++ b/tests/unit/Newsletter/ShortcodesHelperTest.php @@ -0,0 +1,38 @@ +equals( + array( + 'Subscriber', + 'Newsletter', + 'Post Notifications', + 'Date', + 'Links' + ) + ); + } + + function testItCanGetCustomShortShortcodes() { + $shortcodes = ShortcodesHelper::getShortcodes(); + expect(count($shortcodes['Subscriber']))->equals(5); + $custom_field = CustomField::create(); + $custom_field->name = 'name'; + $custom_field->type = 'type'; + $custom_field->save(); + $shortcodes = ShortcodesHelper::getShortcodes(); + expect(count($shortcodes['Subscriber']))->equals(6); + $custom_subscriber_shortcode = end($shortcodes['Subscriber']); + expect($custom_subscriber_shortcode['text'])->equals($custom_field->name); + expect($custom_subscriber_shortcode['shortcode']) + ->equals('subscriber:cf_' . $custom_field->id); + } + + function _after() { + ORM::raw_execute('TRUNCATE ' . CustomField::$_table); + } +} \ No newline at end of file From df0ed9ce53cc2aeb7db3f18c75fd5334863ce501 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tautvidas=20Sipavi=C4=8Dius?= Date: Wed, 1 Jun 2016 16:00:05 +0300 Subject: [PATCH 7/9] Rename mailpoet_custom_fields symlink to mailpoet_shortcodes --- assets/js/lib/mailpoet_custom_fields | 1 - assets/js/lib/mailpoet_shortcodes | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 120000 assets/js/lib/mailpoet_custom_fields create mode 120000 assets/js/lib/mailpoet_shortcodes diff --git a/assets/js/lib/mailpoet_custom_fields b/assets/js/lib/mailpoet_custom_fields deleted file mode 120000 index 8af63bfa98..0000000000 --- a/assets/js/lib/mailpoet_custom_fields +++ /dev/null @@ -1 +0,0 @@ -../src/newsletter_editor/tinymce/mailpoet_custom_fields \ No newline at end of file diff --git a/assets/js/lib/mailpoet_shortcodes b/assets/js/lib/mailpoet_shortcodes new file mode 120000 index 0000000000..6a526a0174 --- /dev/null +++ b/assets/js/lib/mailpoet_shortcodes @@ -0,0 +1 @@ +../src/newsletter_editor/tinymce/mailpoet_shortcodes \ No newline at end of file From a208104fc868ef6c2cb2b4e6867e0c8cf2b0f0fb Mon Sep 17 00:00:00 2001 From: Vlad Date: Wed, 1 Jun 2016 09:40:59 -0400 Subject: [PATCH 8/9] - Fixes naming convention --- .../tinymce/mailpoet_shortcodes/plugin.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/assets/js/src/newsletter_editor/tinymce/mailpoet_shortcodes/plugin.js b/assets/js/src/newsletter_editor/tinymce/mailpoet_shortcodes/plugin.js index 41609f9c3f..e0f08040c9 100644 --- a/assets/js/src/newsletter_editor/tinymce/mailpoet_shortcodes/plugin.js +++ b/assets/js/src/newsletter_editor/tinymce/mailpoet_shortcodes/plugin.js @@ -25,20 +25,20 @@ tinymce.PluginManager.add('mailpoet_shortcodes', function(editor, url) { icon: 'mailpoet_shortcodes', onclick: function() { var shortcodes = [], - configshortcodes = editor.settings.mailpoet_shortcodes; + configShortcodes = editor.settings.mailpoet_shortcodes; - for (var segment in configshortcodes) { - if (configshortcodes.hasOwnProperty(segment)) { + for (var segment in configShortcodes) { + if (configShortcodes.hasOwnProperty(segment)) { shortcodes.push({ type: 'label', text: segment, }); - for (var i = 0; i < configshortcodes[segment].length; i += 1) { + for (var i = 0; i < configShortcodes[segment].length; i += 1) { shortcodes.push({ type: 'button', - text: configshortcodes[segment][i].text, - onClick: generateOnClickFunc(configshortcodes[segment][i].shortcode) + text: configShortcodes[segment][i].text, + onClick: generateOnClickFunc(configShortcodes[segment][i].shortcode) }); } } From 61987a204edb556d437d77c0c5b5f29b34fb24e5 Mon Sep 17 00:00:00 2001 From: Vlad Date: Wed, 1 Jun 2016 09:48:01 -0400 Subject: [PATCH 9/9] - Fixes custom field shortcode matching logic --- .../Shortcodes/Categories/Subscriber.php | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/Newsletter/Shortcodes/Categories/Subscriber.php b/lib/Newsletter/Shortcodes/Categories/Subscriber.php index f533ea5ca6..0ddba1efc4 100644 --- a/lib/Newsletter/Shortcodes/Categories/Subscriber.php +++ b/lib/Newsletter/Shortcodes/Categories/Subscriber.php @@ -1,8 +1,8 @@ user_login; } return $default_value; - break; + break; case 'count': - return SubscriberModel::filter('subscribed')->count(); - break; - case preg_match('/cf_(\d+)/', $action, $custom_field) ? true : false: - if(empty($subscriber['id'])) return false; - $custom_field = SubscriberCustomField - ::where('subscriber_id', $subscriber['id']) - ->where('custom_field_id', $custom_field[1]) - ->findOne(); - return ($custom_field) ? $custom_field->value : false; - break; + return SubscriberModel::filter('subscribed') + ->count(); + break; default: + if(preg_match('/cf_(\d+)/', $action, $custom_field) && + !empty($subscriber['id']) + ) { + $custom_field = SubscriberCustomField + ::where('subscriber_id', $subscriber['id']) + ->where('custom_field_id', $custom_field[1]) + ->findOne(); + return ($custom_field) ? $custom_field->value : false; + } return false; - break; + break; } } } \ No newline at end of file