twitter_stream

The Twitter Stream twitter stream function.

Description

twitter_stream( (constant) $args = FALSE ); 

Parameters (1)

0. $args — Optional. (constant) => FALSE
The args.

Usage

  1. if ( !function_exists( 'twitter_stream' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'twitter-stream/twitter-stream.php'; 
  3.  
  4. // The args. 
  5. $args = FALSE; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = twitter_stream($args); 
  9.  

Defined (1)

The function is defined in the following location(s).

/twitter-stream.php  
  1. function twitter_stream($args = FALSE) { 
  2.  
  3. if(is_array($args)) { //Is it an array? 
  4. $r = &$args; //Good, reference out arguments into our options array. 
  5. } else { 
  6. parse_str($args, $r); //It's a query string, parse out our values into our options array. 
  7.  
  8. if(!isset($r)) { 
  9. $r = array(); 
  10.  
  11. $defaults = array( //Set some defaults 
  12. 'username' => '',  
  13. 'count' => '10',  
  14. 'date' => FALSE,  
  15. 'profile_link' => 'Visit My Profile',  
  16. 'retweets' => 'FALSE',  
  17. 'show_followers' => FALSE,  
  18. 'cache_time' => 1800,  
  19. 'echo' => TRUE 
  20. ); 
  21.  
  22. $r = array_merge($defaults, $r); //Merge our defaults array onto our options array to fill in any missing values with defaults. 
  23.  
  24. if($r['retweets'] == "on") 
  25. $r['retweets'] = "true"; 
  26.  
  27. if(version_compare(PHP_VERSION, '5.0.0', '<')) { //Checked before hand, but if the user didn't listen tell them off & refuse to run. 
  28. _e('You must have PHP5 or higher for this plugin to work.', 'twit_stream'); 
  29. return FALSE; 
  30.  
  31. if($r['username'] != '') { 
  32. $cache_path = dirname(__FILE__).'/'.$r['username'].'.cache'; //Set our cache path. Can be changed if you feel the need. 
  33. } else { 
  34. $cache_path = dirname(__FILE__).'/authuser.cache'; //Set our cache path. Can be changed if you feel the need. 
  35.  
  36. //First we need to check to see if a cache file has already been made. 
  37. if(file_exists($cache_path)) { 
  38. $modtime = filemtime($cache_path); //Get the time the file was last modified. 
  39. $content = twitter_stream_cache($modtime, $cache_path, $r['cache_time']); //Hand it to the cache function & get the data 
  40. if($content !== FALSE) { 
  41. $cache = TRUE; //Cache is still valid 
  42. } else { 
  43. $cache = FALSE; //Cache too old invalidate it 
  44. unset($content); //Delete the content variable to force the script to connect to twitter & renew the cache. 
  45. if( function_exists('wp_cache_clear_cache') ) { 
  46. wp_cache_clear_cache(); 
  47. } elseif ( function_exists('prune_super_cache') ) { 
  48. prune_super_cache(WP_CONTENT_DIR.'/cache/', true ); 
  49. } else { 
  50. $cache = FALSE; //This is probably first run so set the cache to false so it can be created. 
  51. if( function_exists('wp_cache_clear_cache') ) { 
  52. wp_cache_clear_cache(); 
  53. } elseif ( function_exists('prune_super_cache') ) { 
  54. prune_super_cache(WP_CONTENT_DIR.'/cache/', true ); 
  55.  
  56. //No content is set so we either need to create the cache or it has been invalidated and we need to renew it. 
  57. if(!isset($content)) { 
  58. /** Get user access tokens out of the session. */ 
  59. $access_token = get_option('twitter_stream_token'); 
  60. if(empty($access_token) || $access_token === FALSE) { 
  61. _e('Authorizing Twitter Stream with Twitter is no longer optional. You need to go to the Twitter Stream Authorization page in the WordPress Admin (under settings) before your tweets can be shown.'); 
  62. return FALSE; 
  63. /** Create aTwitterOauthobject with consumer/user tokens. */ 
  64. $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']); 
  65. $connection->format = 'json'; 
  66. $content = $connection->get('statuses/user_timeline', array('screen_name' => $r['username'], 'count' => $r['count'], 'include_rts' => $r['retweets'])); 
  67.  
  68. if($cache === FALSE) { 
  69. //If cache was set to false we need to update the cache 
  70. //convert decoded array back to json to store 
  71. $cache_content = json_encode($content); 
  72. $fp = fopen($cache_path, 'w'); 
  73. if(flock($fp, LOCK_EX)) { 
  74. fwrite($fp, $cache_content); 
  75. flock($fp, LOCK_UN); 
  76. fclose($fp); 
  77.  
  78.  
  79. if($r['echo'] !== TRUE || $r['echo'] != 'true' || $r['echo'] != 'TRUE' || $r['echo'] != '1') { 
  80. return $content; 
  81.  
  82. $tweetfollow = twitter_stream_parse_tweets($content, $r); 
  83.  
  84. if(isset($tweetfollow[error])) { 
  85. echo '<strong>Twitter Sent An Error:</strong> ' . $tweetfollow[error]; 
  86. return false; 
  87.  
  88. $output = $tweetfollow[0]; 
  89.  
  90. //Now let's do some highlighting & auto linking. 
  91. //Find all the @replies and place them in a span so CSS can be used to highlight them. 
  92. //$output = preg_replace('~(\@[a-z0-9_]+)~ise', "'<span class=\"at-reply\"><a href=\"http://twitter.com/'.substr('$1', 1).'\" title=\"View '.substr('$1', 1).'\'s profile\">$1</a></span>'", $output); 
  93. //PHP 5.5 Compat thanks to Brandon @ PixelJar 
  94. $output = preg_replace_callback( '~(\@[a-z0-9_]+)~is', 'twitter_stream_reply_wrap', $output ); 
  95. //Find all the #tags and place them in a span so CSS can be used to highlight them. 
  96. //$output = preg_replace('~(\#[a-z0-9_]+)~ise', "'<span class=\"hash-tag\"><a href=\"http://twitter.com/search?q='.urlencode('$1').'\" title=\"Search for $1 on Twitter\">$1</a></span>'", $output); 
  97. //PHP 5.5 Compat thanks to Brandon @ PixelJar 
  98. $output = preg_replace_callback( '~(\#[a-z0-9_]+)~is', 'twitter_stream_hashtag_wrap', $output ); 
  99.  
  100. //Show follower count 
  101. if($r['show_followers']) { 
  102. $output .= '<div class="follower-count">'.$tweetfollow[1].' followers</div>'; 
  103.  
  104. //Link to users profile. Can be customized via the profile_link parameter & via CSS targeting. 
  105. $output .= '<div class="profile-link"><a href="http://twitter.com/'.$tweetfollow[2].'" title="'.$r['profile_link'].'">'.$r['profile_link'].'</a></div>'; 
  106.  
  107.  
  108. echo '<div class="twitter-stream">'.$output.'</div>'; 
  109.