1<?php
2/**
3 * Server-side rendering of the `core/categories` block.
4 *
5 * @package WordPress
6 */
7
8/**
9 * Renders the `core/categories` block on server.
10 *
11 * @since 5.0.0
12 * @since 6.7.0 Enable client-side rendering if enhancedPagination context is true.
13 *
14 * @param array $attributes The block attributes.
15 * @param string $content Block default content.
16 * @param WP_Block $block Block instance.
17 *
18 * @return string Returns the categories list/dropdown markup.
19 */
20function render_block_core_categories( $attributes, $content, $block ) {
21 static $block_id = 0;
22 ++$block_id;
23
24 $taxonomy = get_taxonomy( $attributes['taxonomy'] );
25
26 $args = array(
27 'echo' => false,
28 'hierarchical' => ! empty( $attributes['showHierarchy'] ),
29 'orderby' => 'name',
30 'show_count' => ! empty( $attributes['showPostCounts'] ),
31 'taxonomy' => $attributes['taxonomy'],
32 'title_li' => '',
33 'hide_empty' => empty( $attributes['showEmpty'] ),
34 );
35 if ( ! empty( $attributes['showOnlyTopLevel'] ) && $attributes['showOnlyTopLevel'] ) {
36 $args['parent'] = 0;
37 }
38
39 if ( ! empty( $attributes['displayAsDropdown'] ) ) {
40 $id = 'wp-block-categories-' . $block_id;
41 $args['id'] = $id;
42 $args['name'] = $taxonomy->query_var;
43 $args['value_field'] = 'slug';
44 $args['show_option_none'] = sprintf(
45 /* translators: %s: taxonomy's singular name */
46 __( 'Select %s' ),
47 $taxonomy->labels->singular_name
48 );
49
50 $show_label = empty( $attributes['showLabel'] ) ? ' screen-reader-text' : '';
51 $default_label = $taxonomy->label;
52 $label_text = ! empty( $attributes['label'] ) ? wp_kses_post( $attributes['label'] ) : $default_label;
53 $wrapper_markup = '<div %1$s><label class="wp-block-categories__label' . $show_label . '" for="' . esc_attr( $id ) . '">' . $label_text . '</label>%2$s</div>';
54 $items_markup = wp_dropdown_categories( $args );
55 $type = 'dropdown';
56
57 if ( ! is_admin() ) {
58 // Inject the dropdown script immediately after the select dropdown.
59 $items_markup = preg_replace(
60 '#(?<=</select>)#',
61 build_dropdown_script_block_core_categories( $id ),
62 $items_markup,
63 1
64 );
65 }
66 } else {
67 $args['show_option_none'] = $taxonomy->labels->no_terms;
68
69 $wrapper_markup = '<ul %1$s>%2$s</ul>';
70 $items_markup = wp_list_categories( $args );
71 $type = 'list';
72
73 if ( ! empty( $block->context['enhancedPagination'] ) ) {
74 $p = new WP_HTML_Tag_Processor( $items_markup );
75 while ( $p->next_tag( 'a' ) ) {
76 $p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' );
77 }
78 $items_markup = $p->get_updated_html();
79 }
80 }
81
82 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type}" ) );
83
84 return sprintf(
85 $wrapper_markup,
86 $wrapper_attributes,
87 $items_markup
88 );
89}
90
91/**
92 * Generates the inline script for a categories dropdown field.
93 *
94 * @since 5.0.0
95 *
96 * @param string $dropdown_id ID of the dropdown field.
97 *
98 * @return string Returns the dropdown onChange redirection script.
99 */
100function build_dropdown_script_block_core_categories( $dropdown_id ) {
101 ob_start();
102
103 $exports = array( $dropdown_id, home_url() );
104 ?>
105 <script>
106 ( ( [ dropdownId, homeUrl ] ) => {
107 const dropdown = document.getElementById( dropdownId );
108 function onSelectChange() {
109 setTimeout( () => {
110 if ( 'escape' === dropdown.dataset.lastkey ) {
111 return;
112 }
113 if ( dropdown.value && dropdown instanceof HTMLSelectElement ) {
114 const url = new URL( homeUrl );
115 url.searchParams.set( dropdown.name, dropdown.value );
116 location.href = url.href;
117 }
118 }, 250 );
119 }
120 function onKeyUp( event ) {
121 if ( 'Escape' === event.key ) {
122 dropdown.dataset.lastkey = 'escape';
123 } else {
124 delete dropdown.dataset.lastkey;
125 }
126 }
127 function onClick() {
128 delete dropdown.dataset.lastkey;
129 }
130 dropdown.addEventListener( 'keyup', onKeyUp );
131 dropdown.addEventListener( 'click', onClick );
132 dropdown.addEventListener( 'change', onSelectChange );
133 } )( <?php echo wp_json_encode( $exports, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?> );
134 </script>
135 <?php
136 return wp_get_inline_script_tag(
137 trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) .
138 "\n//# sourceURL=" . rawurlencode( __FUNCTION__ )
139 );
140}
141
142/**
143 * Registers the `core/categories` block on server.
144 *
145 * @since 5.0.0
146 */
147function register_block_core_categories() {
148 register_block_type_from_metadata(
149 __DIR__ . '/categories',
150 array(
151 'render_callback' => 'render_block_core_categories',
152 )
153 );
154}
155add_action( 'init', 'register_block_core_categories' );
156