<?php
class hybrid_twitterfeed_widget extends WP_Widget{
	function hybrid_twitterfeed_widget() 
	{
		parent::WP_Widget(false, $name = 'Hybrid > Twitterfeed');
	}
	
	function widget($args, $instance)
	{
		extract( $args );
		$title = apply_filters('widget_title', $instance['title']);
		$twitter_search = $instance['twitter_search'];
      //  echo $before_widget;
		if ( !(is_page_template('template-blog.php') || is_home() || is_single())) {
					echo '<div class="span3">';// Display some text
			}
		?>
		
		<?php
					require_once("twitteroauth/twitteroauth/twitteroauth.php"); //Path to twitteroauth library
					 
					$twitteruser = 	$twitter_search;
					$notweets = 30;
					$consumerkey = "LNdJXcH0WsCJYvav1t0Yg";
					$consumersecret = "82IfnS9ocxWt1McJ5XUu98AOx7hs9d5Pxdlhsjl8Y";
					$accesstoken = "1510215961-w4IcHbBwKbX1dGEXR9Cn3JUaghYknN6pvwtkvqG";
					$accesstokensecret = "WErUVnTnIB7AdSCqNB91gtrJBn01knKtI4ProceiI";
					 
					function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
					  $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
					  return $connection;
					}
					  
					$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
					 
					$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
					$twiits = json_encode($tweets);
					$twiits1 = json_decode($twiits,true);
					?>
					
					<h3><?php echo $title;?></h3>
					<div class="testmonials">
						<ul id="twitter-feed" class="feed">
						<?php
							foreach ($twiits1 as $tweet)
							{						
								echo '<li><div class="desc">'.$tweet["text"].'</div><div class="testmonials-text"><p class="post-title">'.$tweet['user']["name"].'</p><p class="post-categoty">'.$tweet['user']["followers_count"].' FOLLOWERS</p> </div> </li>';
							}					
						?>      
						</ul>
					</div>
					
		<?php
		if ( !(is_page_template('template-blog.php') || is_home() || is_single())) {
					echo '</div>';
					}
	//	echo $args['after_widget'];
	}
	
	function update($new_instance, $old_instance)
	{
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['twitter_search'] = strip_tags($new_instance['twitter_search']);
		return $instance;
	}
	
	function form($instance)
	{
		$title = isset($instance['title']) ? esc_attr($instance['title']) : "";
		$twitter_search = isset($instance['twitter_search']) ? esc_attr($instance['twitter_search']) : "";
		?>
		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title',"um_lang"); ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('twitter_search'); ?>"><?php _e('Twitter Screen Name (Without @)',"um_lang"); ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id('twitter_search'); ?>" name="<?php echo $this->get_field_name('twitter_search'); ?>" type="text" value="<?php echo $twitter_search; ?>" />
		</p>
		<?php
	}
}

function register_hybrid_twitterfeed_widget() {			
	register_widget('hybrid_twitterfeed_widget');			
}
add_action('widgets_init', 'register_hybrid_twitterfeed_widget');
?>