From e40f5ab827f9cc6f2f23686e3b6649f8b9200011 Mon Sep 17 00:00:00 2001 From: Carlos Garcia Date: Thu, 2 Jun 2022 18:31:09 +0200 Subject: [PATCH 1/2] Fix isGalleryV2Enabled calculation for the native version --- packages/block-library/src/gallery/shared.js | 31 ++++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/packages/block-library/src/gallery/shared.js b/packages/block-library/src/gallery/shared.js index 34cc104acbe6c..8612984021672 100644 --- a/packages/block-library/src/gallery/shared.js +++ b/packages/block-library/src/gallery/shared.js @@ -3,6 +3,11 @@ */ import { get, pick } from 'lodash'; +/** + * WordPress dependencies + */ +import { Platform } from '@wordpress/element'; + export function defaultColumnsNumber( imageCount ) { return imageCount ? Math.min( 3, imageCount ) : 3; } @@ -23,6 +28,15 @@ export const pickRelevantMediaFiles = ( image, sizeSlug = 'large' ) => { return imageProps; }; +function getGalleryBlockV2Enabled() { + // We want to fail early here, at least during beta testing phase, to ensure + // there aren't instances where undefined values cause false negatives. + if ( ! window.wp || typeof window.wp.galleryBlockV2Enabled !== 'boolean' ) { + throw 'window.wp.galleryBlockV2Enabled is not defined'; + } + return window.wp.galleryBlockV2Enabled; +} + /** * 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. The @@ -30,18 +44,17 @@ export const pickRelevantMediaFiles = ( image, sizeSlug = 'large' ) => { * can be removed when minimum supported WP version >=5.9. */ export function isGalleryV2Enabled() { + // The logic for the native version is located in a different if statement + // due to the fact that the `process.env.IS_GUTENBERG_PLUGIN` constant + // should only be used as the condition in an if statement. + if ( Platform.isNative ) { + return getGalleryBlockV2Enabled(); + } + // Only run the Gallery version compat check if the plugin is running, otherwise // assume we are in 5.9 core and enable by default. if ( process.env.IS_GUTENBERG_PLUGIN ) { - // We want to fail early here, at least during beta testing phase, to ensure - // there aren't instances where undefined values cause false negatives. - if ( - ! window.wp || - typeof window.wp.galleryBlockV2Enabled !== 'boolean' - ) { - throw 'window.wp.galleryBlockV2Enabled is not defined'; - } - return window.wp.galleryBlockV2Enabled; + return getGalleryBlockV2Enabled(); } return true; From e8c09bc6469eb68c272e4564a871525decd530a8 Mon Sep 17 00:00:00 2001 From: Carlos Garcia Date: Tue, 14 Jun 2022 15:41:41 +0200 Subject: [PATCH 2/2] Update comment in isGalleryV2Enabled function Co-authored-by: David Calhoun <438664+dcalhoun@users.noreply.github.com> --- packages/block-library/src/gallery/shared.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/gallery/shared.js b/packages/block-library/src/gallery/shared.js index 8612984021672..4765b4395994f 100644 --- a/packages/block-library/src/gallery/shared.js +++ b/packages/block-library/src/gallery/shared.js @@ -45,8 +45,8 @@ function getGalleryBlockV2Enabled() { */ export function isGalleryV2Enabled() { // The logic for the native version is located in a different if statement - // due to the fact that the `process.env.IS_GUTENBERG_PLUGIN` constant - // should only be used as the condition in an if statement. + // due to a lint rule that prohibits a single conditional combining + // `process.env.IS_GUTENBERG_PLUGIN` with a native platform check. if ( Platform.isNative ) { return getGalleryBlockV2Enabled(); }