Twitter Widget Pro - Version 1.1.0

Version Description

Download this release

Release Info

Developer aaroncampbell
Plugin Icon wp plugin Twitter Widget Pro
Version 1.1.0
Comparing to
See all releases

Code changes from version 1.0.0 to 1.1.0

Files changed (3) hide show
  1. json_decode.php +392 -0
  2. readme.txt +1 -1
  3. wp-twitter-widget.php +19 -7
json_decode.php ADDED
@@ -0,0 +1,392 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Zend_Json_Decoder
3
+ {
4
+ /**
5
+ * Parse tokens used to decode the JSON object. These are not
6
+ * for public consumption, they are just used internally to the
7
+ * class.
8
+ */
9
+ const EOF = 0;
10
+ const DATUM = 1;
11
+ const LBRACE = 2;
12
+ const LBRACKET = 3;
13
+ const RBRACE = 4;
14
+ const RBRACKET = 5;
15
+ const COMMA = 6;
16
+ const COLON = 7;
17
+
18
+ /**
19
+ * Use to maintain a "pointer" to the source being decoded
20
+ *
21
+ * @var string
22
+ */
23
+ protected $_source;
24
+
25
+ /**
26
+ * Caches the source length
27
+ *
28
+ * @var int
29
+ */
30
+ protected $_sourceLength;
31
+
32
+ /**
33
+ * The offset within the souce being decoded
34
+ *
35
+ * @var int
36
+ *
37
+ */
38
+ protected $_offset;
39
+
40
+ /**
41
+ * The current token being considered in the parser cycle
42
+ *
43
+ * @var int
44
+ */
45
+ protected $_token;
46
+
47
+ /**
48
+ * Constructor
49
+ *
50
+ * @param string $source String source to decode
51
+ * @param int $decodeType How objects should be decoded -- see
52
+ * {@link Zend_Json::TYPE_ARRAY} and {@link Zend_Json::TYPE_OBJECT} for
53
+ * valid values
54
+ * @return void
55
+ */
56
+ protected function __construct($source)
57
+ {
58
+ // Set defaults
59
+ $this->_source = $source;
60
+ $this->_sourceLength = strlen($source);
61
+ $this->_token = self::EOF;
62
+ $this->_offset = 0;
63
+
64
+ // Set pointer at first token
65
+ $this->_getNextToken();
66
+ }
67
+
68
+ /**
69
+ * Decode a JSON source string
70
+ *
71
+ * Decodes a JSON encoded string. The value returned will be one of the
72
+ * following:
73
+ * - integer
74
+ * - float
75
+ * - boolean
76
+ * - null
77
+ * - StdClass
78
+ * - array
79
+ * - array of one or more of the above types
80
+ *
81
+ * By default, decoded objects will be returned as associative arrays; to
82
+ * return a StdClass object instead, pass {@link Zend_Json::TYPE_OBJECT} to
83
+ * the $objectDecodeType parameter.
84
+ *
85
+ * @static
86
+ * @access public
87
+ * @param string $source String to be decoded
88
+ * @param int $objectDecodeType How objects should be decoded; should be
89
+ * either or {@link Zend_Json::TYPE_ARRAY} or
90
+ * {@link Zend_Json::TYPE_OBJECT}; defaults to TYPE_ARRAY
91
+ * @return mixed
92
+ * @throws Zend_Json_Exception
93
+ */
94
+ public static function decode($source = null)
95
+ {
96
+ if (null === $source || !is_string($source)) {
97
+ return false;
98
+ }
99
+
100
+ $decoder = new self($source);
101
+
102
+ return $decoder->_decodeValue();
103
+ }
104
+
105
+
106
+ /**
107
+ * Recursive driving rountine for supported toplevel tops
108
+ *
109
+ * @return mixed
110
+ */
111
+ protected function _decodeValue()
112
+ {
113
+ switch ($this->_token) {
114
+ case self::DATUM:
115
+ $result = $this->_tokenValue;
116
+ $this->_getNextToken();
117
+ return($result);
118
+ break;
119
+ case self::LBRACE:
120
+ return($this->_decodeObject());
121
+ break;
122
+ case self::LBRACKET:
123
+ return($this->_decodeArray());
124
+ break;
125
+ default:
126
+ return null;
127
+ break;
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Decodes an object of the form:
133
+ * { "attribute: value, "attribute2" : value,...}
134
+ *
135
+ * If ZJsonEnoder or ZJAjax was used to encode the original object
136
+ * then a special attribute called __className which specifies a class
137
+ * name that should wrap the data contained within the encoded source.
138
+ *
139
+ * Decodes to either an array or StdClass object, based on the value of
140
+ * {@link $_decodeType}. If invalid $_decodeType present, returns as an
141
+ * array.
142
+ *
143
+ * @return array|StdClass
144
+ */
145
+ protected function _decodeObject()
146
+ {
147
+ $result = new StdClass();
148
+ $members = array();
149
+ $tok = $this->_getNextToken();
150
+
151
+ while ($tok && $tok != self::RBRACE) {
152
+ if ($tok != self::DATUM || ! is_string($this->_tokenValue)) {
153
+ return $result;
154
+ }
155
+
156
+ $key = $this->_tokenValue;
157
+ $tok = $this->_getNextToken();
158
+
159
+ if ($tok != self::COLON) {
160
+ return $result;
161
+ }
162
+
163
+ $tok = $this->_getNextToken();
164
+ $members[$key] = $this->_decodeValue();
165
+ $tok = $this->_token;
166
+
167
+ if ($tok == self::RBRACE) {
168
+ break;
169
+ }
170
+
171
+ if ($tok != self::COMMA) {
172
+ return $result;
173
+ }
174
+
175
+ $tok = $this->_getNextToken();
176
+ }
177
+
178
+ // Create new StdClass and populate with $members
179
+ foreach ($members as $key => $value) {
180
+ $result->$key = $value;
181
+ }
182
+
183
+ $this->_getNextToken();
184
+ return $result;
185
+ }
186
+
187
+ /**
188
+ * Decodes a JSON array format:
189
+ * [element, element2,...,elementN]
190
+ *
191
+ * @return array
192
+ */
193
+ protected function _decodeArray()
194
+ {
195
+ $result = array();
196
+ $starttok = $tok = $this->_getNextToken(); // Move past the '['
197
+ $index = 0;
198
+
199
+ while ($tok && $tok != self::RBRACKET) {
200
+ $result[$index++] = $this->_decodeValue();
201
+
202
+ $tok = $this->_token;
203
+
204
+ if ($tok == self::RBRACKET || !$tok) {
205
+ break;
206
+ }
207
+
208
+ if ($tok != self::COMMA) {
209
+ return $result;
210
+ }
211
+
212
+ $tok = $this->_getNextToken();
213
+ }
214
+
215
+ $this->_getNextToken();
216
+ return($result);
217
+ }
218
+
219
+
220
+ /**
221
+ * Removes whitepsace characters from the source input
222
+ */
223
+ protected function _eatWhitespace()
224
+ {
225
+ if (preg_match(
226
+ '/([\t\b\f\n\r ])*/s',
227
+ $this->_source,
228
+ $matches,
229
+ PREG_OFFSET_CAPTURE,
230
+ $this->_offset)
231
+ && $matches[0][1] == $this->_offset)
232
+ {
233
+ $this->_offset += strlen($matches[0][0]);
234
+ }
235
+ }
236
+
237
+
238
+ /**
239
+ * Retrieves the next token from the source stream
240
+ *
241
+ * @return int Token constant value specified in class definition
242
+ */
243
+ protected function _getNextToken()
244
+ {
245
+ $this->_token = self::EOF;
246
+ $this->_tokenValue = null;
247
+ $this->_eatWhitespace();
248
+
249
+ if ($this->_offset >= $this->_sourceLength) {
250
+ return(self::EOF);
251
+ }
252
+
253
+ $str = $this->_source;
254
+ $str_length = $this->_sourceLength;
255
+ $i = $this->_offset;
256
+ $start = $i;
257
+
258
+ switch ($str{$i}) {
259
+ case '{':
260
+ $this->_token = self::LBRACE;
261
+ break;
262
+ case '}':
263
+ $this->_token = self::RBRACE;
264
+ break;
265
+ case '[':
266
+ $this->_token = self::LBRACKET;
267
+ break;
268
+ case ']':
269
+ $this->_token = self::RBRACKET;
270
+ break;
271
+ case ',':
272
+ $this->_token = self::COMMA;
273
+ break;
274
+ case ':':
275
+ $this->_token = self::COLON;
276
+ break;
277
+ case '"':
278
+ $result = '';
279
+ do {
280
+ $i++;
281
+ if ($i >= $str_length) {
282
+ break;
283
+ }
284
+
285
+ $chr = $str{$i};
286
+ if ($chr == '\\') {
287
+ $i++;
288
+ if ($i >= $str_length) {
289
+ break;
290
+ }
291
+ $chr = $str{$i};
292
+ switch ($chr) {
293
+ case '"' :
294
+ $result .= '"';
295
+ break;
296
+ case '\\':
297
+ $result .= '\\';
298
+ break;
299
+ case '/' :
300
+ $result .= '/';
301
+ break;
302
+ case 'b' :
303
+ $result .= chr(8);
304
+ break;
305
+ case 'f' :
306
+ $result .= chr(12);
307
+ break;
308
+ case 'n' :
309
+ $result .= chr(10);
310
+ break;
311
+ case 'r' :
312
+ $result .= chr(13);
313
+ break;
314
+ case 't' :
315
+ $result .= chr(9);
316
+ break;
317
+ case '\'' :
318
+ $result .= '\'';
319
+ break;
320
+ default:
321
+ throw new Zend_Json_Exception("Illegal escape "
322
+ . "sequence '" . $chr . "'");
323
+ }
324
+ } elseif ($chr == '"') {
325
+ break;
326
+ } else {
327
+ $result .= $chr;
328
+ }
329
+ } while ($i < $str_length);
330
+
331
+ $this->_token = self::DATUM;
332
+ //$this->_tokenValue = substr($str, $start + 1, $i - $start - 1);
333
+ $this->_tokenValue = $result;
334
+ break;
335
+ case 't':
336
+ if (($i+ 3) < $str_length && substr($str, $start, 4) == "true") {
337
+ $this->_token = self::DATUM;
338
+ }
339
+ $this->_tokenValue = true;
340
+ $i += 3;
341
+ break;
342
+ case 'f':
343
+ if (($i+ 4) < $str_length && substr($str, $start, 5) == "false") {
344
+ $this->_token = self::DATUM;
345
+ }
346
+ $this->_tokenValue = false;
347
+ $i += 4;
348
+ break;
349
+ case 'n':
350
+ if (($i+ 3) < $str_length && substr($str, $start, 4) == "null") {
351
+ $this->_token = self::DATUM;
352
+ }
353
+ $this->_tokenValue = NULL;
354
+ $i += 3;
355
+ break;
356
+ }
357
+
358
+ if ($this->_token != self::EOF) {
359
+ $this->_offset = $i + 1; // Consume the last token character
360
+ return($this->_token);
361
+ }
362
+
363
+ $chr = $str{$i};
364
+ if ($chr == '-' || $chr == '.' || ($chr >= '0' && $chr <= '9')) {
365
+ if (preg_match('/-?([0-9])*(\.[0-9]*)?((e|E)((-|\+)?)[0-9]+)?/s',
366
+ $str, $matches, PREG_OFFSET_CAPTURE, $start) && $matches[0][1] == $start) {
367
+
368
+ $datum = $matches[0][0];
369
+
370
+ if (is_numeric($datum)) {
371
+ if (preg_match('/^0\d+$/', $datum)) {
372
+ return;
373
+ } else {
374
+ $val = intval($datum);
375
+ $fVal = floatval($datum);
376
+ $this->_tokenValue = ($val == $fVal ? $val : $fVal);
377
+ }
378
+ } else {
379
+ return;
380
+ }
381
+
382
+ $this->_token = self::DATUM;
383
+ $this->_offset = $start + strlen($datum);
384
+ }
385
+ } else {
386
+ return;
387
+ }
388
+
389
+ return($this->_token);
390
+ }
391
+ }
392
+
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=paypal%4
4
  Tags: twitter, widget, feed
5
  Requires at least: 2.5
6
  Tested up to: 2.5.1
7
- Stable tag: 1.0.0
8
 
9
  A widget that properly handles twitter feeds (singlue user or including friends), including parsing @username and URLs into links. Requires PHP5.
10
 
4
  Tags: twitter, widget, feed
5
  Requires at least: 2.5
6
  Tested up to: 2.5.1
7
+ Stable tag: 1.1.0
8
 
9
  A widget that properly handles twitter feeds (singlue user or including friends), including parsing @username and URLs into links. Requires PHP5.
10
 
wp-twitter-widget.php CHANGED
@@ -3,13 +3,18 @@
3
  * Plugin Name: Twitter Widget Pro
4
  * Plugin URI: http://xavisys.com/wordpress-twitter-widget/
5
  * Description: A widget that properly handles twitter feeds, including @username and link parsing, feeds that include friends or just one user, and can even display profile images for the users. Requires PHP5.
6
- * Version: 1.0.0
7
  * Author: Aaron D. Campbell
8
  * Author URI: http://xavisys.com/
9
  */
10
 
11
  /**
12
  * Changelog:
 
 
 
 
 
13
  * 04/17/2008: 1.0.0
14
  * - Released to wordpress.org repository
15
  *
@@ -92,7 +97,12 @@ class wpTwitterWidget
92
  $feedUrl = $this->_getFeedUrl($widgetOptions);
93
  $resp = $this->_fetch_remote_file($feedUrl);
94
  if ( $resp->status >= 200 && $resp->status < 300 ) {
95
- return json_decode($resp->results);
 
 
 
 
 
96
  } else {
97
  // Failed to fetch url;
98
  return array();
@@ -249,13 +259,15 @@ class wpTwitterWidget
249
  $before_title .= " <a class='twitterwidget' href='$twitterLink' title='" . attribute_escape("Twitter: {$tweets[0]->user->name}") . "'>";
250
  $after_title = '</a>' . $after_title;
251
  }
252
- if ( !empty( $options[$number]['title'] ) ) { echo $before_title . $options[$number]['title'] . $after_title; } ?>
 
 
 
 
253
  <ul><?php
254
- if ( $options[$number]['feed'] == 'user' && !empty($tweets) ) {
255
  echo '<li>';
256
- if ( $options[$number]['avatar']) {
257
- echo $this->_getProfileImage($tweets[0]->user);
258
- }
259
  echo '<div class="clear" />';
260
  echo '</li>';
261
  }
3
  * Plugin Name: Twitter Widget Pro
4
  * Plugin URI: http://xavisys.com/wordpress-twitter-widget/
5
  * Description: A widget that properly handles twitter feeds, including @username and link parsing, feeds that include friends or just one user, and can even display profile images for the users. Requires PHP5.
6
+ * Version: 1.1.0
7
  * Author: Aaron D. Campbell
8
  * Author URI: http://xavisys.com/
9
  */
10
 
11
  /**
12
  * Changelog:
13
+ * 04/23/2008: 1.1.0
14
+ * - Most major fix is the inclusion of json_decode.php for users that don't have json_decode() which was added in PHP 5.2.0
15
+ * - Fixed problem with displaying a useless li when profile images aren't displayed on a single user widget
16
+ * - Default title is now set to "Twitter: UserName"
17
+ *
18
  * 04/17/2008: 1.0.0
19
  * - Released to wordpress.org repository
20
  *
97
  $feedUrl = $this->_getFeedUrl($widgetOptions);
98
  $resp = $this->_fetch_remote_file($feedUrl);
99
  if ( $resp->status >= 200 && $resp->status < 300 ) {
100
+ if (function_exists('json_decode')) {
101
+ return json_decode($resp->results);
102
+ } else {
103
+ require_once('json_decode.php');
104
+ return Zend_Json_Decoder::decode($resp->results);
105
+ }
106
  } else {
107
  // Failed to fetch url;
108
  return array();
259
  $before_title .= " <a class='twitterwidget' href='$twitterLink' title='" . attribute_escape("Twitter: {$tweets[0]->user->name}") . "'>";
260
  $after_title = '</a>' . $after_title;
261
  }
262
+ if (empty($options[$number]['title'])) {
263
+ $options[$number]['title'] = "Twitter: {$options[$number]['username']}";
264
+ }
265
+ echo $before_title . $options[$number]['title'] . $after_title;
266
+ ?>
267
  <ul><?php
268
+ if ( $options[$number]['feed'] == 'user' && !empty($tweets) && $options[$number]['avatar']) {
269
  echo '<li>';
270
+ echo $this->_getProfileImage($tweets[0]->user);
 
 
271
  echo '<div class="clear" />';
272
  echo '</li>';
273
  }