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