summaryrefslogtreecommitdiff
blob: ff49abf7d1e6986f3b6da7ee73918cd8f8f74210 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php

/**
 * Twitter widget class
 * Display the latest N tweets from a Twitter screenname as a widget
 * Customize screenname, maximum number of tweets displayed, show or hide @replies, and text displayed between tweet text and a timestamp
 * 
 */

/**
 * Register the widget for use in Appearance -> Widgets
 */
add_action( 'widgets_init', 'jetpack_twitter_widget_init' );

function jetpack_twitter_widget_init() {
	register_widget( 'Jetpack_Widget_Twitter' );
}

class Jetpack_Widget_Twitter extends WP_Widget {

	function __construct() {
		parent::__construct(
			'twitter',
			apply_filters( 'jetpack_widget_name', __( 'Twitter', 'jetpack' ) ),
			array(
				'classname' => 'widget_twitter',
				'description' => __( 'Display your Tweets from Twitter', 'jetpack' )
			)
		);

		if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) ) {
			add_action( 'wp_head', array( $this, 'style' ) );
		}
	}

	function style() {
?>
<style type="text/css">
.widget_twitter li {
	word-wrap: break-word;
}
</style>
<?php
	}

	function widget( $args, $instance ) {
		$account = trim( urlencode( $instance['account'] ) );

		if ( empty( $account ) ) {
			if ( current_user_can('edit_theme_options') ) {
				echo $args['before_widget'];
				echo '<p>' . sprintf( __( 'Please configure your Twitter username for the <a href="%s">Twitter Widget</a>.', 'jetpack' ), admin_url( 'widgets.php' ) ) . '</p>';
				echo $args['after_widget'];
			}

			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'];
		$hidepublicized   = (bool) $instance['hidepublicized'];
		$include_retweets = (bool) $instance['includeretweets'];
		$follow_button    = (bool) $instance['followbutton'];

		echo "{$args['before_widget']}{$args['before_title']}<a href='" . esc_url( "http://twitter.com/{$account}" ) . "'>" . esc_html( $title ) . "</a>{$args['after_title']}";

		$tweets = $this->fetch_twitter_user_stream( $account, $hidereplies, $show, $include_retweets );

		if ( isset( $tweets['error'] ) && ( isset( $tweets['data'] ) && ! empty( $tweets['data'] ) ) )
			$tweets['error'] = '';

		if ( empty( $tweets['error'] ) ) {
			$before_tweet     = isset( $instance['beforetweet'] ) ? stripslashes( wp_filter_post_kses( $instance['beforetweet'] ) ) : '';
			$before_timesince = ( isset( $instance['beforetimesince'] ) && ! empty( $instance['beforetimesince'] ) ) ? esc_html( $instance['beforetimesince'] ) : ' ';			

			$this->display_tweets( $show, $tweets['data'], $hidepublicized, $before_tweet, $before_timesince, $account );

			if ( $follow_button )
				$this->display_follow_button( $account );

			add_action( 'wp_footer', array( $this, 'twitter_widget_script' ) );
		} else {
			echo $tweets['error'];
		}

		echo $args['after_widget'];
		do_action( 'jetpack_bump_stats_extras', 'widget', 'twitter' );
	}

	function display_tweets( $show, $tweets, $hidepublicized, $before_tweet, $before_timesince, $account ) {
		$tweets_out = 0;
		?><ul class='tweets'><?php

		foreach( (array) $tweets as $tweet ) {
			if ( $tweets_out >= $show )
				break;

			if ( empty( $tweet['text'] ) )
				continue;

			if( $hidepublicized && false !== strstr( $tweet['source'], 'http://publicize.wp.com/' ) )
				continue;

			$tweet['text'] = esc_html( $tweet['text'] ); // escape here so that Twitter handles in Tweets don't get mangled
			$tweet         = $this->expand_tco_links( $tweet );
			$tweet['text'] = make_clickable( $tweet['text'] ); 

			/*
			 * Create links from plain text based on Twitter patterns
			 * @link http://github.com/mzsanford/twitter-text-rb/blob/master/lib/regex.rb Official Twitter regex
			 */
			$tweet['text'] = preg_replace_callback( '/(^|[^0-9A-Z&\/]+)(#|\xef\xbc\x83)([0-9A-Z_]*[A-Z_]+[a-z0-9_\xc0-\xd6\xd8-\xf6\xf8\xff]*)/iu',  array( $this, '_jetpack_widget_twitter_hashtag' ), $tweet['text'] );
			$tweet['text'] = preg_replace_callback( '/([^a-zA-Z0-9_]|^)([@\xef\xbc\xa0]+)([a-zA-Z0-9_]{1,20})(\/[a-zA-Z][a-zA-Z0-9\x80-\xff-]{0,79})?/u', array( $this, '_jetpack_widget_twitter_username' ), $tweet['text'] );

			if ( isset( $tweet['id_str'] ) )
				$tweet_id = urlencode( $tweet['id_str'] );
			else
				$tweet_id = urlencode( $tweet['id'] );

			?>

			<li>
				<?php echo esc_attr( $before_tweet ) . $tweet['text'] . esc_attr( $before_timesince ) ?>
				<a href="<?php echo esc_url( "http://twitter.com/{$account}/statuses/{$tweet_id}" ); ?>" class="timesince"><?php echo esc_html( str_replace( ' ', '&nbsp;', $this->time_since( strtotime( $tweet['created_at'] ) ) ) ); ?>&nbsp;ago</a>
			</li>

			<?php

			unset( $tweet_it );
			$tweets_out++;
		}

		?></ul><?php
	}

	function display_follow_button( $account ) {
		global $themecolors;

		$follow_colors        = isset( $themecolors['link'] ) ? " data-link-color='#{$themecolors['link']}'" : '';
		$follow_colors       .= isset( $themecolors['text'] ) ? " data-text-color='#{$themecolors['text']}'" : '';
		$follow_button_attrs  = " class='twitter-follow-button' data-show-count='false'{$follow_colors}";
			 
		?><a href="http://twitter.com/<?php echo esc_attr( $account ); ?>" <?php echo $follow_button_attrs; ?>>Follow @<?php echo esc_attr( $account ); ?></a><?php
	}

	function expand_tco_links( $tweet ) {
		if ( ! empty( $tweet['entities']['urls'] ) && is_array( $tweet['entities']['urls'] ) ) {
			foreach ( $tweet['entities']['urls'] as $entity_url ) {
				if ( ! empty( $entity_url['expanded_url'] ) ) {
					$tweet['text'] = str_replace( 
										$entity_url['url'], 
										'<a href="' . esc_url( $entity_url['expanded_url'] ) . '"> ' . esc_html( $entity_url['display_url'] ) . '</a>', 
										$tweet['text'] 
									);
				}
			}
		}

		return $tweet;
	}

	function fetch_twitter_user_stream( $account, $hidereplies, $show, $include_retweets ) {
		$tweets    = get_transient( 'widget-twitter-' . $this->number );
		$the_error = get_transient( 'widget-twitter-error-' . $this->number );

		if ( ! $tweets ) {
			$params = array(
				'screen_name'      => $account, // Twitter account name
				'trim_user'        => true,     // only basic user data (slims the result)
				'include_entities' => true
			);

			// If combined with $count, $exclude_replies only filters that number of tweets (not all tweets up to the requested 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 );

			switch( $response_code ) {
				case 200 : // process tweets and display
					$tweets = json_decode( wp_remote_retrieve_body( $response ), true );

					if ( ! is_array( $tweets ) || isset( $tweets['error'] ) ) {
						do_action( 'jetpack_bump_stats_extras', 'twitter_widget', "request-fail-{$response_code}-bad-data" );
						$the_error = '<p>' . esc_html__( 'Error: Twitter did not respond. Please wait a few minutes and refresh this page.', 'jetpack' ) . '</p>';
						$tweet_cache_expire = 300;
						break;
					} else {				
						set_transient( 'widget-twitter-backup-' . $this->number, $tweets, 86400 ); // A one day backup in case there is trouble talking to Twitter
					}

					do_action( 'jetpack_bump_stats_extras', 'twitter_widget', 'request-success' );
					$tweet_cache_expire =  900; 
					break;
				case 401 : // display private stream notice
					do_action( 'jetpack_bump_stats_extras', 'twitter_widget', "request-fail-{$response_code}" );

					$tweets = array();
					$the_error = '<p>' . sprintf( esc_html__( 'Error: Please make sure the Twitter account is %1$spublic%2$s.', 'jetpack' ), '<a href="http://support.twitter.com/forums/10711/entries/14016">', '</a>' ) . '</p>';
					$tweet_cache_expire = 300;
					break;
				default :  // display an error message
					do_action( 'jetpack_bump_stats_extras', 'twitter_widget', "request-fail-{$response_code}" );

					$tweets = get_transient( 'widget-twitter-backup-' . $this->number );
					$the_error = '<p>' . esc_html__( 'Error: Twitter did not respond. Please wait a few minutes and refresh this page.', 'jetpack' ) . '</p>';
					$tweet_cache_expire = 300;
					break;
			}

			set_transient( 'widget-twitter-' . $this->number, $tweets, $tweet_cache_expire );
			set_transient( 'widget-twitter-error-' . $this->number, $the_error, $tweet_cache_expire );
		}

		return array( 'data' => $tweets, 'error' => $the_error );
	}

	function update( $new_instance, $old_instance ) {
		$instance = array();

		$instance['account'] = trim( wp_kses( $new_instance['account'], array() ) );
		$instance['account'] = str_replace( array( 'http://twitter.com/', '/', '@', '#!', ), array( '', '', '', '', ), $instance['account'] );

		$instance['title']           = wp_kses( $new_instance['title'], array() );
		$instance['show']            = absint( $new_instance['show'] );
		$instance['hidereplies']     = isset( $new_instance['hidereplies'] );
		$instance['hidepublicized']  = isset( $new_instance['hidepublicized'] );
		$instance['includeretweets'] = isset( $new_instance['includeretweets'] );

		if ( $instance['followbutton'] != $new_instance['followbutton'] ) {
			if ( $new_instance['followbutton'] )
				do_action( 'jetpack_bump_stats_extras', 'twitter_widget', 'follow_button_enabled' );
			else
				do_action( 'jetpack_bump_stats_extras', 'twitter_widget', 'follow_button_disabled' );
		}

		$instance['followbutton']    = ! isset( $new_instance['followbutton'] ) ? 0 : 1;
		$instance['beforetimesince'] = $new_instance['beforetimesince'];

		delete_transient( 'widget-twitter-' . $this->number );
		delete_transient( 'widget-twitter-error-' . $this->number );

		return $instance;
	}

	function form( $instance ) {
		//Defaults
		$account          = isset( $instance['account'] )                                                  ? wp_kses( $instance['account'], array() ) : '';
		$title            = isset( $instance['title'] )                                                    ? $instance['title']                       : '';
		$show             = isset( $instance['show'] )                                                     ? absint( $instance['show'] )              : 5;
		$show             = ( $show < 1 || 20 < $show )                                                    ? 5                                        : $show;
		$hidereplies      = isset( $instance['hidereplies'] ) && ! empty( $instance['hidereplies'] )         ? (bool) $instance['hidereplies']          : false;
		$hidepublicized   = isset( $instance['hidepublicized'] ) && ! empty( $instance['hidepublicized'] )   ? (bool) $instance['hidepublicized']       : false;
		$include_retweets = isset( $instance['includeretweets'] ) && ! empty( $instance['includeretweets'] ) ? (bool) $instance['includeretweets']      : false;
		$follow_button    = isset( $instance['followbutton'] ) && ! empty( $instance['followbutton'] )       ? 1                                        : 0;
		$before_timesince = isset( $instance['beforetimesince'] ) && ! empty( $instance['beforetimesince'] ) ? esc_attr( $instance['beforetimesince'] ) : '';
		?>

		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>">
				<?php esc_html_e( 'Title:', 'jetpack' )?>
				<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 ); ?>" />
			</label>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'account' ); ?>">
				<?php esc_html_e( 'Twitter username:', 'jetpack' ); ?> <a href="http://support.wordpress.com/widgets/twitter-widget/#twitter-username" target="_blank">( ? )</a>
				<input class="widefat" id="<?php echo $this->get_field_id( 'account' ); ?>" name="<?php echo $this->get_field_name( 'account' ); ?>" type="text" value="<?php echo esc_attr( $account ); ?>" />
			</label>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'show' ); ?>">
				<?php esc_html_e( 'Maximum number of Tweets to show:', 'jetpack' ); ?>
				<select id="<?php echo $this->get_field_id( 'show' ); ?>" name="<?php echo $this->get_field_name( 'show' ); ?>">
					<?php
					for ( $i = 1; $i <= 20; ++$i ) :
						?><option value="<?php echo esc_attr( $i ); ?>" <?php selected( $show, $i ); ?>><?php echo esc_attr( $i ); ?></option><?php
					endfor;
					?>
				</select>
			</label>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'hidereplies' ); ?>">
				<input id="<?php echo $this->get_field_id( 'hidereplies' );?>" class="checkbox" type="checkbox" name="<?php echo $this->get_field_name( 'hidereplies' ); ?>" <?php checked( $hidereplies, true ); ?> /> 
				<?php esc_html_e( 'Hide replies', 'jetpack' ); ?>
			</label>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'hidepublicized' ); ?>">
				<input id="<?php echo $this->get_field_id( 'hidepublicized' ); ?>" class="checkbox" type="checkbox" name="<?php echo $this->get_field_name( 'hidepublicized' ); ?>" <?php checked( $hidepublicized, true ); ?> />
 				<?php esc_html_e( 'Hide Tweets pushed by Publicize', 'jetpack' ); ?>
 			</label>
 		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'includeretweets' ); ?>">
				<input id="<?php echo $this->get_field_id( 'includeretweets' ); ?>" class="checkbox" type="checkbox" name="<?php echo $this->get_field_name( 'includeretweets' ); ?>" <?php checked( $include_retweets, true ); ?> />
				<?php esc_html_e( 'Include retweets', 'jetpack' ); ?>
			</label>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'followbutton' ); ?>">
				<input id="<?php echo $this->get_field_id( 'followbutton' ); ?>" class="checkbox" type="checkbox" name="<?php echo $this->get_field_name( 'followbutton' ); ?>" <?php checked( $follow_button, 1 ); ?> /> 
				<?php esc_html_e( 'Display Follow Button', 'jetpack' ); ?>
			</label>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'beforetimesince' ); ?>">
				<?php esc_html_e( 'Text to display between Tweet and timestamp:', 'jetpack' ); ?>
				<input class="widefat" id="<?php echo $this->get_field_id( 'beforetimesince' ); ?>" name="<?php echo $this->get_field_name( 'beforetimesince' ); ?>" type="text" value="<?php echo esc_attr( $before_timesince ); ?>" />
			</label>
		</p>

		<?php
	}

	function time_since( $original, $do_more = 0 ) {
		// array of time period chunks
		$chunks = array(
			array(60 * 60 * 24 * 365 , 'year'),
			array(60 * 60 * 24 * 30 , 'month'),
			array(60 * 60 * 24 * 7, 'week'),
			array(60 * 60 * 24 , 'day'),
			array(60 * 60 , 'hour'),
			array(60 , 'minute'),
		);

		$today = time();
		$since = $today - $original;

		for ($i = 0, $j = count($chunks); $i < $j; $i++) {
			$seconds = $chunks[$i][0];
			$name = $chunks[$i][1];

			if (($count = floor($since / $seconds)) != 0)
				break;
		}

		$print = ($count == 1) ? '1 '.$name : "$count {$name}s";

		if ($i + 1 < $j) {
			$seconds2 = $chunks[$i + 1][0];
			$name2 = $chunks[$i + 1][1];

			// add second item if it's greater than 0
			if ( (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) && $do_more )
				$print .= ($count2 == 1) ? ', 1 '.$name2 : ", $count2 {$name2}s";
		}
		return $print;
	}

	/**
	 * 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 _jetpack_widget_twitter_username( array $matches ) { // $matches has already been through wp_specialchars
		return "$matches[1]@<a href='" . esc_url( 'http://twitter.com/' . urlencode( $matches[3] ) ) . "'>$matches[3]</a>";
	}

	/**
	 * 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 _jetpack_widget_twitter_hashtag( array $matches ) { // $matches has already been through wp_specialchars
		return "$matches[1]<a href='" . esc_url( 'http://twitter.com/search?q=%23' . urlencode( $matches[3] ) ) . "'>#$matches[3]</a>";
	}

	function twitter_widget_script() {
		if ( ! wp_script_is( 'twitter-widgets', 'registered' ) ) {
			if ( is_ssl() )
				$twitter_widget_js = 'https://platform.twitter.com/widgets.js';
			else
				$twitter_widget_js = 'http://platform.twitter.com/widgets.js';
			wp_register_script( 'twitter-widgets', $twitter_widget_js,  array(), '20111117', true );
			wp_print_scripts( 'twitter-widgets' );
		}
	}
}