PluginProbe
Gutenberg / 18.9.0
Gutenberg v18.9.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 / block-library / blocks / categories.php

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

109 lines 3.2 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 *
13 * @param array $attributes The block attributes.
14 *
15 * @return string Returns the categories list/dropdown markup.
16 */
17 function gutenberg_render_block_core_categories( $attributes ) {
18 static $block_id = 0;
19 ++$block_id;
20
21 $args = array(
22 'echo' => false,
23 'hierarchical' => ! empty( $attributes['showHierarchy'] ),
24 'orderby' => 'name',
25 'show_count' => ! empty( $attributes['showPostCounts'] ),
26 'title_li' => '',
27 'hide_empty' => empty( $attributes['showEmpty'] ),
28 );
29 if ( ! empty( $attributes['showOnlyTopLevel'] ) && $attributes['showOnlyTopLevel'] ) {
30 $args['parent'] = 0;
31 }
32
33 if ( ! empty( $attributes['displayAsDropdown'] ) ) {
34 $id = 'wp-block-categories-' . $block_id;
35 $args['id'] = $id;
36 $args['show_option_none'] = __( 'Select Category' );
37 $show_label = empty( $attributes['showLabel'] ) ? ' screen-reader-text' : '';
38 $default_label = __( 'Categories' );
39 $label_text = ! empty( $attributes['label'] ) ? $attributes['label'] : $default_label;
40 $wrapper_markup = '<div %1$s><label class="wp-block-categories__label' . $show_label . '" for="' . esc_attr( $id ) . '">' . $label_text . '</label>%2$s</div>';
41 $items_markup = wp_dropdown_categories( $args );
42 $type = 'dropdown';
43
44 if ( ! is_admin() ) {
45 // Inject the dropdown script immediately after the select dropdown.
46 $items_markup = preg_replace(
47 '#(?<=</select>)#',
48 gutenberg_build_dropdown_script_block_core_categories( $id ),
49 $items_markup,
50 1
51 );
52 }
53 } else {
54 $wrapper_markup = '<ul %1$s>%2$s</ul>';
55 $items_markup = wp_list_categories( $args );
56 $type = 'list';
57 }
58
59 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type}" ) );
60
61 return sprintf(
62 $wrapper_markup,
63 $wrapper_attributes,
64 $items_markup
65 );
66 }
67
68 /**
69 * Generates the inline script for a categories dropdown field.
70 *
71 * @since 5.0.0
72 *
73 * @param string $dropdown_id ID of the dropdown field.
74 *
75 * @return string Returns the dropdown onChange redirection script.
76 */
77 function gutenberg_build_dropdown_script_block_core_categories( $dropdown_id ) {
78 ob_start();
79 ?>
80 <script>
81 ( function() {
82 var dropdown = document.getElementById( '<?php echo esc_js( $dropdown_id ); ?>' );
83 function gutenberg_onCatChange() {
84 if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
85 location.href = "<?php echo esc_url( home_url() ); ?>/?cat=" + dropdown.options[ dropdown.selectedIndex ].value;
86 }
87 }
88 dropdown.onchange = gutenberg_onCatChange;
89 })();
90 </script>
91 <?php
92 return wp_get_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) );
93 }
94
95 /**
96 * Registers the `core/categories` block on server.
97 *
98 * @since 5.0.0
99 */
100 function gutenberg_register_block_core_categories() {
101 register_block_type_from_metadata(
102 __DIR__ . '/categories',
103 array(
104 'render_callback' => 'gutenberg_render_block_core_categories',
105 )
106 );
107 }
108 add_action( 'init', 'gutenberg_register_block_core_categories', 20 );
109