Skip to content

Commit

Permalink
Blocks: Ensure that metadata registered on the server for core block …
Browse files Browse the repository at this point in the history
…is preserved on the client (try 2) (#29302)

* Revert "Revert "Blocks: Ensure that metadata registered on the server for core block is preserved on the client (#29213)" (#29279)"

This reverts commit 535ac5b.

* Add polyfill for apiVersion when not set on the server
  • Loading branch information
gziolo authored Feb 26, 2021
1 parent 8e43c3a commit fd24e21
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ export const serverSideBlockDefinitions = {};
// eslint-disable-next-line camelcase
export function unstable__bootstrapServerSideBlockDefinitions( definitions ) {
for ( const blockName of Object.keys( definitions ) ) {
// Don't overwrite if already set. It covers the case when metadata
// was initialized from the server.
if ( serverSideBlockDefinitions[ blockName ] ) {
// We still need to polyfill `apiVersion` for WordPress version
// lower than 5.7. If it isn't present in the definition shared
// from the server, we try to fallback to the definition passed.
// @see https://github.com/WordPress/gutenberg/pull/29279
if (
serverSideBlockDefinitions[ blockName ].apiVersion ===
undefined &&
definitions[ blockName ].apiVersion
) {
serverSideBlockDefinitions[ blockName ].apiVersion =
definitions[ blockName ].apiVersion;
}
continue;
}
serverSideBlockDefinitions[ blockName ] = mapKeys(
pickBy( definitions[ blockName ], ( value ) => ! isNil( value ) ),
( value, key ) => camelCase( key )
Expand Down
37 changes: 37 additions & 0 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,43 @@ describe( 'blocks', () => {
} );
} );

// This test can be removed once the polyfill for apiVersion gets removed.
it( 'should apply apiVersion on the client when not set on the server', () => {
const blockName = 'core/test-block-back-compat';
unstable__bootstrapServerSideBlockDefinitions( {
[ blockName ]: {
category: 'widgets',
},
} );
unstable__bootstrapServerSideBlockDefinitions( {
[ blockName ]: {
apiVersion: 2,
category: 'ignored',
},
} );

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

it( 'should validate the icon', () => {
const blockType = {
save: noop,
Expand Down
28 changes: 28 additions & 0 deletions packages/e2e-tests/plugins/register-block-type-hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Plugin Name: Gutenberg Test Register Block Type Hooks
* Plugin URI: https://github.com/WordPress/gutenberg
* Author: Gutenberg Team
*
* @package gutenberg-test-register-block-type-hooks
*/

/**
* Changes the category for the paragraph block.
*
* @param array $metadata Array of metadata for registering a block type.
*
* @return array Filtered metadata for registering a block type.
*/
function gutenberg_test_block_type_metadata( $metadata ) {
if ( 'core/paragraph' !== $metadata['name'] ) {
return $metadata;
}

return array_merge(
$metadata,
array( 'category' => 'widgets' )
);
}

add_filter( 'block_type_metadata', 'gutenberg_test_block_type_metadata' );
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* WordPress dependencies
*/
import {
activatePlugin,
createNewPost,
deactivatePlugin,
openGlobalBlockInserter,
} from '@wordpress/e2e-test-utils';

describe( 'Register block type hooks', () => {
beforeEach( async () => {
await activatePlugin( 'gutenberg-test-register-block-type-hooks' );
await createNewPost();
} );

afterEach( async () => {
await deactivatePlugin( 'gutenberg-test-register-block-type-hooks' );
} );

it( 'has a custom category for Paragraph block', async () => {
await openGlobalBlockInserter();

const widgetsCategory = await page.$(
'.block-editor-block-types-list[aria-label="Widgets"]'
);

expect(
await widgetsCategory.$( '.editor-block-list-item-paragraph' )
).toBeDefined();
} );
} );

0 comments on commit fd24e21

Please sign in to comment.