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 API: Improve validation after block gets filters applied #14529

Merged
merged 5 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/blocks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 6.3.0 (Unreleased)
gziolo marked this conversation as resolved.
Show resolved Hide resolved

### New Feature

- Added a default implementation for `save` setting in `registerBlockType` which saves no markup in the post content.

## 6.1.0 (2019-03-06)

### New Feature
Expand Down
17 changes: 14 additions & 3 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
/**
* External dependencies
*/
import { get, isFunction, some } from 'lodash';
import {
get,
isFunction,
isPlainObject,
some,
} from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -91,10 +96,16 @@ export function registerBlockType( name, settings ) {
}

settings = applyFilters( 'blocks.registerBlockType', settings, name );
if ( ! isPlainObject( settings ) ) {
console.error(
'Block settings need to remain a plain object after all "blocks.registerBlockType" filters are applied.'
gziolo marked this conversation as resolved.
Show resolved Hide resolved
);
return;
}

if ( ! settings || ! isFunction( settings.save ) ) {
if ( ! isFunction( settings.save ) ) {
console.error(
'The "save" property must be specified and must be a valid function.'
'The "save" property must be a valid function.'
);
return;
}
Expand Down
17 changes: 13 additions & 4 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* External dependencies
*/
import { noop, omit } from 'lodash';
import { noop } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -106,6 +106,15 @@ describe( 'blocks', () => {
expect( block ).toBeUndefined();
} );

it( 'should reject blocks with invalid save function', () => {
const block = registerBlockType( 'my-plugin/fancy-block-5', {
...defaultBlockSettings,
save: 'invalid',
} );
expect( console ).toHaveErroredWith( 'The "save" property must be a valid function.' );
expect( block ).toBeUndefined();
} );

it( 'should reject blocks with an invalid edit function', () => {
const blockType = { save: noop, edit: 'not-a-function', category: 'common', title: 'block title' },
block = registerBlockType( 'my-plugin/fancy-block-6', blockType );
Expand Down Expand Up @@ -318,12 +327,12 @@ describe( 'blocks', () => {
expect( block ).toBeUndefined();
} );

it( 'should reject valid blocks when they become invalid after executing filter which removes save property', () => {
it( 'should reject blocks which become invalid after executing filter which does not return a plain object', () => {
addFilter( 'blocks.registerBlockType', 'core/blocks/without-save', ( settings ) => {
return omit( settings, 'save' );
return [ settings ];
} );
const block = registerBlockType( 'my-plugin/fancy-block-13', defaultBlockSettings );
expect( console ).toHaveErroredWith( 'The "save" property must be specified and must be a valid function.' );
expect( console ).toHaveErroredWith( 'Block settings need to remain a plain object after all "blocks.registerBlockType" filters are applied.' );
gziolo marked this conversation as resolved.
Show resolved Hide resolved
expect( block ).toBeUndefined();
} );
} );
Expand Down