<?php
   add_action('widgets_init', 'tigu_recent_comments_load_widgets');
   function tigu_recent_comments_load_widgets()
   {
      register_widget('Tigu_Recent_Coments_Widget');
   }
      class Tigu_Recent_Coments_Widget extends WP_Widget {
   function Tigu_Recent_Coments_Widget()
   {
      $widget_ops = array(
     'classname' => 'tigu_recent_comments',
     'description' => 'Display recent WordPress comments only.' );
      $control_ops = array(
     'id_base' => 'tigu_recent_comments' );
      $this->WP_Widget('tigu_recent_comments', 'GAMELEON: Recent WordPress Comments', $widget_ops, $control_ops);
   }
   function widget($args, $instance)
   {
      extract($args);

/* User selected settings. */
      $title = $instance['title'];
      $posts = $instance['posts'];
      $comments = $instance['comments'];
      $show_comments = true;
      $show_avatar = isset($instance['show_avatar']) ? 'true' : 'false';
	  $show_home = isset($instance['show_home']) ? 'true' : 'false';
      $show_page = isset($instance['show_page']) ? 'true' : 'false';
      $show_single = isset($instance['show_single']) ? 'true' : 'false';
      if ( ( is_home() && $show_home == 'false')  ||  ( is_page() && $show_page == 'false' ) || ( is_single() && $show_single == 'false' )) {
        } else {
        echo $before_widget;
?>

<!-- START WIDGET -->

<?php
   if($title) {
   echo $before_title.$title.$after_title;
   }
?>
      <div class="grid-sidebar">
<?php if($show_comments == 'true'): ?>
<?php
      $number = $instance['comments'];
      global $wpdb;
      $recent_comments = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_author_email, comment_date_gmt, comment_approved, comment_type, comment_author_url, SUBSTRING(comment_content,1,110) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT $number";
      $the_comments = $wpdb->get_results($recent_comments);
      foreach ($the_comments as $comment) { ?>
      <div class="comm-widget">
<?php if($show_avatar == 'true'): ?>

<?php echo get_avatar($comment->comment_author_email, '50'); ?>
      <p style="font-weight:bold;"><?php echo strip_tags($comment->comment_author); ?> on <a class="comment-text-side" href="<?php echo get_permalink($comment->ID); ?>#comment-<?php echo $comment->comment_ID; ?>" title="<?php echo strip_tags($comment->comment_author); ?> on <?php echo $comment->post_title; ?>"><span class="titlecomm"><?php echo $comment->post_title; ?></span></a></p>
      <p><?php echo string_limit_words(strip_tags($comment->com_excerpt), 14); ?>...</p>


<?php else: ?>


      <span class="commicon"><?php echo strip_tags($comment->comment_author); ?></span> on <a class="comment-text-side" href="<?php echo get_permalink($comment->ID); ?>#comment-<?php echo $comment->comment_ID; ?>" title="<?php echo strip_tags($comment->comment_author); ?> on <?php echo $comment->post_title; ?>"><span class="titlecomm"><?php echo $comment->post_title; ?></span></a>
      <p><?php echo string_limit_words(strip_tags($comment->com_excerpt), 14); ?>...</p>


<?php endif; ?>

      </div>

<?php } ?>
<?php endif; ?>
      </div>

<!-- END WIDGET -->

<?php
      echo $after_widget;
   }
   }
   function update($new_instance, $old_instance)
   {
      $instance = $old_instance;
      $instance['title'] = $new_instance['title'];
      $instance['posts'] = $new_instance['posts'];
      $instance['show_home'] = $new_instance['show_home'];
      $instance['show_page'] = $new_instance['show_page'];
	  $instance['show_single'] = $new_instance['show_single'];
      $instance['comments'] = $new_instance['comments'];
      $instance['show_comments'] = $new_instance['show_comments'];
      $instance['show_avatar'] = $new_instance['show_avatar'];
   return $instance;
   }
   function form($instance)
   {
      $defaults = array(
     'title' => 'Recent Comments',
     'posts' => 3,
     'comments' => '3',
	 'show_home' => 'on',
     'show_page' => 'on',
     'show_single' => 'on',
     'show_comments' => 'on',
     'show_avatar' => 'on' );
      $instance = wp_parse_args((array) $instance, $defaults); ?>

<!-- START ADMIN WIDGETS AREA -->

      <p>
         <label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
         <input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" />
      </p>

      <p>
         <label for="<?php echo $this->get_field_id('comments'); ?>">Number of comments:</label>
         <input class="widefat" style="width: 30px;" id="<?php echo $this->get_field_id('comments'); ?>" name="<?php echo $this->get_field_name('comments'); ?>" value="<?php echo $instance['comments']; ?>" />
      </p>

      <p>
         <input class="checkbox" type="checkbox" <?php checked($instance['show_avatar'], 'on'); ?> id="<?php echo $this->get_field_id('show_avatar'); ?>" name="<?php echo $this->get_field_name('show_avatar'); ?>" />
         <label for="<?php echo $this->get_field_id('show_avatar'); ?>">Show Gravatar</label>
      </p>
	<p>
    <input class="checkbox" type="checkbox" <?php checked($instance['show_home'], 'on'); ?> id="<?php echo $this->get_field_id('show_home'); ?>" name="<?php echo $this->get_field_name('show_home'); ?>" />
    <label for="<?php echo $this->get_field_id('show_home'); ?>">Show on Home Page?</label>
    </p>
    <p>
    <input class="checkbox" type="checkbox" <?php checked($instance['show_page'], 'on'); ?> id="<?php echo $this->get_field_id('show_page'); ?>" name="<?php echo $this->get_field_name('show_page'); ?>" />
    <label for="<?php echo $this->get_field_id('show_page'); ?>">Show on Single Page?</label>
    </p>
    <p>
    <input class="checkbox" type="checkbox" <?php checked($instance['show_single'], 'on'); ?> id="<?php echo $this->get_field_id('show_single'); ?>" name="<?php echo $this->get_field_name('show_single'); ?>" />
    <label for="<?php echo $this->get_field_id('show_single'); ?>">Show on Single Post?</label>
    </p>
<!-- END ADMIN WIDGETS AREA -->

<?php }
}
?>