PluginProbe
Gutenberg / 14.7.0
Gutenberg v14.7.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 14.7.0, at build/block-library/blocks/categories.php

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