Skip to content

Commit 0c2cb7f

Browse files
authored
Blocks: Deprecate non-string descriptions (#44455)
* Blocks: Deprecate non-string descriptions * Add changelog
1 parent 7fba73a commit 0c2cb7f

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

packages/blocks/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Deprecations
6+
7+
- Deprecate non-string descriptions ([#44455](https://github.com/WordPress/gutenberg/pull/44455)).
8+
59
## 11.17.0 (2022-09-21)
610

711
- 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.

packages/blocks/src/api/test/registration.js

+41
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* WordPress dependencies
55
*/
66
import { addFilter, removeAllFilters, removeFilter } from '@wordpress/hooks';
7+
import { logged } from '@wordpress/deprecated';
78
import { select } from '@wordpress/data';
89

910
/**
@@ -61,6 +62,11 @@ describe( 'blocks', () => {
6162
setUnregisteredTypeHandlerName( undefined );
6263
setDefaultBlockName( undefined );
6364
unstable__bootstrapServerSideBlockDefinitions( {} );
65+
66+
// Reset deprecation logging to ensure we properly track warnings.
67+
for ( const key in logged ) {
68+
delete logged[ key ];
69+
}
6470
} );
6571

6672
describe( 'registerBlockType()', () => {
@@ -832,6 +838,41 @@ describe( 'blocks', () => {
832838
// Only attributes of block1 are supposed to be edited by the filter thus it must differ from block2.
833839
expect( block1.attributes ).not.toEqual( block2.attributes );
834840
} );
841+
842+
it( 'should allow non-string descriptions at registration but warn for undesired usage.', () => {
843+
const newDescription = <p>foo bar</p>;
844+
845+
const block = registerBlockType( 'my-plugin/test-block-1', {
846+
...defaultBlockSettings,
847+
description: newDescription,
848+
} );
849+
850+
expect( block.description ).toBe( newDescription );
851+
expect( console ).toHaveWarnedWith(
852+
'Declaring non-string block descriptions is deprecated since version 6.2.'
853+
);
854+
} );
855+
856+
it( 'should allow non-string descriptions through `blocks.registerBlockType` filter but warn for undesired usage.', () => {
857+
const newDescription = <p>foo bar</p>;
858+
addFilter(
859+
'blocks.registerBlockType',
860+
'core/blocks/non-string-description',
861+
( settings ) => {
862+
settings.description = newDescription;
863+
return settings;
864+
}
865+
);
866+
const block = registerBlockType(
867+
'my-plugin/test-block-2',
868+
defaultBlockSettings
869+
);
870+
871+
expect( block.description ).toBe( newDescription );
872+
expect( console ).toHaveWarnedWith(
873+
'Declaring non-string block descriptions is deprecated since version 6.2.'
874+
);
875+
} );
835876
} );
836877

837878
test( 'registers block from metadata', () => {

packages/blocks/src/store/actions.js

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { castArray, pick, some } from 'lodash';
77
/**
88
* WordPress dependencies
99
*/
10+
import deprecated from '@wordpress/deprecated';
1011
import { applyFilters } from '@wordpress/hooks';
1112

1213
/**
@@ -63,6 +64,12 @@ const processBlockType = ( blockType, { select } ) => {
6364
null
6465
);
6566

67+
if ( settings.description && typeof settings.description !== 'string' ) {
68+
deprecated( 'Declaring non-string block descriptions', {
69+
since: '6.2',
70+
} );
71+
}
72+
6673
if ( settings.deprecated ) {
6774
settings.deprecated = settings.deprecated.map( ( deprecation ) =>
6875
pick(

0 commit comments

Comments
 (0)