author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
19 | 1 |
<?php |
2 |
/** |
|
3 |
* Server-side rendering of the `core/post-comments-form` block. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
*/ |
|
7 |
||
8 |
/** |
|
9 |
* Renders the `core/post-comments-form` block on the server. |
|
10 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
11 |
* @since 6.0.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
12 |
* |
19 | 13 |
* @param array $attributes Block attributes. |
14 |
* @param string $content Block default content. |
|
15 |
* @param WP_Block $block Block instance. |
|
16 |
* @return string Returns the filtered post comments form for the current post. |
|
17 |
*/ |
|
18 |
function render_block_core_post_comments_form( $attributes, $content, $block ) { |
|
19 |
if ( ! isset( $block->context['postId'] ) ) { |
|
20 |
return ''; |
|
21 |
} |
|
22 |
||
23 |
if ( post_password_required( $block->context['postId'] ) ) { |
|
24 |
return; |
|
25 |
} |
|
26 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
27 |
$classes = array( 'comment-respond' ); // See comment further below. |
19 | 28 |
if ( isset( $attributes['textAlign'] ) ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
29 |
$classes[] = 'has-text-align-' . $attributes['textAlign']; |
19 | 30 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
31 |
if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
32 |
$classes[] = 'has-link-color'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
33 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
34 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); |
19 | 35 |
|
36 |
add_filter( 'comment_form_defaults', 'post_comments_form_block_form_defaults' ); |
|
37 |
||
38 |
ob_start(); |
|
39 |
comment_form( array(), $block->context['postId'] ); |
|
40 |
$form = ob_get_clean(); |
|
41 |
||
42 |
remove_filter( 'comment_form_defaults', 'post_comments_form_block_form_defaults' ); |
|
43 |
||
44 |
// We use the outermost wrapping `<div />` returned by `comment_form()` |
|
45 |
// which is identified by its default classname `comment-respond` to inject |
|
46 |
// our wrapper attributes. This way, it is guaranteed that all styling applied |
|
47 |
// to the block is carried along when the comment form is moved to the location |
|
48 |
// of the 'Reply' link that the user clicked by Core's `comment-reply.js` script. |
|
49 |
$form = str_replace( 'class="comment-respond"', $wrapper_attributes, $form ); |
|
50 |
||
51 |
// Enqueue the comment-reply script. |
|
52 |
wp_enqueue_script( 'comment-reply' ); |
|
53 |
||
54 |
return $form; |
|
55 |
} |
|
56 |
||
57 |
/** |
|
58 |
* Registers the `core/post-comments-form` block on the server. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
59 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
60 |
* @since 6.0.0 |
19 | 61 |
*/ |
62 |
function register_block_core_post_comments_form() { |
|
63 |
register_block_type_from_metadata( |
|
64 |
__DIR__ . '/post-comments-form', |
|
65 |
array( |
|
66 |
'render_callback' => 'render_block_core_post_comments_form', |
|
67 |
) |
|
68 |
); |
|
69 |
} |
|
70 |
add_action( 'init', 'register_block_core_post_comments_form' ); |
|
71 |
||
72 |
/** |
|
73 |
* Use the button block classes for the form-submit button. |
|
74 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
75 |
* @since 6.0.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
76 |
* |
19 | 77 |
* @param array $fields The default comment form arguments. |
78 |
* |
|
79 |
* @return array Returns the modified fields. |
|
80 |
*/ |
|
81 |
function post_comments_form_block_form_defaults( $fields ) { |
|
82 |
if ( wp_is_block_theme() ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
83 |
$fields['submit_button'] = '<input name="%1$s" type="submit" id="%2$s" class="wp-block-button__link ' . wp_theme_get_element_class_name( 'button' ) . '" value="%4$s" />'; |
19 | 84 |
$fields['submit_field'] = '<p class="form-submit wp-block-button">%1$s %2$s</p>'; |
85 |
} |
|
86 |
||
87 |
return $fields; |
|
88 |
} |