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
PostTransformer.php
178 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Newsletter\Editor; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | class PostTransformer { |
| 9 | /** @var PostTransformerContentsExtractor */ |
| 10 | private $extractor; |
| 11 | |
| 12 | /** @var array */ |
| 13 | private $args; |
| 14 | /** @var bool */ |
| 15 | private $withLayout; |
| 16 | /** @var string */ |
| 17 | private $imagePosition; |
| 18 | |
| 19 | public function __construct( |
| 20 | $args, |
| 21 | ?PostTransformerContentsExtractor $extractor = null |
| 22 | ) { |
| 23 | $this->args = $args; |
| 24 | $this->withLayout = isset($args['withLayout']) ? (bool)filter_var($args['withLayout'], FILTER_VALIDATE_BOOLEAN) : false; |
| 25 | $this->imagePosition = 'left'; |
| 26 | if ($extractor === null) { |
| 27 | $extractor = new PostTransformerContentsExtractor($args); |
| 28 | } |
| 29 | $this->extractor = $extractor; |
| 30 | } |
| 31 | |
| 32 | public function getDivider() { |
| 33 | if (empty($this->withLayout)) { |
| 34 | return $this->args['divider']; |
| 35 | } |
| 36 | return LayoutHelper::row([ |
| 37 | LayoutHelper::col([$this->args['divider']]), |
| 38 | ]); |
| 39 | } |
| 40 | |
| 41 | public function transform($post) { |
| 42 | if (empty($this->withLayout)) { |
| 43 | return $this->getStructure($post); |
| 44 | } |
| 45 | return $this->getStructureWithLayout($post); |
| 46 | } |
| 47 | |
| 48 | private function getStructure($post) { |
| 49 | $content = $this->extractor->getContent($post, true, $this->args['displayType']); |
| 50 | $title = $this->extractor->getTitle($post); |
| 51 | $featuredImage = $this->extractor->getFeaturedImage($post); |
| 52 | $featuredImagePosition = $this->getFeaturedImagePosition($this->extractor->isProduct($post)); |
| 53 | |
| 54 | if ( |
| 55 | $featuredImage |
| 56 | && $featuredImagePosition === 'belowTitle' |
| 57 | && ( |
| 58 | $this->args['displayType'] !== 'titleOnly' |
| 59 | || $this->extractor->isProduct($post) |
| 60 | ) |
| 61 | ) { |
| 62 | array_unshift($content, $title, $featuredImage); |
| 63 | return $content; |
| 64 | } |
| 65 | |
| 66 | if ($content[0]['type'] === 'text') { |
| 67 | $content[0]['text'] = $title['text'] . $content[0]['text']; |
| 68 | } else { |
| 69 | array_unshift($content, $title); |
| 70 | } |
| 71 | |
| 72 | if ($featuredImage && $this->args['displayType'] !== 'titleOnly') { |
| 73 | array_unshift($content, $featuredImage); |
| 74 | } |
| 75 | |
| 76 | return $content; |
| 77 | } |
| 78 | |
| 79 | private function getStructureWithLayout($post) { |
| 80 | $withPostClass = $this->args['displayType'] === 'full' || $this->args['displayType'] === 'excerpt'; |
| 81 | $content = $this->extractor->getContent($post, $withPostClass, $this->args['displayType']); |
| 82 | $title = $this->extractor->getTitle($post); |
| 83 | $featuredImage = $this->extractor->getFeaturedImage($post); |
| 84 | $featuredImagePosition = $this->getFeaturedImagePosition($this->extractor->isProduct($post)); |
| 85 | |
| 86 | if ( |
| 87 | !$featuredImage |
| 88 | || $featuredImagePosition === 'none' |
| 89 | || ( |
| 90 | $this->args['displayType'] === 'titleOnly' |
| 91 | && !$this->extractor->isProduct($post) |
| 92 | ) |
| 93 | ) { |
| 94 | array_unshift($content, $title); |
| 95 | |
| 96 | return [ |
| 97 | LayoutHelper::row([ |
| 98 | LayoutHelper::col($content), |
| 99 | ]), |
| 100 | ]; |
| 101 | } |
| 102 | $titlePosition = isset($this->args['titlePosition']) ? $this->args['titlePosition'] : ''; |
| 103 | |
| 104 | if ($featuredImagePosition === 'aboveTitle' || $featuredImagePosition === 'belowTitle') { |
| 105 | $featuredImagePosition = 'centered'; |
| 106 | } |
| 107 | |
| 108 | if ($featuredImagePosition === 'centered') { |
| 109 | if ($titlePosition === 'aboveExcerpt') { |
| 110 | array_unshift($content, $featuredImage, $title); |
| 111 | } else { |
| 112 | array_unshift($content, $title, $featuredImage); |
| 113 | } |
| 114 | return [ |
| 115 | LayoutHelper::row([ |
| 116 | LayoutHelper::col($content), |
| 117 | ]), |
| 118 | ]; |
| 119 | } |
| 120 | |
| 121 | if ($titlePosition === 'aboveExcerpt') { |
| 122 | array_unshift($content, $title); |
| 123 | } |
| 124 | |
| 125 | if ($featuredImagePosition === 'alternate') { |
| 126 | $featuredImagePosition = $this->nextImagePosition(); |
| 127 | } |
| 128 | |
| 129 | $content = ($featuredImagePosition === 'left') |
| 130 | ? [ |
| 131 | LayoutHelper::col([$featuredImage]), |
| 132 | LayoutHelper::col($content), |
| 133 | ] |
| 134 | : [ |
| 135 | LayoutHelper::col($content), |
| 136 | LayoutHelper::col([$featuredImage]), |
| 137 | ]; |
| 138 | |
| 139 | $result = [ |
| 140 | LayoutHelper::row($content), |
| 141 | ]; |
| 142 | |
| 143 | if ($titlePosition !== 'aboveExcerpt') { |
| 144 | array_unshift( |
| 145 | $result, |
| 146 | LayoutHelper::row( |
| 147 | [ |
| 148 | LayoutHelper::col([$title]), |
| 149 | ] |
| 150 | ) |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | return $result; |
| 155 | } |
| 156 | |
| 157 | private function nextImagePosition() { |
| 158 | $this->imagePosition = ($this->imagePosition === 'left') ? 'right' : 'left'; |
| 159 | return $this->imagePosition; |
| 160 | } |
| 161 | |
| 162 | private function getFeaturedImagePosition(bool $isProduct) { |
| 163 | if ($this->args['displayType'] !== 'full') { |
| 164 | return $this->args['featuredImagePosition']; |
| 165 | } |
| 166 | |
| 167 | // For products with display type 'full' use 'featuredImagePosition' if 'fullPostFeaturedImagePosition' not set. |
| 168 | // This is because products always supported images, even for 'full' post display type. |
| 169 | if ($isProduct && empty($this->args['fullPostFeaturedImagePosition'])) { |
| 170 | return $this->args['featuredImagePosition']; |
| 171 | } |
| 172 | |
| 173 | // For posts with display type 'full' use 'fullPostFeaturedImagePosition'. This is for back compatibility |
| 174 | // with posts that don't have featured image but contain some value for 'featuredImagePosition' in the DB. |
| 175 | return $this->args['fullPostFeaturedImagePosition'] ?? 'none'; |
| 176 | } |
| 177 | } |
| 178 |