Passes single array with shortcode details
This commit is contained in:
@ -6,10 +6,7 @@ use MailPoet\WP\Functions as WPFunctions;
|
|||||||
|
|
||||||
class Date {
|
class Date {
|
||||||
static function process(
|
static function process(
|
||||||
$shortcode,
|
$shortcode_details
|
||||||
$action,
|
|
||||||
$action_argument = false,
|
|
||||||
$action_argument_value = false
|
|
||||||
) {
|
) {
|
||||||
$action_mapping = array(
|
$action_mapping = array(
|
||||||
'd' => 'd',
|
'd' => 'd',
|
||||||
@ -19,11 +16,11 @@ class Date {
|
|||||||
'mtext' => 'F',
|
'mtext' => 'F',
|
||||||
'y' => 'Y'
|
'y' => 'Y'
|
||||||
);
|
);
|
||||||
if(!empty($action_mapping[$action])) {
|
if(!empty($action_mapping[$shortcode_details['action']])) {
|
||||||
return date_i18n($action_mapping[$action], WPFunctions::currentTime('timestamp'));
|
return date_i18n($action_mapping[$shortcode_details['action']], WPFunctions::currentTime('timestamp'));
|
||||||
}
|
}
|
||||||
return ($action === 'custom' && $action_argument === 'format') ?
|
return ($shortcode_details['action'] === 'custom' && $shortcode_details['action_argument'] === 'format') ?
|
||||||
date_i18n($action_argument_value, WPFunctions::currentTime('timestamp')) :
|
date_i18n($shortcode_details['action_argument_value'], WPFunctions::currentTime('timestamp')) :
|
||||||
false;
|
false;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||||
|
|
||||||
use MailPoet\Models\Setting;
|
use MailPoet\Models\Setting;
|
||||||
@ -10,20 +11,17 @@ class Link {
|
|||||||
const CATEGORY_NAME = 'link';
|
const CATEGORY_NAME = 'link';
|
||||||
|
|
||||||
static function process(
|
static function process(
|
||||||
$shortcode,
|
$shortcode_details,
|
||||||
$action,
|
|
||||||
$action_argument,
|
|
||||||
$action_argument_value,
|
|
||||||
$newsletter,
|
$newsletter,
|
||||||
$subscriber,
|
$subscriber,
|
||||||
$queue,
|
$queue,
|
||||||
$content,
|
$content,
|
||||||
$wp_user_preview
|
$wp_user_preview
|
||||||
) {
|
) {
|
||||||
switch($action) {
|
switch($shortcode_details['action']) {
|
||||||
case 'subscription_unsubscribe_url':
|
case 'subscription_unsubscribe_url':
|
||||||
return self::processUrl(
|
return self::processUrl(
|
||||||
$action,
|
$shortcode_details['action'],
|
||||||
SubscriptionUrl::getUnsubscribeUrl($wp_user_preview ? false : $subscriber),
|
SubscriptionUrl::getUnsubscribeUrl($wp_user_preview ? false : $subscriber),
|
||||||
$queue,
|
$queue,
|
||||||
$wp_user_preview
|
$wp_user_preview
|
||||||
@ -31,7 +29,7 @@ class Link {
|
|||||||
|
|
||||||
case 'subscription_manage_url':
|
case 'subscription_manage_url':
|
||||||
return self::processUrl(
|
return self::processUrl(
|
||||||
$action,
|
$shortcode_details['action'],
|
||||||
SubscriptionUrl::getManageUrl($wp_user_preview ? false : $subscriber),
|
SubscriptionUrl::getManageUrl($wp_user_preview ? false : $subscriber),
|
||||||
$queue,
|
$queue,
|
||||||
$wp_user_preview
|
$wp_user_preview
|
||||||
@ -45,10 +43,10 @@ class Link {
|
|||||||
$queue,
|
$queue,
|
||||||
$wp_user_preview
|
$wp_user_preview
|
||||||
);
|
);
|
||||||
return self::processUrl($action, $url, $queue, $wp_user_preview);
|
return self::processUrl($shortcode_details['action'], $url, $queue, $wp_user_preview);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
$shortcode = self::getFullShortcode($action);
|
$shortcode = self::getFullShortcode($shortcode_details['action']);
|
||||||
$url = apply_filters(
|
$url = apply_filters(
|
||||||
'mailpoet_newsletter_shortcode_link',
|
'mailpoet_newsletter_shortcode_link',
|
||||||
$shortcode,
|
$shortcode,
|
||||||
@ -58,14 +56,13 @@ class Link {
|
|||||||
$wp_user_preview
|
$wp_user_preview
|
||||||
);
|
);
|
||||||
return ($url !== $shortcode) ?
|
return ($url !== $shortcode) ?
|
||||||
self::processUrl($action, $url, $queue, $wp_user_preview) :
|
self::processUrl($shortcode_details['action'], $url, $queue, $wp_user_preview) :
|
||||||
false;
|
false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static function processUrl($action, $url, $queue, $wp_user_preview = false) {
|
static function processUrl($action, $url, $queue, $wp_user_preview = false) {
|
||||||
if ($wp_user_preview)
|
if($wp_user_preview) return $url;
|
||||||
return $url;
|
|
||||||
return ($queue !== false && (boolean)Setting::getValue('tracking.enabled')) ?
|
return ($queue !== false && (boolean)Setting::getValue('tracking.enabled')) ?
|
||||||
self::getFullShortcode($action) :
|
self::getFullShortcode($action) :
|
||||||
$url;
|
$url;
|
||||||
|
@ -9,16 +9,13 @@ require_once(ABSPATH . "wp-includes/pluggable.php");
|
|||||||
|
|
||||||
class Newsletter {
|
class Newsletter {
|
||||||
static function process(
|
static function process(
|
||||||
$shortcode,
|
$shortcode_details,
|
||||||
$action,
|
|
||||||
$action_argument,
|
|
||||||
$action_argument_value,
|
|
||||||
$newsletter,
|
$newsletter,
|
||||||
$subscriber,
|
$subscriber,
|
||||||
$queue,
|
$queue,
|
||||||
$content
|
$content
|
||||||
) {
|
) {
|
||||||
switch($action) {
|
switch($shortcode_details['action']) {
|
||||||
case 'subject':
|
case 'subject':
|
||||||
return ($newsletter) ? $newsletter->subject : false;
|
return ($newsletter) ? $newsletter->subject : false;
|
||||||
|
|
||||||
|
@ -9,18 +9,15 @@ require_once(ABSPATH . 'wp-includes/pluggable.php');
|
|||||||
|
|
||||||
class Subscriber {
|
class Subscriber {
|
||||||
static function process(
|
static function process(
|
||||||
$shortcode,
|
$shortcode_details,
|
||||||
$action,
|
|
||||||
$action_argument,
|
|
||||||
$action_argument_value,
|
|
||||||
$newsletter,
|
$newsletter,
|
||||||
$subscriber
|
$subscriber
|
||||||
) {
|
) {
|
||||||
if($subscriber !== false && !is_object($subscriber)) return $shortcode;
|
if($subscriber !== false && !is_object($subscriber)) return $shortcode_details['shortcode'];
|
||||||
$default_value = ($action_argument === 'default') ?
|
$default_value = ($shortcode_details['action_argument'] === 'default') ?
|
||||||
$action_argument_value :
|
$shortcode_details['action_argument_value'] :
|
||||||
'';
|
'';
|
||||||
switch($action) {
|
switch($shortcode_details['action']) {
|
||||||
case 'firstname':
|
case 'firstname':
|
||||||
return (!empty($subscriber->first_name)) ? $subscriber->first_name : $default_value;
|
return (!empty($subscriber->first_name)) ? $subscriber->first_name : $default_value;
|
||||||
case 'lastname':
|
case 'lastname':
|
||||||
@ -37,7 +34,7 @@ class Subscriber {
|
|||||||
return SubscriberModel::filter('subscribed')
|
return SubscriberModel::filter('subscribed')
|
||||||
->count();
|
->count();
|
||||||
default:
|
default:
|
||||||
if(preg_match('/cf_(\d+)/', $action, $custom_field) &&
|
if(preg_match('/cf_(\d+)/', $shortcode_details['action'], $custom_field) &&
|
||||||
!empty($subscriber->id)
|
!empty($subscriber->id)
|
||||||
) {
|
) {
|
||||||
$custom_field = SubscriberCustomField
|
$custom_field = SubscriberCustomField
|
||||||
|
@ -49,20 +49,21 @@ class Shortcodes {
|
|||||||
$processed_shortcodes = array_map(
|
$processed_shortcodes = array_map(
|
||||||
function($shortcode) use ($content, $_this) {
|
function($shortcode) use ($content, $_this) {
|
||||||
$shortcode_details = $_this->match($shortcode);
|
$shortcode_details = $_this->match($shortcode);
|
||||||
$shortcode_category = !empty($shortcode_details['category']) ?
|
$shortcode_details['shortcode'] = $shortcode;
|
||||||
ucfirst($shortcode_details['category']) :
|
$shortcode_details['category'] = !empty($shortcode_details['category']) ?
|
||||||
|
$shortcode_details['category'] :
|
||||||
false;
|
false;
|
||||||
$shortcode_action = !empty($shortcode_details['action']) ?
|
$shortcode_details['action'] = !empty($shortcode_details['action']) ?
|
||||||
$shortcode_details['action'] :
|
$shortcode_details['action'] :
|
||||||
false;
|
false;
|
||||||
$shortcode_class =
|
$shortcode_details['action_argument'] = !empty($shortcode_details['argument']) ?
|
||||||
Shortcodes::SHORTCODE_CATEGORY_NAMESPACE . $shortcode_category;
|
|
||||||
$shortcode_argument = !empty($shortcode_details['argument']) ?
|
|
||||||
$shortcode_details['argument'] :
|
$shortcode_details['argument'] :
|
||||||
false;
|
false;
|
||||||
$shortcode_argument_value = !empty($shortcode_details['argument_value']) ?
|
$shortcode_details['action_argument_value'] = !empty($shortcode_details['argument_value']) ?
|
||||||
$shortcode_details['argument_value'] :
|
$shortcode_details['argument_value'] :
|
||||||
false;
|
false;
|
||||||
|
$shortcode_class =
|
||||||
|
Shortcodes::SHORTCODE_CATEGORY_NAMESPACE . ucfirst($shortcode_details['category']);
|
||||||
if(!class_exists($shortcode_class)) {
|
if(!class_exists($shortcode_class)) {
|
||||||
$custom_shortcode = apply_filters(
|
$custom_shortcode = apply_filters(
|
||||||
'mailpoet_newsletter_shortcode',
|
'mailpoet_newsletter_shortcode',
|
||||||
@ -78,10 +79,7 @@ class Shortcodes {
|
|||||||
$custom_shortcode;
|
$custom_shortcode;
|
||||||
}
|
}
|
||||||
return $shortcode_class::process(
|
return $shortcode_class::process(
|
||||||
$shortcode,
|
$shortcode_details,
|
||||||
$shortcode_action,
|
|
||||||
$shortcode_argument,
|
|
||||||
$shortcode_argument_value,
|
|
||||||
$_this->newsletter,
|
$_this->newsletter,
|
||||||
$_this->subscriber,
|
$_this->subscriber,
|
||||||
$_this->queue,
|
$_this->queue,
|
||||||
|
@ -82,15 +82,21 @@ class ShortcodesTest extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function testItCanProcessDateShortcodes() {
|
function testItCanProcessDateShortcodes() {
|
||||||
$date = new \DateTime(current_time('mysql'));
|
$shortcode_details = array('action' => 'd');
|
||||||
expect(Date::process('[date:d]', 'd'))->equals(date_i18n('d', current_time('timestamp')));
|
expect(Date::process($shortcode_details))->equals(date_i18n('d', current_time('timestamp')));
|
||||||
expect(Date::process('[date:dordinal]', 'dordinal'))->equals(date_i18n('dS', current_time('timestamp')));
|
$shortcode_details = array('action' => 'dordinal');
|
||||||
expect(Date::process('[date:dordinal]', 'dtext'))->equals(date_i18n('l', current_time('timestamp')));
|
expect(Date::process($shortcode_details))->equals(date_i18n('dS', current_time('timestamp')));
|
||||||
expect(Date::process('[date:m]', 'm'))->equals(date_i18n('m', current_time('timestamp')));
|
$shortcode_details = array('action' => 'dtext');
|
||||||
expect(Date::process('[date:mtext]', 'mtext'))->equals(date_i18n('F', current_time('timestamp')));
|
expect(Date::process($shortcode_details))->equals(date_i18n('l', current_time('timestamp')));
|
||||||
expect(Date::process('[date:y]', 'y'))->equals(date_i18n('Y', current_time('timestamp')));
|
$shortcode_details = array('action' => 'm');
|
||||||
|
expect(Date::process($shortcode_details))->equals(date_i18n('m', current_time('timestamp')));
|
||||||
|
$shortcode_details = array('action' => 'mtext');
|
||||||
|
expect(Date::process($shortcode_details))->equals(date_i18n('F', current_time('timestamp')));
|
||||||
|
$shortcode_details = array('action' => 'y');
|
||||||
|
expect(Date::process($shortcode_details))->equals(date_i18n('Y', current_time('timestamp')));
|
||||||
// allow custom date formats (http://php.net/manual/en/function.date.php)
|
// allow custom date formats (http://php.net/manual/en/function.date.php)
|
||||||
expect(Date::process('[date:custom|format:U F]', 'custom', 'format', 'U F'))->equals(date_i18n('U F', current_time('timestamp')));
|
$shortcode_details = array('action' => 'custom', 'action_argument' => 'format', 'action_argument_value' => 'U F');
|
||||||
|
expect(Date::process($shortcode_details))->equals(date_i18n('U F', current_time('timestamp')));
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanProcessNewsletterShortcodes() {
|
function testItCanProcessNewsletterShortcodes() {
|
||||||
|
Reference in New Issue
Block a user