WP_Widget_Recent_Comments::widget( array $args, array $instance )
- Since
- 2.8.0, 5.4.0
- Source
wp-includes/widgets/class-wp-widget-recent-comments.php:77
Compatibility
- WordPress
- since 5.4.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
$argsarray- Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
$instancearray- Settings for the current Recent Comments widget instance.
Performance profile
How much work a call to WP_Widget_Recent_Comments::widget() 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
- 77–132
- Plugin surface
- 3 hooks
- 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 171.
Third-party callbacks on 'widget_title', 'widget_comments_args', 'navigation_widgets_format' 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
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_comments()called directly - optionoption read or write
get_option()called directly - cacheobject cache
wp_cache_get()one call below WP_Widget_Recent_Comments::widget() - serializeserialisation
maybe_unserialize()one call below WP_Widget_Recent_Comments::widget()
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 · 8 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Widget_Recent_Comments::widget() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($instance) && $format !== "html5" | 77–90 | __(), apply_filters(), number(), apply_filters(), current_theme_supports(), apply_filters(), esc_attr() |
!empty($instance) && $format !== "html5" | 83–96 | __(), apply_filters(), absint(), number(), apply_filters(), current_theme_supports(), apply_filters(), esc_attr() |
empty($instance) && $format === "html5" | 91–105 | __(), apply_filters(), number(), apply_filters(), current_theme_supports(), apply_filters(), strip_tags(), esc_attr(), esc_attr() |
!empty($instance) && $format === "html5" | 97–111 | __(), apply_filters(), absint(), number(), apply_filters(), current_theme_supports(), apply_filters(), strip_tags(), esc_attr(), esc_attr() |
empty($instance) && $format !== "html5" && is_array($comments) | 98–111 | __(), apply_filters(), number(), apply_filters(), current_theme_supports(), apply_filters(), esc_attr(), wp_list_pluck(), array_unique(), get_option(), _prime_post_caches() |
!empty($instance) && $format !== "html5" && is_array($comments) | 104–117 | __(), apply_filters(), absint(), number(), apply_filters(), current_theme_supports(), apply_filters(), esc_attr(), wp_list_pluck(), array_unique(), get_option(), _prime_post_caches() |
empty($instance) && $format === "html5" && is_array($comments) | 112–126 | __(), apply_filters(), number(), apply_filters(), current_theme_supports(), apply_filters(), strip_tags(), esc_attr(), esc_attr(), wp_list_pluck(), array_unique(), get_option(), _prime_post_caches() |
!empty($instance) && $format === "html5" && is_array($comments) | 118–132 | __(), apply_filters(), absint(), number(), apply_filters(), current_theme_supports(), apply_filters(), strip_tags(), esc_attr(), esc_attr(), wp_list_pluck(), array_unique(), get_option(), _prime_post_caches() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 170 | 77–131 | 14 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 171 | 77–132 | 14 | |
| 8.4 | 171 | 77–132 | 14 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 176 | 77–137 | 14 | |
| 8.2 | 176 | 77–137 | 14 | |
| 8.1 | 176 | 77–137 | 14 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 178 | 77–139 | 14 |
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 · 3
3 hooks fire while WP_Widget_Recent_Comments::widget() runs, in this order:
- apply_filters( widget_comments_args )filterline 109 (+32 into the body)
Filters the arguments for the Recent Comments widget.
- apply_filters( navigation_widgets_format )filterline 131 (+54 into the body)
Filters the HTML format of widgets with navigation links.
Uses · 14
- __()Retrieves the translation of $text.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- absint()Converts a value to non-negative integer.
- get_comments()Retrieves a list of comments.
- current_theme_supports()Checks a theme's support for a given feature.
- esc_attr()Escaping for HTML attributes.
- 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.
- _x()Retrieves translated string with gettext context.
- get_comment_author_link()Retrieves the HTML link to the URL of the author of the current comment.
- esc_url()Checks and cleans a URL.
Show all 14
- get_comment_link()Retrieves the link to a given comment.
- get_the_title()Retrieves the post title.
Source code
public function widget( $args, $instance ) { static $first_instance = true; if ( ! isset( $args['widget_id'] ) ) { $args['widget_id'] = $this->id; } $output = ''; $default_title = __( 'Recent Comments' ); $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : $default_title; /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5; if ( ! $number ) { $number = 5; } $comments = get_comments( /** * Filters the arguments for the Recent Comments widget. * * @since 3.4.0 * @since 4.9.0 Added the `$instance` parameter. * * @see WP_Comment_Query::query() for information on accepted arguments. * * @param array $comment_args An array of arguments used to retrieve the recent comments. * @param array $instance Array of settings for the current widget. */ apply_filters( 'widget_comments_args', array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish', ), $instance ) ); $output .= $args['before_widget']; if ( $title ) { $output .= $args['before_title'] . $title . $args['after_title']; } $recent_comments_id = ( $first_instance ) ? 'recentcomments' : "recentcomments-{$this->number}"; $first_instance = false; $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml'; /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */ $format = apply_filters( 'navigation_widgets_format', $format ); if ( 'html5' === $format ) { // The title may be filtered: Strip out HTML and make sure the aria-label is never empty. $title = trim( strip_tags( $title ) ); $aria_label = $title ? $title : $default_title; $output .= '<nav aria-label="' . esc_attr( $aria_label ) . '">'; } $output .= '<ul id="' . esc_attr( $recent_comments_id ) . '">'; if ( is_array( $comments ) && $comments ) { // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.) $post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) ); _prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false ); foreach ( (array) $comments as $comment ) { $output .= '<li class="recentcomments">'; $output .= sprintf( /* translators: Comments widget. 1: Comment author, 2: Post link. */ _x( '%1$s on %2$s', 'widgets' ), '<span class="comment-author-link">' . get_comment_author_link( $comment ) . '</span>', '<a href="' . esc_url( get_comment_link( $comment ) ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a>' ); $output .= '</li>'; } }Changelog
Introduced in 2.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
<ul> element if more than one instance is displayed on the page.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/widgets/class-wp-widget-recent-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.