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
Text.php
197 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\Newsletter\Editor\PostContentManager; |
| 9 | use MailPoet\Newsletter\Renderer\EscapeHelper as EHelper; |
| 10 | use MailPoet\Newsletter\Renderer\StylesHelper; |
| 11 | use MailPoet\Util\pQuery\pQuery; |
| 12 | |
| 13 | class Text { |
| 14 | public function render($element, bool $isRtl = false) { |
| 15 | $html = $element['text']; |
| 16 | // replace with spaces |
| 17 | $html = str_replace(' ', ' ', $html); |
| 18 | $html = str_replace('\xc2\xa0', ' ', $html); |
| 19 | $html = $this->convertBlockquotesToTables($html, $isRtl); |
| 20 | $html = $this->convertParagraphsToTables($html, $isRtl); |
| 21 | $html = $this->styleLists($html, $isRtl); |
| 22 | $html = $this->styleHeadings($html, $isRtl); |
| 23 | $html = $this->removeLastLineBreak($html); |
| 24 | $template = ' |
| 25 | <tr> |
| 26 | <td class="mailpoet_text mailpoet_padded_vertical mailpoet_padded_side" valign="top" style="word-break:break-word;word-wrap:break-word;"> |
| 27 | ' . $html . ' |
| 28 | </td> |
| 29 | </tr>'; |
| 30 | return $template; |
| 31 | } |
| 32 | |
| 33 | public function convertBlockquotesToTables($html, bool $isRtl = false) { |
| 34 | $dOMParser = new pQuery(); |
| 35 | $DOM = $dOMParser->parseStr($html); |
| 36 | $blockquotes = $DOM->query('blockquote'); |
| 37 | foreach ($blockquotes as $blockquote) { |
| 38 | $contents = []; |
| 39 | $paragraphs = $blockquote->query('p, h1, h2, h3, h4', 0); |
| 40 | foreach ($paragraphs as $index => $paragraph) { |
| 41 | if (preg_match('/h\d/', $paragraph->getTag())) { |
| 42 | $contents[] = $paragraph->getOuterText(); |
| 43 | } else { |
| 44 | $contents[] = $paragraph->toString(true, true, 1); |
| 45 | } |
| 46 | if ($index + 1 < $paragraphs->count()) $contents[] = '<br />'; |
| 47 | $paragraph->remove(); |
| 48 | } |
| 49 | if (empty($contents)) continue; |
| 50 | $blockquote->setTag('table'); |
| 51 | $blockquote->addClass('mailpoet_blockquote'); |
| 52 | $blockquote->width = '100%'; |
| 53 | $blockquote->spacing = 0; |
| 54 | $blockquote->border = 0; |
| 55 | $blockquote->cellpadding = 0; |
| 56 | $blockquote->html(' |
| 57 | <tbody> |
| 58 | <tr> |
| 59 | <td width="2" bgcolor="#565656"></td> |
| 60 | <td width="10"></td> |
| 61 | <td valign="top"> |
| 62 | <table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-spacing:0;mso-table-lspace:0;mso-table-rspace:0"> |
| 63 | <tr> |
| 64 | <td class="mailpoet_blockquote"> |
| 65 | ' . implode('', $contents) . ' |
| 66 | </td> |
| 67 | </tr> |
| 68 | </table> |
| 69 | </td> |
| 70 | </tr> |
| 71 | </tbody>'); |
| 72 | $blockquote = $this->insertLineBreak($blockquote); |
| 73 | } |
| 74 | return $DOM->__toString(); |
| 75 | } |
| 76 | |
| 77 | public function convertParagraphsToTables($html, bool $isRtl = false) { |
| 78 | $dOMParser = new pQuery(); |
| 79 | $DOM = $dOMParser->parseStr($html); |
| 80 | $paragraphs = $DOM->query('p'); |
| 81 | if (!$paragraphs->count()) return $html; |
| 82 | foreach ($paragraphs as $paragraph) { |
| 83 | // process empty paragraphs |
| 84 | if (!trim($paragraph->html())) { |
| 85 | $nextElement = ($paragraph->getNextSibling()) ? |
| 86 | trim($paragraph->getNextSibling()->text()) : |
| 87 | false; |
| 88 | $previousElement = ($paragraph->getPreviousSibling()) ? |
| 89 | trim($paragraph->getPreviousSibling()->text()) : |
| 90 | false; |
| 91 | $previousElementTag = ($previousElement) ? |
| 92 | $paragraph->getPreviousSibling()->tag : |
| 93 | false; |
| 94 | // if previous or next paragraphs are empty OR previous paragraph |
| 95 | // is a heading, insert a break line |
| 96 | if ( |
| 97 | !$nextElement || |
| 98 | !$previousElement || |
| 99 | (preg_match('/h\d+/', $previousElementTag)) |
| 100 | ) { |
| 101 | $paragraph = $this->insertLineBreak($paragraph); |
| 102 | } |
| 103 | $paragraph->remove(); |
| 104 | continue; |
| 105 | } |
| 106 | $style = (string)$paragraph->style; |
| 107 | if (!preg_match('/text-align/i', $style)) { |
| 108 | $style = 'text-align: ' . ($isRtl ? 'right' : 'left') . ';' . $style; |
| 109 | } elseif ( |
| 110 | $isRtl |
| 111 | && preg_match('/text-align\s*:\s*left/i', $style) |
| 112 | && !preg_match('/text-align\s*:\s*(center|justify|right)/i', $style) |
| 113 | ) { |
| 114 | $style = StylesHelper::joinStyles($style, 'text-align: right;'); |
| 115 | } |
| 116 | $contents = $paragraph->toString(true, true, 1); |
| 117 | $paragraph->setTag('table'); |
| 118 | $paragraph->style = 'border-spacing:0;mso-table-lspace:0;mso-table-rspace:0;'; |
| 119 | $paragraph->width = '100%'; |
| 120 | $paragraph->cellpadding = 0; |
| 121 | $nextElement = $paragraph->getNextSibling(); |
| 122 | // unless this is the last element in column, add double line breaks |
| 123 | $lineBreaks = ($nextElement && !trim($nextElement->text())) ? |
| 124 | '<br /><br />' : |
| 125 | ''; |
| 126 | // if this element is followed by a list, add single line break |
| 127 | $lineBreaks = ($nextElement && preg_match('/<li/i', $nextElement->getOuterText())) ? |
| 128 | '<br />' : |
| 129 | $lineBreaks; |
| 130 | if ($paragraph->hasClass(PostContentManager::WP_POST_CLASS)) { |
| 131 | $paragraph->removeClass(PostContentManager::WP_POST_CLASS); |
| 132 | // if this element is followed by a paragraph or heading, add double line breaks |
| 133 | $lineBreaks = ($nextElement && preg_match('/<(p|h[1-6]{1})/i', $nextElement->getOuterText())) ? |
| 134 | '<br /><br />' : |
| 135 | $lineBreaks; |
| 136 | } |
| 137 | $paragraph->html(' |
| 138 | <tr> |
| 139 | <td class="mailpoet_paragraph" style="word-break:break-word;word-wrap:break-word;' . EHelper::escapeHtmlStyleAttr($style) . '"> |
| 140 | ' . $contents . $lineBreaks . ' |
| 141 | </td> |
| 142 | </tr>'); |
| 143 | } |
| 144 | return $DOM->__toString(); |
| 145 | } |
| 146 | |
| 147 | public function styleLists($html, bool $isRtl = false) { |
| 148 | $dOMParser = new pQuery(); |
| 149 | $DOM = $dOMParser->parseStr($html); |
| 150 | $lists = $DOM->query('ol, ul, li'); |
| 151 | if (!$lists->count()) return $html; |
| 152 | foreach ($lists as $list) { |
| 153 | if ($list->tag === 'li') { |
| 154 | $list->setInnertext($list->toString(true, true, 1)); |
| 155 | $list->class = 'mailpoet_paragraph'; |
| 156 | } else { |
| 157 | $list->class = 'mailpoet_paragraph'; |
| 158 | $list->style = StylesHelper::joinStyles($list->style, 'padding-top:0;padding-bottom:0;margin-top:10px;'); |
| 159 | } |
| 160 | $list->style = StylesHelper::applyTextAlignment($list->style, $isRtl ? 'right' : 'left'); |
| 161 | $list->style = StylesHelper::joinStyles($list->style, 'margin-bottom:10px;'); |
| 162 | $list->style = EHelper::escapeHtmlStyleAttr($list->style); |
| 163 | } |
| 164 | return $DOM->__toString(); |
| 165 | } |
| 166 | |
| 167 | public function styleHeadings($html, bool $isRtl = false) { |
| 168 | $dOMParser = new pQuery(); |
| 169 | $DOM = $dOMParser->parseStr($html); |
| 170 | $headings = $DOM->query('h1, h2, h3, h4'); |
| 171 | if (!$headings->count()) return $html; |
| 172 | foreach ($headings as $heading) { |
| 173 | $heading->style = StylesHelper::applyTextAlignment($heading->style, $isRtl ? 'right' : 'left'); |
| 174 | $heading->style = StylesHelper::joinStyles($heading->style, 'padding:0;font-style:normal;font-weight:normal;'); |
| 175 | $heading->style = EHelper::escapeHtmlStyleAttr($heading->style); |
| 176 | } |
| 177 | return $DOM->__toString(); |
| 178 | } |
| 179 | |
| 180 | public function removeLastLineBreak($html) { |
| 181 | return preg_replace('/(^)?(<br[^>]*?\/?>)+$/i', '', $html); |
| 182 | } |
| 183 | |
| 184 | public function insertLineBreak($element) { |
| 185 | if (!$element->parent) return $element; |
| 186 | $element->parent->insertChild( |
| 187 | [ |
| 188 | 'tag_name' => 'br', |
| 189 | 'self_close' => true, |
| 190 | 'attributes' => [], |
| 191 | ], |
| 192 | $element->index() + 1 |
| 193 | ); |
| 194 | return $element; |
| 195 | } |
| 196 | } |
| 197 |