prefix = framework_get_prefix(); /* Set up the widget options. */ $widget_options = array( 'classname' => 'twitter', 'description' => esc_html__( 'Display your latest tweets.', 'wordsmith' ) ); /* Set up the widget control options. */ $control_options = array( 'width' => 200, 'height' => 350 ); /* Create the widget. */ $this->WP_Widget( "{$this->prefix}-twitter", __( 'Twitter', 'wordsmith' ), $widget_options, $control_options ); } /* Outputs the widget based on the arguments input through the widget controls. */ function widget( $args, $instance ) { extract( $args ); /* Arguments for the widget. */ $args['twitter_count'] = !empty( $instance['twitter_count'] ) ? intval( $instance['twitter_count'] ) : ''; $args['twitter_username'] = strip_tags( $instance['twitter_username'] ); $args['exclude_replies'] = $instance['exclude_replies']; /* Output the theme's $before_widget wrapper. */ echo $before_widget; /* If a title was input by the user, display it. */ if ( !empty( $instance['title'] ) ) echo $before_title . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $after_title; /* Time (in minutes) in between updates. */ $cache_time = 5; /* Name of the value in the database. */ $transient_name = 'list-tweets'; /* Name of the backup value in the database. */ $backup_transient_name = $transient_name . '-backup'; /* Do we already have save tweet data? If not, let's retrieve it. */ if ( false === ( $tweets = get_transient( $transient_name ) ) ) : /* Get the tweets from Twitter. */ $response = wp_remote_get( 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $args['twitter_username'] . '&count=' . $args['twitter_count'] . '&exclude_replies=' . $args['exclude_replies'] ); /* If we didn't find tweets, use the previously stored values. */ if ( !is_wp_error( $response ) && $response['response']['code'] == 200 ) : /* Get tweets into an array. */ $tweets_json = json_decode( $response['body'], true ); /* Now update the array to store just what we need. */ foreach ( $tweets_json as $tweet ) : /* Twitter info. */ $args['twitter_username'] = $tweet['user']['name']; $permalink = 'http://twitter.com/' . $args['twitter_username'] . '/status/' . $tweet['id_str']; /* Alternative image sizes method. @link: http://dev.twitter.com/doc/get/users/profile_image/:screen_name */ $image = $tweet['user']['profile_image_url']; /* Convert links to real links. */ $pattern = '/http:(\S)+/'; $replace = '${0}'; $text = preg_replace( $pattern, $replace, $tweet['text'] ); /* Get the time in Unix format. */ $time = $tweet['created_at']; $time = date_parse( $time ); $uTime = mktime( $time['hour'], $time['minute'], $time['second'], $time['month'], $time['day'], $time['year'] ); /* Now make the new array. */ $tweets[] = array( 'text' => $text, 'name' => $args['twitter_username'], 'permalink' => $permalink, 'image' => $image, 'time' => $uTime ); endforeach; /* Save our new transient, and update the backup. */ set_transient( $transient_name, $tweets, 60 * $cache_time ); update_option( $backup_transient_name, $tweets ); else : $tweets = get_option( $backup_transient_name ); endif; endif; /* Now display the tweets, if we can. */ if ( $tweets ) : ?>
esc_attr__( 'Twitter', 'wordsmith' ), 'twitter_username' => 'wordpressdotcom', 'twitter_count' => '4', 'exclude_replies' => '' ); /* Merge the user-selected arguments with the defaults. */ $instance = wp_parse_args( (array) $instance, $defaults ); ?>

/>