diff --git a/packages/block-library/src/gallery/shared.js b/packages/block-library/src/gallery/shared.js index 34cc104acbe6c1..4765b4395994f7 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 a lint rule that prohibits a single conditional combining + // `process.env.IS_GUTENBERG_PLUGIN` with a native platform check. + 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;