- Adds custom date format as a shortcode action's argument
- Updates shortcodes logic to allow action arguments other than "default"
This commit is contained in:
@@ -2,9 +2,13 @@
|
|||||||
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||||
|
|
||||||
class Date {
|
class Date {
|
||||||
static function process($format) {
|
static function process(
|
||||||
|
$action,
|
||||||
|
$action_argument = false,
|
||||||
|
$action_argument_value = false
|
||||||
|
) {
|
||||||
$date = new \DateTime('now');
|
$date = new \DateTime('now');
|
||||||
$available_formats = array(
|
$action_formats = array(
|
||||||
'd' => $date->format('d'),
|
'd' => $date->format('d'),
|
||||||
'dordinal' => $date->format('dS'),
|
'dordinal' => $date->format('dS'),
|
||||||
'dtext' => $date->format('l'),
|
'dtext' => $date->format('l'),
|
||||||
@@ -12,11 +16,11 @@ class Date {
|
|||||||
'mtext' => $date->format('F'),
|
'mtext' => $date->format('F'),
|
||||||
'y' => $date->format('Y')
|
'y' => $date->format('Y')
|
||||||
);
|
);
|
||||||
if(!empty($available_formats[$format])) {
|
if(!empty($action_formats[$action])) {
|
||||||
return $available_formats[$format];
|
return $action_formats[$action];
|
||||||
}
|
}
|
||||||
return (preg_match('/^custom_(.*?)$/', $format, $custom_format)) ?
|
return ($action === 'custom' && $action_argument === 'format') ?
|
||||||
$date->format($custom_format[1]) :
|
$date->format($action_argument_value) :
|
||||||
false;
|
false;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -9,7 +9,8 @@ use MailPoet\Subscription\Url as SubscriptionUrl;
|
|||||||
class Link {
|
class Link {
|
||||||
static function process(
|
static function process(
|
||||||
$action,
|
$action,
|
||||||
$default_value,
|
$action_argument,
|
||||||
|
$action_argument_value,
|
||||||
$newsletter,
|
$newsletter,
|
||||||
$subscriber,
|
$subscriber,
|
||||||
$queue,
|
$queue,
|
||||||
|
@@ -8,7 +8,8 @@ require_once(ABSPATH . "wp-includes/pluggable.php");
|
|||||||
|
|
||||||
class Newsletter {
|
class Newsletter {
|
||||||
static function process($action,
|
static function process($action,
|
||||||
$default_value,
|
$action_argument,
|
||||||
|
$action_argument_value,
|
||||||
$newsletter,
|
$newsletter,
|
||||||
$subscriber,
|
$subscriber,
|
||||||
$queue,
|
$queue,
|
||||||
|
@@ -10,11 +10,15 @@ require_once(ABSPATH . 'wp-includes/pluggable.php');
|
|||||||
class Subscriber {
|
class Subscriber {
|
||||||
static function process(
|
static function process(
|
||||||
$action,
|
$action,
|
||||||
$default_value,
|
$action_argument,
|
||||||
|
$action_argument_value,
|
||||||
$newsletter,
|
$newsletter,
|
||||||
$subscriber
|
$subscriber
|
||||||
) {
|
) {
|
||||||
if($subscriber !== false && !is_object($subscriber)) return false;
|
if($subscriber !== false && !is_object($subscriber)) return false;
|
||||||
|
$default_value = ($action_argument === 'default') ?
|
||||||
|
$action_argument_value :
|
||||||
|
'';
|
||||||
switch($action) {
|
switch($action) {
|
||||||
case 'firstname':
|
case 'firstname':
|
||||||
return ($subscriber) ? $subscriber->first_name : $default_value;
|
return ($subscriber) ? $subscriber->first_name : $default_value;
|
||||||
|
@@ -37,7 +37,7 @@ class Shortcodes {
|
|||||||
|
|
||||||
function match($shortcode) {
|
function match($shortcode) {
|
||||||
preg_match(
|
preg_match(
|
||||||
'/\[(?P<category>\w+)?:(?P<action>\w+)(?:.*?\|.*?default:(?P<default>.*?))?\]/',
|
'/\[(?P<category>\w+)?:(?P<action>\w+)(?:.*?\|.*?(?P<argument>\w+):(?P<argument_value>.*?))?\]/',
|
||||||
$shortcode,
|
$shortcode,
|
||||||
$match
|
$match
|
||||||
);
|
);
|
||||||
@@ -57,8 +57,11 @@ class Shortcodes {
|
|||||||
false;
|
false;
|
||||||
$shortcode_class =
|
$shortcode_class =
|
||||||
Shortcodes::SHORTCODE_CATEGORY_NAMESPACE . $shortcode_category;
|
Shortcodes::SHORTCODE_CATEGORY_NAMESPACE . $shortcode_category;
|
||||||
$shortcode_default_value = !empty($shortcode_details['default']) ?
|
$shortcode_argument = !empty($shortcode_details['argument']) ?
|
||||||
$shortcode_details['default'] :
|
$shortcode_details['argument'] :
|
||||||
|
false;
|
||||||
|
$shortcode_argument_value = !empty($shortcode_details['argument_value']) ?
|
||||||
|
$shortcode_details['argument_value'] :
|
||||||
false;
|
false;
|
||||||
if(!class_exists($shortcode_class)) {
|
if(!class_exists($shortcode_class)) {
|
||||||
$custom_shortcode = apply_filters(
|
$custom_shortcode = apply_filters(
|
||||||
@@ -76,7 +79,8 @@ class Shortcodes {
|
|||||||
}
|
}
|
||||||
return $shortcode_class::process(
|
return $shortcode_class::process(
|
||||||
$shortcode_action,
|
$shortcode_action,
|
||||||
$shortcode_default_value,
|
$shortcode_argument,
|
||||||
|
$shortcode_argument_value,
|
||||||
$_this->newsletter,
|
$_this->newsletter,
|
||||||
$_this->subscriber,
|
$_this->subscriber,
|
||||||
$_this->queue,
|
$_this->queue,
|
||||||
|
@@ -55,7 +55,8 @@ class ShortcodesTest extends MailPoetTest {
|
|||||||
$details = $shortcodes_object->match($content);
|
$details = $shortcodes_object->match($content);
|
||||||
expect($details['category'])->equals('category');
|
expect($details['category'])->equals('category');
|
||||||
expect($details['action'])->equals('action');
|
expect($details['action'])->equals('action');
|
||||||
expect($details['default'])->equals('default_value');
|
expect($details['argument'])->equals('default');
|
||||||
|
expect($details['argument_value'])->equals('default_value');
|
||||||
$content = '[category:action|default]';
|
$content = '[category:action|default]';
|
||||||
$details = $shortcodes_object->match($content);
|
$details = $shortcodes_object->match($content);
|
||||||
expect($details)->isEmpty();
|
expect($details)->isEmpty();
|
||||||
@@ -86,7 +87,7 @@ class ShortcodesTest extends MailPoetTest {
|
|||||||
expect(Date::process('mtext'))->equals($date->format('F'));
|
expect(Date::process('mtext'))->equals($date->format('F'));
|
||||||
expect(Date::process('y'))->equals($date->format('Y'));
|
expect(Date::process('y'))->equals($date->format('Y'));
|
||||||
// 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('custom_U'))->equals($date->format('U'));
|
expect(Date::process('custom', 'format', 'U'))->equals($date->format('U'));
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanProcessNewsletterShortcodes() {
|
function testItCanProcessNewsletterShortcodes() {
|
||||||
|
Reference in New Issue
Block a user