<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[14483] trunk/wp-includes/default-widgets.php:
Make the recent comments widget use the get_comments()
api and cache the result.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/14483">14483</a></dd>
<dt>Author</dt> <dd>westi</dd>
<dt>Date</dt> <dd>2010-05-06 19:31:45 +0000 (Thu, 06 May 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Make the recent comments widget use the get_comments() api and cache the result. Fixes <a href="http://trac.wordpress.org/ticket/10615">#10615</a> props filosofo and blepoxp.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesdefaultwidgetsphp">trunk/wp-includes/default-widgets.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesdefaultwidgetsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/default-widgets.php (14482 => 14483)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/default-widgets.php        2010-05-06 19:19:53 UTC (rev 14482)
+++ trunk/wp-includes/default-widgets.php        2010-05-06 19:31:45 UTC (rev 14483)
</span><span class="lines">@@ -588,7 +588,6 @@
</span><span class="cx">
</span><span class="cx">                 <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label>
</span><span class="cx">                 <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 />
</span><del>-                <small><?php _e('(at most 15)'); ?></small></p>
</del><span class="cx"> <?php
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="lines">@@ -618,36 +617,48 @@
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         function flush_widget_cache() {
</span><del>-                wp_cache_delete('recent_comments', 'widget');
</del><ins>+                wp_cache_delete('widget_recent_comments', 'widget');
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         function widget( $args, $instance ) {
</span><del>-                global $wpdb, $comments, $comment;
</del><ins>+                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;
+                }
</ins><span class="cx">
</span><del>-                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;
</del><ins>+                 extract($args, EXTR_SKIP);
+                 $output = '';
+                 $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Comments') : $instance['title']);
</ins><span class="cx">
</span><del>-                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' );
-                }
</del><ins>+                if ( ! $number = (int) $instance['number'] )
+                         $number = 5;
+                 else if ( $number < 1 )
+                         $number = 1;
</ins><span class="cx">
</span><del>-                $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
</del><ins>+                $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');
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         function update( $new_instance, $old_instance ) {
</span><span class="lines">@@ -672,7 +683,6 @@
</span><span class="cx">
</span><span class="cx">                 <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of comments to show:'); ?></label>
</span><span class="cx">                 <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 />
</span><del>-                <small><?php _e('(at most 15)'); ?></small></p>
</del><span class="cx"> <?php
</span><span class="cx">         }
</span><span class="cx"> }
</span></span></pre>
</div>
</div>
</body>
</html>