[ 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 * @since 6.3.0 Changed render_block_context priority to `1`. 12 * 13 * @global int $comment_depth 14 * 15 * @param WP_Comment[] $comments The array of comments. 16 * @param WP_Block $block Block instance. 17 * @return string 18 */ 19 function block_core_comment_template_render_comments( $comments, $block ) { 20 global $comment_depth; 21 $thread_comments = get_option( 'thread_comments' ); 22 $thread_comments_depth = get_option( 'thread_comments_depth' ); 23 24 if ( empty( $comment_depth ) ) { 25 $comment_depth = 1; 26 } 27 28 $content = ''; 29 foreach ( $comments as $comment ) { 30 $comment_id = $comment->comment_ID; 31 $filter_block_context = static function ( $context ) use ( $comment_id ) { 32 $context['commentId'] = $comment_id; 33 return $context; 34 }; 35 36 /* 37 * We set commentId context through the `render_block_context` filter so 38 * that dynamically inserted blocks (at `render_block` filter stage) 39 * will also receive that context. 40 * 41 * Use an early priority to so that other 'render_block_context' filters 42 * have access to the values. 43 */ 44 add_filter( 'render_block_context', $filter_block_context, 1 ); 45 46 /* 47 * We construct a new WP_Block instance from the parsed block so that 48 * it'll receive any changes made by the `render_block_data` filter. 49 */ 50 $block_content = ( new WP_Block( $block->parsed_block ) )->render( array( 'dynamic' => false ) ); 51 52 remove_filter( 'render_block_context', $filter_block_context, 1 ); 53 54 $children = $comment->get_children(); 55 56 /* 57 * We need to create the CSS classes BEFORE recursing into the children. 58 * This is because comment_class() uses globals like `$comment_alt` 59 * and `$comment_thread_alt` which are order-sensitive. 60 * 61 * The `false` parameter at the end means that we do NOT want the function 62 * to `echo` the output but to return a string. 63 * See https://developer.wordpress.org/reference/functions/comment_class/#parameters. 64 */ 65 $comment_classes = comment_class( '', $comment->comment_ID, $comment->comment_post_ID, false ); 66 67 // If the comment has children, recurse to create the HTML for the nested 68 // comments. 69 if ( ! empty( $children ) && ! empty( $thread_comments ) ) { 70 if ( $comment_depth < $thread_comments_depth ) { 71 ++$comment_depth; 72 $inner_content = block_core_comment_template_render_comments( 73 $children, 74 $block 75 ); 76 $block_content .= sprintf( '<ol>%1$s</ol>', $inner_content ); 77 --$comment_depth; 78 } else { 79 $block_content .= block_core_comment_template_render_comments( 80 $children, 81 $block 82 ); 83 } 84 } 85 86 $content .= sprintf( '<li id="comment-%1$s" %2$s>%3$s</li>', $comment->comment_ID, $comment_classes, $block_content ); 87 } 88 89 return $content; 90 } 91 92 /** 93 * Renders the `core/comment-template` block on the server. 94 * 95 * @since 6.0.0 96 * 97 * @param array $attributes Block attributes. 98 * @param string $content Block default content. 99 * @param WP_Block $block Block instance. 100 * 101 * @return string Returns the HTML representing the comments using the layout 102 * defined by the block's inner blocks. 103 */ 104 function render_block_core_comment_template( $attributes, $content, $block ) { 105 // Bail out early if the post ID is not set for some reason. 106 if ( empty( $block->context['postId'] ) ) { 107 return ''; 108 } 109 110 if ( post_password_required( $block->context['postId'] ) ) { 111 return; 112 } 113 114 $comment_query = new WP_Comment_Query( 115 build_comment_query_vars_from_block( $block ) 116 ); 117 118 // Get an array of comments for the current post. 119 $comments = $comment_query->get_comments(); 120 if ( count( $comments ) === 0 ) { 121 return ''; 122 } 123 124 $comment_order = get_option( 'comment_order' ); 125 126 if ( 'desc' === $comment_order ) { 127 $comments = array_reverse( $comments ); 128 } 129 130 $wrapper_attributes = get_block_wrapper_attributes(); 131 132 return sprintf( 133 '<ol %1$s>%2$s</ol>', 134 $wrapper_attributes, 135 block_core_comment_template_render_comments( $comments, $block ) 136 ); 137 } 138 139 /** 140 * Registers the `core/comment-template` block on the server. 141 * 142 * @since 6.0.0 143 */ 144 function register_block_core_comment_template() { 145 register_block_type_from_metadata( 146 __DIR__ . '/comment-template', 147 array( 148 'render_callback' => 'render_block_core_comment_template', 149 'skip_inner_blocks' => true, 150 ) 151 ); 152 } 153 add_action( 'init', 'register_block_core_comment_template' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sat Dec 21 08:20:01 2024 | Cross-referenced by PHPXref |