qlstudio

ftf_twitter

Feb 28th, 2013
367
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  * Twitter Function ##
  5.  *
  6.  * @package WordPress
  7.  * @subpackage 4Trees
  8.  * @since 1.0
  9.  * @author QL Studio ##
  10.  * @url http://studio.quintalinda.com
  11.  */
  12.  
  13. function ftf_twitter( $args ){
  14.    
  15.     // default arguments ##
  16.     $defaults = array(
  17.     'mode'      => 'search',
  18.     'debug'     => false,
  19.     'count'     => 20,
  20.     'retweet'   => false
  21.     );
  22.    
  23.     // merge args - WordPress function #
  24.     $args = wp_parse_args( $args, $defaults );
  25.    
  26.     // convert "args" array to object - Custom function ##
  27.     $args = ftf_array_to_object( $args );
  28.    
  29.     // check for required oAuth details ##
  30.     if ( !$args->consumer_key || !$args->consumer_secret || !$args->oauth_access_token || !$args->oauth_access_token_secret ) {
  31.         if ( $args->debug === true ) {
  32.             echo 'Error - missing oAuth details'; // echo an error, if debugging ##
  33.         }
  34.         return false; // return nothing ##
  35.     }
  36.  
  37.     // mode ##
  38.     $twitter_mode = $args->mode; // convert to normal variable for switch - not sure why that's required? ##
  39.     switch ( $twitter_mode ) {
  40.  
  41.     case "search": // search API ##
  42.         $args->url = "https://api.twitter.com/1.1/search/tweets.json"; // url ##
  43.         break;
  44.  
  45.     case "home_timeline": // home_timeline API ##
  46.         $args->url = "https://api.twitter.com/1.1/statuses/home_timeline.json"; // url ##
  47.         break;
  48.  
  49.     case "user_timeline": // user_timeline API - default ##
  50.     default:
  51.         $args->url = "https://api.twitter.com/1.1/statuses/user_timeline.json"; // url ##
  52.         break;
  53.  
  54.     break;
  55.  
  56.     }
  57.  
  58.     // compile querystring ##
  59.     $args->query = '?screen_name='.$args->username.'&include_rts='.$args->retweet.'&q='.urlencode($args->search).'&count='.$args->count;
  60.  
  61.     // prepare oAuth args ##
  62.     $oauth = array(
  63.         'screen_name' => $args->username,
  64.         'count' => $args->count,
  65.         'include_rts' => $args->retweet,
  66.         'q' => ($args->search),
  67.         //'include_entities' => true,
  68.         'oauth_consumer_key' => $args->consumer_key,
  69.         'oauth_nonce' => time(),
  70.         'oauth_signature_method' => 'HMAC-SHA1',
  71.         'oauth_token' => $args->oauth_access_token,
  72.         'oauth_timestamp' => time(),
  73.         'oauth_version' => '1.0'
  74.         );
  75.    
  76.     $base_info = buildBaseString($args->url, 'GET', $oauth);
  77.     $composite_key = rawurlencode($args->consumer_secret) . '&' . rawurlencode($args->oauth_access_token_secret);
  78.     $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
  79.     $oauth['oauth_signature'] = $oauth_signature;
  80.  
  81.     // Make CURL Request ##
  82.     $header = array(buildAuthorizationHeader($oauth), 'Expect:');
  83.     $options = array(
  84.         CURLOPT_HTTPHEADER => $header,
  85.         CURLOPT_HEADER => false,
  86.         CURLOPT_URL => $args->url.$args->query,
  87.         CURLOPT_RETURNTRANSFER => true,
  88.         CURLOPT_SSL_VERIFYPEER => false
  89.         );
  90.  
  91.     $feed = curl_init();
  92.     curl_setopt_array($feed, $options);
  93.     $json = curl_exec($feed);
  94.     curl_close($feed);
  95.  
  96.     // abort on error ##
  97.     if ($json === false) {
  98.         if ( $args->debug === true ) {
  99.             echo 'CURL Error'; // echo an error, if debugging ##
  100.         }
  101.         return false;
  102.     }
  103.  
  104.     // JSON decode returned data ##
  105.     $twitter_data = json_decode($json);
  106.  
  107.     // optionally debug returned data ##
  108.     if ( $args->debug === true ) pr($twitter_data);
  109.  
  110.     // search fix - object returned is nested deeper ##
  111.     if ( $twitter_mode === 'search' ) { // array shift ##
  112.         $twitter_data = $twitter_data->statuses;
  113.     }
  114.    
  115.     // TODO -- some extra error checking here ##
  116.    
  117.     // return array of data ##
  118.     return $twitter_data;
  119.        
  120. }
  121.    
  122. // build base string ##
  123. // credit - http://stackoverflow.com/a/12939923/591486
  124. if ( !function_exists('buildBaseString') ) {
  125.     function buildBaseString($baseURI, $method, $params) {
  126.         $r = array();
  127.         ksort($params);
  128.         foreach($params as $key=>$value){
  129.             $r[] = "$key=" . rawurlencode($value);
  130.         }
  131.         return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
  132.     }
  133. }    
  134.  
  135. // build authorizion header ##
  136. // credit - http://stackoverflow.com/a/12939923/591486
  137. if ( !function_exists('buildAuthorizationHeader') ) {
  138.     function buildAuthorizationHeader($oauth) {
  139.         $r = 'Authorization: OAuth ';
  140.         $values = array();
  141.         foreach($oauth as $key=>$value)
  142.             $values[] = "$key=\"" . rawurlencode($value) . "\"";
  143.         $r .= implode(', ', $values);
  144.         return $r;
  145.     }
  146. }
  147.  
  148. // add href link to text string ##
  149. // credit - http://saturnboy.com/2010/02/parsing-twitter-with-regexp/
  150. if ( !function_exists('ftf_twitter_add_href') ) {
  151.    
  152.     function ftf_twitter_add_href ( $text ) {
  153.        
  154.         $text = preg_replace(
  155.             '@(https?://([-\w\.]+)+(/([\w/_\.]*(\?\S+)?(#\S+)?)?)?)@',
  156.             '<a href="$1" target="_blank" rel="nofollow">$1</a>',
  157.             $text
  158.         );
  159.        
  160.         return $text;
  161.        
  162.     }
  163.    
  164. }
  165.  
  166. // add href link to twitter username in text string ##
  167. // credit - http://saturnboy.com/2010/02/parsing-twitter-with-regexp/
  168. if ( !function_exists('ftf_twitter_add_username') ) {
  169.    
  170.     function ftf_twitter_add_username ( $text ) {
  171.        
  172.         $text = preg_replace(
  173.             '/@(\w+)/',
  174.             '<a href="http://twitter.com/$1" target="_blank" rel="nofollow">@$1</a>',
  175.             $text
  176.         );
  177.        
  178.         return $text;
  179.        
  180.     }
  181.    
  182. }
  183.  
  184. // add href link to twitter hashtag in text string ##
  185. // credit - http://saturnboy.com/2010/02/parsing-twitter-with-regexp/
  186. if ( !function_exists('ftf_twitter_add_hashtag') ) {
  187.    
  188.     function ftf_twitter_add_hashtag ( $text ) {
  189.        
  190.         $text = preg_replace(
  191.             '/\s+#(\w+)/',
  192.             ' <a href="http://search.twitter.com/search?q=%23$1" target="_blank" rel="nofollow">#$1</a>',
  193.             $text
  194.         );
  195.        
  196.         return $text;
  197.        
  198.     }
  199.    
  200. }
  201.  
  202. /*
  203.  * convert an array to an object ##
  204.  *
  205.  * @param array $array
  206.  * @return object
  207.  * @since 0.1
  208.  *
  209.  */
  210. if ( !function_exists( 'ftf_array_to_object' ) ) {
  211. function ftf_array_to_object($array) {
  212.     if(!is_array($array)) {
  213.         return $array;
  214.     }
  215.    
  216.     $object = new stdClass();
  217.     if (is_array($array) && count($array) > 0) {
  218.       foreach ($array as $name=>$value) {
  219.          $name = strtolower(trim($name));
  220.          if (!empty($name)) {
  221.             $object->$name = ftf_array_to_object($value);
  222.          }
  223.       }
  224.       return $object;
  225.     }
  226.     else {
  227.       return FALSE;
  228.     }
  229. }}
  230.  
  231.  
  232. /*
  233.  * pretty print_r / var_dump ##
  234.  */
  235. if ( !function_exists( 'pr' ) ) {
  236. function pr($var) {
  237.     print '<pre class="var_dump">'; var_dump($var); print '</pre>';
  238. }} // pr ##
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×