Skip to content

Commit

Permalink
Gallery block: turn on auto-migration of v1 Gallery blocks to v2 form…
Browse files Browse the repository at this point in the history
…at when edited (#36191)

* Remove update gallery modal
* Add check for gallery v2 compat (block sites with use_BalanceTags option enabled and aren't on WP 5.9 or higher) to editor init so it is available when block deprecation pipeline runs
* Remove references to gallery experimental flag
* Add missing transformation updates
* Update fixture to be compat with v2 of gallery block
* Fix unit tests
* Fix issue with link destination not being set when block migrated
* Use `window.wp` global instead of store for gallery flag To resolve a race with the deprecations and asynchronous store updates for the gallery flag, we can use a property on the global wp object instead.
* Simplify deprecations, etc. by always using using v1 methods if use_balanceTags is on regardless of content format
* Move assignment of gallery global flag to native Editor. This moves the setting of the global flag higher in the view hierarchy, since the provider's constructor is still too late (parsing code invokes the deprecations before even the provider is instantiated).
* Default to `true` for gallery flag when not yet cached This makes sure the global value for the flag is established when the editor is initialized, since some users may not have the value cached locally yet. This is important to avoid incorrectly branching in the parsing step, resulting in an incompatible block format.
* Only update gallery flag if fetch result is explicitly boolean
* Update the wording of the mobile warning
* Add filter to prevent use_balanceTags being enabled on WP < 5.9
* Switch to using settings_error for use_balanceTags notice
* Add a CLI error message

Co-authored-by: Glen Davies <[email protected]>
Co-authored-by: Matthew Kevins <[email protected]>
  • Loading branch information
3 people committed Dec 1, 2021
1 parent 1b93a65 commit 8fb36de
Show file tree
Hide file tree
Showing 45 changed files with 1,669 additions and 983 deletions.
51 changes: 51 additions & 0 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,54 @@ 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
*
* @since 12.1.0
* @todo 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-dom-ready',
'wp.galleryBlockV2Enabled = ' . $v2_gallery_enabled . ';',
'after'
);
}
add_action( 'init', 'gutenberg_check_gallery_block_v2_compatibility' );

/**
* Prevent use_balanceTags being enabled on WordPress 5.8 or earlier as it breaks
* the layout of the new Gallery block.
*
* @since 12.1.0
* @todo This should be removed when the minimum required WP version is >= 5.9.
*
* @param int $new_value The new value for use_balanceTags.
*/
function gutenberg_use_balancetags_check( $new_value ) {
global $wp_version;

if ( 1 === (int) $new_value && version_compare( $wp_version, '5.9', '<' ) ) {
/* translators: %s: Minimum required version */
$message = sprintf( __( 'Gutenberg requires WordPress %s or later in order to enable the &#8220;Correct invalidly nested XHTML automatically&#8221; option. Please upgrade WordPress before enabling.', 'gutenberg' ), '5.9' );
add_settings_error( 'gutenberg_use_balancetags_check', 'gutenberg_use_balancetags_check', $message, 'error' );
if ( class_exists( 'WP_CLI' ) ) {
WP_CLI::error( $message );
}
return 0;
}

return $new_value;
}
add_filter( 'pre_update_option_use_balanceTags', 'gutenberg_use_balancetags_check' );
2 changes: 1 addition & 1 deletion lib/experiments-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
Loading

0 comments on commit 8fb36de

Please sign in to comment.