'widget_twitter', 'description' => __( 'Display your tweets from Twitter', 'jetpack' ) ); parent::WP_Widget( 'twitter', __( 'Twitter (Jetpack)', 'jetpack' ), $widget_ops ); } function widget( $args, $instance ) { extract( $args ); $account = trim( urlencode( $instance['account'] ) ); if ( empty($account) ) return; $title = apply_filters( 'widget_title', $instance['title'] ); if ( empty( $title ) ) $title = __( 'Twitter Updates', 'jetpack' ); $show = absint( $instance['show'] ); // # of Updates to show if ( $show > 200 ) // Twitter paginates at 200 max tweets. update() should not have accepted greater than 20 $show = 200; $hidereplies = (bool) $instance['hidereplies']; $include_retweets = (bool) $instance['includeretweets']; echo "{$before_widget}{$before_title}" . esc_html($title) . "{$after_title}"; if ( false === ( $tweets = get_transient( 'widget-twitter-' . $this->number ) ) ) { $params = array( 'screen_name'=>$account, // Twitter account name 'trim_user'=>true, // only basic user data (slims the result) 'include_entities'=>false // as of Sept 2010 entities were not included in all applicable Tweets. regex still better ); /** * The exclude_replies parameter filters out replies on the server. If combined with count it only filters that number of tweets (not all tweets up to the requested count) * If we are not filtering out replies then we should specify our requested tweet count */ if ( $hidereplies ) $params['exclude_replies'] = true; else $params['count'] = $show; if ( $include_retweets ) $params['include_rts'] = true; $twitter_json_url = esc_url_raw( 'http://api.twitter.com/1/statuses/user_timeline.json?' . http_build_query( $params ), array( 'http', 'https' ) ); unset( $params ); $response = wp_remote_get( $twitter_json_url, array( 'User-Agent' => 'WordPress.com Twitter Widget' ) ); $response_code = wp_remote_retrieve_response_code( $response ); if ( 200 == $response_code ) { $tweets = wp_remote_retrieve_body( $response ); $tweets = json_decode( $tweets, true ); $expire = 900; if ( !is_array( $tweets ) || isset( $tweets['error'] ) ) { $tweets = 'error'; $expire = 300; } } else { $tweets = 'error'; $expire = 300; set_transient( 'widget-twitter-response-code-' . $this->number, $response_code, $expire ); } set_transient( 'widget-twitter-' . $this->number, $tweets, $expire ); } if ( 'error' != $tweets ) : $before_timesince = ' '; if ( isset( $instance['beforetimesince'] ) && !empty( $instance['beforetimesince'] ) ) $before_timesince = esc_html( $instance['beforetimesince'] ); $before_tweet = ''; if ( isset( $instance['beforetweet'] ) && !empty( $instance['beforetweet'] ) ) $before_tweet = stripslashes( wp_filter_post_kses( $instance['beforetweet'] ) ); echo '
' . wp_kses( sprintf( __( 'Error: Please make sure the Twitter account is public.', 'jetpack' ), 'http://support.twitter.com/forums/10711/entries/14016' ), array( 'a' => array( 'href' => true ) ) ) . '
'; else echo '' . esc_html__( 'Error: Twitter did not respond. Please wait a few minutes and refresh this page.', 'jetpack' ) . '
'; endif; echo $after_widget; } function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['account'] = trim( 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['account'] = str_replace( '#!', '', $instance['account'] ); // account for the Ajax URI $instance['title'] = strip_tags( stripslashes( $new_instance['title'] ) ); $instance['show'] = absint( $new_instance['show'] ); $instance['hidereplies'] = isset( $new_instance['hidereplies'] ); $instance['includeretweets'] = isset( $new_instance['includeretweets'] ); $instance['beforetimesince'] = $new_instance['beforetimesince']; delete_transient( 'widget-twitter-' . $this->number ); delete_transient( 'widget-twitter-response-code-' . $this->number ); return $instance; } function form( $instance ) { //Defaults $instance = wp_parse_args( (array) $instance, array( 'account' => '', 'title' => '', 'show' => 5, 'hidereplies' => false, 'includeretweets' => false, 'beforetimesince' => '' ) ); $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']; $include_retweets = (bool) $instance['includeretweets']; $before_timesince = esc_attr( $instance['beforetimesince'] ); echo ' '; echo ''; echo ''; } /** * Link a Twitter user mentioned in the tweet text to the user's page on Twitter. * * @param array $matches regex match * @return string Tweet text with inserted @user link */ function _wpcom_widget_twitter_username( $matches ) { // $matches has already been through wp_specialchars return "$matches[1]@$matches[3]"; } /** * Link a Twitter hashtag with a search results page on Twitter.com * * @param array $matches regex match * @return string Tweet text with inserted #hashtag link */ function _wpcom_widget_twitter_hashtag( $matches ) { // $matches has already been through wp_specialchars return "$matches[1]#$matches[3]"; } } add_action( 'widgets_init', 'wickett_twitter_widget_init' ); function wickett_twitter_widget_init() { register_widget( 'Wickett_Twitter_Widget' ); }