Skip to content

Commit

Permalink
Blocks: Skip null values returned from the server during registration (
Browse files Browse the repository at this point in the history
…#22849)

* Blocks: Skip null values returned from the server during registration

* Revert the default change for description field in REST API for block types
  • Loading branch information
gziolo authored Jun 4, 2020
1 parent 94f665e commit 338e253
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 35 deletions.
24 changes: 12 additions & 12 deletions lib/class-wp-rest-block-types-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ public function get_item_schema() {
),
'icon' => array(
'description' => __( 'Icon of block type.', 'gutenberg' ),
'type' => 'string',
'default' => '',
'type' => array( 'string', 'null' ),
'default' => null,
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
Expand All @@ -386,8 +386,8 @@ public function get_item_schema() {
),
'category' => array(
'description' => __( 'Block category.', 'gutenberg' ),
'type' => 'string',
'default' => '',
'type' => array( 'string', null ),
'default' => null,
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
Expand All @@ -400,29 +400,29 @@ public function get_item_schema() {
),
'editor_script' => array(
'description' => __( 'Editor script handle.', 'gutenberg' ),
'type' => 'string',
'default' => '',
'type' => array( 'string', null ),
'default' => null,
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'script' => array(
'description' => __( 'Public facing script handle.', 'gutenberg' ),
'type' => 'string',
'default' => '',
'type' => array( 'string', null ),
'default' => null,
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'editor_style' => array(
'description' => __( 'Editor style handle.', 'gutenberg' ),
'type' => 'string',
'default' => '',
'type' => array( 'string', null ),
'default' => null,
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'style' => array(
'description' => __( 'Public facing style handle.', 'gutenberg' ),
'type' => 'string',
'default' => '',
'type' => array( 'string', null ),
'default' => null,
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
Expand Down
60 changes: 37 additions & 23 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
/**
* External dependencies
*/
import { get, omit, pick, isFunction, isPlainObject, some } from 'lodash';
import {
get,
isFunction,
isNil,
isPlainObject,
omit,
pick,
pickBy,
some,
} from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -91,26 +100,26 @@ import { DEPRECATED_ENTRY_KEYS } from './constants';
*
* @typedef {Object} WPBlock
*
* @property {string} name Block type's namespaced name.
* @property {string} title Human-readable block type label.
* @property {string} description A detailed block type description.
* @property {string} category Block type category classification,
* used in search interfaces to arrange
* block types by category.
* @property {WPBlockTypeIcon} [icon] Block type icon.
* @property {string[]} [keywords] Additional keywords to produce block
* type as result in search interfaces.
* @property {Object} [attributes] Block type attributes.
* @property {WPComponent} [save] Optional component describing
* serialized markup structure of a
* block type.
* @property {WPComponent} edit Component rendering an element to
* manipulate the attributes of a block
* in the context of an editor.
* @property {WPBlockVariation[]} [variations] The list of block variations.
* @property {Object} [example] Example provides structured data for
* the block preview. When not defined
* then no preview is shown.
* @property {string} name Block type's namespaced name.
* @property {string} title Human-readable block type label.
* @property {string} [description] A detailed block type description.
* @property {string} [category] Block type category classification,
* used in search interfaces to arrange
* block types by category.
* @property {WPBlockTypeIcon} [icon] Block type icon.
* @property {string[]} [keywords] Additional keywords to produce block
* type as result in search interfaces.
* @property {Object} [attributes] Block type attributes.
* @property {WPComponent} [save] Optional component describing
* serialized markup structure of a
* block type.
* @property {WPComponent} edit Component rendering an element to
* manipulate the attributes of a block
* in the context of an editor.
* @property {WPBlockVariation[]} [variations] The list of block variations.
* @property {Object} [example] Example provides structured data for
* the block preview. When not defined
* then no preview is shown.
*/

/**
Expand Down Expand Up @@ -155,10 +164,15 @@ export function registerBlockType( name, settings ) {
settings = {
name,
icon: blockDefault,
attributes: {},
keywords: [],
attributes: {},
supports: {},
styles: [],
save: () => null,
...get( serverSideBlockDefinitions, name ),
...pickBy(
get( serverSideBlockDefinitions, name, {} ),
( value ) => ! isNil( value )
),
...settings,
};

Expand Down
60 changes: 60 additions & 0 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ describe( 'blocks', () => {
},
attributes: {},
keywords: [],
supports: {},
styles: [],
save: noop,
category: 'text',
title: 'block title',
Expand Down Expand Up @@ -268,6 +270,8 @@ describe( 'blocks', () => {
},
attributes: {},
keywords: [],
supports: {},
styles: [],
save: expect.any( Function ),
} );
} );
Expand Down Expand Up @@ -301,10 +305,42 @@ describe( 'blocks', () => {
},
},
keywords: [],
supports: {},
styles: [],
}
);
} );

it( 'should skip null values returned from the server', () => {
const blockName = 'core/test-block-with-null-server-values';
unstable__bootstrapServerSideBlockDefinitions( {
[ blockName ]: {
icon: null,
category: null,
parent: null,
attributes: null,
example: null,
},
} );

const blockType = {
title: 'block title',
};
registerBlockType( blockName, blockType );
expect( getBlockType( blockName ) ).toEqual( {
name: blockName,
save: expect.any( Function ),
title: 'block title',
icon: {
src: blockIcon,
},
attributes: {},
keywords: [],
supports: {},
styles: [],
} );
} );

it( 'should validate the icon', () => {
const blockType = {
save: noop,
Expand Down Expand Up @@ -365,6 +401,8 @@ describe( 'blocks', () => {
},
attributes: {},
keywords: [],
supports: {},
styles: [],
} );
} );

Expand All @@ -391,6 +429,8 @@ describe( 'blocks', () => {
},
attributes: {},
keywords: [],
supports: {},
styles: [],
} );
} );

Expand Down Expand Up @@ -431,6 +471,8 @@ describe( 'blocks', () => {
},
attributes: {},
keywords: [],
supports: {},
styles: [],
} );
} );

Expand Down Expand Up @@ -485,6 +527,8 @@ describe( 'blocks', () => {
},
attributes: {},
keywords: [],
supports: {},
styles: [],
} );
} );

Expand All @@ -508,6 +552,8 @@ describe( 'blocks', () => {
},
attributes: {},
keywords: [],
supports: {},
styles: [],
} );
} );

Expand Down Expand Up @@ -588,6 +634,8 @@ describe( 'blocks', () => {
icon: blockIcon,
attributes: {},
keywords: [],
supports: {},
styles: [],
save: () => null,
...get(
serverSideBlockDefinitions,
Expand Down Expand Up @@ -704,6 +752,8 @@ describe( 'blocks', () => {
},
attributes: {},
keywords: [],
supports: {},
styles: [],
variations: [],
},
] );
Expand All @@ -719,6 +769,8 @@ describe( 'blocks', () => {
},
attributes: {},
keywords: [],
supports: {},
styles: [],
} );
expect( getBlockTypes() ).toEqual( [] );
} );
Expand Down Expand Up @@ -795,6 +847,8 @@ describe( 'blocks', () => {
},
attributes: {},
keywords: [],
supports: {},
styles: [],
} );
} );

Expand All @@ -817,6 +871,8 @@ describe( 'blocks', () => {
},
attributes: {},
keywords: [],
supports: {},
styles: [],
} );
} );
} );
Expand Down Expand Up @@ -846,6 +902,8 @@ describe( 'blocks', () => {
},
attributes: {},
keywords: [],
supports: {},
styles: [],
variations: [],
},
{
Expand All @@ -859,6 +917,8 @@ describe( 'blocks', () => {
},
attributes: {},
keywords: [],
supports: {},
styles: [],
variations: [],
},
] );
Expand Down

0 comments on commit 338e253

Please sign in to comment.