From 094b1d6e4ff69b73311f79407ca0fde94ead99da Mon Sep 17 00:00:00 2001 From: Andrea Fercia Date: Fri, 6 Oct 2023 13:06:27 +0200 Subject: [PATCH 1/3] Remove the filter and view file of the lightbox when the lightbox setting is disabled. --- packages/block-library/src/image/index.php | 60 +++++++++++----------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/packages/block-library/src/image/index.php b/packages/block-library/src/image/index.php index e1f71964622c0c..7a7f47ccee2a4a 100644 --- a/packages/block-library/src/image/index.php +++ b/packages/block-library/src/image/index.php @@ -32,44 +32,46 @@ function render_block_core_image( $attributes, $content, $block ) { $processor->set_attribute( 'data-id', $attributes['data-id'] ); } - $lightbox_enabled = false; $link_destination = isset( $attributes['linkDestination'] ) ? $attributes['linkDestination'] : 'none'; $lightbox_settings = block_core_image_get_lightbox_settings( $block->parsed_block ); - // If the lightbox is enabled and the image is not linked, flag the lightbox to be rendered. - if ( isset( $lightbox_settings ) && 'none' === $link_destination ) { - - if ( isset( $lightbox_settings['enabled'] ) && true === $lightbox_settings['enabled'] ) { - $lightbox_enabled = true; - } + $view_js_file_handle = 'wp-block-image-view'; + $script_handles = $block->block_type->view_script_handles; + + /* + * Remove the filter and the JavaScript view file if previously added by + * other Image blocks. + */ + remove_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15 ); + // If the script is not needed, and it is still in the `view_script_handles`, remove it. + if ( in_array( $view_js_file_handle, $script_handles, true ) ) { + $block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file_handle ) ); } - // If at least one block in the page has the lightbox, mark the block type as interactive. - if ( $lightbox_enabled ) { + /* + * If the lightbox is enabled and the image is not linked, add the filter + * and the JavaScript view file. + */ + if ( + isset( $lightbox_settings ) && + 'none' === $link_destination && + isset( $lightbox_settings['enabled'] ) && + true === $lightbox_settings['enabled'] + ) { $block->block_type->supports['interactivity'] = true; - } - - // Determine whether the view script should be enqueued or not. - $view_js_file = 'wp-block-image-view'; - if ( ! wp_script_is( $view_js_file ) ) { - $script_handles = $block->block_type->view_script_handles; - // If the script is not needed, and it is still in the `view_script_handles`, remove it. - if ( ! $lightbox_enabled && in_array( $view_js_file, $script_handles, true ) ) { - $block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file ) ); + if ( ! in_array( $view_js_file_handle, $script_handles, true ) ) { + $block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file_handle ) ); } - // If the script is needed, but it was previously removed, add it again. - if ( $lightbox_enabled && ! in_array( $view_js_file, $script_handles, true ) ) { - $block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file ) ); - } - } - if ( $lightbox_enabled ) { - // This render needs to happen in a filter with priority 15 to ensure that it - // runs after the duotone filter and that duotone styles are applied to the image - // in the lightbox. We also need to ensure that the lightbox works with any plugins - // that might use filters as well. We can consider removing this in the future if the - // way the blocks are rendered changes, or if a new kind of filter is introduced. + /* + * This render needs to happen in a filter with priority 15 to ensure + * that it runs after the duotone filter and that duotone styles are + * applied to the image in the lightbox. We also need to ensure that the + * lightbox works with any plugins that might use filters as well. We + * can consider removing this in the future if the way the blocks are + * rendered changes, or if a new kind of filter is introduced. + */ add_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15, 2 ); } From b396642eaa15bf5a47f1fa584e356e2a82f581a5 Mon Sep 17 00:00:00 2001 From: Andrea Fercia Date: Mon, 9 Oct 2023 12:25:56 +0200 Subject: [PATCH 2/3] Make sure to add filter and view file when needed. --- packages/block-library/src/image/index.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/block-library/src/image/index.php b/packages/block-library/src/image/index.php index 7a7f47ccee2a4a..79e34a4a986e6b 100644 --- a/packages/block-library/src/image/index.php +++ b/packages/block-library/src/image/index.php @@ -38,16 +38,6 @@ function render_block_core_image( $attributes, $content, $block ) { $view_js_file_handle = 'wp-block-image-view'; $script_handles = $block->block_type->view_script_handles; - /* - * Remove the filter and the JavaScript view file if previously added by - * other Image blocks. - */ - remove_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15 ); - // If the script is not needed, and it is still in the `view_script_handles`, remove it. - if ( in_array( $view_js_file_handle, $script_handles, true ) ) { - $block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file_handle ) ); - } - /* * If the lightbox is enabled and the image is not linked, add the filter * and the JavaScript view file. @@ -73,6 +63,16 @@ function render_block_core_image( $attributes, $content, $block ) { * rendered changes, or if a new kind of filter is introduced. */ add_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15, 2 ); + } else { + /* + * Remove the filter and the JavaScript view file if previously added by + * other Image blocks. + */ + remove_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15 ); + // If the script is not needed, and it is still in the `view_script_handles`, remove it. + if ( in_array( $view_js_file_handle, $script_handles, true ) ) { + $block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file_handle ) ); + } } return $processor->get_updated_html(); From e498bf9579a45bb803d7713f7c338cd2837ec36c Mon Sep 17 00:00:00 2001 From: Andrea Fercia Date: Tue, 10 Oct 2023 14:13:28 +0200 Subject: [PATCH 3/3] Fix block comment indentation. --- packages/block-library/src/image/index.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/image/index.php b/packages/block-library/src/image/index.php index 79e34a4a986e6b..95fb74aac73aa3 100644 --- a/packages/block-library/src/image/index.php +++ b/packages/block-library/src/image/index.php @@ -65,9 +65,9 @@ function render_block_core_image( $attributes, $content, $block ) { add_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15, 2 ); } else { /* - * Remove the filter and the JavaScript view file if previously added by - * other Image blocks. - */ + * Remove the filter and the JavaScript view file if previously added by + * other Image blocks. + */ remove_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15 ); // If the script is not needed, and it is still in the `view_script_handles`, remove it. if ( in_array( $view_js_file_handle, $script_handles, true ) ) {