3 * Widget API: WP_Widget_Recent_Comments class
11 * Core class used to implement a Recent Comments widget.
17 class WP_Widget_Recent_Comments extends WP_Widget {
20 * Sets up a new Recent Comments widget instance.
25 public function __construct() {
26 $widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'Your site’s most recent comments.' ) );
27 parent::__construct('recent-comments', __('Recent Comments'), $widget_ops);
28 $this->alt_option_name = 'widget_recent_comments';
30 if ( is_active_widget(false, false, $this->id_base) )
31 add_action( 'wp_head', array($this, 'recent_comments_style') );
35 * Outputs the default styles for the Recent Comments widget.
40 public function recent_comments_style() {
42 * Filter the Recent Comments default widget styles.
46 * @param bool $active Whether the widget is active. Default true.
47 * @param string $id_base The widget ID.
49 if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876
50 || ! apply_filters( 'show_recent_comments_widget_style', true, $this->id_base ) )
53 <style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
58 * Outputs the content for the current Recent Comments widget instance.
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.
67 public function widget( $args, $instance ) {
68 if ( ! isset( $args['widget_id'] ) )
69 $args['widget_id'] = $this->id;
73 $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Comments' );
75 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
76 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
78 $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
83 * Filter the arguments for the Recent Comments widget.
87 * @see WP_Comment_Query::query() for information on accepted arguments.
89 * @param array $comment_args An array of arguments used to retrieve the recent comments.
91 $comments = get_comments( apply_filters( 'widget_comments_args', array(
93 'status' => 'approve',
94 'post_status' => 'publish'
97 $output .= $args['before_widget'];
99 $output .= $args['before_title'] . $title . $args['after_title'];
102 $output .= '<ul id="recentcomments">';
103 if ( is_array( $comments ) && $comments ) {
104 // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)
105 $post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );
106 _prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false );
108 foreach ( (array) $comments as $comment ) {
109 $output .= '<li class="recentcomments">';
110 /* translators: comments widget: 1: comment author, 2: post link */
111 $output .= sprintf( _x( '%1$s on %2$s', 'widgets' ),
112 '<span class="comment-author-link">' . get_comment_author_link( $comment ) . '</span>',
113 '<a href="' . esc_url( get_comment_link( $comment ) ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a>'
119 $output .= $args['after_widget'];
125 * Handles updating settings for the current Recent Comments widget instance.
130 * @param array $new_instance New settings for this instance as input by the user via
132 * @param array $old_instance Old settings for this instance.
133 * @return array Updated settings to save.
135 public function update( $new_instance, $old_instance ) {
136 $instance = $old_instance;
137 $instance['title'] = sanitize_text_field( $new_instance['title'] );
138 $instance['number'] = absint( $new_instance['number'] );
143 * Outputs the settings form for the Recent Comments widget.
148 * @param array $instance Current settings.
150 public function form( $instance ) {
151 $title = isset( $instance['title'] ) ? $instance['title'] : '';
152 $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
154 <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
155 <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>
157 <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of comments to show:' ); ?></label>
158 <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>
163 * Flushes the Recent Comments widget cache.
168 * @deprecated 4.4.0 Fragment caching was removed in favor of split queries.
170 public function flush_widget_cache() {
171 _deprecated_function( __METHOD__, '4.4' );