class-wp-widget-recent-comments.php 6.9 KB

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