function parseTweets($results = array()) { $tweets = array(); if (!empty($results) && !isset($results['error'])) { foreach ($results as $result) { $temp = explode(' ', $result['created_at']); $timestamp = $temp[2] . ' ' . $temp[1] . ' ' . $temp[5]; $tweets[] = array('timestamp' => $timestamp, 'text' => twitterify($result['text'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH), 'id' => $result['id_str']); } } return $tweets; }
function getLastXTwitterStatus($userid, $x) { $cache_file = get_template_directory() . '/cache_theme/twitter_' . $userid . '.txt'; // Seconds to cache feed (1 hour). $cachetime = 60 * 60; // Time that the cache was last filled. $cache_file_created = @file_exists($cache_file) ? @filemtime($cache_file) : 0; // Show file from cache if still valid. if (time() - $cachetime < $cache_file_created) { include $cache_file; } else { try { $url = "http://twitter.com/statuses/user_timeline/{$userid}.xml?count={$x}"; $statuses = simplexml_load_file($url); ob_start(); ?> <div id="ticker-wrapper" class="no-js"> <?php if (!empty($statuses->status)) { ?> <ul id="js-news" class="js-hidden"> <?php foreach ($statuses->status as $status) { ?> <li class="status-item"><a href="<?php echo 'http://twitter.com/' . $twitter_user . '/statuses/' . $status->id; ?> "><?php echo twitterify($status->text); ?> </a></li> <?php } ?> </ul> <?php } ?> </div><!-- #ticker-wrapper --> <?php $file = @fopen($cache_file, 'w'); // Save the contents of output buffer to the file, and flush the buffer. @fwrite($file, ob_get_contents()); @fclose($file); ob_end_flush(); } catch (Excetion $e) { $result = new StdClass(); $result->status = array(); return $result; } } }
function widget($args, $instance) { $theme_options = get_option('option_tree'); if (!function_exists('twitterify')) { function twitterify($ret) { $ret = preg_replace("#(^|[\n ])([\\w]+?://[\\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret); $ret = preg_replace("#(^|[\n ])((www|ftp)\\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret); $ret = preg_replace("/@(\\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret); $ret = preg_replace("/#(\\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret); return $ret; } } if (!function_exists('twitterTimeAgo')) { function twitterTimeAgo($oldTime, $newTime) { $timeCalc = $newTime - $oldTime; if ($timeCalc > 60 * 60 * 24) { $timeCalc = round($timeCalc / 60 / 60 / 24) . __(" days ago", 'delaware-theme'); } else { if ($timeCalc > 60 * 60) { $timeCalc = round($timeCalc / 60 / 60) . __(" hours ago", 'delaware-theme'); } else { if ($timeCalc > 60) { $timeCalc = round($timeCalc / 60) . __(" minutes ago", 'delaware-theme'); } else { if ($timeCalc > 0) { $timeCalc .= __(" seconds ago", 'delaware-theme'); } } } } return $timeCalc; } } extract($args); extract($instance); $title = apply_filters($this->slug, $title); if (empty($count)) { $count = 3; } if (empty($theme_options['oauth_access_token']) || empty($theme_options['oauth_access_token_secret']) || empty($theme_options['consumer_key']) || empty($theme_options['consumer_secret'])) { _e('Please make sure you have entered all necessary Twitter API Keys under Theme Options -> Settings -> Twitter', 'delaware-theme'); } else { /** Set access tokens here - see: https://dev.twitter.com/apps/ **/ $settings = array('oauth_access_token' => $theme_options['oauth_access_token'], 'oauth_access_token_secret' => $theme_options['oauth_access_token_secret'], 'consumer_key' => $theme_options['consumer_key'], 'consumer_secret' => $theme_options['consumer_secret']); $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'; $getfield = '?count=' . $count; $requestMethod = 'GET'; $twitter = new TwitterAPIExchange($settings); $tweets = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(); $tweets = json_decode($tweets); # generate tweet list $our_tweets = '<ul class="tweet_list">'; foreach ($tweets as $tweet) { $tweetdate = new DateTime($tweet->created_at); $tweetdate = strtotime($tweetdate->format('Y-m-d H:i:s')); $currentdate = strtotime(date('Y-m-d H:i:s')); $days = twitterTimeAgo($tweetdate, $currentdate); $our_tweets .= '<li class="tweet_first tweet_odd"><div>'; $our_tweets .= '<span class="tweet_join"></span>'; $our_tweets .= '<span class="tweet_text">' . twitterify($tweet->text) . '</span>'; $our_tweets .= '<span class="tweet_time"><a href="http://twitter.com/' . $tweet->user->screen_name . '/status/' . $tweet->id . '">' . __('about', 'delaware-theme') . ' ' . $days . '</a></span>'; $our_tweets .= '</div></li>'; } $our_tweets .= '</ul>'; //fallback $text_url = isset($text_url) ? $text_url : ''; $text_loading = isset($text_loading) ? esc_attr($text_loading) : 'loading tweets...'; $title = isset($title) ? $before_title . do_shortcode($title) . $after_title : ''; echo "\n {$before_widget}\n {$title}\n {$our_tweets}\n {$after_widget}"; } }
$twitterImage = str_replace("http:", "https:", $twitterImage); $tweetUrl = $item[tw_tweet_url]; /*person quoted*/ if (isset($m['title'])) { $quoteMakerName = $m['title']; $quoteMakerName = str_replace("on Twitter", "", $quoteMakerName); } if (isset($m['author_name'])) { $quoteMakerHandle = $m['author_name']; } if (isset($m['url'])) { $quotedTweetUrl = $m['url']; } if (isset($m['description'])) { $quotedTweet = $m['description']; $quotedTweet = twitterify($quotedTweet); } if (isset($m['thumbnail_url'])) { $quoteMakerImage = $m['thumbnail_url']; $quoteMakerImage = str_replace("http:", "https:", $quoteMakerImage); } } else { if (isset($m['title'])) { $title = $m['title']; /*trying to remove offending news org credits */ $search = array(' - BBC News', ' - BBC', ' - BBC World Service', ' - CNN.com', ' - CNNPolitics.com', ' - FT.com', ' - Premium Times Nigeria', ' | Reuters', ' | Reuters.com'); $title = str_replace($search, '', $title); } if (isset($m['url'])) { $url = $m['url']; }
function widget($args, $instance) { extract($args); $title = apply_filters('widget_title', empty($instance['title']) ? __('Twitter Update', 'wpdance') : $instance['title'], $instance, $this->id_base); $cache_subname = implode("_", array_map(array($this, "to_good_name"), $instance)); $instance['username'] = esc_attr($instance['username']); //change your own key here $_consumer_key = "diqJlQnUitiOrqoJTGOK8Q"; $_consumer_secret = "RZ8U79iAAooscyd0fZjzAoqBfdrgLNehm0QJMabA"; $_access_token = "859121024-nayLlfxEnETi0bbM1tPjrm3WWkrxHIk5a6WpppxJ"; $_access_token_secret = "9aEuTIDHnohxVcv7UFzctQYOwDSOgcx1GKq9ELAnI"; //end key config echo $before_widget; if (!empty($title)) { echo $before_title . $title . $after_title; } if ($instance['username']) { $cache_file = get_template_directory() . '/cache_theme/twitter_' . $cache_subname . '.txt'; // Seconds to cache feed (1 hour). $cachetime = 60 * 60; // Time that the cache was last filled. $cache_file_created = @file_exists($cache_file) ? @filemtime($cache_file) : 0; // Show file from cache if still valid. if (time() - $cachetime < $cache_file_created) { include $cache_file; } else { try { if (!($limit = $instance['limit'])) { $limit = 5; } $connection = new TwitterOAuth($_consumer_key, $_consumer_secret, $_access_token, $_access_token_secret); $statuses = $connection->get('statuses/user_timeline', array('screen_name' => $instance['username'], 'include_rts' => 1, 'count' => $limit)); ob_start(); $_success = true; if (isset($statuses->errors)) { $_success = false; } ?> <div id="twitter-box" style="overflow: hidden;"> <?php if (!empty($statuses) && $_success) { ?> <ul id="twitter-list"> <?php $i = 0; $class = ""; foreach ($statuses as $status) { $class = $i == 0 ? 'first' : ($i == count($statuses) - 1 ? "last" : ''); $i++; ?> <li class="status-item <?php echo esc_attr($class); ?> "> <span class="tweet-content"><?php echo twitterify($status->text); ?> </span> <div class="name-time"></div> <div class="date-time"><a href="<?php echo 'http://twitter.com/' . $status->user->screen_name . '/statuses/' . $status->id; ?> "><?php echo $this->relativeTime($status->created_at); ?> </a></div> </li> <?php } ?> </ul> <a href="http://twitter.com/<?php echo esc_attr($instance['username']); ?> "><span class="twitter-follow"></span></a> <?php } ?> </div><!-- #ticker-wrapper --> <?php $file = @fopen($cache_file, 'w'); // Save the contents of output buffer to the file, and flush the buffer. @fwrite($file, ob_get_contents()); @fclose($file); ob_end_flush(); } catch (Excetion $e) { $result = new StdClass(); $result->status = array(); return $result; } } } echo $after_widget; }
/** * show_twitter_feed * * * * * */ function get_twitter_feed($username, $limit = 10) { if (empty($username)) { return false; } // $user_id = get_twitter_userid($username); // $url = 'http://api.twitter.com/1/statuses/user_timeline.rss?user_id='.$user_id.'&count='.$limit; $url = 'http://api.twitter.com/1/statuses/user_timeline.rss?count=' . $limit . '&screen_name=' . $username; $tweets = import_rss($url, $limit); if (empty($tweets)) { return false; } $html = ' <ul class="twitter_feed">'; foreach ($tweets as $tweet) { // format date $tweet_date = '<a href="' . $tweet['link'] . '">' . date('F d \\a\\t H:i', strtotime($tweet['date'])) . '</a>'; // get rid of the 'username: '******'description']); $tweet_text = twitterify($tweet_text); // output $html .= ' <li> <span class="tweet">' . $tweet_text . '</span> <span class="tweet_date">' . $tweet_date . '</span> </li>'; } $html .= ' <li> <span class="twitter_link"> <a href="http://twitter.com/' . $username . '">follow me on Twitter</a> </span> </li> </ul>'; return $html; }
?> "><?php echo $link['tweet']; ?> </a></h3> <p><img src="<?php echo $link['from_user']['profile_image_url']; ?> " width="73" height="73" align="left" /> <a href="http://twitter.com/<?php echo $link['from_user']['screen_name']; ?> "><?php echo $link['from_user']['name']; ?> </a>: <?php echo twitterify($link['tweet']); ?> <a href="http://twitter.com/<?php echo $link['from_user']['screen_name']; ?> /status/<?php echo $link['tweet_id']; ?> ">★</a></p> <?php } ?> <p><?php echo date('r', strtotime($link['created_at']));
$signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true))); // this time we're using a normal GET query, and we're only encoding the query params // (without the oauth params) $url .= "?" . http_build_query($query); $url = str_replace("&", "&", $url); //Patch by @Frewuill $oauth['oauth_signature'] = $signature; // don't want to abandon all that work! ksort($oauth); // probably not necessary, but twitter's demo does it // also not necessary, but twitter's demo does this too function add_quotes($str) { return '"' . $str . '"'; } $oauth = array_map("add_quotes", $oauth); // this is the full value of the Authorization line $auth = "OAuth " . urldecode(http_build_query($oauth, '', ', ')); // if you're doing post, you need to skip the GET building above // and instead supply query parameters to CURLOPT_POSTFIELDS $options = array(CURLOPT_HTTPHEADER => array("Authorization: {$auth}"), CURLOPT_HEADER => false, CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false); // do our business $feed = curl_init(); curl_setopt_array($feed, $options); $json = curl_exec($feed); //print_r($json); die; curl_close($feed); $twitter_data = (array) json_decode($json); foreach ($twitter_data as $tval) { echo twitterify($tval->text) . "<br>"; }
/** * show_twitter_feed * * * * * */ function get_twitter_feed($username, $limit = 10) { if (empty($username)) { return false; } $url = 'http://twitter.com/statuses/user_timeline/' . $username . '.rss'; $tweets = import_rss($url, $limit); if (empty($tweets)) { return false; } $html = ' <ul class="twitter_feed">'; foreach ($tweets as $tweet) { // format date $tweet_date = '<a href="' . $tweet['link'] . '">' . date('F d \\a\\t H:i', strtotime($tweet['date'])) . '</a>'; // get rid of the 'username: '******'description']); // replace @replies with link to other user's Twitter page //$reply_pattern = '/@([A-Za-z0-9_-]+) /'; //$reply_link = '<a href="http://twitter.com/$1/" target="_blank">@$1</a> '; //$tweet_text = preg_replace($reply_pattern, $reply_link, $tweet_text); $tweet_text = twitterify($tweet_text); // output $html .= ' <li> <span class="tweet">' . $tweet_text . '</span> <span class="tweet_date">' . $tweet_date . '</span> </li>'; } $html .= ' <li> <span class="twitter_link"> <a href="http://twitter.com/' . $username . '">follow me on Twitter</a> </span> </li> </ul>'; return $html; }