From 27017880ce1781940fe41f07a2c7315a8ce1e434 Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Mon, 22 Mar 2021 13:36:29 +0100 Subject: [PATCH] Blocks: Ensure theme category is only added when not provided --- .../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 220c30a174ea4..fc32814c364a3 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 8ef4491e9756c..ccbcb925950b1 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 5ac9d2a3e78bb..34baf7a7e124d 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 7733391726dd1..5ae68252a6155 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 6293cf85eb8c1..0f1f8eb155ee9 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 ddd0241a65246..4f302a967256c 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 0396737e47cc4..973560fd4dc82 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 = {