-
Notifications
You must be signed in to change notification settings - Fork 19
/
wp-twitter-widget.php
1471 lines (1303 loc) · 55.7 KB
/
wp-twitter-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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: Twitter Widget Pro
* Plugin URI: https://aarondcampbell.com/wordpress-plugin/twitter-widget-pro/
* Description: A widget that properly handles twitter feeds, including @username, #hashtag, and link parsing. It can even display profile images for the users. Requires PHP5.
* Version: 2.9.0
* Author: Aaron D. Campbell
* Author URI: https://aarondcampbell.com/
* License: GPLv2 or later
* Text Domain: twitter-widget-pro
*/
/*
Copyright 2006-current Aaron D. Campbell ( email : [email protected] )
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
( at your option ) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
require_once( 'tlc-transients.php' );
require_once( 'class.wp_widget_twitter_pro.php' );
/**
* wpTwitterWidget is the class that handles everything outside the widget. This
* includes filters that modify tweet content for things like linked usernames.
* It also helps us avoid name collisions.
*/
class wpTwitterWidget {
/**
* @var wpTwitter
*/
private $_wp_twitter_oauth;
/**
* @var wpTwitterWidget - Static property to hold our singleton instance
*/
static $instance = false;
/**
* @var array Plugin settings
*/
protected $_settings;
/**
* @var string - The options page name used in the URL
*/
protected $_hook = 'twitterWidgetPro';
/**
* @var string - The filename for the main plugin file
*/
protected $_file = '';
/**
* @var string - The options page title
*/
protected $_pageTitle = '';
/**
* @var string - The options page menu title
*/
protected $_menuTitle = '';
/**
* @var string - The access level required to see the options page
*/
protected $_accessLevel = 'manage_options';
/**
* @var string - The option group to register
*/
protected $_optionGroup = 'twp-options';
/**
* @var array - An array of options to register to the option group
*/
protected $_optionNames = array( 'twp' );
/**
* @var array - An associated array of callbacks for the options, option name should be index, callback should be value
*/
protected $_optionCallbacks = array();
/**
* @var string - The plugin slug used on WordPress.org
*/
protected $_slug = '';
/**
* @var string - The feed URL for AaronDCampbell.com
*/
protected $_feed_url = 'http://aarondcampbell.com/feed/';
/**
* @var string - The button ID for the PayPal button, override this generic one with a plugin-specific one
*/
protected $_paypalButtonId = '9993090';
protected $_optionsPageAction = 'options.php';
/**
* This is our constructor, which is private to force the use of getInstance()
* @return void
*/
protected function __construct() {
$this->_file = plugin_basename( __FILE__ );
$this->_pageTitle = __( 'Twitter Widget Pro', 'twitter-widget-pro' );
$this->_menuTitle = __( 'Twitter Widget', 'twitter-widget-pro' );
/**
* Add filters and actions
*/
add_action( 'init', array( $this, 'init' ) );
add_action( 'admin_init', array( $this, 'handle_actions' ) );
add_action( 'admin_notices', array( $this, 'show_messages' ) );
add_action( 'widgets_init', array( $this, 'register' ), 11 );
add_filter( 'widget_twitter_content', array( $this, 'linkTwitterUsers' ) );
add_filter( 'widget_twitter_content', array( $this, 'linkUrls' ) );
add_filter( 'widget_twitter_content', array( $this, 'linkHashtags' ) );
add_filter( 'widget_twitter_content', 'convert_chars' );
add_filter( 'twitter-widget-pro-opt-twp', array( $this, 'filterSettings' ) );
add_filter( 'twitter-widget-pro-opt-twp-authed-users', array( $this, 'authed_users_option' ) );
add_shortcode( 'twitter-widget', array( $this, 'handleShortcodes' ) );
$this->_get_settings();
if ( is_callable( array($this, '_post_settings_init') ) )
$this->_post_settings_init();
add_filter( 'init', array( $this, 'init_locale' ) );
add_action( 'admin_init', array( $this, 'register_options' ) );
add_filter( 'plugin_action_links', array( $this, 'add_plugin_page_links' ), 10, 2 );
add_filter( 'plugin_row_meta', array( $this, 'add_plugin_meta_links' ), 10, 2 );
add_action( 'admin_menu', array( $this, 'register_options_page' ) );
if ( is_callable(array( $this, 'add_options_meta_boxes' )) )
add_action( 'admin_init', array( $this, 'add_options_meta_boxes' ) );
add_action( 'admin_init', array( $this, 'add_default_options_meta_boxes' ) );
add_action( 'admin_print_scripts', array( $this,'admin_print_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this,'admin_enqueue_scripts' ) );
add_action ( 'in_plugin_update_message-'.$this->_file , array ( $this , 'changelog' ), null, 2 );
}
protected function _post_settings_init() {
$oauth_settings = array(
'consumer-key' => $this->_settings['twp']['consumer-key'],
'consumer-secret' => $this->_settings['twp']['consumer-secret'],
);
if ( ! class_exists( 'wpTwitter' ) ) {
require_once( 'lib/wp-twitter.php' );
}
$this->_wp_twitter_oauth = new wpTwitter( $oauth_settings );
// We want to fill 'twp-authed-users' but not overwrite them when saving
$this->_settings['twp-authed-users'] = apply_filters('twitter-widget-pro-opt-twp-authed-users', get_option('twp-authed-users'));
}
/**
* Function to instantiate our class and make it a singleton
*/
public static function getInstance() {
if ( !self::$instance )
self::$instance = new self;
return self::$instance;
}
public function handle_actions() {
if ( empty( $_GET['action'] ) || empty( $_GET['page'] ) || $_GET['page'] != $this->_hook )
return;
if ( 'clear-locks' == $_GET['action'] ) {
check_admin_referer( 'clear-locks' );
$redirect_args = array( 'message' => strtolower( $_GET['action'] ) );
global $wpdb;
$locks_q = "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE '_transient_tlc_up__twp%'";
$redirect_args['locks_cleared'] = $wpdb->query( $locks_q );
wp_safe_redirect( add_query_arg( $redirect_args, remove_query_arg( array( 'action', '_wpnonce' ) ) ) );
exit;
}
if ( 'remove' == $_GET['action'] ) {
check_admin_referer( 'remove-' . $_GET['screen_name'] );
$redirect_args = array(
'message' => 'removed',
'removed' => '',
);
unset( $this->_settings['twp-authed-users'][strtolower($_GET['screen_name'])] );
if ( update_option( 'twp-authed-users', $this->_settings['twp-authed-users'] ) );
$redirect_args['removed'] = $_GET['screen_name'];
wp_safe_redirect( add_query_arg( $redirect_args, $this->get_options_url() ) );
exit;
}
if ( 'authorize' == $_GET['action'] ) {
check_admin_referer( 'authorize' );
$auth_redirect = add_query_arg( array( 'action' => 'authorized' ), $this->get_options_url() );
$token = $this->_wp_twitter_oauth->get_request_token( $auth_redirect );
if ( is_wp_error( $token ) ) {
$this->_error = $token;
return;
}
update_option( '_twp_request_token_'.$token['nonce'], $token );
$screen_name = empty( $_GET['screen_name'] )? '':$_GET['screen_name'];
wp_redirect( $this->_wp_twitter_oauth->get_authorize_url( $screen_name ) );
exit;
}
if ( 'authorized' == $_GET['action'] ) {
$redirect_args = array(
'message' => strtolower( $_GET['action'] ),
'authorized' => '',
);
if ( empty( $_GET['oauth_verifier'] ) || empty( $_GET['nonce'] ) )
wp_safe_redirect( add_query_arg( $redirect_args, $this->get_options_url() ) );
$this->_wp_twitter_oauth->set_token( get_option( '_twp_request_token_'.$_GET['nonce'] ) );
delete_option( '_twp_request_token_'.$_GET['nonce'] );
$token = $this->_wp_twitter_oauth->get_access_token( $_GET['oauth_verifier'] );
if ( ! is_wp_error( $token ) ) {
$this->_settings['twp-authed-users'][strtolower($token['screen_name'])] = $token;
update_option( 'twp-authed-users', $this->_settings['twp-authed-users'] );
$redirect_args['authorized'] = $token['screen_name'];
}
wp_safe_redirect( add_query_arg( $redirect_args, $this->get_options_url() ) );
exit;
}
}
public function show_messages() {
if ( ! empty( $_GET['message'] ) ) {
if ( 'clear-locks' == $_GET['message'] ) {
if ( empty( $_GET['locks_cleared'] ) || 0 == $_GET['locks_cleared'] )
$msg = __( 'There were no locks to clear!', 'twitter-widget-pro' );
else
$msg = sprintf( _n( 'Successfully cleared %d lock.', 'Successfully cleared %d locks.', $_GET['locks_cleared'], 'twitter-widget-pro' ), $_GET['locks_cleared'] );
} elseif ( 'authorized' == $_GET['message'] ) {
if ( ! empty( $_GET['authorized'] ) )
$msg = sprintf( __( 'Successfully authorized @%s', 'twitter-widget-pro' ), $_GET['authorized'] );
else
$msg = __( 'There was a problem authorizing your account.', 'twitter-widget-pro' );
} elseif ( 'removed' == $_GET['message'] ) {
if ( ! empty( $_GET['removed'] ) )
$msg = sprintf( __( 'Successfully removed @%s', 'twitter-widget-pro' ), $_GET['removed'] );
else
$msg = __( 'There was a problem removing your account.', 'twitter-widget-pro' );
}
if ( ! empty( $msg ) )
echo "<div class='updated'><p>" . esc_html( $msg ) . '</p></div>';
}
if ( ! empty( $this->_error ) && is_wp_error( $this->_error ) ) {
$msg = '<p>' . implode( '</p><p>', $this->_error->get_error_messages() ) . '</p>';
echo '<div class="error">' . $msg . '</div>';
}
if ( empty( $this->_settings['twp']['consumer-key'] ) || empty( $this->_settings['twp']['consumer-secret'] ) ) {
$msg = sprintf( __( 'You need to <a href="%s">set up your Twitter app keys</a>.', 'twitter-widget-pro' ), $this->get_options_url() );
echo '<div class="error"><p>' . $msg . '</p></div>';
}
if ( empty( $this->_settings['twp-authed-users'] ) ) {
$msg = sprintf( __( 'You need to <a href="%s">authorize your Twitter accounts</a>.', 'twitter-widget-pro' ), $this->get_options_url() );
echo '<div class="error"><p>' . $msg . '</p></div>';
}
}
public function add_options_meta_boxes() {
add_meta_box( 'twitter-widget-pro-oauth', __( 'Authenticated Twitter Accounts', 'twitter-widget-pro' ), array( $this, 'oauth_meta_box' ), 'aaron-twitter-widget-pro', 'main' );
add_meta_box( 'twitter-widget-pro-general-settings', __( 'General Settings', 'twitter-widget-pro' ), array( $this, 'general_settings_meta_box' ), 'aaron-twitter-widget-pro', 'main' );
add_meta_box( 'twitter-widget-pro-defaults', __( 'Default Settings for Shortcodes', 'twitter-widget-pro' ), array( $this, 'default_settings_meta_box' ), 'aaron-twitter-widget-pro', 'main' );
}
public function oauth_meta_box() {
$authorize_url = wp_nonce_url( add_query_arg( array( 'action' => 'authorize' ) ), 'authorize' );
?>
<table class="widefat">
<thead>
<tr valign="top">
<th scope="row">
<?php _e( 'Username', 'twitter-widget-pro' );?>
</th>
<th scope="row">
<?php _e( 'Lists Rate Usage', 'twitter-widget-pro' );?>
</th>
<th scope="row">
<?php _e( 'Statuses Rate Usage', 'twitter-widget-pro' );?>
</th>
</tr>
</thead>
<?php
foreach ( $this->_settings['twp-authed-users'] as $u ) {
$this->_wp_twitter_oauth->set_token( $u );
$rates = $this->_wp_twitter_oauth->send_authed_request( 'application/rate_limit_status', 'GET', array( 'resources' => 'statuses,lists' ) );
$style = $auth_link = '';
if ( is_wp_error( $rates ) ) {
$query_args = array(
'action' => 'authorize',
'screen_name' => $u['screen_name'],
);
$authorize_user_url = wp_nonce_url( add_query_arg( $query_args ), 'authorize' );
$style = 'color:red;';
$auth_link = ' - <a href="' . esc_url( $authorize_user_url ) . '">' . __( 'Reauthorize', 'twitter-widget-pro' ) . '</a>';
}
$query_args = array(
'action' => 'remove',
'screen_name' => $u['screen_name'],
);
$remove_user_url = wp_nonce_url( add_query_arg( $query_args ), 'remove-' . $u['screen_name'] );
?>
<tr valign="top">
<th scope="row" style="<?php echo esc_attr( $style ); ?>">
<strong>@<?php echo esc_html( $u['screen_name'] ) . $auth_link;?></strong>
<br /><a href="<?php echo esc_url( $remove_user_url ) ?>"><?php _e( 'Remove', 'twitter-widget-pro' ) ?></a>
</th>
<?php
if ( ! is_wp_error( $rates ) ) {
$display_rates = array(
__( 'Lists', 'twitter-widget-pro' ) => $rates->resources->lists->{'/lists/statuses'},
__( 'Statuses', 'twitter-widget-pro' ) => $rates->resources->statuses->{'/statuses/user_timeline'},
);
foreach ( $display_rates as $title => $rate ) {
?>
<td>
<strong><?php echo esc_html( $title ); ?></strong>
<p>
<?php echo sprintf( __( 'Used: %d', 'twitter-widget-pro' ), $rate->limit - $rate->remaining ); ?><br />
<?php echo sprintf( __( 'Remaining: %d', 'twitter-widget-pro' ), $rate->remaining ); ?><br />
<?php
$minutes = ceil( ( $rate->reset - gmdate( 'U' ) ) / 60 );
echo sprintf( _n( 'Limits reset in: %d minutes', 'Limits reset in: %d minutes', $minutes, 'twitter-widget-pro' ), $minutes );
?><br />
<small><?php _e( 'This is overall usage, not just usage from Twitter Widget Pro', 'twitter-widget-pro' ); ?></small>
</p>
</td>
<?php
}
} else {
?>
<td>
<p><?php _e( 'There was an error checking your rate limit.', 'twitter-widget-pro' ); ?></p>
</td>
<td>
<p><?php _e( 'There was an error checking your rate limit.', 'twitter-widget-pro' ); ?></p>
</td>
<?php
}
?>
</tr>
<?php
}
?>
</table>
<?php
if ( empty( $this->_settings['twp']['consumer-key'] ) || empty( $this->_settings['twp']['consumer-secret'] ) ) {
?>
<p>
<strong><?php _e( 'You need to fill in the Consumer key and Consumer secret before you can authorize accounts.', 'twitter-widget-pro' ) ?></strong>
</p>
<?php
} else {
?>
<p>
<a href="<?php echo esc_url( $authorize_url );?>" class="button button-large button-primary"><?php _e( 'Authorize New Account', 'twitter-widget-pro' ); ?></a>
</p>
<?php
}
}
public function general_settings_meta_box() {
$clear_locks_url = wp_nonce_url( add_query_arg( array( 'action' => 'clear-locks' ) ), 'clear-locks' );
?>
<table class="form-table">
<tr valign="top">
<th scope="row">
<label for="twp_consumer_key"><?php _e( 'Consumer key', 'twitter-widget-pro' );?></label>
</th>
<td>
<input id="twp_consumer_key" name="twp[consumer-key]" type="text" class="regular-text code" value="<?php esc_attr_e( $this->_settings['twp']['consumer-key'] ); ?>" size="40" />
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="twp_consumer_secret"><?php _e( 'Consumer secret', 'twitter-widget-pro' );?></label>
</th>
<td>
<input id="twp_consumer_secret" name="twp[consumer-secret]" type="text" class="regular-text code" value="<?php esc_attr_e( $this->_settings['twp']['consumer-secret'] ); ?>" size="40" />
</td>
</tr>
<?php
if ( empty( $this->_settings['twp']['consumer-key'] ) || empty( $this->_settings['twp']['consumer-secret'] ) ) {
?>
<tr valign="top">
<th scope="row"> </th>
<td>
<strong><?php _e( 'Directions to get the Consumer Key and Consumer Secret', 'twitter-widget-pro' ) ?></strong>
<ol>
<li><a href="https://dev.twitter.com/apps/new"><?php _e( 'Add a new Twitter application', 'twitter-widget-pro' ) ?></a></li>
<li><?php _e( "Fill in Name, Description, Website, and Callback URL (don't leave any blank) with anything you want" ) ?></a></li>
<li><?php _e( "Agree to rules, fill out captcha, and submit your application" ) ?></a></li>
<li><?php _e( "Copy the Consumer key and Consumer secret into the fields above" ) ?></a></li>
<li><?php _e( "Click the Update Options button at the bottom of this page" ) ?></a></li>
</ol>
</td>
</tr>
<?php
}
?>
<tr>
<th scope="row">
<?php _e( "Clear Update Locks", 'twitter-widget-pro' );?>
</th>
<td>
<a href="<?php echo esc_url( $clear_locks_url ); ?>"><?php _e( 'Clear Update Locks', 'twitter-widget-pro' ); ?></a><br />
<small><?php _e( "A small percentage of servers seem to have issues where an update lock isn't getting cleared. If you're experiencing issues with your feed not updating, try clearing the update locks.", 'twitter-widget-pro' ); ?></small>
</td>
</tr>
<tr>
<th scope="row">
<?php _e( 'Local requests:', 'twitter-widget-pro' ); ?>
</th>
<td>
<?php
if ( ! empty( $_GET['action'] ) && 'test-local-request' == $_GET['action'] ) {
check_admin_referer( 'test-local-request' );
$server_url = home_url( '/?twp-test-local-request' );
$resp = wp_remote_post( $server_url, array( 'body' => array( '_twp-test-local-request' => 'test' ), 'sslverify' => apply_filters( 'https_local_ssl_verify', true ) ) );
if ( !is_wp_error( $resp ) && $resp['response']['code'] >= 200 && $resp['response']['code'] < 300 ) {
if ( 'success' === wp_remote_retrieve_body( $resp ) )
_e( '<p style="color:green;">Local requests appear to be functioning normally.</p>', 'twitter-widget-pro' );
else
_e( '<p style="color:red;">The request went through, but an unexpected response was received.</p>', 'twitter-widget-pro' );
} else {
printf( __( '<p style="color:red;">Failed. Your server said: %s</p>', 'twitter-widget-pro' ), $resp['response']['message'] );
}
}
$query_args = array(
'action' => 'test-local-request',
);
$test_local_url = wp_nonce_url( add_query_arg( $query_args, $this->get_options_url() ), 'test-local-request' );
?>
<a href="<?php echo esc_url( $test_local_url ); ?>" class="button">
<?php _e( 'Test local requests', 'twitter-widget-pro' ); ?>
</a><br />
<small><?php _e( "Twitter Widget Pro updates tweets in the background by placing a local request to your server. If your Tweets aren't updating, test this. If it fails, let your host know that loopback requests aren't working on your site.", 'twitter-widget-pro' ); ?></small>
</td>
</tr>
</table>
<?php
}
public function default_settings_meta_box() {
$users = $this->get_users_list( true );
$lists = $this->get_lists();
?>
<p><?php _e( 'These settings are the default for the shortcodes and all of them can be overridden by specifying a different value in the shortcode itself. All settings for widgets are locate in the individual widget.', 'twitter-widget-pro' ) ?></p>
<table class="form-table">
<tr valign="top">
<th scope="row">
<label for="twp_username"><?php _e( 'Twitter username:', 'twitter-widget-pro' ); ?></label>
</th>
<td>
<select id="twp_username" name="twp[username]">
<option></option>
<?php
$selected = false;
foreach ( $users as $u ) {
?>
<option value="<?php echo esc_attr( strtolower( $u['screen_name'] ) ); ?>"<?php $s = selected( strtolower( $u['screen_name'] ), strtolower( $this->_settings['twp']['username'] ) ) ?>><?php echo esc_html( $u['screen_name'] ); ?></option>
<?php
if ( ! empty( $s ) )
$selected = true;
}
?>
</select>
<?php
if ( ! $selected && ! empty( $this->_settings['twp']['username'] ) ) {
$query_args = array(
'action' => 'authorize',
'screen_name' => $this->_settings['twp']['username'],
);
$authorize_user_url = wp_nonce_url( add_query_arg( $query_args, $this->get_options_url() ), 'authorize' );
?>
<p>
<a href="<?php echo esc_url( $authorize_user_url ); ?>" style="color:red;">
<?php _e( 'You need to authorize this account.', 'twitter-widget-pro' ); ?>
</a>
</p>
<?php
}
?>
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="twp_list"><?php _e( 'Twitter list:', 'twitter-widget-pro' ); ?></label>
</th>
<td>
<select id="twp_list" name="twp[list]">
<option></option>
<?php
foreach ( $lists as $user => $user_lists ) {
echo '<optgroup label="' . esc_attr( $user ) . '">';
foreach ( $user_lists as $list_id => $list_name ) {
?>
<option value="<?php echo esc_attr( $user . '::' . $list_id ); ?>"<?php $s = selected( $user . '::' . $list_id, strtolower( $this->_settings['twp']['list'] ) ) ?>><?php echo esc_html( $list_name ); ?></option>
<?php
}
echo '</optgroup>';
}
?>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="twp_title"><?php _e( 'Give the feed a title ( optional ):', 'twitter-widget-pro' ); ?></label>
</th>
<td>
<input id="twp_title" name="twp[title]" type="text" class="regular-text code" value="<?php esc_attr_e( $this->_settings['twp']['title'] ); ?>" size="40" />
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="twp_items"><?php _e( 'How many items would you like to display?', 'twitter-widget-pro' ); ?></label>
</th>
<td>
<select id="twp_items" name="twp[items]">
<?php
for ( $i = 1; $i <= 20; ++$i ) {
echo "<option value='$i' ". selected( $this->_settings['twp']['items'], $i, false ). ">$i</option>";
}
?>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="twp_avatar"><?php _e( 'Display profile image?', 'twitter-widget-pro' ); ?></label>
</th>
<td>
<select id="twp_avatar" name="twp[avatar]">
<option value=""<?php selected( $this->_settings['twp']['avatar'], '' ) ?>><?php _e( 'Do not show', 'twitter-widget-pro' ); ?></option>
<option value="mini"<?php selected( $this->_settings['twp']['avatar'], 'mini' ) ?>><?php _e( 'Mini - 24px by 24px', 'twitter-widget-pro' ); ?></option>
<option value="normal"<?php selected( $this->_settings['twp']['avatar'], 'normal' ) ?>><?php _e( 'Normal - 48px by 48px', 'twitter-widget-pro' ); ?></option>
<option value="bigger"<?php selected( $this->_settings['twp']['avatar'], 'bigger' ) ?>><?php _e( 'Bigger - 73px by 73px', 'twitter-widget-pro' ); ?></option>
<option value="original"<?php selected( $this->_settings['twp']['avatar'], 'original' ) ?>><?php _e( 'Original', 'twitter-widget-pro' ); ?></option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="twp_errmsg"><?php _e( 'What to display when Twitter is down ( optional ):', 'twitter-widget-pro' ); ?></label>
</th>
<td>
<input id="twp_errmsg" name="twp[errmsg]" type="text" class="regular-text code" value="<?php esc_attr_e( $this->_settings['twp']['errmsg'] ); ?>" size="40" />
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="twp_showts"><?php _e( 'Show date/time of Tweet ( rather than 2 ____ ago ):', 'twitter-widget-pro' ); ?></label>
</th>
<td>
<select id="twp_showts" name="twp[showts]">
<option value="0" <?php selected( $this->_settings['twp']['showts'], '0' ); ?>><?php _e( 'Always', 'twitter-widget-pro' );?></option>
<option value="3600" <?php selected( $this->_settings['twp']['showts'], '3600' ); ?>><?php _e( 'If over an hour old', 'twitter-widget-pro' );?></option>
<option value="86400" <?php selected( $this->_settings['twp']['showts'], '86400' ); ?>><?php _e( 'If over a day old', 'twitter-widget-pro' );?></option>
<option value="604800" <?php selected( $this->_settings['twp']['showts'], '604800' ); ?>><?php _e( 'If over a week old', 'twitter-widget-pro' );?></option>
<option value="2592000" <?php selected( $this->_settings['twp']['showts'], '2592000' ); ?>><?php _e( 'If over a month old', 'twitter-widget-pro' );?></option>
<option value="31536000" <?php selected( $this->_settings['twp']['showts'], '31536000' ); ?>><?php _e( 'If over a year old', 'twitter-widget-pro' );?></option>
<option value="-1" <?php selected( $this->_settings['twp']['showts'], '-1' ); ?>><?php _e( 'Never', 'twitter-widget-pro' );?></option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="twp_dateFormat"><?php echo sprintf( __( 'Format to display the date in, uses <a href="%s">PHP date()</a> format:', 'twitter-widget-pro' ), 'http://php.net/date' ); ?></label>
</th>
<td>
<input id="twp_dateFormat" name="twp[dateFormat]" type="text" class="regular-text code" value="<?php esc_attr_e( $this->_settings['twp']['dateFormat'] ); ?>" size="40" />
</td>
</tr>
<tr valign="top">
<th scope="row">
<?php _e( "Other Setting:", 'twitter-widget-pro' );?>
</th>
<td>
<input type="hidden" value="false" name="twp[showretweets]" />
<input class="checkbox" type="checkbox" value="true" id="twp_showretweets" name="twp[showretweets]"<?php checked( $this->_settings['twp']['showretweets'], 'true' ); ?> />
<label for="twp_showretweets"><?php _e( 'Include retweets', 'twitter-widget-pro' ); ?></label>
<br />
<input type="hidden" value="false" name="twp[hidereplies]" />
<input class="checkbox" type="checkbox" value="true" id="twp_hidereplies" name="twp[hidereplies]"<?php checked( $this->_settings['twp']['hidereplies'], 'true' ); ?> />
<label for="twp_hidereplies"><?php _e( 'Hide @replies', 'twitter-widget-pro' ); ?></label>
<br />
<input type="hidden" value="false" name="twp[hidefrom]" />
<input class="checkbox" type="checkbox" value="true" id="twp_hidefrom" name="twp[hidefrom]"<?php checked( $this->_settings['twp']['hidefrom'], 'true' ); ?> />
<label for="twp_hidefrom"><?php _e( 'Hide sending applications', 'twitter-widget-pro' ); ?></label>
<br />
<input type="hidden" value="false" name="twp[showintents]" />
<input class="checkbox" type="checkbox" value="true" id="twp_showintents" name="twp[showintents]"<?php checked( $this->_settings['twp']['showintents'], 'true' ); ?> />
<label for="twp_showintents"><?php _e( 'Show Tweet Intents (reply, retweet, favorite)', 'twitter-widget-pro' ); ?></label>
<br />
<input type="hidden" value="false" name="twp[showfollow]" />
<input class="checkbox" type="checkbox" value="true" id="twp_showfollow" name="twp[showfollow]"<?php checked( $this->_settings['twp']['showfollow'], 'true' ); ?> />
<label for="twp_showfollow"><?php _e( 'Show Follow Link', 'twitter-widget-pro' ); ?></label>
<br />
<input type="hidden" value="false" name="twp[targetBlank]" />
<input class="checkbox" type="checkbox" value="true" id="twp_targetBlank" name="twp[targetBlank]"<?php checked( $this->_settings['twp']['targetBlank'], 'true' ); ?> />
<label for="twp_targetBlank"><?php _e( 'Open links in a new window', 'twitter-widget-pro' ); ?></label>
</td>
</tr>
</table>
<?php
}
/**
* Replace @username with a link to that twitter user
*
* @param string $text - Tweet text
* @return string - Tweet text with @replies linked
*/
public function linkTwitterUsers( $text ) {
$text = preg_replace_callback('/(^|\s)@(\w+)/i', array($this, '_linkTwitterUsersCallback'), $text);
return $text;
}
private function _linkTwitterUsersCallback( $matches ) {
$linkAttrs = array(
'href' => 'http://twitter.com/' . urlencode( $matches[2] ),
'class' => 'twitter-user'
);
return $matches[1] . $this->_buildLink( '@'.$matches[2], $linkAttrs );
}
/**
* Replace #hashtag with a link to twitter.com for that hashtag
*
* @param string $text - Tweet text
* @return string - Tweet text with #hashtags linked
*/
public function linkHashtags( $text ) {
$text = preg_replace_callback('/(^|\s)(#[\w\x{00C0}-\x{00D6}\x{00D8}-\x{00F6}\x{00F8}-\x{00FF}]+)/iu', array($this, '_linkHashtagsCallback'), $text);
return $text;
}
/**
* Replace #hashtag with a link to twitter.com for that hashtag
*
* @param array $matches - Tweet text
* @return string - Tweet text with #hashtags linked
*/
private function _linkHashtagsCallback( $matches ) {
$linkAttrs = array(
'href' => 'http://twitter.com/search?q=' . urlencode( $matches[2] ),
'class' => 'twitter-hashtag'
);
return $matches[1] . $this->_buildLink( $matches[2], $linkAttrs );
}
/**
* Turn URLs into links
*
* @param string $text - Tweet text
* @return string - Tweet text with URLs repalced with links
*/
public function linkUrls( $text ) {
$text = " {$text} "; // Pad with whitespace to simplify the regexes
$url_clickable = '~
([\\s(<.,;:!?]) # 1: Leading whitespace, or punctuation
( # 2: URL
[\\w]{1,20}+:// # Scheme and hier-part prefix
(?=\S{1,2000}\s) # Limit to URLs less than about 2000 characters long
[\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]*+ # Non-punctuation URL character
(?: # Unroll the Loop: Only allow puctuation URL character if followed by a non-punctuation URL character
[\'.,;:!?)] # Punctuation URL character
[\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]++ # Non-punctuation URL character
)*
)
(\)?) # 3: Trailing closing parenthesis (for parethesis balancing post processing)
~xS';
// The regex is a non-anchored pattern and does not have a single fixed starting character.
// Tell PCRE to spend more time optimizing since, when used on a page load, it will probably be used several times.
$text = preg_replace_callback( $url_clickable, array($this, '_make_url_clickable_cb'), $text );
$text = preg_replace_callback( '#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', array($this, '_make_web_ftp_clickable_cb' ), $text );
$text = preg_replace_callback( '#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', array($this, '_make_email_clickable_cb' ), $text );
$text = substr( $text, 1, -1 ); // Remove our whitespace padding.
return $text;
}
function _make_web_ftp_clickable_cb($matches) {
$ret = '';
$dest = $matches[2];
$dest = 'http://' . $dest;
$dest = esc_url($dest);
if ( empty($dest) )
return $matches[0];
// removed trailing [.,;:)] from URL
if ( in_array( substr($dest, -1), array('.', ',', ';', ':', ')') ) === true ) {
$ret = substr($dest, -1);
$dest = substr($dest, 0, strlen($dest)-1);
}
$linkAttrs = array(
'href' => $dest
);
return $matches[1] . $this->_buildLink( $dest, $linkAttrs ) . $ret;
}
private function _make_email_clickable_cb( $matches ) {
$email = $matches[2] . '@' . $matches[3];
$linkAttrs = array(
'href' => 'mailto:' . $email
);
return $matches[1] . $this->_buildLink( $email, $linkAttrs );
}
private function _make_url_clickable_cb ( $matches ) {
$linkAttrs = array(
'href' => $matches[2]
);
return $matches[1] . $this->_buildLink( $matches[2], $linkAttrs );
}
private function _notEmpty( $v ) {
return !( empty( $v ) );
}
private function _buildLink( $text, $attributes = array(), $noFilter = false ) {
$attributes = array_filter( wp_parse_args( $attributes ), array( $this, '_notEmpty' ) );
$attributes = apply_filters( 'widget_twitter_link_attributes', $attributes );
$attributes = wp_parse_args( $attributes );
$text = apply_filters( 'widget_twitter_link_text', $text );
$noFilter = apply_filters( 'widget_twitter_link_nofilter', $noFilter );
$link = '<a';
foreach ( $attributes as $name => $value ) {
$link .= ' ' . esc_attr( $name ) . '="' . esc_attr( $value ) . '"';
}
$link .= '>';
if ( $noFilter )
$link .= $text;
else
$link .= esc_html( $text );
$link .= '</a>';
return $link;
}
public function register() {
// Fix conflict with Jetpack by disabling their Twitter widget
unregister_widget( 'Wickett_Twitter_Widget' );
register_widget( 'WP_Widget_Twitter_Pro' );
}
public function targetBlank( $attributes ) {
$attributes['target'] = '_blank';
return $attributes;
}
public function display( $args ) {
$args = wp_parse_args( $args );
if ( 'true' == $args['targetBlank'] )
add_filter( 'widget_twitter_link_attributes', array( $this, 'targetBlank' ) );
// Validate our options
$args['items'] = (int) $args['items'];
if ( $args['items'] < 1 || 20 < $args['items'] )
$args['items'] = 10;
if ( !isset( $args['showts'] ) )
$args['showts'] = 86400;
$tweets = $this->_getTweets( $args );
if ( false === $tweets )
return '';
$widgetContent = $args['before_widget'] . '<div>';
if ( empty( $args['title'] ) )
$args['title'] = sprintf( __( 'Twitter: %s', 'twitter-widget-pro' ), $args['username'] );
$args['title'] = apply_filters( 'twitter-widget-title', $args['title'], $args );
$args['title'] = "<span class='twitterwidget twitterwidget-title'>{$args['title']}</span>";
$widgetContent .= $args['before_title'] . $args['title'] . $args['after_title'];
if ( !empty( $tweets[0] ) && is_object( $tweets[0] ) && !empty( $args['avatar'] ) ) {
$widgetContent .= '<div class="twitter-avatar">';
$widgetContent .= $this->_getProfileImage( $tweets[0]->user, $args );
$widgetContent .= '</div>';
}
$widgetContent .= '<ul>';
if ( ! is_array( $tweets ) || count( $tweets ) == 0 ) {
$widgetContent .= '<li class="wpTwitterWidgetEmpty">' . __( 'No Tweets Available', 'twitter-widget-pro' ) . '</li>';
} else {
$count = 0;
foreach ( $tweets as $tweet ) {
// Set our "ago" string which converts the date to "# ___(s) ago"
$tweet->ago = $this->_timeSince( strtotime( $tweet->created_at ), $args['showts'], $args['dateFormat'] );
$entryContent = apply_filters( 'widget_twitter_content', $tweet->text, $tweet );
$widgetContent .= '<li>';
$widgetContent .= "<span class='entry-content'>{$entryContent}</span>";
$widgetContent .= " <span class='entry-meta'>";
$widgetContent .= "<span class='time-meta'>";
$linkAttrs = array(
'href' => "http://twitter.com/{$tweet->user->screen_name}/statuses/{$tweet->id_str}"
);
$widgetContent .= $this->_buildLink( $tweet->ago, $linkAttrs );
$widgetContent .= '</span>';
if ( 'true' != $args['hidefrom'] ) {
$from = sprintf( __( 'from %s', 'twitter-widget-pro' ), str_replace( '&', '&', $tweet->source ) );
$widgetContent .= " <span class='from-meta'>{$from}</span>";
}
if ( !empty( $tweet->in_reply_to_screen_name ) ) {
$rtLinkText = sprintf( __( 'in reply to %s', 'twitter-widget-pro' ), $tweet->in_reply_to_screen_name );
$widgetContent .= ' <span class="in-reply-to-meta">';
$linkAttrs = array(
'href' => "http://twitter.com/{$tweet->in_reply_to_screen_name}/statuses/{$tweet->in_reply_to_status_id_str}",
'class' => 'reply-to'
);
$widgetContent .= $this->_buildLink( $rtLinkText, $linkAttrs );
$widgetContent .= '</span>';
}
$widgetContent .= '</span>';
if ( 'true' == $args['showintents'] ) {
$widgetContent .= ' <span class="intent-meta">';
$lang = $this->_getTwitterLang();
if ( !empty( $lang ) )
$linkAttrs['data-lang'] = $lang;
$linkText = __( 'Reply', 'twitter-widget-pro' );
$linkAttrs['href'] = "http://twitter.com/intent/tweet?in_reply_to={$tweet->id_str}";
$linkAttrs['class'] = 'in-reply-to';
$linkAttrs['title'] = $linkText;
$widgetContent .= $this->_buildLink( $linkText, $linkAttrs );
$linkText = __( 'Retweet', 'twitter-widget-pro' );
$linkAttrs['href'] = "http://twitter.com/intent/retweet?tweet_id={$tweet->id_str}";
$linkAttrs['class'] = 'retweet';
$linkAttrs['title'] = $linkText;
$widgetContent .= $this->_buildLink( $linkText, $linkAttrs );
$linkText = __( 'Favorite', 'twitter-widget-pro' );
$linkAttrs['href'] = "http://twitter.com/intent/favorite?tweet_id={$tweet->id_str}";
$linkAttrs['class'] = 'favorite';
$linkAttrs['title'] = $linkText;
$widgetContent .= $this->_buildLink( $linkText, $linkAttrs );
$widgetContent .= '</span>';
}
$widgetContent .= '</li>';
if ( ++$count >= $args['items'] )
break;
}
}
$widgetContent .= '</ul>';
if ( 'true' == $args['showfollow'] && ! empty( $args['username'] ) ) {
$widgetContent .= '<div class="follow-button">';
$linkText = "@{$args['username']}";
$linkAttrs = array(
'href' => "http://twitter.com/{$args['username']}",
'class' => 'twitter-follow-button',
'title' => sprintf( __( 'Follow %s', 'twitter-widget-pro' ), "@{$args['username']}" ),
);
$lang = $this->_getTwitterLang();
if ( !empty( $lang ) )
$linkAttrs['data-lang'] = $lang;
$widgetContent .= $this->_buildLink( $linkText, $linkAttrs );
$widgetContent .= '</div>';
}
$widgetContent .= '</div>' . $args['after_widget'];
if ( 'true' == $args['showintents'] || 'true' == $args['showfollow'] ) {
$script = 'http://platform.twitter.com/widgets.js';
if ( is_ssl() )
$script = str_replace( 'http://', 'https://', $script );
wp_enqueue_script( 'twitter-widgets', $script, array(), '1.0.0', true );
if ( ! function_exists( '_wp_footer_scripts' ) ) {
// This means we can't just enqueue our script (fixes in WP 3.3)
add_action( 'wp_footer', array( $this, 'add_twitter_js' ) );
}
}
return $widgetContent;
}
private function _getTwitterLang() {
$valid_langs = array(
'en', // English
'it', // Italian
'es', // Spanish
'fr', // French
'ko', // Korean
'ja', // Japanese
);
$locale = get_locale();
$lang = strtolower( substr( get_locale(), 0, 2 ) );
if ( in_array( $lang, $valid_langs ) )
return $lang;
return false;
}
public function add_twitter_js() {
wp_print_scripts( 'twitter-widgets' );
}
/**
* Gets tweets, from cache if possible
*
* @param array $widgetOptions - options needed to get feeds
* @return array - Array of objects
*/
private function _getTweets( $widgetOptions ) {
$key = 'twp_' . md5( maybe_serialize( $this->_get_feed_request_settings( $widgetOptions ) ) );
return tlc_transient( $key )
->expires_in( 300 ) // cache for 5 minutes
->extend_on_fail( 120 ) // On a failed call, don't try again for 2 minutes
->updates_with( array( $this, 'parseFeed' ), array( $widgetOptions ) )
->get();
}
/**
* Pulls the JSON feed from Twitter and returns an array of objects
*
* @param array $widgetOptions - settings needed to get feed url, etc
* @return array
*/
public function parseFeed( $widgetOptions ) {
$parameters = $this->_get_feed_request_settings( $widgetOptions );
$response = array();
if ( ! empty( $parameters['screen_name'] ) ) {
if ( empty( $this->_settings['twp-authed-users'][strtolower( $parameters['screen_name'] )] ) ) {
if ( empty( $widgetOptions['errmsg'] ) )
$widgetOptions['errmsg'] = __( 'Account needs to be authorized', 'twitter-widget-pro' );
} else {
$this->_wp_twitter_oauth->set_token( $this->_settings['twp-authed-users'][strtolower( $parameters['screen_name'] )] );
$response = $this->_wp_twitter_oauth->send_authed_request( 'statuses/user_timeline', 'GET', $parameters );
if ( ! is_wp_error( $response ) )
return $response;
}
} elseif ( ! empty( $parameters['list_id'] ) ) {
$list_info = explode( '::', $widgetOptions['list'] );
$user = array_shift( $list_info );
$this->_wp_twitter_oauth->set_token( $this->_settings['twp-authed-users'][strtolower( $user )] );
$response = $this->_wp_twitter_oauth->send_authed_request( 'lists/statuses', 'GET', $parameters );
if ( ! is_wp_error( $response ) )
return $response;
}
if ( empty( $widgetOptions['errmsg'] ) )
$widgetOptions['errmsg'] = __( 'Invalid Twitter Response.', 'twitter-widget-pro' );
do_action( 'widget_twitter_parsefeed_error', $response, $parameters, $widgetOptions );
throw new Exception( $widgetOptions['errmsg'] );
}
/**
* Gets the parameters for the desired feed.
*
* @param array $widgetOptions - settings needed such as username, feet type, etc
* @return array - Parameters ready to pass to a Twitter request
*/
private function _get_feed_request_settings( $widgetOptions ) {
/**
* user_id
* screen_name *
* since_id
* count
* max_id
* page
* trim_user
* include_rts *
* include_entities
* exclude_replies *
* contributor_details