WP_Widget_Recent_Comments

Core class used to implement a Recent Comments widget.

Defined (1)

The class is defined in the following location(s).

/wp-includes/widgets/class-wp-widget-recent-comments.php  
  1. class WP_Widget_Recent_Comments extends WP_Widget { 
  2.  
  3. /** 
  4. * Sets up a new Recent Comments widget instance. 
  5. * @since 2.8.0 
  6. * @access public 
  7. */ 
  8. public function __construct() { 
  9. $widget_ops = array( 
  10. 'classname' => 'widget_recent_comments',  
  11. 'description' => __( 'Your site’s most recent comments.' ),  
  12. 'customize_selective_refresh' => true,  
  13. ); 
  14. parent::__construct( 'recent-comments', __( 'Recent Comments' ), $widget_ops ); 
  15. $this->alt_option_name = 'widget_recent_comments'; 
  16.  
  17. if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { 
  18. add_action( 'wp_head', array( $this, 'recent_comments_style' ) ); 
  19.  
  20. /** 
  21. * Outputs the default styles for the Recent Comments widget. 
  22. * @since 2.8.0 
  23. * @access public 
  24. */ 
  25. public function recent_comments_style() { 
  26. /** 
  27. * Filters the Recent Comments default widget styles. 
  28. * @since 3.1.0 
  29. * @param bool $active Whether the widget is active. Default true. 
  30. * @param string $id_base The widget ID. 
  31. */ 
  32. if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876 
  33. || ! apply_filters( 'show_recent_comments_widget_style', true, $this->id_base ) ) 
  34. return; 
  35. ?> 
  36. <style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style> 
  37. <?php 
  38.  
  39. /** 
  40. * Outputs the content for the current Recent Comments widget instance. 
  41. * @since 2.8.0 
  42. * @access public 
  43. * @param array $args Display arguments including 'before_title', 'after_title',  
  44. * 'before_widget', and 'after_widget'. 
  45. * @param array $instance Settings for the current Recent Comments widget instance. 
  46. */ 
  47. public function widget( $args, $instance ) { 
  48. if ( ! isset( $args['widget_id'] ) ) 
  49. $args['widget_id'] = $this->id; 
  50.  
  51. $output = ''; 
  52.  
  53. $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Comments' ); 
  54.  
  55. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ 
  56. $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); 
  57.  
  58. $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5; 
  59. if ( ! $number ) 
  60. $number = 5; 
  61.  
  62. /** 
  63. * Filters the arguments for the Recent Comments widget. 
  64. * @since 3.4.0 
  65. * @see WP_Comment_Query::query() for information on accepted arguments. 
  66. * @param array $comment_args An array of arguments used to retrieve the recent comments. 
  67. */ 
  68. $comments = get_comments( apply_filters( 'widget_comments_args', array( 
  69. 'number' => $number,  
  70. 'status' => 'approve',  
  71. 'post_status' => 'publish' 
  72. ) ) ); 
  73.  
  74. $output .= $args['before_widget']; 
  75. if ( $title ) { 
  76. $output .= $args['before_title'] . $title . $args['after_title']; 
  77.  
  78. $output .= '<ul id="recentcomments">'; 
  79. if ( is_array( $comments ) && $comments ) { 
  80. // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.) 
  81. $post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) ); 
  82. _prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false ); 
  83.  
  84. foreach ( (array) $comments as $comment ) { 
  85. $output .= '<li class="recentcomments">'; 
  86. /** translators: comments widget: 1: comment author, 2: post link */ 
  87. $output .= sprintf( _x( '%1$s on %2$s', 'widgets' ),  
  88. '<span class="comment-author-link">' . get_comment_author_link( $comment ) . '</span>',  
  89. '<a href="' . esc_url( get_comment_link( $comment ) ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a>' 
  90. ); 
  91. $output .= '</li>'; 
  92. $output .= '</ul>'; 
  93. $output .= $args['after_widget']; 
  94.  
  95. echo $output; 
  96.  
  97. /** 
  98. * Handles updating settings for the current Recent Comments widget instance. 
  99. * @since 2.8.0 
  100. * @access public 
  101. * @param array $new_instance New settings for this instance as input by the user via 
  102. * WP_Widget::form(). 
  103. * @param array $old_instance Old settings for this instance. 
  104. * @return array Updated settings to save. 
  105. */ 
  106. public function update( $new_instance, $old_instance ) { 
  107. $instance = $old_instance; 
  108. $instance['title'] = sanitize_text_field( $new_instance['title'] ); 
  109. $instance['number'] = absint( $new_instance['number'] ); 
  110. return $instance; 
  111.  
  112. /** 
  113. * Outputs the settings form for the Recent Comments widget. 
  114. * @since 2.8.0 
  115. * @access public 
  116. * @param array $instance Current settings. 
  117. */ 
  118. public function form( $instance ) { 
  119. $title = isset( $instance['title'] ) ? $instance['title'] : ''; 
  120. $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5; 
  121. ?> 
  122. <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 
  123. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p> 
  124.  
  125. <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of comments to show:' ); ?></label> 
  126. <input class="tiny-text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="number" step="1" min="1" value="<?php echo $number; ?>" size="3" /></p> 
  127. <?php 
  128.  
  129. /** 
  130. * Flushes the Recent Comments widget cache. 
  131. * @since 2.8.0 
  132. * @access public 
  133. * @deprecated 4.4.0 Fragment caching was removed in favor of split queries. 
  134. */ 
  135. public function flush_widget_cache() { 
  136. _deprecated_function( __METHOD__, '4.4.0' );