19
|
1 |
<?php |
|
2 |
/** |
|
3 |
* Server-side rendering of the `core/post-comments` block. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
*/ |
|
7 |
|
|
8 |
/** |
|
9 |
* Renders the `core/post-comments` block on the server. |
|
10 |
* |
|
11 |
* @param array $attributes Block attributes. |
|
12 |
* @param string $content Block default content. |
|
13 |
* @param WP_Block $block Block instance. |
|
14 |
* @return string Returns the filtered post comments for the current post wrapped inside "p" tags. |
|
15 |
*/ |
|
16 |
function render_block_core_post_comments( $attributes, $content, $block ) { |
|
17 |
global $post; |
|
18 |
|
|
19 |
$post_id = $block->context['postId']; |
|
20 |
if ( ! isset( $post_id ) ) { |
|
21 |
return ''; |
|
22 |
} |
|
23 |
|
|
24 |
$comment_args = array( |
|
25 |
'post_id' => $post_id, |
|
26 |
'count' => true, |
|
27 |
); |
|
28 |
// Return early if there are no comments and comments are closed. |
|
29 |
if ( ! comments_open( $post_id ) && get_comments( $comment_args ) === 0 ) { |
|
30 |
return ''; |
|
31 |
} |
|
32 |
|
|
33 |
$post_before = $post; |
|
34 |
$post = get_post( $post_id ); |
|
35 |
setup_postdata( $post ); |
|
36 |
|
|
37 |
ob_start(); |
|
38 |
// There's a deprecation warning generated by WP Core. |
|
39 |
// Ideally this deprecation is removed from Core. |
|
40 |
// In the meantime, this removes it from the output. |
|
41 |
add_filter( 'deprecated_file_trigger_error', '__return_false' ); |
|
42 |
comments_template(); |
|
43 |
remove_filter( 'deprecated_file_trigger_error', '__return_false' ); |
|
44 |
$post = $post_before; |
|
45 |
|
|
46 |
$classes = ''; |
|
47 |
if ( isset( $attributes['textAlign'] ) ) { |
|
48 |
$classes .= 'has-text-align-' . $attributes['textAlign']; |
|
49 |
} |
|
50 |
|
|
51 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) ); |
|
52 |
$output = ob_get_clean(); |
|
53 |
|
|
54 |
return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $output ); |
|
55 |
} |
|
56 |
|
|
57 |
/** |
|
58 |
* Registers the `core/post-comments` block on the server. |
|
59 |
*/ |
|
60 |
function register_block_core_post_comments() { |
|
61 |
register_block_type_from_metadata( |
|
62 |
__DIR__ . '/post-comments', |
|
63 |
array( |
|
64 |
'render_callback' => 'render_block_core_post_comments', |
|
65 |
) |
|
66 |
); |
|
67 |
} |
|
68 |
add_action( 'init', 'register_block_core_post_comments' ); |
|
69 |
|
|
70 |
/** |
|
71 |
* Use the button block classes for the form-submit button. |
|
72 |
* |
|
73 |
* @param array $fields The default comment form arguments. |
|
74 |
* |
|
75 |
* @return array Returns the modified fields. |
|
76 |
*/ |
|
77 |
function post_comments_block_form_defaults( $fields ) { |
|
78 |
if ( wp_is_block_theme() ) { |
|
79 |
$fields['submit_button'] = '<input name="%1$s" type="submit" id="%2$s" class="%3$s wp-block-button__link" value="%4$s" />'; |
|
80 |
$fields['submit_field'] = '<p class="form-submit wp-block-button">%1$s %2$s</p>'; |
|
81 |
} |
|
82 |
|
|
83 |
return $fields; |
|
84 |
} |
|
85 |
add_filter( 'comment_form_defaults', 'post_comments_block_form_defaults' ); |