Merge pull request #507 from mailpoet/custom_shortcodes
Implements shortcodes for custom fields
This commit is contained in:
@@ -31,7 +31,7 @@ div.mce-toolbar-grp.mce-container
|
|||||||
box-shadow(0px 0px 3px 1px rgba(0, 0, 0, 0.05))
|
box-shadow(0px 0px 3px 1px rgba(0, 0, 0, 0.05))
|
||||||
|
|
||||||
.mce-window
|
.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
|
div.mce-container-body.mce-abs-layout
|
||||||
overflow: hidden
|
overflow: hidden
|
||||||
|
|
||||||
@@ -40,8 +40,8 @@ div.mce-toolbar-grp.mce-container
|
|||||||
width: -webkit-calc( 100% - 36px )
|
width: -webkit-calc( 100% - 36px )
|
||||||
width: calc( 100% - 36px )
|
width: calc( 100% - 36px )
|
||||||
|
|
||||||
/* TinyMCE mailpoet_custom_fields toolbar icon */
|
/* TinyMCE mailpoet_shortcodes toolbar icon */
|
||||||
.mce-i-mailpoet_custom_fields:before
|
.mce-i-mailpoet_shortcodes:before
|
||||||
font: 400 20px/1 dashicons!important
|
font: 400 20px/1 dashicons!important
|
||||||
content: "\f307"
|
content: "\f307"
|
||||||
|
|
||||||
|
@@ -1 +0,0 @@
|
|||||||
../src/newsletter_editor/tinymce/mailpoet_custom_fields
|
|
1
assets/js/lib/mailpoet_shortcodes
Symbolic link
1
assets/js/lib/mailpoet_shortcodes
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
../src/newsletter_editor/tinymce/mailpoet_shortcodes
|
@@ -56,7 +56,7 @@ define([
|
|||||||
inline: true,
|
inline: true,
|
||||||
|
|
||||||
menubar: false,
|
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",
|
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",
|
invalid_elements: "script",
|
||||||
@@ -64,7 +64,7 @@ define([
|
|||||||
relative_urls: false,
|
relative_urls: false,
|
||||||
remove_script_host: false,
|
remove_script_host: false,
|
||||||
|
|
||||||
plugins: "link textcolor colorpicker mailpoet_custom_fields",
|
plugins: "link textcolor colorpicker mailpoet_shortcodes",
|
||||||
|
|
||||||
setup: function(editor) {
|
setup: function(editor) {
|
||||||
editor.on('change', function(e) {
|
editor.on('change', function(e) {
|
||||||
@@ -80,8 +80,8 @@ define([
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
mailpoet_custom_fields: App.getConfig().get('customFields').toJSON(),
|
mailpoet_shortcodes: App.getConfig().get('shortcodes').toJSON(),
|
||||||
mailpoet_custom_fields_window_title: MailPoet.I18n.t('customFieldsWindowTitle'),
|
mailpoet_shortcodes_window_title: MailPoet.I18n.t('shortcodesWindowTitle'),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@@ -56,7 +56,7 @@ define([
|
|||||||
inline: true,
|
inline: true,
|
||||||
|
|
||||||
menubar: false,
|
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",
|
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",
|
invalid_elements: "script",
|
||||||
@@ -64,7 +64,7 @@ define([
|
|||||||
relative_urls: false,
|
relative_urls: false,
|
||||||
remove_script_host: false,
|
remove_script_host: false,
|
||||||
|
|
||||||
plugins: "link textcolor colorpicker mailpoet_custom_fields",
|
plugins: "link textcolor colorpicker mailpoet_shortcodes",
|
||||||
|
|
||||||
setup: function(editor) {
|
setup: function(editor) {
|
||||||
editor.on('change', function(e) {
|
editor.on('change', function(e) {
|
||||||
@@ -80,8 +80,8 @@ define([
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
mailpoet_custom_fields: App.getConfig().get('customFields').toJSON(),
|
mailpoet_shortcodes: App.getConfig().get('shortcodes').toJSON(),
|
||||||
mailpoet_custom_fields_window_title: MailPoet.I18n.t('customFieldsWindowTitle'),
|
mailpoet_shortcodes_window_title: MailPoet.I18n.t('shortcodesWindowTitle'),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@@ -53,7 +53,7 @@ define([
|
|||||||
|
|
||||||
menubar: false,
|
menubar: false,
|
||||||
toolbar1: "formatselect bold italic forecolor | link unlink",
|
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',
|
//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]",
|
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]",
|
||||||
@@ -62,7 +62,7 @@ define([
|
|||||||
relative_urls: false,
|
relative_urls: false,
|
||||||
remove_script_host: false,
|
remove_script_host: false,
|
||||||
|
|
||||||
plugins: "link code textcolor colorpicker mailpoet_custom_fields",
|
plugins: "link code textcolor colorpicker mailpoet_shortcodes",
|
||||||
|
|
||||||
setup: function(editor) {
|
setup: function(editor) {
|
||||||
editor.on('change', function(e) {
|
editor.on('change', function(e) {
|
||||||
@@ -78,8 +78,8 @@ define([
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
mailpoet_custom_fields: App.getConfig().get('customFields').toJSON(),
|
mailpoet_shortcodes: App.getConfig().get('shortcodes').toJSON(),
|
||||||
mailpoet_custom_fields_window_title: MailPoet.I18n.t('customFieldsWindowTitle'),
|
mailpoet_shortcodes_window_title: MailPoet.I18n.t('shortcodesWindowTitle'),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@@ -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: [],
|
|
||||||
});
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
@@ -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: [],
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
@@ -7,6 +7,7 @@ use MailPoet\Models\CustomField;
|
|||||||
use MailPoet\Models\Form;
|
use MailPoet\Models\Form;
|
||||||
use MailPoet\Models\Segment;
|
use MailPoet\Models\Segment;
|
||||||
use MailPoet\Models\Setting;
|
use MailPoet\Models\Setting;
|
||||||
|
use MailPoet\Newsletter\Shortcodes\ShortcodesHelper;
|
||||||
use MailPoet\Settings\Charsets;
|
use MailPoet\Settings\Charsets;
|
||||||
use MailPoet\Settings\Hosts;
|
use MailPoet\Settings\Hosts;
|
||||||
use MailPoet\Settings\Pages;
|
use MailPoet\Settings\Pages;
|
||||||
@@ -419,15 +420,8 @@ class Menu {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function newletterEditor() {
|
function newletterEditor() {
|
||||||
$custom_fields = array_map(function($field) {
|
|
||||||
return array(
|
|
||||||
'text' => $field['name'],
|
|
||||||
'shortcode' => 'field:' . $field['id'],
|
|
||||||
);
|
|
||||||
}, CustomField::findArray());
|
|
||||||
|
|
||||||
$data = array(
|
$data = array(
|
||||||
'customFields' => $custom_fields,
|
'shortcodes' => ShortcodesHelper::getShortcodes(),
|
||||||
'settings' => Setting::getAll(),
|
'settings' => Setting::getAll(),
|
||||||
'sub_menu' => 'mailpoet-newsletters'
|
'sub_menu' => 'mailpoet-newsletters'
|
||||||
);
|
);
|
||||||
|
@@ -2,32 +2,6 @@
|
|||||||
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||||
|
|
||||||
class Date {
|
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) {
|
static function process($action) {
|
||||||
$date = new \DateTime('now');
|
$date = new \DateTime('now');
|
||||||
$actions = array(
|
$actions = array(
|
||||||
|
@@ -2,25 +2,11 @@
|
|||||||
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||||
|
|
||||||
use MailPoet\Models\Setting;
|
use MailPoet\Models\Setting;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber as SubscriberModel;
|
||||||
use MailPoet\Statistics\Track\Unsubscribes;
|
use MailPoet\Statistics\Track\Unsubscribes;
|
||||||
use MailPoet\Subscription\Url as SubscriptionUrl;
|
use MailPoet\Subscription\Url as SubscriptionUrl;
|
||||||
|
|
||||||
class Link {
|
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,
|
static function process($action,
|
||||||
$default_value = false,
|
$default_value = false,
|
||||||
$newsletter,
|
$newsletter,
|
||||||
@@ -116,7 +102,7 @@ class Link {
|
|||||||
$subscriber['id'] :
|
$subscriber['id'] :
|
||||||
$subscriber,
|
$subscriber,
|
||||||
'subscriber_token' => (isset($subscriber['id'])) ?
|
'subscriber_token' => (isset($subscriber['id'])) ?
|
||||||
Subscriber::generateToken($subscriber['email']) :
|
SubscriberModel::generateToken($subscriber['email']) :
|
||||||
false,
|
false,
|
||||||
'queue' => (isset($queue['id'])) ?
|
'queue' => (isset($queue['id'])) ?
|
||||||
$queue['id'] :
|
$queue['id'] :
|
||||||
|
@@ -7,28 +7,6 @@ use MailPoet\Newsletter\Shortcodes\ShortcodesHelper;
|
|||||||
require_once( ABSPATH . "wp-includes/pluggable.php" );
|
require_once( ABSPATH . "wp-includes/pluggable.php" );
|
||||||
|
|
||||||
class Newsletter {
|
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,
|
static function process($action,
|
||||||
$default_value = false,
|
$default_value = false,
|
||||||
$newsletter,
|
$newsletter,
|
||||||
|
@@ -1,67 +1,51 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||||
|
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber as SubscriberModel;
|
||||||
|
use MailPoet\Models\SubscriberCustomField;
|
||||||
require_once(ABSPATH . 'wp-includes/pluggable.php');
|
|
||||||
|
require_once(ABSPATH . 'wp-includes/pluggable.php');
|
||||||
class User {
|
|
||||||
/*
|
class Subscriber {
|
||||||
{
|
static function process(
|
||||||
text: '<%= __('First Name') %>',
|
$action,
|
||||||
shortcode: 'user:firstname | default:reader',
|
$default_value,
|
||||||
},
|
$newsletter = false,
|
||||||
{
|
$subscriber
|
||||||
text: '<%= __('Last Name') %>',
|
) {
|
||||||
shortcode: 'user:lastname | default:reader',
|
switch($action) {
|
||||||
},
|
case 'firstname':
|
||||||
{
|
return ($subscriber) ? $subscriber['first_name'] : $default_value;
|
||||||
text: '<%= __('Email Address') %>',
|
break;
|
||||||
shortcode: 'user:email',
|
case 'lastname':
|
||||||
},
|
return ($subscriber) ? $subscriber['last_name'] : $default_value;
|
||||||
{
|
break;
|
||||||
text: '<%= __('Wordpress user display name') %>',
|
case 'email':
|
||||||
shortcode: 'user:displayname | default:member',
|
return ($subscriber) ? $subscriber['email'] : false;
|
||||||
},
|
break;
|
||||||
{
|
case 'displayname':
|
||||||
text: '<%= __('Total of subscribers') %>',
|
if($subscriber && $subscriber['wp_user_id']) {
|
||||||
shortcode: 'user:count',
|
$wp_user = get_userdata($subscriber['wp_user_id']);
|
||||||
}
|
return $wp_user->user_login;
|
||||||
*/
|
}
|
||||||
static function process(
|
return $default_value;
|
||||||
$action,
|
break;
|
||||||
$default_value,
|
case 'count':
|
||||||
$newsletter = false,
|
return SubscriberModel::filter('subscribed')
|
||||||
$subscriber
|
->count();
|
||||||
) {
|
break;
|
||||||
switch($action) {
|
default:
|
||||||
case 'firstname':
|
if(preg_match('/cf_(\d+)/', $action, $custom_field) &&
|
||||||
return ($subscriber) ? $subscriber['first_name'] : $default_value;
|
!empty($subscriber['id'])
|
||||||
break;
|
) {
|
||||||
|
$custom_field = SubscriberCustomField
|
||||||
case 'lastname':
|
::where('subscriber_id', $subscriber['id'])
|
||||||
return ($subscriber) ? $subscriber['last_name'] : $default_value;
|
->where('custom_field_id', $custom_field[1])
|
||||||
break;
|
->findOne();
|
||||||
|
return ($custom_field) ? $custom_field->value : false;
|
||||||
case 'email':
|
}
|
||||||
return ($subscriber) ? $subscriber['email'] : false;
|
return false;
|
||||||
break;
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
113
lib/Newsletter/Shortcodes/ShortcodesHelper.php
Normal file
113
lib/Newsletter/Shortcodes/ShortcodesHelper.php
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
<?php
|
||||||
|
namespace MailPoet\Newsletter\Shortcodes;
|
||||||
|
|
||||||
|
use MailPoet\Models\CustomField;
|
||||||
|
|
||||||
|
class ShortcodesHelper {
|
||||||
|
|
||||||
|
static function getShortcodes() {
|
||||||
|
$shortcodes = array(
|
||||||
|
__('Subscriber') => 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);
|
||||||
|
}
|
||||||
|
}
|
38
tests/unit/Newsletter/ShortcodesHelperTest.php
Normal file
38
tests/unit/Newsletter/ShortcodesHelperTest.php
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use MailPoet\Models\CustomField;
|
||||||
|
use MailPoet\Newsletter\Shortcodes\ShortcodesHelper;
|
||||||
|
|
||||||
|
class ShortcodesHelperTest extends MailPoetTest {
|
||||||
|
function testItCanGetShortcodes() {
|
||||||
|
$shortcodes = ShortcodesHelper::getShortcodes();
|
||||||
|
expect(array_keys($shortcodes))->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);
|
||||||
|
}
|
||||||
|
}
|
@@ -1,9 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use MailPoet\Config\Populator;
|
use MailPoet\Config\Populator;
|
||||||
|
use MailPoet\Models\CustomField;
|
||||||
use MailPoet\Models\SendingQueue;
|
use MailPoet\Models\SendingQueue;
|
||||||
use MailPoet\Models\Setting;
|
use MailPoet\Models\Setting;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
|
use MailPoet\Models\SubscriberCustomField;
|
||||||
use MailPoet\Newsletter\Shortcodes\Categories\Date;
|
use MailPoet\Newsletter\Shortcodes\Categories\Date;
|
||||||
|
|
||||||
require_once(ABSPATH . 'wp-includes/pluggable.php');
|
require_once(ABSPATH . 'wp-includes/pluggable.php');
|
||||||
@@ -113,30 +115,52 @@ class ShortcodesTest extends MailPoetTest {
|
|||||||
expect($result['0'])->equals(2);
|
expect($result['0'])->equals(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanProcessUserShortcodes() {
|
function testItCanProcessSubscriberShortcodes() {
|
||||||
$shortcodes_object = $this->shortcodes_object;
|
$shortcodes_object = $this->shortcodes_object;
|
||||||
$result =
|
$result =
|
||||||
$shortcodes_object->process(array('[user:firstname]'));
|
$shortcodes_object->process(array('[subscriber:firstname]'));
|
||||||
expect($result[0])->equals($this->subscriber->first_name);
|
expect($result[0])->equals($this->subscriber->first_name);
|
||||||
$result =
|
$result =
|
||||||
$shortcodes_object->process(array('[user:lastname]'));
|
$shortcodes_object->process(array('[subscriber:lastname]'));
|
||||||
expect($result[0])->equals($this->subscriber->last_name);
|
expect($result[0])->equals($this->subscriber->last_name);
|
||||||
$result =
|
$result =
|
||||||
$shortcodes_object->process(array('[user:displayname]'));
|
$shortcodes_object->process(array('[subscriber:displayname]'));
|
||||||
expect($result[0])->equals($this->WP_user->user_login);
|
expect($result[0])->equals($this->WP_user->user_login);
|
||||||
$subscribers = Subscriber::where('status', 'subscribed')
|
$subscribers = Subscriber::where('status', 'subscribed')
|
||||||
->findMany();
|
->findMany();
|
||||||
$subscriber_count = count($subscribers);
|
$subscriber_count = count($subscribers);
|
||||||
$result =
|
$result =
|
||||||
$shortcodes_object->process(array('[user:count]'));
|
$shortcodes_object->process(array('[subscriber:count]'));
|
||||||
expect($result[0])->equals($subscriber_count);
|
expect($result[0])->equals($subscriber_count);
|
||||||
$this->subscriber->status = 'unsubscribed';
|
$this->subscriber->status = 'unsubscribed';
|
||||||
$this->subscriber->save();
|
$this->subscriber->save();
|
||||||
$result =
|
$result =
|
||||||
$shortcodes_object->process(array('[user:count]'));
|
$shortcodes_object->process(array('[subscriber:count]'));
|
||||||
expect($result[0])->equals(--$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() {
|
function testItCanProcessLinkShortcodes() {
|
||||||
$shortcodes_object = $this->shortcodes_object;
|
$shortcodes_object = $this->shortcodes_object;
|
||||||
$result =
|
$result =
|
||||||
@@ -245,6 +269,8 @@ class ShortcodesTest extends MailPoetTest {
|
|||||||
function _after() {
|
function _after() {
|
||||||
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||||
ORM::raw_execute('TRUNCATE ' . SendingQueue::$_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_post($this->WP_post, true);
|
||||||
wp_delete_user($this->WP_user->ID);
|
wp_delete_user($this->WP_user->ID);
|
||||||
}
|
}
|
||||||
|
@@ -317,7 +317,7 @@
|
|||||||
<%= localize({
|
<%= localize({
|
||||||
'failedToFetchAvailablePosts': __('Failed to fetch available posts'),
|
'failedToFetchAvailablePosts': __('Failed to fetch available posts'),
|
||||||
'failedToFetchRenderedPosts': __('Failed to fetch rendered 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.'),
|
'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.'),
|
'newsletterPreviewEmailMissing': __('Please enter an email where newsletter preview should be sent to.'),
|
||||||
'newsletterPreviewFailed': __('Preview failed. Pleae check console log.'),
|
'newsletterPreviewFailed': __('Preview failed. Pleae check console log.'),
|
||||||
@@ -340,7 +340,7 @@
|
|||||||
<%= javascript(
|
<%= javascript(
|
||||||
'lib/tinymce/tinymce.jquery.min.js',
|
'lib/tinymce/tinymce.jquery.min.js',
|
||||||
'lib/tinymce/jquery.tinymce.min.js',
|
'lib/tinymce/jquery.tinymce.min.js',
|
||||||
'lib/mailpoet_custom_fields/plugin.js',
|
'lib/mailpoet_shortcodes/plugin.js',
|
||||||
'lib/wplink/plugin.js',
|
'lib/wplink/plugin.js',
|
||||||
'newsletter_editor.js'
|
'newsletter_editor.js'
|
||||||
) %>
|
) %>
|
||||||
@@ -1168,93 +1168,7 @@
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
customFields: {
|
shortcodes: <%= json_encode(shortcodes) %>,
|
||||||
'<%= __('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 %>
|
|
||||||
},
|
|
||||||
sidepanelWidth: '331px',
|
sidepanelWidth: '331px',
|
||||||
validation: {
|
validation: {
|
||||||
validateUnsubscribeLinkPresent: true, // TODO: Add validation based on whether Mailpoet MTA is used or not
|
validateUnsubscribeLinkPresent: true, // TODO: Add validation based on whether Mailpoet MTA is used or not
|
||||||
|
Reference in New Issue
Block a user