| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Categorized Tag Cloud |
| 4 |
Plugin URI: http://www.whiletrue.it/ |
| 5 |
Description: Takes the website tags and aggregates them into a categorized cloud widget for sidebar. |
| 6 |
Author: WhileTrue |
| 7 |
Version: 1.0 |
| 8 |
Author URI: http://www.whiletrue.it/ |
| 9 |
*/ |
| 10 |
|
| 11 |
/* |
| 12 |
This program is free software; you can redistribute it and/or modify |
| 13 |
it under the terms of the GNU General Public License version 2, |
| 14 |
as published by the Free Software Foundation. |
| 15 |
|
| 16 |
This program is distributed in the hope that it will be useful, |
| 17 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
GNU General Public License for more details. |
| 20 |
*/ |
| 21 |
|
| 22 |
function categorized_tag_cloud ($instance) { |
| 23 |
|
| 24 |
$plugin_name = 'categorized-tag-cloud'; |
| 25 |
|
| 26 |
// RETRIEVE TAGS |
| 27 |
$words_color = $instance['words_color']; |
| 28 |
$hover_color = (isset($instance['hover_color']) && $instance['hover_color']!='') ? $instance['hover_color'] : 'black'; |
| 29 |
$number = (isset($instance['words_number']) && is_numeric($instance['words_number']) && $instance['words_number'] >0) ? $instance['words_number'] : 20; |
| 30 |
$smallest_size = (isset($instance['smallest_size']) && is_numeric($instance['smallest_size']) && $instance['smallest_size']>0) ? $instance['smallest_size'] : 7; |
| 31 |
$largest_size = (isset($instance['largest_size']) && is_numeric($instance['largest_size']) && $instance['largest_size'] >0) ? $instance['largest_size'] : 14; |
| 32 |
|
| 33 |
$exclude_items = array(); |
| 34 |
$category_filters = (array)json_decode($instance['category_filters']); |
| 35 |
for ($i=0; $i<count($category_filters['cat']); $i++) { |
| 36 |
if (is_category($category_filters['cat'][$i]) || (is_single() && in_category($category_filters['cat'][$i]))) { |
| 37 |
$exclude_items[] = $category_filters['tag'][$i]; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
$tags = wp_tag_cloud('smallest=14&largest=30&number='.$number.'&order=RAND&format=array&exclude='.implode(',',$exclude_items) ); |
| 42 |
|
| 43 |
$out = ''; |
| 44 |
$out_style = ''; |
| 45 |
foreach ($tags as $num=>$tag) { |
| 46 |
$i = $num+1; |
| 47 |
$out .= '<span id="'.$plugin_name.'-el-'.$i.'">'.$tag.'</span> '; |
| 48 |
|
| 49 |
$the_color = ($words_color=='') ? '#'.str_pad(dechex(rand(0,4096)),3,'0',STR_PAD_LEFT) : $words_color; |
| 50 |
$out_style .= ' |
| 51 |
#'.$plugin_name.'-el-'.$i.' a, #'.$plugin_name.'-el-'.$i.' a:visited { text-decoration:none; color:'.$the_color.'; }'; |
| 52 |
} |
| 53 |
|
| 54 |
return ' |
| 55 |
<div id="'.$plugin_name.'">'.$out.'</div> |
| 56 |
<style> |
| 57 |
'.$out_style.' |
| 58 |
</style>'; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
////////// |
| 63 |
|
| 64 |
|
| 65 |
/** |
| 66 |
* CategorizedTagCloudWidget Class |
| 67 |
*/ |
| 68 |
class CategorizedTagCloudWidget extends WP_Widget { |
| 69 |
/** constructor */ |
| 70 |
function CategorizedTagCloudWidget() { |
| 71 |
$this->options = array( |
| 72 |
array( |
| 73 |
'name'=>'title', 'label'=>'Title:', |
| 74 |
'type'=>'text'), |
| 75 |
array( |
| 76 |
'name'=>'words_number', 'label'=>'Number of words to show:', |
| 77 |
'type'=>'text'), |
| 78 |
array( |
| 79 |
'name'=>'words_color', 'label'=>'Word color (random if not entered):', |
| 80 |
'type'=>'text'), |
| 81 |
array( |
| 82 |
'name'=>'hover_color', 'label'=>'Hover color (black if not entered):', |
| 83 |
'type'=>'text'), |
| 84 |
array( |
| 85 |
'name'=>'smallest_font', 'label'=>'Smallest font size (default is 7):', |
| 86 |
'type'=>'text'), |
| 87 |
array( |
| 88 |
'name'=>'largest_font', 'label'=>'Largest font size (default is 14):', |
| 89 |
'type'=>'text'), |
| 90 |
array( |
| 91 |
'label' => __( 'Category filters', 'categorized-tag-cloud' ), |
| 92 |
'type' => 'separator' ), |
| 93 |
array( |
| 94 |
'type' => 'category_filters' ), |
| 95 |
); |
| 96 |
|
| 97 |
$control_ops = array('width' => 500); |
| 98 |
parent::WP_Widget(false, 'Categorized Tag Cloud', array(), $control_ops); |
| 99 |
} |
| 100 |
|
| 101 |
function widget($args, $instance) { |
| 102 |
extract( $args ); |
| 103 |
$title = apply_filters('widget_title', $instance['title']); |
| 104 |
echo $before_widget; |
| 105 |
if ( $title ) echo $before_title . $title . $after_title; |
| 106 |
echo categorized_tag_cloud($instance).$after_widget; |
| 107 |
} |
| 108 |
|
| 109 |
function update($new_instance, $old_instance) { |
| 110 |
$instance = $old_instance; |
| 111 |
|
| 112 |
foreach ($this->options as $val) { |
| 113 |
if ($val['type']=='text') { |
| 114 |
$instance[$val['name']] = strip_tags($new_instance[$val['name']]); |
| 115 |
} else if ($val['type']=='checkbox') { |
| 116 |
$instance[$val['name']] = ($new_instance[$val['name']]=='on') ? true : false; |
| 117 |
} |
| 118 |
|
| 119 |
// CATEGORY FILTERS |
| 120 |
$instance['category_filters'] = ''; |
| 121 |
|
| 122 |
if (is_numeric($_POST['categorized-tag-cloud-num-filters'])) { |
| 123 |
$instance['category_filters'] = array(); |
| 124 |
for ($i=0; $i<$_POST['categorized-tag-cloud-num-filters']; $i++) { |
| 125 |
if ($_POST['categorized-tag-cloud-cat-'.$i] == '' && $_POST['categorized-tag-cloud-tag-'.$i] == '') { |
| 126 |
continue; |
| 127 |
} |
| 128 |
$instance['category_filters']['cat'][] = esc_html($_POST['categorized-tag-cloud-cat-'.$i]); |
| 129 |
$instance['category_filters']['tag'][] = esc_html($_POST['categorized-tag-cloud-tag-'.$i]); |
| 130 |
} |
| 131 |
$instance['category_filters'] = json_encode($instance['category_filters']); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
return $instance; |
| 136 |
} |
| 137 |
|
| 138 |
function form($instance) { |
| 139 |
if (empty($instance)) { |
| 140 |
$instance['title'] = 'Categorized Tag Cloud'; |
| 141 |
$instance['words_number'] = 20; |
| 142 |
$instance['words_color'] = ''; |
| 143 |
$instance['smallest_font'] = '7'; |
| 144 |
$instance['largest_font'] = '14'; |
| 145 |
$instance['horizontal_spread'] = '60'; |
| 146 |
$instance['vertical_spread'] = '60'; |
| 147 |
} |
| 148 |
|
| 149 |
foreach ($this->options as $val) { |
| 150 |
if ($val['type']=='separator') { |
| 151 |
if (isset($val['label']) && $val['label']!='') { |
| 152 |
echo '<h3>'.$val['label'].'</h3>'; |
| 153 |
} else { |
| 154 |
echo '<hr />'; |
| 155 |
} |
| 156 |
if (isset($val['notes']) && $val['notes']!='') { |
| 157 |
echo '<div class="description">'.$val['notes'].'</div>'; |
| 158 |
} |
| 159 |
} else if ($val['type']=='category_filters') { |
| 160 |
$category_filters = (array)json_decode($instance['category_filters']); |
| 161 |
echo '<input name="categorized-tag-cloud-num-filters" type="hidden" value="'.(count($category_filters)+2).'" />'; |
| 162 |
echo '<table> |
| 163 |
<tr><th>'.__('category slug', 'categorized-tag-cloud').'</th><th style="min-width:300px">'.__('excluded tags id, comma separated', 'categorized-tag-cloud').'</th></tr>'; |
| 164 |
for ($i=0; $i<count($category_filters['cat']); $i++) { |
| 165 |
echo ' |
| 166 |
<tr> |
| 167 |
<td><input type="text" name="categorized-tag-cloud-cat-'.$i.'" value="'.$category_filters['cat'][$i].'" /></td> |
| 168 |
<td><input type="text" name="categorized-tag-cloud-tag-'.$i.'" value="'.$category_filters['tag'][$i].'" style="min-width:300px" /></td></tr>'; |
| 169 |
} |
| 170 |
for ($j=$i; $j<($i+2); $j++) { |
| 171 |
echo ' |
| 172 |
<tr> |
| 173 |
<td><input type="text" name="categorized-tag-cloud-cat-'.$j.'" /></td> |
| 174 |
<td><input type="text" name="categorized-tag-cloud-tag-'.$j.'" style="min-width:300px" /></td></tr>'; |
| 175 |
} |
| 176 |
echo '</table>'; |
| 177 |
} else if ($val['type']=='text') { |
| 178 |
echo '<p> |
| 179 |
<label for="'.$this->get_field_id($val['name']).'">'.__($val['label']).'</label> |
| 180 |
'; |
| 181 |
echo '<input class="widefat" id="'.$this->get_field_id($val['name']).'" name="'.$this->get_field_name($val['name']).'" type="text" value="'.esc_attr($instance[$val['name']]).'" />'; |
| 182 |
echo '</p>'; |
| 183 |
} else if ($val['type']=='checkbox') { |
| 184 |
echo '<p> |
| 185 |
<label for="'.$this->get_field_id($val['name']).'">'.__($val['label']).'</label> |
| 186 |
'; |
| 187 |
$checked = ($instance[$val['name']]) ? 'checked="checked"' : ''; |
| 188 |
echo '<input id="'.$this->get_field_id($val['name']).'" name="'.$this->get_field_name($val['name']).'" type="checkbox" '.$checked.' />'; |
| 189 |
echo '</p>'; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
} // class CategorizedTagCloudWidget |
| 197 |
|
| 198 |
// register CategorizedTagCloudWidget widget |
| 199 |
add_action('widgets_init', create_function('', 'return register_widget("CategorizedTagCloudWidget");')); |
| 200 |
|