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

Try: Make gallery randomization work when nested #58733

Merged
merged 7 commits into from Feb 7, 2024

Conversation

t-hamano
Copy link
Contributor

@t-hamano t-hamano commented Feb 6, 2024

Fixes #58404

What?

This PR resolves an issue where the randomization of the Gallery block added in #57477 does not work when nested.

Why?

Randomization is achieved by shuffling the $parsed_block['innerBlocks'] via the render_block_data hook. However, we found a problem with this hook: it cannot override $parsed_block['innerBlocks'] when blocks are nested.

This issue is also reported in the core ticket: https://core.trac.wordpress.org/ticket/56417

Ideally, render_block_data should be improved, but the impact is probably large and there isn't much time left to fix it in WP6.5.

So in this PR, we will try similar randomization in the render callback function instead of render_block_data. If we can't find a good solution, we may need to consider reverting the randomization function.

How?

Sort theIimage blocks within the content HTML based on the following logic:

  • Use WP_HTML_Tag_Processor to add a sequential number (data-image-index) to elements with the wp-block-image class. This is to identify duplicate images when they are included in a gallery.
  • Use the preg_match_all() function to extract elements with the data-image-index attribute, i.e. Image blocks.
  • Randomize the extracted Image blocks.
  • Use the preg_replace_callback() function to extract the Image blocks again and replace each Image block with the randomized Image in turn.
  • Finally, use WP_HTML_Tag_Processor to remove unnecessary data-image-index attributes.

To be honest, this approach may not be perfect for the following reasons:

  • What if the user has already given the data-image-index attribute?
  • What if the user had rewritten the wp-block-image class?
  • What if the user uses the new allowed_blocks to insert something other than an image block?

Let me discuss whether we should ship this approach or bring back the randomization feature. 🙇

Testing Instructions

  • Insert a Gallery block and add images.
  • Enable "Randomize order".
  • On the front end, check that the image order changes every time you reload the browser.

@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Block] Gallery Affects the Gallery Block - used to display groups of images labels Feb 6, 2024
@t-hamano t-hamano self-assigned this Feb 6, 2024
Copy link

github-actions bot commented Feb 6, 2024

Flaky tests detected in ddc7ff9.
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/7800718336
📝 Reported issues:

@t-hamano t-hamano marked this pull request as ready for review February 6, 2024 14:12
@t-hamano t-hamano requested a review from geriux as a code owner February 6, 2024 14:12
Copy link

github-actions bot commented Feb 6, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core SVN

Core Committers: Use this line as a base for the props when committing in SVN:

Props wildworks, aaronrobertshaw, youknowriad, ramonopoly, richtabor.

GitHub Merge commits

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: aaronrobertshaw <aaronrobertshaw@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: richtabor <richtabor@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Contributor

@aaronrobertshaw aaronrobertshaw 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 the fix @t-hamano 👍

✅ I could replicate the issue when a randomized gallery was nested
✅ This PR fixes the issue

I think there's some unnecessary manipulation of the gallery markup which we could avoid. I've left some suggested code tweaks inline but in case it's easier I'll include a diff in this review.

Apologies if I'm missing some logic as to why we need to add and then remove index attributes.

Diff with suggestion changes
diff --git a/packages/block-library/src/gallery/index.php b/packages/block-library/src/gallery/index.php
index e3eeeef3f0..464449154a 100644
--- a/packages/block-library/src/gallery/index.php
+++ b/packages/block-library/src/gallery/index.php
@@ -123,27 +123,17 @@ function block_core_gallery_render( $attributes, $content ) {
 	// nested.
 	// TODO: In the future, if this hook supports updating innerBlocks in
 	// nested blocks, it should be refactored.
-	// See: https://github.com/WordPress/gutenberg/pull/58733
+	// See: https://github.com/WordPress/gutenberg/pull/58733.
 	if ( empty( $attributes['randomOrder'] ) ) {
 		return $processed_content;
 	}
 
-	$processor = new WP_HTML_Tag_Processor( (string) $processed_content );
-
-	// Add an index to each figure tag to uniquely identify each Image block.
-	$i = 0;
-	while ( $processor->next_tag( array( 'class_name' => 'wp-block-image' ) ) ) {
-		$processor->set_attribute( 'data-image-index', $i );
-		++$i;
-	}
-	$content = $processor->get_updated_html();
-
-	$pattern = '/<figure[^>]*\bdata-image-index\b[^>]*>.*?<\/figure>/';
+	$pattern = '/<figure[^>]*\bwp-block-image\b[^>]*>.*?<\/figure>/';
 
 	// Find all Image blocks.
-	preg_match_all( $pattern, $content, $matches );
+	preg_match_all( $pattern, $processed_content, $matches );
 	if ( ! $matches ) {
-		return $content;
+		return $processed_content;
 	}
 	$image_blocks = $matches[0];
 
@@ -157,16 +147,9 @@ function block_core_gallery_render( $attributes, $content ) {
 			++$i;
 			return $new_image_block;
 		},
-		$content
+		$processed_content
 	);
 
-	// Remove the index from each Image block.
-	$processor = new WP_HTML_Tag_Processor( $content );
-	while ( $processor->next_tag( array( 'class_name' => 'wp-block-image' ) ) ) {
-		$processor->remove_attribute( 'data-image-index' );
-	}
-	$content = $processor->get_updated_html();
-
 	return $content;
 }
 /**

Here's what I see with the suggested diff applied. The top gallery is nested while the second is a duplicate of the first gallery but as a top level block.

Screen.Recording.2024-02-07.at.1.24.35.pm.mp4

packages/block-library/src/gallery/index.php Outdated Show resolved Hide resolved
packages/block-library/src/gallery/index.php Outdated Show resolved Hide resolved
packages/block-library/src/gallery/index.php Outdated Show resolved Hide resolved
packages/block-library/src/gallery/index.php Outdated Show resolved Hide resolved
@t-hamano
Copy link
Contributor Author

t-hamano commented Feb 7, 2024

Thanks for the review, @aaronrobertshaw!

I think the index-less approach you suggested makes sense and also made the code simpler. I think I've handled everything, but if I've missed anything please let me know.

Copy link
Contributor

@aaronrobertshaw aaronrobertshaw 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 the fast turnaround on these changes @t-hamano!

The code is looking good. I left one small comment regarding another new linting error but if that gets tweaked, I'm happy to give this an approval now.

This will also need to get backported in time for 6.5, correct?

One last thing, I think this is the sort of thing that would be good to have some test coverage for. As a follow-up, maybe a unit test could be added? The cover block might provide an example.

packages/block-library/src/gallery/index.php Outdated Show resolved Hide resolved
t-hamano and others added 2 commits February 7, 2024 14:48
Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
@t-hamano
Copy link
Contributor Author

t-hamano commented Feb 7, 2024

This will also need to get backported in time for 6.5, correct?

Yes. My understanding is that if we merge this PR now, it will become part of Gutenberg 17.7, so I believe it will automatically reflect this PR as of WP6.5Beta1. Or do I need to backport it first?

One last thing, I think this is the sort of thing that would be good to have some test coverage for. As a follow-up, maybe a unit test could be added? The cover block might provide an example.

I think unit tests make sense. However, even with randomization enabled, there is a small chance that it will return the markup in exactly the same order, which may make the test a bit unstable 😅

@aaronrobertshaw
Copy link
Contributor

so I believe it will automatically reflect this PR as of WP6.5Beta1

My mistake, I was forgetting this was part of the block library and should, I believe, be covered by the package updates if it makes this Gutenberg release.

Best to get this merged then! 🚢

@t-hamano t-hamano closed this Feb 7, 2024
@t-hamano t-hamano reopened this Feb 7, 2024
@t-hamano t-hamano added the props-bot Adding this label triggers the Props Bot workflow for a PR. label Feb 7, 2024
@github-actions github-actions bot removed the props-bot Adding this label triggers the Props Bot workflow for a PR. label Feb 7, 2024
@t-hamano t-hamano merged commit 68b9cc8 into trunk Feb 7, 2024
112 of 113 checks passed
@t-hamano t-hamano deleted the gallery/nested-randomize-order branch February 7, 2024 09:12
@getdave
Copy link
Contributor

getdave commented Feb 7, 2024

Thanks for being diligent folks. I can confirm this will not require a manual backport because it has merged prior to the RC for Gutenberg 17.7.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Gallery Affects the Gallery Block - used to display groups of images [Type] Bug An existing feature does not function as intended
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Gallery randomization does not work when nested within another block
3 participants