PluginProbe
Gutenberg / 24.0.0
Gutenberg v24.0.0
24.0.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 All 403 releases
gutenberg / build / scripts / block-library / categories.php

categories.php in Gutenberg 24.0.0, at build/scripts/block-library/categories.php

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