Ultimate Tag Cloud Widget - Version 1.3

Version Description

  • As requested, support for calling a function to display the widget was added. Se other notes for information on how to use it.
  • Javascript changes in order to fix problems with the options page in WP 3.1 beta 1
Download this release

Release Info

Developer exz
Plugin Icon wp plugin Ultimate Tag Cloud Widget
Version 1.3
Comparing to
See all releases

Code changes from version 1.2 to 1.3

Files changed (3) hide show
  1. readme.txt +65 -4
  2. ultimate-tag-cloud-widget.php +290 -264
  3. utcw.js +13 -11
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: exz
3
  Tags: widget, tags, configurable, tag cloud
4
  Requires at least: 2.8
5
- Tested up to: 3.0.1
6
- Stable tag: 1.2
7
 
8
  This plugin aims to be the most configurable tag cloud widget out there, able to suit all your wierd tag cloud needs.
9
 
@@ -24,7 +24,7 @@ Current version supports the following preferences:
24
  * Transform tags into lowercase, uppercase or Capitalize them
25
  * Can also show categories
26
 
27
- My goal is to try to help everyone who have issues or suggestions for this plugin, so if you're not completely happy with this plugin, don't hesitate to contact me.
28
 
29
  == Installation ==
30
 
@@ -37,6 +37,7 @@ This is the same procedure as with all ordinary plugins.
37
 
38
  All the configuration options is set individually in every instance. Some default values are set if you're unsure on how to configure it.
39
 
 
40
 
41
  == Frequently Asked Questions ==
42
 
@@ -50,6 +51,11 @@ This is a new plugin, haven't had any questions yet. If you have any, be sure to
50
 
51
  == Changelog ==
52
 
 
 
 
 
 
53
  = 1.2 =
54
 
55
  * Removed all the PHP short tags
@@ -67,6 +73,11 @@ This is a new plugin, haven't had any questions yet. If you have any, be sure to
67
 
68
  == Upgrade Notice ==
69
 
 
 
 
 
 
70
  = 1.1 and 1.2 =
71
 
72
  * Just bug fixes, should be safe to upgrade.
@@ -82,4 +93,54 @@ This is my very first WordPress widget and I'd love to hear your feedback. Since
82
  My contact information is
83
 
84
  * rickard (a) 0x539.se
85
- * [twitter.com/rickard2](http://twitter.com/rickard2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  Contributors: exz
3
  Tags: widget, tags, configurable, tag cloud
4
  Requires at least: 2.8
5
+ Tested up to: 3.1 RC2
6
+ Stable tag: 1.3
7
 
8
  This plugin aims to be the most configurable tag cloud widget out there, able to suit all your wierd tag cloud needs.
9
 
24
  * Transform tags into lowercase, uppercase or Capitalize them
25
  * Can also show categories
26
 
27
+ This plugin is under active development and my goal is to try to help everyone who have issues or suggestions for this plugin, so if you're not completely happy with this plugin, don't hesitate to contact me.
28
 
29
  == Installation ==
30
 
37
 
38
  All the configuration options is set individually in every instance. Some default values are set if you're unsure on how to configure it.
39
 
40
+ If your theme doesn't use widgets, you can still use it in your theme by calling the function do_utcw(). See "Other Notes" for more information.
41
 
42
  == Frequently Asked Questions ==
43
 
51
 
52
  == Changelog ==
53
 
54
+ = 1.3 =
55
+
56
+ * As requested, support for calling a function to display the widget was added. Se other notes for information on how to use it.
57
+ * Javascript changes in order to fix problems with the options page in WP 3.1 beta 1
58
+
59
  = 1.2 =
60
 
61
  * Removed all the PHP short tags
73
 
74
  == Upgrade Notice ==
75
 
76
+ = 1.3 =
77
+
78
+ * Support for integrating the widget within your theme added.
79
+ * New javascript fixing problem with options page in WP 3.1 beta 1
80
+
81
  = 1.1 and 1.2 =
82
 
83
  * Just bug fixes, should be safe to upgrade.
93
  My contact information is
94
 
95
  * rickard (a) 0x539.se
96
+ * [twitter.com/rickard2](http://twitter.com/rickard2)
97
+ * [http://www.0x539.se/wordpress/](http://www.0x539.se/wordpress/) My blog
98
+
99
+ == Theme integration ==
100
+
101
+ Upon a request in the forums, you can now integrate the widget within your own theme even if you're not using standard wordpress widgets. Just install and load the plugin as described and use the function
102
+
103
+ `<?php do_utcw($args); ?>`
104
+
105
+ ...with the following options in the array $args:
106
+
107
+ * before_widget (string)
108
+ * after_widget (string)
109
+ * before_title (string)
110
+ * after_title (string)
111
+ * title (string)
112
+ * size_from (integer)
113
+ * size_to (integer)
114
+ * max (integer)
115
+ * reverse (boolean)
116
+ * authors (array of user IDs, integers)
117
+ * exclude (array of taxonomies, IDs or names)
118
+ * color_set (array of hex colors, like #fff or #ffffff)
119
+ * letter_spacing (integer)
120
+ * word_spacing (integer)
121
+ * color_span_from (hex color, like #fff or #ffffff)
122
+ * color_span_to (hex color, dito)
123
+ * case_sensitive (boolean)
124
+ * order (string, valid values: 'random', 'name', 'slug', 'id', 'color', 'count')
125
+ * taxonomy (string, valid values: 'post_tag', 'category')
126
+ * color (string, valid values: 'none', 'random', 'set', 'span')
127
+ * case (string, valid values: 'lowercase', 'uppercase', 'capitalize', 'off')
128
+
129
+ All options are optional, default values can be found in the widget php file. Allowed values for order, taxonomy, color and case can be found in the arrays $utcw_allowed_orders, taxonomys, colors and cases respectively. The final PHP-code might look something like this:
130
+
131
+ `<?php
132
+ $args = array(
133
+ 'before_widget' => '<div id="my_widget_wrapper">',
134
+ 'after_widget' => '</div>',
135
+ 'title' => 'Look at all these cool tagz!',
136
+ 'size_from' => 19,
137
+ 'size_to' => 52,
138
+ 'color' => 'random',
139
+ 'order' => 'name',
140
+ 'reverse' => true
141
+ );
142
+
143
+ do_utcw($args);
144
+ ?>`
145
+
146
+ Good luck and remember to give me feedback if you run into any problems!
ultimate-tag-cloud-widget.php CHANGED
@@ -1,11 +1,11 @@
1
  <?php
2
  /*
3
  Plugin Name: Ultimate tag cloud widget
4
- Plugin URI: http://bennison.se/?page_id=173
5
  Description: This plugin aims to be the most configurable tag cloud widget out there, able to suit all your wierd tag cloud needs.
6
- Version: 1.2
7
  Author: Rickard Andersson
8
- Author URI: http://bennison.se
9
  License: GPLv2
10
  */
11
 
@@ -24,32 +24,34 @@ along with this program; if not, write to the Free Software
24
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
  */
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  /**
28
  * Ultimate tag cloud widget class
29
  * @package UTCW
 
30
  */
31
  class UTCW extends WP_Widget {
32
 
33
- // Default values if the widget isn't configured properly
34
- private $default_title = "Tag Cloud";
35
- private $default_order = "name";
36
- private $default_size_from = 10;
37
- private $default_size_to = 30;
38
- private $default_max = 45;
39
- private $default_taxonomy = "post_tag";
40
- private $default_reverse = false;
41
- private $default_color = "none";
42
- private $default_letter_spacing = "normal";
43
- private $default_word_spacing = "normal";
44
- private $default_case = "off";
45
- private $default_case_sensisive = false;
46
-
47
- // Allowed values for miscellanious options in the widget
48
- private $allowed_orders = array('random', 'name', 'slug', 'id', 'color', 'count');
49
- private $allowed_taxonomys = array('post_tag', 'category');
50
- private $allowed_colors = array('none', 'random', 'set', 'span');
51
- private $allowed_cases = array('lowercase', 'uppercase', 'capitalize', 'off');
52
-
53
  /**
54
  * Constructor
55
  * @return void
@@ -66,17 +68,23 @@ class UTCW extends WP_Widget {
66
  * @retrn array
67
  */
68
  function update($new_instance, $old_instance) {
 
 
 
 
 
 
69
 
70
  extract($new_instance);
71
 
72
  // Check all input values and set the default value if any value is invalid or empty
73
  $instance = $old_instance;
74
- $instance['title'] = strlen($title) > 0 ? apply_filters('widget_title', $title) : apply_filters('widget_title', $this->default_title);
75
- $instance['size_from'] = is_numeric($size_from) ? $size_from : $this->default_size_from;
76
- $instance['size_to'] = is_numeric($size_to) ? $size_to : $this->default_size_to;
77
- $instance['max'] = is_numeric($max) ? $max : $this->default_max;
78
- $instance['letter_spacing'] = is_numeric($letter_spacing) ? $letter_spacing : $this->default_letter_spacing;
79
- $instance['word_spacing'] = is_numeric($word_spacing) ? $word_spacing : $this->default_word_spacing;
80
  $instance['reverse'] = ($reverse == "on");
81
  $instance['exclude'] = strlen($exclude) > 0 ? @explode(",", $exclude) : array();
82
  $instance['color_set'] = strlen($color_set) > 0 ? @explode(",", $color_set) : array();
@@ -86,10 +94,10 @@ class UTCW extends WP_Widget {
86
  $instance['color_span_from'] = preg_match('/#([a-f0-9]{6}|[a-f0-9]{3})/i', $color_span_from) > 0 ? $color_span_from : "";
87
  $instance['color_span_to'] = preg_match('/#([a-f0-9]{6}|[a-f0-9]{3})/i', $color_span_to) > 0 ? $color_span_to : "";
88
 
89
- $instance['taxonomy'] = in_array($taxonomy, $this->allowed_taxonomys) ? $taxonomy : $this->default_taxonomy;
90
- $instance['order'] = in_array($order, $this->allowed_orders) ? $order : $this->default_order;
91
- $instance['color'] = in_array($color, $this->allowed_colors) ? $color : $this->default_color;
92
- $instance['case'] = in_array($case, $this->allowed_cases) ? $case : $this->default_case;
93
 
94
  // Only accept numeric authors (user ID)
95
  if (is_array($authors)) {
@@ -121,213 +129,9 @@ class UTCW extends WP_Widget {
121
  * @param array $instance
122
  * @return void
123
  */
124
- function widget($args, $instance) {
125
-
126
- global $wpdb;
127
-
128
- extract($args);
129
- extract($instance);
130
-
131
- // Parse settings from $instance and set default values where empty or invalid
132
- $title = strlen($title) > 0 ? $title : $this->default_title;
133
- $size_from = is_numeric($size_from) ? $size_from : $this->default_size_from;
134
- $size_to = is_numeric($size_to) ? $size_to : $this->default_size_to;
135
- $max = is_numeric($max) ? $max : $this->default_max;
136
- $reverse = is_bool($reverse) ? $reverse : $this->default_reverse;
137
- $authors = is_array($authors) ? $authors : array();
138
- $exclude = is_array($exclude) ? $exclude : array();
139
- $color_set = is_array($color_set) ? $color_set : array();
140
- $letter_spacing = is_numeric($letter_spacing) ? $letter_spacing . "px" : $this->default_letter_spacing;
141
- $word_spacing = is_numeric($word_spacing) ? $word_spacing . "px" : $this->default_word_spacing;
142
- $color_span_from = is_string($color_span_from) ? $color_span_from : "";
143
- $color_span_to = is_string($color_span_to) ? $color_span_to : "";
144
- $case_sensitive = is_bool($case_sensitive) ? $case_sensitive : $this->default_case_sensitive;
145
-
146
- $order = in_array($order, $this->allowed_orders) ? $order : $this->default_order;
147
- $taxonomy = in_array($taxonomy, $this->allowed_taxonomys) ? $taxonomy : $this->default_taxonomy;
148
- $color = in_array($color, $this->allowed_colors) ? $color : $this->default_color;
149
- $case = in_array($case, $this->allowed_cases) ? $case : $this->default_case;
150
-
151
- // Fallback values
152
- $counts = array();
153
- $tag_array = array();
154
-
155
- // Build SQL query
156
- $q = "SELECT t.term_id, t.name, t.slug, COUNT(tr.term_taxonomy_id) AS `count` ";
157
- $q .= "FROM `$wpdb->posts` AS p ";
158
- $q .= "LEFT JOIN `$wpdb->term_relationships` AS tr ON tr.object_id = p.ID ";
159
- $q .= "LEFT JOIN `$wpdb->terms` AS t ON t.term_id = tr.term_taxonomy_id ";
160
- $q .= "LEFT JOIN `$wpdb->term_taxonomy` AS tt ON tt.term_id = t.term_id ";
161
- $q .= "WHERE tt.taxonomy = '$taxonomy' ";
162
-
163
- if (count($authors) > 0) {
164
- $q .= "AND post_author IN (" . implode(",", $authors) . ") ";
165
- }
166
- if (count($exclude) > 0) {
167
- if (is_array_numeric($exclude)) {
168
- $q .= "AND t.term_id NOT IN ('" . implode("', '", $exclude) . "') ";
169
- } else {
170
- $q .= "AND t.name NOT IN ('" . implode("', '", $exclude) . "') ";
171
- }
172
- }
173
-
174
- $q .= "GROUP BY t.term_id ";
175
- $q .= "ORDER BY COUNT(tr.term_taxonomy_id) DESC ";
176
- $q .= "LIMIT $max;";
177
-
178
- $tag_data = $wpdb->get_results($q);
179
-
180
- if (count($tag_data) > 0) {
181
-
182
- // Extract counts and create an array to work with
183
- foreach ($tag_data as $tag) {
184
- $counts[] = $tag->count;
185
- $tag_array[] = array(
186
- 'term_id' => $tag->term_id,
187
- 'count' => $tag->count,
188
- 'slug' => $tag->slug,
189
- 'name' => $tag->name,
190
- 'link' => get_term_link(intval($tag->term_id), $taxonomy),
191
- 'color' => ""
192
- );
193
- }
194
-
195
- // Highest and lowest values
196
- $min_count = min($counts);
197
- $max_count = max($counts);
198
-
199
- // Get the step size
200
- $font_step = $this->calc_step($min_count, $max_count, $size_from, $size_to);
201
-
202
- // Calculate sizes for all tags
203
- foreach ($tag_array as $key => $tag) {
204
- $tag_array[$key]['size'] = $size_from + ( ( $tag['count'] - $min_count ) * $font_step );
205
- }
206
-
207
- // Check the coloring preference, default is none
208
- switch ($color) {
209
-
210
- // Just get an randomized value, who would ever use this?!
211
- case "random":
212
- foreach ($tag_array as $key => $tag) {
213
- $tag_array[$key]['color'] = sprintf("#%s%s%s", dechex(rand() % 255), dechex(rand() % 255), dechex(rand() % 255));
214
- }
215
- break;
216
-
217
- // Select a random value from the preset colors
218
- case "set":
219
- if (is_array($color_set) && count($color_set) > 0) {
220
- foreach ($tag_array as $key => $tag) {
221
- $tag_array[$key]['color'] = $color_set[ array_rand($color_set) ];
222
- }
223
- }
224
- break;
225
-
226
- // Calculate colors in a span between two values
227
- case "span":
228
-
229
- // Check the color format, #fff or #fffff
230
- if (strlen($color_span_from) == 4) {
231
- $red_from = hexdec(sprintf("%s%s", $color_span_from[1], $color_span_from[1]));
232
- $green_from = hexdec(sprintf("%s%s", $color_span_from[2], $color_span_from[2]));
233
- $blue_from = hexdec(sprintf("%s%s", $color_span_from[3], $color_span_from[3]));
234
- } else {
235
- $red_from = hexdec(substr($color_span_from, 1, 2));
236
- $green_from = hexdec(substr($color_span_from, 3, 2));
237
- $blue_from = hexdec(substr($color_span_from, 5, 2));
238
- }
239
- if (strlen($color_span_to) == 4) {
240
- $red_to = hexdec(sprintf("%s%s", $color_span_to[1], $color_span_to[1]));
241
- $green_to = hexdec(sprintf("%s%s", $color_span_to[2], $color_span_to[2]));
242
- $blue_to = hexdec(sprintf("%s%s", $color_span_to[3], $color_span_to[3]));
243
- } else {
244
- $red_to = hexdec(substr($color_span_to, 1, 2));
245
- $green_to = hexdec(substr($color_span_to, 3, 2));
246
- $blue_to = hexdec(substr($color_span_to, 5, 2));
247
- }
248
-
249
- // Calculate steps for all the colors.
250
- $red_step = $this->calc_step($min_count, $max_count, $red_from, $red_to);
251
- $green_step = $this->calc_step($min_count, $max_count, $green_from, $green_to);
252
- $blue_step = $this->calc_step($min_count, $max_count, $blue_from, $blue_to);
253
-
254
- // Iterate all tags and calculate their color
255
- foreach ($tag_array as $key => $tag) {
256
- $red = round($red_from + ( ( $tag['count'] - $min_count ) * $red_step ));
257
- $green = round($green_from + ( ( $tag['count'] - $min_count ) * $green_step ));
258
- $blue = round($blue_from + ( ( $tag['count'] - $min_count ) * $blue_step ));
259
-
260
- $tag_array[$key]['color'] = sprintf("rgb(%s,%s,%s)", $red, $green, $blue);
261
- }
262
- break;
263
- }
264
-
265
- // Check the ordering preference, default is name
266
- switch ($order) {
267
- case "random":
268
- shuffle($tag_array);
269
- break;
270
-
271
- case "count":
272
- usort($tag_array, 'utcw_cmp_count');
273
- break;
274
-
275
- case "slug";
276
- usort($tag_array, $case_sensitive === true ? 'utcw_cmp_slug' : 'utcw_icmp_slug');
277
- break;
278
-
279
- case "id":
280
- usort($tag_array, 'utcw_cmp_id');
281
- break;
282
-
283
- case "color":
284
- usort($tag_array, 'utcw_cmp_color');
285
- break;
286
-
287
- case "name":
288
- default:
289
- usort($tag_array, $case_sensitive === true ? 'utcw_cmp_name' : 'utcw_icmp_name');
290
- break;
291
- }
292
-
293
- // Reverse the list if the user prefers it that way. Reversing an random sorted result seems correct.
294
- if ($reverse === true) {
295
- $tag_array = array_reverse($tag_array);
296
- }
297
- }
298
-
299
- switch ($case) {
300
- case 'uppercase':
301
- $text_transform = 'text-transform: uppercase;';
302
- break;
303
-
304
- case 'lowercase':
305
- $text_transform = 'text-transform: lowercase;';
306
- break;
307
-
308
- case 'capitalize':
309
- $text_transform = 'text-transform: capitalize;';
310
- break;
311
- }
312
-
313
- // Print the tag cloud content
314
- echo $before_widget;
315
- echo $before_title . $title . $after_title;
316
-
317
- printf('<div class="widget_tag_cloud" style="letter-spacing:%s;word-spacing:%s;%s">',
318
- $letter_spacing, $word_spacing, $text_transform);
319
-
320
- foreach ($tag_array as $tag) {
321
- extract($tag);
322
-
323
- printf('<a class="tag-link-%s" href="%s" style="font-size:%spx;%s;" title="' . _("%s topics") . '">%s</a> ',
324
- $term_id, $link, $size, strlen($color) > 0 ? "color:$color" : "", $count, $name);
325
- }
326
-
327
- echo "</div>";
328
-
329
- echo $after_widget;
330
- }
331
 
332
  /**
333
  * Function for handling the widget control in admin panel
@@ -408,16 +212,16 @@ class UTCW extends WP_Widget {
408
  <fieldset class="utcw">
409
  <legend><?php _e("Appearance"); ?></legend>
410
  <label for="<?php echo $this->get_field_id('title');?>"><?php _e("Title:") ?></label><br/>
411
- <input type="text" id="<?php echo $this->get_field_id('title');?>" name="<?php echo $this->get_field_name('title');?>" value="<?php echo strlen($title) > 0 ? $title : $this->default_title?>" /><br/>
412
  <br/>
413
  <?php _e("Tag size (in px):"); ?><br/>
414
  <label for="<?php echo $this->get_field_id('size_from'); ?>"><?php _e("From"); ?></label>
415
- <input type="text" name="<?php echo $this->get_field_name('size_from'); ?>" id="<?php echo $this->get_field_id('size_from'); ?>" size="3" value="<?php echo is_numeric($size_from) ? $size_from : $this->default_size_from?>" />
416
  <label for="<?php echo $this->get_field_id('size_to'); ?>"><?php _e("to"); ?></label>
417
- <input type="text" name="<?php echo $this->get_field_name('size_to'); ?>" id="<?php echo $this->get_field_id('size_to'); ?>" size="3" value="<?php echo is_numeric($size_to) ? $size_to : $this->default_size_to?>" /><br/>
418
  <br/>
419
  <label for="<?php echo $this->get_field_id('max'); ?>"><?php _e("Max tags:"); ?></label><br/>
420
- <input type="text" name="<?php echo $this->get_field_name('max'); ?>" id="<?php echo $this->get_field_id('max'); ?>" value="<?php echo is_numeric($max) ? $max : $this->default_max?>" /><br/>
421
  <br/>
422
  <?php _e("Coloring:") ?><br/>
423
  <input type="radio" name="<?php echo $this->get_field_name('color'); ?>" id="<?php echo $this->get_field_id('color_none'); ?>" value="none" <?php echo $color == "none" || empty($color) ? 'checked="checked"' : ""; ?> onclick="javascript:utcw_change()" />
@@ -438,9 +242,9 @@ class UTCW extends WP_Widget {
438
  <br/>
439
  <?php _e("Spacing (in px):"); ?><br/>
440
  <label for="<?php echo $this->get_field_id('letter_spacing'); ?>"><?php _e("Between letters:"); ?></label>
441
- <input type="text" size="5" name="<?php echo $this->get_field_name('letter_spacing'); ?>" id="<?php echo $this->get_field_id?>" value="<?php echo strlen($letter_spacing) > 0 ? $letter_spacing : $this->default_letter_spacing?>" /><br/>
442
  <label for="<?php echo $this->get_field_id('word_spacing'); ?>"><?php _e("Between words:"); ?></label>
443
- <input type="text" size="5" name="<?php echo $this->get_field_name('word_spacing'); ?>" id="<?php echo $this->get_field_id('word_spacing'); ?>" value="<?php echo strlen($word_spacing) > 0 ? $word_spacing : $this->default_word_spacing?>" /><br/>
444
  <br/>
445
  <?php _e("Transform tags:"); ?><br/>
446
  <input type="radio" name="<?php echo $this->get_field_name('case'); ?>" id="<?php echo $this->get_field_id('text_transform_off'); ?>" value="off" <?php echo ($case == "off" || empty($case)) ? ' checked="checked"' : ""; ?> />
@@ -454,27 +258,249 @@ class UTCW extends WP_Widget {
454
  </fieldset>
455
  <?php
456
  }
457
- /**
458
- * Used to calculate how step size in spanning values
459
- * @param integer $min
460
- * @param integer $max
461
- * @param integer $from
462
- * @param integer $to
463
- * @return integer
464
- */
465
- private function calc_step($min, $max, $from, $to) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
466
 
467
- // Thank you wordpress for this
468
- $spread = $max - $min;
469
- if ( $spread <= 0 )
470
- $spread = 1;
471
- $font_spread = $to - $from;
472
- if ( $font_spread < 0 && $negative === false)
473
- $font_spread = 1;
474
- $step = $font_spread / $spread;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
475
 
476
- return $step;
 
477
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
478
  }
479
 
480
  // Register widget with wordpress
1
  <?php
2
  /*
3
  Plugin Name: Ultimate tag cloud widget
4
+ Plugin URI: http://www.0x539.se/wordpress/
5
  Description: This plugin aims to be the most configurable tag cloud widget out there, able to suit all your wierd tag cloud needs.
6
+ Version: 1.3
7
  Author: Rickard Andersson
8
+ Author URI: http://www.0x539.se/
9
  License: GPLv2
10
  */
11
 
24
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
  */
26
 
27
+ // Default values if the widget isn't configured properly
28
+ DEFINE("UTCW_DEFAULT_TITLE", "Tag Cloud");
29
+ DEFINE("UTCW_DEFAULT_ORDER", "name");
30
+ DEFINE("UTCW_DEFAULT_SIZE_FROM", 10);
31
+ DEFINE("UTCW_DEFAULT_SIZE_TO", 30);
32
+ DEFINE("UTCW_DEFAULT_MAX", 45);
33
+ DEFINE("UTCW_DEFAULT_TAXONOMY", "post_tag");
34
+ DEFINE("UTCW_DEFAULT_REVERSE", false);
35
+ DEFINE("UTCW_DEFAULT_COLOR", "none");
36
+ DEFINE("UTCW_DEFAULT_LETTER_SPACING", "normal");
37
+ DEFINE("UTCW_DEFAULT_WORD_SPACING", "normal");
38
+ DEFINE("UTCW_DEFAULT_CASE", "off");
39
+ DEFINE("UTCW_DEFAULT_CASE_SENSITIVE", false);
40
+
41
+ // Allowed values for miscellanious options in the widget
42
+ $utcw_allowed_orders = array('random', 'name', 'slug', 'id', 'color', 'count');
43
+ $utcw_allowed_taxonomys = array('post_tag', 'category');
44
+ $utcw_allowed_colors = array('none', 'random', 'set', 'span');
45
+ $utcw_allowed_cases = array('lowercase', 'uppercase', 'capitalize', 'off');
46
+
47
+
48
  /**
49
  * Ultimate tag cloud widget class
50
  * @package UTCW
51
+ * @author Rickard Andersson <rickard@0x539.se>
52
  */
53
  class UTCW extends WP_Widget {
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  /**
56
  * Constructor
57
  * @return void
68
  * @retrn array
69
  */
70
  function update($new_instance, $old_instance) {
71
+
72
+ global $utcw_allowed_orders,
73
+ $utcw_allowed_taxonomys,
74
+ $utcw_allowed_colors,
75
+ $utcw_allowed_cases;
76
+
77
 
78
  extract($new_instance);
79
 
80
  // Check all input values and set the default value if any value is invalid or empty
81
  $instance = $old_instance;
82
+ $instance['title'] = strlen($title) > 0 ? apply_filters('widget_title', $title) : apply_filters('widget_title', UTCW_DEFAULT_TITLE);
83
+ $instance['size_from'] = is_numeric($size_from) ? $size_from : UTCW_DEFAULT_SIZE_FROM;
84
+ $instance['size_to'] = is_numeric($size_to) ? $size_to : UTCW_DEFAULT_SIZE_TO;
85
+ $instance['max'] = is_numeric($max) ? $max : UTCW_DEFALUT_MAX;
86
+ $instance['letter_spacing'] = is_numeric($letter_spacing) ? $letter_spacing : UTCW_DEFAULT_LETTER_SPACING;
87
+ $instance['word_spacing'] = is_numeric($word_spacing) ? $word_spacing : UTCW_DEFAULT_WORD_SPACING;
88
  $instance['reverse'] = ($reverse == "on");
89
  $instance['exclude'] = strlen($exclude) > 0 ? @explode(",", $exclude) : array();
90
  $instance['color_set'] = strlen($color_set) > 0 ? @explode(",", $color_set) : array();
94
  $instance['color_span_from'] = preg_match('/#([a-f0-9]{6}|[a-f0-9]{3})/i', $color_span_from) > 0 ? $color_span_from : "";
95
  $instance['color_span_to'] = preg_match('/#([a-f0-9]{6}|[a-f0-9]{3})/i', $color_span_to) > 0 ? $color_span_to : "";
96
 
97
+ $instance['taxonomy'] = in_array($taxonomy, $utcw_allowed_taxonomys) ? $taxonomy : UTCW_DEFAULT_TAXONOMY;
98
+ $instance['order'] = in_array($order, $utcw_allowed_orders) ? $order : UTCW_DEFAULT_ORDER;
99
+ $instance['color'] = in_array($color, $utcw_allowed_colors) ? $color : UTCW_DEFAULT_COLOR;
100
+ $instance['case'] = in_array($case, $utcw_allowed_cases) ? $case : UTCW_DEFAULT_CASE;
101
 
102
  // Only accept numeric authors (user ID)
103
  if (is_array($authors)) {
129
  * @param array $instance
130
  * @return void
131
  */
132
+ function widget($args, $instance) {
133
+ do_utcw(array_merge($instance, $args));
134
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
  /**
137
  * Function for handling the widget control in admin panel
212
  <fieldset class="utcw">
213
  <legend><?php _e("Appearance"); ?></legend>
214
  <label for="<?php echo $this->get_field_id('title');?>"><?php _e("Title:") ?></label><br/>
215
+ <input type="text" id="<?php echo $this->get_field_id('title');?>" name="<?php echo $this->get_field_name('title');?>" value="<?php echo strlen($title) > 0 ? $title : UTCW_DEFAULT_TITLE?>" /><br/>
216
  <br/>
217
  <?php _e("Tag size (in px):"); ?><br/>
218
  <label for="<?php echo $this->get_field_id('size_from'); ?>"><?php _e("From"); ?></label>
219
+ <input type="text" name="<?php echo $this->get_field_name('size_from'); ?>" id="<?php echo $this->get_field_id('size_from'); ?>" size="3" value="<?php echo is_numeric($size_from) ? $size_from : UTCW_DEFAULT_SIZE_FROM?>" />
220
  <label for="<?php echo $this->get_field_id('size_to'); ?>"><?php _e("to"); ?></label>
221
+ <input type="text" name="<?php echo $this->get_field_name('size_to'); ?>" id="<?php echo $this->get_field_id('size_to'); ?>" size="3" value="<?php echo is_numeric($size_to) ? $size_to : UTCW_DEFAULT_SIZE_TO?>" /><br/>
222
  <br/>
223
  <label for="<?php echo $this->get_field_id('max'); ?>"><?php _e("Max tags:"); ?></label><br/>
224
+ <input type="text" name="<?php echo $this->get_field_name('max'); ?>" id="<?php echo $this->get_field_id('max'); ?>" value="<?php echo is_numeric($max) ? $max : UTCW_DEFAULT_MAX?>" /><br/>
225
  <br/>
226
  <?php _e("Coloring:") ?><br/>
227
  <input type="radio" name="<?php echo $this->get_field_name('color'); ?>" id="<?php echo $this->get_field_id('color_none'); ?>" value="none" <?php echo $color == "none" || empty($color) ? 'checked="checked"' : ""; ?> onclick="javascript:utcw_change()" />
242
  <br/>
243
  <?php _e("Spacing (in px):"); ?><br/>
244
  <label for="<?php echo $this->get_field_id('letter_spacing'); ?>"><?php _e("Between letters:"); ?></label>
245
+ <input type="text" size="5" name="<?php echo $this->get_field_name('letter_spacing'); ?>" id="<?php echo $this->get_field_id?>" value="<?php echo strlen($letter_spacing) > 0 ? $letter_spacing : UTCW_DEFAULT_LETTER_SPACING?>" /><br/>
246
  <label for="<?php echo $this->get_field_id('word_spacing'); ?>"><?php _e("Between words:"); ?></label>
247
+ <input type="text" size="5" name="<?php echo $this->get_field_name('word_spacing'); ?>" id="<?php echo $this->get_field_id('word_spacing'); ?>" value="<?php echo strlen($word_spacing) > 0 ? $word_spacing : UTCW_DEFAULT_WORD_SPACING?>" /><br/>
248
  <br/>
249
  <?php _e("Transform tags:"); ?><br/>
250
  <input type="radio" name="<?php echo $this->get_field_name('case'); ?>" id="<?php echo $this->get_field_id('text_transform_off'); ?>" value="off" <?php echo ($case == "off" || empty($case)) ? ' checked="checked"' : ""; ?> />
258
  </fieldset>
259
  <?php
260
  }
261
+ }
262
+
263
+ /**
264
+ * do_utcw - Prints markup for the widget
265
+ * @param array $args An array with widget settings. See {@link http://wordpress.org/extend/plugins/ultimate-tag-cloud-widget/other_notes/} for details on which options you can set.
266
+ * @return void
267
+ */
268
+
269
+ function do_utcw($args) {
270
+
271
+ global $wpdb;
272
+
273
+ extract($args);
274
+
275
+ global $utcw_allowed_orders,
276
+ $utcw_allowed_taxonomys,
277
+ $utcw_allowed_colors,
278
+ $utcw_allowed_cases;
279
+
280
+ // Parse settings from $instance and set default values where empty or invalid
281
+ $title = strlen($title) > 0 ? $title : UTCW_DEFAULT_TITLE;
282
+ $size_from = is_numeric($size_from) ? $size_from : UTCW_DEFAULT_SIZE_FROM;
283
+ $size_to = is_numeric($size_to) ? $size_to : UTCW_DEFAULT_SIZE_TO;
284
+ $max = is_numeric($max) ? $max : UTCW_DEFAULT_MAX;
285
+ $reverse = is_bool($reverse) ? $reverse : UTCW_DEFAULT_REVERSE;
286
+ $authors = is_array($authors) ? $authors : array();
287
+ $exclude = is_array($exclude) ? $exclude : array();
288
+ $color_set = is_array($color_set) ? $color_set : array();
289
+ $letter_spacing = is_numeric($letter_spacing) ? $letter_spacing . "px" : UTCW_DEFAULT_LETTER_SPACING;
290
+ $word_spacing = is_numeric($word_spacing) ? $word_spacing . "px" : UTCW_DEFAULT_WORD_SPACING;
291
+ $color_span_from = is_string($color_span_from) ? $color_span_from : "";
292
+ $color_span_to = is_string($color_span_to) ? $color_span_to : "";
293
+ $case_sensitive = is_bool($case_sensitive) ? $case_sensitive : UTCW_DEFAULT_CASE_SENSITIVE;
294
+
295
+ $order = in_array($order, $utcw_allowed_orders) ? $order : UTCW_DEFAULT_ORDER;
296
+ $taxonomy = in_array($taxonomy, $utcw_allowed_taxonomys) ? $taxonomy : UTCW_DEFAULT_TAXONOMY;
297
+ $color = in_array($color, $utcw_allowed_colors) ? $color : UTCW_DEFAULT_COLOR;
298
+ $case = in_array($case, $utcw_allowed_cases) ? $case : UTCW_DEFAULT_CASE;
299
+
300
+ // Fallback values
301
+ $counts = array();
302
+ $tag_array = array();
303
+
304
+ // Build SQL query
305
+ $q = "SELECT t.term_id, t.name, t.slug, COUNT(tr.term_taxonomy_id) AS `count` ";
306
+ $q .= "FROM `$wpdb->posts` AS p ";
307
+ $q .= "LEFT JOIN `$wpdb->term_relationships` AS tr ON tr.object_id = p.ID ";
308
+ $q .= "LEFT JOIN `$wpdb->terms` AS t ON t.term_id = tr.term_taxonomy_id ";
309
+ $q .= "LEFT JOIN `$wpdb->term_taxonomy` AS tt ON tt.term_id = t.term_id ";
310
+ $q .= "WHERE tt.taxonomy = '$taxonomy' ";
311
+
312
+ if (count($authors) > 0) {
313
+ $q .= "AND post_author IN (" . implode(",", $authors) . ") ";
314
+ }
315
+ if (count($exclude) > 0) {
316
+ if (is_array_numeric($exclude)) {
317
+ $q .= "AND t.term_id NOT IN ('" . implode("', '", $exclude) . "') ";
318
+ } else {
319
+ $q .= "AND t.name NOT IN ('" . implode("', '", $exclude) . "') ";
320
+ }
321
+ }
322
+
323
+ $q .= "GROUP BY t.term_id ";
324
+ $q .= "ORDER BY COUNT(tr.term_taxonomy_id) DESC ";
325
+ $q .= "LIMIT $max;";
326
+
327
+ $tag_data = $wpdb->get_results($q);
328
+
329
+ if (count($tag_data) > 0) {
330
+
331
+ // Extract counts and create an array to work with
332
+ foreach ($tag_data as $tag) {
333
+ $counts[] = $tag->count;
334
+ $tag_array[] = array(
335
+ 'term_id' => $tag->term_id,
336
+ 'count' => $tag->count,
337
+ 'slug' => $tag->slug,
338
+ 'name' => $tag->name,
339
+ 'link' => get_term_link(intval($tag->term_id), $taxonomy),
340
+ 'color' => ""
341
+ );
342
+ }
343
+
344
+ // Highest and lowest values
345
+ $min_count = min($counts);
346
+ $max_count = max($counts);
347
+
348
+ // Get the step size
349
+ $font_step = calc_step($min_count, $max_count, $size_from, $size_to);
350
+
351
+ // Calculate sizes for all tags
352
+ foreach ($tag_array as $key => $tag) {
353
+ $tag_array[$key]['size'] = $size_from + ( ( $tag['count'] - $min_count ) * $font_step );
354
+ }
355
+
356
+ // Check the coloring preference, default is none
357
+ switch ($color) {
358
+
359
+ // Just get an randomized value, who would ever use this?!
360
+ case "random":
361
+ foreach ($tag_array as $key => $tag) {
362
+ $tag_array[$key]['color'] = sprintf("#%s%s%s", dechex(rand() % 255), dechex(rand() % 255), dechex(rand() % 255));
363
+ }
364
+ break;
365
+
366
+ // Select a random value from the preset colors
367
+ case "set":
368
+ if (is_array($color_set) && count($color_set) > 0) {
369
+ foreach ($tag_array as $key => $tag) {
370
+ $tag_array[$key]['color'] = $color_set[ array_rand($color_set) ];
371
+ }
372
+ }
373
+ break;
374
+
375
+ // Calculate colors in a span between two values
376
+ case "span":
377
+
378
+ // Check the color format, #fff or #fffff
379
+ if (strlen($color_span_from) == 4) {
380
+ $red_from = hexdec(sprintf("%s%s", $color_span_from[1], $color_span_from[1]));
381
+ $green_from = hexdec(sprintf("%s%s", $color_span_from[2], $color_span_from[2]));
382
+ $blue_from = hexdec(sprintf("%s%s", $color_span_from[3], $color_span_from[3]));
383
+ } else {
384
+ $red_from = hexdec(substr($color_span_from, 1, 2));
385
+ $green_from = hexdec(substr($color_span_from, 3, 2));
386
+ $blue_from = hexdec(substr($color_span_from, 5, 2));
387
+ }
388
+ if (strlen($color_span_to) == 4) {
389
+ $red_to = hexdec(sprintf("%s%s", $color_span_to[1], $color_span_to[1]));
390
+ $green_to = hexdec(sprintf("%s%s", $color_span_to[2], $color_span_to[2]));
391
+ $blue_to = hexdec(sprintf("%s%s", $color_span_to[3], $color_span_to[3]));
392
+ } else {
393
+ $red_to = hexdec(substr($color_span_to, 1, 2));
394
+ $green_to = hexdec(substr($color_span_to, 3, 2));
395
+ $blue_to = hexdec(substr($color_span_to, 5, 2));
396
+ }
397
+
398
+ // Calculate steps for all the colors.
399
+ $red_step = calc_step($min_count, $max_count, $red_from, $red_to);
400
+ $green_step = calc_step($min_count, $max_count, $green_from, $green_to);
401
+ $blue_step = calc_step($min_count, $max_count, $blue_from, $blue_to);
402
+
403
+ // Iterate all tags and calculate their color
404
+ foreach ($tag_array as $key => $tag) {
405
+ $red = round($red_from + ( ( $tag['count'] - $min_count ) * $red_step ));
406
+ $green = round($green_from + ( ( $tag['count'] - $min_count ) * $green_step ));
407
+ $blue = round($blue_from + ( ( $tag['count'] - $min_count ) * $blue_step ));
408
+
409
+ $tag_array[$key]['color'] = sprintf("rgb(%s,%s,%s)", $red, $green, $blue);
410
+ }
411
+ break;
412
+ }
413
+
414
+ // Check the ordering preference, default is name
415
+ switch ($order) {
416
+ case "random":
417
+ shuffle($tag_array);
418
+ break;
419
+
420
+ case "count":
421
+ usort($tag_array, 'utcw_cmp_count');
422
+ break;
423
+
424
+ case "slug";
425
+ usort($tag_array, $case_sensitive === true ? 'utcw_cmp_slug' : 'utcw_icmp_slug');
426
+ break;
427
+
428
+ case "id":
429
+ usort($tag_array, 'utcw_cmp_id');
430
+ break;
431
+
432
+ case "color":
433
+ usort($tag_array, 'utcw_cmp_color');
434
+ break;
435
+
436
+ case "name":
437
+ default:
438
+ usort($tag_array, $case_sensitive === true ? 'utcw_cmp_name' : 'utcw_icmp_name');
439
+ break;
440
+ }
441
 
442
+ // Reverse the list if the user prefers it that way. Reversing an random sorted result seems correct.
443
+ if ($reverse === true) {
444
+ $tag_array = array_reverse($tag_array);
445
+ }
446
+ }
447
+
448
+ switch ($case) {
449
+ case 'uppercase':
450
+ $text_transform = 'text-transform: uppercase;';
451
+ break;
452
+
453
+ case 'lowercase':
454
+ $text_transform = 'text-transform: lowercase;';
455
+ break;
456
+
457
+ case 'capitalize':
458
+ $text_transform = 'text-transform: capitalize;';
459
+ break;
460
+
461
+ default:
462
+ $text_transform = '';
463
+ }
464
+
465
+ // Print the tag cloud content
466
+ echo $before_widget;
467
+ echo $before_title . $title . $after_title;
468
+
469
+ printf('<div class="widget_tag_cloud" style="letter-spacing:%s;word-spacing:%s;%s">',
470
+ $letter_spacing, $word_spacing, $text_transform);
471
+
472
+ foreach ($tag_array as $tag) {
473
+ extract($tag);
474
 
475
+ printf('<a class="tag-link-%s" href="%s" style="font-size:%spx;%s;" title="' . _("%s topics") . '">%s</a> ',
476
+ $term_id, $link, $size, strlen($color) > 0 ? "color:$color" : "", $count, $name);
477
  }
478
+
479
+ echo "</div>";
480
+
481
+ echo $after_widget;
482
+ }
483
+
484
+ /**
485
+ * Used to calculate how step size in spanning values
486
+ * @param integer $min
487
+ * @param integer $max
488
+ * @param integer $from
489
+ * @param integer $to
490
+ * @return integer
491
+ */
492
+ function calc_step($min, $max, $from, $to) {
493
+
494
+ // Thank you wordpress for this
495
+ $spread = $max - $min;
496
+ if ( $spread <= 0 )
497
+ $spread = 1;
498
+ $font_spread = $to - $from;
499
+ if ( $font_spread < 0 && $negative === false)
500
+ $font_spread = 1;
501
+ $step = $font_spread / $spread;
502
+
503
+ return $step;
504
  }
505
 
506
  // Register widget with wordpress
utcw.js CHANGED
@@ -1,13 +1,15 @@
1
- function utcw_change() {
2
-
3
- jQuery("div[id$='set_chooser']").addClass('utcw-hidden');
4
- jQuery("div[id$='span_chooser']").addClass('utcw-hidden');
5
 
6
- jQuery("input[id$='color_set']:checked").each(function() {
7
- jQuery("div[id$='set_chooser']").removeClass('utcw-hidden');
8
- });
9
 
10
- jQuery("input[id$='color_span']:checked").each(function() {
11
- jQuery("div[id$='span_chooser']").removeClass('utcw-hidden');
12
- });
13
- }
 
 
 
 
1
+ jQuery(document).ready(function() {
2
+
3
+ jQuery("input[id$=-color_none], input[id$=-color_random], input[id$=-color_set], input[id$=-color_span]").live('click', function() {
 
4
 
5
+ jQuery("div[id$='set_chooser']").addClass('utcw-hidden');
6
+ jQuery("div[id$='span_chooser']").addClass('utcw-hidden');
 
7
 
8
+ if (jQuery(this).val() == 'set') {
9
+ jQuery("div[id$='set_chooser']").removeClass('utcw-hidden');
10
+ } else if (jQuery(this).val() == 'span') {
11
+ jQuery("div[id$='span_chooser']").removeClass('utcw-hidden');
12
+ }
13
+
14
+ })
15
+ });