Add srcset attribute rendering to image block renderer
Srcset is not rendered automatically when image is added in the content hook. [MAILPOET-2750]
This commit is contained in:
committed by
Veljko V
parent
eacc426639
commit
b465e5205d
@ -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']) . '"';
|
||||
|
@ -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
|
||||
|
@ -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';
|
||||
|
Reference in New Issue
Block a user