'widget_twitter', 'description' => __( "Display your tweets from Twitter") ); $this->WP_Widget('twitter', __('Twitter'), $widget_ops); } function widget( $args, $instance ) { extract( $args ); $account = urlencode( $instance['account'] ); if ( empty($account) ) return; $title = apply_filters('widget_title', $instance['title']); if ( empty($title) ) $title = __( 'Twitter Updates' ); $show = absint( $instance['show'] ); // # of Updates to show $hidereplies = $instance['hidereplies']; $before_timesince = esc_html($instance['beforetimesince']); if ( empty($before_timesince) ) $before_timesince = ' '; $before_tweet = esc_html($instance['beforetweet']); echo "{$before_widget}{$before_title}{$title}{$after_title}"; if ( !$tweets = wp_cache_get( 'widget-twitter-' . $this->number , 'widget' ) ) { $twitter_json_url = clean_url( "http://twitter.com/statuses/user_timeline/$account.json", null, 'raw' ); $response = wp_remote_get( $twitter_json_url, array( 'User-Agent' => 'Wickett Twitter Widget' ) ); $response_code = wp_remote_retrieve_response_code( $response ); if ( 200 == $response_code ) { $tweets = wp_remote_retrieve_body( $response ); $tweets = json_decode( $tweets); $expire = 900; if ( !is_array( $tweets ) || isset( $tweets['error'] ) ) { $tweets = 'error'; $expire = 300; } } else { $tweets = 'error'; $expire = 300; wp_cache_add( 'widget-twitter-response-code-' . $this->number, $response_code, 'widget', $expire); } wp_cache_add( 'widget-twitter-' . $this->number, $tweets, 'widget', $expire ); } if ( 'error' != $tweets ) : echo "\n"; else : if ( 401 == wp_cache_get( 'widget-twitter-response-code-' . $this->number , 'widget' ) ) echo "

" . __("Error: Please make sure the Twitter account is public.") . "

"; else echo "

" . __("Error: Twitter did not respond. Please wait a few minutes and refresh this page.") . "

"; endif; echo $after_widget; } function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['account'] = strip_tags(stripslashes($new_instance['account'])); $instance['account'] = str_replace('http://twitter.com/', '', $instance['account']); $instance['account'] = str_replace('/', '', $instance['account']); $instance['account'] = str_replace('@', '', $instance['account']); $instance['title'] = strip_tags(stripslashes($new_instance['title'])); $instance['show'] = absint($new_instance['show']); $instance['hidereplies'] = isset($new_instance['hidereplies']); $instance['beforetimesince'] = $new_instance['beforetimesince']; wp_cache_delete( 'widget-twitter-' . $this->number , 'widget' ); wp_cache_delete( 'widget-twitter-response-code-' . $this->number, 'widget' ); return $instance; } function form( $instance ) { //Defaults $instance = wp_parse_args( (array) $instance, array('account' => '', 'title' => '', 'show' => '5', 'hidereplies' => false) ); $account = esc_attr($instance['account']); $title = esc_attr($instance['title']); $show = absint($instance['show']); if ( $show < 1 || 20 < $show ) $show = '5'; $hidereplies = (bool) $instance['hidereplies']; $before_timesince = esc_attr($instance['beforetimesince']); echo '

'; echo '

'; } function _widget_twitter_username( $matches ) { // $matches has already been through wp_specialchars return "$matches[1]@$matches[2]"; } function _widget_twitter_hashtag( $matches ) { // $matches has already been through wp_specialchars return "$matches[1]#$matches[2]"; } } add_action( 'widgets_init', 'wickett_twitter_widget_init' ); function wickett_twitter_widget_init() { register_widget('Wickett_Twitter_Widget'); }