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