LayoutHelper.php
3 years ago
MetaInformationManager.php
2 months ago
PostContentManager.php
11 months ago
PostListTransformer.php
3 years ago
PostTransformer.php
1 year ago
PostTransformerContentsExtractor.php
2 months ago
StructureTransformer.php
3 years ago
TitleListTransformer.php
1 year ago
Transformer.php
3 years ago
index.php
3 years ago
StructureTransformer.php
135 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Newsletter\Editor; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Util\DOM as DOMUtil; |
| 9 | use MailPoet\Util\pQuery\pQuery; |
| 10 | use MailPoetVendor\pQuery\DomNode; |
| 11 | |
| 12 | class StructureTransformer { |
| 13 | public function transform($content, $imageFullWidth) { |
| 14 | $root = pQuery::parseStr($content); |
| 15 | |
| 16 | $this->hoistImagesToRoot($root); |
| 17 | $structure = $this->transformTagsToBlocks($root, $imageFullWidth); |
| 18 | $structure = $this->mergeNeighboringBlocks($structure); |
| 19 | return $structure; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Hoists images to root level, preserves order by splitting neighboring |
| 24 | * elements and inserts tags as children of top ancestor |
| 25 | */ |
| 26 | protected function hoistImagesToRoot(DomNode $root) { |
| 27 | foreach ($root->query('img') as $item) { |
| 28 | $topAncestor = DOMUtil::findTopAncestor($item); |
| 29 | $offset = $topAncestor->index(); |
| 30 | |
| 31 | if ($item->hasParent('a') || $item->hasParent('figure')) { |
| 32 | $item = $item->parent; |
| 33 | } |
| 34 | |
| 35 | DOMUtil::splitOn($item->getRoot(), $item); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Transforms HTML tags into their respective JSON objects, |
| 41 | * turns other root children into text blocks |
| 42 | */ |
| 43 | private function transformTagsToBlocks(DomNode $root, $imageFullWidth) { |
| 44 | $children = $this->filterOutFiguresWithoutImages($root->children); |
| 45 | return array_map(function($item) use ($imageFullWidth) { |
| 46 | if ($this->isImageElement($item)) { |
| 47 | $image = $item->tag === 'img' ? $item : $item->query('img')[0]; |
| 48 | $width = $image->getAttribute('width'); |
| 49 | $height = $image->getAttribute('height'); |
| 50 | return [ |
| 51 | 'type' => 'image', |
| 52 | 'link' => $item->getAttribute('href') ?: '', |
| 53 | 'src' => $image->getAttribute('src'), |
| 54 | 'alt' => $image->getAttribute('alt'), |
| 55 | 'fullWidth' => $imageFullWidth, |
| 56 | 'width' => $width === null ? 'auto' : $width, |
| 57 | 'height' => $height === null ? 'auto' : $height, |
| 58 | 'styles' => [ |
| 59 | 'block' => [ |
| 60 | 'textAlign' => $this->getImageAlignment($image), |
| 61 | ], |
| 62 | ], |
| 63 | ]; |
| 64 | } else { |
| 65 | return [ |
| 66 | 'type' => 'text', |
| 67 | 'text' => $item->toString(), |
| 68 | ]; |
| 69 | } |
| 70 | |
| 71 | }, $children); |
| 72 | } |
| 73 | |
| 74 | private function filterOutFiguresWithoutImages(array $items) { |
| 75 | $items = array_filter($items, function (DomNode $item) { |
| 76 | if ($item->tag === 'figure' && $item->query('img')->count() === 0) { |
| 77 | return false; |
| 78 | } |
| 79 | return true; |
| 80 | }); |
| 81 | return array_values($items); |
| 82 | } |
| 83 | |
| 84 | private function isImageElement(DomNode $item) { |
| 85 | return $item->tag === 'img' || (in_array($item->tag, ['a', 'figure'], true) && $item->query('img')->count() > 0); |
| 86 | } |
| 87 | |
| 88 | private function getImageAlignment(DomNode $image) { |
| 89 | $alignItem = $image->hasParent('figure') ? $image->parent : $image; |
| 90 | if ($alignItem->hasClass('aligncenter')) { |
| 91 | $align = 'center'; |
| 92 | } elseif ($alignItem->hasClass('alignleft')) { |
| 93 | $align = 'left'; |
| 94 | } elseif ($alignItem->hasClass('alignright')) { |
| 95 | $align = 'right'; |
| 96 | } else { |
| 97 | $align = 'left'; |
| 98 | } |
| 99 | return $align; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Merges neighboring blocks when possible. |
| 104 | * E.g. 2 adjacent text blocks may be combined into one. |
| 105 | */ |
| 106 | private function mergeNeighboringBlocks(array $structure) { |
| 107 | $updatedStructure = []; |
| 108 | $textAccumulator = ''; |
| 109 | foreach ($structure as $item) { |
| 110 | if ($item['type'] === 'text') { |
| 111 | $textAccumulator .= $item['text']; |
| 112 | } |
| 113 | if ($item['type'] !== 'text') { |
| 114 | if (!empty($textAccumulator)) { |
| 115 | $updatedStructure[] = [ |
| 116 | 'type' => 'text', |
| 117 | 'text' => trim($textAccumulator), |
| 118 | ]; |
| 119 | $textAccumulator = ''; |
| 120 | } |
| 121 | $updatedStructure[] = $item; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (!empty($textAccumulator)) { |
| 126 | $updatedStructure[] = [ |
| 127 | 'type' => 'text', |
| 128 | 'text' => trim($textAccumulator), |
| 129 | ]; |
| 130 | } |
| 131 | |
| 132 | return $updatedStructure; |
| 133 | } |
| 134 | } |
| 135 |