Skip to content

Instantly share code, notes, and snippets.

@icco
Forked from anonymous/functions.php
Created January 24, 2010 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icco/285020 to your computer and use it in GitHub Desktop.
Save icco/285020 to your computer and use it in GitHub Desktop.
<?php
/**
* @package WordPress
* @subpackage Classic_Theme
*/
if ( function_exists('register_sidebar') )
register_sidebar(array(
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '',
'after_title' => '',
));
/**
* Regex's a twitter message to make it have links like all the cool kids.
*
* ex.
* @username = http://twitter.com/username
* #hashtag = http://twitter.com/search?q=%23hashtag
* http://something = <a href....
*
* @author Nat Welch
*/
function twitterParse($in)
{
$pieces = explode(" ", $in);
foreach($pieces as &$word)
{
if($word) {
if($word[0] == "@")
$word = "<a href=\"http://twitter.com/" . preg_replace("/(@|[^[:alnum:]])/","",$word) . "/\" >" . $word . "</a>";
else if($word[0] == "#")
$word = '<a href="http://twitter.com/search?q=%23' . preg_replace("/#/","",$word) . '" >' . $word . "</a>";
else if($word[0] == "h")
$word = preg_replace('`((?:https?|ftp)://(\S+[[:alnum:]])/?)`','<a href="$1" alt="\2">\1</a>', $word);
}
}
return implode(" ",$pieces);
}
/*
* Calculates the time since an event
*/
function time_since($original) {
// array of time period chunks
$chunks = array(
array(60 * 60 * 24 * 365 , 'year'),
array(60 * 60 * 24 * 30 , 'month'),
array(60 * 60 * 24 * 7, 'week'),
array(60 * 60 * 24 , 'day'),
array(60 * 60 , 'hour'),
array(60 , 'minute'),
);
// Parse out an integer.
$original = strtotime($original);
$today = time(); /* Current unix time */
$since = $today - $original;
if($since > 604800) {
$print = date("M jS", $original);
if($since > 31536000)
$print .= ", " . date("Y", $original);
return $print;
}
// $j saves performing the count function each time around the loop
for ($i = 0, $j = count($chunks); $i < $j; $i++) {
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
// finding the biggest chunk (if the chunk fits, break)
if (($count = floor($since / $seconds)) != 0)
break;
}
$print = ($count == 1) ? '1 '.$name : "$count {$name}s";
return $print . " ago";
}
function pullFeed($url, $count = 5) {
$items = array();
try {
// We don't have the Curl class and remote fopen is dissabled.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TRANSFERTEXT, 1);
$xml = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception('Curl error: ' . curl_error($ch));
} else {
libxml_use_internal_errors(true);
$feed = new SimpleXMLElement($xml);
if (!$feed)
throw new Exception('Error parsing twitter xml for blog.');
}
curl_close($ch);
} catch (Exception $e) {
return array();
}
foreach ($feed->entry as $post) {
// We don't want "replies" in our feed.
$text = substr($post->content[0], strpos($post->content[0], ':') + 2);
if(!(strpos($text, '@') === 0))
$items[] = $post;
}
return array_splice($items, 0, $count);
}
function getTwitter($count) {
$twiterUrl = 'http://twitter.com/statuses/user_timeline/ifixit.atom';
$mem = Utils::bytecache();
$tweetKey = "twitter_cache";
$reset = false;
if($reset || !($ret = $mem->get($tweetKey))) {
$tweets = pullFeed($twiterUrl, $count);
$ret = array();
foreach($tweets as $tweet) {
$text = $tweet->content[0];
$html = twitterParse(substr($text, strpos($text, ':') + 2));
$date = time_since($tweet->published);
$link = $tweet->link[0]['href'];
$together = $html . ' <a href="' . $link . '" target="_blank" class="tweet_date">' . $date . '</a> ';
$ret[] = $together;
}
$mem->set($tweetKey, $ret, CACHE_SHORT);
}
// Return a nice error if something went wrong. Else return parsed feed.
return $ret ? $ret : array("There was an error retrieving the stream.");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment