Advertisement
jegtheme

twitter-widget.php

Jun 29th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.71 KB | None | 0 0
  1. <?php
  2.  
  3. require_once( JEG_PLUGIN_DIR . '/lib/twitter/Autolink.php');
  4.  
  5. if ( ! class_exists( 'jeg_twitter_class' ) )
  6. {
  7.     class jeg_twitter_class
  8.     {
  9.  
  10.         public $screen_name = "";
  11.         public $cachefile = "";
  12.         public $consumerkey = "";
  13.         public $consumersecret = "";
  14.         public $accesstoken = "";
  15.         public $accesstokensecret = "";
  16.  
  17.         public $tags = true;
  18.         public $nofollow = true;
  19.         public $newwindow = true;
  20.         public $hashtags = true;
  21.         public $attags = true;
  22.  
  23.         public function __construct($screen_name = '', $consumerkey ='', $consumersecret ='', $accesstoken ='', $accesstokensecret ='', $cachefile = '') {
  24.             $this->screen_name = empty($screen_name) ? JEG_PLUGIN_DIR .'/lib/twitter/cache/twitter.txt' : $screen_name;
  25.             $this->cachefile = empty($cachefile) ? JEG_PLUGIN_DIR .'/lib/twitter/cache/twitter.txt' : $cachefile;
  26.             $this->consumerkey = $consumerkey;
  27.             $this->consumersecret = $consumersecret;
  28.             $this->accesstoken = $accesstoken;
  29.             $this->accesstokensecret = $accesstokensecret;
  30.         }
  31.  
  32.         private function cleanTwitterName($twitterid)
  33.         {
  34.             $test = substr($twitterid,0,1);
  35.  
  36.             if($test == "@"){
  37.                 $twitterid = substr($twitterid,1);
  38.             }
  39.  
  40.             return $twitterid;
  41.  
  42.         }
  43.  
  44.         private function changeLink($string)
  45.         {
  46.             // Username linkable
  47.             $string = preg_replace('/(?<=^|\s)@([a-z0-9_]+)/i','<a href="http://www.twitter.com/$1">@$1</a>',$string);
  48.  
  49.             // Hashtag
  50.             $string = preg_replace('/(^|\s)#(\w*[a-zA-Z_]+\w*)/', '\1#<a href="http://twitter.com/search?q=%23\2">\2</a>', $string);
  51.  
  52.             if(!$this->tags){
  53.                 $string = strip_tags($string);
  54.             }
  55.             if($this->nofollow){
  56.                 $string = str_replace('<a ','<a rel="nofollow"', $string);
  57.             }
  58.             if($this->newwindow){
  59.                 $string = str_replace('<a ','<a target="_blank"', $string);
  60.             }
  61.             return $string;
  62.         }
  63.  
  64.         private function getTimeAgo($time)
  65.         {
  66.                 $tweettime = strtotime($time); // This is the value of the time difference - UK + 1 hours (3600 seconds)
  67.                 $nowtime = time();
  68.                 $timeago = ($nowtime-$tweettime);
  69.                 $thehours = floor($timeago/3600);
  70.                 $theminutes = floor($timeago/60);
  71.                 $thedays = floor($timeago/86400);
  72.                 /********************* Checking the times and returning correct value */
  73.                 if($theminutes < 60){
  74.                     if($theminutes < 1){
  75.                         $timemessage =  "Less than 1 minute ago";
  76.                     } else if($theminutes == 1) {
  77.                         $timemessage = $theminutes." minute ago";
  78.                     } else {
  79.                         $timemessage = $theminutes." minutes ago";
  80.                     }
  81.                 } else if($theminutes > 60 && $thedays < 1){
  82.                      if($thehours == 1){
  83.                         $timemessage = $thehours." hour ago";
  84.                      } else {
  85.                         $timemessage = $thehours." hours ago";
  86.                      }
  87.                 } else {
  88.                      if($thedays == 1){
  89.                         $timemessage = $thedays." day ago";
  90.                      } else {
  91.                         $timemessage = $thedays." days ago";
  92.                      }
  93.                 }
  94.             return $timemessage;
  95.         }
  96.  
  97.         private function removeSpamCharacters($string)
  98.         {
  99.             $string = preg_replace('/[^(\x20-\x7F)]*/','', $string);
  100.             return $string;
  101.         }
  102.  
  103.         public function formatTweets($tweets)
  104.         {
  105.             $t = array();
  106.             $i = 0;
  107.  
  108.             foreach($tweets as $tweet)
  109.             {
  110.                 if(isset($tweet->retweeted_status)){
  111.                     $text = $this->removeSpamCharacters($tweet->retweeted_status->text);
  112.                 } else {
  113.                     $text = $this->removeSpamCharacters($tweet->text);
  114.                 }
  115.                 $urls = $tweet->entities->urls;
  116.                 $mentions = $tweet->entities->user_mentions;
  117.                 $hashtags = $tweet->entities->hashtags;
  118.                 if($urls){
  119.                     foreach($urls as $url){
  120.                         if(strpos($text,$url->url) !== false){
  121.                             $text = str_replace($url->url,'<a href="'.$url->url.'">'.$url->url.'</a>',$text);
  122.                         }
  123.                     }
  124.                 }
  125.                 if($mentions && $this->attags){
  126.                     foreach($mentions as $mention){
  127.                         if(strpos($text,$mention->screen_name) !== false){
  128.                             $text = str_replace("@".$mention->screen_name." ",'<a href="http://twitter.com/'.$mention->screen_name.'">@'.$mention->screen_name.'</a> ',$text);
  129.                         }
  130.                     }
  131.                 }
  132.                 if($hashtags && $this->hashtags){
  133.                     foreach($hashtags as $hashtag){
  134.                         if(strpos($text,$hashtag->text) !== false){
  135.                             $text = str_replace('#'.$hashtag->text." ",'<a href="http://twitter.com/search?q=%23'.$hashtag->text.'">#'.$hashtag->text.'</a> ',$text);
  136.                         }
  137.                     }
  138.                 }
  139.                 $t[$i]["tweet"] = Twitter_Autolink::create($text, false)
  140.                                         ->setNoFollow(false)->setExternal(false)->setTarget('')
  141.                                         ->setUsernameClass('')
  142.                                         ->setHashtagClass('')
  143.                                         ->setURLClass('')
  144.                                         ->addLinks(); //trim($this->changeLink($text));
  145.                 $t[$i]["time"] = trim($this->getTimeAgo($tweet->created_at));
  146.                 $i++;
  147.             }
  148.  
  149.             $this->saveCachedTweets($t);
  150.             return $t;
  151.         }
  152.  
  153.         private function saveCachedTweets($data)
  154.         {
  155.             $data = json_encode($data);
  156.             $f = file_put_contents($this->cachefile, $data);
  157.         }
  158.  
  159.         private function getCachedTweets()
  160.         {
  161.             return file_get_contents($this->cachefile);
  162.         }
  163.  
  164.         public function getTweets($count = 1)
  165.         {
  166.             if(class_exists('TwitterOAuth')) {
  167.                 require_once(JEG_PLUGIN_DIR . '/lib/twitter/twitteroauth.php');
  168.             }
  169.            
  170.             $twitterconn = new TwitterOAuth($this->consumerkey, $this->consumersecret, $this->accesstoken, $this->accesstokensecret);
  171.  
  172.             $latesttweets = $twitterconn->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$this->screen_name."&count=".$count);
  173.  
  174.             if(!isset($latesttweets->errors)){
  175.                 return $this->formatTweets($latesttweets);
  176.             } else {
  177.                 return json_decode($this->getCachedTweets(), true);
  178.             }
  179.  
  180.         }
  181.     }
  182. }
  183.  
  184. if(!function_exists('jeg_fetch_twitter'))
  185. {
  186.     function jeg_fetch_twitter($twitter_username, $twitter_count = 5, $twitter_consumer_key, $twitter_consumer_secret, $twitter_access_token, $twitter_access_token_secret)
  187.     {
  188.         /*$cachetimeout = 60 * 60 * 3;
  189.         $currentcachetimeout = get_option( 'jeg_tweet_timeout', 0);
  190.         if( ( time() - $cachetimeout ) >  $currentcachetimeout)
  191.         {*/
  192.             $twitter = new jeg_twitter_class($twitter_username, $twitter_consumer_key, $twitter_consumer_secret, $twitter_access_token, $twitter_access_token_secret);
  193.    
  194.             $twitter->consumerkey = $twitter_consumer_key;
  195.             $twitter->consumersecret = $twitter_consumer_secret;
  196.             $twitter->accesstoken = $twitter_access_token;
  197.             $twitter->accesstokensecret = $twitter_access_token_secret;
  198.    
  199.             # cache file
  200.             $tweets = $twitter->getTweets($twitter_count);
  201.    
  202.             update_option('jeg_tweet_timeout', time());
  203.             update_option('jeg_tweet_cache_content', $tweets);
  204.    
  205.             return $tweets;
  206.    
  207.         /*} else {
  208.             return get_option('jeg_tweet_cache_content', __('Sorry, no tweet found.', 'jeg_textdomain'));
  209.         }*/
  210.     }
  211. }
  212.  
  213. $jeg_tweets = jeg_fetch_twitter($twitter_username, $twitter_count, $twitter_consumer_key, $twitter_consumer_secret, $twitter_access_token, $twitter_access_token_secret);
  214.  
  215. ?>
  216.  
  217. <div class="jeg-twitter-container">
  218.     <div class="jeg-tweets">
  219.         <ul>
  220.             <?php if (is_array($jeg_tweets)) : foreach ($jeg_tweets as $jeg_tweet) : ?>
  221.                 <li class="jeg-tweet-container">
  222.                     <div class="jeg-tweet"><?php echo $jeg_tweet['tweet'] ?></div>
  223.                     <span class="jeg-tweet-time"><?php echo $jeg_tweet['time'] ?></span>
  224.                 </li>
  225.             <?php endforeach; else : ?>
  226.                 <li><?php echo $jeg_tweets ?></li>
  227.             <?php endif; ?>
  228.         </ul>
  229.     </div>
  230.  
  231.     <div class="jeg-follow-button clearfix">
  232.         <a href="https://twitter.com/<?php echo $twitter_username ?>" class="twitter-follow-button" data-show-count="false">Follow @<?php echo $twitter_username ?></a>
  233.         <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
  234.     </div>
  235. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement