diff --git a/packages/block-library/src/term-template/block.json b/packages/block-library/src/term-template/block.json index 676a9f50af2a13..dd82c312198836 100644 --- a/packages/block-library/src/term-template/block.json +++ b/packages/block-library/src/term-template/block.json @@ -8,7 +8,7 @@ "ancestor": [ "core/terms-query" ], "description": "Contains the block elements used to render a taxonomy term, like the name, description, and more.", "textdomain": "default", - "usesContext": [ "termQuery" ], + "usesContext": [ "termQuery", "termId" ], "supports": { "reusable": false, "html": false, diff --git a/packages/block-library/src/term-template/edit.js b/packages/block-library/src/term-template/edit.js index ea595dac89e505..74f8db8e8645b6 100644 --- a/packages/block-library/src/term-template/edit.js +++ b/packages/block-library/src/term-template/edit.js @@ -96,10 +96,11 @@ export default function TermTemplateEdit( { order, orderBy, hideEmpty, - hierarchical = false, - parent = 0, + showNested = false, + parent = false, perPage = 10, } = {}, + termId, }, __unstableLayoutClassNames, } ) { @@ -116,9 +117,16 @@ export default function TermTemplateEdit( { per_page: 100, }; - // Nested terms are returned by default from REST API as long as parent is not set. - // If we want to show nested terms, we must not set parent at all. - if ( parent || ! hierarchical ) { + if ( parent === 'inherit' ) { + if ( termId ) { + queryArgs.parent = termId; + } else { + // If there is no termId in context, we can't inherit, so we fall back to 0. + queryArgs.parent = 0; + } + } else if ( parent !== false || ! showNested ) { + // Nested terms are returned by default from REST API as long as parent is not set. + // If we want to show nested terms, we must not set parent at all. queryArgs.parent = parent || 0; } diff --git a/packages/block-library/src/term-template/index.php b/packages/block-library/src/term-template/index.php index 8e9db6f972ff55..9c9ff90ad0146b 100644 --- a/packages/block-library/src/term-template/index.php +++ b/packages/block-library/src/term-template/index.php @@ -39,21 +39,25 @@ function render_block_core_term_template( $attributes, $content, $block ) { // We set parent only when inheriting from the taxonomy archive context or not // showing nested terms, otherwise nested terms are not displayed. + $parent = $query['parent'] ?? false; if ( - isset( $query['inherit'] ) - && $query['inherit'] - && ( + 'inherit' === $parent + ) { + if ( isset( $query_block_context['termId'] ) ) { + // Get the current term ID from context. + $query_args['parent'] = $query_block_context['termId']; + } elseif ( is_tax( $query_args['taxonomy'] ) // is_tax() does not detect built-in category or tag archives, only custom taxonomies. || ( 'category' === $query_args['taxonomy'] && is_category() ) || ( 'post_tag' === $query_args['taxonomy'] && is_tag() ) - ) - ) { - // Get the current term ID from the queried object. - $current_term_id = get_queried_object_id(); - $query_args['parent'] = $current_term_id; - } elseif ( empty( $query['hierarchical'] ) ) { - $query_args['parent'] = 0; + ) { + // Get the current term ID from the queried object. + $current_term_id = get_queried_object_id(); + $query_args['parent'] = $current_term_id; + } + } elseif ( false !== $parent || empty( $query['showNested'] ) ) { + $query_args['parent'] = $parent ? $parent : 0; } $terms_query = new WP_Term_Query( $query_args ); diff --git a/packages/block-library/src/terms-query/block.json b/packages/block-library/src/terms-query/block.json index 1e2c3098fe94de..837ead66ee5086 100644 --- a/packages/block-library/src/terms-query/block.json +++ b/packages/block-library/src/terms-query/block.json @@ -17,9 +17,8 @@ "order": "asc", "orderBy": "name", "hideEmpty": true, - "parent": false, - "hierarchical": false, - "inherit": false + "showNested": false, + "parent": false } }, "tagName": { @@ -30,6 +29,7 @@ "providesContext": { "termQuery": "termQuery" }, + "usesContext": [ "termId" ], "supports": { "align": [ "wide", "full" ], "html": false, diff --git a/packages/block-library/src/terms-query/edit/inspector-controls/index.js b/packages/block-library/src/terms-query/edit/inspector-controls/index.js index de348fe5edf7c0..e09513f3c71752 100644 --- a/packages/block-library/src/terms-query/edit/inspector-controls/index.js +++ b/packages/block-library/src/terms-query/edit/inspector-controls/index.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { __ } from '@wordpress/i18n'; +import { __, sprintf } from '@wordpress/i18n'; import { __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, @@ -24,9 +24,10 @@ import AdvancedControls from './advanced-controls'; export default function TermsQueryInspectorControls( { attributes, - setQuery, setAttributes, clientId, + context, + setQuery, } ) { const { termQuery, tagName: TagName } = attributes; const { @@ -34,10 +35,11 @@ export default function TermsQueryInspectorControls( { orderBy, order, hideEmpty, - inherit, - hierarchical, + parent = false, + showNested, perPage, } = termQuery; + const { termId } = context; const dropdownMenuProps = useToolsPanelDropdownMenuProps(); const { templateSlug } = useSelect( ( select ) => { @@ -64,17 +66,21 @@ export default function TermsQueryInspectorControls( { // Only display the inherit control if the taxonomy is hierarchical and matches the current template. const displayInheritControl = - isTaxonomyHierarchical && isTaxonomyMatchingTemplate; + isTaxonomyHierarchical && ( isTaxonomyMatchingTemplate || termId ); - // Only display the hierarchical control if the taxonomy is hierarchical and not inheriting. - const displayHierarchicalControl = - isTaxonomyHierarchical && ! termQuery.inherit; + // Only display the show nested control if the taxonomy is hierarchical and not inheriting. + const displayShowNestedControl = + isTaxonomyHierarchical && termQuery.parent === false; // Labels shared between ToolsPanelItem and its child control. const taxonomyControlLabel = __( 'Taxonomy' ); const orderByControlLabel = __( 'Order by' ); const emptyTermsControlLabel = __( 'Show empty terms' ); - const inheritControlLabel = __( 'Inherit parent term from archive' ); + const inheritControlLabel = sprintf( + /* translators: %s: either "parent block" or "archive" */ + __( 'Inherit parent term from %s' ), + termId ? __( 'parent block' ) : __( 'archive' ) + ); const nestedTermsControlLabel = __( 'Show nested terms' ); const maxTermsControlLabel = __( 'Max terms' ); @@ -90,7 +96,7 @@ export default function TermsQueryInspectorControls( { order: 'asc', orderBy: 'name', hideEmpty: true, - hierarchical: false, + showNested: false, parent: false, perPage: 10, }, @@ -149,32 +155,32 @@ export default function TermsQueryInspectorControls( { { displayInheritControl && ( inherit !== false } + hasValue={ () => parent !== false } label={ inheritControlLabel } - onDeselect={ () => setQuery( { inherit: false } ) } + onDeselect={ () => setQuery( { parent: false } ) } isShownByDefault > ) } - { displayHierarchicalControl && ( + { displayShowNestedControl && ( hierarchical !== false } + hasValue={ () => showNested !== false } label={ nestedTermsControlLabel } onDeselect={ () => - setQuery( { hierarchical: false } ) + setQuery( { showNested: false } ) } isShownByDefault > - setQuery( { hierarchical: value } ) + setQuery( { showNested: value } ) } /> diff --git a/packages/block-library/src/terms-query/edit/inspector-controls/inherit-control.js b/packages/block-library/src/terms-query/edit/inspector-controls/inherit-control.js index 4cc8a7a51576c5..335abe0854a178 100644 --- a/packages/block-library/src/terms-query/edit/inspector-controls/inherit-control.js +++ b/packages/block-library/src/terms-query/edit/inspector-controls/inherit-control.js @@ -10,9 +10,9 @@ export default function InheritControl( { value, onChange, ...props } ) { checked={ value } onChange={ ( inherit ) => onChange( { - inherit, - // When enabling inherit, hierarchical is not supported. - ...( inherit ? { hierarchical: false } : {} ), + parent: inherit ? 'inherit' : false, + // When enabling inherit, showNested is not supported. + ...( inherit ? { showNested: false } : {} ), } ) } { ...props } diff --git a/packages/block-library/src/terms-query/edit/terms-query-content.js b/packages/block-library/src/terms-query/edit/terms-query-content.js index 1fd052a601e7df..26fadf4615f5a1 100644 --- a/packages/block-library/src/terms-query/edit/terms-query-content.js +++ b/packages/block-library/src/terms-query/edit/terms-query-content.js @@ -11,12 +11,8 @@ import TermsQueryInspectorControls from './inspector-controls'; const TEMPLATE = [ [ 'core/term-template' ] ]; -export default function TermsQueryContent( { - attributes, - setAttributes, - clientId, - name, -} ) { +export default function TermsQueryContent( props ) { + const { attributes, setAttributes } = props; const { tagName: TagName } = attributes; const blockProps = useBlockProps(); const innerBlocksProps = useInnerBlocksProps( blockProps, { @@ -31,13 +27,7 @@ export default function TermsQueryContent( { ); return ( <> - + );