Skip to content

Commit cddc0bc

Browse files
glendaviesnzGlen Davies
andcommitted
Add fallback to old gallery edit and save for existing gallery (#28961)
Co-authored-by: Glen Davies <[email protected]>
1 parent 6b943d0 commit cddc0bc

File tree

16 files changed

+1806
-862
lines changed

16 files changed

+1806
-862
lines changed

lib/experiments-page.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ function gutenberg_initialize_experiments_settings() {
6262
'id' => 'gutenberg-widgets-in-customizer',
6363
)
6464
);
65+
add_settings_field(
66+
'gutenberg-gallery-refactor',
67+
__( 'Gallery Refactor', 'gutenberg' ),
68+
'gutenberg_display_experiment_field',
69+
'gutenberg-experiments',
70+
'gutenberg_experiments_section',
71+
array(
72+
'label' => __( 'Enable the refactored gallery block', 'gutenberg' ),
73+
'id' => 'gutenberg-gallery-refactor',
74+
)
75+
);
6576
register_setting(
6677
'gutenberg-experiments',
6778
'gutenberg-experiments'
@@ -99,3 +110,19 @@ function gutenberg_display_experiment_section() {
99110

100111
<?php
101112
}
113+
114+
/**
115+
* Extends default editor settings with experiments settings.
116+
*
117+
* @param array $settings Default editor settings.
118+
*
119+
* @return array Filtered editor settings.
120+
*/
121+
function gutenberg_experiments_editor_settings( $settings ) {
122+
$experiments_exist = get_option( 'gutenberg-experiments' );
123+
$experiments_settings = array(
124+
'__experimentalGalleryRefactor' => $experiments_exist ? array_key_exists( 'gutenberg-gallery-refactor', get_option( 'gutenberg-experiments' ) ) : false,
125+
);
126+
return array_merge( $settings, $experiments_settings );
127+
}
128+
add_filter( 'block_editor_settings', 'gutenberg_experiments_editor_settings' );

packages/block-editor/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ _Properties_
506506
- _\_\_experimentalBlockDirectory_ `boolean`: Whether the user has enabled the Block Directory
507507
- _\_\_experimentalBlockPatterns_ `Array`: Array of objects representing the block patterns
508508
- _\_\_experimentalBlockPatternCategories_ `Array`: Array of objects representing the block pattern categories
509+
- _\_\_experimentalGalleryRefactor_ `boolean`: Whether the user has enabled the refactored gallery block which uses InnerBlocks
509510

510511
<a name="SkipToSelectedBlock" href="#SkipToSelectedBlock">#</a> **SkipToSelectedBlock**
511512

packages/block-editor/src/components/media-placeholder/index.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,54 @@ export function MediaPlaceholder( {
128128

129129
const onFilesUpload = ( files ) => {
130130
if ( isGallery ) {
131-
// Because the Gallery hands the files over to Image component InnerBlocks just
131+
// Because the refactored Gallery hands the files over to Image component InnerBlocks just
132132
// hand the handling of the files over to the Gallery
133133
onSelect( files );
134134
return;
135135
}
136136
onFilesPreUpload( files );
137137
let setMedia;
138138
if ( multiple ) {
139-
setMedia = onSelect;
139+
// This is still needed to handle v1 versions of the Gallery block. It can be removed
140+
// once all Gallery instances are forced to migrate.
141+
if ( addToGallery ) {
142+
// Since the setMedia function runs multiple times per upload group
143+
// and is passed newMedia containing every item in its group each time, we must
144+
// filter out whatever this upload group had previously returned to the
145+
// gallery before adding and returning the image array with replacement newMedia
146+
// values.
147+
148+
// Define an array to store urls from newMedia between subsequent function calls.
149+
let lastMediaPassed = [];
150+
setMedia = ( newMedia ) => {
151+
// Remove any images this upload group is responsible for (lastMediaPassed).
152+
// Their replacements are contained in newMedia.
153+
const filteredMedia = ( value ?? [] ).filter( ( item ) => {
154+
// If Item has id, only remove it if lastMediaPassed has an item with that id.
155+
if ( item.id ) {
156+
return ! lastMediaPassed.some(
157+
// Be sure to convert to number for comparison.
158+
( { id } ) => Number( id ) === Number( item.id )
159+
);
160+
}
161+
// Compare transient images via .includes since gallery may append extra info onto the url.
162+
return ! lastMediaPassed.some( ( { urlSlug } ) =>
163+
item.url.includes( urlSlug )
164+
);
165+
} );
166+
// Return the filtered media array along with newMedia.
167+
onSelect( filteredMedia.concat( newMedia ) );
168+
// Reset lastMediaPassed and set it with ids and urls from newMedia.
169+
lastMediaPassed = newMedia.map( ( media ) => {
170+
// Add everything up to '.fileType' to compare via .includes.
171+
const cutOffIndex = media.url.lastIndexOf( '.' );
172+
const urlSlug = media.url.slice( 0, cutOffIndex );
173+
return { id: media.id, urlSlug };
174+
} );
175+
};
176+
} else {
177+
setMedia = onSelect;
178+
}
140179
} else {
141180
setMedia = ( [ media ] ) => onSelect( media );
142181
}

packages/block-editor/src/store/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const PREFERENCES_DEFAULTS = {
2828
* @property {boolean} __experimentalBlockDirectory Whether the user has enabled the Block Directory
2929
* @property {Array} __experimentalBlockPatterns Array of objects representing the block patterns
3030
* @property {Array} __experimentalBlockPatternCategories Array of objects representing the block pattern categories
31+
* @property {boolean} __experimentalGalleryRefactor Whether the user has enabled the refactored gallery block which uses InnerBlocks
3132
*/
3233
export const SETTINGS_DEFAULTS = {
3334
alignWide: false,
@@ -151,6 +152,7 @@ export const SETTINGS_DEFAULTS = {
151152
__experimentalBlockPatterns: [],
152153
__experimentalBlockPatternCategories: [],
153154
__experimentalSpotlightEntityBlocks: [],
155+
__experimentalGalleryRefactor: false,
154156

155157
// gradients setting is not used anymore now defaults are passed from theme.json on the server and core has its own defaults.
156158
// The setting is only kept for backward compatibility purposes.

packages/block-library/src/gallery/block.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,57 @@
33
"name": "core/gallery",
44
"category": "media",
55
"attributes": {
6+
"images": {
7+
"type": "array",
8+
"default": [],
9+
"source": "query",
10+
"selector": ".blocks-gallery-item",
11+
"query": {
12+
"url": {
13+
"type": "string",
14+
"source": "attribute",
15+
"selector": "img",
16+
"attribute": "src"
17+
},
18+
"fullUrl": {
19+
"type": "string",
20+
"source": "attribute",
21+
"selector": "img",
22+
"attribute": "data-full-url"
23+
},
24+
"link": {
25+
"type": "string",
26+
"source": "attribute",
27+
"selector": "img",
28+
"attribute": "data-link"
29+
},
30+
"alt": {
31+
"type": "string",
32+
"source": "attribute",
33+
"selector": "img",
34+
"attribute": "alt",
35+
"default": ""
36+
},
37+
"id": {
38+
"type": "string",
39+
"source": "attribute",
40+
"selector": "img",
41+
"attribute": "data-id"
42+
},
43+
"caption": {
44+
"type": "string",
45+
"source": "html",
46+
"selector": ".blocks-gallery-item__caption"
47+
}
48+
}
49+
},
50+
"ids": {
51+
"type": "array",
52+
"items": {
53+
"type": "number"
54+
},
55+
"default": []
56+
},
657
"imageUploads": {
758
"type": "array",
859
"default": [],

0 commit comments

Comments
 (0)