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 |
11 * @since 5.0.0 |
|
12 * @since 6.7.0 Enable client-side rendering if enhancedPagination context is true. |
12 * |
13 * |
13 * @param array $attributes The block attributes. |
14 * @param array $attributes The block attributes. |
|
15 * @param string $content Block default content. |
|
16 * @param WP_Block $block Block instance. |
14 * |
17 * |
15 * @return string Returns the categories list/dropdown markup. |
18 * @return string Returns the categories list/dropdown markup. |
16 */ |
19 */ |
17 function render_block_core_categories( $attributes ) { |
20 function render_block_core_categories( $attributes, $content, $block ) { |
18 static $block_id = 0; |
21 static $block_id = 0; |
19 ++$block_id; |
22 ++$block_id; |
|
23 |
|
24 $taxonomy = get_taxonomy( $attributes['taxonomy'] ); |
20 |
25 |
21 $args = array( |
26 $args = array( |
22 'echo' => false, |
27 'echo' => false, |
23 'hierarchical' => ! empty( $attributes['showHierarchy'] ), |
28 'hierarchical' => ! empty( $attributes['showHierarchy'] ), |
24 'orderby' => 'name', |
29 'orderby' => 'name', |
25 'show_count' => ! empty( $attributes['showPostCounts'] ), |
30 'show_count' => ! empty( $attributes['showPostCounts'] ), |
|
31 'taxonomy' => $attributes['taxonomy'], |
26 'title_li' => '', |
32 'title_li' => '', |
27 'hide_empty' => empty( $attributes['showEmpty'] ), |
33 'hide_empty' => empty( $attributes['showEmpty'] ), |
28 ); |
34 ); |
29 if ( ! empty( $attributes['showOnlyTopLevel'] ) && $attributes['showOnlyTopLevel'] ) { |
35 if ( ! empty( $attributes['showOnlyTopLevel'] ) && $attributes['showOnlyTopLevel'] ) { |
30 $args['parent'] = 0; |
36 $args['parent'] = 0; |
31 } |
37 } |
32 |
38 |
33 if ( ! empty( $attributes['displayAsDropdown'] ) ) { |
39 if ( ! empty( $attributes['displayAsDropdown'] ) ) { |
34 $id = 'wp-block-categories-' . $block_id; |
40 $id = 'wp-block-categories-' . $block_id; |
35 $args['id'] = $id; |
41 $args['id'] = $id; |
36 $args['show_option_none'] = __( 'Select Category' ); |
42 $args['name'] = $taxonomy->query_var; |
37 $wrapper_markup = '<div %1$s><label class="screen-reader-text" for="' . esc_attr( $id ) . '">' . __( 'Categories' ) . '</label>%2$s</div>'; |
43 $args['value_field'] = 'slug'; |
38 $items_markup = wp_dropdown_categories( $args ); |
44 $args['show_option_none'] = sprintf( |
39 $type = 'dropdown'; |
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'; |
40 |
56 |
41 if ( ! is_admin() ) { |
57 if ( ! is_admin() ) { |
42 // Inject the dropdown script immediately after the select dropdown. |
58 // Inject the dropdown script immediately after the select dropdown. |
43 $items_markup = preg_replace( |
59 $items_markup = preg_replace( |
44 '#(?<=</select>)#', |
60 '#(?<=</select>)#', |
46 $items_markup, |
62 $items_markup, |
47 1 |
63 1 |
48 ); |
64 ); |
49 } |
65 } |
50 } else { |
66 } else { |
|
67 $args['show_option_none'] = $taxonomy->labels->no_terms; |
|
68 |
51 $wrapper_markup = '<ul %1$s>%2$s</ul>'; |
69 $wrapper_markup = '<ul %1$s>%2$s</ul>'; |
52 $items_markup = wp_list_categories( $args ); |
70 $items_markup = wp_list_categories( $args ); |
53 $type = 'list'; |
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 } |
54 } |
80 } |
55 |
81 |
56 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type}" ) ); |
82 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type}" ) ); |
57 |
83 |
58 return sprintf( |
84 return sprintf( |
76 ?> |
102 ?> |
77 <script> |
103 <script> |
78 ( function() { |
104 ( function() { |
79 var dropdown = document.getElementById( '<?php echo esc_js( $dropdown_id ); ?>' ); |
105 var dropdown = document.getElementById( '<?php echo esc_js( $dropdown_id ); ?>' ); |
80 function onCatChange() { |
106 function onCatChange() { |
81 if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) { |
107 if ( dropdown.options[ dropdown.selectedIndex ].value !== -1 ) { |
82 location.href = "<?php echo esc_url( home_url() ); ?>/?cat=" + dropdown.options[ dropdown.selectedIndex ].value; |
108 location.href = "<?php echo esc_url( home_url() ); ?>/?" + dropdown.name + '=' + dropdown.options[ dropdown.selectedIndex ].value; |
83 } |
109 } |
84 } |
110 } |
85 dropdown.onchange = onCatChange; |
111 dropdown.onchange = onCatChange; |
86 })(); |
112 })(); |
87 </script> |
113 </script> |