From 5aae434de68aeac4e4ded6c6350d4ca84f5f19be Mon Sep 17 00:00:00 2001 From: Akanshu Singh Date: Mon, 6 Jan 2025 20:00:00 +0530 Subject: [PATCH 1/3] feat: Adds attributes and support to show current taxonomy in categories block --- docs/reference-guides/core-blocks.md | 2 +- .../block-library/src/categories/block.json | 4 ++ packages/block-library/src/categories/edit.js | 39 +++++++++++++++---- .../block-library/src/categories/index.php | 21 +++++++--- 4 files changed, 53 insertions(+), 13 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 0715b1e3547e2a..a46bea4e4560a7 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -79,7 +79,7 @@ Display a list of all terms of a given taxonomy. ([Source](https://github.com/Wo - **Name:** core/categories - **Category:** widgets - **Supports:** align, interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~ -- **Attributes:** displayAsDropdown, label, showEmpty, showHierarchy, showLabel, showOnlyTopLevel, showPostCounts, taxonomy +- **Attributes:** displayAsDropdown, label, showCurrentTaxonomy, 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 3609bdf9ab97c0..736491fa63b3e2 100644 --- a/packages/block-library/src/categories/block.json +++ b/packages/block-library/src/categories/block.json @@ -39,6 +39,10 @@ "showLabel": { "type": "boolean", "default": true + }, + "showCurrentTaxonomy": { + "type": "boolean", + "default": false } }, "usesContext": [ "enhancedPagination" ], diff --git a/packages/block-library/src/categories/edit.js b/packages/block-library/src/categories/edit.js index 8bd2769b0ec711..2d6672bf9e645f 100644 --- a/packages/block-library/src/categories/edit.js +++ b/packages/block-library/src/categories/edit.js @@ -24,6 +24,12 @@ import { decodeEntities } from '@wordpress/html-entities'; import { __, sprintf } from '@wordpress/i18n'; import { pin } from '@wordpress/icons'; import { useEntityRecords } from '@wordpress/core-data'; +import { useSelect } from '@wordpress/data'; + +/** + * Internal dependencies + */ +import { store as coreEditorStore } from '../../../../packages/editor'; export default function CategoriesEdit( { attributes: { @@ -35,6 +41,7 @@ export default function CategoriesEdit( { label, showLabel, taxonomy: taxonomySlug, + showCurrentTaxonomy, }, setAttributes, className, @@ -64,6 +71,11 @@ export default function CategoriesEdit( { query ); + const isSingular = useSelect( ( select ) => { + const currentPostType = select( coreEditorStore ).getCurrentPostType(); + return currentPostType && currentPostType !== 'wp_template'; + }, [] ); + const getCategoriesList = ( parentId ) => { if ( ! categories?.length ) { return []; @@ -209,13 +221,26 @@ export default function CategoriesEdit( { onChange={ toggleAttribute( 'displayAsDropdown' ) } /> { displayAsDropdown && ( - + <> + + { ! isSingular && ( + + ) } + ) } query_var; $args['value_field'] = 'slug'; - $args['show_option_none'] = sprintf( - /* translators: %s: taxonomy's singular name */ - __( 'Select %s' ), - $taxonomy->labels->singular_name - ); + + if ( + ! empty( $attributes['showCurrentTaxonomy'] ) && + $attributes['showCurrentTaxonomy'] && + isset( $taxonomy->query_var ) && + ! empty( $taxonomy->query_var ) + ) { + $args['selected'] = get_query_var( $taxonomy->query_var ); + } else { + + $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 = $taxonomy->label; From b2bc33861b7e310c70d8dc7d67dc1253cf457e78 Mon Sep 17 00:00:00 2001 From: Akanshu Singh Date: Wed, 8 Jan 2025 12:45:29 +0530 Subject: [PATCH 2/3] fix: Removes internal dependency import --- packages/block-library/src/categories/edit.js | 50 ++++++++----------- .../block-library/src/categories/index.php | 17 +++---- 2 files changed, 29 insertions(+), 38 deletions(-) diff --git a/packages/block-library/src/categories/edit.js b/packages/block-library/src/categories/edit.js index 2d6672bf9e645f..a88454455d6428 100644 --- a/packages/block-library/src/categories/edit.js +++ b/packages/block-library/src/categories/edit.js @@ -24,12 +24,6 @@ import { decodeEntities } from '@wordpress/html-entities'; import { __, sprintf } from '@wordpress/i18n'; import { pin } from '@wordpress/icons'; import { useEntityRecords } from '@wordpress/core-data'; -import { useSelect } from '@wordpress/data'; - -/** - * Internal dependencies - */ -import { store as coreEditorStore } from '../../../../packages/editor'; export default function CategoriesEdit( { attributes: { @@ -71,11 +65,6 @@ export default function CategoriesEdit( { query ); - const isSingular = useSelect( ( select ) => { - const currentPostType = select( coreEditorStore ).getCurrentPostType(); - return currentPostType && currentPostType !== 'wp_template'; - }, [] ); - const getCategoriesList = ( parentId ) => { if ( ! categories?.length ) { return []; @@ -144,13 +133,15 @@ export default function CategoriesEdit( { ) }