diff --git a/packages/blocks/CHANGELOG.md b/packages/blocks/CHANGELOG.md index b8d3fed3e8b731..4036cf8ac78f8e 100644 --- a/packages/blocks/CHANGELOG.md +++ b/packages/blocks/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Deprecations + +- Deprecate non-string descriptions ([#44455](https://github.com/WordPress/gutenberg/pull/44455)). + ## 11.17.0 (2022-09-21) - The block attribute sources `children` and `node` have been deprecated. Please use the `html` source instead. See https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/introducing-attributes-and-editable-fields/ and the core blocks for examples. diff --git a/packages/blocks/src/api/test/registration.js b/packages/blocks/src/api/test/registration.js index 490b547e7ef593..58bf57726a3b9b 100644 --- a/packages/blocks/src/api/test/registration.js +++ b/packages/blocks/src/api/test/registration.js @@ -4,6 +4,7 @@ * WordPress dependencies */ import { addFilter, removeAllFilters, removeFilter } from '@wordpress/hooks'; +import { logged } from '@wordpress/deprecated'; import { select } from '@wordpress/data'; /** @@ -61,6 +62,11 @@ describe( 'blocks', () => { setUnregisteredTypeHandlerName( undefined ); setDefaultBlockName( undefined ); unstable__bootstrapServerSideBlockDefinitions( {} ); + + // Reset deprecation logging to ensure we properly track warnings. + for ( const key in logged ) { + delete logged[ key ]; + } } ); describe( 'registerBlockType()', () => { @@ -832,6 +838,41 @@ describe( 'blocks', () => { // Only attributes of block1 are supposed to be edited by the filter thus it must differ from block2. expect( block1.attributes ).not.toEqual( block2.attributes ); } ); + + it( 'should allow non-string descriptions at registration but warn for undesired usage.', () => { + const newDescription =
foo bar
; + + const block = registerBlockType( 'my-plugin/test-block-1', { + ...defaultBlockSettings, + description: newDescription, + } ); + + expect( block.description ).toBe( newDescription ); + expect( console ).toHaveWarnedWith( + 'Declaring non-string block descriptions is deprecated since version 6.2.' + ); + } ); + + it( 'should allow non-string descriptions through `blocks.registerBlockType` filter but warn for undesired usage.', () => { + const newDescription =foo bar
; + addFilter( + 'blocks.registerBlockType', + 'core/blocks/non-string-description', + ( settings ) => { + settings.description = newDescription; + return settings; + } + ); + const block = registerBlockType( + 'my-plugin/test-block-2', + defaultBlockSettings + ); + + expect( block.description ).toBe( newDescription ); + expect( console ).toHaveWarnedWith( + 'Declaring non-string block descriptions is deprecated since version 6.2.' + ); + } ); } ); test( 'registers block from metadata', () => { diff --git a/packages/blocks/src/store/actions.js b/packages/blocks/src/store/actions.js index 7d178969615cfd..1e160175dc75ee 100644 --- a/packages/blocks/src/store/actions.js +++ b/packages/blocks/src/store/actions.js @@ -7,6 +7,7 @@ import { castArray, pick, some } from 'lodash'; /** * WordPress dependencies */ +import deprecated from '@wordpress/deprecated'; import { applyFilters } from '@wordpress/hooks'; /** @@ -63,6 +64,12 @@ const processBlockType = ( blockType, { select } ) => { null ); + if ( settings.description && typeof settings.description !== 'string' ) { + deprecated( 'Declaring non-string block descriptions', { + since: '6.2', + } ); + } + if ( settings.deprecated ) { settings.deprecated = settings.deprecated.map( ( deprecation ) => pick(