Make the recent comments widget use the get_comments() api and cache the result. Fixes #10615 props filosofo and blepoxp.

git-svn-id: http://svn.automattic.com/wordpress/trunk@14483 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
westi 2010-05-06 19:31:45 +00:00
parent 1e13b43af0
commit 62141ab55f
1 changed files with 37 additions and 27 deletions

View File

@ -588,7 +588,6 @@ class WP_Widget_Recent_Posts extends WP_Widget {
<p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label>
<input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /><br />
<small><?php _e('(at most 15)'); ?></small></p>
<?php
}
}
@ -618,36 +617,48 @@ class WP_Widget_Recent_Comments extends WP_Widget {
}
function flush_widget_cache() {
wp_cache_delete('recent_comments', 'widget');
wp_cache_delete('widget_recent_comments', 'widget');
}
function widget( $args, $instance ) {
global $wpdb, $comments, $comment;
extract($args, EXTR_SKIP);
$title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Comments') : $instance['title'], $instance, $this->id_base);
if ( !$number = (int) $instance['number'] )
$number = 5;
else if ( $number < 1 )
$number = 1;
else if ( $number > 15 )
$number = 15;
if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
$comments = $wpdb->get_results("SELECT $wpdb->comments.* FROM $wpdb->comments JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID WHERE comment_approved = '1' AND post_status = 'publish' ORDER BY comment_date_gmt DESC LIMIT 15");
wp_cache_add( 'recent_comments', $comments, 'widget' );
global $comments, $comment;
$cache = wp_cache_get('widget_recent_comments', 'widget');
if ( ! is_array( $cache ) )
$cache = array();
if ( isset( $cache[$args['widget_id']] ) ) {
echo $cache[$args['widget_id']];
return;
}
$comments = array_slice( (array) $comments, 0, $number );
?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<ul id="recentcomments"><?php
if ( $comments ) : foreach ( (array) $comments as $comment) :
echo '<li class="recentcomments">' . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x('%1$s on %2$s', 'widgets'), get_comment_author_link(), '<a href="' . esc_url( get_comment_link($comment->comment_ID) ) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
endforeach; endif;?></ul>
<?php echo $after_widget; ?>
<?php
extract($args, EXTR_SKIP);
$output = '';
$title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Comments') : $instance['title']);
if ( ! $number = (int) $instance['number'] )
$number = 5;
else if ( $number < 1 )
$number = 1;
$comments = get_comments(array('number' => $number));
$output .= $before_widget;
if ( $title )
$output .= $before_title . $title . $after_title;
$output .= '<ul id="recentcomments">';
if ( $comments ) {
foreach ( (array) $comments as $comment) {
$output .= '<li class="recentcomments">' . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x('%1$s on %2$s', 'widgets'), get_comment_author_link(), '<a href="' . esc_url( get_comment_link($comment->comment_ID) ) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
}
}
$output .= '</ul>';
$output .= $after_widget;
echo $output;
$cache[$args['widget_id']] = $output;
wp_cache_set('widget_recent_comments', $cache, 'widget');
}
function update( $new_instance, $old_instance ) {
@ -672,7 +683,6 @@ class WP_Widget_Recent_Comments extends WP_Widget {
<p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of comments to show:'); ?></label>
<input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /><br />
<small><?php _e('(at most 15)'); ?></small></p>
<?php
}
}