Last active
December 11, 2015 09:08
-
-
Save esgy/4577975 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
/** | |
* Plugin Name: p47 Twitter Widget | |
* Plugin URI: http://point47.com | |
* Description: Displays any message designated | |
* Version: 1.0 | |
* Author: Sorin Gitlan | |
* Author URI: http://point47.com | |
*/ | |
class p47_Twitter_Widget extends WP_Widget { | |
public function __construct(){ | |
$params = array( | |
'description' => 'Displays and cache tweets', | |
'name' => 'p47 Twitter Widget' | |
); | |
parent::__construct('p47_Twitter_Widget', '', $params); | |
} | |
/** | |
* Responsible for the form in the Widget management area | |
* @param Array $instance The form array Post data | |
* @return String | |
*/ | |
public function form($instance){ | |
extract($instance); | |
?> | |
<p> | |
<label for="<?php echo $this->get_field_id('title'); ?>">Title: </label> | |
<input class="widefat" | |
id="<?php echo $this->get_field_id('title'); ?>" | |
name="<?php echo $this->get_field_name('title'); ?>" | |
value="<?php if(isset($title)) echo esc_attr($title); ?>" | |
> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id('username'); ?>">Twitter Username: </label> | |
<input class="widefat" | |
id="<?php echo $this->get_field_id('username'); ?>" | |
name="<?php echo $this->get_field_name('username'); ?>" | |
value="<?php if(isset($username)) echo esc_attr($username); ?>" | |
> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id('tweet_count'); ?>">Number of tweets to receive: </label> | |
<input type="number" min="1" max="10" | |
id="<?php echo $this->get_field_id('tweet_count'); ?>" | |
name="<?php echo $this->get_field_name('tweet_count'); ?>" | |
value="<?php echo !empty($tweet_count) ? $tweet_count : 5; ?>" | |
> | |
</p> | |
<?php | |
} | |
/** | |
* Responsible to display the results of our form | |
* @param Array $args the values that can be used to tailor how the widget displays | |
* @param Array $instance the values that are available from the widget area | |
* @return String | |
*/ | |
public function widget($args, $instance){ | |
extract($args); | |
extract($instance); | |
$title = !empty($title) ? apply_filters( 'widget_title', $title ) : 'Recent tweets'; | |
$data = $this->twitter($tweet_count, $username); | |
if( false !== $data && isset($data->tweets) ){ | |
echo $before_widget; | |
echo $before_title; | |
echo $title; | |
echo $after_title; | |
echo '<ul><li>'. implode('</li><li>', $data->tweets) . '</li></ul>'; | |
echo $after_widget; | |
} | |
} | |
/** | |
* | |
* @param [type] $tweet_count [description] | |
* @param [type] $username [description] | |
* @return [type] [description] | |
*/ | |
private function twitter($tweet_count, $username){ | |
if(empty($username)) return false; | |
// get cached data | |
$tweets = get_transient( 'recent_tweets_widget' ); | |
// fetch tweets if no cached data or username or tweet_count changed | |
if( !$tweets || | |
$tweets->username !== $username || | |
$tweets->tweet_count !== $tweet_count | |
){ | |
return $this->fetch_tweets($tweet_count, $username); | |
} | |
return $tweets; | |
} | |
/** | |
* | |
* @param [type] $tweet_count [description] | |
* @param [type] $username [description] | |
* @return [type] [description] | |
*/ | |
private function fetch_tweets($tweet_count, $username){ | |
$tweets = $this->curl('https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=1&screen_name=@' | |
. $username . '&count=' . $tweet_count . '&published_when=1'); | |
$data = new stdClass(); | |
$data->username = $username; | |
$data->tweet_count = $tweet_count; | |
$data->tweets = array(); | |
foreach (json_decode($tweets) as $tweet) { | |
$data->tweets[] = $this->filter_tweet( $tweet->text ); | |
} | |
// standardized way of storing cached data in the database temporarily by giving it a custom | |
// name and a timeframe after which it will expire and be deleted. | |
set_transient( 'recent_tweets_widget', $data, 60*5); | |
return $data; | |
} | |
/** | |
* | |
* @param [type] $tweet [description] | |
* @return [type] [description] | |
*/ | |
private function filter_tweet( $tweet ){ | |
$tweet = preg_replace('/(http[^\s]+)/im', '<a href="$1">$1</a>', $tweet); | |
$tweet = preg_replace('/@([^\s]+)/i', '<a href="http://twitter.com/$1">@$1</a>', $tweet); | |
return $tweet; | |
} | |
/** | |
* | |
* @param [type] $url [description] | |
* @return [type] [description] | |
*/ | |
private function curl($url){ | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); | |
return curl_exec($curl); | |
curl_close($curl); | |
} | |
} | |
add_action('widgets_init', function(){ | |
register_widget( 'p47_Twitter_Widget' ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment