From 406baf149ff5fa26f865c99e14ff7bc3f488dc29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=28Greg=29=20Zi=C3=B3=C5=82kowski?= Date: Thu, 28 Mar 2019 14:22:20 +0100 Subject: [PATCH] Blocks API: Improve validation after block gets filters applied (#14529) * Block API: Improve validation after block gets filters applied * Update CHANGELOG.md * Update CHANGELOG.md * Apply suggestions from code review Co-Authored-By: gziolo * Update CHANGELOG.md --- packages/blocks/CHANGELOG.md | 6 ++++++ packages/blocks/src/api/registration.js | 17 ++++++++++++++--- packages/blocks/src/api/test/registration.js | 17 +++++++++++++---- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/packages/blocks/CHANGELOG.md b/packages/blocks/CHANGELOG.md index 96d8125c985e1..55def7da9db4c 100644 --- a/packages/blocks/CHANGELOG.md +++ b/packages/blocks/CHANGELOG.md @@ -1,3 +1,9 @@ +## x.x.x (Unreleased) + +### 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 diff --git a/packages/blocks/src/api/registration.js b/packages/blocks/src/api/registration.js index 727ee171f2b79..f6d5a29544803 100644 --- a/packages/blocks/src/api/registration.js +++ b/packages/blocks/src/api/registration.js @@ -3,7 +3,12 @@ /** * External dependencies */ -import { get, isFunction, some } from 'lodash'; +import { + get, + isFunction, + isPlainObject, + some, +} from 'lodash'; /** * WordPress dependencies @@ -91,10 +96,16 @@ export function registerBlockType( name, settings ) { } settings = applyFilters( 'blocks.registerBlockType', settings, name ); + if ( ! isPlainObject( settings ) ) { + console.error( + 'Block settings must be a valid object.' + ); + 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; } diff --git a/packages/blocks/src/api/test/registration.js b/packages/blocks/src/api/test/registration.js index 7b47b97afb840..05cc3140779ea 100644 --- a/packages/blocks/src/api/test/registration.js +++ b/packages/blocks/src/api/test/registration.js @@ -3,7 +3,7 @@ /** * External dependencies */ -import { noop, omit } from 'lodash'; +import { noop } from 'lodash'; /** * WordPress dependencies @@ -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 ); @@ -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 must be a valid object.' ); expect( block ).toBeUndefined(); } ); } );