Created
August 29, 2012 14:13
-
-
Save carldanley/3513176 to your computer and use it in GitHub Desktop.
CD Twitter Caching
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 | |
//------------------------------------------------------------------------------ | |
class CD_Twitter{ | |
protected $_base_twitter_url = 'https://api.twitter.com/1/statuses/user_timeline.json?'; | |
protected $_default_options = array( | |
'include_entities' => true, | |
'include_rts' => true, | |
'screen_name' => 'carldanley', | |
'count' => 10 | |
); | |
protected $_cached_tweets_key = 'cd_cached_tweets'; | |
protected $_expiration_time = 300; | |
protected $_tweets_to_show = 5; | |
public function __construct(){ | |
$this->_check_tweets(); | |
} | |
public function render_tweets(){ | |
$tweets = $this->_check_tweets(); | |
//render a basic html layout which can be changed entirely | |
$html = array(); | |
$html[] = '<div class="cd-tweets">'; | |
$counter = 0; | |
foreach( $tweets as $tweet ){ | |
if( $counter === $this->_tweets_to_show ){ | |
break; | |
} | |
$html[] = '<div class="tweet">'; | |
$html[] = '<div class="left icon twitter"></div>'; | |
$html[] = '<div class="left text">'; | |
$html[] = '<p>' . $tweet[ 'text' ] . '</p>'; | |
$html[] = '<p class="time"><a href="' . $tweet[ 'url' ] . '">' . $tweet[ 'friendly_timestamp' ] . '→</a></p>'; | |
$html[] = '</div>'; | |
$html[] = '<div class="clear"></div>'; | |
$html[] = '</div>'; | |
$counter++; | |
} | |
$html[] = '</div'; | |
$html = apply_filters( 'render-tweets', implode( '', $html ), $tweets ); | |
echo $html; | |
} | |
private function _check_tweets(){ | |
//check to see if the tweets exist | |
$tweets = get_transient( $this->_cached_tweets_key ); | |
if( $tweets === false ){ | |
//get the most recent tweets | |
$tweets = $this->_get_twitter_feed(); | |
if( $tweets === false ){ | |
return array(); | |
} | |
//set the transient now | |
set_transient( $this->_cached_tweets_key, $tweets, $this->_expiration_time ); | |
} | |
return $tweets; | |
} | |
private function _get_twitter_feed(){ | |
//build the base url for querying | |
$url = $this->_base_twitter_url . http_build_query( $this->_default_options ); | |
return $this->_retrieve_tweets( $url ); | |
} | |
private function _retrieve_tweets( $url ){ | |
$response = wp_remote_get( $url, array( 'sslverify' => false ) ); | |
$response_code = wp_remote_retrieve_response_code( $response ); | |
if( $response_code !== 200 ){ | |
return false; | |
} | |
$body = wp_remote_retrieve_body( $response ); | |
$tweets = $this->_build_twitter_data( $body ); | |
if( empty( $tweets ) === false ){ | |
return $tweets; | |
} | |
else{ | |
return false; | |
} | |
} | |
private function _build_twitter_data( $tweets ){ | |
$tweets = json_decode( $tweets, true ); | |
$data = array(); | |
foreach( $tweets as $tweet ){ | |
//build out the data for this tweet, including the html to be displayed | |
$tmp = array( | |
'created_at' => $tweet[ 'created_at' ], | |
'id' => $tweet[ 'id_str' ], | |
'text' => $this->_build_tweet_text( $tweet ), | |
'friendly_timestamp' => $this->_create_friendly_timestamp( $tweet[ 'created_at' ] ), | |
'url' => 'https://twitter.com/' . $this->_default_options[ 'screen_name' ] . '/status/' . $tweet[ 'id_str' ] | |
); | |
$data[] = $tmp; | |
} | |
return $data; | |
} | |
private function _build_tweet_text( $tweet ){ | |
//we need to format the tweet to add the href's for the user mentions and hashtags and links | |
$text = $tweet[ 'text' ]; | |
$entities = $tweet[ 'entities' ]; | |
//start off replacing the mentions with profile links | |
foreach( $entities[ 'user_mentions' ] as $mention ){ | |
$user = '@' . $mention[ 'screen_name' ]; | |
$link = '<a href="' . 'https://twitter.com/' . $mention[ 'screen_name' ] . '">' . $user . '</a>'; | |
$text = str_replace( $user, $link, $text ); | |
} | |
//now replace hashtags | |
foreach( $entities[ 'hashtags' ] as $hashtag ){ | |
$tag = '#' . $hashtag[ 'text' ]; | |
$link = '<a href="https://twitter.com/#!/search/?q=' . urlencode( $tag ) . '">' . $tag . '</a>'; | |
$text = str_replace( $tag, $link, $text ); | |
} | |
//now replace links | |
foreach( $entities[ 'urls' ] as $url ){ | |
$text = str_replace( $url[ 'url' ], '<a href="' . $url[ 'expanded_url' ] . '">' . $url[ 'url' ] . '</a>', $text ); | |
} | |
return $text; | |
} | |
private function _create_friendly_timestamp( $date ){ | |
// Make sure the server is using the right time zone | |
if ( get_option( 'timezone_string' ) && strpos( get_option( 'timezone_string' ), 'UTC' ) === false ){ | |
date_default_timezone_set( get_option( 'timezone_string' ) ); | |
} | |
// Get the time difference in seconds | |
$post_time = strtotime( $date ); | |
$current_time = time(); | |
$time_difference = $current_time - $post_time; | |
// Seconds per... | |
$minute = 60; | |
$hour = 3600; | |
$day = 86400; | |
$week = $day * 7; | |
$month = $day * 31; | |
$year = $day * 366; | |
// if over 3 years | |
if ( $time_difference > $year * 3 ) { | |
$friendly_date = __( 'a long while ago', BNM_LOCALE ); | |
} | |
// if over 2 years | |
else if ( $time_difference > $year * 2 ) { | |
$friendly_date = __( 'over 2 years ago', BNM_LOCALE ); | |
} | |
// if over 1 year | |
else if ( $time_difference > $year ) { | |
$friendly_date = __( 'over a year ago', BNM_LOCALE ); | |
} | |
// if over 11 months | |
else if ( $time_difference >= $month * 11 ) { | |
$friendly_date = __( 'about a year ago', BNM_LOCALE ); | |
} | |
// if over 2 months | |
else if ( $time_difference >= $month * 2 ) { | |
$months = (int) $time_difference / $month; | |
$friendly_date = sprintf( __( '%d months ago', BNM_LOCALE ), $months ); | |
} | |
// if over 4 weeks ago | |
else if ( $time_difference > $week * 4 ) { | |
$friendly_date = __( 'last month', BNM_LOCALE ); | |
} | |
// if over 3 weeks ago | |
else if ( $time_difference > $week * 3 ) { | |
$friendly_date = __( '3 weeks ago', BNM_LOCALE ); | |
} | |
// if over 2 weeks ago | |
else if ( $time_difference > $week * 2 ) { | |
$friendly_date = __( '2 weeks ago', BNM_LOCALE ); | |
} | |
// if equal to or more than a week ago | |
else if ( $time_difference >= $day * 7 ) { | |
$friendly_date = __( 'last week', BNM_LOCALE ); | |
} | |
// if equal to or more than 2 days ago | |
else if ( $time_difference >= $day * 2 ) { | |
$days = (int) $time_difference / $day; | |
$friendly_date = sprintf( __( '%d days ago', BNM_LOCALE ), $days ); | |
} | |
// if equal to or more than 1 day ago | |
else if ( $time_difference >= $day ) { | |
$friendly_date = __( 'yesterday', BNM_LOCALE ); | |
} | |
// 2 or more hours ago | |
else if ( $time_difference >= $hour * 2 ) { | |
$hours = (int) $time_difference / $hour; | |
$friendly_date = sprintf( __( '%d hours ago', BNM_LOCALE ), $hours ); | |
} | |
// 1 hour ago | |
else if ( $time_difference >= $hour ) { | |
$friendly_date = __( 'an hour ago', BNM_LOCALE ); | |
} | |
// 2?59 minutes ago | |
else if ( $time_difference >= $minute * 2 ) { | |
$minutes = (int) $time_difference / $minute; | |
$friendly_date = sprintf( __( '%d minutes ago', BNM_LOCALE ), $minutes ); | |
} | |
else { | |
$friendly_date = __( 'just now', BNM_LOCALE ); | |
} | |
// HTML 5 FTW | |
return '<time title="' . $date . '" datetime="' . date( 'c', strtotime( $date ) ) . '" pubdate>' . ucfirst( $friendly_date ) . '</time>'; | |
} | |
} | |
//------------------------------------------------------------------------------ | |
$cd_twitter = new CD_Twitter(); | |
//------------------------------------------------------------------------------ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment