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: Update default block categories #19279

Merged
merged 7 commits into from
Jun 2, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ _Parameters_

_Returns_

- `Array`: Categories list.
- `Array<WPBlockCategory>`: Categories list.

<a name="getChildBlockNames" href="#getChildBlockNames">#</a> **getChildBlockNames**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ registerBlockType( 'gutenberg-examples/example-02-stylesheets', {

icon: 'universal-access-alt',

category: 'layout',
category: 'design',

example: {},

Expand All @@ -35,7 +35,7 @@ registerBlockType( 'gutenberg-examples/example-02-stylesheets', {
blocks.registerBlockType( 'gutenberg-examples/example-02-stylesheets', {
title: 'Example: Stylesheets',
icon: 'universal-access-alt',
category: 'layout',
category: 'design',
example: {},
edit: function( props ) {
return el(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
registerBlockType( 'gutenberg-examples/example-04-controls-esnext', {
title: 'Example: Controls (esnext)',
icon: 'universal-access-alt',
category: 'layout',
category: 'design',
attributes: {
content: {
type: 'array',
Expand Down Expand Up @@ -101,7 +101,7 @@ registerBlockType( 'gutenberg-examples/example-04-controls-esnext', {
blocks.registerBlockType( 'gutenberg-examples/example-04-controls', {
title: 'Example: Controls',
icon: 'universal-access-alt',
category: 'layout',
category: 'design',

attributes: {
content: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import { RichText } from '@wordpress/block-editor';
registerBlockType( 'gutenberg-examples/example-03-editable-esnext', {
title: 'Example: Editable (esnext)',
icon: 'universal-access-alt',
category: 'layout',
category: 'design',
attributes: {
content: {
type: 'array',
Expand Down Expand Up @@ -102,7 +102,7 @@ registerBlockType( 'gutenberg-examples/example-03-editable-esnext', {
blocks.registerBlockType( 'gutenberg-examples/example-03-editable', {
title: 'Example: Editable',
icon: 'universal-access-alt',
category: 'layout',
category: 'design',

attributes: {
content: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Here is the basic InnerBlocks usage.

blocks.registerBlockType( 'gutenberg-examples/example-06', {
title: 'Example: Inner Blocks',
category: 'layout',
category: 'design',

edit: function( props ) {
return el(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const blockStyle = {
registerBlockType( 'gutenberg-examples/example-01-basic-esnext', {
title: 'Example: Basic (esnext)',
icon: 'universal-access-alt',
category: 'layout',
category: 'design',
example: {},
edit() {
return <div style={ blockStyle }>Hello World, step 1 (from the editor).</div>;
Expand All @@ -85,7 +85,7 @@ registerBlockType( 'gutenberg-examples/example-01-basic-esnext', {
blocks.registerBlockType( 'gutenberg-examples/example-01-basic', {
title: 'Example: Basic',
icon: 'universal-access-alt',
category: 'layout',
category: 'design',
example: {},
edit: function() {
return el(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ import { registerBlockType } from '@wordpress/blocks';
registerBlockType( 'myguten/test-block', {
title: 'Basic Example',
icon: 'smiley',
category: 'layout',
category: 'design',
edit: () => <div>Hola, mundo!</div>,
save: () => <div>Hola, mundo!</div>,
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Add this code to your JavaScript file (this tutorial will call the file `myguten
registerBlockType( 'myguten/meta-block', {
title: 'Meta Block',
icon: 'smiley',
category: 'common',
category: 'text',

edit: function( props ) {
var className = props.className;
Expand Down Expand Up @@ -82,7 +82,7 @@ import { useEntityProp } from '@wordpress/core-data';
registerBlockType( 'myguten/meta-block', {
title: 'Meta Block',
icon: 'smiley',
category: 'common',
category: 'text',

edit( { className, setAttributes, attributes } ) {
const postType = useSelect(
Expand Down
6 changes: 3 additions & 3 deletions docs/rfc/block-registration.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ To register a new block type, start by creating a `block.json` file. This file:
{
"name": "my-plugin/notice",
"title": "Notice",
"category": "common",
"category": "text",
"parent": [ "core/group" ],
"icon": "star",
"description": "Shows warning, error or success notices ...",
Expand Down Expand Up @@ -134,7 +134,7 @@ This is the display title for your block, which can be translated with our trans
* Property: `category`

```json
{ "category": "common" }
{ "category": "text" }
```

Blocks are grouped into categories to help users browse and discover them.
Expand All @@ -149,7 +149,7 @@ The core provided categories are:

Plugins and Themes can also register [custom block categories](/docs/designers-developers/developers/filters/block-filters.md#managing-block-categories).

An implementation should expect and tolerate unknown categories, providing some reasonable fallback behavior (e.g. a "common" category).
An implementation should expect and tolerate unknown categories, providing some reasonable fallback behavior (e.g. a "text" category).

### Parent

Expand Down
61 changes: 61 additions & 0 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,67 @@ function gutenberg_get_post_from_context() {
return get_post();
}

/**
* Filters default block categories to substitute legacy category names with new
* block categories.
*
* This can be removed when plugin support requires WordPress 5.5.0+.
*
* @see https://core.trac.wordpress.org/ticket/50278
*
* @param array[] $default_categories Array of block categories.
*
* @return array[] Filtered block categories.
*/
function gutenberg_replace_default_block_categories( $default_categories ) {
$substitution = array(
'common' => array(
'slug' => 'text',
'title' => __( 'Text', 'gutenberg' ),
'icon' => null,
),
'formatting' => array(
'slug' => 'media',
'title' => __( 'Media', 'gutenberg' ),
'icon' => null,
),
'layout' => array(
'slug' => 'design',
'title' => __( 'Design', 'gutenberg' ),
'icon' => null,
),
);

// Loop default categories to perform in-place substitution by legacy slug.
foreach ( $default_categories as $i => $default_category ) {
$slug = $default_category['slug'];
if ( isset( $substitution[ $slug ] ) ) {
$default_categories[ $i ] = $substitution[ $slug ];
unset( $substitution[ $slug ] );
}
}

/*
* At this point, `$substitution` should contain only the categories which
* could not be in-place substituted with a default category, likely in the
* case that core has since been updated to use the default categories.
* Check to verify they exist.
*/
$default_category_slugs = wp_list_pluck( $default_categories, 'slug' );
foreach ( $substitution as $i => $substitute_category ) {
if ( in_array( $substitute_category['slug'], $default_category_slugs, true ) ) {
unset( $substitution[ $i ] );
}
}

/*
* Any substitutes remaining should be appended, as they are not yet
* assigned in the default categories array.
*/
return array_merge( $default_categories, array_values( $substitution ) );
}
add_filter( 'block_categories', 'gutenberg_replace_default_block_categories' );

/**
* Shim that hooks into `pre_render_block` so as to override `render_block` with
* a function that assigns block context.
Expand Down
8 changes: 4 additions & 4 deletions packages/block-editor/src/autocompleters/test/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ describe( 'block', () => {
name: 'core/foo',
title: 'foo',
keywords: [ 'foo-keyword-1', 'foo-keyword-2' ],
category: 'formatting',
category: 'design',
};
const inserterItemWithTitleAndEmptyKeywords = {
name: 'core/bar',
title: 'bar',
// Intentionally empty keyword list
keywords: [],
category: 'common',
category: 'text',
};
const inserterItemWithTitleAndUndefinedKeywords = {
name: 'core/baz',
Expand All @@ -91,12 +91,12 @@ describe( 'block', () => {

expect(
blockCompleter.getOptionKeywords( inserterItemWithTitleAndKeywords )
).toEqual( [ 'formatting', 'foo-keyword-1', 'foo-keyword-2', 'foo' ] );
).toEqual( [ 'design', 'foo-keyword-1', 'foo-keyword-2', 'foo' ] );
expect(
blockCompleter.getOptionKeywords(
inserterItemWithTitleAndEmptyKeywords
)
).toEqual( [ 'common', 'bar' ] );
).toEqual( [ 'text', 'bar' ] );
expect(
blockCompleter.getOptionKeywords(
inserterItemWithTitleAndUndefinedKeywords
Expand Down
10 changes: 5 additions & 5 deletions packages/block-editor/src/components/block-edit/test/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe( 'Edit', () => {
const edit = () => <div />;
registerBlockType( 'core/test-block', {
save: noop,
category: 'common',
category: 'text',
title: 'block title',
edit,
} );
Expand All @@ -50,7 +50,7 @@ describe( 'Edit', () => {
const save = () => <div />;
registerBlockType( 'core/test-block', {
save,
category: 'common',
category: 'text',
title: 'block title',
} );

Expand All @@ -67,7 +67,7 @@ describe( 'Edit', () => {
registerBlockType( 'core/test-block', {
edit,
save: noop,
category: 'common',
category: 'text',
title: 'block title',
} );

Expand All @@ -84,7 +84,7 @@ describe( 'Edit', () => {
it( 'should assign context', () => {
const edit = ( { context } ) => context.value;
registerBlockType( 'core/test-block', {
category: 'common',
category: 'text',
title: 'block title',
context: [ 'value' ],
edit,
Expand All @@ -104,7 +104,7 @@ describe( 'Edit', () => {
it( 'should assign context', () => {
const edit = ( { context } ) => context.value;
registerBlockType( 'core/test-block', {
category: 'common',
category: 'text',
title: 'block title',
context: [ 'value' ],
supports: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe( 'BlockSwitcher', () => {

beforeAll( () => {
registerBlockType( 'core/heading', {
category: 'common',
category: 'text',
title: 'Heading',
edit: () => {},
save: () => {},
Expand All @@ -71,7 +71,7 @@ describe( 'BlockSwitcher', () => {
} );

registerBlockType( 'core/paragraph', {
category: 'common',
category: 'text',
title: 'Paragraph',
edit: () => {},
save: () => {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe( 'InnerBlocks', () => {

it( 'should return element as string, with inner blocks', () => {
registerBlockType( 'core/fruit', {
category: 'common',
category: 'text',

title: 'fruit',

Expand Down Expand Up @@ -88,7 +88,7 @@ describe( 'InnerBlocks', () => {
</p>
);
},
category: 'common',
category: 'text',
title: 'block title',
};
registerBlockType( 'core/test-block', blockType );
Expand Down
Loading