PluginProbe
Tableberg – Simple Gutenberg Table Block / trunk
Tableberg – Simple Gutenberg Table Block vtrunk
1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 0.5.8 All 41 releases
tableberg / renderer / Image / ImageRenderer.php

ImageRenderer.php in Tableberg – Simple Gutenberg Table Block trunk, at renderer/Image/ImageRenderer.php

182 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Tableberg\Renderer\Image;
4
5 class ImageRenderer {
6 /**
7 * @param array<string, mixed>|null $attributes
8 * @return string
9 */
10 public function render($attributes) {
11 $attrs = ImageAttrs::from_array($attributes);
12
13 $imageUrl = $attrs->media->url->asUrl();
14
15 $sizeSlug = $attrs->sizeSlug->asText();
16 if ($sizeSlug !== '') {
17 $sizeData = $attrs->media->sizes[$sizeSlug] ?? null;
18
19 if (
20 $sizeData instanceof MediaSizeAttrs &&
21 $sizeData->url->isNotEmpty()
22 ) {
23 $url = $sizeData->url->asUrl();
24 if ($url !== '') {
25 $imageUrl = $url;
26 }
27 }
28 }
29
30 if ($imageUrl === '') {
31 return '';
32 }
33
34 $imageStyles = ['display:block'];
35 if ($attrs->aspectRatio->isNotEmpty()) {
36 $imageStyles[] = 'aspect-ratio:' . $attrs->aspectRatio->asAttr();
37 }
38 if ($attrs->scale->isNotEmpty()) {
39 $imageStyles[] = 'object-fit:' . $attrs->scale->asAttr();
40 }
41 if ($attrs->width->isNotEmpty()) {
42 $imageStyles[] = 'width:' . $attrs->width->asAttr();
43 }
44 if ($attrs->height->isNotEmpty()) {
45 $imageStyles[] = 'height:' . $attrs->height->asAttr();
46 }
47 if ($attrs->border->top->isNotEmpty()) {
48 $imageStyles[] = 'border-top:' . $attrs->border->top->asAttr();
49 }
50 if ($attrs->border->right->isNotEmpty()) {
51 $imageStyles[] = 'border-right:' . $attrs->border->right->asAttr();
52 }
53 if ($attrs->border->bottom->isNotEmpty()) {
54 $imageStyles[] = 'border-bottom:' . $attrs->border->bottom->asAttr();
55 }
56 if ($attrs->border->left->isNotEmpty()) {
57 $imageStyles[] = 'border-left:' . $attrs->border->left->asAttr();
58 }
59 if ($attrs->borderRadius->topLeft->isNotEmpty()) {
60 $imageStyles[] = 'border-top-left-radius:' . $attrs->borderRadius->topLeft->asAttr();
61 }
62 if ($attrs->borderRadius->topRight->isNotEmpty()) {
63 $imageStyles[] = 'border-top-right-radius:' . $attrs->borderRadius->topRight->asAttr();
64 }
65 if ($attrs->borderRadius->bottomLeft->isNotEmpty()) {
66 $imageStyles[] = 'border-bottom-left-radius:' . $attrs->borderRadius->bottomLeft->asAttr();
67 }
68 if ($attrs->borderRadius->bottomRight->isNotEmpty()) {
69 $imageStyles[] = 'border-bottom-right-radius:' . $attrs->borderRadius->bottomRight->asAttr();
70 }
71
72 $alt = $attrs->alt->isNotEmpty() ? $attrs->alt->asAttr() : $attrs->media->alt->asAttr();
73
74 $imageStyleString = \esc_attr(implode(';', $imageStyles));
75
76 $imgHtml =
77 "<img
78 src='{$imageUrl}'
79 alt='{$alt}'
80 style='{$imageStyleString}'
81 />";
82
83 if ($attrs->href->isNotEmpty()) {
84 $target = $attrs->linkTarget->asAttr();
85 $href = $attrs->href->asUrl();
86 $relAttr = $target === '_blank' ? "rel='noopener noreferrer'" : '';
87
88 $imgHtml =
89 "<a
90 href='{$href}'
91 target='{$target}'
92 {$relAttr}
93 >
94 {$imgHtml}
95 </a>";
96 }
97
98 $captionHtml = '';
99 if ($attrs->caption->isNotEmpty()) {
100 $caption = $attrs->caption->asHtml();
101
102 $captionHtml =
103 "<figcaption class='tableberg-image-caption'>
104 {$caption}
105 </figcaption>";
106 }
107
108 $figureClass = 'tableberg-image-element';
109 if ($this->should_render_lightbox($attrs)) {
110 $figureClass .= ' wp-block-image';
111 }
112
113 $figureHtml =
114 "<figure
115 class='{$figureClass}'
116 style='margin:0;line-height:1'
117 >
118 {$imgHtml}
119 {$captionHtml}
120 </figure>";
121
122 if ($this->should_render_lightbox($attrs)) {
123 return $this->render_core_lightbox($figureHtml, $attrs);
124 }
125
126 return $figureHtml;
127 }
128
129 private function should_render_lightbox($attrs) {
130 return $attrs->lightbox->enabled->value() && $attrs->href->isEmpty();
131 }
132
133 private function render_core_lightbox($figureHtml, $attrs) {
134 if (
135 !function_exists('block_core_image_render_lightbox') ||
136 !function_exists('wp_enqueue_script_module')
137 ) {
138 return $figureHtml;
139 }
140
141 \wp_enqueue_script_module('@wordpress/block-library/image/view');
142
143 if (function_exists('wp_style_is') && \wp_style_is('wp-block-image', 'registered')) {
144 \wp_enqueue_style('wp-block-image');
145 }
146
147 $blockAttrs = [
148 'lightbox' => [
149 'enabled' => true,
150 ],
151 ];
152
153 if ($attrs->media->id->value() > 0) {
154 $blockAttrs['id'] = $attrs->media->id->value();
155 }
156
157 if ($attrs->scale->isNotEmpty()) {
158 $blockAttrs['scale'] = $attrs->scale->dangerouslyUseRawValue();
159 }
160
161 // block_core_image_render_lightbox() type-hints a real WP_Block, not
162 // just any object with a `context` property — a plain stdClass
163 // fatals with a TypeError. Reusing the registered `core/image` name
164 // gives it a real block type without side effects: the function
165 // only reads `$block_instance->context`, which stays empty here
166 // same as it would for a standalone (non-gallery) image.
167 $blockInstance = class_exists('WP_Block')
168 ? new \WP_Block(['blockName' => 'core/image', 'attrs' => $blockAttrs])
169 : null;
170
171 if (!$blockInstance) {
172 return $figureHtml;
173 }
174
175 return \block_core_image_render_lightbox(
176 $figureHtml,
177 ['attrs' => $blockAttrs],
178 $blockInstance
179 );
180 }
181 }
182