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

Categories List block: Add variations for taxonomies #64805

Closed
Closed
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
1 change: 0 additions & 1 deletion packages/block-library/src/categories/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
29 changes: 5 additions & 24 deletions packages/block-library/src/categories/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import clsx from 'clsx';
import {
PanelBody,
Placeholder,
SelectControl,
Spinner,
ToggleControl,
VisuallyHidden,
Expand All @@ -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: {
Expand All @@ -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 ) {
Expand Down Expand Up @@ -185,21 +181,6 @@ export default function CategoriesEdit( {
<TagName { ...blockProps }>
<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
{ Array.isArray( taxonomies ) && (
<SelectControl
__nextHasNoMarginBottom
__next40pxDefaultSize
label={ __( 'Taxonomy' ) }
options={ taxonomies.map( ( t ) => ( {
label: t.name,
value: t.slug,
} ) ) }
value={ taxonomySlug }
onChange={ ( selectedTaxonomy ) =>
setAttributes( { taxonomy: selectedTaxonomy } )
}
/>
) }
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Display as dropdown' ) }
Expand Down
60 changes: 59 additions & 1 deletion packages/block-library/src/categories/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,74 @@ function onCatChange() {
return wp_get_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', 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',
)
);
}
Expand Down
Loading