Changeset 57578 for trunk/src/wp-includes/blocks/gallery.php
- Timestamp:
- 02/09/2024 06:20:12 PM (11 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/gallery.php
r57377 r57578 34 34 35 35 /** 36 * Filter to randomize the order of image blocks. 37 * 38 * @param array $parsed_block The block being rendered. 39 * @return array The block object with randomized order of image blocks. 40 */ 41 function block_core_gallery_random_order( $parsed_block ) { 42 if ( 'core/gallery' === $parsed_block['blockName'] && ! empty( $parsed_block['attrs']['randomOrder'] ) ) { 43 shuffle( $parsed_block['innerBlocks'] ); 44 } 45 return $parsed_block; 46 } 47 48 add_filter( 'render_block_data', 'block_core_gallery_random_order' ); 49 50 /** 51 * Adds a style tag for the --wp--style--unstable-gallery-gap var. 52 * 53 * The Gallery block needs to recalculate Image block width based on 54 * the current gap setting in order to maintain the number of flex columns 55 * so a css var is added to allow this. 36 * Renders the `core/gallery` block on the server. 56 37 * 57 38 * @param array $attributes Attributes of the block being rendered. … … 60 41 */ 61 42 function block_core_gallery_render( $attributes, $content ) { 43 // Adds a style tag for the --wp--style--unstable-gallery-gap var. 44 // The Gallery block needs to recalculate Image block width based on 45 // the current gap setting in order to maintain the number of flex columns 46 // so a css var is added to allow this. 47 62 48 $gap = $attributes['style']['spacing']['blockGap'] ?? null; 63 49 // Skip if gap value contains unsupported characters. … … 131 117 ) 132 118 ); 133 return (string) $processed_content; 119 120 // The WP_HTML_Tag_Processor class calls get_updated_html() internally 121 // when the instance is treated as a string, but here we explicitly 122 // convert it to a string. 123 $updated_content = $processed_content->get_updated_html(); 124 125 /* 126 * Randomize the order of image blocks. Ideally we should shuffle 127 * the `$parsed_block['innerBlocks']` via the `render_block_data` hook. 128 * However, this hook doesn't apply inner block updates when blocks are 129 * nested. 130 * @todo: In the future, if this hook supports updating innerBlocks in 131 * nested blocks, it should be refactored. 132 * 133 * @see: https://github.com/WordPress/gutenberg/pull/58733 134 */ 135 if ( empty( $attributes['randomOrder'] ) ) { 136 return $updated_content; 137 } 138 139 // This pattern matches figure elements with the `wp-block-image` class to 140 // avoid the gallery's wrapping `figure` element and extract images only. 141 $pattern = '/<figure[^>]*\bwp-block-image\b[^>]*>.*?<\/figure>/'; 142 143 // Find all Image blocks. 144 preg_match_all( $pattern, $updated_content, $matches ); 145 if ( ! $matches ) { 146 return $updated_content; 147 } 148 $image_blocks = $matches[0]; 149 150 // Randomize the order of Image blocks. 151 shuffle( $image_blocks ); 152 $i = 0; 153 $content = preg_replace_callback( 154 $pattern, 155 static function () use ( $image_blocks, &$i ) { 156 $new_image_block = $image_blocks[ $i ]; 157 ++$i; 158 return $new_image_block; 159 }, 160 $updated_content 161 ); 162 163 return $content; 134 164 } 135 165 /**
Note: See TracChangeset
for help on using the changeset viewer.