Skip to content

Instantly share code, notes, and snippets.

@ericrallen
Created January 25, 2013 06:00
Show Gist options
  • Save ericrallen/4632128 to your computer and use it in GitHub Desktop.
Save ericrallen/4632128 to your computer and use it in GitHub Desktop.
Pulls in Twitter feed from specific twitter WordPress plug-in for developers and converts links to anchor tags
<?php
/*--------------------------------------------------------------------------
This pulls in tweets gathered from the WordPress plugin
ouath-twitter-feed-for-developers and converts them into
an <article> tag
It takes retweets into account and then updates links within
the tweet text to convert urls, tags, and hashes into anchor
tags
it is based slightly on a script found here:
http:stackoverflow.com/questions/11533214/php-how-to-use-the-twitter-apis-data-to-convert-urls-mentions-and-hastags-in#answer-11929224
--------------------------------------------------------------------------*/
function parse_message($tweet) {
if(!empty($tweet['entities'])) {
if(count($tweet['retweeted_status'])) {
$new_tweet = $tweet['retweeted_status']['text'];
$ent_array = $tweet['retweeted_status']['entities'];
} else {
$new_tweet = $tweet['text'];
$ent_array = $tweet['entities'];
}
foreach($ent_array as $area => $items) {
switch($area) {
case 'hashtags':
foreach($items as $item) {
$find = 'text';
$prefix = '#';
$url = 'http://twitter.com/search/?src=hash&q=%23';
get_new_tweet($find, $prefix, $url, $item, $new_tweet);
}
break;
case 'user_mentions':
foreach($items as $item) {
$find = 'screen_name';
$prefix = '@';
$url = 'https://twitter.com/';
get_new_tweet($find, $prefix, $url, $item, $new_tweet);
}
break;
case 'media': case 'urls':
foreach($items as $item) {
$find = 'url';
$prefix = '';
$url = '';
get_new_tweet($find, $prefix, $url, $item, $new_tweet);
}
break;
default:
break;
}
}
}
return $new_tweet;
}
function get_new_tweet($find, $prefix, $url, $item, $new_tweet) {
$string = $item[$find];
$href = $url . $string;
$start = $item['indices'][0];
$stop = $item['indices'][1];
$replace = $prefix . $string;
$with = '<a href="' . $href . '">' . $prefix . $string .'</a>';
$new_tweet = str_replace($replace, $with, $new_tweet);
}
$local_tweets = '';
//check for cache from 15 minutes ago
if(get_transient('jled_tweets_short')) {
$local_tweets = get_transient('jled_tweets_short');
//if it isn't found, try to update tweets
} else {
$tweets = getTweets(3);
//if Twitter returned results
if(count($tweets)) {
//loop through it and output tweets
foreach ($tweets as $entry) {
$text = parse_message($entry);
$date = date('g:i A - j M y', strtotime($entry['created_at']));
$id = $entry['id'];
$local_tweets .= '<article id="tweet-' . $id . '" class="tweet">' .
'<p>' . $text . '</p>' .
'<p><span>' . $date . '</span></p>' .
'</article>';
}
//reset our 15 min and 1 day caches
set_transient('jled_tweets_long', $local_tweets, 60 * 60 * 24 * 1);
set_transient('jled_tweets_short', $local_tweets, 60 * 15);
//if Twitter didn't respond
} else {
//check for the 1 day cache
if(get_transient('jled_tweets_long')) {
$local_tweets = get_transient('jled_tweets_long');
//if we can't find that, respond with an error message
} else {
$local_tweets = '<article class="tweet"><p><strong>No tweets at this time.</strong></p>';
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment