Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Twitter Function ##
- *
- * @package WordPress
- * @subpackage 4Trees
- * @since 1.0
- * @author QL Studio ##
- * @url http://studio.quintalinda.com
- */
- function ftf_twitter( $args ){
- // default arguments ##
- $defaults = array(
- 'mode' => 'search',
- 'debug' => false,
- 'count' => 20,
- 'retweet' => false
- );
- // merge args - WordPress function #
- $args = wp_parse_args( $args, $defaults );
- // convert "args" array to object - Custom function ##
- $args = ftf_array_to_object( $args );
- // check for required oAuth details ##
- if ( !$args->consumer_key || !$args->consumer_secret || !$args->oauth_access_token || !$args->oauth_access_token_secret ) {
- if ( $args->debug === true ) {
- echo 'Error - missing oAuth details'; // echo an error, if debugging ##
- }
- return false; // return nothing ##
- }
- // mode ##
- $twitter_mode = $args->mode; // convert to normal variable for switch - not sure why that's required? ##
- switch ( $twitter_mode ) {
- case "search": // search API ##
- $args->url = "https://api.twitter.com/1.1/search/tweets.json"; // url ##
- break;
- case "home_timeline": // home_timeline API ##
- $args->url = "https://api.twitter.com/1.1/statuses/home_timeline.json"; // url ##
- break;
- case "user_timeline": // user_timeline API - default ##
- default:
- $args->url = "https://api.twitter.com/1.1/statuses/user_timeline.json"; // url ##
- break;
- break;
- }
- // compile querystring ##
- $args->query = '?screen_name='.$args->username.'&include_rts='.$args->retweet.'&q='.urlencode($args->search).'&count='.$args->count;
- // prepare oAuth args ##
- $oauth = array(
- 'screen_name' => $args->username,
- 'count' => $args->count,
- 'include_rts' => $args->retweet,
- 'q' => ($args->search),
- //'include_entities' => true,
- 'oauth_consumer_key' => $args->consumer_key,
- 'oauth_nonce' => time(),
- 'oauth_signature_method' => 'HMAC-SHA1',
- 'oauth_token' => $args->oauth_access_token,
- 'oauth_timestamp' => time(),
- 'oauth_version' => '1.0'
- );
- $base_info = buildBaseString($args->url, 'GET', $oauth);
- $composite_key = rawurlencode($args->consumer_secret) . '&' . rawurlencode($args->oauth_access_token_secret);
- $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
- $oauth['oauth_signature'] = $oauth_signature;
- // Make CURL Request ##
- $header = array(buildAuthorizationHeader($oauth), 'Expect:');
- $options = array(
- CURLOPT_HTTPHEADER => $header,
- CURLOPT_HEADER => false,
- CURLOPT_URL => $args->url.$args->query,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_SSL_VERIFYPEER => false
- );
- $feed = curl_init();
- curl_setopt_array($feed, $options);
- $json = curl_exec($feed);
- curl_close($feed);
- // abort on error ##
- if ($json === false) {
- if ( $args->debug === true ) {
- echo 'CURL Error'; // echo an error, if debugging ##
- }
- return false;
- }
- // JSON decode returned data ##
- $twitter_data = json_decode($json);
- // optionally debug returned data ##
- if ( $args->debug === true ) pr($twitter_data);
- // search fix - object returned is nested deeper ##
- if ( $twitter_mode === 'search' ) { // array shift ##
- $twitter_data = $twitter_data->statuses;
- }
- // TODO -- some extra error checking here ##
- // return array of data ##
- return $twitter_data;
- }
- // build base string ##
- // credit - http://stackoverflow.com/a/12939923/591486
- if ( !function_exists('buildBaseString') ) {
- function buildBaseString($baseURI, $method, $params) {
- $r = array();
- ksort($params);
- foreach($params as $key=>$value){
- $r[] = "$key=" . rawurlencode($value);
- }
- return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
- }
- }
- // build authorizion header ##
- // credit - http://stackoverflow.com/a/12939923/591486
- if ( !function_exists('buildAuthorizationHeader') ) {
- function buildAuthorizationHeader($oauth) {
- $r = 'Authorization: OAuth ';
- $values = array();
- foreach($oauth as $key=>$value)
- $values[] = "$key=\"" . rawurlencode($value) . "\"";
- $r .= implode(', ', $values);
- return $r;
- }
- }
- // add href link to text string ##
- // credit - http://saturnboy.com/2010/02/parsing-twitter-with-regexp/
- if ( !function_exists('ftf_twitter_add_href') ) {
- function ftf_twitter_add_href ( $text ) {
- $text = preg_replace(
- '@(https?://([-\w\.]+)+(/([\w/_\.]*(\?\S+)?(#\S+)?)?)?)@',
- '<a href="$1" target="_blank" rel="nofollow">$1</a>',
- $text
- );
- return $text;
- }
- }
- // add href link to twitter username in text string ##
- // credit - http://saturnboy.com/2010/02/parsing-twitter-with-regexp/
- if ( !function_exists('ftf_twitter_add_username') ) {
- function ftf_twitter_add_username ( $text ) {
- $text = preg_replace(
- '/@(\w+)/',
- '<a href="http://twitter.com/$1" target="_blank" rel="nofollow">@$1</a>',
- $text
- );
- return $text;
- }
- }
- // add href link to twitter hashtag in text string ##
- // credit - http://saturnboy.com/2010/02/parsing-twitter-with-regexp/
- if ( !function_exists('ftf_twitter_add_hashtag') ) {
- function ftf_twitter_add_hashtag ( $text ) {
- $text = preg_replace(
- '/\s+#(\w+)/',
- ' <a href="http://search.twitter.com/search?q=%23$1" target="_blank" rel="nofollow">#$1</a>',
- $text
- );
- return $text;
- }
- }
- /*
- * convert an array to an object ##
- *
- * @param array $array
- * @return object
- * @since 0.1
- *
- */
- if ( !function_exists( 'ftf_array_to_object' ) ) {
- function ftf_array_to_object($array) {
- if(!is_array($array)) {
- return $array;
- }
- $object = new stdClass();
- if (is_array($array) && count($array) > 0) {
- foreach ($array as $name=>$value) {
- $name = strtolower(trim($name));
- if (!empty($name)) {
- $object->$name = ftf_array_to_object($value);
- }
- }
- return $object;
- }
- else {
- return FALSE;
- }
- }}
- /*
- * pretty print_r / var_dump ##
- */
- if ( !function_exists( 'pr' ) ) {
- function pr($var) {
- print '<pre class="var_dump">'; var_dump($var); print '</pre>';
- }} // pr ##
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement