/
widget.php
582 lines (499 loc) · 21.2 KB
/
widget.php
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<?php
/*
Plugin Name: Multi Twitter Stream
Plugin URI: https://github.com/msenateatplos/multi-twitter-widget
Description: A widget for multiple twitter accounts and keyword search
Author: Clayton McIlrath, Roger Hamilton, Matt Senate
Version: 1.5.1
*/
/*
* @TODO:
* + Options for order arrangement (chrono, alpha, etc)
* + Change admin form to allow free text field (using intval() anyway to sanitize)
* + Remove in-line styles, create custom style sheet and enqueue w/ WP
*/
function human_time($datefrom, $dateto = -1)
{
// Defaults and assume if 0 is passed in that its an error rather than the epoch
if ( $datefrom <= 0 )
return "A long time ago";
if ( $dateto == -1) {
$dateto = time();
}
// Calculate the difference in seconds betweeen the two timestamps
$difference = $dateto - $datefrom;
// If difference is less than 60 seconds use 'seconds'
if ( $difference < 60 )
{
$interval = "s";
}
// If difference is between 60 seconds and 60 minutes use 'minutes'
else if ( $difference >= 60 AND $difference < (60*60) )
{
$interval = "n";
}
// If difference is between 1 hour and 24 hours use 'hours'
else if ( $difference >= (60*60) AND $difference < (60*60*24) )
{
$interval = "h";
}
// If difference is between 1 day and 7 days use 'days'
else if ( $difference >= (60*60*24) AND $difference < (60*60*24*7) )
{
$interval = "d";
}
// If difference is between 1 week and 30 days use 'weeks'
else if ( $difference >= (60*60*24*7) AND $difference < (60*60*24*30) )
{
$interval = "ww";
}
// If difference is between 30 days and 365 days use 'months'
else if ( $difference >= (60*60*24*30) AND $difference < (60*60*24*365) )
{
$interval = "m";
}
// If difference is greater than or equal to 365 days use 'years'
else if ( $difference >= (60*60*24*365) )
{
$interval = "y";
}
// Based on the interval, determine the number of units between the two dates
// If the $datediff returned is 1, be sure to return the singular
// of the unit, e.g. 'day' rather 'days'
switch ($interval)
{
case "m" :
$months_difference = floor($difference / 60 / 60 / 24 / 29);
while(
mktime(date("H", $datefrom), date("i", $datefrom),
date("s", $datefrom), date("n", $datefrom)+($months_difference),
date("j", $dateto), date("Y", $datefrom)) < $dateto)
{
$months_difference++;
}
$datediff = $months_difference;
// We need this in here because it is possible to have an 'm' interval and a months
// difference of 12 because we are using 29 days in a month
if ( $datediff == 12 )
{
$datediff--;
}
$res = ($datediff==1) ? "$datediff month ago" : "$datediff months ago";
break;
case "y" :
$datediff = floor($difference / 60 / 60 / 24 / 365);
$res = ($datediff==1) ? "$datediff year ago" : "$datediff years ago";
break;
case "d" :
$datediff = floor($difference / 60 / 60 / 24);
$res = ($datediff==1) ? "$datediff day ago" : "$datediff days ago";
break;
case "ww" :
$datediff = floor($difference / 60 / 60 / 24 / 7);
$res = ($datediff==1) ? "$datediff week ago" : "$datediff weeks ago";
break;
case "h" :
$datediff = floor($difference / 60 / 60);
$res = ($datediff==1) ? "$datediff hour ago" : "$datediff hours ago";
break;
case "n" :
$datediff = floor($difference / 60);
$res = ($datediff==1) ? "$datediff minute ago" : "$datediff minutes ago";
break;
case "s":
$datediff = $difference;
$res = ($datediff==1) ? "$datediff second ago" : "$datediff seconds ago";
break;
}
return $res;
}
function format_tweet($tweet, $widget)
{
if ( $widget['reply'] )
$tweet = preg_replace('/(^|\s)@(\w+)/', '\1@<a href="http://www.twitter.com/\2">\2</a>', $tweet);
if ( $widget['hash'] )
$tweet = preg_replace('/(^|\s)#(\w+)/', '\1#<a href="http://search.twitter.com/search?q=%23\2">\2</a>', $tweet);
if( $widget['links'] )
$tweet = preg_replace('#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)#is', '\1<a href="\2">\2</a>', $tweet);
return $tweet;
}
function feed_sort($a, $b)
{
if ( $a['created_at'] )
{
$a_t = strtotime($a['created_at']);
$b_t = strtotime($b['created_at']);
}
else if ( $a['updated'] )
{
$a_t = strtotime($a['updated']);
$b_t = strtotime($b['updated']);
}
if ($a_t == $b_t)
return 0;
return ($a_t > $b_t) ? -1 : 1;
}
function multi_twitter($widget)
{
if ( ! class_exists('Codebird') )
require 'lib/codebird.php';
// Initialize Codebird with our keys. We'll wait and
// pass the token when we make an actual request
Codebird::setConsumerKey($widget['consumer_key'], $widget['consumer_secret']);
$cb = Codebird::getInstance();
$output = '';
// Get our root upload directory and create cache if necessary
$upload = wp_upload_dir();
$upload_dir = $upload['basedir'] . "/cache";
if ( ! file_exists($upload_dir) )
{
if ( ! mkdir($upload_dir))
{
$output .= '<span style="color: red;">could not create dir' . $upload_dir . ' please create this directory</span>';
return $output;
}
}
// split the accounts and search terms specified in the widget
$accounts = explode(" ", $widget['users']);
$terms = explode(", ", $widget['terms']);
$output .= '<ul class="multi-twitter">';
// Parse the accounts and CRUD cache
$feeds = null;
foreach ( $accounts as $account )
{
if ( $account != "" )
{
$cache = false;
// Assume the cache is empty
$cFile = "$upload_dir/users_$account.txt";
if ( file_exists($cFile) )
{
$modtime = filemtime($cFile);
$timeago = time() - 1800; // 30 minutes ago
// Check if length is less than new limit
$str = file_get_contents($cFile);
$content = unserialize($str);
$length = count($content);
if ( $modtime < $timeago or $length != $widget['user_limit'] )
{
// Set to false just in case as the cache needs to be renewed
$cache = false;
}
else
{
// The cache is not too old so the cache can be used.
$cache = true;
}
}
// begin
if ( $cache === false )
{
$cb->setToken($widget['access_token'], $widget['access_token_secret']);
$params = array('screen_name' => $account, 'count' => $widget['user_limit']);
// let Codebird make an authenticated request Result is json
$reply = $cb->statuses_userTimeline($params);
// turn the json into an array
$json = json_decode($reply, true);
$length = count($json);
for ($i = 0; $i < $length; $i++)
{
// add it to the feeds array
$feeds[] = $json[$i];
// prepare it for caching
$content[] = $json[$i];
}
// Let's save our data into uploads/cache/
$fp = fopen($cFile, 'w');
if (!$fp)
{
$output .= '<li style="color: red;">Permission to write cache dir to <em>' . $cFile . '</em> not granted</li>';
}
else
{
$str = serialize($content);
fwrite($fp, $str);
}
fclose($fp);
$content = null;
}
else
{
//cache is true let's load the data from the cached file
$str = file_get_contents($cFile);
$content = unserialize($str);
$length = count($content);
// echo $length;
for ($i = 0; $i < $length; $i++)
{
// add it to the feeds array
$feeds[] = $content[$i];
}
}
} // end account empty check
} // end accounts foreach
// Parse the terms and CRUD cache
foreach ( $terms as $term )
{
if ( $term != "" )
{
$cache = false;
// Assume the cache is empty
$cFile = "$upload_dir/term_$term.txt";
if (file_exists($cFile))
{
$modtime = filemtime($cFile);
$timeago = time() - 1800; // 30 minutes ago
// Check if length is less than new limit
$str = file_get_contents($cFile);
$content = unserialize($str);
$length = count($content);
if ( $modtime < $timeago or $length != $widget['term_limit'] )
{
// Set to false just in case as the cache needs to be renewed
$cache = false;
}
else
{
// The cache is not too old so the cache can be used.
$cache = true;
}
}
if ( $cache === false )
{
$cb->setToken($widget['access_token'], $widget['access_token_secret']);
$search_params = array(
'q' => $term,
'count' => $widget['term_limit']
);
$reply = $cb->search_tweets($search_params);
$json = json_decode($reply, true);
$length = count($json['statuses']);
for ( $i = 0; $i < $length; $i++ )
{
// add it to the feeds array
$feeds[] = $json['statuses'][$i];
// prepare it for caching
$content[] = $json['statuses'][$i];
}
if ($content === false)
{
// Content couldn't be retrieved... Do something..
$output .= '<li>Content could not be retrieved. Twitter API failed...</li>';
}
else
{
// Let's save our data into uploads/cache/
$fp = fopen($cFile, 'w');
if (!$fp)
{
$output .= '<li style="color: red;">Permission to write cache dir to <em>' . $cFile . '</em> not granted</li>';
}
else
{
fwrite($fp, serialize($content));
}
fclose($fp);
$content = null;
}
}
else
{
//cache is true let's load the data from the cached file
$str = file_get_contents($cFile);
$content = unserialize($str);
$length = count($content);
for ($i = 0; $i < $length; $i++)
{
// add it to the feeds array
$feeds[] = $content[$i];
}
}
} // end terms empty check
} // end terms foreach
// Sort our $feeds array
if ( $widget['sort_by_date'] )
{
usort($feeds, "feed_sort");
}
// Split array and output results
$i = 1;
// format the tweet for display
foreach ( $feeds as $feed )
{
if ( ! empty($feed) )
{
$output .= '<li class="tweet clearfix" style="margin-bottom:8px;">' . '<a href="http://twitter.com/' . $feed['user']['screen_name'] . '">' . '<img class="twitter-avatar" style="float:left;margin:4px 10px 0 0;" src="' . $feed['user']['profile_image_url'] . '" width="40" height="40" alt="' . $feed['user']['screen_name'] . '" />' . '</a>';
$output .= '<span class="tweet-userName">' . $feed['user']['name'] . ' - </span>';
if ($widget['date'])
{
$output .= '<span class="tweet-time"><em>' . human_time(strtotime($feed['created_at'])) . ' - </em></span>';
}
$output .= '<span class="tweet-message">' . format_tweet($feed['text'], $widget) . '</span>';
$output .= '</li>';
}
}
$output .= '</ul>';
if ($widget['credits'] === true)
{
$output .= '<hr />' . '<strong>development by</strong> ' . '<a href="http://twitter.com/thinkclay" target="_blank">Clay McIlrath</a>, <a href="http://twitter.com/roger_hamilton" target="_blank">Roger Hamilton</a>, and <a href="http://twitter.com/wrought/" target="_blank">Matt Senate</a>.';
}
if ($widget['styles'] === true)
{
$output .= '<style type="text/css">' . '.twitter-avatar { clear: both; float: left; padding: 6px 12px 2px 0; }' . '.twitter{background:none;}' . '.tweet{min-height:48px;margin:0!important;}' . '.tweet a{text-decoration:underline;}' . '.tweet-userName{padding-top:7px;font-size:12px;line-height:0;color:#454545;font-family:Arial,sans-serif;font-weight:700;margin-bottom:10px;margin-left:8px;float:left;min-width:50px;}' . '.twitter-avatar{width:48px;height:48px;-webkit-border-radius:5px;-moz-border-radius:5px;border-radius:5px;padding:0!important;}' . '.tweet-time{color:#8A8A8A;float:left;margin-top:-3px;font-size:11px!important;}' . '.tweet-message{font-size:11px;line-height:14px;color:#333;font-family:Arial,sans-serif;word-wrap:break-word;margin-top:-30px!important;width:200px;margin-left:58px;}' . '</style>';
}
echo $output;
}
/*
* Attempts to implement extension of WP_Widget class
*/
class multiTwitterWidget extends WP_Widget {
function multiTwitterWidget() {
// Instantiate parent object
parent::__construct(
'multi_twitter',
'Multi Twitter',
array( 'description' => 'Widget to display tweets from multiple twitter accounts')
);
}
function widget( $args, $instance ) {
// Widget output
extract($args);
echo $before_widget;
echo $before_title;
echo $instance['title'];
echo $after_title;
multi_twitter($instance);
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
// Save widget options
$new_instance = (array) $new_instance;
$instance = array( 'hash' => 0, 'reply' => 0, 'links' => 0, 'date' => 0, 'sort_by_date' => 0, 'credits' => 0, 'styles' => 0 );
// Set values
foreach ( $instance as $field => $val ) {
if (isset($new_instance[$field]) )
$instance[$field] = 1; // loop through booleans
}
$instance['consumer_key'] = htmlspecialchars($new_instance['consumer_key']);
$instance['consumer_secret'] = htmlspecialchars($new_instance['consumer_secret']);
$instance['access_token'] = htmlspecialchars($new_instance['access_token']);
$instance['access_token_secret'] = htmlspecialchars($new_instance['access_token_secret']);
$instance['title'] = htmlspecialchars($new_instance['title']);
$instance['users'] = htmlspecialchars($new_instance['users']);
$instance['terms'] = htmlspecialchars($new_instance['terms']);
$instance['user_limit'] = intval($new_instance['user_limit']);
$instance['term_limit'] = intval($new_instance['term_limit']);
// Return
return $instance;
}
function form( $instance ) {
// Default values (if key is not set, use default values below)
$instance = wp_parse_args( (array) $instance, array( 'consumer_key' => 'lNbFfDyYdUdZPhqqlsAGVA', 'consumer_secret' => 'izPPTeFQYlC2UFsxxu6ODZtoOC6FFyPZyXX959p4Z4', 'access_token' => '16468552-NUSSANz4tgU7gUCZPHMWxSdgatO10YtG1SrBuYUAA', 'access_token_secret' => 'SZJZIJ0jdRKa9EQ9T6JpamxOz28GDWuvDl7tydwzHIQ', 'title' => 'Multi Twitter', 'users' => 'thinkclay roger_hamilton wrought', 'terms' => 'wordpress, plos', 'user_limit' => 1, 'term_limit' => 1, 'links' => 1, 'reply' => 1, 'hash' => 1, 'date' => 1, 'sort_by_date' => 1, 'credits' => 0, 'styles' => 1 ) );
// Output admin widget options form
?>
<fieldset style="border:1px solid gray;padding:10px;">
<legend>Oauth Settings</legend>
<p>
<label for="<?php echo $this->get_field_id('consumer_key'); ?>">Consumer Key: </label><br />
<input type="text" class="widefat" id="<?php echo $this->get_field_id('consumer_key'); ?>" name="<?php echo $this->get_field_name('consumer_key'); ?>" value="<?php echo $instance['consumer_key']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('consumer_secret'); ?>">Consumer Secret: </label><br />
<input type="text" class="widefat" id="<?php echo $this->get_field_id('consumer_secret'); ?>" name="<?php echo $this->get_field_name('consumer_secret'); ?>" value="<?php echo $instance['consumer_secret']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('access_token'); ?>">Access Token: </label><br />
<input type="text" class="widefat" id="<?php echo $this->get_field_id('access_token'); ?>" name="<?php echo $this->get_field_name('access_token'); ?>" value="<?php echo $instance['access_token']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('access_token_secret'); ?>">Access Token Secret: </label><br />
<input type="text" class="widefat" id="<?php echo $this->get_field_id('access_token_secret'); ?>" name="<?php echo $this->get_field_name('access_token_secret'); ?>" value="<?php echo $instance['access_token_secret']; ?>" />
</p>
</fieldset>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Widget Title: </label><br />
<input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('users'); ?>">Users: </label><br />
<input type="text" class="widefat" id="<?php echo $this->get_field_id('users'); ?>" name="<?php echo $this->get_field_name('users'); ?>" value="<?php echo $instance['users']; ?>" /><br />
<small><em>enter accounts separated with a space</em></small>
</p>
<p>
<label for="<?php echo $this->get_field_id('terms'); ?>">Search Terms: </label><br />
<input type="text" class="widefat" id="<?php echo $this->get_field_id('terms'); ?>" name="<?php echo $this->get_field_name('terms'); ?>" value="<?php echo $instance['terms']; ?>" /><br />
<small><em>enter search terms separated with a comma</em></small>
</p>
<p>
<label for="<?php echo $this->get_field_id('user_limit'); ?>">Limit user feed to: </label>
<select id="<?php echo $this->get_field_id('user_limit'); ?>" name="<?php echo $this->get_field_name('user_limit'); ?>">
<option value="<?php echo $instance['user_limit']; ?>"><?php echo $instance['user_limit']; ?></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<br />
<small><em>Limits number of results for each user.</em></small>
</p>
<p>
<label for="<?php echo $this->get_field_id('term_limit'); ?>">Limit search terms to: </label>
<select id="<?php echo $this->get_field_id('term_limit'); ?>" name="<?php echo $this->get_field_name('term_limit'); ?>">
<option value="<?php echo $instance['term_limit']; ?>"><?php echo $instance['term_limit']; ?></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<br />
<small><em>Limits number of results for each term.</em></small>
</p>
<p>
<label for="<?php echo $this->get_field_id('links'); ?>">Automatically convert links?</label>
<input type="checkbox" name="<?php echo $this->get_field_name('links'); ?>" id="<?php echo $this->get_field_id('links'); ?>" <?php if ($instance['links']) echo 'checked="checked"'; ?> />
</p>
<p>
<label for="<?php echo $this->get_field_id('reply'); ?>">Automatically convert @replies?</label>
<input type="checkbox" name="<?php echo $this->get_field_name('reply'); ?>" id="<?php echo $this->get_field_id('reply'); ?>" <?php if ($instance['reply']) echo 'checked="checked"'; ?> />
</p>
<p>
<label for="<?php echo $this->get_field_id('hash'); ?>">Automatically convert #hashtags?</label>
<input type="checkbox" name="<?php echo $this->get_field_name('hash'); ?>" id="<?php echo $this->get_field_id('hash'); ?>" <?php if ($instance['hash']) echo 'checked="checked"'; ?> />
</p>
<p>
<label for="<?php echo $this->get_field_id('date'); ?>">Show Date?</label>
<input type="checkbox" name="<?php echo $this->get_field_name('date'); ?>" id="<?php echo $this->get_field_id('date'); ?>" <?php if ($instance['date']) echo 'checked="checked"'; ?> />
</p>
<p>
<label for="<?php echo $this->get_field_id('sort_by_date'); ?>">Sort by Date?</label>
<input type="checkbox" name="<?php echo $this->get_field_name('sort_by_date'); ?>" id="<?php echo $this->get_field_id('sort_by_date'); ?>" <?php if ($instance['sort_by_date']) echo 'checked="checked"'; ?> />
</p>
<p>
<label for="<?php echo $this->get_field_id('credits'); ?>">Show Credits?</label>
<input type="checkbox" name="<?php echo $this->get_field_name('credits'); ?>" id="<?php echo $this->get_field_id('credits'); ?>" <?php if ($instance['credits']) echo 'checked="checked"'; ?> />
</p>
<p>
<label for="<?php echo $this->get_field_id('styles'); ?>">Use Default Styles?</label>
<input type="checkbox" name="<?php echo $this->get_field_name('styles'); ?>" id="<?php echo $this->get_field_id('styles'); ?>" <?php if ($instance['styles']) echo 'checked="checked"'; ?> />
</p>
<?php
}
}
function multi_twitter_init()
{
register_widget( "multiTwitterWidget" );
}
add_action("widgets_init", "multi_twitter_init");
?>