<?php
   add_action('widgets_init', 'tigu_popular_tags_load_widgets');
   function tigu_popular_tags_load_widgets()
   {
      register_widget('Tigu_Popular_Tags_Widget');
   }
      class Tigu_Popular_Tags_Widget extends WP_Widget {
   function Tigu_Popular_Tags_Widget()
   {
      $widget_ops = array(
     'classname' => 'tigu_popular_tags',
     'description' => 'Popular Tags Widget for sidebar or homepage' );
      $control_ops = array(
     'id_base' => 'tigu_popular_tags' );
      $this->WP_Widget('tigu_popular_tags', 'GAMELEON: Popular Tags Widget', $widget_ops, $control_ops);
   }
   function widget($args, $instance)
   {
      extract($args);

/* User selected settings. */
      $title = $instance['title'];
      $posts = $instance['posts'];
      $tags_count = $instance['tags'];
	  $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;
?>

<?php
   if($title) {
   echo $before_title.$title.$after_title;
   }
?>

<!-- START WIDGET -->

      <div class="grid-sidebar">
      <div class="content" style="margin-bottom:4px; margin-left:-1px">
      <div class="tag_clouds widget_tags">
      <div class='tag_cloud'>
<?php
      $tags = get_tags(array(
     'orderby' => 'count',
     'order' => 'DESC',
     'number' => $tags_count ));
      foreach ((array) $tags as $tag) {
?>
<?php echo '<a href="' . get_tag_link ($tag->term_id) . '" rel="tag">' . $tag->name . '</a>';	?>
<?php } ?>
      </div>
      </div>
      </div>
      </div>

<!-- END WIDGET -->


<?php
   echo $after_widget;
   }
   }
   function update($new_instance, $old_instance)
   {
      $instance = $old_instance;
      $instance['posts'] = $new_instance['posts'];
      $instance['title'] = $new_instance['title'];
      $instance['show_home'] = $new_instance['show_home'];
      $instance['show_page'] = $new_instance['show_page'];
	  $instance['show_single'] = $new_instance['show_single'];
      $instance['tags'] = $new_instance['tags'];
      return $instance;
   }
   function form($instance)
   {
      $defaults = array(
     'title' => 'Popular Tags',
     'tags' => 8,
	 'show_home' => 'on',
     'show_page' => 'on',
     'show_single' => 'on',
     'show_tags' =>  '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('tags'); ?>">Number of tags to show:</label>
         <input class="widefat" style="width: 30px;" id="<?php echo $this->get_field_id('tags'); ?>" name="<?php echo $this->get_field_name('tags'); ?>" value="<?php echo $instance['tags']; ?>" />
      </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 }
}
?>