Created
July 24, 2012 17:57
-
-
Save georgestephanis/3171510 to your computer and use it in GitHub Desktop.
Twitter Widget
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 | |
| //Custom Twitter Widget by George | |
| class speck_twitter_widget extends WP_Widget { | |
| public function __construct() { | |
| parent::__construct( | |
| 'speck_twitter_widget', // Base ID | |
| 'Speck Twitter Widget', | |
| array( 'description' => __( 'A widget to display the most recent tweet from Speck!', 'speck_twitter_widget' ), ) | |
| ); | |
| } | |
| public function form( $instance ) { | |
| $title = isset( $instance[ 'title' ] ) ? $instance[ 'title' ] : __( 'Latest Tweet', 'speck_twitter_widget' ); | |
| $handle = isset( $instance[ 'handle' ] ) ? $instance[ 'handle' ] : 'speckproducts'; | |
| ?> | |
| <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> | |
| <p> | |
| <label for="<?php echo $this->get_field_id( 'handle' ); ?>"><?php _e( 'Handle:' ); ?></label> | |
| <input class="widefat" id="<?php echo $this->get_field_id( 'handle' ); ?>" name="<?php echo $this->get_field_name( 'handle' ); ?>" type="text" value="<?php echo esc_attr( $handle ); ?>" /> | |
| </p> | |
| <?php | |
| } | |
| public function update( $new_instance, $old_instance ) { | |
| $instance = array(); | |
| $instance['title'] = strip_tags( $new_instance['title'] ); | |
| $instance['handle'] = strip_tags( $new_instance['handle'] ); | |
| return $instance; | |
| } | |
| public function widget( $args, $instance ) { | |
| extract( $args ); | |
| $title = apply_filters( 'widget_title', $instance['title'] ); | |
| $handle = $instance['handle']; | |
| $tweet = self::get_latest_tweet( $handle ); | |
| if( is_wp_error( $tweet ) ) return; | |
| echo $before_widget; | |
| if ( ! empty( $title ) ) echo $before_title . $title . $after_title; | |
| echo '<div class="tweet">'; | |
| echo wpautop( make_clickable( self::twitterify( $tweet->text ) ) ); | |
| echo '</div>'; | |
| echo '<p class="tweet-links">' | |
| .'<a href="https://twitter.com/intent/tweet?in_reply_to='.$tweet->id.'">Reply</a> • ' | |
| .'<a href="https://twitter.com/intent/retweet?tweet_id='.$tweet->id.'">Retweet</a> • ' | |
| .'<a href="https://twitter.com/intent/favorite?tweet_id='.$tweet->id.'">Favorite</a> • ' | |
| .'<a href="https://twitter.com/intent/user?user_id='.$tweet->user->id.'">Follow</a>' | |
| .'</p>'; | |
| echo $after_widget; | |
| } | |
| public function get_latest_tweet( $handle, $minutes_to_save = 5 ) { | |
| $minutes_to_save = (int) $minutes_to_save; | |
| $transient_name = __CLASS__.'_'.$handle.'_latest_tweets'; | |
| if ( false === ( $value = get_transient( $transient_name ) ) ) { | |
| if( !class_exists( 'WP_Http' ) ) require_once( ABSPATH . WPINC . '/class-http.php' ); | |
| $wp_http = new WP_Http; | |
| $response = $wp_http->request( 'http://api.twitter.com/1/statuses/user_timeline/'.$handle.'.json' ); | |
| if( is_wp_error( $response ) ) return $response; | |
| $value = $response['body']; | |
| set_transient( $transient_name, $value, 60*$minutes_to_save ); | |
| } | |
| $json = json_decode( $value ); | |
| return array_shift( $json ); | |
| } | |
| function twitterify($ret) { | |
| $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret); | |
| $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"//\\2\" target=\"_blank\">\\2</a>", $ret); | |
| $ret = preg_replace("/@(\w+)/", "<a href=\"//www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret); | |
| $ret = preg_replace("/#(\w+)/", "<a href=\"//search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret); | |
| return $ret; | |
| } | |
| } | |
| register_widget( 'speck_twitter_widget' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment