diff --git a/packages/block-editor/src/hooks/index.js b/packages/block-editor/src/hooks/index.js index 1307af959c2c76..7572b4792812fd 100644 --- a/packages/block-editor/src/hooks/index.js +++ b/packages/block-editor/src/hooks/index.js @@ -3,7 +3,6 @@ */ import './compat'; import './align'; -import './lock'; import './anchor'; import './custom-class-name'; import './generated-class-name'; diff --git a/packages/block-editor/src/hooks/lock.js b/packages/block-editor/src/hooks/lock.js deleted file mode 100644 index 7e9b7b43c3ab99..00000000000000 --- a/packages/block-editor/src/hooks/lock.js +++ /dev/null @@ -1,34 +0,0 @@ -/** - * External dependencies - */ -import { has } from 'lodash'; - -/** - * WordPress dependencies - */ -import { addFilter } from '@wordpress/hooks'; - -/** - * Filters registered block settings, extending attributes to include `lock`. - * - * @param {Object} settings Original block settings. - * - * @return {Object} Filtered block settings. - */ -export function addAttribute( settings ) { - // allow blocks to specify their own attribute definition with default values if needed. - if ( has( settings.attributes, [ 'lock', 'type' ] ) ) { - return settings; - } - // Gracefully handle if settings.attributes is undefined. - settings.attributes = { - ...settings.attributes, - lock: { - type: 'object', - }, - }; - - return settings; -} - -addFilter( 'blocks.registerBlockType', 'core/lock/addAttribute', addAttribute ); diff --git a/packages/blocks/src/api/registration.js b/packages/blocks/src/api/registration.js index e1ff1c2a23f0e2..e6d61fa3313d43 100644 --- a/packages/blocks/src/api/registration.js +++ b/packages/blocks/src/api/registration.js @@ -12,6 +12,7 @@ import { isObject, isPlainObject, isString, + has, mapKeys, omit, pick, @@ -365,6 +366,17 @@ export function registerBlockType( blockNameOrMetadata, settings ) { return; } + // Handle `lock` as a "build-in" attribute. + // But allow blocks to specify their own with defaults. + if ( ! has( settings.attributes, [ 'lock', 'type' ] ) ) { + settings.attributes = { + ...settings.attributes, + lock: { + type: 'object', + }, + }; + } + dispatch( blocksStore ).addBlockTypes( settings ); return settings; diff --git a/packages/blocks/src/api/test/registration.js b/packages/blocks/src/api/test/registration.js index 4b3d2864a0b229..554eda8dd9c922 100644 --- a/packages/blocks/src/api/test/registration.js +++ b/packages/blocks/src/api/test/registration.js @@ -44,6 +44,11 @@ describe( 'blocks', () => { category: 'text', title: 'block title', }; + const lockAttribute = { + lock: { + type: 'object', + }, + }; beforeAll( () => { // Initialize the block store. @@ -121,7 +126,9 @@ describe( 'blocks', () => { expect( block ).toEqual( { name: 'my-plugin/fancy-block-4', icon: { src: BLOCK_ICON_DEFAULT }, - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: {}, usesContext: [], keywords: [], @@ -266,7 +273,9 @@ describe( 'blocks', () => { title: 'block title', category: 'text', icon: { src: BLOCK_ICON_DEFAULT }, - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: {}, usesContext: [], keywords: [], @@ -298,6 +307,7 @@ describe( 'blocks', () => { title: 'block title', icon: { src: BLOCK_ICON_DEFAULT }, attributes: { + ...lockAttribute, ok: { type: 'boolean', }, @@ -332,7 +342,9 @@ describe( 'blocks', () => { save: expect.any( Function ), title: 'block title', icon: { src: BLOCK_ICON_DEFAULT }, - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: {}, usesContext: [], keywords: [], @@ -363,7 +375,9 @@ describe( 'blocks', () => { save: expect.any( Function ), title: 'block title', icon: { src: BLOCK_ICON_DEFAULT }, - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: { fontSize: 'fontSize', }, @@ -400,7 +414,9 @@ describe( 'blocks', () => { title: 'block title', category: 'widgets', icon: { src: BLOCK_ICON_DEFAULT }, - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: {}, usesContext: [], keywords: [], @@ -467,7 +483,9 @@ describe( 'blocks', () => { ), }, - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: {}, usesContext: [], keywords: [], @@ -497,7 +515,9 @@ describe( 'blocks', () => { icon: { src: 'foo', }, - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: {}, usesContext: [], keywords: [], @@ -541,7 +561,9 @@ describe( 'blocks', () => { icon: { src: MyTestIcon, }, - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: {}, usesContext: [], keywords: [], @@ -599,7 +621,9 @@ describe( 'blocks', () => { ), }, - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: {}, usesContext: [], keywords: [], @@ -624,7 +648,9 @@ describe( 'blocks', () => { category: 'text', title: 'block title', icon: { src: BLOCK_ICON_DEFAULT }, - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: {}, usesContext: [], keywords: [], @@ -816,7 +842,9 @@ describe( 'blocks', () => { src: 'palmtree', }, keywords: [], - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: {}, usesContext: [], supports: {}, @@ -882,7 +910,9 @@ describe( 'blocks', () => { src: 'palmtree', }, keywords: [ 'i18n (translated)', 'metadata (translated)' ], - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: {}, usesContext: [], supports: {}, @@ -946,7 +976,9 @@ describe( 'blocks', () => { category: 'text', title: 'block title', icon: { src: BLOCK_ICON_DEFAULT }, - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: {}, usesContext: [], keywords: [], @@ -963,7 +995,9 @@ describe( 'blocks', () => { category: 'text', title: 'block title', icon: { src: BLOCK_ICON_DEFAULT }, - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: {}, usesContext: [], keywords: [], @@ -1041,7 +1075,9 @@ describe( 'blocks', () => { category: 'text', title: 'block title', icon: { src: BLOCK_ICON_DEFAULT }, - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: {}, usesContext: [], keywords: [], @@ -1065,7 +1101,9 @@ describe( 'blocks', () => { category: 'text', title: 'block title', icon: { src: BLOCK_ICON_DEFAULT }, - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: {}, usesContext: [], keywords: [], @@ -1096,7 +1134,9 @@ describe( 'blocks', () => { category: 'text', title: 'block title', icon: { src: BLOCK_ICON_DEFAULT }, - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: {}, usesContext: [], keywords: [], @@ -1111,7 +1151,9 @@ describe( 'blocks', () => { category: 'text', title: 'block title', icon: { src: BLOCK_ICON_DEFAULT }, - attributes: {}, + attributes: { + ...lockAttribute, + }, providesContext: {}, usesContext: [], keywords: [], diff --git a/packages/blocks/src/api/test/utils.js b/packages/blocks/src/api/test/utils.js index 3ffc1bf5944bc5..fd125581816354 100644 --- a/packages/blocks/src/api/test/utils.js +++ b/packages/blocks/src/api/test/utils.js @@ -359,10 +359,10 @@ describe( '__experimentalGetBlockAttributesNamesByRole', () => { 'core/test-block-3', ].forEach( unregisterBlockType ); } ); - it( 'should return empty array if block has no attributes', () => { + it( 'should return lock attribute block has no other attributes', () => { expect( __experimentalGetBlockAttributesNamesByRole( 'core/test-block-3' ) - ).toEqual( [] ); + ).toEqual( [ 'lock' ] ); } ); it( 'should return all attribute names if no role is provided', () => { expect(