diff --git a/packages/block-library/src/categories/block.json b/packages/block-library/src/categories/block.json index bfd8461f8eda4..842a33c17bb80 100644 --- a/packages/block-library/src/categories/block.json +++ b/packages/block-library/src/categories/block.json @@ -5,7 +5,6 @@ "title": "Terms List", "category": "widgets", "description": "Display a list of all terms of a given taxonomy.", - "keywords": [ "categories" ], "textdomain": "default", "attributes": { "taxonomy": { diff --git a/packages/block-library/src/categories/edit.js b/packages/block-library/src/categories/edit.js index ccbf919433940..ee1b9d3adab91 100644 --- a/packages/block-library/src/categories/edit.js +++ b/packages/block-library/src/categories/edit.js @@ -9,7 +9,6 @@ import clsx from 'clsx'; import { PanelBody, Placeholder, - SelectControl, Spinner, ToggleControl, VisuallyHidden, @@ -23,7 +22,7 @@ import { import { decodeEntities } from '@wordpress/html-entities'; 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: { @@ -41,17 +40,14 @@ export default function CategoriesEdit( { } ) { const selectId = useInstanceId( CategoriesEdit, 'blocks-category-select' ); - const { records: allTaxonomies, isResolvingTaxonomies } = useEntityRecords( + const { record: taxonomy, isResolvingTaxonomy } = useEntityRecord( 'root', - 'taxonomy' + 'taxonomy', + taxonomySlug ); - const taxonomies = allTaxonomies?.filter( ( t ) => t.visibility.public ); - - const taxonomy = taxonomies?.find( ( t ) => t.slug === taxonomySlug ); - const isHierarchicalTaxonomy = - ! isResolvingTaxonomies && taxonomy?.hierarchical; + ! isResolvingTaxonomy && taxonomy?.hierarchical; const query = { per_page: -1, hide_empty: ! showEmpty, context: 'view' }; if ( isHierarchicalTaxonomy && showOnlyTopLevel ) { @@ -185,21 +181,6 @@ export default function CategoriesEdit( { - { Array.isArray( taxonomies ) && ( - ( { - label: t.name, - value: t.slug, - } ) ) } - value={ taxonomySlug } - onChange={ ( selectedTaxonomy ) => - setAttributes( { taxonomy: selectedTaxonomy } ) - } - /> - ) } ', '' ), '', 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 all terms for 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', ) ); }