<?php   

/* 
Plugin Name: About us
Description: About us widget 
Version: 1.0 
Author: Stefan Naumovski 
*/    

add_action( 'widgets_init', 'social_widget' );

function social_widget() {register_widget( 'social_widget_t1t' );}

class social_widget_t1t extends WP_Widget {
	
	/* Register widget with WordPress. */
	
	function __construct() {
		parent::__construct(
			'social_t1t', // Widget ID
			'Social Widget', // Name
			array( 'description' => '', ) // Args
			);}
		
		/* Front-end display of widget. */
		
		public 	function widget($args, $instance){
			
		$defaults = array('title' => 'Social Widget', 'widget_style' => 'horizontal-social', 'facebook_page_url' => '', 'gplus_id' => '', 'yt_id' => '', 'facebook_text' => 'Facebook', 'twitter_text' => 'Twitter', 'gplus_text' => 'Google', 'gplus_api'=>'', 'yt_text' => 'Youtube', 'twitter_id' => '', 'consumer_key' => '', 'consumer_secret' => '', 'access_token' => '', 'access_token_secret' => '', 'instagram_id' => '', 'instagram_token' => '',  'instagram_text' => 'Instagram', 'pinterest_text' =>'Pinterest', 'pinterest' => '');
		$instance = wp_parse_args( (array) $instance, $defaults );

		$title		= $instance['title'];
		$widget_style	= $instance['widget_style'];
		$facebook 	= $instance['facebook_page_url'];		
		$gplus 		= $instance['gplus_id'];
		$gplus_api 		= $instance['gplus_api'];
		$yt 		= $instance['yt_id'];		
		$facebook_text 	= $instance['facebook_text'];
		$twitter_text 	= $instance['twitter_text'];
		$gplus_text 	= $instance['gplus_text'];
		$yt_text 		= $instance['yt_text'];
		$twitter_id 			= $instance['twitter_id'];
		$consumer_key 			= $instance['consumer_key'];
		$consumer_secret 		= $instance['consumer_secret'];
		$access_token 			= $instance['access_token'];
		$access_token_secret 	= $instance['access_token_secret'];		
		$instagram_id 		= $instance['instagram_id'];
		$instagram_token 	= $instance['instagram_token'];
		$instagram_text 	= $instance['instagram_text'];		
		$pinterest 			= $instance['pinterest'];
		$pinterest_text 	= $instance['pinterest_text'];	
		
		
		$args['before_widget'] = str_replace('class="home-widget', 'class="home-widget one-part' , $args['before_widget']);			
		echo $args['before_widget'];
			if ( ! empty( $title ) )
				echo $args['before_title'] . $title . $args['after_title'];
		
		
		
		?>        
		<div class="social-widget <?php echo $widget_style;?>">
			<ul>   
				<?php if(!empty($facebook)){ ?>               
					<li>
						<a href="<?php echo $facebook; ?>" target="_blank">
						<div class="social-icon-box">
							<div class="social-widget-icon fb-widget-icon">
								<span class="fb-social-icon"></span>
							</div>
							<!--social-widget-icon-->
						</div>
						<!--social-icon-box-->
						<div class="social-count-number">
							<?php echo facebook_like_count($facebook);?>
						</div>
						<!--social-count-number-->
						<div class="social-widget-text">						
						<?php echo $facebook_text; ?>
						</div>
						<!--social-widget-text-->						
						</a>	
					</li>
				<?php } ?>
				<?php if(!empty($twitter_id)){ ?>
					<li>
						<a href="https://twitter.com/<?php echo $twitter_id; ?>" target="_blank">
						<div class="social-icon-box">
							<div class="social-widget-icon twitter-widget-icon">
								<span class="twitter-social-icon"></span>
							</div>
							<!--social-widget-icon-->
						</div>
						<!--social-icon-box-->
						<div class="social-count-number">
							<?php echo tweet_count($twitter_id, $consumer_key, $consumer_secret, $access_token, $access_token_secret);?>
						</div>
						<!--social-count-number-->
						<div class="social-widget-text">
						<?php echo $twitter_text; ?>
						</div>
						<!--social-widget-text-->						
						</a>
					</li>
				<?php } ?>
				<?php if(!empty($gplus)){?> 
					<li>
						<a href="https://plus.google.com/<?php echo $gplus; ?>/posts" target="_blank">
						<div class="social-icon-box">
							<div class="social-widget-icon google-widget-icon">
								<span class="google-social-icon"></span>
							</div>
							<!--social-widget-icon-->
						</div>
						<!--social-icon-box-->		
						<div class="social-count-number">			
							<?php echo google_plus_count($gplus, $gplus_api);?>
						</div>
						<!--social-count-number-->
						<div class="social-widget-text">
						<?php echo $gplus_text; ?>
						</div>
						<!--social-widget-text-->						
						</a>
					</li>
				<?php } ?>
				<?php if(!empty($yt)){?> 
					<li>
						<a href="http://www.youtube.com/user/<?php echo $yt; ?>" target="_blank">
						<div class="social-icon-box">
							<div class="social-widget-icon youtube-widget-icon">
								<span class="youtube-social-icon"></span>
							</div>
							<!--social-widget-icon-->
						</div>
						<!--social-icon-box-->	
						<div class="social-count-number">
							<?php echo get_yt_subs($yt);?>
						</div>
						<!--social-count-number-->
						<div class="social-widget-text">
						<?php echo $yt_text; ?>
						</div>
						<!--social-widget-text-->						
						</a>
					</li>
				<?php } ?>
				<?php if(!empty($instagram_id)){?> 
					<li>
						<a href="https://instagram.com/<?php echo $instagram_id; ?>" target="_blank">
						<div class="social-icon-box">
							<div class="social-widget-icon instagram-widget-icon">
								<span class="instagram-social-icon"></span>
							</div>
							<!--social-widget-icon-->
						</div>
						<!--social-icon-box-->	
						<div class="social-count-number">
							<?php echo get_instagram_followers( $instagram_id, $instagram_token );?>
						</div>
						<!--social-count-number-->
						<div class="social-widget-text">
						<?php echo $instagram_text; ?>
						</div>
						<!--social-widget-text-->						
						</a>
					</li>
				<?php } ?>
				<?php if(!empty($pinterest)){?> 
					<li>
						<a href="https://pinterest.com/<?php echo $pinterest; ?>" target="_blank">
						<div class="social-icon-box">
							<div class="social-widget-icon pinterest-widget-icon">
								<span class="pinterest-social-icon"></span>
							</div>
							<!--social-widget-icon-->
						</div>
						<!--social-icon-box-->	
						<div class="social-count-number">
							<?php echo get_pinterest_followers( $pinterest);?>
						</div>
						<!--social-count-number-->
						<div class="social-widget-text">
							<?php echo $pinterest_text; ?>
						</div>
						<!--social-widget-text-->
						</a>
					</li>
				<?php } ?>
			</ul>
		</div>
<?php

		/* After widget. */

		echo $args['after_widget'];
	}
	
	function update($new_instance, $old_instance){
		$instance 						= $old_instance;
		$instance['title'] 				= $new_instance['title'];
		$instance['widget_style'] 				= $new_instance['widget_style'];
		
		$instance['facebook_page_url'] 	= $new_instance['facebook_page_url'];
		$instance['twitter_id'] 		= $new_instance['twitter_id'];
		$instance['gplus_id'] 			= $new_instance['gplus_id'];
		$instance['yt_id'] 				= $new_instance['yt_id'];
		
		$instance['facebook_text'] 		= $new_instance['facebook_text'];
		$instance['twitter_text'] 		= $new_instance['twitter_text'];
		$instance['gplus_text'] 		= $new_instance['gplus_text'];	
		$instance['gplus_api'] 		= $new_instance['gplus_api'];	
		$instance['yt_text'] 			= $new_instance['yt_text'];
		
		$instance['consumer_key'] 		= $new_instance['consumer_key'];
		$instance['consumer_secret'] 	= $new_instance['consumer_secret'];
		$instance['access_token'] 		= $new_instance['access_token'];
		$instance['access_token_secret'] = $new_instance['access_token_secret'];
		$instance['twitter_id'] 		= $new_instance['twitter_id'];
		
		$instance['instagram_id'] 		=	$new_instance['instagram_id'];
		$instance['instagram_token']	=	$new_instance['instagram_token'];
		$instance['instagram_text']		=	$new_instance['instagram_text'];
		
		$instance['pinterest'] 		=	$new_instance['pinterest'];
		$instance['pinterest_text']		=	$new_instance['pinterest_text'];
		
		return $instance;
		}
	
	
	/**
	* Creates the edit form for the widget.
	*
	*/
	function form($instance){

$defaults = array('title' => 'Social Widget', 'widget_style' => 'horizontal-social', 'facebook_page_url' => '', 'gplus_id' => '', 'yt_id' => '', 'facebook_text' => 'Facebook', 'twitter_text' => 'Twitter', 'gplus_text' => 'Google', 'gplus_api'=>'', 'yt_text' => 'Youtube', 'twitter_id' => '', 'consumer_key' => '', 'consumer_secret' => '', 'access_token' => '', 'access_token_secret' => '', 'instagram_id' => '', 'instagram_token' => '',  'instagram_text' => 'Instagram', 'pinterest_text' =>'Pinterest', 'pinterest' => '');
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

<p>
	<label for="<?php echo $this->get_field_id( 'title' ); ?>">
		Title:
	</label>
	<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
</p>
<!-- style -->
<p>
	<label for="<?php echo $this->get_field_id('widget_style'); ?>">Widget style:</label>
	<select name="<?php echo $this->get_field_name('widget_style'); ?>" id="<?php echo $this->get_field_id('widget_style'); ?>" class="widefat" >
		<?php $options = array('vertical-social', 'horizontal-social');
		foreach ($options as $option) {?>
		<option value='<?php echo $option; ?>' <?php if ($option == $instance['widget_style']) echo 'selected="selected"'; ?>><?php echo $option; ?></option>
		<?php } ?>
	</select>
</p>
<p>
<h3>Facebook</h3>
</p>
<p>
	<label for="<?php echo $this->get_field_id( 'facebook_text' ); ?>">
		Facebook Icon Title:(e.g. Fans , Followers , Subscribers , etc )
	</label>
	<input id="<?php echo $this->get_field_id( 'facebook_text' ); ?>" name="<?php echo $this->get_field_name( 'facebook_text' ); ?>" value="<?php echo $instance['facebook_text']; ?>" style="width:100%;" />
</p>
<p>
	<label for="<?php echo $this->get_field_id( 'facebook_page_url' ); ?>">
		Facebook page url:(e.g.  https://www.facebook.com/stepfoxthemes)
	</label>
	<input id="<?php echo $this->get_field_id( 'facebook_page_url' ); ?>" name="<?php echo $this->get_field_name( 'facebook_page_url' ); ?>" value="<?php echo $instance['facebook_page_url']; ?>" style="width:100%;" />

</p>	

<p>
<h3>Twitter</h3>
</p>
<p>
	<label for="<?php echo $this->get_field_id( 'twitter_text' ); ?>">
		Twitter Icon Title:(e.g. Fans , Followers , Subscribers , etc )
	</label>
	<input id="<?php echo $this->get_field_id( 'twitter_text' ); ?>" name="<?php echo $this->get_field_name( 'twitter_text' ); ?>" value="<?php echo $instance['twitter_text']; ?>" style="width:100%;" />
</p>
<p>
	<label for="<?php echo $this->get_field_id( 'twitter_id' ); ?>">
		Twitter id:(e.g. stepfoxthemes )
	</label>
	<input id="<?php echo $this->get_field_id( 'twitter_id' ); ?>" name="<?php echo $this->get_field_name( 'twitter_id' ); ?>" value="<?php echo $instance['twitter_id']; ?>" style="width:100%;" />
</p>	

<p>
	<label for="<?php echo $this->get_field_id( 'consumer_key' ); ?>">
		Consumer key:(register app here and get your key: https://apps.twitter.com/ )
	</label>
	<input id="<?php echo $this->get_field_id( 'consumer_key' ); ?>" name="<?php echo $this->get_field_name( 'consumer_key' ); ?>" value="<?php echo $instance['consumer_key']; ?>" style="width:100%;" />
</p>

<p>
	<label for="<?php echo $this->get_field_id( 'consumer_secret' ); ?>">
		Consumer secret:(register app here and get your secret: https://apps.twitter.com/ )
	</label>
	<input id="<?php echo $this->get_field_id( 'consumer_secret' ); ?>" name="<?php echo $this->get_field_name( 'consumer_secret' ); ?>" value="<?php echo $instance['consumer_secret']; ?>" style="width:100%;" />
</p>

<p>
	<label for="<?php echo $this->get_field_id( 'access_token' ); ?>">
		Access token:(register app here and get your token: https://apps.twitter.com/ )
	</label>
	<input id="<?php echo $this->get_field_id( 'access_token' ); ?>" name="<?php echo $this->get_field_name( 'access_token' ); ?>" value="<?php echo $instance['access_token']; ?>" style="width:100%;" />
</p>

<p>
	<label for="<?php echo $this->get_field_id( 'access_token_secret' ); ?>">
		Access token secret:(register app here and get your token: https://apps.twitter.com/ )
	</label>
	<input id="<?php echo $this->get_field_id( 'access_token_secret' ); ?>" name="<?php echo $this->get_field_name( 'access_token_secret' ); ?>" value="<?php echo $instance['access_token_secret']; ?>" style="width:100%;" />
</p>
<p>
<h3>Pinterest</h3>
</p>
<p>
	<label for="<?php echo $this->get_field_id( 'pinterest_text' ); ?>">
		Pinterest Icon Title:(e.g. Fans , Followers , Subscribers , etc )
	</label>
	<input id="<?php echo $this->get_field_id( 'pinterest_text' ); ?>" name="<?php echo $this->get_field_name( 'pinterest_text' ); ?>" value="<?php echo $instance['pinterest_text']; ?>" style="width:100%;" />
</p>
<p>
	<label for="<?php echo $this->get_field_id( 'pinterest' ); ?>">
		Pinterest id:(e.g. https://www.pinterest.com/stepfoxthemes [use this part:stepfoxthemes])
	</label>
	<input id="<?php echo $this->get_field_id( 'pinterest' ); ?>" name="<?php echo $this->get_field_name( 'pinterest' ); ?>" value="<?php echo $instance['pinterest']; ?>" style="width:100%;" />
</p>
<p>
<h3>Google plus</h3>
</p>	
<p>
	<label for="<?php echo $this->get_field_id( 'gplus_text' ); ?>">
		Google Icon Title:(e.g. Fans , Followers , Subscribers , etc )
	</label>
	<input id="<?php echo $this->get_field_id( 'gplus_text' ); ?>" name="<?php echo $this->get_field_name( 'gplus_text' ); ?>" value="<?php echo $instance['gplus_text']; ?>" style="width:100%;" />
</p>
<p>
	<label for="<?php echo $this->get_field_id( 'gplus_id' ); ?>">
		Google id:(e.g. https://plus.google.com/+Stepfoxthemes/posts [use this part:+Stepfoxthemes] or if your url is like https://plus.google.com/u/0/112425853526219744490/posts [use this part:112425853526219744490] )
	</label>
	<input id="<?php echo $this->get_field_id( 'gplus_id' ); ?>" name="<?php echo $this->get_field_name( 'gplus_id' ); ?>" value="<?php echo $instance['gplus_id']; ?>" style="width:100%;" />
</p>
<p>
	<label for="<?php echo $this->get_field_id( 'gplus_api' ); ?>">
		Google api key:(tutorial: https://developers.google.com/+/api/oauth)
	</label>
	<input id="<?php echo $this->get_field_id( 'gplus_api' ); ?>" name="<?php echo $this->get_field_name( 'gplus_api' ); ?>" value="<?php echo $instance['gplus_api']; ?>" style="width:100%;" />
</p>

<p>
<h3>Youtube</h3>
</p>			
<p>
	<label for="<?php echo $this->get_field_id( 'yt_text' ); ?>">
		Youtube Icon Title:(e.g. Fans , Followers , Subscribers , etc )
	</label>
	<input id="<?php echo $this->get_field_id( 'yt_text' ); ?>" name="<?php echo $this->get_field_name( 'yt_text' ); ?>" value="<?php echo $instance['yt_text']; ?>" style="width:100%;" />
</p>
<p>
	<label for="<?php echo $this->get_field_id( 'yt_id' ); ?>">
		Youtube id:(e.g. https://www.youtube.com/user/topoviletaat [Use this part:topoviletaat])
	</label>
	<input id="<?php echo $this->get_field_id( 'yt_id' ); ?>" name="<?php echo $this->get_field_name( 'yt_id' ); ?>" value="<?php echo $instance['yt_id']; ?>" style="width:100%;" />
</p>
<p>
<h3>Instagram</h3>
</p>			
<p>
	<label for="<?php echo $this->get_field_id( 'instagram_text' ); ?>">
		Instagram Icon Title:(e.g. Fans , Followers , Subscribers , etc )
	</label>
	<input id="<?php echo $this->get_field_id( 'instagram_text' ); ?>" name="<?php echo $this->get_field_name( 'instagram_text' ); ?>" value="<?php echo $instance['instagram_text']; ?>" style="width:100%;" />
</p>
<p>
	<label for="<?php echo $this->get_field_id( 'instagram_id' ); ?>">
		Instagram id:(e.g. https://instagram.com/developer/)
	</label>
	<input id="<?php echo $this->get_field_id( 'instagram_id' ); ?>" name="<?php echo $this->get_field_name( 'instagram_id' ); ?>" value="<?php echo $instance['instagram_id']; ?>" style="width:100%;" />
</p>
<p>
	<label for="<?php echo $this->get_field_id( 'instagram_token' ); ?>">
		Instagram Token:(e.g. https://instagram.com/developer/)
	</label>
	<input id="<?php echo $this->get_field_id( 'instagram_token' ); ?>" name="<?php echo $this->get_field_name( 'instagram_token' ); ?>" value="<?php echo $instance['instagram_token']; ?>" style="width:100%;" />
</p>
	
<?php } //end of form

	}// END class
	
	function facebook_like_count($page_link){
	$url = 'http://graph.facebook.com/'. urlencode( $page_link );
	$content = wp_remote_retrieve_body( wp_remote_request( $url));
	if( is_wp_error( $content))
		return 0;
	$content = json_decode( $content);
	$value = intval( $content->likes);
	return $value;
	}
	
	function tweet_count($twitter_id, $consumer_key, $consumer_secret, $access_token, $access_token_secret ){
	$twitter_id = $twitter_id;		
	$consumer_key = $consumer_key;
	$consumer_secret = $consumer_secret;
	$access_token = $access_token;
	$access_token_secret = $access_token_secret;
	if($twitter_id && $consumer_key && $consumer_secret && $access_token && $access_token_secret) { 
		$transName = 'list_tweets_'.$args['widget_id'];
		$cacheTime = 10;
		delete_transient($transName);
		if(false === ($twitterData = get_transient($transName))) {
		     // require the twitter auth class
		     @require_once 'twitteroauth/twitteroauth.php';
		     $twitterConnection = new TwitterOAuth(
							$consumer_key,	// Consumer Key
							$consumer_secret,   	// Consumer secret
							$access_token,       // Access token
							$access_token_secret    	// Access token secret
							);
		    $twitterData = $twitterConnection->get(
							  'statuses/user_timeline',
							  array(
							    'screen_name'     => $twitter_id,
							    'count'           => $count,
							    'exclude_replies' => false
							  )
							);
		     if($twitterConnection->http_code != 200){$twitterData = get_transient($transName);}
		     // Save our new transient.
		     set_transient($transName, $twitterData, 60 * $cacheTime);
		};
		$twitter = get_transient($transName);
		
		if($twitter && is_array($twitter)) {
			$count = $twitter[0]->user->followers_count;
			return $count;
		 }}
	}		
		
	function google_plus_count( $gplus, $gplus_api) {
			$params = array('sslverify' => false,'timeout'   => 60);
			$googleplus = wp_remote_get( 'https://www.googleapis.com/plus/v1/people/'.$gplus.'?key='.$gplus_api, $params);		
			$_data = json_decode( $googleplus['body'], true );
			$count = intval( $_data['circledByCount'] );
			return $count;
	}		
	
	function get_yt_subs($uname) { 
		$link = "http://gdata.youtube.com/feeds/api/users/".$uname;
		$xmlData = wp_remote_fopen("$link");
		$xmlData = str_replace('yt:', 'yt', $xmlData); 
		$xml = new SimpleXMLElement($xmlData); 
		$subs = $xml->ytstatistics['subscriberCount']; 
		return($subs);
	 }
		
	function get_instagram_followers( $instagram_id, $instagram_token ) {
			$instagram = wp_remote_get( 'https://api.instagram.com/v1/users/'.$instagram_id.'/?access_token='.$instagram_token);
			$_data = json_decode( $instagram['body'], true );
			$count = intval( $_data['data']['counts']['followed_by'] );
 			return ($count);
	} 	 	 
	
	
	function get_pinterest_followers( $pinterest ) {	
		$metas = wp_remote_get('https://api.pinterest.com/v3/pidgets/users/'. $pinterest .'/pins');
		$_data = json_decode( $metas['body'], true );
		$count = intval( $_data['data']['user']['follower_count'] );
		return ($count);				
	} 
?>