id = 'twittergrid';
$this->title = 'TwitterGrid';
$this->version = '0.3';
$this->plugin_url = 'https://wordpress.org/plugins/twittergrid/';
$this->name = 'TwitterGrid v'. $this->version;
$this->url = get_bloginfo('wpurl'). '/wp-content/plugins/' . $this->id;
$this->locale = get_locale();
$this->path = dirname(__FILE__);
$this->cache_file = $this->path . '/cache/friends.html';
if(empty($this->locale)) {
$this->locale = 'en_US';
}
load_textdomain($this->id, sprintf('%s/%s.mo', $this->path, $this->locale));
$this->loadOptions();
if(!is_admin()) {
add_filter('wp_head', array(&$this, 'blogHeader'));
}
else {
add_action('admin_menu', array( &$this, 'optionMenu'));
}
add_action('widgets_init', array( &$this, 'initWidget'));
}
function optionMenu() {
add_options_page($this->title, $this->title, 8, __FILE__, array(&$this, 'optionMenuPage'));
}
function optionMenuPage() {
?>
=$this->title?>
id])) {
/**
* nasty checkbox handling
*/
foreach(array('link', 'nofollow', 'show_twitter_link', 'target_blank') as $field ) {
if(!isset($_POST[$this->id][$field])) {
$_POST[$this->id][$field] = '0';
}
}
@unlink($this->cache_file);
$this->updateOptions( $_POST[ $this->id ] );
echo '
' . __( 'Settings saved!', $this->id) . '
';
}
?>
options as $k => $v) {
if(array_key_exists( $k, $options)) {
$this->options[ $k ] = trim($options[ $k ]);
}
}
update_option($this->id, $this->options);
}
function loadOptions() {
$this->options = get_option( $this->id );
if( !$this->options ) {
$this->options = array(
'installed' => time(),
'username' => '',
'link' => 1,
'nofollow' => 1,
'limit' => 18,
'target_blank' => 1,
'width' => 160,
'height' => 400,
'last_check' => 0,
'show_twitter_link' => 1,
'title' => 'TwitterGrid'
);
add_option($this->id, $this->options, $this->name, 'yes');
if(is_admin()) {
add_filter('admin_footer', array(&$this, 'addAdminFooter'));
}
}
}
function initWidget() {
if(function_exists('register_sidebar_widget')) {
register_sidebar_widget($this->title . ' Widget', array($this, 'showWidget'), null, 'widget_twittergrid');
}
}
function showWidget( $args ) {
extract($args);
printf( '%s%s%s%s%s%s', $before_widget, $before_title, $this->options['title'], $after_title, $this->getCode(), $after_widget );
}
function blogHeader() {
printf('' . "\n", $this->id, $this->id, $this->version);
printf(''. "\n", $this->url, $this->id);
}
function getToken($data, $pattern) {
if(preg_match('|<' . $pattern . '>(.*?)' . $pattern . '>|s', $data, $matches)) {
return $matches[1];
}
return '';
}
function getFriends($user) {
if(empty($user)) {
return false;
}
if(!class_exists('Snoopy')) {
if(!@include_once(ABSPATH . WPINC . '/class-snoopy.php')) {
return false;
}
}
$Snoopy = new Snoopy();
/**
* not the best way, but we can't assume that every webhost simplexml installed
*/
if($Snoopy->fetch('http://twitter.com/statuses/friends/' . $user . '.xml')) {
if(!empty($Snoopy->results)) {
if(preg_match_all('/(.*?)<\/user>/s', $Snoopy->results, $matches)) {
$result = array();
foreach($matches[0] as $matche) {
$result[] = array(
$this->getToken($matche, 'screen_name'),
$this->getToken($matche, 'profile_image_url')
);
}
return $result;
}
}
}
return false;
}
function getCode() {
$create = false;
if(!file_exists($this->cache_file)) {
$create = true;
}
elseif(time() - filemtime($this->cache_file) > (3600 * 3)) {
$create = true;
}
if(!$create) {
return file_get_contents($this->cache_file);
}
$friends = $this->getFriends($this->options['username']);
$count = count($friends);
if($friends && $count > 0) {
if($count > intval($this->options['limit'])) {
$friends = array_slice($friends, 0, intval($this->options['limit']));
}
$data = '';
foreach($friends as $friend) {
$item = sprintf(
'
',
$friend[1],
$friend[0],
$friend[0]
);
if(intval($this->options['link']) == 1) {
$item = sprintf(
'%s',
$friend[0],
$this->options['target_blank'] == 1 ? ' target="_blank"' : '',
$this->options['nofollow'] == 1 ? ' rel="nofollow"' : '',
$item
);
}
$data .= $item;
}
$data = '';
if(is_writeable($this->path. '/cache')) {
file_put_contents($this->cache_file, $data);
}
return $data;
}
return '';
}
}
function twittergrid_display() {
global $TwitterGrid;
if($TwitterGrid) {
echo $TwitterGrid->getcode();
}
}
add_action( 'plugins_loaded', create_function( '$TwitterGrid_5kqll', 'global $TwitterGrid; $TwitterGrid = new TwitterGrid();' ) );
?>