mailpoet
/
lib
/
Automation
/
Integrations
/
MailPoet
/
Templates
/
TemplateEmailPreviewRenderer.php
EmailFactory.php
3 months ago
TemplateEmailPreviewRenderer.php
1 month ago
TemplatesFactory.php
1 month ago
index.php
3 years ago
TemplateEmailPreviewRenderer.php
126 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\MailPoet\Templates; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use Automattic\WooCommerce\EmailEditor\Email_Editor_Container; |
| 9 | use Automattic\WooCommerce\EmailEditor\Engine\Personalizer; |
| 10 | use Automattic\WooCommerce\EmailEditor\Engine\Renderer\Renderer as GutenbergRenderer; |
| 11 | use MailPoet\EmailEditor\Integrations\MailPoet\Patterns\PatternsController; |
| 12 | use MailPoet\EmailEditor\Integrations\MailPoet\Templates\TemplatesController; |
| 13 | use MailPoet\WP\Functions as WPFunctions; |
| 14 | |
| 15 | /** |
| 16 | * Renders an automation template's pre-built email pattern to HTML for previewing |
| 17 | * in the template library, without persisting a newsletter or mailpoet_email post. |
| 18 | * |
| 19 | * The block-editor renderer resolves post content through the core/post-content |
| 20 | * block, which reads the post via get_post(). To render without a database write |
| 21 | * we prime the WordPress object cache with an in-memory post under an unused ID, |
| 22 | * render, then remove it again. Nothing is written to the database. |
| 23 | */ |
| 24 | class TemplateEmailPreviewRenderer { |
| 25 | // High, implausible-to-exist post ID range used only to back the in-memory |
| 26 | // preview post. A fresh ID is drawn per render so concurrent previews can't |
| 27 | // clobber each other's cache entry when a persistent object cache is in use. |
| 28 | private const PREVIEW_POST_ID_MIN = 2000000000; |
| 29 | private const PREVIEW_POST_ID_MAX = 2147483646; |
| 30 | |
| 31 | private PatternsController $patternsController; |
| 32 | private TemplatesController $templatesController; |
| 33 | private WPFunctions $wp; |
| 34 | |
| 35 | public function __construct( |
| 36 | PatternsController $patternsController, |
| 37 | TemplatesController $templatesController, |
| 38 | WPFunctions $wp |
| 39 | ) { |
| 40 | $this->patternsController = $patternsController; |
| 41 | $this->templatesController = $templatesController; |
| 42 | $this->wp = $wp; |
| 43 | } |
| 44 | |
| 45 | public function patternExists(string $patternName): bool { |
| 46 | return $this->patternsController->getPatternPreviewContent($patternName) !== null; |
| 47 | } |
| 48 | |
| 49 | public function render(string $patternName, string $subject = '', string $preheader = ''): ?string { |
| 50 | $content = $this->patternsController->getPatternPreviewContent($patternName); |
| 51 | if ($content === null) { |
| 52 | return null; |
| 53 | } |
| 54 | |
| 55 | $previewPostId = random_int(self::PREVIEW_POST_ID_MIN, self::PREVIEW_POST_ID_MAX); |
| 56 | $post = new \WP_Post((object)[ |
| 57 | 'ID' => $previewPostId, |
| 58 | 'post_author' => 0, |
| 59 | 'post_date' => '2024-01-01 00:00:00', |
| 60 | 'post_date_gmt' => '2024-01-01 00:00:00', |
| 61 | 'post_content' => $content, |
| 62 | 'post_title' => $subject, |
| 63 | 'post_status' => 'publish', |
| 64 | 'post_name' => 'mailpoet-automation-template-email-preview', |
| 65 | 'post_type' => 'mailpoet_email', |
| 66 | 'post_modified' => '2024-01-01 00:00:00', |
| 67 | 'post_modified_gmt' => '2024-01-01 00:00:00', |
| 68 | 'filter' => 'raw', |
| 69 | ]); |
| 70 | |
| 71 | $contextFilter = function (array $context): array { |
| 72 | return array_merge($context, [ |
| 73 | 'integration' => 'mailpoet', |
| 74 | 'newsletter_id' => 0, |
| 75 | 'queue_id' => 0, |
| 76 | 'email_type' => 'automation', |
| 77 | 'is_real_send' => false, |
| 78 | 'is_preview' => true, |
| 79 | 'is_single_recipient' => false, |
| 80 | 'subscriber_count' => 0, |
| 81 | 'mailpoet_is_automation' => true, |
| 82 | ]); |
| 83 | }; |
| 84 | |
| 85 | $this->wp->wpCacheSet($previewPostId, $post, 'posts'); |
| 86 | $this->wp->addFilter('woocommerce_email_editor_rendering_email_context', $contextFilter); |
| 87 | try { |
| 88 | $rendered = $this->getRenderer()->render( |
| 89 | $post, |
| 90 | $subject, |
| 91 | $preheader, |
| 92 | (string)$this->wp->getBloginfo('language'), |
| 93 | '<meta name="robots" content="noindex, nofollow" />', |
| 94 | $this->templatesController->getDefaultTemplateSlug() |
| 95 | ); |
| 96 | } finally { |
| 97 | $this->wp->removeFilter('woocommerce_email_editor_rendering_email_context', $contextFilter); |
| 98 | $this->wp->wpCacheDelete($previewPostId, 'posts'); |
| 99 | } |
| 100 | |
| 101 | $html = is_string($rendered['html'] ?? null) ? $rendered['html'] : null; |
| 102 | if ($html === null) { |
| 103 | return null; |
| 104 | } |
| 105 | |
| 106 | // Replace personalization tags (e.g. <!--[mailpoet/site-title]-->) with their |
| 107 | // preview values; without this they render as empty placeholders. |
| 108 | $personalizer = $this->getPersonalizer(); |
| 109 | $personalizer->set_context([ |
| 110 | 'recipient_email' => null, |
| 111 | 'is_user_preview' => true, |
| 112 | 'newsletter_id' => 0, |
| 113 | 'queue_id' => null, |
| 114 | ]); |
| 115 | return $personalizer->personalize_content($html); |
| 116 | } |
| 117 | |
| 118 | private function getRenderer(): GutenbergRenderer { |
| 119 | return Email_Editor_Container::container()->get(GutenbergRenderer::class); |
| 120 | } |
| 121 | |
| 122 | private function getPersonalizer(): Personalizer { |
| 123 | return Email_Editor_Container::container()->get(Personalizer::class); |
| 124 | } |
| 125 | } |
| 126 |