From 8adf0ea685c839b6a91c473062f1d95104d3131d Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 26 Aug 2024 17:38:45 +0200 Subject: [PATCH 01/24] Categories List block: Add new taxonomy attribute --- docs/reference-guides/core-blocks.md | 2 +- packages/block-library/src/categories/block.json | 4 ++++ packages/block-library/src/categories/edit.js | 3 ++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index b9cae44550181c..d34ec86c3e629f 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -79,7 +79,7 @@ Display a list of all categories. ([Source](https://github.com/WordPress/gutenbe - **Name:** core/categories - **Category:** widgets - **Supports:** align, interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~ -- **Attributes:** displayAsDropdown, label, showEmpty, showHierarchy, showLabel, showOnlyTopLevel, showPostCounts +- **Attributes:** displayAsDropdown, label, showEmpty, showHierarchy, showLabel, showOnlyTopLevel, showPostCounts, taxonomy ## Code diff --git a/packages/block-library/src/categories/block.json b/packages/block-library/src/categories/block.json index 460fbef92fdab2..8373a1f286319b 100644 --- a/packages/block-library/src/categories/block.json +++ b/packages/block-library/src/categories/block.json @@ -7,6 +7,10 @@ "description": "Display a list of all categories.", "textdomain": "default", "attributes": { + "taxonomy": { + "type": "string", + "default": "category" + }, "displayAsDropdown": { "type": "boolean", "default": false diff --git a/packages/block-library/src/categories/edit.js b/packages/block-library/src/categories/edit.js index 48d505eeb75aee..97270d85620e57 100644 --- a/packages/block-library/src/categories/edit.js +++ b/packages/block-library/src/categories/edit.js @@ -33,6 +33,7 @@ export default function CategoriesEdit( { showEmpty, label, showLabel, + taxonomy, }, setAttributes, className, @@ -45,7 +46,7 @@ export default function CategoriesEdit( { const { records: categories, isResolving } = useEntityRecords( 'taxonomy', - 'category', + taxonomy, query ); From 0ba38ab4b3cf2a92fdbe7db6d93049b6051dc896 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 26 Aug 2024 17:39:48 +0200 Subject: [PATCH 02/24] Categories List block: Generate block variations --- .../block-library/src/categories/index.php | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/categories/index.php b/packages/block-library/src/categories/index.php index 5c0cc63415cb78..34661abfb620e6 100644 --- a/packages/block-library/src/categories/index.php +++ b/packages/block-library/src/categories/index.php @@ -103,16 +103,74 @@ function onCatChange() { return wp_get_inline_script_tag( str_replace( array( '' ), '', ob_get_clean() ) ); } +/** + * Returns the available variations for the `core/categories` block. + * + * @since 6.7.0 + * + * @return array The available variations for the block. + */ +function block_core_categories_build_variations() { + $taxonomies = get_taxonomies( + array( + 'publicly_queryable' => true, + 'show_in_rest' => true, + ), + 'objects' + ); + + // Split the available taxonomies to `built_in` and custom ones, + // in order to prioritize the `built_in` taxonomies at the + // search results. + $built_ins = array(); + $custom_variations = array(); + + // Create and register the eligible taxonomies variations. + foreach ( $taxonomies as $taxonomy ) { + $variation = array( + 'name' => $taxonomy->name, + 'title' => sprintf( + /* translators: %s: taxonomy's label */ + __( '%s List' ), + $taxonomy->label + ), + 'description' => sprintf( + /* translators: %s: taxonomy's label */ + __( 'Display a list of terms from the taxonomy: %s' ), + $taxonomy->label + ), + 'attributes' => array( + 'taxonomy' => $taxonomy->name, + ), + 'isActive' => array( 'taxonomy' ), + 'scope' => array( 'inserter', 'transform' ), + ); + // Set the category variation as the default one. + if ( 'category' === $taxonomy->name ) { + $variation['isDefault'] = true; + } + if ( $taxonomy->_builtin ) { + $built_ins[] = $variation; + } else { + $custom_variations[] = $variation; + } + } + + return array_merge( $built_ins, $custom_variations ); +} + /** * Registers the `core/categories` block on server. * * @since 5.0.0 + * @since 6.7.0 Added the `variation_callback` argument. */ function register_block_core_categories() { register_block_type_from_metadata( __DIR__ . '/categories', array( - 'render_callback' => 'render_block_core_categories', + 'render_callback' => 'render_block_core_categories', + 'variation_callback' => 'block_core_categories_build_variations', ) ); } From e9e469a790e83676e7f5275325e5695547689375 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 26 Aug 2024 17:56:29 +0200 Subject: [PATCH 03/24] Pass taxonomy arg to frontend --- packages/block-library/src/categories/index.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/block-library/src/categories/index.php b/packages/block-library/src/categories/index.php index 34661abfb620e6..a0ec1bf5d26450 100644 --- a/packages/block-library/src/categories/index.php +++ b/packages/block-library/src/categories/index.php @@ -26,6 +26,7 @@ function render_block_core_categories( $attributes, $content, $block ) { 'hierarchical' => ! empty( $attributes['showHierarchy'] ), 'orderby' => 'name', 'show_count' => ! empty( $attributes['showPostCounts'] ), + 'taxonomy' => $attributes['taxonomy'], 'title_li' => '', 'hide_empty' => empty( $attributes['showEmpty'] ), ); From c23f489513931c34cce4267148ee6399b1b630c7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 26 Aug 2024 18:26:51 +0200 Subject: [PATCH 04/24] Update integration test snapshot --- test/integration/fixtures/blocks/core__categories.json | 1 + 1 file changed, 1 insertion(+) diff --git a/test/integration/fixtures/blocks/core__categories.json b/test/integration/fixtures/blocks/core__categories.json index 5d271aa5c84990..698105d8921341 100644 --- a/test/integration/fixtures/blocks/core__categories.json +++ b/test/integration/fixtures/blocks/core__categories.json @@ -3,6 +3,7 @@ "name": "core/categories", "isValid": true, "attributes": { + "taxonomy": "category", "displayAsDropdown": false, "showHierarchy": false, "showPostCounts": false, From 1ed944e57e47fb078f3a397c33cb134f7508388f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 28 Aug 2024 12:53:27 +0200 Subject: [PATCH 05/24] Tweak variation description --- packages/block-library/src/categories/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/categories/index.php b/packages/block-library/src/categories/index.php index a0ec1bf5d26450..33dd79650deae0 100644 --- a/packages/block-library/src/categories/index.php +++ b/packages/block-library/src/categories/index.php @@ -137,7 +137,7 @@ function block_core_categories_build_variations() { ), 'description' => sprintf( /* translators: %s: taxonomy's label */ - __( 'Display a list of terms from the taxonomy: %s' ), + __( 'Display a list of all terms for the taxonomy: %s' ), $taxonomy->label ), 'attributes' => array( From 3884104b3431584d42c05af3dea15182ea7a8edb Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 2 Sep 2024 15:30:19 +0200 Subject: [PATCH 06/24] Show correct labels for dropdown on frontend --- packages/block-library/src/categories/index.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/categories/index.php b/packages/block-library/src/categories/index.php index 33dd79650deae0..b1a14257292431 100644 --- a/packages/block-library/src/categories/index.php +++ b/packages/block-library/src/categories/index.php @@ -21,6 +21,8 @@ function render_block_core_categories( $attributes, $content, $block ) { static $block_id = 0; ++$block_id; + $taxonomy = get_taxonomy( $attributes['taxonomy'] ); + $args = array( 'echo' => false, 'hierarchical' => ! empty( $attributes['showHierarchy'] ), @@ -37,9 +39,13 @@ function render_block_core_categories( $attributes, $content, $block ) { if ( ! empty( $attributes['displayAsDropdown'] ) ) { $id = 'wp-block-categories-' . $block_id; $args['id'] = $id; - $args['show_option_none'] = __( 'Select Category' ); + $args['show_option_none'] = sprintf( + /* translators: %s: taxonomy's singular name */ + __( 'Select %s' ), + $taxonomy->labels->singular_name + ); $show_label = empty( $attributes['showLabel'] ) ? ' screen-reader-text' : ''; - $default_label = __( 'Categories' ); + $default_label = $taxonomy->label; $label_text = ! empty( $attributes['label'] ) ? $attributes['label'] : $default_label; $wrapper_markup = '
%2$s
'; $items_markup = wp_dropdown_categories( $args ); From 3a059a8c7f9d1fc585d5fb0a5c4eed6f974f7d98 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 2 Sep 2024 15:46:13 +0200 Subject: [PATCH 07/24] Show correct labels for dropdown in editor --- packages/block-library/src/categories/edit.js | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/categories/edit.js b/packages/block-library/src/categories/edit.js index 97270d85620e57..9c6e087374b662 100644 --- a/packages/block-library/src/categories/edit.js +++ b/packages/block-library/src/categories/edit.js @@ -20,9 +20,9 @@ import { RichText, } from '@wordpress/block-editor'; import { decodeEntities } from '@wordpress/html-entities'; -import { __ } from '@wordpress/i18n'; +import { __, sprintf } from '@wordpress/i18n'; import { pin } from '@wordpress/icons'; -import { useEntityRecords } from '@wordpress/core-data'; +import { useEntityRecord, useEntityRecords } from '@wordpress/core-data'; export default function CategoriesEdit( { attributes: { @@ -33,7 +33,7 @@ export default function CategoriesEdit( { showEmpty, label, showLabel, - taxonomy, + taxonomy: taxonomySlug, }, setAttributes, className, @@ -44,9 +44,15 @@ export default function CategoriesEdit( { query.parent = 0; } + const { record: taxonomy } = useEntityRecord( + 'root', + 'taxonomy', + taxonomySlug + ); + const { records: categories, isResolving } = useEntityRecords( 'taxonomy', - taxonomy, + taxonomySlug, query ); @@ -103,7 +109,7 @@ export default function CategoriesEdit( { @@ -112,11 +118,17 @@ export default function CategoriesEdit( { /> ) : ( - { label ? label : __( 'Categories' ) } + { label ? label : taxonomy.name } ) }