Process Shortcodes in WooCommerce custom template

MAILPOET-5110
This commit is contained in:
Oluwaseun Olorunsola
2023-03-21 17:14:31 +01:00
committed by Aschepikov
parent 685af267cb
commit c75dc63cc5
2 changed files with 36 additions and 1 deletions

View File

@@ -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];

View File

@@ -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,