Skip to content
Open
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
2 changes: 1 addition & 1 deletion packages/block-library/src/term-template/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 13 additions & 5 deletions packages/block-library/src/term-template/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ export default function TermTemplateEdit( {
order,
orderBy,
hideEmpty,
hierarchical = false,
parent = 0,
showNested = false,
parent = false,
perPage = 10,
} = {},
termId,
},
__unstableLayoutClassNames,
} ) {
Expand All @@ -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;
}

Expand Down
24 changes: 14 additions & 10 deletions packages/block-library/src/term-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
6 changes: 3 additions & 3 deletions packages/block-library/src/terms-query/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
"order": "asc",
"orderBy": "name",
"hideEmpty": true,
"parent": false,
"hierarchical": false,
"inherit": false
"showNested": false,
"parent": false
}
},
"tagName": {
Expand All @@ -30,6 +29,7 @@
"providesContext": {
"termQuery": "termQuery"
},
"usesContext": [ "termId" ],
"supports": {
"align": [ "wide", "full" ],
"html": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import {
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
Expand All @@ -24,20 +24,22 @@ import AdvancedControls from './advanced-controls';

export default function TermsQueryInspectorControls( {
attributes,
setQuery,
setAttributes,
clientId,
context,
setQuery,
} ) {
const { termQuery, tagName: TagName } = attributes;
const {
taxonomy,
orderBy,
order,
hideEmpty,
inherit,
hierarchical,
parent = false,
showNested,
perPage,
} = termQuery;
const { termId } = context;
const dropdownMenuProps = useToolsPanelDropdownMenuProps();

const { templateSlug } = useSelect( ( select ) => {
Expand All @@ -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' );

Expand All @@ -90,7 +96,7 @@ export default function TermsQueryInspectorControls( {
order: 'asc',
orderBy: 'name',
hideEmpty: true,
hierarchical: false,
showNested: false,
parent: false,
perPage: 10,
},
Expand Down Expand Up @@ -149,32 +155,32 @@ export default function TermsQueryInspectorControls( {
</ToolsPanelItem>
{ displayInheritControl && (
<ToolsPanelItem
hasValue={ () => inherit !== false }
hasValue={ () => parent !== false }
label={ inheritControlLabel }
onDeselect={ () => setQuery( { inherit: false } ) }
onDeselect={ () => setQuery( { parent: false } ) }
isShownByDefault
>
<InheritControl
label={ inheritControlLabel }
value={ inherit }
value={ parent === 'inherit' }
onChange={ setQuery }
/>
</ToolsPanelItem>
) }
{ displayHierarchicalControl && (
{ displayShowNestedControl && (
<ToolsPanelItem
hasValue={ () => hierarchical !== false }
hasValue={ () => showNested !== false }
label={ nestedTermsControlLabel }
onDeselect={ () =>
setQuery( { hierarchical: false } )
setQuery( { showNested: false } )
}
isShownByDefault
>
<NestedTermsControl
label={ nestedTermsControlLabel }
value={ hierarchical }
value={ showNested }
onChange={ ( value ) =>
setQuery( { hierarchical: value } )
setQuery( { showNested: value } )
}
/>
</ToolsPanelItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -31,13 +27,7 @@ export default function TermsQueryContent( {
);
return (
<>
<TermsQueryInspectorControls
name={ name }
attributes={ attributes }
setQuery={ setQuery }
setAttributes={ setAttributes }
clientId={ clientId }
/>
<TermsQueryInspectorControls { ...props } setQuery={ setQuery } />
<TagName { ...innerBlocksProps } />
</>
);
Expand Down
Loading