Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix isGalleryV2Enabled calculation for the native version
  • Loading branch information
fluiddot committed Jun 3, 2022
commit e40f5ab827f9cc6f2f23686e3b6649f8b9200011
31 changes: 22 additions & 9 deletions packages/block-library/src/gallery/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -23,25 +28,33 @@ 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
* window.wp.galleryBlockV2Enabled flag is set in lib/compat.php. This method
* 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;
Expand Down