PluginProbe
Gutenberg / 22.9.0
Gutenberg v22.9.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / scripts / block-library / term-template.php

term-template.php in Gutenberg 22.9.0, at build/scripts/block-library/term-template.php

138 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Server-side rendering of the `core/term-template` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Renders the `core/term-template` block on the server.
10 *
11 * @since 6.9.0
12 *
13 * @param array $attributes Block attributes.
14 * @param string $content Block default content.
15 * @param WP_Block $block Block instance.
16 *
17 * @return string Returns the output of the term template.
18 */
19 function gutenberg_render_block_core_term_template( $attributes, $content, $block ) {
20 if ( ! isset( $block->context ) || empty( $block->context['termQuery'] ) ) {
21 return '';
22 }
23
24 $query = $block->context['termQuery'];
25
26 $query_args = array(
27 'number' => $query['perPage'],
28 'order' => $query['order'],
29 'orderby' => $query['orderBy'],
30 'hide_empty' => $query['hideEmpty'],
31 );
32
33 $inherit_query = isset( $query['inherit'] )
34 && $query['inherit']
35 && ( is_tax() || is_category() || is_tag() );
36
37 if ( $inherit_query ) {
38 // Get the current term and taxonomy from the queried object.
39 $queried_object = get_queried_object();
40
41 // For hierarchical taxonomies, show children of the current term.
42 // For non-hierarchical taxonomies, show all terms (don't set parent).
43 if ( is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
44 // If showNested is true, use child_of to include nested terms.
45 // Otherwise, use parent to show only direct children.
46 if ( ! empty( $query['showNested'] ) ) {
47 $query_args['child_of'] = $queried_object->term_id;
48 } else {
49 $query_args['parent'] = $queried_object->term_id;
50 }
51 }
52 $query_args['taxonomy'] = $queried_object->taxonomy;
53 } else {
54 // If not inheriting set `taxonomy` from the block attribute.
55 $query_args['taxonomy'] = $query['taxonomy'];
56
57 // If we are including specific terms we ignore `showNested` argument.
58 if ( ! empty( $query['include'] ) ) {
59 $query_args['include'] = array_unique( array_map( 'intval', $query['include'] ) );
60 $query_args['orderby'] = 'include';
61 $query_args['order'] = 'asc';
62 } elseif ( is_taxonomy_hierarchical( $query['taxonomy'] ) && empty( $query['showNested'] ) ) {
63 // We set parent only when inheriting from the taxonomy archive context or not
64 // showing nested terms, otherwise nested terms are not displayed.
65 $query_args['parent'] = 0;
66 }
67 }
68
69 $terms_query = new WP_Term_Query( $query_args );
70 $terms = $terms_query->get_terms();
71
72 if ( ! $terms || is_wp_error( $terms ) ) {
73 return '';
74 }
75
76 $content = '';
77 foreach ( $terms as $term ) {
78 // Get an instance of the current Term Template block.
79 $block_instance = $block->parsed_block;
80
81 // Set the block name to one that does not correspond to an existing registered block.
82 // This ensures that for the inner instances of the Term Template block, we do not render any block supports.
83 $block_instance['blockName'] = 'core/null';
84
85 $term_id = $term->term_id;
86 $taxonomy = $term->taxonomy;
87
88 $filter_block_context = static function ( $context ) use ( $term_id, $taxonomy ) {
89 $context['termId'] = $term_id;
90 $context['taxonomy'] = $taxonomy;
91 return $context;
92 };
93
94 // Use an early priority to so that other 'render_block_context' filters have access to the values.
95 add_filter( 'render_block_context', $filter_block_context, 1 );
96
97 // Render the inner blocks of the Term Template block with `dynamic` set to `false` to prevent calling
98 // `render_callback` and ensure that no wrapper markup is included.
99 $block_content = ( new WP_Block( $block_instance ) )->render( array( 'dynamic' => false ) );
100
101 remove_filter( 'render_block_context', $filter_block_context, 1 );
102
103 // Wrap the render inner blocks in a `li` element with the appropriate term classes.
104 $term_classes = "wp-block-term term-{$term->term_id} {$term->taxonomy} taxonomy-{$term->taxonomy}";
105
106 $content .= '<li class="' . esc_attr( $term_classes ) . '">' . $block_content . '</li>';
107 }
108
109 $classnames = '';
110
111 if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
112 $classnames .= 'has-link-color';
113 }
114
115 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => trim( $classnames ) ) );
116
117 return sprintf(
118 '<ul %s>%s</ul>',
119 $wrapper_attributes,
120 $content
121 );
122 }
123
124 /**
125 * Registers the `core/term-template` block on the server.
126 *
127 * @since 6.9.0
128 */
129 function gutenberg_register_block_core_term_template() {
130 register_block_type_from_metadata(
131 __DIR__ . '/term-template',
132 array(
133 'render_callback' => 'gutenberg_render_block_core_term_template',
134 )
135 );
136 }
137 add_action( 'init', 'gutenberg_register_block_core_term_template', 20 );
138