Changeset 56849 for trunk/src/wp-includes/blocks/image.php
- Timestamp:
- 10/12/2023 01:55:27 PM (16 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/image.php
r56816 r56849 17 17 */ 18 18 function render_block_core_image( $attributes, $content, $block ) { 19 if ( false === stripos( $content, '<img' ) ) { 20 return ''; 21 } 19 22 20 23 $processor = new WP_HTML_Tag_Processor( $content ); 21 $processor->next_tag( 'img' ); 22 23 if ( $processor->get_attribute( 'src' ) === null ) { 24 25 if ( ! $processor->next_tag( 'img' ) || null === $processor->get_attribute( 'src' ) ) { 24 26 return ''; 25 27 } … … 33 35 } 34 36 35 $lightbox_enabled = false;36 37 $link_destination = isset( $attributes['linkDestination'] ) ? $attributes['linkDestination'] : 'none'; 37 38 $lightbox_settings = block_core_image_get_lightbox_settings( $block->parsed_block ); 38 39 39 // If the lightbox is enabled and the image is not linked, flag the lightbox to be rendered. 40 if ( isset( $lightbox_settings ) && 'none' === $link_destination ) { 41 42 if ( isset( $lightbox_settings['enabled'] ) && true === $lightbox_settings['enabled'] ) { 43 $lightbox_enabled = true; 44 } 45 } 46 47 // If at least one block in the page has the lightbox, mark the block type as interactive. 48 if ( $lightbox_enabled ) { 40 $view_js_file_handle = 'wp-block-image-view'; 41 $script_handles = $block->block_type->view_script_handles; 42 43 /* 44 * If the lightbox is enabled and the image is not linked, add the filter 45 * and the JavaScript view file. 46 */ 47 if ( 48 isset( $lightbox_settings ) && 49 'none' === $link_destination && 50 isset( $lightbox_settings['enabled'] ) && 51 true === $lightbox_settings['enabled'] 52 ) { 49 53 $block->block_type->supports['interactivity'] = true; 50 } 51 52 // Determine whether the view script should be enqueued or not. 53 $view_js_file = 'wp-block-image-view'; 54 if ( ! wp_script_is( $view_js_file ) ) { 55 $script_handles = $block->block_type->view_script_handles; 56 54 55 if ( ! in_array( $view_js_file_handle, $script_handles, true ) ) { 56 $block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file_handle ) ); 57 } 58 59 /* 60 * This render needs to happen in a filter with priority 15 to ensure 61 * that it runs after the duotone filter and that duotone styles are 62 * applied to the image in the lightbox. We also need to ensure that the 63 * lightbox works with any plugins that might use filters as well. We 64 * can consider removing this in the future if the way the blocks are 65 * rendered changes, or if a new kind of filter is introduced. 66 */ 67 add_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15, 2 ); 68 } else { 69 /* 70 * Remove the filter and the JavaScript view file if previously added by 71 * other Image blocks. 72 */ 73 remove_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15 ); 57 74 // If the script is not needed, and it is still in the `view_script_handles`, remove it. 58 if ( ! $lightbox_enabled && in_array( $view_js_file, $script_handles, true ) ) { 59 $block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file ) ); 60 } 61 // If the script is needed, but it was previously removed, add it again. 62 if ( $lightbox_enabled && ! in_array( $view_js_file, $script_handles, true ) ) { 63 $block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file ) ); 64 } 65 } 66 67 if ( $lightbox_enabled ) { 68 // This render needs to happen in a filter with priority 15 to ensure that it 69 // runs after the duotone filter and that duotone styles are applied to the image 70 // in the lightbox. We also need to ensure that the lightbox works with any plugins 71 // that might use filters as well. We can consider removing this in the future if the 72 // way the blocks are rendered changes, or if a new kind of filter is introduced. 73 add_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15, 2 ); 75 if ( in_array( $view_js_file_handle, $script_handles, true ) ) { 76 $block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file_handle ) ); 77 } 74 78 } 75 79 … … 124 128 */ 125 129 function block_core_image_render_lightbox( $block_content, $block ) { 130 /* 131 * If it's not possible that an IMG element exists then return the given 132 * block content as-is. It may be that there's no actual image in the block 133 * or it could be that another plugin already modified this HTML. 134 */ 135 if ( false === stripos( $block_content, '<img' ) ) { 136 return $block_content; 137 } 138 126 139 $processor = new WP_HTML_Tag_Processor( $block_content ); 127 140 128 141 $aria_label = __( 'Enlarge image' ); 129 142 130 $processor->next_tag( 'img' ); 143 /* 144 * If there's definitely no IMG element in the block then return the given 145 * block content as-is. There's nothing that this code can knowingly modify 146 * to add the lightbox behavior. 147 */ 148 if ( ! $processor->next_tag( 'img' ) ) { 149 return $block_content; 150 } 151 131 152 $alt_attribute = $processor->get_attribute( 'alt' ); 132 153 … … 311 332 * 312 333 * @global WP_Scripts $wp_scripts 313 *314 * @return void315 334 */ 316 335 function block_core_image_ensure_interactivity_dependency() { … … 328 347 /** 329 348 * Registers the `core/image` block on server. 330 *331 * @return void332 349 */ 333 350 function register_block_core_image() {
Note: See TracChangeset
for help on using the changeset viewer.