WordPress/wp-includes/blocks/comment-template.php

134 lines
3.6 KiB
PHP
Raw Normal View History

<?php
/**
* Server-side rendering of the `core/comment-template` block.
*
* @package WordPress
*/
/**
* Function that recursively renders a list of nested comments.
*
* @global int $comment_depth
*
* @param WP_Comment[] $comments The array of comments.
* @param WP_Block $block Block instance.
* @return string
*/
function block_core_comment_template_render_comments( $comments, $block ) {
global $comment_depth;
Editor: Update packages for 6.1 Beta 2. Package updates for bug and regression fixes: * @wordpress/block-directory: 3.15.3 * @wordpress/block-editor: 10.0.3 * @wordpress/block-library: 7.14.3 * @wordpress/block-serialization-default-parser: 4.17.1 * @wordpress/blocks: 11.16.3 * @wordpress/components: 21.0.3 * @wordpress/compose: 5.15.2 * @wordpress/core-data: 5.0.3 * @wordpress/customize-widgets: 3.14.3 * @wordpress/edit-post: 6.14.3 * @wordpress/edit-site: 4.14.4 * @wordpress/edit-widgets: 4.14.3 * @wordpress/editor: 12.16.3 * @wordpress/format-library: 3.15.3 * @wordpress/interface: 4.16.3 * @wordpress/list-reusable-blocks: 3.15.3 * @wordpress/nux: 5.15.3 * @wordpress/preferences: 2.9.3 * @wordpress/reusable-blocks: 3.15.3 * @wordpress/server-side-render: 3.15.3 * @wordpress/style-engine: 1.0.2 * @wordpress/widgets: 2.15.3 References: * [https://github.com/WordPress/gutenberg/pull/44233 Gutenberg PR 44233] – Blocks: Fix searching of blocks when description is non-string * [https://github.com/WordPress/gutenberg/pull/44301 Gutenberg PR 44301] – Block Toolbar: update position when moving blocks * [https://github.com/WordPress/gutenberg/pull/44334 Gutenberg PR 44334] – Global Styles: Re-add styles that were removed, for classic themes * [https://github.com/WordPress/gutenberg/pull/44351 Gutenberg PR 44351] – Comments block: Support nested comments settings in the comments blocks * [https://github.com/WordPress/gutenberg/pull/44448 Gutenberg PR 44448] – Add a correct TS signature for useEntityRecords * [https://github.com/WordPress/gutenberg/pull/44315 Gutenberg PR 44315] – Pullquote: fix transform to quote crash * [https://github.com/WordPress/gutenberg/pull/44446 Gutenberg PR 44446] – Fix spacing property generation in flow layout type. * [https://github.com/WordPress/gutenberg/pull/44408 Gutenberg PR 44408] – Upgrade react-easy-crop to bring in fix for site editor iframe * [https://github.com/WordPress/gutenberg/pull/44406 Gutenberg PR 44406] – Style engine: kebab case preset slugs in the editor * [https://github.com/WordPress/gutenberg/pull/44209 Gutenberg PR 44209] – Fixing padding on the post editor when RootPaddingAwareAlignments setting is enabled * [https://github.com/WordPress/gutenberg/pull/42950 Gutenberg PR 42950] – Popover: fix limitShift logic by adding iframe offset correctly (and a custom shift limiter) * [https://github.com/WordPress/gutenberg/pull/44337 Gutenberg PR 44337] – Submenu block href only if url is not empty * [https://github.com/WordPress/gutenberg/pull/44291 Gutenberg PR 44291] – Add role=application to list view to prevent browse mode triggering in NVDA * [https://github.com/WordPress/gutenberg/pull/44283 Gutenberg PR 44283] – Navigation block: Fix submenu colors for imported classic menus * [https://github.com/WordPress/gutenberg/pull/44282 Gutenberg PR 44282] – Fix popover stacking in the customize widgets editor * [https://github.com/WordPress/gutenberg/pull/44247 Gutenberg PR 44247] – Spacing presets: switch to using numbers instead of t-shirt sizes for labels * [https://github.com/WordPress/gutenberg/pull/44299 Gutenberg PR 44299] – Backport template creation changes from core * [https://github.com/WordPress/gutenberg/pull/44294 Gutenberg PR 44294] – [Block Library - Query Loop]: Fix broken preview in specific category template * [https://github.com/WordPress/gutenberg/pull/44287 Gutenberg PR 44287] – [Block Library]: Rename Comments pagination inner blocks * [https://github.com/WordPress/gutenberg/pull/44256 Gutenberg PR 44256] – Avoid showing the recursion warning in previews when replacing template parts * [https://github.com/WordPress/gutenberg/pull/44265 Gutenberg PR 44265] – Blocks: officially deprecated the children and node matchers * [https://github.com/WordPress/gutenberg/pull/44251 Gutenberg PR 44251] – Global styles: Remove the beta label from global styles header Props bernhard-reiter, cbravobernal. See #56467. Built from https://develop.svn.wordpress.org/trunk@54335 git-svn-id: http://core.svn.wordpress.org/trunk@53894 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-27 19:32:52 +02:00
$thread_comments = get_option( 'thread_comments' );
$thread_comments_depth = get_option( 'thread_comments_depth' );
if ( empty( $comment_depth ) ) {
$comment_depth = 1;
}
$content = '';
foreach ( $comments as $comment ) {
$block_content = ( new WP_Block(
$block->parsed_block,
array(
'commentId' => $comment->comment_ID,
)
) )->render( array( 'dynamic' => false ) );
$children = $comment->get_children();
/*
* 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 );
// If the comment has children, recurse to create the HTML for the nested
// comments.
Editor: Update packages for 6.1 Beta 2. Package updates for bug and regression fixes: * @wordpress/block-directory: 3.15.3 * @wordpress/block-editor: 10.0.3 * @wordpress/block-library: 7.14.3 * @wordpress/block-serialization-default-parser: 4.17.1 * @wordpress/blocks: 11.16.3 * @wordpress/components: 21.0.3 * @wordpress/compose: 5.15.2 * @wordpress/core-data: 5.0.3 * @wordpress/customize-widgets: 3.14.3 * @wordpress/edit-post: 6.14.3 * @wordpress/edit-site: 4.14.4 * @wordpress/edit-widgets: 4.14.3 * @wordpress/editor: 12.16.3 * @wordpress/format-library: 3.15.3 * @wordpress/interface: 4.16.3 * @wordpress/list-reusable-blocks: 3.15.3 * @wordpress/nux: 5.15.3 * @wordpress/preferences: 2.9.3 * @wordpress/reusable-blocks: 3.15.3 * @wordpress/server-side-render: 3.15.3 * @wordpress/style-engine: 1.0.2 * @wordpress/widgets: 2.15.3 References: * [https://github.com/WordPress/gutenberg/pull/44233 Gutenberg PR 44233] – Blocks: Fix searching of blocks when description is non-string * [https://github.com/WordPress/gutenberg/pull/44301 Gutenberg PR 44301] – Block Toolbar: update position when moving blocks * [https://github.com/WordPress/gutenberg/pull/44334 Gutenberg PR 44334] – Global Styles: Re-add styles that were removed, for classic themes * [https://github.com/WordPress/gutenberg/pull/44351 Gutenberg PR 44351] – Comments block: Support nested comments settings in the comments blocks * [https://github.com/WordPress/gutenberg/pull/44448 Gutenberg PR 44448] – Add a correct TS signature for useEntityRecords * [https://github.com/WordPress/gutenberg/pull/44315 Gutenberg PR 44315] – Pullquote: fix transform to quote crash * [https://github.com/WordPress/gutenberg/pull/44446 Gutenberg PR 44446] – Fix spacing property generation in flow layout type. * [https://github.com/WordPress/gutenberg/pull/44408 Gutenberg PR 44408] – Upgrade react-easy-crop to bring in fix for site editor iframe * [https://github.com/WordPress/gutenberg/pull/44406 Gutenberg PR 44406] – Style engine: kebab case preset slugs in the editor * [https://github.com/WordPress/gutenberg/pull/44209 Gutenberg PR 44209] – Fixing padding on the post editor when RootPaddingAwareAlignments setting is enabled * [https://github.com/WordPress/gutenberg/pull/42950 Gutenberg PR 42950] – Popover: fix limitShift logic by adding iframe offset correctly (and a custom shift limiter) * [https://github.com/WordPress/gutenberg/pull/44337 Gutenberg PR 44337] – Submenu block href only if url is not empty * [https://github.com/WordPress/gutenberg/pull/44291 Gutenberg PR 44291] – Add role=application to list view to prevent browse mode triggering in NVDA * [https://github.com/WordPress/gutenberg/pull/44283 Gutenberg PR 44283] – Navigation block: Fix submenu colors for imported classic menus * [https://github.com/WordPress/gutenberg/pull/44282 Gutenberg PR 44282] – Fix popover stacking in the customize widgets editor * [https://github.com/WordPress/gutenberg/pull/44247 Gutenberg PR 44247] – Spacing presets: switch to using numbers instead of t-shirt sizes for labels * [https://github.com/WordPress/gutenberg/pull/44299 Gutenberg PR 44299] – Backport template creation changes from core * [https://github.com/WordPress/gutenberg/pull/44294 Gutenberg PR 44294] – [Block Library - Query Loop]: Fix broken preview in specific category template * [https://github.com/WordPress/gutenberg/pull/44287 Gutenberg PR 44287] – [Block Library]: Rename Comments pagination inner blocks * [https://github.com/WordPress/gutenberg/pull/44256 Gutenberg PR 44256] – Avoid showing the recursion warning in previews when replacing template parts * [https://github.com/WordPress/gutenberg/pull/44265 Gutenberg PR 44265] – Blocks: officially deprecated the children and node matchers * [https://github.com/WordPress/gutenberg/pull/44251 Gutenberg PR 44251] – Global styles: Remove the beta label from global styles header Props bernhard-reiter, cbravobernal. See #56467. Built from https://develop.svn.wordpress.org/trunk@54335 git-svn-id: http://core.svn.wordpress.org/trunk@53894 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-27 19:32:52 +02:00
if ( ! empty( $children ) && ! empty( $thread_comments ) ) {
if ( $comment_depth < $thread_comments_depth ) {
$comment_depth += 1;
$inner_content = block_core_comment_template_render_comments(
$children,
$block
);
$block_content .= sprintf( '<ol>%1$s</ol>', $inner_content );
$comment_depth -= 1;
} else {
$inner_content = block_core_comment_template_render_comments(
$children,
$block
);
$block_content .= sprintf( $inner_content );
}
}
$content .= sprintf( '<li id="comment-%1$s" %2$s>%3$s</li>', $comment->comment_ID, $comment_classes, $block_content );
}
return $content;
}
/**
* Renders the `core/comment-template` 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 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 '';
}
if ( post_password_required( $block->context['postId'] ) ) {
return;
}
$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.
*/
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' );