Skip to content

Commit

Permalink
Blocks: Deprecate non-string descriptions (#44455)
Browse files Browse the repository at this point in the history
* Blocks: Deprecate non-string descriptions

* Add changelog
  • Loading branch information
tyxla committed Sep 26, 2022
1 parent 7fba73a commit 0c2cb7f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/blocks/CHANGELOG.md
Expand Up @@ -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.
Expand Down
41 changes: 41 additions & 0 deletions packages/blocks/src/api/test/registration.js
Expand Up @@ -4,6 +4,7 @@
* WordPress dependencies
*/
import { addFilter, removeAllFilters, removeFilter } from '@wordpress/hooks';
import { logged } from '@wordpress/deprecated';
import { select } from '@wordpress/data';

/**
Expand Down Expand Up @@ -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()', () => {
Expand Down Expand Up @@ -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 = <p>foo bar</p>;

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 = <p>foo bar</p>;
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', () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/blocks/src/store/actions.js
Expand Up @@ -7,6 +7,7 @@ import { castArray, pick, some } from 'lodash';
/**
* WordPress dependencies
*/
import deprecated from '@wordpress/deprecated';
import { applyFilters } from '@wordpress/hooks';

/**
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 0c2cb7f

Please sign in to comment.