diff --git a/lib/Newsletter/Shortcodes/Categories/Date.php b/lib/Newsletter/Shortcodes/Categories/Date.php index b276706145..350ef79573 100644 --- a/lib/Newsletter/Shortcodes/Categories/Date.php +++ b/lib/Newsletter/Shortcodes/Categories/Date.php @@ -6,10 +6,7 @@ use MailPoet\WP\Functions as WPFunctions; class Date { static function process( - $shortcode, - $action, - $action_argument = false, - $action_argument_value = false + $shortcode_details ) { $action_mapping = array( 'd' => 'd', @@ -19,11 +16,11 @@ class Date { 'mtext' => 'F', 'y' => 'Y' ); - if(!empty($action_mapping[$action])) { - return date_i18n($action_mapping[$action], WPFunctions::currentTime('timestamp')); + if(!empty($action_mapping[$shortcode_details['action']])) { + return date_i18n($action_mapping[$shortcode_details['action']], WPFunctions::currentTime('timestamp')); } - return ($action === 'custom' && $action_argument === 'format') ? - date_i18n($action_argument_value, WPFunctions::currentTime('timestamp')) : + return ($shortcode_details['action'] === 'custom' && $shortcode_details['action_argument'] === 'format') ? + date_i18n($shortcode_details['action_argument_value'], WPFunctions::currentTime('timestamp')) : false; } } \ No newline at end of file diff --git a/lib/Newsletter/Shortcodes/Categories/Link.php b/lib/Newsletter/Shortcodes/Categories/Link.php index f8e3c787b8..9ed60427f8 100644 --- a/lib/Newsletter/Shortcodes/Categories/Link.php +++ b/lib/Newsletter/Shortcodes/Categories/Link.php @@ -1,4 +1,5 @@ subject : false; diff --git a/lib/Newsletter/Shortcodes/Categories/Subscriber.php b/lib/Newsletter/Shortcodes/Categories/Subscriber.php index 0f39b856c5..7a18ed3c9d 100644 --- a/lib/Newsletter/Shortcodes/Categories/Subscriber.php +++ b/lib/Newsletter/Shortcodes/Categories/Subscriber.php @@ -9,18 +9,15 @@ require_once(ABSPATH . 'wp-includes/pluggable.php'); class Subscriber { static function process( - $shortcode, - $action, - $action_argument, - $action_argument_value, + $shortcode_details, $newsletter, $subscriber ) { - if($subscriber !== false && !is_object($subscriber)) return $shortcode; - $default_value = ($action_argument === 'default') ? - $action_argument_value : + if($subscriber !== false && !is_object($subscriber)) return $shortcode_details['shortcode']; + $default_value = ($shortcode_details['action_argument'] === 'default') ? + $shortcode_details['action_argument_value'] : ''; - switch($action) { + switch($shortcode_details['action']) { case 'firstname': return (!empty($subscriber->first_name)) ? $subscriber->first_name : $default_value; case 'lastname': @@ -37,7 +34,7 @@ class Subscriber { return SubscriberModel::filter('subscribed') ->count(); default: - if(preg_match('/cf_(\d+)/', $action, $custom_field) && + if(preg_match('/cf_(\d+)/', $shortcode_details['action'], $custom_field) && !empty($subscriber->id) ) { $custom_field = SubscriberCustomField diff --git a/lib/Newsletter/Shortcodes/Shortcodes.php b/lib/Newsletter/Shortcodes/Shortcodes.php index a93cd674af..2bb10350a0 100644 --- a/lib/Newsletter/Shortcodes/Shortcodes.php +++ b/lib/Newsletter/Shortcodes/Shortcodes.php @@ -49,20 +49,21 @@ class Shortcodes { $processed_shortcodes = array_map( function($shortcode) use ($content, $_this) { $shortcode_details = $_this->match($shortcode); - $shortcode_category = !empty($shortcode_details['category']) ? - ucfirst($shortcode_details['category']) : + $shortcode_details['shortcode'] = $shortcode; + $shortcode_details['category'] = !empty($shortcode_details['category']) ? + $shortcode_details['category'] : false; - $shortcode_action = !empty($shortcode_details['action']) ? + $shortcode_details['action'] = !empty($shortcode_details['action']) ? $shortcode_details['action'] : false; - $shortcode_class = - Shortcodes::SHORTCODE_CATEGORY_NAMESPACE . $shortcode_category; - $shortcode_argument = !empty($shortcode_details['argument']) ? + $shortcode_details['action_argument'] = !empty($shortcode_details['argument']) ? $shortcode_details['argument'] : false; - $shortcode_argument_value = !empty($shortcode_details['argument_value']) ? + $shortcode_details['action_argument_value'] = !empty($shortcode_details['argument_value']) ? $shortcode_details['argument_value'] : false; + $shortcode_class = + Shortcodes::SHORTCODE_CATEGORY_NAMESPACE . ucfirst($shortcode_details['category']); if(!class_exists($shortcode_class)) { $custom_shortcode = apply_filters( 'mailpoet_newsletter_shortcode', @@ -78,10 +79,7 @@ class Shortcodes { $custom_shortcode; } return $shortcode_class::process( - $shortcode, - $shortcode_action, - $shortcode_argument, - $shortcode_argument_value, + $shortcode_details, $_this->newsletter, $_this->subscriber, $_this->queue, diff --git a/tests/unit/Newsletter/ShortcodesTest.php b/tests/unit/Newsletter/ShortcodesTest.php index 68b65a150f..7d29c6338c 100644 --- a/tests/unit/Newsletter/ShortcodesTest.php +++ b/tests/unit/Newsletter/ShortcodesTest.php @@ -82,15 +82,21 @@ class ShortcodesTest extends \MailPoetTest { } function testItCanProcessDateShortcodes() { - $date = new \DateTime(current_time('mysql')); - expect(Date::process('[date:d]', 'd'))->equals(date_i18n('d', current_time('timestamp'))); - expect(Date::process('[date:dordinal]', 'dordinal'))->equals(date_i18n('dS', current_time('timestamp'))); - expect(Date::process('[date:dordinal]', 'dtext'))->equals(date_i18n('l', current_time('timestamp'))); - expect(Date::process('[date:m]', 'm'))->equals(date_i18n('m', current_time('timestamp'))); - expect(Date::process('[date:mtext]', 'mtext'))->equals(date_i18n('F', current_time('timestamp'))); - expect(Date::process('[date:y]', 'y'))->equals(date_i18n('Y', current_time('timestamp'))); + $shortcode_details = array('action' => 'd'); + expect(Date::process($shortcode_details))->equals(date_i18n('d', current_time('timestamp'))); + $shortcode_details = array('action' => 'dordinal'); + expect(Date::process($shortcode_details))->equals(date_i18n('dS', current_time('timestamp'))); + $shortcode_details = array('action' => 'dtext'); + expect(Date::process($shortcode_details))->equals(date_i18n('l', 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) - 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() {