<?php
add_action('widgets_init', 'pyre_social_counter_load_widgets');

function pyre_social_counter_load_widgets()
{
	register_widget('Pyre_Social_Counter_Widget');
}

class Pyre_Social_Counter_Widget extends WP_Widget {
	
	function Pyre_Social_Counter_Widget()
	{
		$widget_ops = array('classname' => 'pyre_social_counter', 'description' => 'Show number of RSS subscribes, twitter followers and facebook fans.');

		$control_ops = array('id_base' => 'pyre_social_counter-widget');

		$this->WP_Widget('pyre_social_counter-widget', 'Broadway: Social Counter', $widget_ops, $control_ops);
	}
	
	function widget($args, $instance)
	{
		extract($args);

		echo $before_widget;
		?>
		<?php
		if($instance['title']) {
			echo $before_title.$instance['title'].$after_title;
		}
		?>
		
		<?php if($instance['fb_id']): ?>
		<?php
		$interval = 43200;

		if($_SERVER['REQUEST_TIME'] > get_option('pyre_facebook_cache_time')) {
			@$api = wp_remote_get('http://graph.facebook.com/' . $instance['fb_id']);
			@$json = json_decode($api['body']);
			
			if($json->likes >= 1) {
				update_option('pyre_facebook_cache_time', $_SERVER['REQUEST_TIME'] + $interval);
				update_option('pyre_facebook_followers', $json->likes);
				update_option('pyre_facebook_link', $json->link);
			}
		}
		?>
		<?php endif; ?>

		<?php if($instance['twitter_id']): ?>
		<?php
		$interval = 43200;

		if($_SERVER['REQUEST_TIME'] > get_option('pyre_twitter_cache_time')) {
			@$api = wp_remote_get('http://twitter.com/statuses/user_timeline/' . $instance['twitter_id'] . '.json');
			@$json = json_decode($api['body']);
			
			if(@$api['headers']['x-ratelimit-remaining'] >= 1) {
				update_option('pyre_twitter_cache_time', $_SERVER['REQUEST_TIME'] + $interval);
				update_option('pyre_twitter_followers', $json[0]->user->followers_count);
			}
		}
		?>
		<?php endif; ?>

		<?php
		if($instance['feedb_url']) {
			$interval = 43200;
			
			if($_SERVER['REQUEST_TIME'] > get_option('pyre_feedburner_cache_time')) {
				@$api = wp_remote_get('http://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=' . $instance['feedb_url']);
				@$xml = new SimpleXmlElement($api['body'], LIBXML_NOCDATA);
				@$feedburner_followers = (string) $xml->feed->entry['circulation'];
				
				if($feedburner_followers >= 1) {
					update_option('pyre_feedburner_cache_time', $_SERVER['REQUEST_TIME'] + $interval);
					update_option('pyre_feedburner_followers', $feedburner_followers);
				}
			}
		}
		?>
		<?php
		$count = 0;
		if($instance) {
			foreach($instance as $i) {
				if($i) {
					$count++;
				}
			};
		}
		?>
		<ul class="social-sidebar social-sidebar-<?php echo $count; ?>">
			<?php if($instance['fb_id']): ?>
			<li class="facebook"><a href="<?php echo get_option('pyre_facebook_link'); ?>" title="Like on Facebook" class="fb"><span class="text">Like Us</span><span class="number"><?php echo number_format(get_option('pyre_facebook_followers')); ?> Fans</span></a></li>
			<?php endif; ?>
			<?php if($instance['twitter_id']): ?>
			<li class="twitter"><a href="http://twitter.com/<?php echo $instance['twitter_id']; ?>" title="Follow on Twitter" class="twitter rightcol"><span class="text">Follow Us</span><span class="number"><?php echo number_format(get_option('pyre_twitter_followers')); ?> Followers</span></a></li>
			<?php endif; ?>
			<?php
			if($instance['feedb_url']) {
				$rss = $instance['feedb_url'];
				$rss_text = number_format(get_option('pyre_feedburner_followers')) . ' Subscribers';
			} else {
				$rss = get_bloginfo('rss2_url');
				$rss_text = '';
			}
			?>
			<li class="rss"><a href="<?php echo $rss; ?>" title="Subscribe to RSS Feed" class="rss"><span class="text">Subscribe</span><span class="number"><?php echo $rss_text; ?></span></a></li>
		</ul>
		<?php
		echo $after_widget;
	}
	
	function update($new_instance, $old_instance)
	{
		$instance = $old_instance;

		$instance['title'] = $new_instance['title'];
		$instance['fb_id'] = $new_instance['fb_id'];
		$instance['twitter_id'] = $new_instance['twitter_id'];
		$instance['feedb_url'] = $new_instance['feedb_url'];

		return $instance;
	}

	function form($instance)
	{
		$defaults = array('title' => 'Follow & Subscribe', 'fb_id' => '', 'twitter_id' => '', 'feedb_url' => '');
		$instance = wp_parse_args((array) $instance, $defaults); ?>
		<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('fb_id'); ?>">Facebook ID:</label>
			<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('fb_id'); ?>" name="<?php echo $this->get_field_name('fb_id'); ?>" value="<?php echo $instance['fb_id']; ?>" />
		<small>If your facebook page URL is http://facebook.com/cocacola, your facebook id is 'coacola'. If your facebook page ID has numbers for e.g: http://facebook.com/cocacola/7846745634632 then your facebook ID is '7846745634632'.</small>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id('twitter_id'); ?>">Twitter ID:</label>
			<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('twitter_id'); ?>" name="<?php echo $this->get_field_name('twitter_id'); ?>" value="<?php echo $instance['twitter_id']; ?>" />
		</p>


		<p>
			<label for="<?php echo $this->get_field_id('feedb_url'); ?>">Feedburner URL:</label>
			<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('feedb_url'); ?>" name="<?php echo $this->get_field_name('feedb_url'); ?>" value="<?php echo $instance['feedb_url']; ?>" />
		</p>
	<?php }
}
?>