Skip to content

Commit 0824ebc

Browse files
kmanijakkmanijakmikachanMarc-pi
authored
Terms Query inspector controls revamp + add subterms option (#71633)
* Split controls into separate files * Add subterms control * Extract Empty Terms control * Change the default parent to false * Extract hierarchy control and make it render conditionally * Make the terms to show a radio * Modify the query depends on high level filters * Default to all terms when changing some attributes * Pass terms to show via context * Filter only children terms when subtree is chosen * Assign default query for subterms option * Add explanation to subterms option * Add a guard if terms is null * Add support for subtree on the frontend * Fix lint * Fix merge conflicts * Make code more defensive * Simplify the condition * Add end dot to comment * Fix lint * Rename function to match file name * Rename MaxTermsControls to MaxTermsControl to make it consistent with the rest of inspector controls * Fix duplicated taxonomies when showing all and hierarchical * Fix lint Co-authored-by: kmanijak <[email protected]> Co-authored-by: mikachan <[email protected]> Co-authored-by: Marc-pi <[email protected]>
1 parent 2f61418 commit 0824ebc

File tree

14 files changed

+489
-256
lines changed

14 files changed

+489
-256
lines changed

docs/reference-guides/core-blocks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ An advanced block that allows displaying taxonomy terms based on different query
10071007
- **Category:** theme
10081008
- **Allowed Blocks:** core/term-template
10091009
- **Supports:** align (full, wide), interactivity, ~~html~~
1010-
- **Attributes:** namespace, tagName, termQuery, termQueryId
1010+
- **Attributes:** namespace, tagName, termQuery, termQueryId, termsToShow
10111011

10121012
## Text Columns (deprecated)
10131013

packages/block-library/src/term-template/block.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"ancestor": [ "core/terms-query" ],
99
"description": "Contains the block elements used to render a taxonomy term, like the name, description, and more.",
1010
"textdomain": "default",
11-
"usesContext": [ "termQuery" ],
11+
"usesContext": [ "termQuery", "termsToShow" ],
1212
"attributes": {
1313
"namespace": {
1414
"type": "string"

packages/block-library/src/term-template/edit.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,13 @@ export default function TermTemplateEdit( {
191191
clientId,
192192
setAttributes,
193193
context: {
194+
termsToShow,
194195
termQuery: {
195196
taxonomy,
196197
order,
197198
orderBy,
198199
hideEmpty,
199200
hierarchical,
200-
parent,
201201
perPage = 10,
202202
} = {},
203203
},
@@ -222,13 +222,18 @@ export default function TermTemplateEdit( {
222222
queryArgs
223223
);
224224

225-
// Filter to show only top-level terms if "Show only top-level terms" is enabled.
226225
const filteredTerms = useMemo( () => {
227-
if ( ! terms || parent !== 0 ) {
228-
return terms;
226+
if ( ! terms ) {
227+
return [];
229228
}
230-
return terms.filter( ( term ) => ! term.parent );
231-
}, [ terms, parent ] );
229+
if ( termsToShow === 'top-level' ) {
230+
return terms.filter( ( term ) => ! term.parent );
231+
}
232+
if ( termsToShow === 'subterms' ) {
233+
return terms.filter( ( term ) => term.parent );
234+
}
235+
return terms;
236+
}, [ terms, termsToShow ] );
232237

233238
const { blocks, variations, defaultVariation } = useSelect(
234239
( select ) => {

packages/block-library/src/term-template/index.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ function render_block_core_term_template( $attributes, $content, $block ) {
2727
return '';
2828
}
2929

30-
$query = $query_block_context['termQuery'];
30+
$query = $query_block_context['termQuery'];
31+
$terms_to_show = $query_block_context['termsToShow'] ?? 'all';
3132

3233
$query_args = array(
3334
'taxonomy' => $query['taxonomy'] ?? 'category',
@@ -39,13 +40,18 @@ function render_block_core_term_template( $attributes, $content, $block ) {
3940
'exclude' => $query['exclude'] ?? array(),
4041
);
4142

42-
// Handle parent.
43-
if ( ! empty( $query['hierarchical'] ) && isset( $query['parent'] ) ) {
44-
$query_args['parent'] = $query['parent'];
45-
} elseif ( ! empty( $query['hierarchical'] ) ) {
43+
// We set parent to 0 only if we show all terms as hierarchical or we show top-level terms.
44+
if ( ( 'all' === $terms_to_show && ! empty( $query['hierarchical'] ) ) || 'top-level' === $terms_to_show ) {
4645
$query_args['parent'] = 0;
47-
} elseif ( isset( $query['parent'] ) ) {
48-
$query_args['parent'] = $query['parent'];
46+
} elseif ( 'subterms' === $terms_to_show ) {
47+
// Check if we're in a taxonomy archive context.
48+
if ( is_tax( $query_args['taxonomy'] ) ) {
49+
// Get the current term ID from the queried object.
50+
$current_term_id = get_queried_object_id();
51+
if ( $current_term_id && $current_term_id > 0 ) {
52+
$query_args['parent'] = $current_term_id;
53+
}
54+
}
4955
}
5056

5157
$terms_query = new WP_Term_Query( $query_args );

packages/block-library/src/terms-query/block.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@
2323
"hideEmpty": true,
2424
"include": [],
2525
"exclude": [],
26-
"parent": 0,
26+
"parent": false,
2727
"hierarchical": false
2828
}
2929
},
30+
"termsToShow": {
31+
"type": "string",
32+
"default": "all",
33+
"enum": [ "all", "top-level", "subterms" ]
34+
},
3035
"tagName": {
3136
"type": "string",
3237
"default": "div"
@@ -37,7 +42,8 @@
3742
},
3843
"providesContext": {
3944
"termQueryId": "termQueryId",
40-
"termQuery": "termQuery"
45+
"termQuery": "termQuery",
46+
"termsToShow": "termsToShow"
4147
},
4248
"supports": {
4349
"align": [ "wide", "full" ],

packages/block-library/src/terms-query/inspector-controls.js

Lines changed: 0 additions & 239 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { __ } from '@wordpress/i18n';
5+
import {
6+
InspectorControls,
7+
privateApis as blockEditorPrivateApis,
8+
} from '@wordpress/block-editor';
9+
10+
/**
11+
* Internal dependencies
12+
*/
13+
import { unlock } from '../../lock-unlock';
14+
15+
const { HTMLElementControl } = unlock( blockEditorPrivateApis );
16+
17+
export default function AdvancedControls( {
18+
TagName,
19+
setAttributes,
20+
clientId,
21+
} ) {
22+
return (
23+
<InspectorControls group="advanced">
24+
<HTMLElementControl
25+
tagName={ TagName }
26+
onChange={ ( value ) => setAttributes( { tagName: value } ) }
27+
clientId={ clientId }
28+
options={ [
29+
{ label: __( 'Default (<div>)' ), value: 'div' },
30+
{ label: '<main>', value: 'main' },
31+
{ label: '<section>', value: 'section' },
32+
{ label: '<aside>', value: 'aside' },
33+
] }
34+
/>
35+
</InspectorControls>
36+
);
37+
}

0 commit comments

Comments
 (0)