[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Server-side rendering of the `core/image` block. 4 * 5 * @package WordPress 6 */ 7 8 /** 9 * Renders the `core/image` block on the server, 10 * adding a data-id attribute to the element if core/gallery has added on pre-render. 11 * 12 * @since 5.9.0 13 * 14 * @param array $attributes The block attributes. 15 * @param string $content The block content. 16 * @param WP_Block $block The block object. 17 * 18 * @return string The block content with the data-id attribute added. 19 */ 20 function render_block_core_image( $attributes, $content, $block ) { 21 if ( false === stripos( $content, '<img' ) ) { 22 return ''; 23 } 24 25 $p = new WP_HTML_Tag_Processor( $content ); 26 27 if ( ! $p->next_tag( 'img' ) || ! $p->get_attribute( 'src' ) ) { 28 return ''; 29 } 30 31 $has_id_binding = isset( $attributes['metadata']['bindings']['id'] ) && isset( $attributes['id'] ); 32 33 // Ensure the `wp-image-id` classname on the image block supports block bindings. 34 if ( $has_id_binding ) { 35 // If there's a mismatch with the 'wp-image-' class and the actual id, the id was 36 // probably overridden by block bindings. Update it to the correct value. 37 // See https://github.com/WordPress/gutenberg/issues/62886 for why this is needed. 38 $id = $attributes['id']; 39 $image_classnames = $p->get_attribute( 'class' ); 40 $class_with_binding_value = "wp-image-$id"; 41 if ( is_string( $image_classnames ) && ! str_contains( $image_classnames, $class_with_binding_value ) ) { 42 $image_classnames = preg_replace( '/wp-image-(\d+)/', $class_with_binding_value, $image_classnames ); 43 $p->set_attribute( 'class', $image_classnames ); 44 } 45 } 46 47 // For backwards compatibility, the data-id html attribute is only set for 48 // image blocks nested in a gallery. Detect if the image is in a gallery by 49 // checking the data-id attribute. 50 // See the `block_core_gallery_data_id_backcompatibility` function. 51 if ( isset( $attributes['data-id'] ) ) { 52 // If there's a binding for the `id`, the `id` attribute is used for the 53 // value, since `data-id` does not support block bindings. 54 // Else the `data-id` is used for backwards compatibility, since 55 // third parties may be filtering its value. 56 $data_id = $has_id_binding ? $attributes['id'] : $attributes['data-id']; 57 $p->set_attribute( 'data-id', $data_id ); 58 } 59 60 $link_destination = isset( $attributes['linkDestination'] ) ? $attributes['linkDestination'] : 'none'; 61 $lightbox_settings = block_core_image_get_lightbox_settings( $block->parsed_block ); 62 63 /* 64 * If the lightbox is enabled and the image is not linked, adds the filter and 65 * the JavaScript view file. 66 */ 67 if ( 68 isset( $lightbox_settings ) && 69 'none' === $link_destination && 70 isset( $lightbox_settings['enabled'] ) && 71 true === $lightbox_settings['enabled'] 72 ) { 73 wp_enqueue_script_module( '@wordpress/block-library/image/view' ); 74 75 /* 76 * This render needs to happen in a filter with priority 15 to ensure that 77 * it runs after the duotone filter and that duotone styles are applied to 78 * the image in the lightbox. Lightbox has to work with any plugins that 79 * might use filters as well. Removing this can be considered in the future 80 * if the way the blocks are rendered changes, or if a new kind of filter is 81 * introduced. 82 */ 83 add_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15, 2 ); 84 } else { 85 /* 86 * Remove the filter if previously added by other Image blocks. 87 */ 88 remove_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15 ); 89 } 90 91 return $p->get_updated_html(); 92 } 93 94 /** 95 * Adds the lightboxEnabled flag to the block data. 96 * 97 * This is used to determine whether the lightbox should be rendered or not. 98 * 99 * @since 6.4.0 100 * 101 * @param array $block Block data. 102 * 103 * @return array Filtered block data. 104 */ 105 function block_core_image_get_lightbox_settings( $block ) { 106 // Gets the lightbox setting from the block attributes. 107 if ( isset( $block['attrs']['lightbox'] ) ) { 108 $lightbox_settings = $block['attrs']['lightbox']; 109 } 110 111 if ( ! isset( $lightbox_settings ) ) { 112 $lightbox_settings = wp_get_global_settings( array( 'lightbox' ), array( 'block_name' => 'core/image' ) ); 113 114 // If not present in global settings, check the top-level global settings. 115 // 116 // NOTE: If no block-level settings are found, the previous call to 117 // `wp_get_global_settings` will return the whole `theme.json` structure in 118 // which case we can check if the "lightbox" key is present at the top-level 119 // of the global settings and use its value. 120 if ( isset( $lightbox_settings['lightbox'] ) ) { 121 $lightbox_settings = wp_get_global_settings( array( 'lightbox' ) ); 122 } 123 } 124 125 return $lightbox_settings ?? null; 126 } 127 128 /** 129 * Adds the directives and layout needed for the lightbox behavior. 130 * 131 * @since 6.4.0 132 * 133 * @param string $block_content Rendered block content. 134 * @param array $block Block object. 135 * 136 * @return string Filtered block content. 137 */ 138 function block_core_image_render_lightbox( $block_content, $block ) { 139 /* 140 * If there's no IMG tag in the block then return the given block content 141 * as-is. There's nothing that this code can knowingly modify to add the 142 * lightbox behavior. 143 */ 144 $p = new WP_HTML_Tag_Processor( $block_content ); 145 if ( $p->next_tag( 'figure' ) ) { 146 $p->set_bookmark( 'figure' ); 147 } 148 if ( ! $p->next_tag( 'img' ) ) { 149 return $block_content; 150 } 151 152 $alt = $p->get_attribute( 'alt' ); 153 $img_uploaded_src = $p->get_attribute( 'src' ); 154 $img_class_names = $p->get_attribute( 'class' ); 155 $img_styles = $p->get_attribute( 'style' ); 156 $img_width = 'none'; 157 $img_height = 'none'; 158 $aria_label = __( 'Enlarge' ); 159 $dialog_aria_label = __( 'Enlarged image' ); 160 161 if ( isset( $block['attrs']['id'] ) ) { 162 $img_uploaded_src = wp_get_attachment_url( $block['attrs']['id'] ); 163 $img_metadata = wp_get_attachment_metadata( $block['attrs']['id'] ); 164 $img_width = $img_metadata['width'] ?? 'none'; 165 $img_height = $img_metadata['height'] ?? 'none'; 166 } 167 168 // Figure. 169 $p->seek( 'figure' ); 170 $figure_class_names = $p->get_attribute( 'class' ); 171 $figure_styles = $p->get_attribute( 'style' ); 172 173 // Create unique id and set the image metadata in the state. 174 $unique_image_id = uniqid(); 175 176 wp_interactivity_state( 177 'core/image', 178 array( 179 'metadata' => array( 180 $unique_image_id => array( 181 'uploadedSrc' => $img_uploaded_src, 182 'figureClassNames' => $figure_class_names, 183 'figureStyles' => $figure_styles, 184 'imgClassNames' => $img_class_names, 185 'imgStyles' => $img_styles, 186 'targetWidth' => $img_width, 187 'targetHeight' => $img_height, 188 'scaleAttr' => $block['attrs']['scale'] ?? false, 189 'ariaLabel' => $dialog_aria_label, 190 'alt' => $alt, 191 ), 192 ), 193 ) 194 ); 195 196 $p->add_class( 'wp-lightbox-container' ); 197 $p->set_attribute( 'data-wp-interactive', 'core/image' ); 198 $p->set_attribute( 199 'data-wp-context', 200 wp_json_encode( 201 array( 202 'imageId' => $unique_image_id, 203 ), 204 JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP 205 ) 206 ); 207 208 // Image. 209 $p->next_tag( 'img' ); 210 $p->set_attribute( 'data-wp-init', 'callbacks.setButtonStyles' ); 211 $p->set_attribute( 'data-wp-on-async--load', 'callbacks.setButtonStyles' ); 212 $p->set_attribute( 'data-wp-on-async-window--resize', 'callbacks.setButtonStyles' ); 213 // Sets an event callback on the `img` because the `figure` element can also 214 // contain a caption, and we don't want to trigger the lightbox when the 215 // caption is clicked. 216 $p->set_attribute( 'data-wp-on-async--click', 'actions.showLightbox' ); 217 $p->set_attribute( 'data-wp-class--hide', 'state.isContentHidden' ); 218 $p->set_attribute( 'data-wp-class--show', 'state.isContentVisible' ); 219 220 $body_content = $p->get_updated_html(); 221 222 // Adds a button alongside image in the body content. 223 $img = null; 224 preg_match( '/<img[^>]+>/', $body_content, $img ); 225 226 $button = 227 $img[0] 228 . '<button 229 class="lightbox-trigger" 230 type="button" 231 aria-haspopup="dialog" 232 aria-label="' . esc_attr( $aria_label ) . '" 233 data-wp-init="callbacks.initTriggerButton" 234 data-wp-on-async--click="actions.showLightbox" 235 data-wp-style--right="state.imageButtonRight" 236 data-wp-style--top="state.imageButtonTop" 237 > 238 <svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12"> 239 <path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" /> 240 </svg> 241 </button>'; 242 243 $body_content = preg_replace( '/<img[^>]+>/', $button, $body_content ); 244 245 add_action( 'wp_footer', 'block_core_image_print_lightbox_overlay' ); 246 247 return $body_content; 248 } 249 250 /** 251 * @since 6.5.0 252 */ 253 function block_core_image_print_lightbox_overlay() { 254 $close_button_label = esc_attr__( 'Close' ); 255 256 // If the current theme does NOT have a `theme.json`, or the colors are not 257 // defined, it needs to set the background color & close button color to some 258 // default values because it can't get them from the Global Styles. 259 $background_color = '#fff'; 260 $close_button_color = '#000'; 261 if ( wp_theme_has_theme_json() ) { 262 $global_styles_color = wp_get_global_styles( array( 'color' ) ); 263 if ( ! empty( $global_styles_color['background'] ) ) { 264 $background_color = esc_attr( $global_styles_color['background'] ); 265 } 266 if ( ! empty( $global_styles_color['text'] ) ) { 267 $close_button_color = esc_attr( $global_styles_color['text'] ); 268 } 269 } 270 271 echo <<<HTML 272 <div 273 class="wp-lightbox-overlay zoom" 274 data-wp-interactive="core/image" 275 data-wp-context='{}' 276 data-wp-bind--role="state.roleAttribute" 277 data-wp-bind--aria-label="state.currentImage.ariaLabel" 278 data-wp-bind--aria-modal="state.ariaModal" 279 data-wp-class--active="state.overlayEnabled" 280 data-wp-class--show-closing-animation="state.showClosingAnimation" 281 data-wp-watch="callbacks.setOverlayFocus" 282 data-wp-on--keydown="actions.handleKeydown" 283 data-wp-on-async--touchstart="actions.handleTouchStart" 284 data-wp-on--touchmove="actions.handleTouchMove" 285 data-wp-on-async--touchend="actions.handleTouchEnd" 286 data-wp-on-async--click="actions.hideLightbox" 287 data-wp-on-async-window--resize="callbacks.setOverlayStyles" 288 data-wp-on-async-window--scroll="actions.handleScroll" 289 data-wp-bind--style="state.overlayStyles" 290 tabindex="-1" 291 > 292 <button type="button" aria-label="$close_button_label" style="fill: $close_button_color" class="close-button"> 293 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20" aria-hidden="true" focusable="false"><path d="m13.06 12 6.47-6.47-1.06-1.06L12 10.94 5.53 4.47 4.47 5.53 10.94 12l-6.47 6.47 1.06 1.06L12 13.06l6.47 6.47 1.06-1.06L13.06 12Z"></path></svg> 294 </button> 295 <div class="lightbox-image-container"> 296 <figure data-wp-bind--class="state.currentImage.figureClassNames" data-wp-bind--style="state.figureStyles"> 297 <img data-wp-bind--alt="state.currentImage.alt" data-wp-bind--class="state.currentImage.imgClassNames" data-wp-bind--style="state.imgStyles" data-wp-bind--src="state.currentImage.currentSrc"> 298 </figure> 299 </div> 300 <div class="lightbox-image-container"> 301 <figure data-wp-bind--class="state.currentImage.figureClassNames" data-wp-bind--style="state.figureStyles"> 302 <img data-wp-bind--alt="state.currentImage.alt" data-wp-bind--class="state.currentImage.imgClassNames" data-wp-bind--style="state.imgStyles" data-wp-bind--src="state.enlargedSrc"> 303 </figure> 304 </div> 305 <div class="scrim" style="background-color: $background_color" aria-hidden="true"></div> 306 </div> 307 HTML; 308 } 309 310 /** 311 * Registers the `core/image` block on server. 312 * 313 * @since 5.9.0 314 */ 315 function register_block_core_image() { 316 register_block_type_from_metadata( 317 __DIR__ . '/image', 318 array( 319 'render_callback' => 'render_block_core_image', 320 ) 321 ); 322 } 323 add_action( 'init', 'register_block_core_image' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Fri Feb 21 08:20:01 2025 | Cross-referenced by PHPXref |