/wp-includes/widgets/class-wp-widget-recent-comments.php

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