Repository navigation
Expand file tree
/
Copy pathtwitter-tracker.php
More file actions
434 lines (364 loc) · 14 KB
/
Copy pathtwitter-tracker.php
File metadata and controls
434 lines (364 loc) · 14 KB
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
<?php
/*
Plugin Name: Twitter Tracker
Plugin URI: http://wordpress.org/extend/plugins/twitter-tracker/
Description: Tracks the search results on Twitter search or Twitter profile in a sidebar widget.
Author: Simon Wheatley (Code for the People)
Version: 3.3.6
Author URI: http://codeforthepeople.com/
*/
// http://twitter.com/search.atom?q=wordcampuk
/* Copyright 2013 Code For The People
_____________
/ ____ \
_____/ \ \ \
/\ \ \___\ \
/ \ \ \
/ / / _______\
/ / / \ /
/ / / \ /
\ \ \ _____ ___\ /
\ \ /\ \ / \
\ \ / \____\/ _____\
\ \/ / / / \
\ /____/ /___\
\ /
\______________________/
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
--------------------------------------------------------------------------
Emoji conversion library
https://github.com/iamcal/php-emoji/
By Cal Henderson cal@iamcal.com
Parser rewrite based on a fork by 杜志刚
This work is licensed under the GPL v3
Emoji images MIT licensed: https://github.com/github/gemoji/blob/master/LICENSE
*/
require_once( dirname (__FILE__) . '/plugin.php' );
require_once( dirname (__FILE__) . '/class-TwitterTracker_Widget.php' );
require_once( dirname (__FILE__) . '/class-TwitterTracker_Profile_Widget.php' );
require_once( dirname (__FILE__) . '/class.twitter-authentication.php' );
/**
*
* @package default
* @author Simon Wheatley
**/
class TwitterTracker extends TwitterTracker_Plugin
{
public $widget;
public function __construct()
{
$this->register_plugin( 'twitter-tracker', __FILE__ );
if ( is_admin() ) {
$this->register_activation (__FILE__);
$this->add_action( 'save_post', 'process_metabox', null, 2 );
$this->add_action( 'add_meta_boxes' );
}
// Init
$this->register_plugin ( 'twitter-tracker', __FILE__ );
$this->add_action( 'init' );
$this->add_action( 'wp_enqueue_scripts', 'action_wp_enqueue_scripts' );
$this->add_filter( 'tt_allowed_post_types', 'warn_tt_allowed_post_types' );
// register widget
add_action('widgets_init', create_function('', 'return register_widget( "TwitterTracker_Widget" );'));
add_action('widgets_init', create_function('', 'return register_widget( "TwitterTracker_Profile_Widget" );'));
}
// DOING IT WRONG
// ==============
/**
* Hooks the warn_tt_allowed_post_types filter to throw a
* doing it wrong warning if anyone has used the filter.
*
* @param mixed $pass_through A value to pass right through
* @return A value to pass right through
**/
function warn_tt_allowed_post_types( $pass_through ) {
remove_filter( 'tt_allowed_post_types', array( & $this, 'warn_tt_allowed_post_types' ) );
if ( has_filter( 'tt_allowed_post_types' ) )
_doing_it_wrong( 'tt_allowed_post_types', __( 'Twitter Tracker filter error: The tt_allowed_post_types filter has been deprecated and will be removed in a future version, please use tt_post_types_with_override instead.', 'twitter-tracker' ), '2.5' );
return $pass_through;
}
// HOOKS
// =====
public function activate() {
// Empty
}
/**
* Hooks the WP add_meta_boxes action to add the metaboxes to
* post_type editing screens.
*
* @return void
**/
function add_meta_boxes() {
// Allow other plugins to add to the allowed post types for this metabox
// First use the legacy filter name
$allowed_post_types = apply_filters( 'tt_allowed_post_types', array( 'page', 'post' ) );
// Now use the new, more sensible filter name
$allowed_post_types = apply_filters( 'tt_post_types_with_override', $allowed_post_types );
// This work around because the WO CIDIW client needs to upgrade WP,
// but we need to get this plugin working before they do… can be removed
// once everyone is up to v3.1.0 as required by the plugin.
if ( function_exists( 'get_current_screen' ) ) {
$screen = get_current_screen();
if ( ! in_array( $screen->post_type, $allowed_post_types ) )
return;
}
$this->add_meta_box( 'twitter_tracker', __( 'Twitter Tracker', 'twitter-tracker' ), 'metabox', 'post', 'normal', 'default' );
$this->add_meta_box( 'twitter_tracker', __( 'Twitter Tracker', 'twitter-tracker' ), 'metabox', 'page', 'normal', 'default' );
}
/**
* Hooks the WP wp_enqueue_scripts action
*
* @action wp_enqueue_scripts
*
* @return void
* @author Simon Wheatley
**/
function action_wp_enqueue_scripts() {
if ( 'convert' == get_option( 'tt_convert_emoji', 'hide' ) )
wp_enqueue_style( 'tt_emoji', $this->url() . '/emoji/emoji.css' , null, $this->filemtime( '/emoji/emoji.css' ) );
}
/**
* Callback function providing the HTML for the metabox
*
* @return void
* @author Simon Wheatley
**/
public function metabox() {
global $post;
$vars = array();
$vars[ 'query' ] = get_post_meta( $post->ID, '_tt_query', true );
$vars[ 'username' ] = get_post_meta( $post->ID, '_tt_username', true );
$this->render_admin( 'metabox', $vars );
}
public function process_metabox( $post_id, $post ) {
// Are we being asked to do anything?
$do_something = (bool) @ $_POST[ '_tt_query_nonce' ];
if ( ! $do_something ) return;
// Allow other plugins to add to the allowed post types
// First use the legacy filter name
$allowed_post_types = apply_filters( 'tt_allowed_post_types', array( 'page', 'post' ) );
// Now use the new, more sensible filter name
$allowed_post_types = apply_filters( 'tt_post_types_with_override', $allowed_post_types );
// Don't bother doing this on revisions and wot not
if ( ! in_array( $post->post_type, $allowed_post_types ) )
return;
// Are we authorised to do anything?
check_admin_referer( 'tt_query', '_tt_query_nonce' );
// OK. We are good to go.
$query = @ $_POST[ 'tt_query' ];
update_post_meta( $post_id, '_tt_query', $query );
$username = @ $_POST[ 'tt_username' ];
update_post_meta( $post_id, '_tt_username', $username );
}
public function init()
{
// Slightly cheeky, but change the cache age of Magpie from 60 minutes to 15 minutes
// That's still plenty of caching IMHO :)
if ( ! defined( 'MAGPIE_CACHE_AGE' ) )
define( 'MAGPIE_CACHE_AGE', 60 * 15 ); // Fifteen of your Earth minutes
}
public function show( $instance = array() ) {
// Backwards compatibility
return $this->show_search( $instance );
}
public function show_search( $instance = array() )
{
$defaults = array (
'convert_emoji' => 'hide',
'hide_replies' => false,
'include_retweets' => false,
'mandatory_hash' => '',
'max_tweets' => 30,
'html_after' => '',
'preamble' => '',
);
$instance = wp_parse_args( $instance, $defaults );
extract( $instance );
// Allow the local custom field to overwrite the widget's query
if ( is_singular() && is_single() && $post_id = get_queried_object_id() )
if ( $local_query = trim( get_post_meta( $post_id, '_tt_query', true ) ) )
$twitter_search = $local_query;
// Let the user know if there's no search query
$twitter_search = trim( $twitter_search );
if ( empty( $twitter_search ) ) {
$vars = array(
'msg' => __( 'For this Twitter Tracker search widget to work you need to set at least a Twitter Search in the widget settings.', 'twitter-tracker' ),
'additional_error_class' => '',
'strong' => true,
);
$this->render( 'widget-error', $vars );
return;
}
// Let the user know if there's no auth
if ( ! TT_Twitter_Authentication::init()->is_authenticated() ) {
$vars = array(
'msg' => __( 'For this Twitter Tracker search widget to work you need to authorise with Twitter in "Dashboard" -> "Settings" -> "Twitter Tracker Auth".', 'twitter-tracker' ),
'additional_error_class' => '',
'strong' => true,
);
$this->render( 'widget-error', $vars );
return;
}
require_once( 'class.oauth.php' );
require_once( 'class.wp-twitter-oauth.php' );
require_once( 'class.response.php' );
require_once( 'class.twitter-service.php' );
$args = array(
'params' => array(
'count' => max( ($max_tweets * 4), 200 ), // Get *lots* as we have to throw some away later
'q' => $twitter_search,
),
);
$transient_key = 'tt_profile-' . md5( serialize( $instance ) . serialize( $args ) );
if ( $output = get_transient( $transient_key ) ) {
echo $output;
return;
}
$service = new TT_Service;
$response = $service->request_search( $args );
if ( is_wp_error( $response ) ) {
error_log( "Twitter Tracker response error: " . print_r( $response, true ) );
return;
}
if ( $hide_replies )
$response->remove_replies();
if ( ! $include_retweets )
$response->remove_retweets();
$response->convert_emoji( $convert_emoji );
$mandatory_hash = strtolower( trim( ltrim( $mandatory_hash, '#' ) ) );
if ( $mandatory_hash )
$response->remove_without_hash( $mandatory_hash );
$vars = array(
'tweets' => array_slice( $response->items, 0, $max_tweets ),
'preamble' => $preamble,
'html_after' => $html_after,
);
$vars[ 'datef' ] = _x( 'M j, Y @ G:i', 'Publish box date format', 'twitter-tracker' );
if ( ! $response->have_tweets() ) {
$vars[ 'msg' ] = apply_filters( 'tt_no_tweets', __( 'No tweets found.', 'twitter-tracker' ), $twitter_search, $instance );
$vars[ 'additional_error_class' ] = 'no-tweets';
$vars[ 'strong' ] = false;
$output = $this->capture( 'widget-error', $vars );
} else {
$output = $this->capture( 'widget-contents', $vars );
}
echo PHP_EOL . "<!-- Regenerating cache $transient_key at " . current_time( 'mysql' ) . " -->" . PHP_EOL;
echo $output;
$output = PHP_EOL . "<!-- Retrieved from $transient_key, cached at " . current_time( 'mysql' ) . " -->" . PHP_EOL . $output;
set_transient( $transient_key, $output, apply_filters( 'tt_cache_expiry', 300, $transient_key, $args ) );
}
public function show_profile( $instance = array() )
{
$defaults = array (
'convert_emoji' => 'hide',
'hide_replies' => false,
'include_retweets' => false,
'mandatory_hash' => '',
'max_tweets' => 3,
'html_after' => '',
'preamble' => '',
);
$instance = wp_parse_args( $instance, $defaults );
extract( $instance );
// Allow the local custom field to overwrite the widget's query, but
// only on single post (of any type)
if ( is_singular() && $post_id = get_queried_object_id() )
if ( $local_username = trim( get_post_meta( $post_id, '_tt_username', true ) ) )
$username = $local_username;
// Let the user know if there's no search query
$username = trim( $username );
if ( empty( $username ) ) {
$vars = array(
'msg' => __( 'For this Twitter Tracker profile widget to work you need to set at least a Twitter screenname (username) in the widget settings.', 'twitter-tracker' ),
'additional_error_class' => '',
'strong' => true,
);
$this->render( 'widget-error', $vars );
return;
}
// Let the user know if there's no auth
if ( ! TT_Twitter_Authentication::init()->is_authenticated() ) {
$vars = array(
'msg' => __( 'For this Twitter Tracker profile widget to work you need to authorise with Twitter in "Dashboard" -> "Settings" -> "Twitter Tracker Auth".', 'twitter-tracker' ),
'additional_error_class' => '',
'strong' => true,
);
$this->render( 'widget-error', $vars );
return;
}
require_once( 'class.oauth.php' );
require_once( 'class.wp-twitter-oauth.php' );
require_once( 'class.response.php' );
require_once( 'class.twitter-service.php' );
$args = array(
'count' => max( ($max_tweets * 4), 200 ), // Get *lots* as we have to throw some away later
);
$transient_key = 'tt_search-' . md5( serialize( $instance ) . $username . serialize( $args ) );
if ( $output = get_transient( $transient_key ) ) {
echo $output;
return;
}
$service = new TT_Service;
$response = $service->request_user_timeline( $username, $args );
if ( is_wp_error( $response ) ) {
error_log( "Twitter Tracker response error: " . print_r( $response, true ) );
return;
}
if ( $hide_replies )
$response->remove_replies();
if ( ! $include_retweets )
$response->remove_retweets();
$response->convert_emoji();
$mandatory_hash = strtolower( trim( ltrim( $mandatory_hash, '#' ) ) );
if ( $mandatory_hash )
$response->remove_without_hash( $mandatory_hash );
// @TODO Setup a method for the default vars needed
$vars = array(
'tweets' => array_slice( $response->items, 0, $max_tweets ),
'preamble' => $preamble,
'html_after' => $html_after,
);
$vars[ 'datef' ] = _x( 'M j, Y @ G:i', 'Publish box date format', 'twitter-tracker' );
$output = $this->capture( 'widget-contents', $vars );
echo PHP_EOL . "<!-- Regenerating cache $transient_key at " . current_time( 'mysql' ) . " -->" . PHP_EOL;
echo $output;
$output = PHP_EOL . "<!-- Retrieved from $transient_key, cached at " . current_time( 'mysql' ) . " -->" . PHP_EOL . $output;
set_transient( $transient_key, $output, apply_filters( 'tt_cache_expiry', 300, $transient_key, $username, $args ) );
}
public static function & get()
{
static $instance;
if ( ! isset ( $instance ) ) {
$c = __CLASS__;
$instance = new $c;
}
return $instance;
}
}
function twitter_tracker( $instance )
{
$tracker = TwitterTracker::get();
$tracker->show_search( $instance );
}
function twitter_tracker_profile( $instance )
{
$tracker = TwitterTracker::get();
$tracker->show_profile( $instance );
}
/**
* Instantiate the plugin
*
* @global
**/
$GLOBALS[ 'TwitterTracker' ] = new TwitterTracker();
?>