From c75dc63cc504d1c4095ffd5294d0fe0c17a6cc76 Mon Sep 17 00:00:00 2001 From: Oluwaseun Olorunsola Date: Tue, 21 Mar 2023 17:14:31 +0100 Subject: [PATCH] Process Shortcodes in WooCommerce custom template MAILPOET-5110 --- .../TransactionalEmails/Renderer.php | 6 +++- .../TransactionalEmails/RendererTest.php | 31 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/mailpoet/lib/WooCommerce/TransactionalEmails/Renderer.php b/mailpoet/lib/WooCommerce/TransactionalEmails/Renderer.php index 62c14888ab..b76b05f97b 100644 --- a/mailpoet/lib/WooCommerce/TransactionalEmails/Renderer.php +++ b/mailpoet/lib/WooCommerce/TransactionalEmails/Renderer.php @@ -2,6 +2,7 @@ namespace MailPoet\WooCommerce\TransactionalEmails; +use MailPoet\Cron\Workers\SendingQueue\Tasks\Shortcodes; use MailPoet\Entities\NewsletterEntity; use MailPoet\Newsletter\Renderer\Renderer as NewsletterRenderer; use MailPoetVendor\csstidy; @@ -33,8 +34,11 @@ class Renderer { } public function render(NewsletterEntity $newsletter, ?string $subject = null) { - $renderedHtml = $this->renderer->renderAsPreview($newsletter, 'html', $subject); + $renderedNewsletter = $this->renderer->renderAsPreview($newsletter, 'html', $subject); $headingText = $subject ?? ''; + + $renderedHtml = Shortcodes::process($renderedNewsletter, null, $newsletter, null, null); + $renderedHtml = str_replace(ContentPreprocessor::WC_HEADING_PLACEHOLDER, $headingText, $renderedHtml); $html = explode(ContentPreprocessor::WC_CONTENT_PLACEHOLDER, $renderedHtml); $this->htmlBeforeContent = $html[0]; diff --git a/mailpoet/tests/integration/WooCommerce/TransactionalEmails/RendererTest.php b/mailpoet/tests/integration/WooCommerce/TransactionalEmails/RendererTest.php index 6236bed531..c1e5a166be 100644 --- a/mailpoet/tests/integration/WooCommerce/TransactionalEmails/RendererTest.php +++ b/mailpoet/tests/integration/WooCommerce/TransactionalEmails/RendererTest.php @@ -95,6 +95,37 @@ class RendererTest extends \MailPoetTest { expect($css)->stringContainsString("#mailpoet_woocommerce_container h1 {\nfont-weight:700\n}"); } + public function testItReplaceShortcodes() { + $this->newsletter->setBody([ + 'content' => L::col([ + L::row([L::col([['type' => 'text', 'text' => 'Some text before heading']])]), + L::row([L::col([['type' => 'text', 'text' => 'Some text between heading and content']])]), + ['type' => 'woocommerceContent'], + ['type' => 'woocommerceHeading'], + L::row([L::col([['type' => 'text', 'text' => 'Some text after content']])]), + L::row([L::col([ + ['type' => 'text', 'text' => '[site:title]'], + ['type' => 'text', 'text' => '[site:homepage_url]'], + ['type' => 'text', 'text' => '[date:mtext]'], + ['type' => 'text', 'text' => '[date:y]'], + ])]), + ]), + ]); + $this->newslettersRepository->persist($this->newsletter); + $renderer = new Renderer(new csstidy, $this->getNewsletterRenderer()); + $renderer->render($this->newsletter, 'Heading Text'); + $html = $renderer->getHTMLAfterContent(); + + $siteName = strval(get_option('blogname')); + expect($html)->stringContainsString($siteName); // [site:title] + + $siteUrl = strval(get_option('home')); + expect($html)->stringContainsString($siteUrl); // [site:homepage_url] + + expect($html)->stringContainsString(date_i18n('F', WPFunctions::get()->currentTime('timestamp'))); // [date:mtext] + expect($html)->stringContainsString(date_i18n('Y', WPFunctions::get()->currentTime('timestamp'))); // [date:y] + } + private function getNewsletterRenderer(): NewsletterRenderer { $wooPreprocessor = new ContentPreprocessor(Stub::make( \MailPoet\WooCommerce\TransactionalEmails::class,