wp = $wp;
$this->htmlSanitizer = $htmlSanitizer;
}
public function render(array $block): string {
if (empty($block['params']['url'])) {
return '';
}
return $this->wrapImage($block['params'], $this->renderImage($block['params']));
}
private function renderImage(array $params): string {
$attributes = [];
$styles = [];
$attributes[] = 'src="' . $this->wp->escAttr($params['url']) . '"';
$attributes[] = $params['alt'] ? 'alt="' . $this->wp->escAttr($params['alt']) . '"' : 'alt';
if ($params['title']) {
$attributes[] = 'title="' . $this->wp->escAttr($params['title']) . '"';
}
if ($params['id']) {
$attributes[] = 'class="wp-image-' . $this->wp->escAttr($params['id']) . '"';
$attributes[] = 'srcset="' . $this->wp->wpGetAttachmentImageSrcset(intval($params['id']), $params['size_slug']) . '"';
}
if ($params['width']) {
$attributes[] = 'width=' . intval($params['width']);
$styles[] = 'width: ' . intval($params['width']) . 'px';
}
if ($params['height']) {
$attributes[] = 'height=' . intval($params['height']);
$styles[] = 'height: ' . intval($params['height']) . 'px';
}
if ($styles) {
$attributes[] = 'style="' . $this->wp->escAttr(implode(';', $styles)) . '"';
}
return '';
}
private function wrapImage(array $params, string $img): string {
// Figure
$figureClasses = ['size-' . $params['size_slug']];
if ($params['align']) {
$figureClasses[] = 'align' . $params['align'];
}
// Link
if ($params['href']) {
$img = $this->wrapToLink($params, $img);
}
$caption = $params['caption'] ? "