Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the lightbox filter and view file when the lightbox setting is disabled. #55120

Merged
merged 3 commits into from
Oct 11, 2023

Conversation

afercia
Copy link
Contributor

@afercia afercia commented Oct 6, 2023

Fixes #55119

What?

With multiple image block on a page, setting one image to open in the lightbox makes all following images open in the lightbox, even if the related setting is disabled.

Why?

Clearly not the expected behavior.

How?

  • Makes sure the filter that adds the lightbox markup and the view JavaScript file are removed when not needed.
  • Simplifies the code as there is really no need to set a $lightbox_enabled flag that stays true for all the following blocks.

Testing Instructions

  • Create a post.
  • Add four image blocks.
  • Set only the second image block to open the image in the lightbox.
  • Publish and view the front end.
  • Observe the first image has no lightbox enabled.
  • Observe the second image does have the lightbox enabled, as expected.
  • Check the lightbox works as expected.
  • Observe the third image has no lightbox enabled, as expected.
  • Observe the fourth image has no lightbox enabled, as expevted.

Additionally:

  • Set only the first image to open in the lightbox.
  • Save and view the front end.
  • Observe that only the first image opens in the lightbox.
  • Check the lightbox works as expected.

Testing Instructions for Keyboard

Screenshots or screencast

@afercia afercia added [Type] Bug An existing feature does not function as intended [Block] Image Affects the Image Block Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta [Feature] Interactivity API API to add frontend interactivity to blocks. labels Oct 6, 2023
@afercia afercia force-pushed the fix/image-block-lightbox-multiple-lightboxes branch from c5f1543 to 50fbe83 Compare October 6, 2023 13:45
@github-actions
Copy link

github-actions bot commented Oct 6, 2023

Flaky tests detected in f662b0dff40a991fe90d04b43675548b6647a33b.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6456476338
📝 Reported issues:

Copy link
Contributor

@artemiomorales artemiomorales left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this PR! I tested and while the code works when the lightbox is enabled for the second or subsequent images, it does not work when the lightbox is enabled on the first image, resulting in an error (see comment below).

I think this requires a little reworking of the logic. Here's a suggested revision that will only remove the view script if the lightbox settings are absent or the lightbox is disabled, which seems to work, though there are lots of ways of doing this and you may have ideas on how else we can clearly express the intent:

/*
* 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 lightbox is enabled and the image is not linked, add the filter
* and the JavaScript view file.
*/
if ( isset( $lightbox_settings ) && 'none' === $link_destination ) {

	if( isset( $lightbox_settings['enabled'] ) && true === $lightbox_settings['enabled'] ) {
		$block->block_type->supports['interactivity'] = true;

		if ( ! in_array( $view_js_file_handle, $block->block_type->view_script_handles, true ) ) {
			$block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file_handle ) );
		}

		/*
		* 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 );
	} else {
		// 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();

// 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 ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that when running this code when the lightbox is enabled for just the first image block, $script_handles contains the 'wp-block-image-view' handle, so this conditional erroneously evaluates to false and the view script is not loaded, resulting in the following error:

Screenshot 2023-10-06 at 4 36 22 PM

@afercia
Copy link
Contributor Author

afercia commented Oct 9, 2023

@artemiomorales thanks. Right, I think one of my last changes aimed to avoid the if / else but I see it is necessary instead.

@afercia
Copy link
Contributor Author

afercia commented Oct 9, 2023

An E2E test related to the cover image keeps failing.

editor/blocks/cover.spec.js:74:2 › Cover › dims background image down by 50% with the average image color when an image is uploaded 

It passes locally for me. It seems completely unrelated to the changes in this PR. I'll try to rebase onto latest trunk but if it keeps failing I't need some help to understand why.

@afercia afercia force-pushed the fix/image-block-lightbox-multiple-lightboxes branch from 382480b to f662b0d Compare October 9, 2023 12:16
@tellthemachines
Copy link
Contributor

An E2E test related to the cover image keeps failing.

This was just fixed in #55168 so another rebase should get it to pass!

@afercia afercia force-pushed the fix/image-block-lightbox-multiple-lightboxes branch from f662b0d to b396642 Compare October 10, 2023 08:26
@afercia
Copy link
Contributor Author

afercia commented Oct 10, 2023

Rebased on top of latest trunk. Hopefully the failing test should now pass.

@afercia
Copy link
Contributor Author

afercia commented Oct 10, 2023

Qustion: should this:

$block->block_type->supports['interactivity'] = true;

be reverted to false in the else?
I have no idea what that code line actually does. Any feedback would be appreciated.

Copy link
Contributor

@artemiomorales artemiomorales left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works as expected now 🙌

'none' === $link_destination &&
isset( $lightbox_settings['enabled'] ) &&
true === $lightbox_settings['enabled']
) {
$block->block_type->supports['interactivity'] = true;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@artemiomorales @SantosGuillamot when you have a chance, I'd appreciate your feedback on this line of code:

  • I have no idea what it does.
  • Is it really necessary?
  • If it is, should it be changed back to false in the else statement?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I understand, that line moves the interactivity scripts to the footer, which is a helpful optimization.
It shouldn't be changed to false, because it should be true so long as any image has the lightbox enabled.

Pinging @luisherranz to see if he'd like to detail better! In any case, I don't believe a change here is a blocker for this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're actually thinking about removing the conditionality and use supports.interactivity: true for any block that might be interactive at some point.

But we also need to figure out how those conditional interactive blocks can declare that conditionality, or even be able to combine different interactive behaviors depending on their configuration.

Related conversation here:

@artemiomorales artemiomorales merged commit a7d0c6f into trunk Oct 11, 2023
49 checks passed
@artemiomorales artemiomorales deleted the fix/image-block-lightbox-multiple-lightboxes branch October 11, 2023 08:02
@github-actions github-actions bot added this to the Gutenberg 16.9 milestone Oct 11, 2023
mikachan pushed a commit that referenced this pull request Oct 11, 2023
… disabled. (#55120)

* Remove the filter and view file of the lightbox when the lightbox setting is disabled.

* Make sure to add filter and view file when needed.

* Fix block comment indentation.
@mikachan
Copy link
Member

I just cherry-picked this PR to the 6.4-rc1 branch to get it included in the next release: 63bba04

@mikachan mikachan removed the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Oct 11, 2023
gziolo added a commit that referenced this pull request Oct 12, 2023
* List: fix forward merging of nested list (#55121)

* Private APIs: Update consent string for unlocking access. (#55182)

Replace the consent string with `I know using unstable features means my theme or plugin will inevitably break in the next version of WordPress`.

* useBlockSettings: add missing useMemo dependencies (#55204)

* Remove the lightbox filter and view file when the lightbox setting is disabled. (#55120)

* Remove the filter and view file of the lightbox when the lightbox setting is disabled.

* Make sure to add filter and view file when needed.

* Fix block comment indentation.

* Patterns: Remove the version enforcement for npm in `engines` field (#55245)

* Patterns: Remove the version enforcement for npm in `engines`

* Regenerate the lock file

* Remove `@return void` from PHP function docs. (#55237)

# Conflicts:
#	packages/block-library/src/form/index.php

* Image: Disable lightbox editor UI for linked images (#55141)

* Disable lightbox UI and add help text for linked images

* Update help text

* Image: Stop crashing with Lightbox on image blocks without an image (#55269)

* Stop crashing with Lightbox on image blocks without an image

* Fix PHPCS error

* Update fullscreen icon (#55021)

* Template Part block: Fall back to current theme if no theme attribute is given. (#55217)

* Template Part block: Fall back to current theme if no theme attribute is given.
* Remove now unnecessary _inject_theme_attribute_in_template_part_block() call from pattern block.

---------

Co-authored-by: Greg Ziółkowski <grzegorz@gziolo.pl>
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Co-authored-by: Felix Arntz <felixarntz@google.com>

---------

Co-authored-by: Ella <4710635+ellatrix@users.noreply.github.com>
Co-authored-by: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com>
Co-authored-by: Jarda Snajdr <jsnajdr@gmail.com>
Co-authored-by: Andrea Fercia <a.fercia@gmail.com>
Co-authored-by: Greg Ziółkowski <grzegorz@gziolo.pl>
Co-authored-by: tellthemachines <tellthemachines@users.noreply.github.com>
Co-authored-by: Artemio Morales <artemio.morales@a8c.com>
Co-authored-by: Rich Tabor <hi@richtabor.com>
Co-authored-by: Bernie Reiter <96308+ockham@users.noreply.github.com>
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Co-authored-by: Felix Arntz <felixarntz@google.com>
pento pushed a commit to WordPress/wordpress-develop that referenced this pull request Oct 12, 2023
Updates the npm packages and code for:

* [WordPress/gutenberg#55121 List: fix forward merging of nested list]

* [WordPress/gutenberg#55182 Update consent string for using private APIs.]

* [WordPress/gutenberg#55204 useBlockSettings: add missing useMemo dependencies]

* [WordPress/gutenberg#55120 Remove the lightbox filter and view file when the lightbox setting is disabled.]

* [WordPress/gutenberg#55245 Patterns: Remove the version enforcement for npm in engines field]

* [WordPress/gutenberg#55237 Remove `@return void` from PHP function docs]

* [WordPress/gutenberg#55141 Image: Disable lightbox editor UI for linked images]

* [WordPress/gutenberg#55269 Image: Stop crashing with Lightbox on image blocks without an image]

* [WordPress/gutenberg#55021 Update fullscreen icon]

* [WordPress/gutenberg#55217 Template Part block: Fall back to current theme if no theme attribute is given.] This change is part of fix for a performance regression re-introduced by [56818].

References:
* [WordPress/gutenberg#55298 Gutenberg PR 55298] Cherry-pick commits

Follow-up to [56818], [56816].

Props ellatrix, peterwilsoncc, jsnajdr, afercia, gziolo, isabel_brison, artemiosans, richtabor, bernhard-reiter, flixos90, mikachan, spacedmonkey, hellofromTonya.
See #59583, #59411.

git-svn-id: https://develop.svn.wordpress.org/trunk@56849 602fd350-edb4-49c9-b593-d223f7449a82
markjaquith pushed a commit to WordPress/WordPress that referenced this pull request Oct 12, 2023
Updates the npm packages and code for:

* [WordPress/gutenberg#55121 List: fix forward merging of nested list]

* [WordPress/gutenberg#55182 Update consent string for using private APIs.]

* [WordPress/gutenberg#55204 useBlockSettings: add missing useMemo dependencies]

* [WordPress/gutenberg#55120 Remove the lightbox filter and view file when the lightbox setting is disabled.]

* [WordPress/gutenberg#55245 Patterns: Remove the version enforcement for npm in engines field]

* [WordPress/gutenberg#55237 Remove `@return void` from PHP function docs]

* [WordPress/gutenberg#55141 Image: Disable lightbox editor UI for linked images]

* [WordPress/gutenberg#55269 Image: Stop crashing with Lightbox on image blocks without an image]

* [WordPress/gutenberg#55021 Update fullscreen icon]

* [WordPress/gutenberg#55217 Template Part block: Fall back to current theme if no theme attribute is given.] This change is part of fix for a performance regression re-introduced by [56818].

References:
* [WordPress/gutenberg#55298 Gutenberg PR 55298] Cherry-pick commits

Follow-up to [56818], [56816].

Props ellatrix, peterwilsoncc, jsnajdr, afercia, gziolo, isabel_brison, artemiosans, richtabor, bernhard-reiter, flixos90, mikachan, spacedmonkey, hellofromTonya.
See #59583, #59411.
Built from https://develop.svn.wordpress.org/trunk@56849


git-svn-id: http://core.svn.wordpress.org/trunk@56361 1a063a9b-81f0-0310-95a4-ce76da25c4cd
github-actions bot pushed a commit to platformsh/wordpress-performance that referenced this pull request Oct 12, 2023
Updates the npm packages and code for:

* [WordPress/gutenberg#55121 List: fix forward merging of nested list]

* [WordPress/gutenberg#55182 Update consent string for using private APIs.]

* [WordPress/gutenberg#55204 useBlockSettings: add missing useMemo dependencies]

* [WordPress/gutenberg#55120 Remove the lightbox filter and view file when the lightbox setting is disabled.]

* [WordPress/gutenberg#55245 Patterns: Remove the version enforcement for npm in engines field]

* [WordPress/gutenberg#55237 Remove `@return void` from PHP function docs]

* [WordPress/gutenberg#55141 Image: Disable lightbox editor UI for linked images]

* [WordPress/gutenberg#55269 Image: Stop crashing with Lightbox on image blocks without an image]

* [WordPress/gutenberg#55021 Update fullscreen icon]

* [WordPress/gutenberg#55217 Template Part block: Fall back to current theme if no theme attribute is given.] This change is part of fix for a performance regression re-introduced by [56818].

References:
* [WordPress/gutenberg#55298 Gutenberg PR 55298] Cherry-pick commits

Follow-up to [56818], [56816].

Props ellatrix, peterwilsoncc, jsnajdr, afercia, gziolo, isabel_brison, artemiosans, richtabor, bernhard-reiter, flixos90, mikachan, spacedmonkey, hellofromTonya.
See #59583, #59411.
Built from https://develop.svn.wordpress.org/trunk@56849


git-svn-id: https://core.svn.wordpress.org/trunk@56361 1a063a9b-81f0-0310-95a4-ce76da25c4cd
aaronjorbin pushed a commit to luminuu/wordpress-develop that referenced this pull request Oct 17, 2023
Updates the npm packages and code for:

* [WordPress/gutenberg#55121 List: fix forward merging of nested list]

* [WordPress/gutenberg#55182 Update consent string for using private APIs.]

* [WordPress/gutenberg#55204 useBlockSettings: add missing useMemo dependencies]

* [WordPress/gutenberg#55120 Remove the lightbox filter and view file when the lightbox setting is disabled.]

* [WordPress/gutenberg#55245 Patterns: Remove the version enforcement for npm in engines field]

* [WordPress/gutenberg#55237 Remove `@return void` from PHP function docs]

* [WordPress/gutenberg#55141 Image: Disable lightbox editor UI for linked images]

* [WordPress/gutenberg#55269 Image: Stop crashing with Lightbox on image blocks without an image]

* [WordPress/gutenberg#55021 Update fullscreen icon]

* [WordPress/gutenberg#55217 Template Part block: Fall back to current theme if no theme attribute is given.] This change is part of fix for a performance regression re-introduced by [56818].

References:
* [WordPress/gutenberg#55298 Gutenberg PR 55298] Cherry-pick commits

Follow-up to [56818], [56816].

Props ellatrix, peterwilsoncc, jsnajdr, afercia, gziolo, isabel_brison, artemiosans, richtabor, bernhard-reiter, flixos90, mikachan, spacedmonkey, hellofromTonya.
See #59583, #59411.

git-svn-id: https://develop.svn.wordpress.org/trunk@56849 602fd350-edb4-49c9-b593-d223f7449a82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Image Affects the Image Block [Feature] Interactivity API API to add frontend interactivity to blocks. [Type] Bug An existing feature does not function as intended
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Image block lightbox: setting lightbox on an image makes all following images open in the lightbox
5 participants