[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Server-side rendering of the `core/comment-template` block. 4 * 5 * @package WordPress 6 */ 7 8 /** 9 * Function that recursively renders a list of nested comments. 10 * 11 * @global int $comment_depth 12 * 13 * @param WP_Comment[] $comments The array of comments. 14 * @param WP_Block $block Block instance. 15 * @return string 16 */ 17 function block_core_comment_template_render_comments( $comments, $block ) { 18 global $comment_depth; 19 $thread_comments = get_option( 'thread_comments' ); 20 $thread_comments_depth = get_option( 'thread_comments_depth' ); 21 22 if ( empty( $comment_depth ) ) { 23 $comment_depth = 1; 24 } 25 26 $content = ''; 27 foreach ( $comments as $comment ) { 28 29 $block_content = ( new WP_Block( 30 $block->parsed_block, 31 array( 32 'commentId' => $comment->comment_ID, 33 ) 34 ) )->render( array( 'dynamic' => false ) ); 35 36 $children = $comment->get_children(); 37 38 /* 39 * We need to create the CSS classes BEFORE recursing into the children. 40 * This is because comment_class() uses globals like `$comment_alt` 41 * and `$comment_thread_alt` which are order-sensitive. 42 * 43 * The `false` parameter at the end means that we do NOT want the function 44 * to `echo` the output but to return a string. 45 * See https://developer.wordpress.org/reference/functions/comment_class/#parameters. 46 */ 47 $comment_classes = comment_class( '', $comment->comment_ID, $comment->comment_post_ID, false ); 48 49 // If the comment has children, recurse to create the HTML for the nested 50 // comments. 51 if ( ! empty( $children ) && ! empty( $thread_comments ) ) { 52 if ( $comment_depth < $thread_comments_depth ) { 53 ++$comment_depth; 54 $inner_content = block_core_comment_template_render_comments( 55 $children, 56 $block 57 ); 58 $block_content .= sprintf( '<ol>%1$s</ol>', $inner_content ); 59 --$comment_depth; 60 } else { 61 $block_content .= block_core_comment_template_render_comments( 62 $children, 63 $block 64 ); 65 } 66 } 67 68 $content .= sprintf( '<li id="comment-%1$s" %2$s>%3$s</li>', $comment->comment_ID, $comment_classes, $block_content ); 69 } 70 71 return $content; 72 } 73 74 /** 75 * Renders the `core/comment-template` block on the server. 76 * 77 * @param array $attributes Block attributes. 78 * @param string $content Block default content. 79 * @param WP_Block $block Block instance. 80 * 81 * @return string Returns the HTML representing the comments using the layout 82 * defined by the block's inner blocks. 83 */ 84 function render_block_core_comment_template( $attributes, $content, $block ) { 85 // Bail out early if the post ID is not set for some reason. 86 if ( empty( $block->context['postId'] ) ) { 87 return ''; 88 } 89 90 if ( post_password_required( $block->context['postId'] ) ) { 91 return; 92 } 93 94 $comment_query = new WP_Comment_Query( 95 build_comment_query_vars_from_block( $block ) 96 ); 97 98 // Get an array of comments for the current post. 99 $comments = $comment_query->get_comments(); 100 if ( count( $comments ) === 0 ) { 101 return ''; 102 } 103 104 $comment_order = get_option( 'comment_order' ); 105 106 if ( 'desc' === $comment_order ) { 107 $comments = array_reverse( $comments ); 108 } 109 110 $wrapper_attributes = get_block_wrapper_attributes(); 111 112 return sprintf( 113 '<ol %1$s>%2$s</ol>', 114 $wrapper_attributes, 115 block_core_comment_template_render_comments( $comments, $block ) 116 ); 117 } 118 119 /** 120 * Registers the `core/comment-template` block on the server. 121 */ 122 function register_block_core_comment_template() { 123 register_block_type_from_metadata( 124 __DIR__ . '/comment-template', 125 array( 126 'render_callback' => 'render_block_core_comment_template', 127 'skip_inner_blocks' => true, 128 ) 129 ); 130 } 131 add_action( 'init', 'register_block_core_comment_template' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sun Jun 4 08:20:02 2023 | Cross-referenced by PHPXref |