wppaste
WordPress

render_block_core_latest_comments( array $attributes ): string

Since
5.1.0
Source
wp-includes/blocks/latest-comments.php:45
Renders the core/latest-comments block on server.

Compatibility

WordPress
since 5.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$attributesarray
The block attributes.

Return value

string
Returns the post content with latest comments added.

Performance profile

How much work a call to render_block_core_latest_comments() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_comments().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
45–78

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 228.

Plugin surface
1 hook

Third-party callbacks on 'widget_comments_args' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • querycontent queryget_comments()called directly
  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()called directly
  • cacheobject cachewp_cache_get()one call below render_block_core_latest_comments()
  • serializeserialisationmaybe_unserialize()one call below render_block_core_latest_comments()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost render_block_core_latest_comments() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always45–55number(), apply_filters(), class(), sprintf()
empty($comments)48–58number(), apply_filters(), class(), __(), sprintf()
!empty($comments)64–75number(), apply_filters(), wp_list_pluck(), array_unique(), get_option(), _prime_post_caches(), class(), sprintf()
always67–78number(), apply_filters(), wp_list_pluck(), array_unique(), get_option(), _prime_post_caches(), class(), __(), sprintf()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev22645–78202 fewer instructions than PHP 8.5
8.522845–7820
8.422845–78206 fewer instructions than PHP 8.3
8.323448–8420
8.223448–8420
8.123448–8420
7.423448–8420

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while render_block_core_latest_comments() runs, in this order:

  1. apply_filters( widget_comments_args )filterline 55 (+10 into the body)

    Filters the arguments for the Recent Comments widget.

Uses · 21

Show all 21

Source code

function render_block_core_latest_comments( $attributes ) {	// Handle backward compatibility: check for old displayExcerpt attribute	if ( isset( $attributes['displayExcerpt'] ) ) {		$display_content = $attributes['displayExcerpt'] ? 'excerpt' : 'none';	} else {		$display_content = $attributes['displayContent'] ?? 'excerpt';	} 	$comments = get_comments(		/** This filter is documented in wp-includes/widgets/class-wp-widget-recent-comments.php */		apply_filters(			'widget_comments_args',			array(				'number'      => $attributes['commentsToShow'],				'status'      => 'approve',				'post_status' => 'publish',			),			array()		)	); 	$list_items_markup = '';	if ( ! empty( $comments ) ) {		// Prime the cache for associated posts. This is copied from \WP_Widget_Recent_Comments::widget().		$post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );		_prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false ); 		foreach ( $comments as $comment ) {			$list_items_markup .= '<li class="wp-block-latest-comments__comment">';			if ( $attributes['displayAvatar'] ) {				$avatar = get_avatar(					$comment,					48,					'',					'',					array(						'class' => 'wp-block-latest-comments__comment-avatar',					)				);				if ( $avatar ) {					$list_items_markup .= $avatar;				}			} 			$list_items_markup .= '<article>';			$list_items_markup .= '<footer class="wp-block-latest-comments__comment-meta">';			$author_url         = get_comment_author_url( $comment );			if ( empty( $author_url ) && ! empty( $comment->user_id ) ) {				$author_url = get_author_posts_url( $comment->user_id );			} 			$author_markup = '';			if ( $author_url ) {				$author_markup .= '<a class="wp-block-latest-comments__comment-author" href="' . esc_url( $author_url ) . '">' . get_comment_author( $comment ) . '</a>';			} else {				$author_markup .= '<span class="wp-block-latest-comments__comment-author">' . get_comment_author( $comment ) . '</span>';			} 			// `_draft_or_post_title` calls `esc_html()` so we don't need to wrap that call in			// `esc_html`.			$post_title = '<a class="wp-block-latest-comments__comment-link" href="' . esc_url( get_comment_link( $comment ) ) . '">' . wp_latest_comments_draft_or_post_title( $comment->comment_post_ID ) . '</a>'; 			$list_items_markup .= sprintf(				/* translators: 1: author name (inside <a> or <span> tag, based on if they have a URL), 2: post title related to this comment */				__( '%1$s on %2$s' ),				$author_markup,				$post_title			); 			if ( $attributes['displayDate'] ) {				$list_items_markup .= sprintf(					'<time datetime="%1$s" class="wp-block-latest-comments__comment-date">%2$s</time>',					esc_attr( get_comment_date( 'c', $comment ) ),					date_i18n( get_option( 'date_format' ), get_comment_date( 'U', $comment ) )				);			}			$list_items_markup .= '</footer>';			if ( 'full' === $display_content ) {				$comment_text       = post_password_required( $comment->comment_post_ID )					? __( 'Password protected' )

Changelog

Introduced in 5.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/blocks/latest-comments.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.