<?php
add_action('widgets_init', 'reedwan_social_counter_load_widgets');

function reedwan_social_counter_load_widgets()
{
	register_widget('Reedwan_Social_Counter_Widget');
}

class Reedwan_Social_Counter_Widget extends WP_Widget {
	
	function Reedwan_Social_Counter_Widget()
	{
		$widget_ops = array('classname' => 'reedwan_social_counter', 'description' => 'Show number of RSS subscribes, twitter followers and facebook fans.');

		$control_ops = array('id_base' => 'reedwan_social_counter-widget');

		$this->WP_Widget('reedwan_social_counter-widget', 'Backstreet : Social Counter', $widget_ops, $control_ops);
	}
	
	function widget($args, $instance)
	{
		extract($args);
		
	
		?>
		<!-- BEGIN WIDGET -->
		<?php
		if(get_option('reedwan_feedburner')) {
			$rss = get_option('reedwan_feedburner');
		} else {
			$rss = get_bloginfo('rss2_url');
		}
		?>
		<div class="social-counter-wrapper">
		<div class="social-counter rss-count">
			<a href='<?php echo $rss; ?>'></a>
			<?php
			if(get_option('reedwan_feedburner')) {
				$interval = 43200;
				
				if($_SERVER['REQUEST_TIME'] > get_option('reedwan_feedburner_cache_time')) {
					@$api = wp_remote_get('http://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=' . get_option('reedwan_feedburner'));
					@$xml = new SimpleXmlElement($api['body'], LIBXML_NOCDATA);
					@$feedburner_followers = (string) $xml->feed->entry['circulation'];
					
					update_option('reedwan_feedburner_cache_time', $_SERVER['REQUEST_TIME'] + $interval);
					update_option('reedwan_feedburner_followers', $feedburner_followers);
				}
			}
			?>
			
			
			<div class="social-count"><?php echo get_option('reedwan_feedburner_followers'); ?></div>
			<div class="social-descrip"><?php _e('Subscribers', 'Backstreet'); ?></div>
			
		</div>
		<?php if(get_option('reedwan_twitter_id')): ?>
		<div class="social-counter twitter-count">
			<a href='http://twitter.com/<?php echo get_option('reedwan_twitter_id'); ?>'></a>
			<?php
			$interval = 3600;
			
			if($_SERVER['REQUEST_TIME'] > get_option('reedwan_twitter_cache_time')) {
				@$api = wp_remote_get('http://twitter.com/statuses/user_timeline/' . get_option('reedwan_twitter_id') . '.json');
				@$json = json_decode($api['body']);
				
				update_option('reedwan_twitter_cache_time', $_SERVER['REQUEST_TIME'] + $interval);
				update_option('reedwan_twitter_followers', $json[0]->user->followers_count);
			}
			?>
			<div class="social-count"><?php echo get_option('reedwan_twitter_followers'); ?></div>
			<div class="social-descrip"><?php _e('Followers', 'Backstreet'); ?></div>
		</div>
		<?php endif; ?>
		<?php if(get_option('reedwan_facebook_id')): ?>
		<div class="social-counter facebook-count">
			<?php
			$interval = 3600;
			
			if($_SERVER['REQUEST_TIME'] > get_option('reedwan_facebook_cache_time')) {
				@$api = wp_remote_get('http://graph.facebook.com/' . get_option('reedwan_facebook_id'));
				@$json = json_decode($api['body']);
				
				update_option('reedwan_facebook_cache_time', $_SERVER['REQUEST_TIME'] + $interval);
				update_option('reedwan_facebook_followers', $json->likes);
				update_option('reedwan_facebook_link', $json->link);
			}
			?>
			
			<a href='<?php echo get_option('reedwan_facebook_link'); ?>'></a>
			
			<div class="social-count"><?php echo get_option('reedwan_facebook_followers'); ?></div>
			<div class="social-descrip"><?php _e('Fans', 'Reedwan'); ?></div>
		</div>
		<?php endif; ?>
		</div>
		<!-- END WIDGET -->
		<?php

	}
	
	function update($new_instance, $old_instance)
	{
	}

	function form($instance)
	{
	}
}
?>