| @@ -104,14 +104,78 @@ | ||
| 104 | 104 | {$caption} |
| 105 | 105 | </figcaption>"; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - return | |
| 108 | + $figureClass = 'tableberg-image-element'; | |
| 109 | + if ($this->should_render_lightbox($attrs)) { | |
| 110 | + $figureClass .= ' wp-block-image'; | |
| 111 | + } | |
| 112 | + | |
| 113 | + $figureHtml = | |
| 109 | 114 | "<figure |
| 110 | - class='tableberg-image-element' | |
| 115 | + class='{$figureClass}' | |
| 111 | 116 | style='margin:0;line-height:1' |
| 112 | 117 | > |
| 113 | 118 | {$imgHtml} |
| 114 | 119 | {$captionHtml} |
| 115 | 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 | + ); | |
| 116 | 180 | } |
| 117 | 181 | } |