Skip to content

Instantly share code, notes, and snippets.

@haqu
Created May 7, 2010 03:33
Show Gist options
  • Save haqu/393013 to your computer and use it in GitHub Desktop.
Save haqu/393013 to your computer and use it in GitHub Desktop.
Recent comments for iStudio theme
<?php
/**
* Plugin Name: iStudio Recent Comments
* Version: 1.0
* Plugin URI: http://gist.github.com/393013
* Description: Recent comments for iStudio theme
* Author: Sergey Tikhonov
* Author URI: http://haqu.net
*/
class WP_Widget_iStudio_Recent_Comments extends WP_Widget {
function WP_Widget_iStudio_Recent_Comments() {
$widget_ops = array(
'classname' => 'widget_istudio_recent_comments',
'description' => __( 'The most recent comments for iStudio theme' ) );
$this->WP_Widget('istudio-recent-comments', __('iStudio Recent Comments'), $widget_ops);
$this->alt_option_name = 'widget_istudio_recent_comments';
add_action( 'comment_post', array(&$this, 'flush_widget_cache') );
add_action( 'transition_comment_status', array(&$this, 'flush_widget_cache') );
}
function flush_widget_cache() {
wp_cache_delete('istudio_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']);
if ( !$number = (int) $instance['number'] )
$number = 5;
else if ( $number < 1 )
$number = 1;
else if ( $number > 15 )
$number = 15;
if ( !$comments = wp_cache_get( 'istudio_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( 'istudio_recent_comments', $comments, 'widget' );
}
$comments = array_slice( (array) $comments, 0, $number );
?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<ul><?php
$wpchres = get_option('blog_charset');
if ( $comments ) : foreach ( (array) $comments as $comment) :
echo '<li class="comment">' . sprintf(_x('%1$s: %2$s','widgets'), '<a href="' . esc_url(get_comment_link($comment->comment_ID)) . '">' . get_comment_author() . '</a>', mb_substr($comment->comment_content,0,15,$wpchres) . '&hellip;') . '</li>';
endforeach; endif;?></ul>
<?php echo $after_widget; ?>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = (int) $new_instance['number'];
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['widget_istudio_recent_comments']) )
delete_option('widget_istudio_recent_comments');
return $instance;
}
function form( $instance ) {
$title = isset($instance['title']) ? esc_attr($instance['title']) : '';
$number = isset($instance['number']) ? absint($instance['number']) : 5;
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
<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
}
}
// Register widget
add_action('widgets_init', create_function('', 'return register_widget("WP_Widget_iStudio_Recent_Comments");'));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment