Renderer.php
155 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Newsletter\Renderer\Columns; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Newsletter\Renderer\EscapeHelper as EHelper; |
| 9 | |
| 10 | class Renderer { |
| 11 | public function render($contentBlock, $columnsData) { |
| 12 | if (!isset($contentBlock['blocks']) || !is_countable($contentBlock['blocks']) || !is_iterable($contentBlock['blocks'])) { |
| 13 | if (isset($contentBlock['type'])) { |
| 14 | return "<!-- Skipped unsupported block type: {$contentBlock['type']} -->"; |
| 15 | } |
| 16 | return "<!-- Skipped unsupported block -->"; |
| 17 | } |
| 18 | |
| 19 | $columnsCount = count($contentBlock['blocks']); |
| 20 | |
| 21 | if ($columnsCount === 1) { |
| 22 | return $this->renderOneColumn($contentBlock, $columnsData[0]); |
| 23 | } |
| 24 | return $this->renderMultipleColumns($contentBlock, $columnsData); |
| 25 | } |
| 26 | |
| 27 | private function renderOneColumn($contentBlock, $content) { |
| 28 | $template = $this->getOneColumnTemplate( |
| 29 | $contentBlock['styles']['block'], |
| 30 | isset($contentBlock['image']) ? $contentBlock['image'] : null |
| 31 | ); |
| 32 | return $template['content_start'] . $content . $template['content_end']; |
| 33 | } |
| 34 | |
| 35 | public function getOneColumnTemplate($styles, $image) { |
| 36 | $backgroundCss = $this->getBackgroundCss($styles, $image); |
| 37 | $template['content_start'] = ' |
| 38 | <tr> |
| 39 | <td class="mailpoet_content" align="center" style="border-collapse:collapse;' . $backgroundCss . '" ' . $this->getBgColorAttribute($styles, $image) . '> |
| 40 | <table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-spacing:0;mso-table-lspace:0;mso-table-rspace:0"> |
| 41 | <tbody> |
| 42 | <tr> |
| 43 | <td style="padding-left:0;padding-right:0"> |
| 44 | <table width="100%" border="0" cellpadding="0" cellspacing="0" class="mailpoet_' . ColumnsHelper::columnClass(1) . '" style="border-spacing:0;mso-table-lspace:0;mso-table-rspace:0;table-layout:fixed;margin-left:auto;margin-right:auto;padding-left:0;padding-right:0;"> |
| 45 | <tbody>'; |
| 46 | $template['content_end'] = ' |
| 47 | </tbody> |
| 48 | </table> |
| 49 | </td> |
| 50 | </tr> |
| 51 | </tbody> |
| 52 | </table> |
| 53 | </td> |
| 54 | </tr>'; |
| 55 | return $template; |
| 56 | } |
| 57 | |
| 58 | private function renderMultipleColumns($contentBlock, $columnsData) { |
| 59 | $columnsCount = count($contentBlock['blocks']); |
| 60 | $columnsLayout = isset($contentBlock['columnLayout']) ? $contentBlock['columnLayout'] : null; |
| 61 | |
| 62 | $widths = ColumnsHelper::columnWidth($columnsCount, $columnsLayout); |
| 63 | $class = ColumnsHelper::columnClass($columnsCount); |
| 64 | $alignment = ColumnsHelper::columnAlignment($columnsCount); |
| 65 | $index = 0; |
| 66 | $result = $this->getMultipleColumnsContainerStart($class, $contentBlock['styles']['block'], isset($contentBlock['image']) ? $contentBlock['image'] : null); |
| 67 | foreach ($columnsData as $content) { |
| 68 | $result .= $this->getMultipleColumnsContentStart($widths[$index++], $alignment, $class); |
| 69 | $result .= $content; |
| 70 | $result .= $this->getMultipleColumnsContentEnd(); |
| 71 | } |
| 72 | $result .= $this->getMultipleColumnsContainerEnd(); |
| 73 | return $result; |
| 74 | } |
| 75 | |
| 76 | private function getMultipleColumnsContainerStart($class, $styles, $image) { |
| 77 | return ' |
| 78 | <tr> |
| 79 | <td class="mailpoet_content-' . $class . '" align="left" style="border-collapse:collapse;' . $this->getBackgroundCss($styles, $image) . '" ' . $this->getBgColorAttribute($styles, $image) . '> |
| 80 | <table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-spacing:0;mso-table-lspace:0;mso-table-rspace:0"> |
| 81 | <tbody> |
| 82 | <tr> |
| 83 | <td align="center" style="font-size:0;"><!--[if mso]> |
| 84 | <table border="0" width="100%" cellpadding="0" cellspacing="0"> |
| 85 | <tbody> |
| 86 | <tr>'; |
| 87 | } |
| 88 | |
| 89 | private function getMultipleColumnsContainerEnd() { |
| 90 | return ' |
| 91 | </tr> |
| 92 | </tbody> |
| 93 | </table> |
| 94 | <![endif]--></td> |
| 95 | </tr> |
| 96 | </tbody> |
| 97 | </table> |
| 98 | </td> |
| 99 | </tr>'; |
| 100 | } |
| 101 | |
| 102 | private function getMultipleColumnsContentEnd() { |
| 103 | return ' |
| 104 | </tbody> |
| 105 | </table> |
| 106 | </div><!--[if mso]> |
| 107 | </td>'; |
| 108 | } |
| 109 | |
| 110 | public function getMultipleColumnsContentStart($width, $alignment, $class) { |
| 111 | return ' |
| 112 | <td width="' . $width . '" valign="top"> |
| 113 | <![endif]--><div style="display:inline-block; max-width:' . $width . 'px; vertical-align:top; width:100%;"> |
| 114 | <table width="' . $width . '" class="mailpoet_' . $class . '" border="0" cellpadding="0" cellspacing="0" align="' . $alignment . '" style="width:100%;max-width:' . $width . 'px;border-spacing:0;mso-table-lspace:0;mso-table-rspace:0;table-layout:fixed;margin-left:auto;margin-right:auto;padding-left:0;padding-right:0;"> |
| 115 | <tbody>'; |
| 116 | } |
| 117 | |
| 118 | private function getBackgroundCss($styles, $image) { |
| 119 | if ($image !== null && $image['src'] !== null) { |
| 120 | $backgroundColor = isset($styles['backgroundColor']) && $styles['backgroundColor'] !== 'transparent' ? $styles['backgroundColor'] : '#ffffff'; |
| 121 | $repeat = $image['display'] === 'tile' ? 'repeat' : 'no-repeat'; |
| 122 | $size = $image['display'] === 'scale' ? 'cover' : 'contain'; |
| 123 | $style = sprintf( |
| 124 | 'background: %s url(%s) %s center/%s;background-color: %s;background-image: url(%s);background-repeat: %s;background-position: center;background-size: %s;', |
| 125 | $backgroundColor, |
| 126 | $image['src'], |
| 127 | $repeat, |
| 128 | $size, |
| 129 | $backgroundColor, |
| 130 | $image['src'], |
| 131 | $repeat, |
| 132 | $size |
| 133 | ); |
| 134 | return EHelper::escapeHtmlStyleAttr($style); |
| 135 | } else { |
| 136 | if (!isset($styles['backgroundColor'])) return false; |
| 137 | $backgroundColor = $styles['backgroundColor']; |
| 138 | return ($backgroundColor !== 'transparent') ? |
| 139 | EHelper::escapeHtmlStyleAttr(sprintf('background-color:%s!important;', $backgroundColor)) : |
| 140 | false; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | private function getBgColorAttribute($styles, $image) { |
| 145 | if ( |
| 146 | ($image === null || $image['src'] === null) |
| 147 | && isset($styles['backgroundColor']) |
| 148 | && $styles['backgroundColor'] !== 'transparent' |
| 149 | ) { |
| 150 | return 'bgcolor="' . EHelper::escapeHtmlAttr($styles['backgroundColor']) . '"'; |
| 151 | } |
| 152 | return null; |
| 153 | } |
| 154 | } |
| 155 |