From adb9d0e1dd1d219a643427326f96a993b99b2869 Mon Sep 17 00:00:00 2001 From: Tetsuaki Hamano Date: Sun, 31 Dec 2023 14:12:29 +0900 Subject: [PATCH 1/4] Gallery Block: Add random order setting --- docs/reference-guides/core-blocks.md | 2 +- packages/block-library/src/gallery/block.json | 4 ++++ packages/block-library/src/gallery/edit.js | 13 ++++++++++++- packages/block-library/src/gallery/index.php | 18 ++++++++++++++++++ .../block-library/src/gallery/transforms.js | 3 ++- 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 9f25ad0a594b8..b091f9d143b7a 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -333,7 +333,7 @@ Display multiple images in a rich gallery. ([Source](https://github.com/WordPres - **Name:** core/gallery - **Category:** media - **Supports:** align, anchor, color (background, gradients, ~~text~~), layout (default, ~~allowEditing~~, ~~allowInheriting~~, ~~allowSwitching~~), spacing (blockGap, margin, padding), units (em, px, rem, vh, vw), ~~html~~ -- **Attributes:** allowResize, caption, columns, fixedHeight, ids, imageCrop, images, linkTarget, linkTo, shortCodeTransforms, sizeSlug +- **Attributes:** allowResize, caption, columns, fixedHeight, ids, imageCrop, images, linkTarget, linkTo, randomOrder, shortCodeTransforms, sizeSlug ## Group diff --git a/packages/block-library/src/gallery/block.json b/packages/block-library/src/gallery/block.json index fad92aed59bf7..a5425c55381f9 100644 --- a/packages/block-library/src/gallery/block.json +++ b/packages/block-library/src/gallery/block.json @@ -80,6 +80,10 @@ "type": "boolean", "default": true }, + "randomOrder": { + "type": "boolean", + "default": false + }, "fixedHeight": { "type": "boolean", "default": true diff --git a/packages/block-library/src/gallery/edit.js b/packages/block-library/src/gallery/edit.js index 371a13b1bf5ad..4a646ce836223 100644 --- a/packages/block-library/src/gallery/edit.js +++ b/packages/block-library/src/gallery/edit.js @@ -88,7 +88,8 @@ function GalleryEdit( props ) { onFocus, } = props; - const { columns, imageCrop, linkTarget, linkTo, sizeSlug } = attributes; + const { columns, imageCrop, randomOrder, linkTarget, linkTo, sizeSlug } = + attributes; const { __unstableMarkNextChangeAsNotPersistent, @@ -388,6 +389,10 @@ function GalleryEdit( props ) { : __( 'Thumbnails are not cropped.' ); } + function toggleRandomOrder() { + setAttributes( { randomOrder: ! randomOrder } ); + } + function toggleOpenInNewTab( openInNewTab ) { const newLinkTarget = openInNewTab ? '_blank' : undefined; setAttributes( { linkTarget: newLinkTarget } ); @@ -552,6 +557,12 @@ function GalleryEdit( props ) { onChange={ toggleImageCrop } help={ getImageCropHelp } /> + parseInt( id, 10 ) ); @@ -190,6 +190,7 @@ const transforms = { { columns: parseInt( columns, 10 ), linkTo, + randomOrder: orderby === 'rand', }, imageIds.map( ( imageId ) => createBlock( 'core/image', { id: imageId } ) From 33a64a3c1575d478a1e9501cfabeb8dc6f0a4930 Mon Sep 17 00:00:00 2001 From: Tetsuaki Hamano Date: Tue, 2 Jan 2024 15:16:17 +0900 Subject: [PATCH 2/4] Regenerate fixtures --- test/integration/fixtures/blocks/core__gallery-with-caption.json | 1 + test/integration/fixtures/blocks/core__gallery.json | 1 + test/integration/fixtures/blocks/core__gallery__columns.json | 1 + .../integration/fixtures/blocks/core__gallery__deprecated-7.json | 1 + 4 files changed, 4 insertions(+) diff --git a/test/integration/fixtures/blocks/core__gallery-with-caption.json b/test/integration/fixtures/blocks/core__gallery-with-caption.json index 12b516606641d..1106c8ccb64b2 100644 --- a/test/integration/fixtures/blocks/core__gallery-with-caption.json +++ b/test/integration/fixtures/blocks/core__gallery-with-caption.json @@ -8,6 +8,7 @@ "shortCodeTransforms": [], "caption": "Gallery Caption", "imageCrop": true, + "randomOrder": false, "fixedHeight": true, "linkTo": "none", "sizeSlug": "large", diff --git a/test/integration/fixtures/blocks/core__gallery.json b/test/integration/fixtures/blocks/core__gallery.json index 8b7a1000d37cc..27fc4c75dc8dc 100644 --- a/test/integration/fixtures/blocks/core__gallery.json +++ b/test/integration/fixtures/blocks/core__gallery.json @@ -8,6 +8,7 @@ "shortCodeTransforms": [], "caption": "", "imageCrop": true, + "randomOrder": false, "fixedHeight": true, "linkTo": "none", "sizeSlug": "large", diff --git a/test/integration/fixtures/blocks/core__gallery__columns.json b/test/integration/fixtures/blocks/core__gallery__columns.json index d0c40b3d3a9a9..beb97c3ac5080 100644 --- a/test/integration/fixtures/blocks/core__gallery__columns.json +++ b/test/integration/fixtures/blocks/core__gallery__columns.json @@ -9,6 +9,7 @@ "columns": 1, "caption": "", "imageCrop": true, + "randomOrder": false, "fixedHeight": true, "linkTo": "none", "sizeSlug": "large", diff --git a/test/integration/fixtures/blocks/core__gallery__deprecated-7.json b/test/integration/fixtures/blocks/core__gallery__deprecated-7.json index 4eaf85b46906d..05a630370f9b0 100644 --- a/test/integration/fixtures/blocks/core__gallery__deprecated-7.json +++ b/test/integration/fixtures/blocks/core__gallery__deprecated-7.json @@ -8,6 +8,7 @@ "shortCodeTransforms": [], "caption": "", "imageCrop": true, + "randomOrder": false, "fixedHeight": true, "linkTo": "media", "sizeSlug": "large", From 39cd25ec636be26c694126fa5c54e0c7fee22886 Mon Sep 17 00:00:00 2001 From: Tetsuaki Hamano Date: Tue, 2 Jan 2024 15:18:53 +0900 Subject: [PATCH 3/4] Update variable name --- packages/block-library/src/gallery/index.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/gallery/index.php b/packages/block-library/src/gallery/index.php index 698b689ac85ac..a06fa523ddd59 100644 --- a/packages/block-library/src/gallery/index.php +++ b/packages/block-library/src/gallery/index.php @@ -38,14 +38,14 @@ function block_core_gallery_data_id_backcompatibility( $parsed_block ) { * @param array $parsed_block The block being rendered. * @return array The block object with randomized order of image blocks. */ -function block_core_gallery_random_order( $block ) { - if ( 'core/gallery' === $block['blockName'] && isset( $block['attrs']['randomOrder'] ) && $block['attrs']['randomOrder'] ) { - $inner_blocks = $block['innerBlocks']; +function block_core_gallery_random_order( $parsed_block ) { + if ( 'core/gallery' === $parsed_block['blockName'] && isset( $parsed_block['attrs']['randomOrder'] ) && $parsed_block['attrs']['randomOrder'] ) { + $inner_blocks = $parsed_block['innerBlocks']; shuffle( $inner_blocks ); - $block['innerBlocks'] = $inner_blocks; + $parsed_block['innerBlocks'] = $inner_blocks; } - return $block; + return $parsed_block; } add_filter( 'render_block_data', 'block_core_gallery_random_order' ); From b954e6d2be7f06618feec4a4a66b63f2d0da3f7e Mon Sep 17 00:00:00 2001 From: Aki Hamano <54422211+t-hamano@users.noreply.github.com> Date: Thu, 4 Jan 2024 10:59:15 +0900 Subject: [PATCH 4/4] Update packages/block-library/src/gallery/index.php Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> --- packages/block-library/src/gallery/index.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/gallery/index.php b/packages/block-library/src/gallery/index.php index a06fa523ddd59..97877141ef333 100644 --- a/packages/block-library/src/gallery/index.php +++ b/packages/block-library/src/gallery/index.php @@ -39,13 +39,13 @@ function block_core_gallery_data_id_backcompatibility( $parsed_block ) { * @return array The block object with randomized order of image blocks. */ function block_core_gallery_random_order( $parsed_block ) { - if ( 'core/gallery' === $parsed_block['blockName'] && isset( $parsed_block['attrs']['randomOrder'] ) && $parsed_block['attrs']['randomOrder'] ) { - $inner_blocks = $parsed_block['innerBlocks']; - shuffle( $inner_blocks ); - $parsed_block['innerBlocks'] = $inner_blocks; + if ( 'core/gallery' === $parsed_block['blockName'] && ! empty( $parsed_block['attrs']['randomOrder'] ) ) { + shuffle( $parsed_block['innerBlocks'] ); } return $parsed_block; + + return $parsed_block; } add_filter( 'render_block_data', 'block_core_gallery_random_order' );