You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

179 lines
5.7 KiB

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