-
Notifications
You must be signed in to change notification settings - Fork 1
/
twitterlitte.plugin.php
212 lines (197 loc) · 5.89 KB
/
twitterlitte.plugin.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
<?php
/**
* Twitter Litte Plugin
*
**/
class TwitterLitte extends Plugin
{
/**
* On plugin init, add the template included with this plugin to the available templates in the theme
*/
public function action_init( )
{
$this->load_text_domain( 'twitterlitte' );
$this->add_template( 'block.twitterlitte', dirname( __FILE__ ) . '/block.twitterlitte.php' );
}
public function filter_block_list( $block_list )
{
$block_list[ 'twitterlitte' ] = _t( 'Twitter Litte', 'twitterlitte' );
return $block_list;
}
public function action_block_form_twitterlitte( $form, $block )
{
$form->append( 'text', 'username', $block, _t( 'Twitter Username', 'twitterlitte' ) );
$form->username->add_validator( array( $this, 'validate_username' ) );
$form->username->add_validator( 'validate_required' );
// How many tweets to show?
$form->append( 'text', 'limit', $block, _t( '№ of Tweets', 'twitterlitte' ) );
$form->limit->add_validator( array( $this, 'validate_uint' ) );
// Match specified string
$form->append( 'text', 'search', $block, _t( 'Filter by', 'twitterlitte' ) );
// Cache
$form->append( 'text', 'cache', $block, _t( 'Cache Expiry (in seconds)', 'twitterlitte' ) );
$form->cache->add_validator( array( $this, 'validate_uint' ) );
}
public function validate_username( $username )
{
if ( preg_match( '/[A-Za-z0-9-_]+/', $username ) === false ) {
return array( _t( 'Your Twitter username is not valid.', 'twitterlitte' ) );
}
return array( );
}
public function validate_uint( $value )
{
if (!ctype_digit( $value ) || strstr( $value, '.' ) || $value < 0 ) {
return array( _t( 'This field must be positive integer.', 'twitterlitte' ) );
}
return array( );
}
private static function build_api_url( $block )
{
if ( $block->search != '' ) {
$url = 'http://search.twitter.com/search.json?';
$url .= http_build_query( array(
'from' => $block->username,
'phrase' => $block->search,
'rpp' => $block->limit
), '', '&' );
}
else {
$url = 'http://twitter.com/statuses/user_timeline/' . $block->username . '.json';
}
return $url;
}
private static function get_external_content( $url )
{
// Get JSON content via Twitter API
$call = new RemoteRequest( $url );
$call->set_timeout( 5 );
$result = $call->execute( );
if ( Error::is_error( $result ) ) {
throw Error::raise( _t( 'Unable to contact Twitter.', 'twitterlitte' ) );
}
return $call->get_response_body( );
}
private static function parse_json( $block, $json )
{
// Decode JSON
$obj = json_decode( $json );
if ( isset( $obj->query ) ) {
$obj = $obj->results;
// Strip user designate tags
foreach ( $obj as &$o ) {
$o->text = str_replace( $block->search, '', $o->text );
}
}
if (!is_array( $obj ) ) {
// Response is not JSON
throw Error::raise( _t( 'Response is not correct, Twitter server may be down or API is changed.', 'twitterlitte' ) );
}
$serial = serialize( $obj );
// Convert stdClass to TwitterLitteTweet and TwitterLitteUser
$serial = str_replace( 's:4:"user";O:8:"stdClass":', 's:4:"user";O:16:"TwitterLitteUser":', $serial );
$serial = str_replace( 'O:8:"stdClass":', 'O:17:"TwitterLitteTweet":', $serial );
$tweets = unserialize( $serial );
return $tweets;
}
public function action_block_content_twitterlitte( $block, $theme )
{
$cache_name = 'twitterlitte_' . md5( serialize( array(
$block->username,
$block->limit,
$block->search
) ) );
if ( $block->username != '' ) {
if ( Cache::has( $cache_name ) ) {
$block->tweets = Cache::get( $cache_name );
}
else {
try {
$url = self::build_api_url( $block );
$json = self::get_external_content( $url );
$tweets = self::parse_json( $block, $json );
// Pass $tweets to $theme
$block->tweets = array_slice( $tweets, 0, $block->limit );
// Do cache
Cache::set( $cache_name, $block->tweets, $block->cache );
}
catch ( Exception $e ) {
$block->tweets = $e->getMessage( );
}
}
}
else {
$block->tweets = _t( 'Please set your username in the Twitter Litte block config.', 'twitterlitte' );
}
}
}
class TwitterLitteTweet
{
function __get( $name )
{
switch ( $name ) {
case 'url':
return $this->user->profile_url . '/status/' . $this->id_str;
break;
case 'message_out':
$message = $this->text;
// Linkify URIs
$message = preg_replace(
'#(?<uri>' .
'(?<scheme>[a-z][a-z0-9-.+]+)://' .
'(?:(?<username>[a-z0-9-_.!~*\'()%]+)(?::(?<password>[a-z0-9-_.!~*\'()%;&=+$,]+))?@)?' .
'(?<host>(?:(?:(?:[a-z0-9]+|[a-z0-9][a-z0-9\-]+[a-z0-9]+)\.)+(?:[a-z]|[a-z][a-z0-9\-]+[a-z0-9])+|[0-9]{1,3}(?:\.[0-9]{1,3}){3})\.?)' .
'(?::(?<port>\d{2,5}))?' .
'(?<path>(?:/[a-z0-9\'\-!$%&()*,.:;@_~+=]+|[a-z0-9\'\-!$%&()*,.:;?@_~+=][a-z0-9\'\-!$%&()*,./:;?@_~+=]+)+)?' .
'(?<query>\?[a-z0-9\'\-!$%&()*,./:;?@[]_{}~+=]+)?' .
'(?<fragment>\#[a-z0-9\'\-!$%&()*,./:;?@_~+=]+)?' .
')#i', '<a href="${1}">${5}${7}</a>', $message );
// Linkify Users
$message = preg_replace( '|\B@([a-z0-9_]+)\b|i', '@<a href="http://twitter.com/${1}">${1}</a>', $message );
return $message;
break;
case 'user': // Append 'user' when search
if ( isset( $this->user ) ) {
return $this->user;
}
else {
return new TwitterLitteUser( array(
'profile_image_url' => $this->profile_image_url,
'screen_name' => $this->from_user,
'id' => $this->from_user_id
) );
}
break;
default:
return NULL;
break;
}
}
}
class TwitterLitteUser
{
function __construct( $params = NULL )
{
if ( is_array( $params ) ) {
foreach ( $params as $k => $v ) {
$this->$k = $v;
}
}
}
public static function profile_url( $username )
{
return 'http://twitter.com/' . $username;
}
function __get( $name )
{
SWITCH ( $name ) {
case 'profile_url':
return self::profile_url( $this->screen_name );
break;
default:
return NULL;
break;
}
}
}
?>