Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blocks: Deprecate non-string descriptions #44455

Merged
merged 2 commits into from Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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