PluginProbe
Gutenberg / 22.3.0
Gutenberg v22.3.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 / categories.php

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

156 lines 4.7 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 $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 gutenberg_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} wp-block-categories-taxonomy-{$attributes['taxonomy']}" ) );
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 */
100 function gutenberg_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 gutenberg_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 gutenberg_onKeyUp( event ) {
121 if ( 'Escape' === event.key ) {
122 dropdown.dataset.lastkey = 'escape';
123 } else {
124 delete dropdown.dataset.lastkey;
125 }
126 }
127 function gutenberg_onClick() {
128 delete dropdown.dataset.lastkey;
129 }
130 dropdown.addEventListener( 'keyup', gutenberg_onKeyUp );
131 dropdown.addEventListener( 'click', gutenberg_onClick );
132 dropdown.addEventListener( 'change', gutenberg_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 */
147 function gutenberg_register_block_core_categories() {
148 register_block_type_from_metadata(
149 __DIR__ . '/categories',
150 array(
151 'render_callback' => 'gutenberg_render_block_core_categories',
152 )
153 );
154 }
155 add_action( 'init', 'gutenberg_register_block_core_categories', 20 );
156