author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
child 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 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
12 |
* |
9 | 13 |
* @param array $attributes The block attributes. |
14 |
* |
|
15 |
* @return string Returns the categories list/dropdown markup. |
|
16 |
*/ |
|
17 |
function render_block_core_categories( $attributes ) { |
|
18 |
static $block_id = 0; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
19 |
++$block_id; |
9 | 20 |
|
21 |
$args = array( |
|
22 |
'echo' => false, |
|
23 |
'hierarchical' => ! empty( $attributes['showHierarchy'] ), |
|
24 |
'orderby' => 'name', |
|
25 |
'show_count' => ! empty( $attributes['showPostCounts'] ), |
|
26 |
'title_li' => '', |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
27 |
'hide_empty' => empty( $attributes['showEmpty'] ), |
9 | 28 |
); |
19 | 29 |
if ( ! empty( $attributes['showOnlyTopLevel'] ) && $attributes['showOnlyTopLevel'] ) { |
30 |
$args['parent'] = 0; |
|
31 |
} |
|
9 | 32 |
|
33 |
if ( ! empty( $attributes['displayAsDropdown'] ) ) { |
|
34 |
$id = 'wp-block-categories-' . $block_id; |
|
35 |
$args['id'] = $id; |
|
36 |
$args['show_option_none'] = __( 'Select Category' ); |
|
19 | 37 |
$wrapper_markup = '<div %1$s><label class="screen-reader-text" for="' . esc_attr( $id ) . '">' . __( 'Categories' ) . '</label>%2$s</div>'; |
9 | 38 |
$items_markup = wp_dropdown_categories( $args ); |
39 |
$type = 'dropdown'; |
|
40 |
||
41 |
if ( ! is_admin() ) { |
|
18 | 42 |
// Inject the dropdown script immediately after the select dropdown. |
43 |
$items_markup = preg_replace( |
|
44 |
'#(?<=</select>)#', |
|
45 |
build_dropdown_script_block_core_categories( $id ), |
|
46 |
$items_markup, |
|
47 |
1 |
|
48 |
); |
|
9 | 49 |
} |
50 |
} else { |
|
18 | 51 |
$wrapper_markup = '<ul %1$s>%2$s</ul>'; |
9 | 52 |
$items_markup = wp_list_categories( $args ); |
53 |
$type = 'list'; |
|
54 |
} |
|
55 |
||
18 | 56 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type}" ) ); |
9 | 57 |
|
16 | 58 |
return sprintf( |
9 | 59 |
$wrapper_markup, |
18 | 60 |
$wrapper_attributes, |
9 | 61 |
$items_markup |
62 |
); |
|
63 |
} |
|
64 |
||
65 |
/** |
|
66 |
* Generates the inline script for a categories dropdown field. |
|
67 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
68 |
* @since 5.0.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
69 |
* |
9 | 70 |
* @param string $dropdown_id ID of the dropdown field. |
71 |
* |
|
72 |
* @return string Returns the dropdown onChange redirection script. |
|
73 |
*/ |
|
74 |
function build_dropdown_script_block_core_categories( $dropdown_id ) { |
|
75 |
ob_start(); |
|
76 |
?> |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
77 |
<script> |
9 | 78 |
( function() { |
79 |
var dropdown = document.getElementById( '<?php echo esc_js( $dropdown_id ); ?>' ); |
|
80 |
function onCatChange() { |
|
81 |
if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
82 |
location.href = "<?php echo esc_url( home_url() ); ?>/?cat=" + dropdown.options[ dropdown.selectedIndex ].value; |
9 | 83 |
} |
84 |
} |
|
85 |
dropdown.onchange = onCatChange; |
|
86 |
})(); |
|
87 |
</script> |
|
88 |
<?php |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
89 |
return wp_get_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ); |
9 | 90 |
} |
91 |
||
92 |
/** |
|
93 |
* 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
|
94 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
95 |
* @since 5.0.0 |
9 | 96 |
*/ |
97 |
function register_block_core_categories() { |
|
16 | 98 |
register_block_type_from_metadata( |
99 |
__DIR__ . '/categories', |
|
9 | 100 |
array( |
101 |
'render_callback' => 'render_block_core_categories', |
|
102 |
) |
|
103 |
); |
|
104 |
} |
|
105 |
add_action( 'init', 'register_block_core_categories' ); |