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.

git-svn-id: https://develop.svn.wordpress.org/trunk@47371 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-02-25 19:27:22 +00:00
parent 97d43fbff5
commit f852ca2cc1
2 changed files with 12 additions and 1 deletions

View File

@ -34,6 +34,8 @@ class WP_Widget_Categories extends WP_Widget {
* Outputs the content for the current Categories widget instance.
*
* @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
*

View File

@ -66,12 +66,18 @@ class WP_Widget_Recent_Comments extends WP_Widget {
* Outputs the content for the current Recent Comments widget instance.
*
* @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',
* 'before_widget', and 'after_widget'.
* @param array $instance Settings for the current Recent Comments widget instance.
*/
public function widget( $args, $instance ) {
static $first_instance = true;
if ( ! isset( $args['widget_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 .= '<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 ) {
// 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' ) );