Skip to content

Block Locking: prevent override of parent's Template Lock#42825

Open
dougwollison wants to merge 1 commit intoWordPress:trunkfrom
dougwollison:fix/block-lock-check-parent-lock
Open

Block Locking: prevent override of parent's Template Lock#42825
dougwollison wants to merge 1 commit intoWordPress:trunkfrom
dougwollison:fix/block-lock-check-parent-lock

Conversation

@dougwollison
Copy link
Contributor

What?

Prevent users from overriding the locks on a block imposed by the parent via templateLock.

Why?

Currently, if a post type or parent block uses templateLock, users can override those movement/removal restrictions on individual inner blocks. I feel this is counter-intuitive; if I intend a block's content to be fixed, the user should not be able to override it.

This can actually render the block or entire block editor unusable. For example, say I have a post type with a template set to a single main content block, and set the template_lock to "insert"; the intention is that the main block cannot be removed and nothing can be added above/below it. Currently, the user would be able to override that lock, delete the block, and be left with now way to insert content, because the insert prevention is still in place.

The only current workarounds are to either disable lock support on the applicable blocks, or set the canLockBlocks setting to false. Neither of which are ideal as there may be instances within that same page where locking is permitted on the blocks involved.

How?

While not comprehensive (as lock overrides can still be set via other means), this PR adds a check to the useBlockLock hook to check if there is a parent lock in place when calculating the canLock flag, rather than solely on the lock-ability of the block's type. I can see some edge cases where you'd want to programatically allow removing blocks from an otherwise fixed template.

Testing Instructions

  1. Register a post type with a template (e.g. a single paragraph) and template_lock set to "insert" or "all"
  2. Create a new post of that type, and select the block
  3. The Unlock menu/toolbar item should not be present, nor should Remove Block from the block menu
  4. Use the code editor to manualy add a lock.remove override ({"lock":{"remove":false}})
  5. The Remove Block option should now be available

When testing if a block can be locked, check the parent's template lock; if the parent has template locking in place, the user should not be able to override that.
@dougwollison dougwollison requested a review from ellatrix as a code owner July 29, 2022 20:05
@Mamaduka Mamaduka self-requested a review July 30, 2022 14:39
@Mamaduka Mamaduka added the [Feature] Block Locking The API allowing for the ability to lock/unlock blocks label Aug 1, 2022
Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing, @dougwollison, and sorry for the late reply.

The current UI matches the API design introduced in WordPress 5.9. If we apply the proposed change interface will be inconsistent with the API.

I recommend curating the Block locking experience with existing tools.

@dougwollison
Copy link
Contributor Author

Okay but how do we address the issue I described with post-type level templateLock virtually bricking the editor? If I have a post type with a fixed content template, but the user overrides the lock on each block and removes them, they can't add anything to the editor.

@Mamaduka
Copy link
Member

@dougwollison, you can also disable the block lock controls for the post type with template_lock and users with lower capabilities.

Example:

add_filter( 'block_editor_settings_all', function( $settings, $context ) {
	if ( ! $context->post ) {
		return $settings;
	}

	$post_type_object = get_post_type_object( $context->post->post_type );
	if ( $post_type_object->template_lock ) {
		$settings['canLockBlocks'] = current_user_can( 'edit_theme_options' );
	}

	return $settings;
}, 10, 2 );

@dougwollison
Copy link
Contributor Author

@Mamaduka yes but I have use cases where we want to allow block locking on stuff within child blocks of the template.

@Mamaduka
Copy link
Member

@dougwollison, I agree there is definitely room for improvement for more granular controls.

Maybe instead of using templateLock we can introduce a new block-level setting. That can override global settings for block locking controls.

@mtias, @jorgefilipecosta, what do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Block Locking The API allowing for the ability to lock/unlock blocks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants