PluginProbe
Gutenberg / 12.1.0
Gutenberg v12.1.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 / block-library / blocks / categories.php

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

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