WordPress.org

Make WordPress Core

Changeset 14483


Ignore:
Timestamp:
05/06/10 19:31:45 (4 years ago)
Author:
westi
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/default-widgets.php

    r14374 r14483  
    589589        <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label> 
    590590        <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 /> 
    591         <small><?php _e('(at most 15)'); ?></small></p> 
    592591<?php 
    593592    } 
     
    619618 
    620619    function flush_widget_cache() { 
    621         wp_cache_delete('recent_comments', 'widget'); 
     620        wp_cache_delete('widget_recent_comments', 'widget'); 
    622621    } 
    623622 
    624623    function widget( $args, $instance ) { 
    625         global $wpdb, $comments, $comment; 
    626  
    627         extract($args, EXTR_SKIP); 
    628         $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Comments') : $instance['title'], $instance, $this->id_base); 
    629         if ( !$number = (int) $instance['number'] ) 
    630             $number = 5; 
    631         else if ( $number < 1 ) 
    632             $number = 1; 
    633         else if ( $number > 15 ) 
    634             $number = 15; 
    635  
    636         if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) { 
    637             $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"); 
    638             wp_cache_add( 'recent_comments', $comments, 'widget' ); 
    639         } 
    640  
    641         $comments = array_slice( (array) $comments, 0, $number ); 
    642 ?> 
    643         <?php echo $before_widget; ?> 
    644             <?php if ( $title ) echo $before_title . $title . $after_title; ?> 
    645             <ul id="recentcomments"><?php 
    646             if ( $comments ) : foreach ( (array) $comments as $comment) : 
    647             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>'; 
    648             endforeach; endif;?></ul> 
    649         <?php echo $after_widget; ?> 
    650 <?php 
     624        global $comments, $comment; 
     625         
     626        $cache = wp_cache_get('widget_recent_comments', 'widget'); 
     627         
     628        if ( ! is_array( $cache ) ) 
     629            $cache = array(); 
     630         
     631        if ( isset( $cache[$args['widget_id']] ) ) { 
     632            echo $cache[$args['widget_id']]; 
     633            return; 
     634        } 
     635 
     636        extract($args, EXTR_SKIP); 
     637        $output = ''; 
     638        $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Comments') : $instance['title']); 
     639 
     640        if ( ! $number = (int) $instance['number'] ) 
     641            $number = 5; 
     642        else if ( $number < 1 ) 
     643            $number = 1; 
     644 
     645        $comments = get_comments(array('number' => $number)); 
     646        $output .= $before_widget; 
     647        if ( $title )  
     648            $output .= $before_title . $title . $after_title; 
     649 
     650        $output .= '<ul id="recentcomments">'; 
     651        if ( $comments ) {  
     652            foreach ( (array) $comments as $comment) { 
     653                $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>'; 
     654            } 
     655        } 
     656        $output .= '</ul>'; 
     657        $output .= $after_widget; 
     658 
     659        echo $output; 
     660        $cache[$args['widget_id']] = $output; 
     661        wp_cache_set('widget_recent_comments', $cache, 'widget'); 
    651662    } 
    652663 
     
    673684        <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of comments to show:'); ?></label> 
    674685        <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 /> 
    675         <small><?php _e('(at most 15)'); ?></small></p> 
    676686<?php 
    677687    } 
Note: See TracChangeset for help on using the changeset viewer.