Created
July 14, 2012 07:39
-
-
Save floq-design/3109871 to your computer and use it in GitHub Desktop.
Wordpress twitter feed with cache template fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// source: http://spaceninja.com/2009/07/twitter-php-caching/ | |
// | |
// define $twitterUsername and $twitterTweets in functions.php | |
// | |
global $twitterUsername, $twitterTweets; | |
define("SECOND", 1); | |
define("MINUTE", 60 * SECOND); | |
define("HOUR", 60 * MINUTE); | |
define("DAY", 24 * HOUR); | |
define("MONTH", 30 * DAY); | |
function relativeTime($time) { | |
$delta = strtotime('+2 hours') - $time; | |
if ($delta < 2 * MINUTE) { | |
return "1 min ago"; | |
} | |
if ($delta < 45 * MINUTE) { | |
return floor($delta / MINUTE) . " mins ago"; | |
} | |
if ($delta < 90 * MINUTE) { | |
return "1 hour ago"; | |
} | |
if ($delta < 24 * HOUR) { | |
return floor($delta / HOUR) . " hours ago"; | |
} | |
if ($delta < 48 * HOUR) { | |
return "Yesterday"; | |
} | |
if ($delta < 30 * DAY) { | |
return floor($delta / DAY) . " days ago"; | |
} | |
if ($delta < 12 * MONTH) { | |
$months = floor($delta / DAY / 30); | |
return $months <= 1 ? "1 month ago" : $months . " months ago"; | |
} else { | |
$years = floor($delta / DAY / 365); | |
return $years <= 1 ? "1 year ago" : $years . " years ago"; | |
} | |
} | |
function parse_cache_feed($usernames, $limit) { | |
$username_for_feed = str_replace(" ", "+OR+from%3A", $usernames); | |
$feed = "http://search.twitter.com/search.atom?q=from%3A" . $username_for_feed . "&rpp=" . $limit; | |
$usernames_for_file = str_replace(" ", "-", $usernames); | |
$cache_file = get_template_directory() . '/' . $usernames_for_file . '-twitter-cache'; | |
$last = filemtime($cache_file); | |
$now = time(); | |
$interval = 600; // ten minutes | |
// check the cache file | |
if ( !$last || (( $now - $last ) > $interval) ) { | |
// cache file doesn't exist, or is old, so refresh it | |
$cache_rss = file_get_contents($feed); | |
if (!$cache_rss) { | |
// we didn't get anything back from twitter | |
echo "<!-- ERROR: Twitter feed was blank! Using cache file. -->"; | |
} else { | |
// we got good results from twitter | |
echo "<!-- SUCCESS: Twitter feed used to update cache file -->"; | |
$cache_static = fopen($cache_file, 'wb'); | |
fwrite($cache_static, serialize($cache_rss)); | |
fclose($cache_static); | |
} | |
// read from the cache file | |
$rss = @unserialize(file_get_contents($cache_file)); | |
} | |
else { | |
// cache file is fresh enough, so read from it | |
echo "<!-- SUCCESS: Cache file was recent enough to read from -->"; | |
$rss = @unserialize(file_get_contents($cache_file)); | |
} | |
// clean up and output the twitter feed | |
$feed = str_replace("&", "&", $rss); | |
$feed = str_replace("<", "<", $feed); | |
$feed = str_replace(">", ">", $feed); | |
$clean = explode("<entry>", $feed); | |
$clean = str_replace(""", "\"", $clean); | |
$clean = str_replace("'", "'", $clean); | |
$amount = count($clean) - 1; | |
if ($amount) { // are there any tweets? | |
for ($i = 1; $i <= $amount; $i++) { | |
$entry_close = explode("</entry>", $clean[$i]); | |
$clean_content_1 = explode("<content type=\"html\">", $entry_close[0]); | |
$clean_content = explode("</content>", $clean_content_1[1]); | |
$clean_name_2 = explode("<name>", $entry_close[0]); | |
$clean_name_1 = explode("(", $clean_name_2[1]); | |
$clean_name = explode(")</name>", $clean_name_1[1]); | |
$clean_user = explode(" (", $clean_name_2[1]); | |
$clean_lower_user = strtolower($clean_user[0]); | |
$clean_uri_1 = explode("<uri>", $entry_close[0]); | |
$clean_uri = explode("</uri>", $clean_uri_1[1]); | |
$clean_time_1 = explode("<published>", $entry_close[0]); | |
$clean_time = explode("</published>", $clean_time_1[1]); | |
$unix_time = strtotime($clean_time[0]); | |
$pretty_time = relativeTime($unix_time); | |
?> | |
<!-- format markup however required --> | |
<p class="content"> | |
<?php echo $clean_content[0]; ?> | |
</p> | |
<p class="time"> | |
<a href="http://twitter.com/<?php echo $username_for_feed ?>" target="_blank"> | |
<?php echo $pretty_time; ?> | |
</a> | |
</p> | |
<?php | |
} | |
} | |
} | |
parse_cache_feed($twitterUsername, $twitterTweets); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment