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
Image.php
77 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\Renderer\EscapeHelper as EHelper; |
| 9 | use MailPoet\Newsletter\Renderer\StylesHelper; |
| 10 | use MailPoet\WP\Functions as WPFunctions; |
| 11 | |
| 12 | class Image { |
| 13 | public function render($element, $columnBaseWidth) { |
| 14 | if (empty($element['src'])) { |
| 15 | return ''; |
| 16 | } |
| 17 | if (substr($element['src'], 0, 1) == '/' && substr($element['src'], 1, 1) != '/') { |
| 18 | $element['src'] = WPFunctions::get()->getOption('siteurl') . $element['src']; |
| 19 | } |
| 20 | |
| 21 | $element['width'] = str_replace('px', '', $element['width']); |
| 22 | $element['height'] = str_replace('px', '', $element['height']); |
| 23 | $originalWidth = 0; |
| 24 | if (is_numeric($element['width']) && is_numeric($element['height'])) { |
| 25 | $element['width'] = (int)$element['width']; |
| 26 | $element['height'] = (int)$element['height']; |
| 27 | $originalWidth = $element['width']; |
| 28 | $element = $this->adjustImageDimensions($element, $columnBaseWidth); |
| 29 | } |
| 30 | |
| 31 | // If image was downsized because of column width set width to aways fill full column (e.g. on mobile) |
| 32 | $style = ''; |
| 33 | if ($element['fullWidth'] === true && $originalWidth > $element['width']) { |
| 34 | $style = 'style="width:100%"'; |
| 35 | } |
| 36 | |
| 37 | $imageTemplate = ' |
| 38 | <img src="' . EHelper::escapeHtmlLinkAttr($element['src']) . '" width="' . EHelper::escapeHtmlAttr($element['width']) . '" alt="' . EHelper::escapeHtmlAttr($element['alt']) . '"' . $style . '/> |
| 39 | '; |
| 40 | if (!empty($element['link'])) { |
| 41 | $imageTemplate = '<a href="' . EHelper::escapeHtmlLinkAttr($element['link']) . '">' . trim($imageTemplate) . '</a>'; |
| 42 | } |
| 43 | $align = 'center'; |
| 44 | if (!empty($element['styles']['block']['textAlign']) && in_array($element['styles']['block']['textAlign'], ['left', 'right'])) { |
| 45 | $align = $element['styles']['block']['textAlign']; |
| 46 | } |
| 47 | |
| 48 | $template = ' |
| 49 | <tr> |
| 50 | <td class="mailpoet_image ' . (($element['fullWidth'] === false) ? 'mailpoet_padded_vertical mailpoet_padded_side' : '') . '" align="' . EHelper::escapeHtmlAttr($align) . '" valign="top"> |
| 51 | ' . trim($imageTemplate) . ' |
| 52 | </td> |
| 53 | </tr>'; |
| 54 | return $template; |
| 55 | } |
| 56 | |
| 57 | public function adjustImageDimensions($element, $columnBaseWidth) { |
| 58 | $paddedWidth = StylesHelper::$paddingWidth * 2; |
| 59 | // scale image to fit column width |
| 60 | if ($element['width'] > $columnBaseWidth) { |
| 61 | $ratio = $element['width'] / $columnBaseWidth; |
| 62 | $element['width'] = $columnBaseWidth; |
| 63 | $element['height'] = (int)ceil($element['height'] / $ratio); |
| 64 | } |
| 65 | // resize image if the image is padded and wider than padded column width |
| 66 | if ( |
| 67 | $element['fullWidth'] === false && |
| 68 | $element['width'] > ($columnBaseWidth - $paddedWidth) |
| 69 | ) { |
| 70 | $ratio = $element['width'] / ($columnBaseWidth - $paddedWidth); |
| 71 | $element['width'] = $columnBaseWidth - $paddedWidth; |
| 72 | $element['height'] = (int)ceil($element['height'] / $ratio); |
| 73 | } |
| 74 | return $element; |
| 75 | } |
| 76 | } |
| 77 |