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:
Rostislav Wolny
2020-04-02 16:58:40 +02:00
committed by Veljko V
parent eacc426639
commit b465e5205d
3 changed files with 30 additions and 2 deletions

View File

@ -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']) . '"';