Keine Beschreibung

Renderer.php 5.9KB

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