Skip to content

Instantly share code, notes, and snippets.

@a-fro
Last active December 16, 2015 02:59
Embed
What would you like to do?
<?php
/**
* @file
* Tweet block bean plugin.
*/
/**
* Require the twitteroauth library
*/
require_once(drupal_get_path('module','bean_tweets') . '/libraries/twitteroauth/twitteroauth.php');
class TweetBlockBean extends BeanPlugin {
// ... Code left out
/**
* Gets the tweets from the cache or twitter.
*/
private function getTweets(&$bean) {
// Check the cache
if ($cache = cache_get('bean_tweets-' . $bean->settings['handle'])) {
$tweets = $cache->data;
}
else { // Establish a connection
$connection = new TwitterOAuth(
variable_get('twitter_consumer_key'),
variable_get('twitter_consumer_secret'),
variable_get('twitter_access_token'),
variable_get('twitter_token_secret')
);
// Set the host to the 1.1 API
$connection->host = "https://api.twitter.com/1.1/";
// Get the tweets
$tweets = $connection->get(
'statuses/user_timeline',
array(
'screen_name' => $bean->settings['handle'],
'count' => $bean->settings['number_of_tweets']
)
);
// Cache the results if no errors
if (!isset($tweets->errors)) {
// Cache the results
cache_set(
'bean_tweets-' . $bean->settings['handle'],
$tweets,
'cache',
time() + 60 * $bean->settings['cache_duration']
);
}
else { // Nullify tweets since there was an error
$tweets = NULL;
}
}
return $tweets;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment