From e10eae2c17a8eea7e162643e7dc54b0821c82d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=B3=C5=82kowski?= Date: Mon, 22 Mar 2021 16:31:40 +0100 Subject: [PATCH] Blocks: Ensure theme category is only added when not provided (#30089) --- .../submitting-to-block-directory.md | 1 + .../block-api/block-metadata.md | 1 + .../block-api/block-registration.md | 3 +-- lib/blocks.php | 19 +++++++++++++++++-- packages/create-block/CHANGELOG.md | 4 ++++ packages/create-block/README.md | 2 +- packages/create-block/lib/prompts.js | 2 +- 7 files changed, 26 insertions(+), 6 deletions(-) diff --git a/docs/getting-started/tutorials/create-block/submitting-to-block-directory.md b/docs/getting-started/tutorials/create-block/submitting-to-block-directory.md index 220c30a174ea49..fc32814c364a3d 100644 --- a/docs/getting-started/tutorials/create-block/submitting-to-block-directory.md +++ b/docs/getting-started/tutorials/create-block/submitting-to-block-directory.md @@ -60,6 +60,7 @@ The Block Editor allows you to indicate the category your block belongs in, maki - media - design - widgets +- theme - embed [Read more about categories.](/docs/reference-guides/block-api/block-metadata.md#category) diff --git a/docs/reference-guides/block-api/block-metadata.md b/docs/reference-guides/block-api/block-metadata.md index 8ef4491e9756cd..ccbcb925950b11 100644 --- a/docs/reference-guides/block-api/block-metadata.md +++ b/docs/reference-guides/block-api/block-metadata.md @@ -128,6 +128,7 @@ The core provided categories are: - media - design - widgets +- theme - embed Plugins and Themes can also register [custom block categories](/docs/reference-guides/filters/block-filters.md#managing-block-categories). diff --git a/docs/reference-guides/block-api/block-registration.md b/docs/reference-guides/block-api/block-registration.md index 5ac9d2a3e78bb3..34baf7a7e124d1 100644 --- a/docs/reference-guides/block-api/block-registration.md +++ b/docs/reference-guides/block-api/block-registration.md @@ -60,6 +60,7 @@ The core provided categories are: - media - design - widgets +- theme - embed ```js @@ -226,8 +227,6 @@ example: { Similarly to how the block's style variations can be declared, a block type can define block variations that the user can pick from. The difference is that, rather than changing only the visual appearance, this field provides a way to apply initial custom attributes and inner blocks at the time when a block is inserted. See the [Block Variations API](/docs/reference-guides/block-api/block-variations.md) for more details. - - #### supports (optional) - **_Type:_** `Object` diff --git a/lib/blocks.php b/lib/blocks.php index 7733391726dd1d..5ae68252a61556 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -347,11 +347,26 @@ function gutenberg_register_legacy_social_link_blocks() { /** * Filters the default block categories array to add a new one for themes. - * Should be removed and turned into a core.trac ticket for merge. * - * @param array $categories The list of default block categories. + * This can be removed when plugin support requires WordPress 5.8.0+. + * + * @see https://core.trac.wordpress.org/ticket/52883 + * + * @param array[] $categories The list of default block categories. + * + * @return array[] Filtered block categories. */ function gutenberg_register_theme_block_category( $categories ) { + foreach ( $categories as $category ) { + // Skip when the category is already set in WordPress core. + if ( + isset( $category['slug'] ) && + 'theme' === $category['slug'] + ) { + return $categories; + } + } + $categories[] = array( 'slug' => 'theme', 'title' => _x( 'Theme', 'block category', 'gutenberg' ), diff --git a/packages/create-block/CHANGELOG.md b/packages/create-block/CHANGELOG.md index 6293cf85eb8c19..0f1f8eb155ee95 100644 --- a/packages/create-block/CHANGELOG.md +++ b/packages/create-block/CHANGELOG.md @@ -6,6 +6,10 @@ - Scaffolded plugin requires WordPress 5.7 now ([#29757](https://github.com/WordPress/gutenberg/pull/29757)). +### New Features + +- Add new `theme` category to select for the block type ([#30089](https://github.com/WordPress/gutenberg/pull/30089)). + ## 2.1.0 (2021-03-17) ### New Features diff --git a/packages/create-block/README.md b/packages/create-block/README.md index ddd0241a652465..4f302a967256cf 100644 --- a/packages/create-block/README.md +++ b/packages/create-block/README.md @@ -177,7 +177,7 @@ The following configurable variables are used with the template files. Template - `title` (no default) - a display title for your block. - `description` (no default) - a short description for your block. - `dashicon` (no default) - an icon property thats makes it easier to identify a block, see https://developer.wordpress.org/resource/dashicons/. -- `category` (default: `'widgets'`) - blocks are grouped into categories to help users browse and discover them. The categories provided by core are `text`, `media`, `design`, `widgets`, and `embed`. +- `category` (default: `'widgets'`) - blocks are grouped into categories to help users browse and discover them. The categories provided by core are `text`, `media`, `design`, `widgets`, `theme`, and `embed`. - `attributes` (no default) - see https://developer.wordpress.org/block-editor/developers/block-api/block-attributes/. - `supports` (no default) - optional block extended support features, see https://developer.wordpress.org/block-editor/developers/block-api/block-supports/. - `author` (default: `'The WordPress Contributors'`) diff --git a/packages/create-block/lib/prompts.js b/packages/create-block/lib/prompts.js index 0396737e47cc47..973560fd4dc825 100644 --- a/packages/create-block/lib/prompts.js +++ b/packages/create-block/lib/prompts.js @@ -70,7 +70,7 @@ const category = { type: 'list', name: 'category', message: 'The category name to help users browse and discover your block:', - choices: [ 'text', 'media', 'design', 'widgets', 'embed' ], + choices: [ 'text', 'media', 'design', 'widgets', 'theme', 'embed' ], }; const author = {