Ticket #10615: 10615-4-no-output-buffering-updated.diff

File 10615-4-no-output-buffering-updated.diff, 3.1 KB (added by blepoxp, 3 years ago)

Refreshed. Doesn't use output buffering. Uses get_comments()

Line 
1Index: default-widgets.php
2===================================================================
3--- default-widgets.php (revision 14226)
4+++ default-widgets.php (working copy)
5@@ -618,36 +618,48 @@
6        }
7 
8        function flush_widget_cache() {
9-               wp_cache_delete('recent_comments', 'widget');
10+               wp_cache_delete('widget_recent_comments', 'widget');
11        }
12 
13        function widget( $args, $instance ) {
14-               global $wpdb, $comments, $comment;
15+               global $comments, $comment;
16+               
17+               $cache = wp_cache_get('widget_recent_comments', 'widget');
18 
19-               extract($args, EXTR_SKIP);
20-               $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Comments') : $instance['title'], $instance, $this->id_base);
21-               if ( !$number = (int) $instance['number'] )
22-                       $number = 5;
23-               else if ( $number < 1 )
24-                       $number = 1;
25-               else if ( $number > 15 )
26-                       $number = 15;
27-
28-               if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
29-                       $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");
30-                       wp_cache_add( 'recent_comments', $comments, 'widget' );
31+                       if ( ! is_array( $cache ) )
32+                       $cache = array();
33+               
34+               if ( isset( $cache[$args['widget_id']] ) ) {
35+                       echo 'cache '.$cache[$args['widget_id']];
36+                       return;
37                }
38 
39-               $comments = array_slice( (array) $comments, 0, $number );
40-?>
41-               <?php echo $before_widget; ?>
42-                       <?php if ( $title ) echo $before_title . $title . $after_title; ?>
43-                       <ul id="recentcomments"><?php
44-                       if ( $comments ) : foreach ( (array) $comments as $comment) :
45-                       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>';
46-                       endforeach; endif;?></ul>
47-               <?php echo $after_widget; ?>
48-<?php
49+               extract($args, EXTR_SKIP);
50+               $output = '';
51+               $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Comments') : $instance['title']);
52+
53+               if ( ! $number = (int) $instance['number'] )
54+                       $number = 5;
55+               else if ( $number < 1 )
56+                       $number = 1;
57+
58+               $comments = get_comments(array('number' => $number));
59+               $output .= $before_widget;
60+               if ( $title )
61+                       $output .= $before_title . $title . $after_title;
62+
63+               $output .= '<ul id="recentcomments">';
64+               if ( $comments ) {
65+                       foreach ( (array) $comments as $comment) {
66+                               $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>';
67+                       }
68+               }
69+               $output .= '</ul>';
70+               $output .= $after_widget;
71+
72+               echo $output;
73+               $cache[$args['widget_id']] = $output;
74+               wp_cache_set('widget_recent_comments', 'widget');
75        }
76 
77        function update( $new_instance, $old_instance ) {