AbandonedCartContent.php
1 year ago
AutomatedLatestContentBlock.php
3 years ago
Button.php
1 year ago
Coupon.php
3 years ago
Divider.php
2 years ago
DynamicProductsBlock.php
11 months ago
Footer.php
2 months ago
Header.php
2 months ago
Image.php
3 years ago
Placeholder.php
4 years ago
Renderer.php
2 months ago
Social.php
2 months ago
Spacer.php
3 years ago
Text.php
2 months ago
index.php
3 years ago
Renderer.php
158 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Newsletter\Renderer\Blocks; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\NewsletterEntity; |
| 9 | use MailPoet\Newsletter\Renderer\Columns\ColumnsHelper; |
| 10 | use MailPoet\Newsletter\Renderer\StylesHelper; |
| 11 | |
| 12 | class Renderer { |
| 13 | /** @var AutomatedLatestContentBlock */ |
| 14 | private $ALC; |
| 15 | |
| 16 | /** @var Button */ |
| 17 | private $button; |
| 18 | |
| 19 | /** @var Divider */ |
| 20 | private $divider; |
| 21 | |
| 22 | /** @var Footer */ |
| 23 | private $footer; |
| 24 | |
| 25 | /** @var Header */ |
| 26 | private $header; |
| 27 | |
| 28 | /** @var Image */ |
| 29 | private $image; |
| 30 | |
| 31 | /** @var Social */ |
| 32 | private $social; |
| 33 | |
| 34 | /** @var Spacer */ |
| 35 | private $spacer; |
| 36 | |
| 37 | /** @var Text */ |
| 38 | private $text; |
| 39 | |
| 40 | /** @var Placeholder */ |
| 41 | private $placeholder; |
| 42 | |
| 43 | /** @var Coupon */ |
| 44 | private $coupon; |
| 45 | |
| 46 | /** @var DynamicProductsBlock */ |
| 47 | private $dynamicProducts; |
| 48 | |
| 49 | public function __construct( |
| 50 | AutomatedLatestContentBlock $ALC, |
| 51 | Button $button, |
| 52 | Divider $divider, |
| 53 | Footer $footer, |
| 54 | Header $header, |
| 55 | Image $image, |
| 56 | Social $social, |
| 57 | Spacer $spacer, |
| 58 | Text $text, |
| 59 | Placeholder $placeholder, |
| 60 | Coupon $coupon, |
| 61 | DynamicProductsBlock $dynamicProducts |
| 62 | ) { |
| 63 | $this->ALC = $ALC; |
| 64 | $this->button = $button; |
| 65 | $this->divider = $divider; |
| 66 | $this->footer = $footer; |
| 67 | $this->header = $header; |
| 68 | $this->image = $image; |
| 69 | $this->social = $social; |
| 70 | $this->spacer = $spacer; |
| 71 | $this->text = $text; |
| 72 | $this->placeholder = $placeholder; |
| 73 | $this->coupon = $coupon; |
| 74 | $this->dynamicProducts = $dynamicProducts; |
| 75 | } |
| 76 | |
| 77 | public function render(NewsletterEntity $newsletter, $data, bool $isRtl = false) { |
| 78 | if (!isset($data['blocks']) || !is_countable($data['blocks']) || !is_iterable($data['blocks'])) { |
| 79 | return null; |
| 80 | } |
| 81 | $columnCount = count($data['blocks']); |
| 82 | $columnsLayout = $data['columnLayout'] ?? null; |
| 83 | $columnWidths = ColumnsHelper::columnWidth($columnCount, $columnsLayout); |
| 84 | $columnContent = []; |
| 85 | |
| 86 | foreach ($data['blocks'] as $index => $columnBlocks) { |
| 87 | $renderedBlockElement = $this->renderBlocksInColumn($newsletter, $columnBlocks, $columnWidths[$index], $isRtl); |
| 88 | $columnContent[] = $renderedBlockElement; |
| 89 | } |
| 90 | |
| 91 | return $columnContent; |
| 92 | } |
| 93 | |
| 94 | private function renderBlocksInColumn(NewsletterEntity $newsletter, $block, $columnBaseWidth, bool $isRtl = false) { |
| 95 | $blockContent = ''; |
| 96 | $_this = $this; |
| 97 | array_map(function($block) use (&$blockContent, $columnBaseWidth, $newsletter, $_this, $isRtl) { |
| 98 | $renderedBlockElement = $_this->createElementFromBlockType($newsletter, $block, $columnBaseWidth, $isRtl); |
| 99 | if (isset($block['blocks'])) { |
| 100 | $renderedBlockElement = $_this->renderBlocksInColumn($newsletter, $block, $columnBaseWidth, $isRtl); |
| 101 | // nested vertical column container is rendered as an array |
| 102 | if (is_array($renderedBlockElement)) { |
| 103 | $renderedBlockElement = implode('', array_map(static fn($v): string => is_scalar($v) ? (string)$v : '', $renderedBlockElement)); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | $blockContent .= $renderedBlockElement; |
| 108 | }, $block['blocks']); |
| 109 | return $blockContent; |
| 110 | } |
| 111 | |
| 112 | public function createElementFromBlockType(NewsletterEntity $newsletter, $block, $columnBaseWidth, bool $isRtl = false) { |
| 113 | if ($block['type'] === 'automatedLatestContent') { |
| 114 | return $this->processAutomatedLatestContent($newsletter, $block, $columnBaseWidth, $isRtl); |
| 115 | } |
| 116 | if ($block['type'] === 'dynamicProducts') { |
| 117 | return $this->processDynamicProducts($block, $columnBaseWidth); |
| 118 | } |
| 119 | $block = StylesHelper::applyTextAlignment($block); |
| 120 | switch ($block['type']) { |
| 121 | case 'button': |
| 122 | return $this->button->render($block, $columnBaseWidth); |
| 123 | case 'divider': |
| 124 | return $this->divider->render($block); |
| 125 | case 'footer': |
| 126 | return $this->footer->render($block, $isRtl); |
| 127 | case 'header': |
| 128 | return $this->header->render($block, $isRtl); |
| 129 | case 'image': |
| 130 | return $this->image->render($block, $columnBaseWidth); |
| 131 | case 'social': |
| 132 | return $this->social->render($block); |
| 133 | case 'spacer': |
| 134 | return $this->spacer->render($block); |
| 135 | case 'text': |
| 136 | return $this->text->render($block, $isRtl); |
| 137 | case 'placeholder': |
| 138 | return $this->placeholder->render($block); |
| 139 | case Coupon::TYPE: |
| 140 | return $this->coupon->render($block, $columnBaseWidth); |
| 141 | } |
| 142 | return "<!-- Skipped unsupported block type: {$block['type']} -->"; |
| 143 | } |
| 144 | |
| 145 | public function processAutomatedLatestContent(NewsletterEntity $newsletter, $args, $columnBaseWidth, bool $isRtl = false) { |
| 146 | $transformedPosts = [ |
| 147 | 'blocks' => $this->ALC->render($newsletter, $args), |
| 148 | ]; |
| 149 | $transformedPosts = StylesHelper::applyTextAlignment($transformedPosts); |
| 150 | return $this->renderBlocksInColumn($newsletter, $transformedPosts, $columnBaseWidth, $isRtl); |
| 151 | } |
| 152 | |
| 153 | public function processDynamicProducts($args, $columnBaseWidth) { |
| 154 | $renderedProducts = $this->dynamicProducts->render($args, $columnBaseWidth); |
| 155 | return $renderedProducts; |
| 156 | } |
| 157 | } |
| 158 |