author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
9 | 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 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
11 |
* @since 5.0.0 |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
12 |
* @since 6.7.0 Enable client-side rendering if enhancedPagination context is true. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
13 |
* |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
14 |
* @param array $attributes The block attributes. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
15 |
* @param string $content Block default content. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
16 |
* @param WP_Block $block Block instance. |
9 | 17 |
* |
18 |
* @return string Returns the categories list/dropdown markup. |
|
19 |
*/ |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
20 |
function render_block_core_categories( $attributes, $content, $block ) { |
9 | 21 |
static $block_id = 0; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
22 |
++$block_id; |
9 | 23 |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
24 |
$taxonomy = get_taxonomy( $attributes['taxonomy'] ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
25 |
|
9 | 26 |
$args = array( |
27 |
'echo' => false, |
|
28 |
'hierarchical' => ! empty( $attributes['showHierarchy'] ), |
|
29 |
'orderby' => 'name', |
|
30 |
'show_count' => ! empty( $attributes['showPostCounts'] ), |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
31 |
'taxonomy' => $attributes['taxonomy'], |
9 | 32 |
'title_li' => '', |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
33 |
'hide_empty' => empty( $attributes['showEmpty'] ), |
9 | 34 |
); |
19 | 35 |
if ( ! empty( $attributes['showOnlyTopLevel'] ) && $attributes['showOnlyTopLevel'] ) { |
36 |
$args['parent'] = 0; |
|
37 |
} |
|
9 | 38 |
|
39 |
if ( ! empty( $attributes['displayAsDropdown'] ) ) { |
|
40 |
$id = 'wp-block-categories-' . $block_id; |
|
41 |
$args['id'] = $id; |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
42 |
$args['name'] = $taxonomy->query_var; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
43 |
$args['value_field'] = 'slug'; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
44 |
$args['show_option_none'] = sprintf( |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
45 |
/* translators: %s: taxonomy's singular name */ |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
46 |
__( 'Select %s' ), |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
47 |
$taxonomy->labels->singular_name |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
48 |
); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
49 |
|
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
50 |
$show_label = empty( $attributes['showLabel'] ) ? ' screen-reader-text' : ''; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
51 |
$default_label = $taxonomy->label; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
52 |
$label_text = ! empty( $attributes['label'] ) ? wp_kses_post( $attributes['label'] ) : $default_label; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
53 |
$wrapper_markup = '<div %1$s><label class="wp-block-categories__label' . $show_label . '" for="' . esc_attr( $id ) . '">' . $label_text . '</label>%2$s</div>'; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
54 |
$items_markup = wp_dropdown_categories( $args ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
55 |
$type = 'dropdown'; |
9 | 56 |
|
57 |
if ( ! is_admin() ) { |
|
18 | 58 |
// Inject the dropdown script immediately after the select dropdown. |
59 |
$items_markup = preg_replace( |
|
60 |
'#(?<=</select>)#', |
|
61 |
build_dropdown_script_block_core_categories( $id ), |
|
62 |
$items_markup, |
|
63 |
1 |
|
64 |
); |
|
9 | 65 |
} |
66 |
} else { |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
67 |
$args['show_option_none'] = $taxonomy->labels->no_terms; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
68 |
|
18 | 69 |
$wrapper_markup = '<ul %1$s>%2$s</ul>'; |
9 | 70 |
$items_markup = wp_list_categories( $args ); |
71 |
$type = 'list'; |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
72 |
|
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
73 |
if ( ! empty( $block->context['enhancedPagination'] ) ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
74 |
$p = new WP_HTML_Tag_Processor( $items_markup ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
75 |
while ( $p->next_tag( 'a' ) ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
76 |
$p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
77 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
78 |
$items_markup = $p->get_updated_html(); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
79 |
} |
9 | 80 |
} |
81 |
||
18 | 82 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type}" ) ); |
9 | 83 |
|
16 | 84 |
return sprintf( |
9 | 85 |
$wrapper_markup, |
18 | 86 |
$wrapper_attributes, |
9 | 87 |
$items_markup |
88 |
); |
|
89 |
} |
|
90 |
||
91 |
/** |
|
92 |
* Generates the inline script for a categories dropdown field. |
|
93 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
94 |
* @since 5.0.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
95 |
* |
9 | 96 |
* @param string $dropdown_id ID of the dropdown field. |
97 |
* |
|
98 |
* @return string Returns the dropdown onChange redirection script. |
|
99 |
*/ |
|
100 |
function build_dropdown_script_block_core_categories( $dropdown_id ) { |
|
101 |
ob_start(); |
|
102 |
?> |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
103 |
<script> |
9 | 104 |
( function() { |
105 |
var dropdown = document.getElementById( '<?php echo esc_js( $dropdown_id ); ?>' ); |
|
106 |
function onCatChange() { |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
107 |
if ( dropdown.options[ dropdown.selectedIndex ].value !== -1 ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
108 |
location.href = "<?php echo esc_url( home_url() ); ?>/?" + dropdown.name + '=' + dropdown.options[ dropdown.selectedIndex ].value; |
9 | 109 |
} |
110 |
} |
|
111 |
dropdown.onchange = onCatChange; |
|
112 |
})(); |
|
113 |
</script> |
|
114 |
<?php |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
115 |
return wp_get_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ); |
9 | 116 |
} |
117 |
||
118 |
/** |
|
119 |
* Registers the `core/categories` block on server. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
120 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
121 |
* @since 5.0.0 |
9 | 122 |
*/ |
123 |
function register_block_core_categories() { |
|
16 | 124 |
register_block_type_from_metadata( |
125 |
__DIR__ . '/categories', |
|
9 | 126 |
array( |
127 |
'render_callback' => 'render_block_core_categories', |
|
128 |
) |
|
129 |
); |
|
130 |
} |
|
131 |
add_action( 'init', 'register_block_core_categories' ); |