Skip to content

Broken Deprecation on core/block deletes templateLock when false #49159

@kraftner

Description

@kraftner

Description

The latest deprecation migration for a core/group block discards the templateLock set on that block if the value isn't one of the string values but the also valid false.

Step-by-step reproduction instructions

  1. Start a new post
  2. Switch to the code editor
  3. Paste the following:
    <!-- wp:group {"templateLock":false} -->
    <div class="wp-block-group"></div>
    <!-- /wp:group -->
    
  4. Click outside the code editor to apply the deprecation migrations and see the templateLock gets removed instantly turning this into:
    <!-- wp:group -->
    <div class="wp-block-group"></div>
    <!-- /wp:group -->
    

If you want to see a more real world flow:

  1. Create a post with a core/group block that doesn't have layout set but a templateLock value of false.
    There are two ways how to do this:
    • Create the block in an old WP version prior to the default layout, I think 6.0 should already do.
    • Manually edit the post_content in the database so it looks like this:
    <!-- wp:group {"templateLock":false} -->
    <div class="wp-block-group"></div>
    <!-- /wp:group -->
    
  2. Open the post in WP 6.1.
  3. Switch to Code Editor and see post_content now looks like this:
    <!-- wp:group -->
    <div class="wp-block-group"></div>
    <!-- /wp:group -->
    

Screenshots, screen recording, code snippet

Video demonstrating the issue:

templateLock.webm

The problem seems to be the deprecation migration to add the default layout, specifically the fact that the templateLock attribute is wrongly defined as string only:

templateLock: {
type: 'string',
},

while it can also be bool:

"templateLock": {
"type": [ "string", "boolean" ],
"enum": [ "all", "insert", "contentOnly", false ]
}

This then leads to the bool attribute being dropped since it is considered invalid but any other string value like all does indeed survive:

templateLock2.webm

This is further proven by the fact that adding this snippet does resolve the issue at hand:

wp.hooks.addFilter(
	'blocks.registerBlockType',
	'fix/template-lock',
	(settings, name) => {
		if(
			name==='core/group'
			&&
			settings.deprecated.length === 5
			&&
			settings.deprecated[0].attributes?.templateLock?.type === 'string'
		){
			settings.deprecated[0].attributes.templateLock.type = [ "string", "boolean" ];
			settings.deprecated[0].attributes.templateLock.enum =  [ "all", "insert", "contentOnly", false ];
		}
		return settings;
	}
);

Environment info

  • WordPress 6.1.1, without Gutenberg as well as with Gutenberg 15.3.1 enabled
  • Firefox
  • Desktop with Ubuntu 22.04

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

Metadata

Metadata

Assignees

Labels

[Block] GroupAffects the Group Block (and row, stack and grid variants)[Type] BugAn existing feature does not function as intended

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions