<?php
function widget_twitter() {
	$options = get_option("widget_twitter");
	$username = $options['username'];
	$limit = $options['number'];
	$link = $options['link'];
	
	$prefix = "<ul class='mytweets'>"; 
	$suffix = "</ul>";
	$tweetprefix = "<li>";
	$tweetsuffix = "</li>";
	
	$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=" . $limit;
	
	function parse_feed($feed, $prefix, $tweetprefix, $tweetsuffix, $suffix) {
	
		$feed = str_replace("&lt;", "<", $feed);
		$feed = str_replace("&gt;", ">", $feed);
		$feed = str_replace("&quot;", '"', $feed);
		$clean = explode("<content type=\"html\">", $feed);
		
		$amount = count($clean) - 1;
		
		echo $prefix;
		
		for ($i = 1; $i <= $amount; $i++) {
		$cleaner = explode("</content>", $clean[$i]);
		echo $tweetprefix;
		echo str_replace("&amp;", "&", $cleaner[0]);
		echo $tweetsuffix;
		}
		
		echo $suffix;
	}
	$twitterFeed = wp_remote_fopen($feed);
	parse_feed($twitterFeed, $prefix, $tweetprefix, $tweetsuffix, $suffix);
	echo '<a href="http://twitter.com/'.$username.'" class="twitterlink">'.$link.'</a>';
	?>
  <?php            
}

function widget_twitterFeature($args) {
	extract($args); 
	
	$options = get_option("widget_twitter");
	
	echo $before_widget;
    echo '<h3 class="twitter">';
    echo $options['title'];
    echo '</h3>';
	widget_twitter();
	echo $after_widget; 
}

function twitterFeature_control()
{
  $options = get_option("widget_twitter");

  if (!is_array( $options ))
	{
		$options = array(
		'title' => 'Twitter Feed',
		'username' => '',
		'number' => '3',
		'link' => 'Follow Me on Twitter!'
	    );
  }      

  if ($_POST['twitter-Submit'])
  {
    $options['title'] = htmlspecialchars($_POST['twitter-WidgetTitle']);
    $options['username'] = htmlspecialchars($_POST['twitter-Username']);
    $options['number'] = htmlspecialchars($_POST['twitter-PostNumber']);
    $options['link'] = htmlspecialchars($_POST['twitter-LinkText']);

    update_option("widget_twitter", $options);
  }

?>
  <p>
    <label for="twitter-WidgetTitle"><?php _e('Title', "moderno"); ?>: </label><br />
    <input class="widefat" type="text" id="twitter-WidgetTitle" name="twitter-WidgetTitle" value="<?php echo $options['title'];?>" />
    <br /><br />
    <label for="twitter-WidgetTitle"><?php _e('Username', "moderno"); ?>: </label><br />
    <input class="widefat" type="text" id="twitter-Username" name="twitter-Username" value="<?php echo $options['username'];?>" />
    <br /><br />    
    <label for="twitter-PostNumber"><?php _e('Number of Tweets', "moderno"); ?>: </label>
    <input type="text" id="twitter-PostNumber" name="twitter-PostNumber" style="width: 25px; text-align: center;" maxlength="1" value="<?php echo $options['number'];?>" />
    <br /><br />
    <label for="twitter-LinkText"><?php _e('Link Text', "moderno"); ?>: </label>
    <input type="text" id="twitter-LinkText" name="twitter-LinkText" value="<?php echo $options['link'];?>" />
    <br /><br />
    <input type="hidden" id="twitter-Submit" name="twitter-Submit" value="1" />
  </p>
<?php
}

wp_register_sidebar_widget( 'twitter_feed', __('Mod - Twitter Feed', "moderno"), 'widget_twitterFeature', array('description' => __('Displays your tweets', "moderno")) );
wp_register_widget_control( 'twitter_feed', __('Mod - Twitter Feed', "moderno"), 'twitterFeature_control');

?>