<?php
   add_action('widgets_init', 'tigu_social_counter_load_widgets');
   function tigu_social_counter_load_widgets()
   {
      register_widget('Tigu_Social_Counter_Widget');
   }
      class Tigu_Social_Counter_Widget extends WP_Widget {
   function Tigu_Social_Counter_Widget()
   {
      $widget_ops = array(
     'classname' => 'tigu_social_counter',
     'description' => 'Show the number of Twitter followers, Facebook fans, RSS Feed and other site statistics.' );
      $control_ops = array(
     'id_base' => 'tigu_social_counter-widget');
      $this->WP_Widget('tigu_social_counter-widget', 'GAMELEON: Social Counter & Statistics', $widget_ops, $control_ops);
   }
   function widget($args, $instance)
   {
      extract($args);

/* User selected settings. */
      $title = $instance['title'];
      $tigu_facebook_id = $instance['tigu_facebook_id'];
      $tigu_feedburner = $instance['tigu_feedburner'];
	  $tigu_count_facebook = isset($instance['tigu_count_facebook']) ? 'true' : 'false';
	  $tigu_count_twitter = isset($instance['tigu_count_twitter']) ? 'true' : 'false';
	  $tigu_statistics_rss = isset($instance['tigu_statistics_rss']) ? 'true' : 'false';
	  $tigu_statistics_comments = isset($instance['tigu_statistics_comments']) ? 'true' : 'false';
	  $tigu_statistics_players = isset($instance['tigu_statistics_players']) ? 'true' : 'false';
	  $tigu_statistics_games = isset($instance['tigu_statistics_games']) ? '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;
      if($title) {
      echo $before_title.$title.$after_title;
   }
?>

<!-- START WIDGET -->
      <ul id="social-counters">
<?php if($tigu_count_facebook == 'true'): ?>
 
<?php
   function facebook_count($value='')
{
 $url='http://api.facebook.com/method/fql.query?query=SELECT fan_count FROM page WHERE';
 if(is_numeric($value)) { $qry=' page_id="'.$value.'"';} //If value is a page ID
 else {$qry=' username="'.$value.'"';} //If value is not a ID.
 $xml = @simplexml_load_file($url.$qry) or die ("invalid operation");
 $facebook_count = $xml->page->fan_count;
 return $facebook_count;
}
?>


      <li id="counter-facebook">
      <a target="_blank" href='https://www.facebook.com/<?php echo $tigu_facebook_id; ?>'>
<?php echo facebook_count($tigu_facebook_id);?> <?php _e('Facebook Fans', 'Gameleon'); ?>
      </a>
      </li>

<?php endif; ?>
<?php if($tigu_count_twitter == 'true'): ?>


<?php 

function tigu_followers_count() {
	require_once(TEMPLATEPATH . '/includes/plugins/jnewsticker/vendor/twitteroauth/twitteroauth.php');
	try {
		$tigu_twitter_id 		= get_option('tigu_twitter_id');
		$consumer_key 			= get_option('tigu_twitter_consumer_key');
		$consumer_secret		= get_option('tigu_twitter_consumer_secret');
		$access_token 			= get_option('tigu_twitter_access_token');
		$access_token_secret 	= get_option('tigu_twitter_access_token_secret');
		
		$twitterConnection = new TwitterOAuth( 
        $consumer_key , 
        $consumer_secret , 
        $access_token , 
        $access_token_secret );
		$twitterData = $twitterConnection->get(
        'users/show', 
        array(
        'screen_name' => $tigu_twitter_id
         )
        );
		$twitter['page_url'] = 'http://www.twitter.com/'.$tigu_twitter_id;
		$twitter['followers_count'] = $twitterData->followers_count;;
	} catch (Exception $e) {
		$twitter['page_url'] = 'http://www.twitter.com/'.$tigu_twitter_id;
		$twitter['followers_count'] = 0;
	}
	if( get_option( 'followers_count') < $twitter['followers_count'] )
		update_option( 'followers_count' , $twitter['followers_count'] );
		
	if( $twitter['followers_count'] == 0 && get_option( 'followers_count') )
		$twitter['followers_count'] = get_option( 'followers_count');
			
	elseif( $twitter['followers_count'] == 0 && !get_option( 'followers_count') )
		$twitter['followers_count'] = 0;
	
	return $twitter;
};

?>

      <li id="counter-twitter">
      <a target="_blank" href='http://twitter.com/<?php echo get_option('tigu_twitter_id'); ?>'>
<?php 
$twitter = tigu_followers_count(); ?>
<?php echo @number_format($twitter['followers_count']) ?>				
 <?php _e('Twitter Followers', 'Gameleon'); ?>
      </a>
      </li>
<?php endif; ?>

<?php if($tigu_statistics_rss == 'true'): ?>

 <li id="counter-rss">
      <a target="_blank" href='<?php if ($instance['tigu_feedburner']) { echo $instance['tigu_feedburner']; } else { bloginfo( 'rss2_url' ); } ?>'>
<?php _e('Subscribe to our Feed', 'Gameleon'); ?></a>
      </li>
 <?php endif; ?>

<?php if($tigu_statistics_comments == 'true'): ?>

      <li id="counter-comments">

<p><?php _e('Total Comments', 'Gameleon'); ?>: 

<?php $comments_count = wp_count_comments(); echo  $comments_count->total_comments ;?>

</p>
      </li>
 <?php endif; ?>


<?php if($tigu_statistics_players == 'true'): ?>

<li id="counter-users">
<p>
<?php if( get_option('tigu_template_type') == 'WordPres Default'): ?>
<?php _e('Total Members', 'Gameleon'); ?>
<?php else: ?>
<?php _e('Total Players', 'Gameleon'); ?>
<?php endif; ?>: <?php $result = count_users(); echo $result['total_users']; ?></p>
</li>
 <?php endif; ?>

<?php if($tigu_statistics_games == 'true'): ?>

<?php if( get_option('tigu_template_type') == 'WordPres Default'): ?>
 <li id="counter-posts">
<?php else: ?>
 <li id="counter-games">
<?php endif; ?>
<p>
<?php if( get_option('tigu_template_type') == 'WordPres Default'): ?>
<?php _e('Total Posts', 'Gameleon'); ?>
<?php else: ?>
<?php _e('Total Games', 'Gameleon'); ?>
<?php endif; ?>: 
<?php echo wp_count_posts()->publish; ?>
</p>
      </li>
 <?php endif; ?>

      </ul>



<!-- END WIDGET -->

<?php
      echo $after_widget;
   }
   }
   function update($new_instance, $old_instance)
   {
      $instance = $old_instance;
      $instance['tigu_facebook_id'] = strip_tags($new_instance['tigu_facebook_id']);
      $instance['tigu_feedburner'] = strip_tags($new_instance['tigu_feedburner']);
      $instance['title'] = $new_instance['title'];
      $instance['tigu_count_facebook'] = $new_instance['tigu_count_facebook'];
      $instance['tigu_count_twitter'] = $new_instance['tigu_count_twitter'];
      $instance['tigu_statistics_rss'] = $new_instance['tigu_statistics_rss'];
      $instance['tigu_statistics_comments'] = $new_instance['tigu_statistics_comments'];
      $instance['tigu_statistics_players'] = $new_instance['tigu_statistics_players'];
      $instance['tigu_statistics_games'] = $new_instance['tigu_statistics_games'];
      $instance['show_home'] = $new_instance['show_home'];
      $instance['show_page'] = $new_instance['show_page'];
	  $instance['show_single'] = $new_instance['show_single'];
      return $instance;
      }
   function form($instance)
   {
      $defaults = array(
     'title' => 'Statistics',
     'tigu_facebook_id' => '',
	 'tigu_count_facebook' => 'on',
	 'tigu_count_twitter' => 'on',
	 'tigu_statistics_rss' => 'on',
	 'tigu_statistics_comments' => 'on',
	 'tigu_statistics_players' => 'on',
	 'tigu_statistics_games' => 'on',
     'tigu_feedburner' => '',
	 'show_home' => 'on',
     'show_single' => 'on',
     'show_page' => '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" type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" />
      </p>


	<p>
    <input class="checkbox" type="checkbox" <?php checked($instance['tigu_count_facebook'], 'on'); ?> id="<?php echo $this->get_field_id('tigu_count_facebook'); ?>" name="<?php echo $this->get_field_name('tigu_count_facebook'); ?>" />
    <label for="<?php echo $this->get_field_id('tigu_count_facebook'); ?>">Show Facebook Fans Counter</label>
    </p>
    <p>
    <input class="checkbox" type="checkbox" <?php checked($instance['tigu_count_twitter'], 'on'); ?> id="<?php echo $this->get_field_id('tigu_count_twitter'); ?>" name="<?php echo $this->get_field_name('tigu_count_twitter'); ?>" />
    <label for="<?php echo $this->get_field_id('tigu_count_twitter'); ?>">Show Twitter Followers Counter</label>
    </p>
    <p>
    <input class="checkbox" type="checkbox" <?php checked($instance['tigu_statistics_rss'], 'on'); ?> id="<?php echo $this->get_field_id('tigu_statistics_rss'); ?>" name="<?php echo $this->get_field_name('tigu_statistics_rss'); ?>" />
    <label for="<?php echo $this->get_field_id('tigu_statistics_rss'); ?>">Show RSS Feed Counter</label>
     </p>
    <p>
    <input class="checkbox" type="checkbox" <?php checked($instance['tigu_statistics_comments'], 'on'); ?> id="<?php echo $this->get_field_id('tigu_statistics_comments'); ?>" name="<?php echo $this->get_field_name('tigu_statistics_comments'); ?>" />
    <label for="<?php echo $this->get_field_id('tigu_statistics_comments'); ?>">Show Comments Counter</label>
     </p>
    <p>
    <input class="checkbox" type="checkbox" <?php checked($instance['tigu_statistics_players'], 'on'); ?> id="<?php echo $this->get_field_id('tigu_statistics_players'); ?>" name="<?php echo $this->get_field_name('tigu_statistics_players'); ?>" />
    <label for="<?php echo $this->get_field_id('tigu_statistics_players'); ?>">Show Total Members Counter</label>
     </p>
    <p>
    <input class="checkbox" type="checkbox" <?php checked($instance['tigu_statistics_games'], 'on'); ?> id="<?php echo $this->get_field_id('tigu_statistics_games'); ?>" name="<?php echo $this->get_field_name('tigu_statistics_games'); ?>" />
    <label for="<?php echo $this->get_field_id('tigu_statistics_games'); ?>">Show Total Posts Counter</label>
     </p>



<div style="border:1px solid #008EFF; padding:10px;"> 
	<p>
	<label for="<?php echo $this->get_field_id('tigu_facebook_id'); ?>"><strong>Facebook ID:</strong></label>
	<input class="widefat" type="text"  id="<?php echo $this->get_field_id('tigu_facebook_id'); ?>" name="<?php echo $this->get_field_name('tigu_facebook_id'); ?>" value="<?php echo $instance['tigu_facebook_id']; ?>" />
<br />
<small>*If your facebook page address is http://facebook.com/gameleon, your facebook id is <strong>gameleon</strong>. If your facebook page ID has numbers for e.x: http://facebook.com/gameleon/46327774632 then your facebook ID is <strong>46327774632</strong></small>

	</p>
</div><br />

<div style="border:1px solid #008EFF; padding:10px;"> 
	<label><strong>Twitter Followers:</strong></label>
<small><p>In order to display Twitter Followers Count, you should follow the steps below:</p></small>
<small>
<ol><li>Create <a style="text-decoration:none"; href="https://dev.twitter.com/apps" target="_blank"><span style="color:#fb2900">Twitter APP</span></a>. Read the theme documnetation for more info.</li>
<li>Setup the Twitter API OAuth settings under Gameleon options panel &raquo; Twitter API OAuth Settings.</li>
</ol>
</small>

</div>
<br />

<div style="border:1px solid #008EFF; padding:10px;"> 
	<p>
	<label for="<?php echo $this->get_field_id('tigu_feedburner'); ?>"><strong>Feedburner URL:</strong></label>
	<input class="widefat" type="text"  id="<?php echo $this->get_field_id('tigu_feedburner'); ?>" name="<?php echo $this->get_field_name('tigu_feedburner'); ?>" value="<?php echo $instance['tigu_feedburner']; ?>" />
	<br />
<small>*If left blank, will generate the default feed url, like http://www.yoursite.com/feed.</small>

</p>

</div><br />
	<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 }
}
?>