diff --git a/lib/Cron/Workers/SendingQueue.php b/lib/Cron/Workers/SendingQueue.php index 528234f48d..7e57fac328 100644 --- a/lib/Cron/Workers/SendingQueue.php +++ b/lib/Cron/Workers/SendingQueue.php @@ -10,7 +10,6 @@ use MailPoet\Models\Subscriber; use MailPoet\Newsletter\Renderer\Renderer; use MailPoet\Newsletter\Shortcodes\Shortcodes; use MailPoet\Util\Helpers; -use MailPoet\Util\Security; if(!defined('ABSPATH')) exit; @@ -22,13 +21,13 @@ class SendingQueue { const batch_size = 50; function __construct($timer = false) { - $this->mta_config = $this->getMailerConfig(); +/* $this->mta_config = $this->getMailerConfig(); $this->mta_log = $this->getMailerLog(); $this->processing_method = ($this->mta_config['method'] === 'MailPoet') ? 'processBulkSubscribers' : 'processIndividualSubscriber'; $this->timer = ($timer) ? $timer : microtime(true); - CronHelper::checkExecutionTimer($this->timer); + CronHelper::checkExecutionTimer($this->timer);*/ } function process() { @@ -87,7 +86,7 @@ class SendingQueue { ); } else { $newsletter_statistics = - array_map(function ($data) use ($newsletter, $subscribers_ids, $queue) { + array_map(function($data) use ($newsletter, $subscribers_ids, $queue) { return array( $newsletter['id'], $subscribers_ids[$data], @@ -147,13 +146,14 @@ class SendingQueue { function processNewsletter($newsletter, $subscriber = false) { $divider = '***MailPoet***'; + $body = implode($divider, $newsletter['body']); $shortcodes = new Shortcodes( - implode($divider, $newsletter['body']), $newsletter, $subscriber ); list($newsletter['body']['html'], $newsletter['body']['text']) = - explode($divider, $shortcodes->replace()); + explode($divider, $shortcodes->replace($body)); + $newsletter['subject'] = $shortcodes->replace($newsletter['subject']); return $newsletter; } diff --git a/lib/Newsletter/Editor/PostTransformer.php b/lib/Newsletter/Editor/PostTransformer.php index 52bf232c4f..ebc7c68642 100644 --- a/lib/Newsletter/Editor/PostTransformer.php +++ b/lib/Newsletter/Editor/PostTransformer.php @@ -169,6 +169,6 @@ class PostTransformer { $alignment = (in_array($this->args['titleAlignment'], array('left', 'right', 'center'))) ? $this->args['titleAlignment'] : 'left'; - return '<' . $tag . ' style="text-align: ' . $alignment . '">' . $title . ''; + return '<' . $tag . ' class="mailpoet_alc_post" style="text-align: ' . $alignment . '">' . $title . ''; } } diff --git a/lib/Newsletter/Shortcodes/Categories/Newsletter.php b/lib/Newsletter/Shortcodes/Categories/Newsletter.php index b837dfc3f0..5b2d740ef8 100644 --- a/lib/Newsletter/Shortcodes/Categories/Newsletter.php +++ b/lib/Newsletter/Shortcodes/Categories/Newsletter.php @@ -1,6 +1,8 @@ asArray(); } @@ -38,8 +42,7 @@ class Newsletter { break; case 'total': - $posts = wp_count_posts(); - return $posts->publish; + return substr_count($text, 'mailpoet_alc_post'); break; case 'post_title': @@ -48,8 +51,10 @@ class Newsletter { break; case 'number': - // TODO: implement - return 1; + if ($newsletter['type'] !== 'notification') return false; + $sent_newsletters = (int) + SendingQueue::where('newsletter_id', $newsletter['id'])->count(); + return ++$sent_newsletters; break; case 'view_in_browser': diff --git a/lib/Newsletter/Shortcodes/Shortcodes.php b/lib/Newsletter/Shortcodes/Shortcodes.php index 8e138701f6..22c39f39ef 100644 --- a/lib/Newsletter/Shortcodes/Shortcodes.php +++ b/lib/Newsletter/Shortcodes/Shortcodes.php @@ -2,52 +2,51 @@ namespace MailPoet\Newsletter\Shortcodes; class Shortcodes { - public $rendered_newsletter; public $newsletter; public $subscriber; function __construct( - $rendered_newsletter, $newsletter = false, $subscriber = false ) { - $this->rendered_newsletter = $rendered_newsletter; $this->newsletter = $newsletter; $this->subscriber = $subscriber; } - function extract() { - preg_match_all('/\[(?:\w+):.*?\]/', $this->rendered_newsletter, $shortcodes); + function extract($text) { + preg_match_all('/\[(?:\w+):.*?\]/', $text, $shortcodes); return array_unique($shortcodes[0]); } - function process($shortcodes) { + function process($shortcodes, $text) { $processed_shortcodes = array_map( - function ($shortcode) { + function($shortcode) use($text) { preg_match( '/\[(?P\w+):(?P\w+)(?:.*?default:(?P.*?))?\]/', $shortcode, $shortcode_details ); - $shortcode_class = __NAMESPACE__ . '\\Categories\\' . ucfirst($shortcode_details['type']); + $shortcode_action = $shortcode_details['action']; + $shortcode_default_value = isset($shortcode_details['default']) + ? $shortcode_details['default'] : false; if(!class_exists($shortcode_class)) return false; return $shortcode_class::process( - $shortcode_details['action'], - isset($shortcode_details['default']) - ? $shortcode_details['default'] : false, + $shortcode_action, + $shortcode_default_value, $this->newsletter, - $this->subscriber + $this->subscriber, + $text ); }, $shortcodes); return $processed_shortcodes; } - function replace() { - $shortcodes = $this->extract($this->rendered_newsletter); - $processed_shortcodes = $this->process($shortcodes); + function replace($text) { + $shortcodes = $this->extract($text); + $processed_shortcodes = $this->process($shortcodes, $text); $shortcodes = array_intersect_key($shortcodes, $processed_shortcodes); - return str_replace($shortcodes, $processed_shortcodes, $this->rendered_newsletter); + return str_replace($shortcodes, $processed_shortcodes, $text); } } \ No newline at end of file diff --git a/lib/Router/Newsletters.php b/lib/Router/Newsletters.php index ce8e1cfebd..685f984f1c 100644 --- a/lib/Router/Newsletters.php +++ b/lib/Router/Newsletters.php @@ -139,11 +139,8 @@ class Newsletters { } $renderer = new Renderer($data); $rendered_newsletter = $renderer->render(); - $shortcodes = new \MailPoet\Newsletter\Shortcodes\Shortcodes( - $rendered_newsletter['html'], - $data - ); - $rendered_newsletter = $shortcodes->replace(); + $shortcodes = new \MailPoet\Newsletter\Shortcodes\Shortcodes($data); + $rendered_newsletter = $shortcodes->replace($rendered_newsletter['html']); return array('rendered_body' => $rendered_newsletter); } diff --git a/tests/unit/Newsletter/ShortcodesTest.php b/tests/unit/Newsletter/ShortcodesTest.php index 5315f59ebc..c96cf152b4 100644 --- a/tests/unit/Newsletter/ShortcodesTest.php +++ b/tests/unit/Newsletter/ShortcodesTest.php @@ -1,5 +1,6 @@ up(); $this->wp_user = $this->_createWPUser(); $this->subscriber = $this->_createSubscriber(); - $this->newsletter['subject'] = 'some subject'; + $this->newsletter = array( + 'subject' => 'some subject', + 'type' => 'notification', + 'id' => 1 + ); $this->rendered_newsletter = ' Hello [user:displayname | default:member]. Your first name is [user:firstname | default:First Name]. @@ -22,7 +27,10 @@ class ShortcodesTest extends MailPoetTest { Thank you for subscribing with [user:email]. We already have [user:count] users. - There are [newsletter:total] posts on this blog. +

some post

+

another post

+ + There are [newsletter:total] posts in this newsletter. You are reading [newsletter:subject]. The latest post on this blog is called [newsletter:post_title]. The issue number of this newsletter is [newsletter:number]. @@ -38,24 +46,35 @@ class ShortcodesTest extends MailPoetTest { Manage your subscription here: [subscription:manage_url]. View this newsletter in browser: [newsletter:view_in_browser_url].'; $this->shortcodes_object = new MailPoet\Newsletter\Shortcodes\Shortcodes( - $this->rendered_newsletter, $this->newsletter, $this->subscriber ); } - function testItCanProcessShortcodes() { - $shortcodes = $this->shortcodes_object->extract(); + function testItCanExtractShortcodes() { + $shortcodes = $this->shortcodes_object->extract($this->rendered_newsletter); expect(count($shortcodes))->equals(18); + } + + function testItCanProcessShortcodes() { $wp_user = get_userdata($this->wp_user); - $wp_post_count = wp_count_posts(); $wp_latest_post = wp_get_recent_posts(array('numberposts' => 1)); $wp_latest_post = (isset($wp_latest_post)) ? $wp_latest_post[0]['post_title'] : false; + + $queue = SendingQueue::create(); + $queue->newsletter_id = $this->newsletter['id']; + $queue->save(); + $issue_number = 2; + + $number_of_posts = 2; + $date = new \DateTime('now'); $subscriber_count = Subscriber::count(); - $newsletter_with_replaced_shortcodes = $this->shortcodes_object->replace(); + $newsletter_with_replaced_shortcodes = $this->shortcodes_object->replace( + $this->rendered_newsletter + ); $unsubscribe_url = SubscriptionUrl::getUnsubscribeUrl($this->subscriber); $manage_url = SubscriptionUrl::getManageUrl($this->subscriber); @@ -68,10 +87,13 @@ class ShortcodesTest extends MailPoetTest { Thank you for subscribing with {$this->subscriber->email}. We already have {$subscriber_count} users. - There are {$wp_post_count->publish} posts on this blog. +

some post

+

another post

+ + There are {$number_of_posts} posts in this newsletter. You are reading {$this->newsletter['subject']}. The latest post on this blog is called {$wp_latest_post}. - The issue number of this newsletter is 1. + The issue number of this newsletter is {$issue_number}. Date: {$date->format('d')}. Ordinal date: {$date->format('dS')}.