| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of wp-discourse blocks. |
| 4 |
* |
| 5 |
* @package WPDiscourse |
| 6 |
*/ |
| 7 |
|
| 8 |
use WPDiscourse\DiscourseCommentFormatter\DiscourseCommentFormatter; |
| 9 |
use WPDiscourse\DiscourseComment\DiscourseComment; |
| 10 |
|
| 11 |
/** |
| 12 |
* Renders the `wp-discourse/comments` block on the server. |
| 13 |
* |
| 14 |
* @param array $attributes Block attributes. |
| 15 |
* @param string $content Block default content. |
| 16 |
* @param WP_Block $block Block instance. |
| 17 |
* @return string Returns the filtered post comments for the current post wrapped inside "p" tags. |
| 18 |
*/ |
| 19 |
function render_block_wpdc_comments( $attributes, $content, $block ) { |
| 20 |
$post_id = $block->context['postId']; |
| 21 |
if ( ! isset( $post_id ) ) { |
| 22 |
return ''; |
| 23 |
} |
| 24 |
|
| 25 |
$comment_formatter = new DiscourseCommentFormatter(); |
| 26 |
$comment = new DiscourseComment( $comment_formatter ); |
| 27 |
$default = ''; |
| 28 |
|
| 29 |
$comment->setup_options(); |
| 30 |
$comment_formatter->setup_options(); |
| 31 |
|
| 32 |
ob_start(); |
| 33 |
|
| 34 |
$comment->comments_template( $default ); |
| 35 |
$result = ob_get_contents(); |
| 36 |
|
| 37 |
ob_end_clean(); |
| 38 |
|
| 39 |
return $result; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Registers the `wp-discourse/comments` block on the server. |
| 44 |
*/ |
| 45 |
function register_wpdc_blocks() { |
| 46 |
if ( version_compare( get_bloginfo( 'version' ), '5.5', '>=' ) ) { |
| 47 |
register_block_type_from_metadata( |
| 48 |
WPDISCOURSE_PATH . 'blocks/comments/build/block.json', |
| 49 |
array( |
| 50 |
'render_callback' => 'render_block_wpdc_comments', |
| 51 |
) |
| 52 |
); |
| 53 |
} |
| 54 |
} |
| 55 |
add_action( 'init', 'register_wpdc_blocks' ); |
| 56 |
|