Skip to content

Instantly share code, notes, and snippets.

@wturrell
Last active December 14, 2015 13:08
Show Gist options
  • Save wturrell/5090844 to your computer and use it in GitHub Desktop.
Save wturrell/5090844 to your computer and use it in GitHub Desktop.
Modified PyroCMS Twitter widget - uses Twitter OAuth PHP class (/system/cms/libraries/Twitter.php) to connect using v1.1 of Twitter API and grab/cache Tweets. To use this, you need to: - create an 'application at http://dev.twitter.com and then use the same page to generate your 'access tokens' AND ensure /system/cms/libraries/Twitter.php has th…
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Show Twitter streams in your site
*
* @author Phil Sturgeon
* @author PyroCMS Dev Team
* @package PyroCMS\Core\Widgets
*/
class Widget_Twitter_feed extends Widgets
{
/**
* The translations for the widget title
*
* @var array
*/
public $title = array(
'en' => 'Twitter Feed',
'el' => 'Ροή Twitter',
'nl' => 'Twitterfeed',
'br' => 'Feed do Twitter',
'pt' => 'Feed do Twitter',
'ru' => 'Лента Twitter\'а',
'id' => 'Twitter Feed',
'fi' => 'Twitter Syöte'
);
/**
* The translations for the widget description
*
* @var array
*/
public $description = array(
'en' => 'Display Twitter feeds on your website',
'el' => 'Προβολή των τελευταίων tweets από το Twitter',
'nl' => 'Toon Twitterfeeds op uw website',
'br' => 'Mostra os últimos tweets de um usuário do Twitter no seu site.',
'pt' => 'Mostra os últimos tweets de um utilizador do Twitter no seu site.',
'ru' => 'Выводит ленту новостей Twitter на страницах вашего сайта',
'id' => 'Menampilkan koleksi Tweet di situs Anda',
'fi' => 'Näytä Twitter syöte sivustollasi',
);
/**
* The author of the widget
*
* @var string
*/
public $author = 'Phil Sturgeon';
/**
* The author's website.
*
* @var string
*/
public $website = 'http://philsturgeon.co.uk/';
/**
* The version of the widget
*
* @var string
*/
public $version = '1.2';
/**
* The fields for customizing the options of the widget.
*
* @var array
*/
public $fields = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'number',
'label' => 'Number of tweets',
'rules' => 'numeric'
)
);
/**
* The URL used to get statuses from the Twitter API
*
* @var string
*/
private $twitter_keys = array('consumer_key' => 'foo',
'consumer_secret' => 'bar',
'oauth_token' => 'foo',
'oauth_token_secret' => 'bar');
/**
* The main function of the widget.
*
* @param array $options The options for the twitter username and the number of tweets to display
* @return array
*/
public function run($options)
{
$cache_key = sprintf('twitter-%s-%s',$options['username'], $options['number']);
if ( ! $tweets = $this->pyrocache->get($cache_key) )
{
$twitter_oauth = new Twitter($this->twitter_keys['consumer_key'],
$this->twitter_keys['consumer_secret'],
$this->twitter_keys['oauth_token'],
$this->twitter_keys['oauth_token_secret']);
$params = array('screen_name' => $options['username'],
'count' => $options['number'],
'trim_user' => 1,
'include_rts' => 1,
'exclude_replies' => 1);
$tweets = $twitter_oauth->get('statuses/user_timeline', $params);
$this->pyrocache->write($tweets, $cache_key, $this->settings->twitter_cache);
}
$patterns = array(
// Detect URL's
'((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)' => '<a href="$0" target="_blank">$0</a>',
// Detect Email
'|([a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6})|i' => '<a href="mailto:$1">$1</a>',
// Detect Twitter @usernames
'| @([a-z0-9-_]+)|i' => '<a href="https://twitter.com/$1" target="_blank">$0</a>',
// Detect Twitter #tags
'|#([a-z0-9-_]+)|i' => '<a href="https://twitter.com/search?q=%23$1" target="_blank">$0</a>'
);
if ($tweets)
{
foreach ($tweets as &$tweet)
{
$tweet->text = str_replace($options['username'].': ', '', $tweet->text);
$tweet->text = preg_replace(array_keys($patterns), $patterns, $tweet->text);
}
}
// Store the feed items
return array(
'username' => $options['username'],
'tweets' => $tweets ? $tweets : array(),
);
}
}
@sanstream
Copy link

Seems like it, but still very helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment