6 */ |
6 */ |
7 |
7 |
8 /** |
8 /** |
9 * Renders the `core/categories` block on server. |
9 * Renders the `core/categories` block on server. |
10 * |
10 * |
|
11 * @since 5.0.0 |
|
12 * |
11 * @param array $attributes The block attributes. |
13 * @param array $attributes The block attributes. |
12 * |
14 * |
13 * @return string Returns the categories list/dropdown markup. |
15 * @return string Returns the categories list/dropdown markup. |
14 */ |
16 */ |
15 function render_block_core_categories( $attributes ) { |
17 function render_block_core_categories( $attributes ) { |
16 static $block_id = 0; |
18 static $block_id = 0; |
17 $block_id++; |
19 ++$block_id; |
18 |
20 |
19 $args = array( |
21 $args = array( |
20 'echo' => false, |
22 'echo' => false, |
21 'hierarchical' => ! empty( $attributes['showHierarchy'] ), |
23 'hierarchical' => ! empty( $attributes['showHierarchy'] ), |
22 'orderby' => 'name', |
24 'orderby' => 'name', |
23 'show_count' => ! empty( $attributes['showPostCounts'] ), |
25 'show_count' => ! empty( $attributes['showPostCounts'] ), |
24 'title_li' => '', |
26 'title_li' => '', |
|
27 'hide_empty' => empty( $attributes['showEmpty'] ), |
25 ); |
28 ); |
26 if ( ! empty( $attributes['showOnlyTopLevel'] ) && $attributes['showOnlyTopLevel'] ) { |
29 if ( ! empty( $attributes['showOnlyTopLevel'] ) && $attributes['showOnlyTopLevel'] ) { |
27 $args['parent'] = 0; |
30 $args['parent'] = 0; |
28 } |
31 } |
29 |
32 |
60 } |
63 } |
61 |
64 |
62 /** |
65 /** |
63 * Generates the inline script for a categories dropdown field. |
66 * Generates the inline script for a categories dropdown field. |
64 * |
67 * |
|
68 * @since 5.0.0 |
|
69 * |
65 * @param string $dropdown_id ID of the dropdown field. |
70 * @param string $dropdown_id ID of the dropdown field. |
66 * |
71 * |
67 * @return string Returns the dropdown onChange redirection script. |
72 * @return string Returns the dropdown onChange redirection script. |
68 */ |
73 */ |
69 function build_dropdown_script_block_core_categories( $dropdown_id ) { |
74 function build_dropdown_script_block_core_categories( $dropdown_id ) { |
70 ob_start(); |
75 ob_start(); |
71 ?> |
76 ?> |
72 <script type='text/javascript'> |
77 <script> |
73 /* <![CDATA[ */ |
|
74 ( function() { |
78 ( function() { |
75 var dropdown = document.getElementById( '<?php echo esc_js( $dropdown_id ); ?>' ); |
79 var dropdown = document.getElementById( '<?php echo esc_js( $dropdown_id ); ?>' ); |
76 function onCatChange() { |
80 function onCatChange() { |
77 if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) { |
81 if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) { |
78 location.href = "<?php echo home_url(); ?>/?cat=" + dropdown.options[ dropdown.selectedIndex ].value; |
82 location.href = "<?php echo esc_url( home_url() ); ?>/?cat=" + dropdown.options[ dropdown.selectedIndex ].value; |
79 } |
83 } |
80 } |
84 } |
81 dropdown.onchange = onCatChange; |
85 dropdown.onchange = onCatChange; |
82 })(); |
86 })(); |
83 /* ]]> */ |
|
84 </script> |
87 </script> |
85 <?php |
88 <?php |
86 return ob_get_clean(); |
89 return wp_get_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ); |
87 } |
90 } |
88 |
91 |
89 /** |
92 /** |
90 * Registers the `core/categories` block on server. |
93 * Registers the `core/categories` block on server. |
|
94 * |
|
95 * @since 5.0.0 |
91 */ |
96 */ |
92 function register_block_core_categories() { |
97 function register_block_core_categories() { |
93 register_block_type_from_metadata( |
98 register_block_type_from_metadata( |
94 __DIR__ . '/categories', |
99 __DIR__ . '/categories', |
95 array( |
100 array( |