From ebbb23e25b20b0509c64cdc40abcd1880c98be49 Mon Sep 17 00:00:00 2001 From: Peter Westwood Date: Thu, 6 May 2010 19:31:45 +0000 Subject: [PATCH] Make the recent comments widget use the get_comments() api and cache the result. Fixes #10615 props filosofo and blepoxp. git-svn-id: https://develop.svn.wordpress.org/trunk@14483 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/default-widgets.php | 64 +++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/wp-includes/default-widgets.php b/wp-includes/default-widgets.php index 973d450735..7d06991a5b 100644 --- a/wp-includes/default-widgets.php +++ b/wp-includes/default-widgets.php @@ -588,7 +588,6 @@ class WP_Widget_Recent_Posts extends WP_Widget {


-

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 ); -?> - - - - - $number)); + $output .= $before_widget; + if ( $title ) + $output .= $before_title . $title . $after_title; + + $output .= ''; + $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 {


-