Blocks
2 months ago
Columns
5 months ago
PostProcess
4 days ago
BodyRenderer.php
2 months ago
EscapeHelper.php
2 years ago
Preprocessor.php
1 year ago
Renderer.php
1 month ago
StylesHelper.php
2 months ago
Template.html
2 months ago
index.php
3 years ago
Preprocessor.php
102 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Newsletter\Renderer; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\NewsletterEntity; |
| 9 | use MailPoet\Entities\SendingQueueEntity; |
| 10 | use MailPoet\Newsletter\Renderer\Blocks\AbandonedCartContent; |
| 11 | use MailPoet\Newsletter\Renderer\Blocks\AutomatedLatestContentBlock; |
| 12 | use MailPoet\Newsletter\Renderer\Blocks\DynamicProductsBlock; |
| 13 | use MailPoet\WooCommerce\CouponPreProcessor; |
| 14 | use MailPoet\WooCommerce\TransactionalEmails\ContentPreprocessor; |
| 15 | |
| 16 | class Preprocessor { |
| 17 | const WC_HEADING_BEFORE = ' |
| 18 | <table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-spacing:0;mso-table-lspace:0;mso-table-rspace:0"> |
| 19 | <tr> |
| 20 | <td class="mailpoet_text" valign="top" style="padding-top:20px;padding-bottom:20px;word-break:break-word;word-wrap:break-word;">'; |
| 21 | const WC_HEADING_AFTER = ' |
| 22 | </td> |
| 23 | </tr> |
| 24 | </table>'; |
| 25 | |
| 26 | /** @var AbandonedCartContent */ |
| 27 | private $abandonedCartContent; |
| 28 | |
| 29 | /** @var AutomatedLatestContentBlock */ |
| 30 | private $automatedLatestContent; |
| 31 | |
| 32 | /** @var ContentPreprocessor */ |
| 33 | private $wooCommerceContentPreprocessor; |
| 34 | |
| 35 | /*** @var CouponPreProcessor */ |
| 36 | private $couponPreProcessor; |
| 37 | |
| 38 | /** @var DynamicProductsBlock */ |
| 39 | private $dynamicProductsBlock; |
| 40 | |
| 41 | public function __construct( |
| 42 | AbandonedCartContent $abandonedCartContent, |
| 43 | AutomatedLatestContentBlock $automatedLatestContent, |
| 44 | ContentPreprocessor $wooCommerceContentPreprocessor, |
| 45 | CouponPreProcessor $couponPreProcessor, |
| 46 | DynamicProductsBlock $dynamicProductsBlock |
| 47 | ) { |
| 48 | $this->abandonedCartContent = $abandonedCartContent; |
| 49 | $this->automatedLatestContent = $automatedLatestContent; |
| 50 | $this->wooCommerceContentPreprocessor = $wooCommerceContentPreprocessor; |
| 51 | $this->couponPreProcessor = $couponPreProcessor; |
| 52 | $this->dynamicProductsBlock = $dynamicProductsBlock; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param array $content |
| 57 | * @param NewsletterEntity $newsletter |
| 58 | * @return array |
| 59 | */ |
| 60 | public function process(NewsletterEntity $newsletter, $content, bool $preview = false, ?SendingQueueEntity $sendingQueue = null) { |
| 61 | if (!array_key_exists('blocks', $content)) { |
| 62 | return $content; |
| 63 | } |
| 64 | $contentBlocks = $content['blocks']; |
| 65 | $contentBlocks = $this->couponPreProcessor->processCoupons($newsletter, $contentBlocks, $preview, $sendingQueue); |
| 66 | $content['blocks'] = $this->processContainer($newsletter, $contentBlocks, $preview, $sendingQueue); |
| 67 | return $content; |
| 68 | } |
| 69 | |
| 70 | public function processContainer(NewsletterEntity $newsletter, $blocks, bool $preview, ?SendingQueueEntity $sendingQueue): array { |
| 71 | $containerBlocks = []; |
| 72 | foreach ($blocks as $block) { |
| 73 | if ($block['type'] === 'container' && isset($block['blocks'])) { |
| 74 | $block['blocks'] = $this->processContainer($newsletter, $block['blocks'], $preview, $sendingQueue); |
| 75 | $containerBlocks = array_merge($containerBlocks, [$block]); |
| 76 | } else { |
| 77 | $processedBlock = $this->processBlock($newsletter, $block, $preview, $sendingQueue); |
| 78 | if (!empty($processedBlock)) { |
| 79 | $containerBlocks = array_merge($containerBlocks, $processedBlock); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | return $containerBlocks; |
| 84 | } |
| 85 | |
| 86 | public function processBlock(NewsletterEntity $newsletter, array $block, bool $preview = false, ?SendingQueueEntity $sendingQueue = null): array { |
| 87 | switch ($block['type']) { |
| 88 | case 'abandonedCartContent': |
| 89 | return $this->abandonedCartContent->render($newsletter, $block, $preview, $sendingQueue); |
| 90 | case 'automatedLatestContentLayout': |
| 91 | return $this->automatedLatestContent->render($newsletter, $block); |
| 92 | case 'dynamicProducts': |
| 93 | return $this->dynamicProductsBlock->render($newsletter, $block, $preview, $sendingQueue); |
| 94 | case 'woocommerceHeading': |
| 95 | return $this->wooCommerceContentPreprocessor->preprocessHeader(); |
| 96 | case 'woocommerceContent': |
| 97 | return $this->wooCommerceContentPreprocessor->preprocessContent(); |
| 98 | } |
| 99 | return [$block]; |
| 100 | } |
| 101 | } |
| 102 |