From af87cc0677e3891f4887e9ad42e9a89fce783657 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 4 Nov 2021 15:29:57 +1300 Subject: [PATCH 01/30] Update experimental flag to block sites with use_BalanceTags option enabled and aren't on WP 5.9 or higher --- lib/experiments-page.php | 2 +- .../block-library/src/gallery/deprecated.js | 1402 ++++++++++------- 2 files changed, 845 insertions(+), 559 deletions(-) diff --git a/lib/experiments-page.php b/lib/experiments-page.php index bd4ca968073f8a..75d4b529ee2256 100644 --- a/lib/experiments-page.php +++ b/lib/experiments-page.php @@ -101,7 +101,7 @@ function gutenberg_experiments_editor_settings( $settings ) { // This bypass needs to remain in place until this is resolved and a patch released. // https://core.trac.wordpress.org/ticket/54130. $experiments_settings = array( - '__unstableGalleryWithImageBlocks' => (int) get_option( 'use_balanceTags' ) !== 1, + '__unstableGalleryWithImageBlocks' => (int) get_option( 'use_balanceTags' ) !== 1 || is_wp_version_compatible( '5.9' ), ); return array_merge( $settings, $experiments_settings ); } diff --git a/packages/block-library/src/gallery/deprecated.js b/packages/block-library/src/gallery/deprecated.js index bc89ebbcc9ab16..487e259faf9642 100644 --- a/packages/block-library/src/gallery/deprecated.js +++ b/packages/block-library/src/gallery/deprecated.js @@ -7,7 +7,25 @@ import { map, some } from 'lodash'; /** * WordPress dependencies */ -import { RichText } from '@wordpress/block-editor'; +import { + RichText, + store as blockEditorStore, + useBlockProps, +} from '@wordpress/block-editor'; +import { select } from '@wordpress/data'; +import { createBlock } from '@wordpress/blocks'; + +/** + * Internal dependencies + */ +import { + LINK_DESTINATION_ATTACHMENT, + LINK_DESTINATION_MEDIA, + LINK_DESTINATION_NONE, +} from './constants'; + +const DEPRECATED_LINK_DESTINATION_MEDIA = 'file'; +const DEPRECATED_LINK_DESTINATION_ATTACHMENT = 'post'; /** * Original function to determine default number of columns from a block's @@ -22,419 +40,199 @@ export function defaultColumnsNumberV1( attributes ) { return Math.min( 3, attributes.images.length ); } -const deprecated = [ - { - attributes: { - images: { - type: 'array', - default: [], - source: 'query', - selector: '.blocks-gallery-item', - query: { - url: { - type: 'string', - source: 'attribute', - selector: 'img', - attribute: 'src', - }, - fullUrl: { - type: 'string', - source: 'attribute', - selector: 'img', - attribute: 'data-full-url', - }, - link: { - type: 'string', - source: 'attribute', - selector: 'img', - attribute: 'data-link', - }, - alt: { - type: 'string', - source: 'attribute', - selector: 'img', - attribute: 'alt', - default: '', - }, - id: { - type: 'string', - source: 'attribute', - selector: 'img', - attribute: 'data-id', - }, - caption: { - type: 'string', - source: 'html', - selector: '.blocks-gallery-item__caption', - }, - }, - }, - ids: { - type: 'array', - items: { - type: 'number', - }, - default: [], - }, - columns: { - type: 'number', - minimum: 1, - maximum: 8, - }, - caption: { - type: 'string', - source: 'html', - selector: '.blocks-gallery-caption', - }, - imageCrop: { - type: 'boolean', - default: true, - }, - linkTo: { - type: 'string', - default: 'none', - }, - sizeSlug: { - type: 'string', - default: 'large', - }, - }, - supports: { - align: true, - }, - isEligible( { linkTo } ) { - return ! linkTo || linkTo === 'attachment' || linkTo === 'media'; - }, - migrate( attributes ) { - let linkTo = attributes.linkTo; - if ( ! attributes.linkTo ) { - linkTo = 'none'; - } else if ( attributes.linkTo === 'attachment' ) { - linkTo = 'post'; - } else if ( attributes.linkTo === 'media' ) { - linkTo = 'file'; - } +/** + * Original function to determine new href and linkDestination values for an image block from the + * supplied Gallery link destination. + * + * Used in deprecations: v1-6. + * + * @param {Object} image Gallery image. + * @param {string} destination Gallery's selected link destination. + * @return {Object} New attributes to assign to image block. + */ +export function getHrefAndDestination( image, destination ) { + // Need to determine the URL that the selected destination maps to. + // Gutenberg and WordPress use different constants so the new link + // destination also needs to be tweaked. + switch ( destination ) { + case DEPRECATED_LINK_DESTINATION_MEDIA: return { - ...attributes, - linkTo, + href: image?.source_url || image?.url, // eslint-disable-line camelcase + linkDestination: LINK_DESTINATION_MEDIA, }; - }, - save( { attributes } ) { - const { - images, - columns = defaultColumnsNumberV1( attributes ), - imageCrop, - caption, - linkTo, - } = attributes; - - return ( -
- - { ! RichText.isEmpty( caption ) && ( - - ) } -
- ); + return {}; +} + +function runV2Migration( attributes ) { + let linkTo = attributes.linkTo ? attributes.linkTo : 'none'; + + if ( linkTo === 'post' ) { + linkTo = 'attachment'; + } else if ( linkTo === 'file' ) { + linkTo = 'media'; + } + + const imageBlocks = attributes.images.map( ( image ) => { + return getImageBlock( image, attributes.sizeSlug, linkTo ); + } ); + + return [ + { + caption: attributes.caption, + columns: attributes.columns, + imageCrop: attributes.imageCrop, + linkTo, + sizeSlug: attributes.sizeSlug, + allowResize: false, }, - }, - { - attributes: { - images: { - type: 'array', - default: [], - source: 'query', - selector: '.blocks-gallery-item', - query: { - url: { - source: 'attribute', - selector: 'img', - attribute: 'src', - }, - fullUrl: { - source: 'attribute', - selector: 'img', - attribute: 'data-full-url', - }, - link: { - source: 'attribute', - selector: 'img', - attribute: 'data-link', - }, - alt: { - source: 'attribute', - selector: 'img', - attribute: 'alt', - default: '', - }, - id: { - source: 'attribute', - selector: 'img', - attribute: 'data-id', - }, - caption: { - type: 'string', - source: 'html', - selector: '.blocks-gallery-item__caption', - }, + imageBlocks, + ]; +} +/** + * Gets an Image block from gallery image data + * + * Used to migrate Galleries to nested Image InnerBlocks. + * + * @param {Object} image Image properties. + * @param {string} sizeSlug Gallery sizeSlug attribute. + * @param {string} linkTo Gallery linkTo attribute. + * @return {Object} Image block. + */ +export function getImageBlock( image, sizeSlug, linkTo ) { + return createBlock( 'core/image', { + ...( image.id && { id: parseInt( image.id ) } ), + url: image.url, + alt: image.alt, + caption: image.caption, + sizeSlug, + ...getHrefAndDestination( image, linkTo ), + } ); +} + +const v6 = { + attributes: { + images: { + type: 'array', + default: [], + source: 'query', + selector: '.blocks-gallery-item', + query: { + url: { + type: 'string', + source: 'attribute', + selector: 'img', + attribute: 'src', + }, + fullUrl: { + type: 'string', + source: 'attribute', + selector: 'img', + attribute: 'data-full-url', + }, + link: { + type: 'string', + source: 'attribute', + selector: 'img', + attribute: 'data-link', + }, + alt: { + type: 'string', + source: 'attribute', + selector: 'img', + attribute: 'alt', + default: '', + }, + id: { + type: 'string', + source: 'attribute', + selector: 'img', + attribute: 'data-id', + }, + caption: { + type: 'string', + source: 'html', + selector: '.blocks-gallery-item__caption', }, }, - ids: { - type: 'array', - default: [], - }, - columns: { + }, + ids: { + type: 'array', + items: { type: 'number', }, - caption: { - type: 'string', - source: 'html', - selector: '.blocks-gallery-caption', - }, - imageCrop: { - type: 'boolean', - default: true, - }, - linkTo: { - type: 'string', - default: 'none', - }, + default: [], }, - supports: { - align: true, + columns: { + type: 'number', + minimum: 1, + maximum: 8, }, - isEligible( { ids } ) { - return ids && ids.some( ( id ) => typeof id === 'string' ); + caption: { + type: 'string', + source: 'html', + selector: '.blocks-gallery-caption', }, - migrate( attributes ) { - return { - ...attributes, - ids: map( attributes.ids, ( id ) => { - const parsedId = parseInt( id, 10 ); - return Number.isInteger( parsedId ) ? parsedId : null; - } ), - }; + imageCrop: { + type: 'boolean', + default: true, }, - save( { attributes } ) { - const { - images, - columns = defaultColumnsNumberV1( attributes ), - imageCrop, - caption, - linkTo, - } = attributes; - - return ( -
- - { ! RichText.isEmpty( caption ) && ( - - ) } -
- ); + linkTo: { + type: 'string', }, - }, - { - attributes: { - images: { - type: 'array', - default: [], - source: 'query', - selector: 'ul.wp-block-gallery .blocks-gallery-item', - query: { - url: { - source: 'attribute', - selector: 'img', - attribute: 'src', - }, - fullUrl: { - source: 'attribute', - selector: 'img', - attribute: 'data-full-url', - }, - alt: { - source: 'attribute', - selector: 'img', - attribute: 'alt', - default: '', - }, - id: { - source: 'attribute', - selector: 'img', - attribute: 'data-id', - }, - link: { - source: 'attribute', - selector: 'img', - attribute: 'data-link', - }, - caption: { - type: 'array', - source: 'children', - selector: 'figcaption', - }, - }, - }, - ids: { - type: 'array', - default: [], - }, - columns: { - type: 'number', - }, - imageCrop: { - type: 'boolean', - default: true, - }, - linkTo: { - type: 'string', - default: 'none', - }, + sizeSlug: { + type: 'string', + default: 'large', }, - supports: { - align: true, - }, - save( { attributes } ) { - const { - images, - columns = defaultColumnsNumberV1( attributes ), - imageCrop, - linkTo, - } = attributes; - return ( - - ); - }, + { ! RichText.isEmpty( caption ) && ( + + ) } + + ); }, - { - attributes: { - images: { - type: 'array', - default: [], - source: 'query', - selector: 'ul.wp-block-gallery .blocks-gallery-item', - query: { - url: { - source: 'attribute', - selector: 'img', - attribute: 'src', - }, - alt: { - source: 'attribute', - selector: 'img', - attribute: 'alt', - default: '', - }, - id: { - source: 'attribute', - selector: 'img', - attribute: 'data-id', - }, - link: { - source: 'attribute', - selector: 'img', - attribute: 'data-link', - }, - caption: { - type: 'array', - source: 'children', - selector: 'figcaption', - }, + migrate( attributes ) { + const settings = select( blockEditorStore ).getSettings(); + + if ( settings.__unstableGalleryWithImageBlocks ) { + return runV2Migration( attributes ); + } + + return attributes; + }, +}; +const v5 = { + attributes: { + images: { + type: 'array', + default: [], + source: 'query', + selector: '.blocks-gallery-item', + query: { + url: { + type: 'string', + source: 'attribute', + selector: 'img', + attribute: 'src', + }, + fullUrl: { + type: 'string', + source: 'attribute', + selector: 'img', + attribute: 'data-full-url', + }, + link: { + type: 'string', + source: 'attribute', + selector: 'img', + attribute: 'data-link', + }, + alt: { + type: 'string', + source: 'attribute', + selector: 'img', + attribute: 'alt', + default: '', + }, + id: { + type: 'string', + source: 'attribute', + selector: 'img', + attribute: 'data-id', + }, + caption: { + type: 'string', + source: 'html', + selector: '.blocks-gallery-item__caption', }, }, - columns: { + }, + ids: { + type: 'array', + items: { type: 'number', }, - imageCrop: { - type: 'boolean', - default: true, - }, - linkTo: { - type: 'string', - default: 'none', - }, + default: [], }, - isEligible( { images, ids } ) { - return ( - images && - images.length > 0 && - ( ( ! ids && images ) || - ( ids && images && ids.length !== images.length ) || - some( images, ( id, index ) => { - if ( ! id && ids[ index ] !== null ) { - return true; - } - return parseInt( id, 10 ) !== ids[ index ]; - } ) ) - ); + columns: { + type: 'number', + minimum: 1, + maximum: 8, }, - migrate( attributes ) { - return { - ...attributes, - ids: map( attributes.images, ( { id } ) => { - if ( ! id ) { - return null; - } - return parseInt( id, 10 ); - } ), - }; + caption: { + type: 'string', + source: 'html', + selector: '.blocks-gallery-caption', }, - supports: { - align: true, - }, - save( { attributes } ) { - const { - images, - columns = defaultColumnsNumberV1( attributes ), - imageCrop, - linkTo, - } = attributes; - return ( - - ); - }, + { ! RichText.isEmpty( caption ) && ( + + ) } + + ); }, - { - attributes: { - images: { - type: 'array', - default: [], - source: 'query', - selector: - 'div.wp-block-gallery figure.blocks-gallery-image img', - query: { - url: { - source: 'attribute', - attribute: 'src', - }, - alt: { - source: 'attribute', - attribute: 'alt', - default: '', - }, - id: { - source: 'attribute', - attribute: 'data-id', - }, +}; + +const v4 = { + attributes: { + images: { + type: 'array', + default: [], + source: 'query', + selector: '.blocks-gallery-item', + query: { + url: { + source: 'attribute', + selector: 'img', + attribute: 'src', + }, + fullUrl: { + source: 'attribute', + selector: 'img', + attribute: 'data-full-url', + }, + link: { + source: 'attribute', + selector: 'img', + attribute: 'data-link', + }, + alt: { + source: 'attribute', + selector: 'img', + attribute: 'alt', + default: '', + }, + id: { + source: 'attribute', + selector: 'img', + attribute: 'data-id', + }, + caption: { + type: 'string', + source: 'html', + selector: '.blocks-gallery-item__caption', }, }, - columns: { - type: 'number', - }, - imageCrop: { - type: 'boolean', - default: true, - }, - linkTo: { - type: 'string', - default: 'none', - }, - align: { - type: 'string', - default: 'none', - }, }, - supports: { - align: true, - }, - save( { attributes } ) { - const { - images, - columns = defaultColumnsNumberV1( attributes ), - align, - imageCrop, - linkTo, - } = attributes; - const className = classnames( `columns-${ columns }`, { - alignnone: align === 'none', - 'is-cropped': imageCrop, - } ); - return ( -
+ ids: { + type: 'array', + default: [], + }, + columns: { + type: 'number', + }, + caption: { + type: 'string', + source: 'html', + selector: '.blocks-gallery-caption', + }, + imageCrop: { + type: 'boolean', + default: true, + }, + linkTo: { + type: 'string', + default: 'none', + }, + }, + supports: { + align: true, + }, + isEligible( { ids } ) { + return ids && ids.some( ( id ) => typeof id === 'string' ); + }, + migrate( attributes ) { + const settings = select( blockEditorStore ).getSettings(); + + if ( settings.__unstableGalleryWithImageBlocks ) { + return runV2Migration( attributes ); + } + + return { + ...attributes, + ids: map( attributes.ids, ( id ) => { + const parsedId = parseInt( id, 10 ); + return Number.isInteger( parsedId ) ? parsedId : null; + } ), + }; + }, + save( { attributes } ) { + const { + images, + columns = defaultColumnsNumberV1( attributes ), + imageCrop, + caption, + linkTo, + } = attributes; + + return ( +
+
    { images.map( ( image ) => { let href; switch ( linkTo ) { case 'media': - href = image.url; + href = image.fullUrl || image.url; break; case 'attachment': href = image.link; @@ -691,22 +588,411 @@ const deprecated = [ src={ image.url } alt={ image.alt } data-id={ image.id } + data-full-url={ image.fullUrl } + data-link={ image.link } + className={ + image.id ? `wp-image-${ image.id }` : null + } /> ); return ( -
    - { href ? { img } : img } -
    +
    + { href ? ( + { img } + ) : ( + img + ) } + { ! RichText.isEmpty( image.caption ) && ( + + ) } +
    + ); } ) } -
- ); + + { ! RichText.isEmpty( caption ) && ( + + ) } + + ); + }, +}; +const v3 = { + attributes: { + images: { + type: 'array', + default: [], + source: 'query', + selector: 'ul.wp-block-gallery .blocks-gallery-item', + query: { + url: { + source: 'attribute', + selector: 'img', + attribute: 'src', + }, + fullUrl: { + source: 'attribute', + selector: 'img', + attribute: 'data-full-url', + }, + alt: { + source: 'attribute', + selector: 'img', + attribute: 'alt', + default: '', + }, + id: { + source: 'attribute', + selector: 'img', + attribute: 'data-id', + }, + link: { + source: 'attribute', + selector: 'img', + attribute: 'data-link', + }, + caption: { + type: 'array', + source: 'children', + selector: 'figcaption', + }, + }, + }, + ids: { + type: 'array', + default: [], + }, + columns: { + type: 'number', + }, + imageCrop: { + type: 'boolean', + default: true, + }, + linkTo: { + type: 'string', + default: 'none', + }, + }, + supports: { + align: true, + }, + save( { attributes } ) { + const { + images, + columns = defaultColumnsNumberV1( attributes ), + imageCrop, + linkTo, + } = attributes; + return ( +
    + { images.map( ( image ) => { + let href; + + switch ( linkTo ) { + case 'media': + href = image.fullUrl || image.url; + break; + case 'attachment': + href = image.link; + break; + } + + const img = ( + { + ); + + return ( +
  • +
    + { href ? { img } : img } + { image.caption && image.caption.length > 0 && ( + + ) } +
    +
  • + ); + } ) } +
+ ); + }, + migrate( attributes ) { + const settings = select( blockEditorStore ).getSettings(); + + if ( settings.__unstableGalleryWithImageBlocks ) { + return runV2Migration( attributes ); + } + }, +}; +const v2 = { + attributes: { + images: { + type: 'array', + default: [], + source: 'query', + selector: 'ul.wp-block-gallery .blocks-gallery-item', + query: { + url: { + source: 'attribute', + selector: 'img', + attribute: 'src', + }, + alt: { + source: 'attribute', + selector: 'img', + attribute: 'alt', + default: '', + }, + id: { + source: 'attribute', + selector: 'img', + attribute: 'data-id', + }, + link: { + source: 'attribute', + selector: 'img', + attribute: 'data-link', + }, + caption: { + type: 'array', + source: 'children', + selector: 'figcaption', + }, + }, + }, + columns: { + type: 'number', + }, + imageCrop: { + type: 'boolean', + default: true, + }, + linkTo: { + type: 'string', + default: 'none', + }, + }, + isEligible( { images, ids } ) { + return ( + images && + images.length > 0 && + ( ( ! ids && images ) || + ( ids && images && ids.length !== images.length ) || + some( images, ( id, index ) => { + if ( ! id && ids[ index ] !== null ) { + return true; + } + return parseInt( id, 10 ) !== ids[ index ]; + } ) ) + ); + }, + migrate( attributes ) { + const settings = select( blockEditorStore ).getSettings(); + + if ( settings.__unstableGalleryWithImageBlocks ) { + return runV2Migration( attributes ); + } + return { + ...attributes, + ids: map( attributes.images, ( { id } ) => { + if ( ! id ) { + return null; + } + return parseInt( id, 10 ); + } ), + }; + }, + supports: { + align: true, + }, + save( { attributes } ) { + const { + images, + columns = defaultColumnsNumberV1( attributes ), + imageCrop, + linkTo, + } = attributes; + return ( +
    + { images.map( ( image ) => { + let href; + + switch ( linkTo ) { + case 'media': + href = image.url; + break; + case 'attachment': + href = image.link; + break; + } + + const img = ( + { + ); + + return ( +
  • +
    + { href ? { img } : img } + { image.caption && image.caption.length > 0 && ( + + ) } +
    +
  • + ); + } ) } +
+ ); + }, +}; + +const v1 = { + attributes: { + images: { + type: 'array', + default: [], + source: 'query', + selector: 'div.wp-block-gallery figure.blocks-gallery-image img', + query: { + url: { + source: 'attribute', + attribute: 'src', + }, + alt: { + source: 'attribute', + attribute: 'alt', + default: '', + }, + id: { + source: 'attribute', + attribute: 'data-id', + }, + }, + }, + columns: { + type: 'number', }, + imageCrop: { + type: 'boolean', + default: true, + }, + linkTo: { + type: 'string', + default: 'none', + }, + align: { + type: 'string', + default: 'none', + }, + }, + supports: { + align: true, + }, + save( { attributes } ) { + const { + images, + columns = defaultColumnsNumberV1( attributes ), + align, + imageCrop, + linkTo, + } = attributes; + const className = classnames( `columns-${ columns }`, { + alignnone: align === 'none', + 'is-cropped': imageCrop, + } ); + return ( +
+ { images.map( ( image ) => { + let href; + + switch ( linkTo ) { + case 'media': + href = image.url; + break; + case 'attachment': + href = image.link; + break; + } + + const img = ( + { + ); + + return ( +
+ { href ? { img } : img } +
+ ); + } ) } +
+ ); + }, + migrate( attributes ) { + const settings = select( blockEditorStore ).getSettings(); + + if ( settings.__unstableGalleryWithImageBlocks ) { + return runV2Migration( attributes ); + } + + return attributes; }, -]; +}; -export default deprecated; +export default [ v6, v5, v4, v3, v2, v1 ]; From 09077a8e6ff22a0145889a9a844f25457693960b Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 29 Nov 2021 13:42:47 +1300 Subject: [PATCH 02/30] Remove update gallery modal --- packages/block-library/src/gallery/v1/edit.js | 29 +----- .../src/gallery/v1/update-gallery-modal.js | 97 ------------------- 2 files changed, 1 insertion(+), 125 deletions(-) delete mode 100644 packages/block-library/src/gallery/v1/update-gallery-modal.js diff --git a/packages/block-library/src/gallery/v1/edit.js b/packages/block-library/src/gallery/v1/edit.js index cfa1755445d8a0..ca57494142feb9 100644 --- a/packages/block-library/src/gallery/v1/edit.js +++ b/packages/block-library/src/gallery/v1/edit.js @@ -24,10 +24,8 @@ import { ToggleControl, withNotices, RangeControl, - ToolbarButton, } from '@wordpress/components'; import { - BlockControls, MediaPlaceholder, InspectorControls, useBlockProps, @@ -53,7 +51,6 @@ import { LINK_DESTINATION_MEDIA, LINK_DESTINATION_NONE, } from './constants'; -import UpdateGalleryModal from './update-gallery-modal'; const MAX_COLUMNS = 8; const linkOptions = [ @@ -102,13 +99,10 @@ function GalleryEdit( props ) { mediaUpload, getMedia, wasBlockJustInserted, - __unstableGalleryWithImageBlocks, } = useSelect( ( select ) => { const settings = select( blockEditorStore ).getSettings(); return { - __unstableGalleryWithImageBlocks: - settings.__unstableGalleryWithImageBlocks, imageSizes: settings.imageSizes, mediaUpload: settings.mediaUpload, getMedia: select( coreStore ).getMedia, @@ -414,10 +408,6 @@ function GalleryEdit( props ) { /> ); - const [ isUpdateOpen, setUpdateOpen ] = useState( false ); - const openUpdateModal = () => setUpdateOpen( true ); - const closeUpdateModal = () => setUpdateOpen( false ); - const blockProps = useBlockProps(); if ( ! hasImages ) { @@ -466,24 +456,7 @@ function GalleryEdit( props ) { ) } - { /* TODO: Remove platform condition when native conversion is ready */ } - { Platform.isWeb && __unstableGalleryWithImageBlocks && ( - - - { __( 'Update' ) } - - - ) } - { Platform.isWeb && isUpdateOpen && ( - - ) } + { noticeUI } () => { - let link; - const { - attributes: { sizeSlug, linkTo, images, caption }, - } = getBlock( clientId ); - - switch ( linkTo ) { - case 'post': - link = LINK_DESTINATION_ATTACHMENT; - break; - case 'file': - link = LINK_DESTINATION_MEDIA; - break; - default: - link = LINK_DESTINATION_NONE; - break; - } - const innerBlocks = images.map( ( image ) => - createBlock( 'core/image', { - id: image.id ? parseInt( image.id, 10 ) : null, - url: image.url, - alt: image.alt, - caption: image.caption, - linkDestination: link, - } ) - ); - - replaceBlocks( - clientId, - createBlock( - 'core/gallery', - { sizeSlug, linkTo: link, caption }, - innerBlocks - ) - ); -}; - -export default function UpdateGalleryModal( { onClose, clientId } ) { - const { getBlock } = useSelect( blockEditorStore ); - const { replaceBlocks } = useDispatch( blockEditorStore ); - return ( - -

- { __( - 'Updating to the new format adds the ability to use custom links or styles on individual images in the gallery, and makes it easier to add or move them around.' - ) } -

- -
- - -
-
- ); -} From 69b0899cc1697436183968c8c5eb6ce67194ce94 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 5 Nov 2021 14:01:09 +1300 Subject: [PATCH 03/30] Add check for gallery v2 compat to editor init so it is available when block deprecation pipeline runs --- lib/compat.php | 24 ++++++++++++++ .../block-library/src/gallery/deprecated.js | 32 +++++-------------- packages/block-library/src/gallery/save.js | 2 +- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/lib/compat.php b/lib/compat.php index 8d1f908161870e..80bae7f52551d2 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -191,3 +191,27 @@ function gutenberg_safe_style_attrs( $attrs ) { return $attrs; } add_filter( 'safe_style_css', 'gutenberg_safe_style_attrs' ); + +/** + * The new gallery block format is not compatible with the use_BalanceTags option + * in WP versions <= 5.8 https://core.trac.wordpress.org/ticket/54130. + * This method adds a variable to the wp namespace to indicate if the new gallery block + * format can be enabled or not. It needs to be added this early and to the wp namespace + * as it needs to be available when the intial block parsing runs on editor load, and most of + * the editor store and standard flags are not loaded yet at that point + * + * Note: This should be removed when the minimum required WP version is >= 5.9. + * + * @return void. + */ +function gutenberg_check_gallery_block_v2_compatibility() { + $use_balance_tags = (int) get_option( 'use_balanceTags' ); + $v2_gallery_enabled = boolval( 1 !== $use_balance_tags || is_wp_version_compatible( '5.9' ) ) ? 'true' : 'false'; + + wp_add_inline_script( + 'wp-blocks', + 'wp.galleryBlockV2Enabled = ' . $v2_gallery_enabled . ';', + 'after' + ); +} +add_action( 'init', 'gutenberg_check_gallery_block_v2_compatibility' ); diff --git a/packages/block-library/src/gallery/deprecated.js b/packages/block-library/src/gallery/deprecated.js index 487e259faf9642..2367546ff131a0 100644 --- a/packages/block-library/src/gallery/deprecated.js +++ b/packages/block-library/src/gallery/deprecated.js @@ -7,12 +7,8 @@ import { map, some } from 'lodash'; /** * WordPress dependencies */ -import { - RichText, - store as blockEditorStore, - useBlockProps, -} from '@wordpress/block-editor'; -import { select } from '@wordpress/data'; +import { RichText, useBlockProps } from '@wordpress/block-editor'; + import { createBlock } from '@wordpress/blocks'; /** @@ -284,9 +280,7 @@ const v6 = { ); }, migrate( attributes ) { - const settings = select( blockEditorStore ).getSettings(); - - if ( settings.__unstableGalleryWithImageBlocks ) { + if ( window.wp.galleryBlockV2Enabled ) { return runV2Migration( attributes ); } @@ -376,9 +370,7 @@ const v5 = { return ! linkTo || linkTo === 'attachment' || linkTo === 'media'; }, migrate( attributes ) { - const settings = select( blockEditorStore ).getSettings(); - - if ( settings.__unstableGalleryWithImageBlocks ) { + if ( window.wp.galleryBlockV2Enabled ) { return runV2Migration( attributes ); } @@ -541,9 +533,7 @@ const v4 = { return ids && ids.some( ( id ) => typeof id === 'string' ); }, migrate( attributes ) { - const settings = select( blockEditorStore ).getSettings(); - - if ( settings.__unstableGalleryWithImageBlocks ) { + if ( window.wp.galleryBlockV2Enabled ) { return runV2Migration( attributes ); } @@ -749,9 +739,7 @@ const v3 = { ); }, migrate( attributes ) { - const settings = select( blockEditorStore ).getSettings(); - - if ( settings.__unstableGalleryWithImageBlocks ) { + if ( window.wp.galleryBlockV2Enabled ) { return runV2Migration( attributes ); } }, @@ -819,9 +807,7 @@ const v2 = { ); }, migrate( attributes ) { - const settings = select( blockEditorStore ).getSettings(); - - if ( settings.__unstableGalleryWithImageBlocks ) { + if ( window.wp.galleryBlockV2Enabled ) { return runV2Migration( attributes ); } return { @@ -985,9 +971,7 @@ const v1 = { ); }, migrate( attributes ) { - const settings = select( blockEditorStore ).getSettings(); - - if ( settings.__unstableGalleryWithImageBlocks ) { + if ( window.wp.galleryBlockV2Enabled ) { return runV2Migration( attributes ); } diff --git a/packages/block-library/src/gallery/save.js b/packages/block-library/src/gallery/save.js index 781b96d2aedbe9..711a6abca48a31 100644 --- a/packages/block-library/src/gallery/save.js +++ b/packages/block-library/src/gallery/save.js @@ -18,7 +18,7 @@ import { import saveWithoutInnerBlocks from './v1/save'; export default function saveWithInnerBlocks( { attributes } ) { - if ( attributes?.ids?.length > 0 || attributes?.images?.length > 0 ) { + if ( ! window.wp.galleryBlockV2Enabled ) { return saveWithoutInnerBlocks( { attributes } ); } From ea46fd49a016488ccc3478cab7150a2572cadb55 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 5 Nov 2021 16:50:26 +1300 Subject: [PATCH 04/30] Remove references to gallery experimental flag --- lib/experiments-page.php | 4 +-- packages/block-editor/README.md | 1 - packages/block-editor/src/store/defaults.js | 2 -- .../block-library/src/gallery/edit-wrapper.js | 7 +--- .../block-library/src/gallery/transforms.js | 32 +++++-------------- .../provider/use-block-editor-settings.js | 1 - 6 files changed, 10 insertions(+), 37 deletions(-) diff --git a/lib/experiments-page.php b/lib/experiments-page.php index 75d4b529ee2256..de0b9f5a8e9945 100644 --- a/lib/experiments-page.php +++ b/lib/experiments-page.php @@ -100,9 +100,7 @@ function gutenberg_experiments_editor_settings( $settings ) { // The refactored gallery currently can't be run on sites with use_balanceTags option set. // This bypass needs to remain in place until this is resolved and a patch released. // https://core.trac.wordpress.org/ticket/54130. - $experiments_settings = array( - '__unstableGalleryWithImageBlocks' => (int) get_option( 'use_balanceTags' ) !== 1 || is_wp_version_compatible( '5.9' ), - ); + $experiments_settings = array(); return array_merge( $settings, $experiments_settings ); } // This can be removed when plugin support requires WordPress 5.8.0+. diff --git a/packages/block-editor/README.md b/packages/block-editor/README.md index 94a227890243b6..8b6a5453acbb3e 100644 --- a/packages/block-editor/README.md +++ b/packages/block-editor/README.md @@ -569,7 +569,6 @@ _Properties_ - _\_\_experimentalBlockDirectory_ `boolean`: Whether the user has enabled the Block Directory - _\_\_experimentalBlockPatterns_ `Array`: Array of objects representing the block patterns - _\_\_experimentalBlockPatternCategories_ `Array`: Array of objects representing the block pattern categories -- _\_\_unstableGalleryWithImageBlocks_ `boolean`: Whether the user has enabled the refactored gallery block which uses InnerBlocks ### SkipToSelectedBlock diff --git a/packages/block-editor/src/store/defaults.js b/packages/block-editor/src/store/defaults.js index 33165125325e13..49035943cb4a50 100644 --- a/packages/block-editor/src/store/defaults.js +++ b/packages/block-editor/src/store/defaults.js @@ -28,7 +28,6 @@ export const PREFERENCES_DEFAULTS = { * @property {boolean} __experimentalBlockDirectory Whether the user has enabled the Block Directory * @property {Array} __experimentalBlockPatterns Array of objects representing the block patterns * @property {Array} __experimentalBlockPatternCategories Array of objects representing the block pattern categories - * @property {boolean} __unstableGalleryWithImageBlocks Whether the user has enabled the refactored gallery block which uses InnerBlocks */ export const SETTINGS_DEFAULTS = { alignWide: false, @@ -156,7 +155,6 @@ export const SETTINGS_DEFAULTS = { __experimentalBlockPatterns: [], __experimentalBlockPatternCategories: [], __experimentalSpotlightEntityBlocks: [], - __unstableGalleryWithImageBlocks: false, // gradients setting is not used anymore now defaults are passed from theme.json on the server and core has its own defaults. // The setting is only kept for backward compatibility purposes. diff --git a/packages/block-library/src/gallery/edit-wrapper.js b/packages/block-library/src/gallery/edit-wrapper.js index 0e101dfa85e216..2a0c83e26bfc5b 100644 --- a/packages/block-library/src/gallery/edit-wrapper.js +++ b/packages/block-library/src/gallery/edit-wrapper.js @@ -25,11 +25,6 @@ export default function GalleryEditWrapper( props ) { [ clientId ] ); - const __unstableGalleryWithImageBlocks = useSelect( ( select ) => { - const settings = select( blockEditorStore ).getSettings(); - return settings.__unstableGalleryWithImageBlocks; - }, [] ); - // This logic is used to infer version information from content with higher // precedence than the flag. New galleries (and existing empty galleries) will // honor the flag. @@ -38,7 +33,7 @@ export default function GalleryEditWrapper( props ) { 0 < attributes?.ids?.length || 0 < attributes?.images?.length; if ( hasOldVersionContent || - ( ! hasNewVersionContent && ! __unstableGalleryWithImageBlocks ) + ( ! hasNewVersionContent && ! window.wp.galleryBlockV2Enabled ) ) { return ; } diff --git a/packages/block-library/src/gallery/transforms.js b/packages/block-library/src/gallery/transforms.js index f5fe6b341cd9e6..f1aa7697cd4776 100644 --- a/packages/block-library/src/gallery/transforms.js +++ b/packages/block-library/src/gallery/transforms.js @@ -49,9 +49,8 @@ const parseShortcodeIds = ( ids ) => { * @return {Block} The transformed block. */ function updateThirdPartyTransformToGallery( block ) { - const settings = select( blockEditorStore ).getSettings(); if ( - settings.__unstableGalleryWithImageBlocks && + window.wp.galleryBlockV2Enabled && block.name === 'core/gallery' && block.attributes?.images.length > 0 ) { @@ -145,8 +144,7 @@ const transforms = { const validImages = filter( attributes, ( { url } ) => url ); - const settings = select( blockEditorStore ).getSettings(); - if ( settings.__unstableGalleryWithImageBlocks ) { + if ( window.wp.galleryBlockV2Enabled ) { const innerBlocks = validImages.map( ( image ) => { return createBlock( 'core/image', image ); } ); @@ -184,10 +182,7 @@ const transforms = { images: { type: 'array', shortcode: ( { named: { ids } } ) => { - const settings = select( - blockEditorStore - ).getSettings(); - if ( ! settings.__unstableGalleryWithImageBlocks ) { + if ( ! window.wp.galleryBlockV2Enabled ) { return parseShortcodeIds( ids ).map( ( id ) => ( { id: toString( id ), } ) ); @@ -197,10 +192,7 @@ const transforms = { ids: { type: 'array', shortcode: ( { named: { ids } } ) => { - const settings = select( - blockEditorStore - ).getSettings(); - if ( ! settings.__unstableGalleryWithImageBlocks ) { + if ( ! window.wp.galleryBlockV2Enabled ) { return parseShortcodeIds( ids ); } }, @@ -208,10 +200,7 @@ const transforms = { shortCodeTransforms: { type: 'array', shortcode: ( { named: { ids } } ) => { - const settings = select( - blockEditorStore - ).getSettings(); - if ( settings.__unstableGalleryWithImageBlocks ) { + if ( window.wp.galleryBlockV2Enabled ) { return parseShortcodeIds( ids ).map( ( id ) => ( { id: parseInt( id ), } ) ); @@ -227,10 +216,7 @@ const transforms = { linkTo: { type: 'string', shortcode: ( { named: { link } } ) => { - const settings = select( - blockEditorStore - ).getSettings(); - if ( ! settings.__unstableGalleryWithImageBlocks ) { + if ( ! window.wp.galleryBlockV2Enabled ) { switch ( link ) { case 'post': return DEPRECATED_LINK_DESTINATION_ATTACHMENT; @@ -273,8 +259,7 @@ const transforms = { ); }, transform( files ) { - const settings = select( blockEditorStore ).getSettings(); - if ( settings.__unstableGalleryWithImageBlocks ) { + if ( window.wp.galleryBlockV2Enabled ) { const innerBlocks = files.map( ( file ) => createBlock( 'core/image', { url: createBlobURL( file ), @@ -299,8 +284,7 @@ const transforms = { type: 'block', blocks: [ 'core/image' ], transform: ( { align, images, ids, sizeSlug }, innerBlocks ) => { - const settings = select( blockEditorStore ).getSettings(); - if ( settings.__unstableGalleryWithImageBlocks ) { + if ( window.wp.galleryBlockV2Enabled ) { if ( innerBlocks.length > 0 ) { return innerBlocks.map( ( { diff --git a/packages/editor/src/components/provider/use-block-editor-settings.js b/packages/editor/src/components/provider/use-block-editor-settings.js index aaed50728fb376..09481b66114e6d 100644 --- a/packages/editor/src/components/provider/use-block-editor-settings.js +++ b/packages/editor/src/components/provider/use-block-editor-settings.js @@ -96,7 +96,6 @@ function useBlockEditorSettings( settings, hasTemplate ) { '__experimentalFeatures', '__experimentalPreferredStyleVariations', '__experimentalSetIsInserterOpened', - '__unstableGalleryWithImageBlocks', 'alignWide', 'allowedBlockTypes', 'bodyPlaceholder', From d1743b617533e391eab5a7dd5107f444874108a2 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 9 Nov 2021 15:23:40 +1300 Subject: [PATCH 05/30] Revert "Remove references to gallery experimental flag" This reverts commit b988d24cde235e131224123b98b8d93b1ffd8940. --- lib/experiments-page.php | 4 ++- packages/block-editor/README.md | 1 + packages/block-editor/src/store/defaults.js | 2 ++ .../block-library/src/gallery/edit-wrapper.js | 7 +++- .../block-library/src/gallery/transforms.js | 32 ++++++++++++++----- .../provider/use-block-editor-settings.js | 1 + 6 files changed, 37 insertions(+), 10 deletions(-) diff --git a/lib/experiments-page.php b/lib/experiments-page.php index de0b9f5a8e9945..75d4b529ee2256 100644 --- a/lib/experiments-page.php +++ b/lib/experiments-page.php @@ -100,7 +100,9 @@ function gutenberg_experiments_editor_settings( $settings ) { // The refactored gallery currently can't be run on sites with use_balanceTags option set. // This bypass needs to remain in place until this is resolved and a patch released. // https://core.trac.wordpress.org/ticket/54130. - $experiments_settings = array(); + $experiments_settings = array( + '__unstableGalleryWithImageBlocks' => (int) get_option( 'use_balanceTags' ) !== 1 || is_wp_version_compatible( '5.9' ), + ); return array_merge( $settings, $experiments_settings ); } // This can be removed when plugin support requires WordPress 5.8.0+. diff --git a/packages/block-editor/README.md b/packages/block-editor/README.md index 8b6a5453acbb3e..94a227890243b6 100644 --- a/packages/block-editor/README.md +++ b/packages/block-editor/README.md @@ -569,6 +569,7 @@ _Properties_ - _\_\_experimentalBlockDirectory_ `boolean`: Whether the user has enabled the Block Directory - _\_\_experimentalBlockPatterns_ `Array`: Array of objects representing the block patterns - _\_\_experimentalBlockPatternCategories_ `Array`: Array of objects representing the block pattern categories +- _\_\_unstableGalleryWithImageBlocks_ `boolean`: Whether the user has enabled the refactored gallery block which uses InnerBlocks ### SkipToSelectedBlock diff --git a/packages/block-editor/src/store/defaults.js b/packages/block-editor/src/store/defaults.js index 49035943cb4a50..33165125325e13 100644 --- a/packages/block-editor/src/store/defaults.js +++ b/packages/block-editor/src/store/defaults.js @@ -28,6 +28,7 @@ export const PREFERENCES_DEFAULTS = { * @property {boolean} __experimentalBlockDirectory Whether the user has enabled the Block Directory * @property {Array} __experimentalBlockPatterns Array of objects representing the block patterns * @property {Array} __experimentalBlockPatternCategories Array of objects representing the block pattern categories + * @property {boolean} __unstableGalleryWithImageBlocks Whether the user has enabled the refactored gallery block which uses InnerBlocks */ export const SETTINGS_DEFAULTS = { alignWide: false, @@ -155,6 +156,7 @@ export const SETTINGS_DEFAULTS = { __experimentalBlockPatterns: [], __experimentalBlockPatternCategories: [], __experimentalSpotlightEntityBlocks: [], + __unstableGalleryWithImageBlocks: false, // gradients setting is not used anymore now defaults are passed from theme.json on the server and core has its own defaults. // The setting is only kept for backward compatibility purposes. diff --git a/packages/block-library/src/gallery/edit-wrapper.js b/packages/block-library/src/gallery/edit-wrapper.js index 2a0c83e26bfc5b..0e101dfa85e216 100644 --- a/packages/block-library/src/gallery/edit-wrapper.js +++ b/packages/block-library/src/gallery/edit-wrapper.js @@ -25,6 +25,11 @@ export default function GalleryEditWrapper( props ) { [ clientId ] ); + const __unstableGalleryWithImageBlocks = useSelect( ( select ) => { + const settings = select( blockEditorStore ).getSettings(); + return settings.__unstableGalleryWithImageBlocks; + }, [] ); + // This logic is used to infer version information from content with higher // precedence than the flag. New galleries (and existing empty galleries) will // honor the flag. @@ -33,7 +38,7 @@ export default function GalleryEditWrapper( props ) { 0 < attributes?.ids?.length || 0 < attributes?.images?.length; if ( hasOldVersionContent || - ( ! hasNewVersionContent && ! window.wp.galleryBlockV2Enabled ) + ( ! hasNewVersionContent && ! __unstableGalleryWithImageBlocks ) ) { return ; } diff --git a/packages/block-library/src/gallery/transforms.js b/packages/block-library/src/gallery/transforms.js index f1aa7697cd4776..f5fe6b341cd9e6 100644 --- a/packages/block-library/src/gallery/transforms.js +++ b/packages/block-library/src/gallery/transforms.js @@ -49,8 +49,9 @@ const parseShortcodeIds = ( ids ) => { * @return {Block} The transformed block. */ function updateThirdPartyTransformToGallery( block ) { + const settings = select( blockEditorStore ).getSettings(); if ( - window.wp.galleryBlockV2Enabled && + settings.__unstableGalleryWithImageBlocks && block.name === 'core/gallery' && block.attributes?.images.length > 0 ) { @@ -144,7 +145,8 @@ const transforms = { const validImages = filter( attributes, ( { url } ) => url ); - if ( window.wp.galleryBlockV2Enabled ) { + const settings = select( blockEditorStore ).getSettings(); + if ( settings.__unstableGalleryWithImageBlocks ) { const innerBlocks = validImages.map( ( image ) => { return createBlock( 'core/image', image ); } ); @@ -182,7 +184,10 @@ const transforms = { images: { type: 'array', shortcode: ( { named: { ids } } ) => { - if ( ! window.wp.galleryBlockV2Enabled ) { + const settings = select( + blockEditorStore + ).getSettings(); + if ( ! settings.__unstableGalleryWithImageBlocks ) { return parseShortcodeIds( ids ).map( ( id ) => ( { id: toString( id ), } ) ); @@ -192,7 +197,10 @@ const transforms = { ids: { type: 'array', shortcode: ( { named: { ids } } ) => { - if ( ! window.wp.galleryBlockV2Enabled ) { + const settings = select( + blockEditorStore + ).getSettings(); + if ( ! settings.__unstableGalleryWithImageBlocks ) { return parseShortcodeIds( ids ); } }, @@ -200,7 +208,10 @@ const transforms = { shortCodeTransforms: { type: 'array', shortcode: ( { named: { ids } } ) => { - if ( window.wp.galleryBlockV2Enabled ) { + const settings = select( + blockEditorStore + ).getSettings(); + if ( settings.__unstableGalleryWithImageBlocks ) { return parseShortcodeIds( ids ).map( ( id ) => ( { id: parseInt( id ), } ) ); @@ -216,7 +227,10 @@ const transforms = { linkTo: { type: 'string', shortcode: ( { named: { link } } ) => { - if ( ! window.wp.galleryBlockV2Enabled ) { + const settings = select( + blockEditorStore + ).getSettings(); + if ( ! settings.__unstableGalleryWithImageBlocks ) { switch ( link ) { case 'post': return DEPRECATED_LINK_DESTINATION_ATTACHMENT; @@ -259,7 +273,8 @@ const transforms = { ); }, transform( files ) { - if ( window.wp.galleryBlockV2Enabled ) { + const settings = select( blockEditorStore ).getSettings(); + if ( settings.__unstableGalleryWithImageBlocks ) { const innerBlocks = files.map( ( file ) => createBlock( 'core/image', { url: createBlobURL( file ), @@ -284,7 +299,8 @@ const transforms = { type: 'block', blocks: [ 'core/image' ], transform: ( { align, images, ids, sizeSlug }, innerBlocks ) => { - if ( window.wp.galleryBlockV2Enabled ) { + const settings = select( blockEditorStore ).getSettings(); + if ( settings.__unstableGalleryWithImageBlocks ) { if ( innerBlocks.length > 0 ) { return innerBlocks.map( ( { diff --git a/packages/editor/src/components/provider/use-block-editor-settings.js b/packages/editor/src/components/provider/use-block-editor-settings.js index 09481b66114e6d..aaed50728fb376 100644 --- a/packages/editor/src/components/provider/use-block-editor-settings.js +++ b/packages/editor/src/components/provider/use-block-editor-settings.js @@ -96,6 +96,7 @@ function useBlockEditorSettings( settings, hasTemplate ) { '__experimentalFeatures', '__experimentalPreferredStyleVariations', '__experimentalSetIsInserterOpened', + '__unstableGalleryWithImageBlocks', 'alignWide', 'allowedBlockTypes', 'bodyPlaceholder', From cba6433b14ee57019aaa7cc8f7654a9c9ab3079e Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 11 Nov 2021 11:28:34 +1300 Subject: [PATCH 06/30] Abstract out the check for v2 gallery support and use the existing experimental flag for mobile --- .../block-library/src/gallery/deprecated.js | 13 +++++---- .../block-library/src/gallery/edit-wrapper.js | 8 ++---- packages/block-library/src/gallery/save.js | 3 +- packages/block-library/src/gallery/shared.js | 20 +++++++++++++ .../block-library/src/gallery/transforms.js | 28 +++++-------------- 5 files changed, 38 insertions(+), 34 deletions(-) diff --git a/packages/block-library/src/gallery/deprecated.js b/packages/block-library/src/gallery/deprecated.js index 2367546ff131a0..fde1ae5960e0c7 100644 --- a/packages/block-library/src/gallery/deprecated.js +++ b/packages/block-library/src/gallery/deprecated.js @@ -19,6 +19,7 @@ import { LINK_DESTINATION_MEDIA, LINK_DESTINATION_NONE, } from './constants'; +import { isGalleryV2Enabled } from './shared'; const DEPRECATED_LINK_DESTINATION_MEDIA = 'file'; const DEPRECATED_LINK_DESTINATION_ATTACHMENT = 'post'; @@ -280,7 +281,7 @@ const v6 = { ); }, migrate( attributes ) { - if ( window.wp.galleryBlockV2Enabled ) { + if ( isGalleryV2Enabled() ) { return runV2Migration( attributes ); } @@ -370,7 +371,7 @@ const v5 = { return ! linkTo || linkTo === 'attachment' || linkTo === 'media'; }, migrate( attributes ) { - if ( window.wp.galleryBlockV2Enabled ) { + if ( isGalleryV2Enabled() ) { return runV2Migration( attributes ); } @@ -533,7 +534,7 @@ const v4 = { return ids && ids.some( ( id ) => typeof id === 'string' ); }, migrate( attributes ) { - if ( window.wp.galleryBlockV2Enabled ) { + if ( isGalleryV2Enabled() ) { return runV2Migration( attributes ); } @@ -739,7 +740,7 @@ const v3 = { ); }, migrate( attributes ) { - if ( window.wp.galleryBlockV2Enabled ) { + if ( isGalleryV2Enabled() ) { return runV2Migration( attributes ); } }, @@ -807,7 +808,7 @@ const v2 = { ); }, migrate( attributes ) { - if ( window.wp.galleryBlockV2Enabled ) { + if ( isGalleryV2Enabled() ) { return runV2Migration( attributes ); } return { @@ -971,7 +972,7 @@ const v1 = { ); }, migrate( attributes ) { - if ( window.wp.galleryBlockV2Enabled ) { + if ( isGalleryV2Enabled() ) { return runV2Migration( attributes ); } diff --git a/packages/block-library/src/gallery/edit-wrapper.js b/packages/block-library/src/gallery/edit-wrapper.js index 0e101dfa85e216..2f3638f7fe58c4 100644 --- a/packages/block-library/src/gallery/edit-wrapper.js +++ b/packages/block-library/src/gallery/edit-wrapper.js @@ -9,6 +9,7 @@ import { useSelect } from '@wordpress/data'; */ import EditWithInnerBlocks from './edit'; import EditWithoutInnerBlocks from './v1/edit'; +import { isGalleryV2Enabled } from './shared'; /* * Using a wrapper around the logic to load the edit for v1 of Gallery block @@ -25,11 +26,6 @@ export default function GalleryEditWrapper( props ) { [ clientId ] ); - const __unstableGalleryWithImageBlocks = useSelect( ( select ) => { - const settings = select( blockEditorStore ).getSettings(); - return settings.__unstableGalleryWithImageBlocks; - }, [] ); - // This logic is used to infer version information from content with higher // precedence than the flag. New galleries (and existing empty galleries) will // honor the flag. @@ -38,7 +34,7 @@ export default function GalleryEditWrapper( props ) { 0 < attributes?.ids?.length || 0 < attributes?.images?.length; if ( hasOldVersionContent || - ( ! hasNewVersionContent && ! __unstableGalleryWithImageBlocks ) + ( ! hasNewVersionContent && ! isGalleryV2Enabled() ) ) { return ; } diff --git a/packages/block-library/src/gallery/save.js b/packages/block-library/src/gallery/save.js index 711a6abca48a31..655a1823020fc5 100644 --- a/packages/block-library/src/gallery/save.js +++ b/packages/block-library/src/gallery/save.js @@ -16,9 +16,10 @@ import { * Internal dependencies */ import saveWithoutInnerBlocks from './v1/save'; +import { isGalleryV2Enabled } from './shared'; export default function saveWithInnerBlocks( { attributes } ) { - if ( ! window.wp.galleryBlockV2Enabled ) { + if ( ! isGalleryV2Enabled() ) { return saveWithoutInnerBlocks( { attributes } ); } diff --git a/packages/block-library/src/gallery/shared.js b/packages/block-library/src/gallery/shared.js index 0de4e8586c33ab..f9fdd7748ace73 100644 --- a/packages/block-library/src/gallery/shared.js +++ b/packages/block-library/src/gallery/shared.js @@ -3,6 +3,13 @@ */ import { get, pick } from 'lodash'; +/** + * WordPress dependencies + */ +import { Platform } from '@wordpress/element'; +import { select } from '@wordpress/data'; +import { store as blockEditorStore } from '@wordpress/block-editor'; + export function defaultColumnsNumber( imageCount ) { return imageCount ? Math.min( 3, imageCount ) : 3; } @@ -21,3 +28,16 @@ export const pickRelevantMediaFiles = ( image, sizeSlug = 'large' ) => { } return imageProps; }; + +export function isGalleryV2Enabled() { + if ( Platform.isWeb && window.wp.galleryBlockV2Enabled ) { + return true; + } + + const settings = select( blockEditorStore ).getSettings(); + if ( settings.__unstableGalleryWithImageBlocks ) { + return true; + } + + return false; +} diff --git a/packages/block-library/src/gallery/transforms.js b/packages/block-library/src/gallery/transforms.js index f5fe6b341cd9e6..5843b452b00179 100644 --- a/packages/block-library/src/gallery/transforms.js +++ b/packages/block-library/src/gallery/transforms.js @@ -24,7 +24,7 @@ import { LINK_DESTINATION_ATTACHMENT as DEPRECATED_LINK_DESTINATION_ATTACHMENT, LINK_DESTINATION_MEDIA as DEPRECATED_LINK_DESTINATION_MEDIA, } from './v1/constants'; -import { pickRelevantMediaFiles } from './shared'; +import { pickRelevantMediaFiles, isGalleryV2Enabled } from './shared'; const parseShortcodeIds = ( ids ) => { if ( ! ids ) { @@ -49,9 +49,8 @@ const parseShortcodeIds = ( ids ) => { * @return {Block} The transformed block. */ function updateThirdPartyTransformToGallery( block ) { - const settings = select( blockEditorStore ).getSettings(); if ( - settings.__unstableGalleryWithImageBlocks && + isGalleryV2Enabled() && block.name === 'core/gallery' && block.attributes?.images.length > 0 ) { @@ -145,8 +144,7 @@ const transforms = { const validImages = filter( attributes, ( { url } ) => url ); - const settings = select( blockEditorStore ).getSettings(); - if ( settings.__unstableGalleryWithImageBlocks ) { + if ( isGalleryV2Enabled() ) { const innerBlocks = validImages.map( ( image ) => { return createBlock( 'core/image', image ); } ); @@ -184,10 +182,7 @@ const transforms = { images: { type: 'array', shortcode: ( { named: { ids } } ) => { - const settings = select( - blockEditorStore - ).getSettings(); - if ( ! settings.__unstableGalleryWithImageBlocks ) { + if ( ! isGalleryV2Enabled() ) { return parseShortcodeIds( ids ).map( ( id ) => ( { id: toString( id ), } ) ); @@ -197,10 +192,7 @@ const transforms = { ids: { type: 'array', shortcode: ( { named: { ids } } ) => { - const settings = select( - blockEditorStore - ).getSettings(); - if ( ! settings.__unstableGalleryWithImageBlocks ) { + if ( ! isGalleryV2Enabled() ) { return parseShortcodeIds( ids ); } }, @@ -208,10 +200,7 @@ const transforms = { shortCodeTransforms: { type: 'array', shortcode: ( { named: { ids } } ) => { - const settings = select( - blockEditorStore - ).getSettings(); - if ( settings.__unstableGalleryWithImageBlocks ) { + if ( isGalleryV2Enabled() ) { return parseShortcodeIds( ids ).map( ( id ) => ( { id: parseInt( id ), } ) ); @@ -227,10 +216,7 @@ const transforms = { linkTo: { type: 'string', shortcode: ( { named: { link } } ) => { - const settings = select( - blockEditorStore - ).getSettings(); - if ( ! settings.__unstableGalleryWithImageBlocks ) { + if ( ! isGalleryV2Enabled() ) { switch ( link ) { case 'post': return DEPRECATED_LINK_DESTINATION_ATTACHMENT; From 6e6e643c26a87b73251c25c492d0f6a449e68892 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 11 Nov 2021 11:51:20 +1300 Subject: [PATCH 07/30] Add missing transformation updates --- packages/block-library/src/gallery/transforms.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/gallery/transforms.js b/packages/block-library/src/gallery/transforms.js index 5843b452b00179..36106b4bb0664a 100644 --- a/packages/block-library/src/gallery/transforms.js +++ b/packages/block-library/src/gallery/transforms.js @@ -8,8 +8,6 @@ import { filter, every, toString } from 'lodash'; */ import { createBlock } from '@wordpress/blocks'; import { createBlobURL } from '@wordpress/blob'; -import { select } from '@wordpress/data'; -import { store as blockEditorStore } from '@wordpress/block-editor'; import { addFilter } from '@wordpress/hooks'; /** @@ -259,8 +257,7 @@ const transforms = { ); }, transform( files ) { - const settings = select( blockEditorStore ).getSettings(); - if ( settings.__unstableGalleryWithImageBlocks ) { + if ( isGalleryV2Enabled() ) { const innerBlocks = files.map( ( file ) => createBlock( 'core/image', { url: createBlobURL( file ), @@ -285,8 +282,7 @@ const transforms = { type: 'block', blocks: [ 'core/image' ], transform: ( { align, images, ids, sizeSlug }, innerBlocks ) => { - const settings = select( blockEditorStore ).getSettings(); - if ( settings.__unstableGalleryWithImageBlocks ) { + if ( isGalleryV2Enabled() ) { if ( innerBlocks.length > 0 ) { return innerBlocks.map( ( { From 5e8db7601702b99a67b0199afe000f40850d67cd Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 11 Nov 2021 12:33:59 +1300 Subject: [PATCH 08/30] Update fixture to be compat with v2 of gallery block --- packages/block-library/src/gallery/shared.js | 4 ++ .../blocks/core__gallery-with-caption.html | 39 +++++++---- .../blocks/core__gallery-with-caption.json | 58 ++++++++++----- .../core__gallery-with-caption.parsed.json | 49 +++++++++++-- ...core__gallery-with-caption.serialized.html | 10 ++- .../fixtures/blocks/core__gallery.html | 39 +++++++---- .../fixtures/blocks/core__gallery.json | 56 ++++++++++----- .../fixtures/blocks/core__gallery.parsed.json | 40 +++++++++-- .../blocks/core__gallery.serialized.html | 10 ++- .../blocks/core__gallery__columns.html | 37 ++++++---- .../blocks/core__gallery__columns.json | 55 ++++++++++----- .../blocks/core__gallery__columns.parsed.json | 51 ++++++++++++-- .../core__gallery__columns.serialized.html | 10 ++- .../blocks/core__gallery__deprecated-1.json | 37 ++++++---- ...ore__gallery__deprecated-1.serialized.html | 10 ++- .../blocks/core__gallery__deprecated-2.json | 44 +++++++----- ...ore__gallery__deprecated-2.serialized.html | 10 ++- .../blocks/core__gallery__deprecated-3.json | 38 ++++++---- ...ore__gallery__deprecated-3.serialized.html | 10 ++- .../blocks/core__gallery__deprecated-4.json | 57 ++++++++++----- ...ore__gallery__deprecated-4.serialized.html | 14 +++- .../blocks/core__gallery__deprecated-5.json | 70 ++++++++++++------- ...ore__gallery__deprecated-5.serialized.html | 14 +++- .../blocks/core__gallery__deprecated-6.html | 54 ++++++++++++++ .../blocks/core__gallery__deprecated-6.json | 62 ++++++++++++++++ .../core__gallery__deprecated-6.parsed.json | 14 ++++ ...ore__gallery__deprecated-6.serialized.html | 13 ++++ 27 files changed, 697 insertions(+), 208 deletions(-) create mode 100644 test/integration/fixtures/blocks/core__gallery__deprecated-6.html create mode 100644 test/integration/fixtures/blocks/core__gallery__deprecated-6.json create mode 100644 test/integration/fixtures/blocks/core__gallery__deprecated-6.parsed.json create mode 100644 test/integration/fixtures/blocks/core__gallery__deprecated-6.serialized.html diff --git a/packages/block-library/src/gallery/shared.js b/packages/block-library/src/gallery/shared.js index f9fdd7748ace73..a6a767e0c9407a 100644 --- a/packages/block-library/src/gallery/shared.js +++ b/packages/block-library/src/gallery/shared.js @@ -30,6 +30,10 @@ export const pickRelevantMediaFiles = ( image, sizeSlug = 'large' ) => { }; export function isGalleryV2Enabled() { + if ( typeof process !== 'undefined' && process.env?.NODE_ENV === 'test' ) { + return true; + } + if ( Platform.isWeb && window.wp.galleryBlockV2Enabled ) { return true; } diff --git a/test/integration/fixtures/blocks/core__gallery-with-caption.html b/test/integration/fixtures/blocks/core__gallery-with-caption.html index ca644b223d7254..0ec3003092d8b9 100644 --- a/test/integration/fixtures/blocks/core__gallery-with-caption.html +++ b/test/integration/fixtures/blocks/core__gallery-with-caption.html @@ -1,17 +1,26 @@ - -" } ] diff --git a/test/integration/fixtures/blocks/core__gallery-with-caption.parsed.json b/test/integration/fixtures/blocks/core__gallery-with-caption.parsed.json index 8060435a4fdc6f..bb43e7abcd7f82 100644 --- a/test/integration/fixtures/blocks/core__gallery-with-caption.parsed.json +++ b/test/integration/fixtures/blocks/core__gallery-with-caption.parsed.json @@ -2,12 +2,53 @@ { "blockName": "core/gallery", "attrs": { - "ids": [ null, null ] + "linkTo": "none" }, - "innerBlocks": [], - "innerHTML": "\n\n", + "innerBlocks": [ + { + "blockName": "core/image", + "attrs": { + "id": 1421, + "sizeSlug": "large", + "linkDestination": "none", + "inheritedAttributes": { + "linkDestination": true, + "linkTarget": true, + "sizeSlug": true + } + }, + "innerBlocks": [], + "innerHTML": "\n\t
\n\t\t\n\t
\n\t", + "innerContent": [ + "\n\t
\n\t\t\n\t
\n\t" + ] + }, + { + "blockName": "core/image", + "attrs": { + "id": 1440, + "sizeSlug": "large", + "linkDestination": "none", + "inheritedAttributes": { + "linkDestination": true, + "linkTarget": true, + "sizeSlug": true + } + }, + "innerBlocks": [], + "innerHTML": "\n\t
\n\t\t\n\t
\n\t", + "innerContent": [ + "\n\t
\n\t\t\n\t
\n\t" + ] + } + ], + "innerHTML": "\n\n\t\n\n\t\n\t\n\n", "innerContent": [ - "\n\n" + "\n\n\t", + null, + "\n\n\t", + null, + "\n\t\n\n" ] } ] diff --git a/test/integration/fixtures/blocks/core__gallery-with-caption.serialized.html b/test/integration/fixtures/blocks/core__gallery-with-caption.serialized.html index 39a9da57a48fa8..f67c8682af23ea 100644 --- a/test/integration/fixtures/blocks/core__gallery-with-caption.serialized.html +++ b/test/integration/fixtures/blocks/core__gallery-with-caption.serialized.html @@ -1,3 +1,9 @@ - - + + diff --git a/test/integration/fixtures/blocks/core__gallery.html b/test/integration/fixtures/blocks/core__gallery.html index a0f62e68fc5273..3db587c8d72d58 100644 --- a/test/integration/fixtures/blocks/core__gallery.html +++ b/test/integration/fixtures/blocks/core__gallery.html @@ -1,16 +1,25 @@ - -" } ] diff --git a/test/integration/fixtures/blocks/core__gallery.parsed.json b/test/integration/fixtures/blocks/core__gallery.parsed.json index c75387eef15d54..6ab1b02f4ba332 100644 --- a/test/integration/fixtures/blocks/core__gallery.parsed.json +++ b/test/integration/fixtures/blocks/core__gallery.parsed.json @@ -2,12 +2,44 @@ { "blockName": "core/gallery", "attrs": { - "ids": [ null, null ] + "linkTo": "none", + "className": "columns-2" }, - "innerBlocks": [], - "innerHTML": "\n\n", + "innerBlocks": [ + { + "blockName": "core/image", + "attrs": { + "id": 1421, + "sizeSlug": "large", + "linkDestination": "none" + }, + "innerBlocks": [], + "innerHTML": "\n\t
\n\t\t\n\t
\n\t", + "innerContent": [ + "\n\t
\n\t\t\n\t
\n\t" + ] + }, + { + "blockName": "core/image", + "attrs": { + "id": 1440, + "sizeSlug": "large", + "linkDestination": "none" + }, + "innerBlocks": [], + "innerHTML": "\n\t
\n\t\t\n\t
\n\t", + "innerContent": [ + "\n\t
\n\t\t\n\t
\n\t" + ] + } + ], + "innerHTML": "\n\n\t\n\n\t\n\n", "innerContent": [ - "\n\n" + "\n\n\t", + null, + "\n\n\t", + null, + "\n\n" ] } ] diff --git a/test/integration/fixtures/blocks/core__gallery.serialized.html b/test/integration/fixtures/blocks/core__gallery.serialized.html index 62c72702eb57f6..6ab87b7c6031d8 100644 --- a/test/integration/fixtures/blocks/core__gallery.serialized.html +++ b/test/integration/fixtures/blocks/core__gallery.serialized.html @@ -1,3 +1,9 @@ - - + + diff --git a/test/integration/fixtures/blocks/core__gallery__columns.html b/test/integration/fixtures/blocks/core__gallery__columns.html index 6ab9083784937c..b70ac2b6f06a85 100644 --- a/test/integration/fixtures/blocks/core__gallery__columns.html +++ b/test/integration/fixtures/blocks/core__gallery__columns.html @@ -1,16 +1,23 @@ - -