wp-discourse/blocks/comments/comments.php
Angus McLeod 22ee91dfb5
Two Five Seven (#541)
* Don't try to add url to <head> if it's not present

* Update js config and formatting for comment block and sidebar

* PHP Linting

* FIX: Don't auto-publish updates to existing posts.

See: https://meta.discourse.org/t/disable-posting-wordpress-articles-to-discourse-when-theyre-updated/204488

* Bump version and release notes.

* Fix remote-post.php linting

* Update tests.yml to install svn

* Re-generate comments js build
2025-04-15 16:53:23 -07:00

55 lines
1.4 KiB
PHP
Vendored

<?php
/**
* Server-side rendering of wp-discourse blocks.
*
* @package WPDiscourse
*/
use WPDiscourse\DiscourseCommentFormatter\DiscourseCommentFormatter;
use WPDiscourse\DiscourseComment\DiscourseComment;
/**
* Renders the `wp-discourse/comments` block on the server.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
* @return string Returns the filtered post comments for the current post wrapped inside "p" tags.
*/
function render_block_wpdc_comments( $attributes, $content, $block ) {
$post_id = $block->context['postId'];
if ( ! isset( $post_id ) ) {
return '';
}
$comment_formatter = new DiscourseCommentFormatter();
$comment = new DiscourseComment( $comment_formatter );
$default = '';
$comment->setup_options();
$comment_formatter->setup_options();
ob_start();
$comment->comments_template( $default );
$result = ob_get_contents();
ob_end_clean();
return $result;
}
/**
* Registers the `wp-discourse/comments` block on the server.
*/
function register_wpdc_blocks() {
if ( version_compare( get_bloginfo( 'version' ), '5.5', '>=' ) ) {
register_block_type_from_metadata(
WPDISCOURSE_PATH . 'blocks/comments/build/block.json',
array(
'render_callback' => 'render_block_wpdc_comments',
)
);
}
}
add_action( 'init', 'register_wpdc_blocks' );