PluginProbe
Gutenberg / 20.0.1
Gutenberg v20.0.1
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 / block-library / blocks / categories.php

categories.php in Gutenberg 20.0.1, at build/block-library/blocks/categories.php

132 lines 4.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 $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}" ) );
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 <script>
104 ( function() {
105 var dropdown = document.getElementById( '<?php echo esc_js( $dropdown_id ); ?>' );
106 function gutenberg_onCatChange() {
107 if ( dropdown.options[ dropdown.selectedIndex ].value !== -1 ) {
108 location.href = "<?php echo esc_url( home_url() ); ?>/?" + dropdown.name + '=' + dropdown.options[ dropdown.selectedIndex ].value;
109 }
110 }
111 dropdown.onchange = gutenberg_onCatChange;
112 })();
113 </script>
114 <?php
115 return wp_get_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
116 }
117
118 /**
119 * Registers the `core/categories` block on server.
120 *
121 * @since 5.0.0
122 */
123 function gutenberg_register_block_core_categories() {
124 register_block_type_from_metadata(
125 __DIR__ . '/categories',
126 array(
127 'render_callback' => 'gutenberg_render_block_core_categories',
128 )
129 );
130 }
131 add_action( 'init', 'gutenberg_register_block_core_categories', 20 );
132