2022-04-12 11:12:47 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Server-side rendering of the `core/comment-template` block.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function that recursively renders a list of nested comments.
|
|
|
|
*
|
2023-07-25 04:40:23 -04:00
|
|
|
* @since 6.3.0 Changed render_block_context priority to `1`.
|
|
|
|
*
|
2022-04-26 09:42:07 -04:00
|
|
|
* @global int $comment_depth
|
|
|
|
*
|
2022-04-19 11:11:17 -04:00
|
|
|
* @param WP_Comment[] $comments The array of comments.
|
2022-04-12 11:12:47 -04:00
|
|
|
* @param WP_Block $block Block instance.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function block_core_comment_template_render_comments( $comments, $block ) {
|
2022-04-19 11:11:17 -04:00
|
|
|
global $comment_depth;
|
2022-09-27 13:32:52 -04:00
|
|
|
$thread_comments = get_option( 'thread_comments' );
|
|
|
|
$thread_comments_depth = get_option( 'thread_comments_depth' );
|
2022-04-19 11:11:17 -04:00
|
|
|
|
|
|
|
if ( empty( $comment_depth ) ) {
|
|
|
|
$comment_depth = 1;
|
|
|
|
}
|
|
|
|
|
2022-04-12 11:12:47 -04:00
|
|
|
$content = '';
|
|
|
|
foreach ( $comments as $comment ) {
|
2023-06-27 10:24:19 -04:00
|
|
|
$comment_id = $comment->comment_ID;
|
2023-09-26 10:23:26 -04:00
|
|
|
$filter_block_context = static function ( $context ) use ( $comment_id ) {
|
2023-06-27 10:24:19 -04:00
|
|
|
$context['commentId'] = $comment_id;
|
|
|
|
return $context;
|
|
|
|
};
|
2022-04-12 11:12:47 -04:00
|
|
|
|
2023-06-27 10:24:19 -04:00
|
|
|
/*
|
|
|
|
* We set commentId context through the `render_block_context` filter so
|
|
|
|
* that dynamically inserted blocks (at `render_block` filter stage)
|
|
|
|
* will also receive that context.
|
2023-07-11 02:59:09 -04:00
|
|
|
*
|
|
|
|
* Use an early priority to so that other 'render_block_context' filters
|
|
|
|
* have access to the values.
|
2023-06-27 10:24:19 -04:00
|
|
|
*/
|
2023-07-11 02:59:09 -04:00
|
|
|
add_filter( 'render_block_context', $filter_block_context, 1 );
|
2023-06-27 10:24:19 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We construct a new WP_Block instance from the parsed block so that
|
|
|
|
* it'll receive any changes made by the `render_block_data` filter.
|
|
|
|
*/
|
|
|
|
$block_content = ( new WP_Block( $block->parsed_block ) )->render( array( 'dynamic' => false ) );
|
|
|
|
|
2023-07-11 02:59:09 -04:00
|
|
|
remove_filter( 'render_block_context', $filter_block_context, 1 );
|
2022-04-12 11:12:47 -04:00
|
|
|
|
|
|
|
$children = $comment->get_children();
|
|
|
|
|
2022-04-26 09:42:07 -04:00
|
|
|
/*
|
|
|
|
* We need to create the CSS classes BEFORE recursing into the children.
|
|
|
|
* This is because comment_class() uses globals like `$comment_alt`
|
|
|
|
* and `$comment_thread_alt` which are order-sensitive.
|
|
|
|
*
|
|
|
|
* The `false` parameter at the end means that we do NOT want the function
|
|
|
|
* to `echo` the output but to return a string.
|
|
|
|
* See https://developer.wordpress.org/reference/functions/comment_class/#parameters.
|
|
|
|
*/
|
|
|
|
$comment_classes = comment_class( '', $comment->comment_ID, $comment->comment_post_ID, false );
|
|
|
|
|
2022-04-12 11:12:47 -04:00
|
|
|
// If the comment has children, recurse to create the HTML for the nested
|
|
|
|
// comments.
|
2022-09-27 13:32:52 -04:00
|
|
|
if ( ! empty( $children ) && ! empty( $thread_comments ) ) {
|
|
|
|
if ( $comment_depth < $thread_comments_depth ) {
|
2023-02-07 02:04:52 -05:00
|
|
|
++$comment_depth;
|
2022-09-27 13:32:52 -04:00
|
|
|
$inner_content = block_core_comment_template_render_comments(
|
|
|
|
$children,
|
|
|
|
$block
|
|
|
|
);
|
|
|
|
$block_content .= sprintf( '<ol>%1$s</ol>', $inner_content );
|
2023-02-07 02:04:52 -05:00
|
|
|
--$comment_depth;
|
2022-09-27 13:32:52 -04:00
|
|
|
} else {
|
Editor: Update block editor packages to the latest patch releases.
This updates the block editor related npm dependencies to their latest patch versions ahead of WordPress 6.2.1 RC1.
Updated packages:
- @wordpress/annotations@2.26.4
- @wordpress/block-directory@4.3.12
- @wordpress/block-editor@11.3.10
- @wordpress/block-library@8.3.12
- @wordpress/components@23.3.7
- @wordpress/customize-widgets@4.3.12
- @wordpress/edit-post@7.3.12
- @wordpress/edit-site@5.3.12
- @wordpress/edit-widgets@5.3.12
- @wordpress/editor@13.3.10
- @wordpress/format-library@4.3.10
- @wordpress/interface@5.3.8
- @wordpress/list-reusable-blocks@4.3.7
- @wordpress/preferences@3.3.7
- @wordpress/reusable-blocks@4.3.10
- @wordpress/rich-text@6.3.4
- @wordpress/server-side-render@4.3.7
- @wordpress/widgets@3.3.10
This changeset includes the following fixes:
- i18n: Add context to labels related to CSS position properties gutenberg#49135
- Comments: Fix 'sprintf requires more than 1 params' error gutenberg#49054
- Fix the site editor loading in multi-site installs gutenberg#49861
- Fix quick inserter going off-screen in some situations gutenberg#49881
- Site Editor: Decode the site title properly gutenberg#49685
- Firefox: fix input rules (React async state issue) gutenberg#48210
- Only show alignment info when parent layout is constrained. gutenberg#49703
- [Inserter]: Fix onHover error on patterns tab in mobile gutenberg#49450
- Fix site editor redirection after creating new template or template part gutenberg#49364
Props mamaduka, audrasjb, wildworks, ocean90, aristath, costdev, hellofromtonya, youknowriad, mdxfr, oandregal, mattwiebe, bph, ndiego, talldanwp, joen, ellatrix, kevin940726, isabel_brison, andrewserong, ntsekouras, welcher.
Fixes #58274.
Built from https://develop.svn.wordpress.org/trunk@55737
git-svn-id: http://core.svn.wordpress.org/trunk@55249 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-05-09 10:13:25 -04:00
|
|
|
$block_content .= block_core_comment_template_render_comments(
|
2022-09-27 13:32:52 -04:00
|
|
|
$children,
|
|
|
|
$block
|
|
|
|
);
|
|
|
|
}
|
2022-04-12 11:12:47 -04:00
|
|
|
}
|
|
|
|
|
2022-04-19 11:11:17 -04:00
|
|
|
$content .= sprintf( '<li id="comment-%1$s" %2$s>%3$s</li>', $comment->comment_ID, $comment_classes, $block_content );
|
2022-04-12 11:12:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the `core/comment-template` block on the server.
|
|
|
|
*
|
2024-05-23 15:39:28 -04:00
|
|
|
* @since 6.0.0
|
|
|
|
*
|
2022-04-12 11:12:47 -04:00
|
|
|
* @param array $attributes Block attributes.
|
|
|
|
* @param string $content Block default content.
|
|
|
|
* @param WP_Block $block Block instance.
|
|
|
|
*
|
|
|
|
* @return string Returns the HTML representing the comments using the layout
|
|
|
|
* defined by the block's inner blocks.
|
|
|
|
*/
|
|
|
|
function render_block_core_comment_template( $attributes, $content, $block ) {
|
|
|
|
// Bail out early if the post ID is not set for some reason.
|
|
|
|
if ( empty( $block->context['postId'] ) ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2022-05-03 11:00:36 -04:00
|
|
|
if ( post_password_required( $block->context['postId'] ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-12 11:12:47 -04:00
|
|
|
$comment_query = new WP_Comment_Query(
|
|
|
|
build_comment_query_vars_from_block( $block )
|
|
|
|
);
|
|
|
|
|
|
|
|
// Get an array of comments for the current post.
|
|
|
|
$comments = $comment_query->get_comments();
|
|
|
|
if ( count( $comments ) === 0 ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$comment_order = get_option( 'comment_order' );
|
|
|
|
|
|
|
|
if ( 'desc' === $comment_order ) {
|
|
|
|
$comments = array_reverse( $comments );
|
|
|
|
}
|
|
|
|
|
|
|
|
$wrapper_attributes = get_block_wrapper_attributes();
|
|
|
|
|
|
|
|
return sprintf(
|
|
|
|
'<ol %1$s>%2$s</ol>',
|
|
|
|
$wrapper_attributes,
|
|
|
|
block_core_comment_template_render_comments( $comments, $block )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the `core/comment-template` block on the server.
|
2024-05-23 15:39:28 -04:00
|
|
|
*
|
|
|
|
* @since 6.0.0
|
2022-04-12 11:12:47 -04:00
|
|
|
*/
|
|
|
|
function register_block_core_comment_template() {
|
|
|
|
register_block_type_from_metadata(
|
|
|
|
__DIR__ . '/comment-template',
|
|
|
|
array(
|
|
|
|
'render_callback' => 'render_block_core_comment_template',
|
|
|
|
'skip_inner_blocks' => true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
add_action( 'init', 'register_block_core_comment_template' );
|