'Displays your latest twitter.com updates' ) ); } function form($instance) { // outputs the options form on admin if ( !function_exists('quot') ){ function quot($txt){ return str_replace( "\"", """, $txt ); } } // format some of the options as valid html @$username = htmlspecialchars($instance['user'], ENT_QUOTES); @$updateCount = htmlspecialchars($instance['count'], ENT_QUOTES); @$showTwitterIconTF = $instance['showTwitterIconTF']; @$showProfilePicTF = $instance['showProfilePicTF']; @$showTweetTimeTF = $instance['showTweetTimeTF']; @$widgetTitle = stripslashes(quot($instance['widgetTitle'])); @$includeRepliesTF = $instance['includeRepliesTF']; ?>

 

checked="checked">

checked="checked">

checked="checked">

 

checked="checked">

checked="checked">

 

To style the output of the widget, modify this CSS stylesheet. You should also back this file up before updating the plugin.

file_missing_or_old( $jsonFileName, .5 )){ //get new data from twitter $jsonData = $this->save_remote_file( $jsonURL, $jsonFileName ); } else{ //already have file, get the data out of it $jsonData = $this->get_json_data_from_file( $jsonFileName ); } // check for errors--rate limit or curl not installed // data returned will be: {"error":"Rate limit exceeded. Clients may not make more than 150 requests per hour.","request":"\/1\/statuses\/user_timeline.json?screen_name=salzano&include_entities=true"} if( @$jsonData == "" || @iconv_strlen( $tweets->error, "UTF-8" )){ //delete the json file so it will surely be downloaded on next page view if( file_exists( dirname(__FILE__) ."/". $jsonFileName )){ unlink( dirname(__FILE__) ."/". $jsonFileName ); } //get the backup data $jsonData = $this->get_json_data_from_file( $jsonTempFileName ); } else{ //good file, create a backup if( file_exists( dirname(__FILE__) . "/" . $jsonFileName )){ copy( dirname(__FILE__) . "/" . $jsonFileName, dirname(__FILE__) . "/" . $jsonTempFileName ); } } if( $tweets = json_decode( $jsonData )){ $haveTwitterData = true; } else{ //tweets is null $haveTwitterData = false; } // $tweets has been json_decoded if( $haveTwitterData && $showProfilePicTF ){ //make sure we have the profile picture saved locally $twitterUserData = $tweets[0]->user; $profilePicURL = $twitterUserData->profile_image_url; $profilePicPieces = explode( ".", $profilePicURL ); $profilePicExt = end( $profilePicPieces ); $profilePicFileName = $username . "." . $profilePicExt; if( $this->file_missing_or_old( $profilePicFileName, .5 )){ $this->save_remote_file( $profilePicURL, $profilePicFileName ); } } // output the widget $title = empty($title) ? ' ' : apply_filters('widget_title', $title); echo $before_widget; if( !empty( $title ) && $title != " " ) { echo $before_title . $title . $after_title; }; if( $haveTwitterData ){ $linkHTML = ""; $pluginURL = home_url()."/wp-content/plugins/latest-twitter-sidebar-widget/"; $icon = $pluginURL . "twitter.png"; @$pic = $pluginURL . $profilePicFileName; if( $showTwitterIconTF ){ echo $linkHTML . "\"t\""; } else{ if( $showProfilePicTF ){ echo $linkHTML . "\"\""; } } $i=1; foreach( $tweets as $tweet ){ //exit this loop if we have reached updateCount if( $i > $updateCount ){ break; } //skip this iteration of the loop if this is a reply and we are not showing replies if( !$includeRepliesTF && strlen( $tweet->in_reply_to_screen_name )){ continue; } echo "
"" . $this->fix_twitter_update( $tweet->text, $tweet->entities ) . ""
"; if( $showTweetTimeTF ){ echo "
" . $this->twitter_time_ltw( $tweet->created_at ) . "
"; } $i++; } } else{ echo "have data = false $jsonData"; } //show this no matter what, tweets or no tweets echo "
follow @$username on twitter
"; echo $after_widget; } function fix_twitter_update($origTweet,$entities) { if( $entities == null ){ return $origTweet; } foreach( $entities->urls as $url ){ $index[$url->indices[0]] = "url."\">".$url->url.""; $endEntity[(int)$url->indices[0]] = (int)$url->indices[1]; } foreach( $entities->hashtags as $hashtag ){ $index[$hashtag->indices[0]] = "text."\">#".$hashtag->text.""; $endEntity[$hashtag->indices[0]] = $hashtag->indices[1]; } foreach( $entities->user_mentions as $user_mention ){ $index[$user_mention->indices[0]] = "screen_name."\">@".$user_mention->screen_name.""; $endEntity[$user_mention->indices[0]] = $user_mention->indices[1]; } $fixedTweet=""; for($i=0;$i0){ $fixedTweet .= $index[(int)$i]; $i = $endEntity[(int)$i]-1; } else{ $fixedTweet .= iconv_substr( $origTweet,$i,1, "UTF-8" ); } } return $fixedTweet; } function twitter_time_ltw($a) { //get current timestamp $b = strtotime("now"); //get timestamp when tweet created $c = strtotime($a); //get difference $d = $b - $c; //calculate different time values $minute = 60; $hour = $minute * 60; $day = $hour * 24; $week = $day * 7; if(is_numeric($d) && $d > 0) { //if less then 3 seconds if($d < 3) return "right now"; //if less then minute if($d < $minute) return floor($d) . " seconds ago"; //if less then 2 minutes if($d < $minute * 2) return "about 1 minute ago"; //if less then hour if($d < $hour) return floor($d / $minute) . " minutes ago"; //if less then 2 hours if($d < $hour * 2) return "about 1 hour ago"; //if less then day if($d < $day) return floor($d / $hour) . " hours ago"; //if more then day, but less then 2 days if($d > $day && $d < $day * 2) return "yesterday"; //if less then year if($d < $day * 365) return floor($d / $day) . " days ago"; //else return more than a year return "over a year ago"; } } function save_remote_file( $url, $fileName ){ $response = wp_remote_get( $url ); if( is_wp_error( $response ) || $response['response']['code'] != "200" ){ //GET failed return false; } else{ //save the body of the response in $fileName $filePath = dirname(__FILE__) ."/". $fileName; $fp = fopen( $filePath, "w"); fwrite( $fp, $response['body'] ); fclose( $fp ); //that worked out well return $response['body']; } } function file_missing_or_old( $fileName, $ageInHours ){ $fileName = dirname(__FILE__) ."/". $fileName; if( !file_exists( $fileName )){ return true; } else{ $fileModified = filemtime( $fileName ); $today = time( ); $hoursSince = round(($today - $fileModified)/3600, 3); if( $hoursSince > $ageInHours ){ return true; } else{ return false; } } } function get_json_data_from_file( $jsonFileName ){ $fileName = dirname(__FILE__) ."/". $jsonFileName; $jsonData = ""; if( file_exists( $fileName )){ $theFile = fopen( $fileName, "r" ); $jsonData = fread( $theFile, filesize( $fileName )); fclose( $theFile ); } return $jsonData; } } if( !function_exists('register_latest_twitter_widget')){ add_action('widgets_init', 'register_latest_twitter_widget'); function register_latest_twitter_widget() { register_widget('latest_twitter_widget'); } } if( !function_exists('latest_twitter_widget_css')){ function latest_twitter_widget_css( ){ ?>