Skip to content

Instantly share code, notes, and snippets.

@joebuckle-dev
Created August 9, 2014 10:36
Show Gist options
  • Save joebuckle-dev/f6510f80e09cecee1ee6 to your computer and use it in GitHub Desktop.
Save joebuckle-dev/f6510f80e09cecee1ee6 to your computer and use it in GitHub Desktop.
A WordPress Twitter Widget Class
<?php
class twitterWidget extends WP_Widget {
public function config() {
return 'twitterWidget'; // Obj
}
function __construct() {
parent::__construct(
'twitter_widget', // Base ID
__('[WF] Twitter Widget', 'text_domain'), // Name
array( 'description' => __( 'Displays a Twitter Feed', 'text_domain' ), ) // Args
);
add_action( 'wp_ajax_WF_twitter_widget', array($this, 'ajax') );
add_action( 'wp_ajax_nopriv_WF_twitter_widget', array($this, 'ajax') );
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
$tweetlimit = $instance['twitter_tweetlimit'];
$username = $instance['twitter_username'];
$consumerkey = $instance['twitter_consumerkey'];
$consumersecret = $instance['twitter_consumersecret'];
$accesstoken = $instance['twitter_accesstoken'];
$accesstokensecret = $instance['twitter_accesstokensecret'];
$excludereplies = $instance['twitter_excludereplies'];
$includeretweets = $instance['twitter_includeretweets'];
$enableajax = $instance['twitter_enableajax'];
echo $args['before_widget'];
if(!isset($args['ajax_calling'])) {
echo '<div class="'.framework_prefix.'-widget-twitter" data-ajax="'.$enableajax.'" data-id="'.preg_replace("/[^0-9]/","",$args['widget_id']).'">';
}
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
if($enableajax=='true' && !isset($args['ajax_calling'])) {
echo $args['after_widget'];
echo '</div>';
return;
}
/*
* Twitter Feed
*/
$tweetOut = '';
define('TWEET_LIMIT', $tweetlimit);
define('CONSUMER_KEY', $consumerkey);
define('CONSUMER_SECRET', $consumersecret);
define('ACCESS_TOKEN', $accesstoken);
define('ACCESS_TOKEN_SECRET', $accesstokensecret);
# Create the connection
$twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
# Migrate over to SSL/TLS
$twitter->ssl_verifypeer = true;
# Load the Tweets
$tweets = array();
if (strpos($username,',') !== false) {
$username = explode(",", $username);
}
if(is_array($username)) {
foreach($username as $u) {
$a = $twitter->get('statuses/user_timeline',
array(
'screen_name' => $u,
'exclude_replies' => $excludereplies,
'include_rts' => $includeretweets,
'result_type' => 'recent'
));
$tweets[] = $a;
}
} else {
$tweets[] = $twitter->get('statuses/user_timeline',
array(
'screen_name' => $username,
'exclude_replies' => $excludereplies,
'include_rts' => $includeretweets,
'result_type' => 'recent'
));
}
# Example output
if(!empty($tweets)) {
$bind = '';
foreach($tweets as $tweet) {
foreach($tweet as $val) {
$bind[] = $val;
}
}
usort($bind, function($a, $b) {
if(strtotime($a->created_at)==strtotime($b->created_at)) return 0;
return strtotime($a->created_at) < strtotime($b->created_at)?1:-1;
});
$i=1;
foreach($bind as $tweet) {
# Access as an object
$tweetText = $tweet->text;
$tweetThumb= $tweet->user->profile_image_url;
# Make links active
$tweetText = preg_replace("/(http:\\/\\/|(www.))(([^s<]{4,68})[^s<]*)/", "<a href='http://$2$3' target='_blank'>$1$2$4</a>", $tweetText);
# Linkify user mentions
$tweetText = preg_replace("/@(w+)/", "<a href='http:\\/\\/www.twitter.com/$1' target='_blank'>@$1</a>", $tweetText);
# Linkify tags
$tweetOut .= '<div class="iteration">';
$tweetOut .= '<div class="thumb">';
$tweetOut .= '<img src="'.$tweetThumb.'" alt="" />';
$tweetOut .= '</div>';
$tweetOut .= '<div class="content">'.preg_replace("/#(w+)/", "<a href='http:\\/\\/search.twitter.com/search?q=$1' target='_blank'>#$1</a>", $tweetText).'</div>';
$tweetOut .= '</div>';
if($i==intval(TWEET_LIMIT)) {
break;
}
$i++;
}
}
echo $tweetOut;
if(!isset($args['ajax_calling'])) {
echo '</div>';
}
/*
* End Twitter Feed
*/
echo $args['after_widget'];
}
public function ajax() {
$widget = new twitterWidget();
$instance = $widget->get_settings();
//print_r($instance[$_REQUEST['id']]);
//$this->widget($args, $instance);
the_widget('twitterWidget', $instance[$_REQUEST['widget_id']], array('ajax_calling'=>true));
die();
}
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'Twitter Widget Title', 'text_domain' );
}
if ( isset( $instance[ 'twitter_tweetlimit' ] ) ) {
$tweetlimit = $instance[ 'twitter_tweetlimit' ];
}
else {
$tweetlimit = __( '1', 'text_domain' );
}
if ( isset( $instance[ 'twitter_username' ] ) ) {
$username = $instance[ 'twitter_username' ];
}
else {
$username = __( 'Enter your username', 'text_domain' );
}
if ( isset( $instance[ 'twitter_consumerkey' ] ) ) {
$consumerkey = $instance[ 'twitter_consumerkey' ];
}
else {
$consumerkey = __( 'Enter your Consumer Key', 'text_domain' );
}
if ( isset( $instance[ 'twitter_consumersecret' ] ) ) {
$consumersecret = $instance[ 'twitter_consumersecret' ];
}
else {
$consumersecret = __( 'Enter your Consumer Secret', 'text_domain' );
}
if ( isset( $instance[ 'twitter_accesstoken' ] ) ) {
$accesstoken = $instance[ 'twitter_accesstoken' ];
}
else {
$accesstoken = __( 'Enter your Access Token', 'text_domain' );
}
if ( isset( $instance[ 'twitter_accesstokensecret' ] ) ) {
$accesstokensecret = $instance[ 'twitter_accesstokensecret' ];
}
else {
$accesstokensecret = __( 'Enter your Access Token Secret', 'text_domain' );
}
if ( isset( $instance[ 'twitter_excludereplies' ] ) ) {
$excludereplies = $instance[ 'twitter_excludereplies' ];
}
else {
$excludereplies = __( 'false', 'text_domain' );
}
if ( isset( $instance[ 'twitter_includeretweets' ] ) ) {
$includeretweets = $instance[ 'twitter_includeretweets' ];
}
else {
$includeretweets = __( 'false', 'text_domain' );
}
if ( isset( $instance[ 'twitter_enableajax' ] ) ) {
$enableajax = $instance[ 'twitter_enableajax' ];
}
else {
$enableajax = __( 'false', 'text_domain' );
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<table width="100%">
<tr>
<td>
<label for="<?php echo $this->get_field_id( 'twitter_tweetlimit' ); ?>"><?php _e( 'Limit:' ); ?></label>
</td>
<td>
<input class="shortfat" id="<?php echo $this->get_field_id( 'twitter_tweetlimit' ); ?>" name="<?php echo $this->get_field_name( 'twitter_tweetlimit' ); ?>" type="text" value="<?php echo esc_attr( $tweetlimit ); ?>">
</td>
</tr>
<tr>
<td>
<label for="<?php echo $this->get_field_id( 'twitter_username' ); ?>"><?php _e( 'Username:' ); ?></label><br />
<i>For multiple twitter accounts, separate using a comma</i>
</td>
<td>
<input class="widefat" id="<?php echo $this->get_field_id( 'twitter_username' ); ?>" name="<?php echo $this->get_field_name( 'twitter_username' ); ?>" type="text" value="<?php echo esc_attr( $username ); ?>">
</td>
</tr>
<tr>
<td>
<label for="<?php echo $this->get_field_id( 'twitter_consumerkey' ); ?>"><?php _e( 'Consumer Key:' ); ?></label>
</td>
<td>
<input class="widefat" id="<?php echo $this->get_field_id( 'twitter_consumerkey' ); ?>" name="<?php echo $this->get_field_name( 'twitter_consumerkey' ); ?>" type="text" value="<?php echo esc_attr( $consumerkey ); ?>">
</td>
</tr>
<tr>
<td>
<label for="<?php echo $this->get_field_id( 'twitter_consumersecret' ); ?>"><?php _e( 'Consumer Secret:' ); ?></label>
</td>
<td>
<input class="widefat" id="<?php echo $this->get_field_id( 'twitter_consumersecret' ); ?>" name="<?php echo $this->get_field_name( 'twitter_consumersecret' ); ?>" type="text" value="<?php echo esc_attr( $consumersecret ); ?>">
</td>
</tr>
<tr>
<td>
<label for="<?php echo $this->get_field_id( 'twitter_accesstoken' ); ?>"><?php _e( 'Access Token:' ); ?></label>
</td>
<td>
<input class="widefat" id="<?php echo $this->get_field_id( 'twitter_accesstoken' ); ?>" name="<?php echo $this->get_field_name( 'twitter_accesstoken' ); ?>" type="text" value="<?php echo esc_attr( $accesstoken ); ?>">
</td>
</tr>
<tr>
<td>
<label for="<?php echo $this->get_field_id( 'twitter_accesstokensecret' ); ?>"><?php _e( 'Access Token Secret:' ); ?></label>
</td>
<td>
<input class="widefat" id="<?php echo $this->get_field_id( 'twitter_accesstokensecret' ); ?>" name="<?php echo $this->get_field_name( 'twitter_accesstokensecret' ); ?>" type="text" value="<?php echo esc_attr( $accesstokensecret ); ?>">
</td>
</tr>
<tr>
<td>
<label for="<?php echo $this->get_field_id( 'twitter_excludereplies' ); ?>"><?php _e( 'Exclude Replies?' ); ?></label>
</td>
<td>
<input type="checkbox" id="<?php echo $this->get_field_id( 'twitter_excludereplies' ); ?>" name="<?php echo $this->get_field_name( 'twitter_excludereplies' ); ?>" value="true" <?php
if($excludereplies=='true') {
echo 'checked';
}
?>/>
</td>
</tr>
<tr>
<td>
<label for="<?php echo $this->get_field_id( 'twitter_includeretweets' ); ?>"><?php _e( 'Include Retweets?' ); ?></label>
</td>
<td>
<input type="checkbox" id="<?php echo $this->get_field_id( 'twitter_includeretweets' ); ?>" name="<?php echo $this->get_field_name( 'twitter_includeretweets' ); ?>" value="true" <?php
if($includeretweets=='true') {
echo 'checked';
}
?>/>
</td>
</tr>
<tr>
<td>
<label for="<?php echo $this->get_field_id( 'twitter_enableajax' ); ?>"><?php _e( 'Enable Ajax?' ); ?></label>
</td>
<td>
<input type="checkbox" id="<?php echo $this->get_field_id( 'twitter_enableajax' ); ?>" name="<?php echo $this->get_field_name( 'twitter_enableajax' ); ?>" value="true" <?php
if($enableajax=='true') {
echo 'checked';
}
?>/>
</td>
</tr>
</table>
<br />
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$twitter_tweetlimit = preg_replace('/\D/', '', $new_instance['twitter_tweetlimit']);
$instance['twitter_tweetlimit'] = ( ! empty( $twitter_tweetlimit ) ) ? strip_tags( $twitter_tweetlimit ) : '1';
$instance['twitter_username'] = ( ! empty( $new_instance['twitter_username'] ) ) ? strip_tags( $new_instance['twitter_username'] ) : 'Enter your username';
$instance['twitter_consumerkey'] = ( ! empty( $new_instance['twitter_consumerkey'] ) ) ? strip_tags( $new_instance['twitter_consumerkey'] ) : 'Enter your Consumer Key';
$instance['twitter_consumersecret'] = ( ! empty( $new_instance['twitter_consumersecret'] ) ) ? strip_tags( $new_instance['twitter_consumersecret'] ) : 'Enter your Consumer Secret';
$instance['twitter_accesstoken'] = ( ! empty( $new_instance['twitter_accesstoken'] ) ) ? strip_tags( $new_instance['twitter_accesstoken'] ) : 'Enter your Access Token';
$instance['twitter_accesstokensecret'] = ( ! empty( $new_instance['twitter_accesstokensecret'] ) ) ? strip_tags( $new_instance['twitter_accesstokensecret'] ) : 'Enter your Access Token Secret';
$instance['twitter_excludereplies'] = ( ! empty( $new_instance['twitter_excludereplies'] ) ) ? strip_tags( $new_instance['twitter_excludereplies'] ) : 'false';
$instance['twitter_includeretweets'] = ( ! empty( $new_instance['twitter_includeretweets'] ) ) ? strip_tags( $new_instance['twitter_includeretweets'] ) : 'false';
$instance['twitter_enableajax'] = ( ! empty( $new_instance['twitter_enableajax'] ) ) ? strip_tags( $new_instance['twitter_enableajax'] ) : 'false';
return $instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment