LayoutHelper.php
3 years ago
MetaInformationManager.php
2 months ago
PostContentManager.php
10 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
PostTransformerContentsExtractor.php
268 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\Config\Env; |
| 9 | use MailPoet\WooCommerce\Helper as WooCommerceHelper; |
| 10 | use MailPoet\WP\Functions as WPFunctions; |
| 11 | |
| 12 | class PostTransformerContentsExtractor { |
| 13 | |
| 14 | private $args; |
| 15 | |
| 16 | /** @var WPFunctions */ |
| 17 | private $wp; |
| 18 | |
| 19 | /** @var WooCommerceHelper */ |
| 20 | private $woocommerceHelper; |
| 21 | |
| 22 | public function __construct( |
| 23 | $args |
| 24 | ) { |
| 25 | $this->args = $args; |
| 26 | $this->wp = new WPFunctions(); |
| 27 | $this->woocommerceHelper = new WooCommerceHelper($this->wp); |
| 28 | } |
| 29 | |
| 30 | public function getContent($post, $withPostClass, $displayType) { |
| 31 | $contentManager = new PostContentManager(); |
| 32 | $metaManager = new MetaInformationManager(); |
| 33 | |
| 34 | $content = $contentManager->getContent($post, $this->args['displayType']); |
| 35 | if ($this->isWcProduct($post)) { |
| 36 | // For WC_Product objects, we need to get the corresponding post |
| 37 | $postId = $post->get_id(); |
| 38 | $wpPost = $this->wp->getPost($postId); |
| 39 | if ($wpPost) { |
| 40 | $content = $metaManager->appendMetaInformation($content, $wpPost, $this->args); |
| 41 | } |
| 42 | } else { |
| 43 | $content = $metaManager->appendMetaInformation($content, $post, $this->args); |
| 44 | } |
| 45 | $content = $contentManager->filterContent($content, $displayType, $withPostClass); |
| 46 | |
| 47 | $structureTransformer = new StructureTransformer(); |
| 48 | $content = $structureTransformer->transform($content, $this->args['imageFullWidth'] === true); |
| 49 | |
| 50 | if ($this->isProduct($post)) { |
| 51 | $content = $this->addProductDataToContent($content, $post); |
| 52 | } |
| 53 | |
| 54 | $readMoreBtn = $this->getReadMoreButton($post); |
| 55 | $blocksCount = count($content); |
| 56 | if (!$readMoreBtn) { |
| 57 | // Don't attach a button |
| 58 | } else if ($readMoreBtn['type'] === 'text' && $blocksCount > 0 && $content[$blocksCount - 1]['type'] === 'text') { |
| 59 | $content[$blocksCount - 1]['text'] .= $readMoreBtn['text']; |
| 60 | } else { |
| 61 | $content[] = $readMoreBtn; |
| 62 | } |
| 63 | return $content; |
| 64 | } |
| 65 | |
| 66 | private function getImageInfo($id) { |
| 67 | /* |
| 68 | * In some cases wp_get_attachment_image_src ignore the second parameter |
| 69 | * and use global variable $content_width value instead. |
| 70 | * By overriding it ourselves when ensure a constant behaviour regardless |
| 71 | * of the user setup. |
| 72 | * |
| 73 | * https://mailpoet.atlassian.net/browse/MAILPOET-1365 |
| 74 | */ |
| 75 | global $content_width; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps, default is NULL |
| 76 | |
| 77 | $contentWidthCopy = $content_width; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 78 | $content_width = Env::NEWSLETTER_CONTENT_WIDTH; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 79 | $imageInfo = $this->wp->wpGetAttachmentImageSrc($id, 'mailpoet_newsletter_max'); |
| 80 | $content_width = $contentWidthCopy; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 81 | |
| 82 | return $imageInfo; |
| 83 | } |
| 84 | |
| 85 | public function getFeaturedImage($post) { |
| 86 | if ($this->isWcProduct($post)) { |
| 87 | $postId = $post->get_id(); |
| 88 | $postTitle = $this->sanitizeTitle($post->get_name()); |
| 89 | } else { |
| 90 | $postId = $post->ID; |
| 91 | $postTitle = $this->sanitizeTitle($post->post_title); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 92 | } |
| 93 | $imageFullWidth = (bool)filter_var($this->args['imageFullWidth'], FILTER_VALIDATE_BOOLEAN); |
| 94 | |
| 95 | if (!has_post_thumbnail($postId)) { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | $thumbnailId = $this->wp->getPostThumbnailId($postId); |
| 100 | $imageInfo = $this->getImageInfo($thumbnailId); |
| 101 | |
| 102 | // get alt text |
| 103 | $rawAlt = get_post_meta( |
| 104 | $thumbnailId, |
| 105 | '_wp_attachment_image_alt', |
| 106 | true |
| 107 | ); |
| 108 | $altText = trim(strip_tags(is_string($rawAlt) ? $rawAlt : '')); |
| 109 | if (strlen($altText) === 0) { |
| 110 | // if the alt text is empty then use the post title |
| 111 | $altText = trim(strip_tags($postTitle)); |
| 112 | } |
| 113 | |
| 114 | return [ |
| 115 | 'type' => 'image', |
| 116 | 'link' => $this->wp->getPermalink($postId), |
| 117 | 'src' => $imageInfo[0], |
| 118 | 'alt' => $altText, |
| 119 | 'fullWidth' => $imageFullWidth, |
| 120 | 'width' => $imageInfo[1], |
| 121 | 'height' => $imageInfo[2], |
| 122 | 'styles' => [ |
| 123 | 'block' => [ |
| 124 | 'textAlign' => 'center', |
| 125 | ], |
| 126 | ], |
| 127 | ]; |
| 128 | } |
| 129 | |
| 130 | private function getReadMoreButton($post) { |
| 131 | if ($this->args['readMoreType'] === 'none') { |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | $postId = $this->isWcProduct($post) ? $post->get_id() : $post->ID; |
| 136 | |
| 137 | if ($this->args['readMoreType'] === 'button') { |
| 138 | $button = $this->args['readMoreButton']; |
| 139 | $button['url'] = $this->wp->getPermalink($postId); |
| 140 | return $button; |
| 141 | } |
| 142 | |
| 143 | $readMoreText = sprintf( |
| 144 | '<p><a href="%s">%s</a></p>', |
| 145 | $this->wp->getPermalink($postId), |
| 146 | $this->args['readMoreText'] |
| 147 | ); |
| 148 | |
| 149 | return [ |
| 150 | 'type' => 'text', |
| 151 | 'text' => $readMoreText, |
| 152 | ]; |
| 153 | } |
| 154 | |
| 155 | public function getTitle($post) { |
| 156 | if ($this->isWcProduct($post)) { |
| 157 | $title = $post->get_name(); |
| 158 | $postId = $post->get_id(); |
| 159 | } else { |
| 160 | $title = $post->post_title; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 161 | $postId = $post->ID; |
| 162 | } |
| 163 | |
| 164 | if (filter_var($this->args['titleIsLink'], FILTER_VALIDATE_BOOLEAN)) { |
| 165 | $title = '<a href="' . $this->wp->getPermalink($postId) . '">' . $title . '</a>'; |
| 166 | } |
| 167 | |
| 168 | if (in_array($this->args['titleFormat'], ['h1', 'h2', 'h3'])) { |
| 169 | $tag = $this->args['titleFormat']; |
| 170 | } elseif ($this->args['titleFormat'] === 'ul') { |
| 171 | $tag = 'li'; |
| 172 | } else { |
| 173 | $tag = 'h1'; |
| 174 | } |
| 175 | |
| 176 | $alignment = (in_array($this->args['titleAlignment'], ['left', 'right', 'center'])) ? $this->args['titleAlignment'] : 'left'; |
| 177 | |
| 178 | $title = '<' . $tag . ' data-post-id="' . $postId . '" style="text-align: ' . $alignment . ';">' . $title . '</' . $tag . '>'; |
| 179 | |
| 180 | // The allowed HTML is based on all the possible ways we might construct a $title above |
| 181 | $commonAttributes = [ |
| 182 | 'data-post-id' => [], |
| 183 | 'style' => [], |
| 184 | ]; |
| 185 | |
| 186 | $allowedTitleHtml = [ |
| 187 | 'a' => [ |
| 188 | 'href' => [], |
| 189 | ], |
| 190 | 'li' => $commonAttributes, |
| 191 | 'h1' => $commonAttributes, |
| 192 | 'h2' => $commonAttributes, |
| 193 | 'h3' => $commonAttributes, |
| 194 | 'b' => [], |
| 195 | 'i' => [], |
| 196 | 'strong' => [], |
| 197 | 'em' => [], |
| 198 | 'small' => [], |
| 199 | ]; |
| 200 | |
| 201 | return [ |
| 202 | 'type' => 'text', |
| 203 | 'text' => wp_kses($title, $allowedTitleHtml), |
| 204 | ]; |
| 205 | } |
| 206 | |
| 207 | private function getPrice($post) { |
| 208 | $price = null; |
| 209 | $product = null; |
| 210 | if ($this->woocommerceHelper->isWooCommerceActive()) { |
| 211 | if ($this->isWcProduct($post)) { |
| 212 | $product = $post; |
| 213 | } else { |
| 214 | $product = $this->woocommerceHelper->wcGetProduct($post->ID); |
| 215 | } |
| 216 | } |
| 217 | if ($product) { |
| 218 | $price = '<h2>' . strip_tags($product->get_price_html(), '<span><del>') . '</h2>'; |
| 219 | } |
| 220 | return $price; |
| 221 | } |
| 222 | |
| 223 | private function addProductDataToContent($content, $post) { |
| 224 | if (!isset($this->args['pricePosition']) || $this->args['pricePosition'] === 'hidden') { |
| 225 | return $content; |
| 226 | } |
| 227 | $price = $this->getPrice($post); |
| 228 | $blocksCount = count($content); |
| 229 | if ($blocksCount > 0 && $content[$blocksCount - 1]['type'] === 'text') { |
| 230 | if ($this->args['pricePosition'] === 'below') { |
| 231 | $content[$blocksCount - 1]['text'] = $content[$blocksCount - 1]['text'] . $price; |
| 232 | } else { |
| 233 | $content[$blocksCount - 1]['text'] = $price . $content[$blocksCount - 1]['text']; |
| 234 | } |
| 235 | } else { |
| 236 | $content[] = [ |
| 237 | 'type' => 'text', |
| 238 | 'text' => $price, |
| 239 | ]; |
| 240 | } |
| 241 | return $content; |
| 242 | } |
| 243 | |
| 244 | public function isProduct($post) { |
| 245 | return $this->isWcProduct($post) || $this->isPostTypeProduct($post); |
| 246 | } |
| 247 | |
| 248 | public function isWcProduct($post) { |
| 249 | return class_exists('\WC_Product') && $post instanceof \WC_Product; |
| 250 | } |
| 251 | |
| 252 | public function isPostTypeProduct($post) { |
| 253 | return isset($post->post_type) && $post->post_type === 'product'; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Replaces double quote character with a unicode |
| 258 | * alternative to avoid problems when inlining CSS. |
| 259 | * [MAILPOET-1937] |
| 260 | * |
| 261 | * @param string $title |
| 262 | * @return string |
| 263 | */ |
| 264 | private function sanitizeTitle($title) { |
| 265 | return str_replace('"', '"', $title); |
| 266 | } |
| 267 | } |
| 268 |