render_block_core_latest_comments( array $attributes ): string
- Since
- 5.1.0
- Source
wp-includes/blocks/latest-comments.php:45
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
- Scaling
- Scales with input
- Instructions
- 45–78
- Plugin surface
- 1 hook
- Called by
- 0
Reaches the database via get_comments().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 228.
Third-party callbacks on 'widget_comments_args' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- querycontent query
get_comments()called directly - hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()called directly - cacheobject cache
wp_cache_get()one call below render_block_core_latest_comments() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 45–55 | number(), apply_filters(), class(), sprintf() |
empty($comments) | 48–58 | number(), apply_filters(), class(), __(), sprintf() |
!empty($comments) | 64–75 | number(), apply_filters(), wp_list_pluck(), array_unique(), get_option(), _prime_post_caches(), class(), sprintf() |
| always | 67–78 | number(), apply_filters(), wp_list_pluck(), array_unique(), get_option(), _prime_post_caches(), class(), __(), sprintf() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 226 | 45–78 | 20 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 228 | 45–78 | 20 | |
| 8.4 | 228 | 45–78 | 20 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 234 | 48–84 | 20 | |
| 8.2 | 234 | 48–84 | 20 | |
| 8.1 | 234 | 48–84 | 20 | |
| 7.4 | 234 | 48–84 | 20 |
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:
- apply_filters( widget_comments_args )filterline 55 (+10 into the body)
Filters the arguments for the Recent Comments widget.
Uses · 21
- get_comments()Retrieves a list of comments.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_list_pluck()Plucks a certain field out of each object or array in an array.
- _prime_post_caches()Adds any posts from the given IDs to the cache that do not already exist in cache.
- get_option()Retrieves an option value based on an option name.
- get_avatar()Retrieves the avatar `` tag for a user, email address, MD5 hash, comment, or post.
- get_comment_author_url()Retrieves the URL of the author of the current comment, not linked.
- get_author_posts_url()Retrieves the URL to the author page for the user with the ID provided.
- esc_url()Checks and cleans a URL.
- get_comment_author()Retrieves the author of the current comment.
- get_comment_link()Retrieves the link to a given comment.
- wp_latest_comments_draft_or_post_title()Get the post title.
Show all 21
- __()Retrieves the translation of $text.
- esc_attr()Escaping for HTML attributes.
- get_comment_date()Retrieves the comment date of the current comment.
- date_i18n()Retrieves the date in localized format, based on a sum of Unix timestamp and timezone offset in seconds.
- post_password_required()Determines whether the post requires password and whether a correct password has been provided.
- get_comment_text()Retrieves the text of the current comment.
- wpautop()Replaces double line breaks with paragraph elements.
- get_comment_excerpt()Retrieves the excerpt of the given comment.
- get_block_wrapper_attributes()Generates a string of attributes by applying to the current block being rendered all of the features that the block supports.
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.
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.