BlockRendererHelper.php
3 weeks ago
Checkbox.php
1 year ago
Close.php
2 weeks ago
Column.php
2 months ago
Columns.php
2 months ago
Date.php
1 year ago
Divider.php
3 years ago
Heading.php
2 months ago
Html.php
1 year ago
Image.php
10 months ago
Paragraph.php
3 weeks ago
Radio.php
1 year ago
Segment.php
2 months ago
Select.php
2 months ago
Submit.php
1 year ago
Text.php
1 year ago
Textarea.php
1 year ago
index.php
3 years ago
Image.php
93 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Form\Block; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Form\FormHtmlSanitizer; |
| 9 | use MailPoet\WP\Functions as WPFunctions; |
| 10 | |
| 11 | class Image { |
| 12 | /** @var WPFunctions */ |
| 13 | private $wp; |
| 14 | |
| 15 | /** @var FormHtmlSanitizer */ |
| 16 | private $htmlSanitizer; |
| 17 | |
| 18 | public function __construct( |
| 19 | WPFunctions $wp, |
| 20 | FormHtmlSanitizer $htmlSanitizer |
| 21 | ) { |
| 22 | $this->wp = $wp; |
| 23 | $this->htmlSanitizer = $htmlSanitizer; |
| 24 | } |
| 25 | |
| 26 | public function render(array $block): string { |
| 27 | if (empty($block['params']['url'])) { |
| 28 | return ''; |
| 29 | } |
| 30 | return $this->wrapImage($block['params'], $this->renderImage($block['params'])); |
| 31 | } |
| 32 | |
| 33 | private function renderImage(array $params): string { |
| 34 | $attributes = []; |
| 35 | $styles = []; |
| 36 | $attributes[] = 'src="' . $this->wp->escUrl($params['url']) . '"'; |
| 37 | $attributes[] = $params['alt'] ? 'alt="' . $this->wp->escAttr($params['alt']) . '"' : 'alt'; |
| 38 | if ($params['title']) { |
| 39 | $attributes[] = 'title="' . $this->wp->escAttr($params['title']) . '"'; |
| 40 | } |
| 41 | if ($params['id']) { |
| 42 | $attributes[] = 'class="wp-image-' . $this->wp->escAttr($params['id']) . '"'; |
| 43 | $attributes[] = 'srcset="' . $this->wp->wpGetAttachmentImageSrcset(intval($params['id']), $params['size_slug']) . '"'; |
| 44 | } |
| 45 | if ($params['width']) { |
| 46 | $attributes[] = 'width=' . intval($params['width']); |
| 47 | $styles[] = 'width: ' . intval($params['width']) . 'px'; |
| 48 | } |
| 49 | if ($params['height']) { |
| 50 | $attributes[] = 'height=' . intval($params['height']); |
| 51 | $styles[] = 'height: ' . intval($params['height']) . 'px'; |
| 52 | } |
| 53 | if ($styles) { |
| 54 | $attributes[] = 'style="' . $this->wp->escAttr(implode(';', $styles)) . '"'; |
| 55 | } |
| 56 | return '<img ' . implode(' ', $attributes) . '>'; |
| 57 | } |
| 58 | |
| 59 | private function wrapImage(array $params, string $img): string { |
| 60 | // Figure |
| 61 | $figureClasses = ['size-' . $params['size_slug']]; |
| 62 | if ($params['align']) { |
| 63 | $figureClasses[] = 'align' . $params['align']; |
| 64 | } |
| 65 | // Link |
| 66 | if ($params['href']) { |
| 67 | $img = $this->wrapToLink($params, $img); |
| 68 | } |
| 69 | $caption = isset($params['caption']) && $params['caption'] ? "<figcaption>{$this->htmlSanitizer->sanitize($params['caption'])}</figcaption>" : ''; |
| 70 | $figure = '<figure class="' . $this->wp->escAttr(implode(' ', $figureClasses)) . '">' . $img . $caption . '</figure>'; |
| 71 | // Main wrapper |
| 72 | $divClasses = ['mailpoet_form_image']; |
| 73 | if (trim($params['class_name'])) { |
| 74 | $divClasses[] = trim($params['class_name']); |
| 75 | } |
| 76 | return '<div class="' . $this->wp->escAttr(implode(' ', $divClasses)) . '">' . $figure . '</div>'; |
| 77 | } |
| 78 | |
| 79 | private function wrapToLink(array $params, string $img): string { |
| 80 | $attributes = ['href="' . $this->wp->escUrl($params['href']) . '"']; |
| 81 | if ($params['link_class']) { |
| 82 | $attributes[] = 'class="' . $this->wp->escAttr($params['link_class']) . '"'; |
| 83 | } |
| 84 | if ($params['link_target']) { |
| 85 | $attributes[] = 'target="' . $this->wp->escAttr($params['link_target']) . '"'; |
| 86 | } |
| 87 | if ($params['rel']) { |
| 88 | $attributes[] = 'rel="' . $this->wp->escAttr($params['rel']) . '"'; |
| 89 | } |
| 90 | return '<a ' . implode(' ', $attributes) . ' >' . $img . '</a>'; |
| 91 | } |
| 92 | } |
| 93 |