Widgets: Create a unique HTML ID for the <ul> element of Recent Comments widget if more than one instance is displayed on the page.

Props peterwilsoncc, audrasjb, birgire, justinahinon, mbrailer, desrosj.
Fixes #46747.
Built from https://develop.svn.wordpress.org/trunk@47371


git-svn-id: http://core.svn.wordpress.org/trunk@47158 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2020-02-25 19:29:06 +00:00
parent 00f110740b
commit 8b0a0c44aa
3 changed files with 13 additions and 2 deletions

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.4-beta2-47370'; $wp_version = '5.4-beta2-47371';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -34,6 +34,8 @@ class WP_Widget_Categories extends WP_Widget {
* Outputs the content for the current Categories widget instance. * Outputs the content for the current Categories widget instance.
* *
* @since 2.8.0 * @since 2.8.0
* @since 4.2.0 Creates a unique HTML ID for the `<select>` element
* if more than one instance is displayed on the page.
* *
* @staticvar bool $first_dropdown * @staticvar bool $first_dropdown
* *

View File

@ -66,12 +66,18 @@ class WP_Widget_Recent_Comments extends WP_Widget {
* Outputs the content for the current Recent Comments widget instance. * Outputs the content for the current Recent Comments widget instance.
* *
* @since 2.8.0 * @since 2.8.0
* @since 5.4.0 Creates a unique HTML ID for the `<ul>` element
* if more than one instance is displayed on the page.
*
* @staticvar bool $first_instance
* *
* @param array $args Display arguments including 'before_title', 'after_title', * @param array $args Display arguments including 'before_title', 'after_title',
* 'before_widget', and 'after_widget'. * 'before_widget', and 'after_widget'.
* @param array $instance Settings for the current Recent Comments widget instance. * @param array $instance Settings for the current Recent Comments widget instance.
*/ */
public function widget( $args, $instance ) { public function widget( $args, $instance ) {
static $first_instance = true;
if ( ! isset( $args['widget_id'] ) ) { if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id; $args['widget_id'] = $this->id;
} }
@ -116,7 +122,10 @@ class WP_Widget_Recent_Comments extends WP_Widget {
$output .= $args['before_title'] . $title . $args['after_title']; $output .= $args['before_title'] . $title . $args['after_title'];
} }
$output .= '<ul id="recentcomments">'; $recent_comments_id = ( $first_instance ) ? 'recentcomments' : "recentcomments-{$this->number}";
$first_instance = false;
$output .= '<ul id="' . esc_attr( $recent_comments_id ) . '">';
if ( is_array( $comments ) && $comments ) { if ( is_array( $comments ) && $comments ) {
// Prime cache for associated posts. (Prime post term cache if we need it for permalinks.) // 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' ) ); $post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );