diff --git a/lib/Form/Block/Image.php b/lib/Form/Block/Image.php index 68aee51cf1..8437c8b356 100644 --- a/lib/Form/Block/Image.php +++ b/lib/Form/Block/Image.php @@ -2,7 +2,16 @@ namespace MailPoet\Form\Block; +use MailPoet\WP\Functions as WPFunctions; + class Image { + /** @var WPFunctions */ + private $wp; + + public function __construct(WPFunctions $wp) { + $this->wp = $wp; + } + public function render(array $block): string { if (empty($block['params']['url'])) { return ''; @@ -18,8 +27,8 @@ class Image { $attributes[] = 'title="' . $params['title'] . '"'; } if ($params['id']) { - // WordPress automatically renders srcset based on this class $attributes[] = 'class="wp-image-' . $params['id'] . '"'; + $attributes[] = 'srcset="' . $this->wp->wpGetAttachmentImageSrcset(intval($params['id']), $params['size_slug']) . '"'; } if ($params['width']) { $attributes[] = 'width="' . intval($params['width']) . '"'; diff --git a/lib/WP/Functions.php b/lib/WP/Functions.php index e11d575bc5..377a43c0f1 100644 --- a/lib/WP/Functions.php +++ b/lib/WP/Functions.php @@ -635,6 +635,10 @@ class Functions { return activate_plugin($plugin, $redirect, $networkWide, $silent); } + public function wpGetAttachmentImageSrcset(int $attachmentId, $size = 'medium', $imageMeta = null) { + return wp_get_attachment_image_srcset($attachmentId, $size, $imageMeta); + } + /** * @param string $host * @return array|bool diff --git a/tests/unit/Form/Block/ImageTest.php b/tests/unit/Form/Block/ImageTest.php index bd01eba46a..981ead466e 100644 --- a/tests/unit/Form/Block/ImageTest.php +++ b/tests/unit/Form/Block/ImageTest.php @@ -4,6 +4,8 @@ namespace MailPoet\Test\Form\Block; use MailPoet\Form\Block\Image; use MailPoet\Test\Form\HtmlParser; +use MailPoet\WP\Functions as WPFunctions; +use PHPUnit\Framework\MockObject\MockObject; require_once __DIR__ . '/../HtmlParser.php'; @@ -11,6 +13,9 @@ class ImageTest extends \MailPoetUnitTest { /** @var Image */ private $image; + /** @var MockObject & WPFunctions */ + private $wpMock; + /** @var array */ private $block = [ 'type' => 'image', @@ -40,11 +45,17 @@ class ImageTest extends \MailPoetUnitTest { public function _before() { parent::_before(); - $this->image = new Image(); + $this->wpMock = $this->createMock(WPFunctions::class); + $this->image = new Image($this->wpMock); $this->htmlParser = new HtmlParser(); + } public function testItShouldRenderImageBlock() { + $this->wpMock + ->expects($this->once()) + ->method('wpGetAttachmentImageSrcset') + ->willReturn('srcsetvalue'); $html = $this->image->render($this->block); $block = $this->htmlParser->getElementByXpath($html, '//div'); $blockClass = $this->htmlParser->getAttribute($block, 'class'); @@ -57,6 +68,8 @@ class ImageTest extends \MailPoetUnitTest { $img = $this->htmlParser->getChildElement($figure, 'img'); $imgSrc = $this->htmlParser->getAttribute($img, 'src'); expect($imgSrc->value)->equals('http://example.com/image.jpg'); + $imgSrcset = $this->htmlParser->getAttribute($img, 'srcset'); + expect($imgSrcset->value)->equals('srcsetvalue'); $imgWidth = $this->htmlParser->getAttribute($img, 'width'); expect($imgWidth->value)->equals(100); $imgHeight = $this->htmlParser->getAttribute($img, 'height'); @@ -71,7 +84,9 @@ class ImageTest extends \MailPoetUnitTest { } public function testItShouldRenderImageBlockWithLink() { + $this->wpMock->expects($this->never())->method('wpGetAttachmentImageSrcset'); $block = $this->block; + $block['params']['id'] = null; $block['params']['link_class'] = 'link-class'; $block['params']['link_target'] = '_blank'; $block['params']['rel'] = 'relrel';