Skip to content

Instantly share code, notes, and snippets.

@shawnsandy
Created December 8, 2012 23:46
Show Gist options
  • Save shawnsandy/4242559 to your computer and use it in GitHub Desktop.
Save shawnsandy/4242559 to your computer and use it in GitHub Desktop.
WordPress Twitter Feed class / plugin
<?php
/*
Plugin Name: EXT_Tweets
Plugin URI: http://blog.shawnsandy.com
Description: A WordPress drop-in plugin class for loading and displaying tweets. (Add to mu-plugin directory for auto install / use)
Version: 0.1 - beta
Author: Shawn Sandy
Author URI: http://shawnsandy.com
License: GPL2
*/
/**
* @link http://www.problogdesign.com/wordpress/how-to-use-the-twitter-api-in-wordpress/ (Original source credits -- modified)
*/
class EXT_Tweets {
private $username = 'WordPress',
$api_request_url = null,
$transient_name = 'al-tweet-list',
$api_url = 'https://api.twitter.com/1/statuses/',
$trans_lifetime = 60,
$tweet_count = 5,
$tweets,
$cacheTime = 5;
public function get_api_request_url() {
return $this->api_request_url;
}
public function get_api_url() {
return $this->api_url;
}
function __construct() {
}
public function get_tweets() {
return $this->tweets;
}
public function set_tweets($tweets) {
$this->tweets = $tweets;
}
public function set_username($username) {
$this->username = $username;
return $this;
}
public function set_api_request_url($query_string) {
$this->api_request_url = $query_string;
return $this;
}
public function set_transient_name($transient_name) {
$this->transient_name = $transient_name;
return $this;
}
public function set_api_url($api_url) {
$this->api_url = $api_url;
return $this;
}
public function set_trans_lifetime($trans_lifetime) {
$this->trans_lifetime = $trans_lifetime;
return $this;
}
public function set_tweet_count($tweet_count) {
$this->tweet_count = $tweet_count;
return $this;
}
public static function factory() {
$factory = new EXT_Tweets();
return $factory;
}
public function load_tweets() {
// Do we already have saved tweet data? If not, lets get it.
if (false === ($this->tweets = get_transient($this->transient_name) )) :
// Get the tweets from Twitter.
$url = $this->api_request_url;
$response = wp_remote_get($url);
// $response = wp_remote_get("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=WordPress&count=5&exclude_replies=false");
// If we didn't find tweets, use the previously stored values.
if (!is_wp_error($response) && $response['response']['code'] == 200) :
// Get tweets into an array.
//$tweets_json = json_decode($response['body'], true);
$this->tweets = json_decode($response['body'], true);
// Now update the array to store just what we need.
// (Done here instead of PHP doing this for every page load)
foreach ($this->tweets as $tweet) :
// Core info.
$name = $tweet['user']['name'];
$permalink = 'http://twitter.com/#!/' . $name . '/status/' . $tweet['id_str'];
/* Alternative image sizes method: http://dev.twitter.com/doc/get/users/profile_image/:screen_name */
$image = $tweet['user']['profile_image_url'];
// Message. Convert links to real links.
$pattern = '/http:(\S)+/';
$replace = '<a href="${0}" target="_blank" rel="nofollow">${0}</a>';
$text = preg_replace($pattern, $replace, $tweet['text']);
// Need to get time in Unix format.
$time = $tweet['created_at'];
$time = date_parse($time);
$uTime = mktime($time['hour'], $time['minute'], $time['second'], $time['month'], $time['day'], $time['year']);
// Now make the new array.
$tweets[] = array(
'text' => $text,
'name' => $name,
'permalink' => $permalink,
'image' => $image,
'time' => $uTime
);
endforeach;
// Save our new transient, and update the backup.
set_transient($this->transient_name, $tweets, $this->trans_lifetime * $this->cacheTime);
update_option('backup-' . $this->transient_name, $tweets);
else : // i.e. Fetching new tweets failed.
$this->tweets = get_option('backup-' . $this->transient_name); // False if there has never been data saved.
endif;
endif;
return $this;
}
public function display_tweets() {
//load up tweets
$this->load_tweets();
ob_start();
if ($this->tweets) :
?>
<ul class="bj-tweets">
<?php foreach ($this->tweets as $tweet) : ?>
<li>
<img src="<?php echo $tweet['image']; ?>" width="48" height="48" alt="" />
<div class="tweet-inner">
<p>
<?php echo $tweet['name'] . ': ' . $tweet['text']; ?>
<span class="tweet-time"><?php echo human_time_diff($tweet['time'], current_time('timestamp')); ?> ago</span>
</p>
</div><!-- /tweet-inner -->
</li>
<?php endforeach; ?>
</ul>
<p class="author-link">
<a href="http://twitter.com/#!/<?php echo $tweet['name']; ?>" target="_blank">Follow us on Twitter</a>
</p>
<?php else : ?>
<p>Sorry no tweets found.</p>
<?php endif; ?>
<?php
$tweets = ob_get_clean();
return $tweets;
}
/**
* Display a list theme options twitter user tweets
*/
public static function default_site_tweets($tweet_count = 5, $retweets = false) {
$user = get_theme_mod('bjc_twitter_username', 'WordPress');
$my_tweets = EXT_Tweets::factory()
->set_transient_name('default-tweet')
->set_api_request_url('http://api.twitter.com/1/statuses/user_timeline.json?count=' . $tweet_count . '&screen_name=' . $user . '&include_rts=' . $retweets . 'false')
->load_tweets();
echo $my_tweets->display_tweets();
}
/**
* Get a list of of user tweets
* @param String $user - WordPress / Twitter user name
* @param init $tweet_count - 5 / Number of tweets
* @param bool $retweets - false / display retweets
*
*/
public static function user_tweets($user = 'WordPress', $tweet_count = 5, $retweets = false) {
$my_tweets = EXT_Tweets::factory()
->set_transient_name('default-tweet')
->set_api_request_url('http://api.twitter.com/1/statuses/user_timeline.json?count=' . $tweet_count . '&screen_name=' . $user . '&include_rts=' . $retweets . 'false')
->load_tweets();
echo $my_tweets->display_tweets();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment