diff --git a/lib/Cron/Workers/SendingQueue/Tasks/Newsletter.php b/lib/Cron/Workers/SendingQueue/Tasks/Newsletter.php index 0bfc299efc..5c7623a5de 100644 --- a/lib/Cron/Workers/SendingQueue/Tasks/Newsletter.php +++ b/lib/Cron/Workers/SendingQueue/Tasks/Newsletter.php @@ -94,7 +94,13 @@ class Newsletter { // extract and save newsletter posts PostsTask::extractAndSave($rendered_newsletter, $newsletter); // update queue with the rendered and pre-processed newsletter - $queue->newsletter_rendered_subject = Shortcodes::process($newsletter->subject, $newsletter, null, $queue); + $queue->newsletter_rendered_subject = ShortcodesTask::process( + $newsletter->subject, + $rendered_newsletter['html'], + $newsletter, + null, + $queue + ); $queue->newsletter_rendered_body = $rendered_newsletter; $queue->save(); // catch DB errors @@ -123,6 +129,7 @@ class Newsletter { ); $prepared_newsletter = ShortcodesTask::process( $prepared_newsletter, + null, $newsletter, $subscriber, $queue diff --git a/lib/Cron/Workers/SendingQueue/Tasks/Shortcodes.php b/lib/Cron/Workers/SendingQueue/Tasks/Shortcodes.php index e342de6d1c..03663e57fe 100644 --- a/lib/Cron/Workers/SendingQueue/Tasks/Shortcodes.php +++ b/lib/Cron/Workers/SendingQueue/Tasks/Shortcodes.php @@ -1,4 +1,5 @@ replace($content); + return $shortcodes->replace($content, $content_source); } } \ No newline at end of file diff --git a/lib/Newsletter/Shortcodes/Shortcodes.php b/lib/Newsletter/Shortcodes/Shortcodes.php index 3271eda7b4..88c8f525c5 100644 --- a/lib/Newsletter/Shortcodes/Shortcodes.php +++ b/lib/Newsletter/Shortcodes/Shortcodes.php @@ -91,12 +91,18 @@ class Shortcodes { return $processed_shortcodes; } - function replace($content, $categories = false) { + function replace($content, $content_source = null, $categories = null) { $shortcodes = $this->extract($content, $categories); if(!$shortcodes) { return $content; } - $processed_shortcodes = $this->process($shortcodes, $content); + // if content contains only shortcodes (e.g., [newsletter:post_title]) but their processing + // depends on some other content (e.g., "post_id" inside a rendered newsletter), + // then we should use that content source when processing shortcodes + $processed_shortcodes = $this->process( + $shortcodes, + ($content_source) ? $content_source : $content + ); $shortcodes = array_intersect_key($shortcodes, $processed_shortcodes); return str_replace($shortcodes, $processed_shortcodes, $content); } diff --git a/tests/unit/Cron/Workers/SendingQueue/Tasks/ShortcodesTest.php b/tests/unit/Cron/Workers/SendingQueue/Tasks/ShortcodesTest.php index 0e2a3686a4..e03a451777 100644 --- a/tests/unit/Cron/Workers/SendingQueue/Tasks/ShortcodesTest.php +++ b/tests/unit/Cron/Workers/SendingQueue/Tasks/ShortcodesTest.php @@ -1,4 +1,5 @@ WP_post = wp_insert_post( + array( + 'post_title' => 'Sample Post', + 'post_content' => 'contents', + 'post_status' => 'publish', + ) + ); + } + function testItCanReplaceShortcodes() { $queue = $newsletter = (object)array( 'id' => 1 @@ -18,12 +29,26 @@ class ShortcodesTest extends \MailPoetTest { 'last_name' => 'Doe' ); $rendered_body = '[subscriber:firstname] [subscriber:lastname]'; - $result = Shortcodes::process($rendered_body, $newsletter, $subscriber, $queue); + $result = Shortcodes::process($rendered_body, null, $newsletter, $subscriber, $queue); expect($result)->equals('John Doe'); } + function testItCanReplaceShortcodesInOneStringUsingContentsFromAnother() { + $wp_post = get_post($this->WP_post); + $content = 'Subject line with one shortcode: [newsletter:post_title]'; + $content_source = 'latest post'; + + // [newsletter:post_title] depends on the "data-post-id" tag and the shortcode will + // get replaced with an empty string if that tag is not found + expect(trim(Shortcodes::process($content)))->equals('Subject line with one shortcode:'); + + // when tag is found, the shortcode will be processed and replaced + expect(Shortcodes::process($content, $content_source))->equals('Subject line with one shortcode: ' . $wp_post->post_title); + } + function _after() { \ORM::raw_execute('TRUNCATE ' . SendingQueue::$_table); \ORM::raw_execute('TRUNCATE ' . Newsletter::$_table); + wp_delete_post($this->WP_post, true); } } \ No newline at end of file