| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Categorized Tag Cloud |
| 4 |
Plugin URI: https://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.2.25 |
| 8 |
Author URI: https://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 |
|
| 25 |
$plugin_name = 'categorized-tag-cloud'; |
| 26 |
|
| 27 |
// RETRIEVE TAGS |
| 28 |
$words_color = $instance['words_color']; |
| 29 |
$hover_color = (isset($instance['hover_color']) && $instance['hover_color'] != '') ? $instance['hover_color'] : 'black'; |
| 30 |
$number = (isset($instance['words_number']) && is_numeric($instance['words_number']) && $instance['words_number'] > 0) ? $instance['words_number'] : 20; |
| 31 |
$smallest_font = (isset($instance['smallest_font']) && is_numeric($instance['smallest_font']) && $instance['smallest_font'] > 0) ? $instance['smallest_font'] : 7; |
| 32 |
$largest_font = (isset($instance['largest_font']) && is_numeric($instance['largest_font']) && $instance['largest_font'] > 0) ? $instance['largest_font'] : 14; |
| 33 |
$order = (isset($instance['order']) && $instance['order'] == 'a') ? 'ASC' : 'RAND'; |
| 34 |
|
| 35 |
$exclude_items = []; |
| 36 |
$category_filters = (array) json_decode($instance['category_filters']); |
| 37 |
if (!isset($category_filters['cat']) || !is_array($category_filters['cat'])) { |
| 38 |
$category_filters['cat'] = []; |
| 39 |
} |
| 40 |
for ($i = 0; $i < count($category_filters['cat']); $i++) { |
| 41 |
if (is_category($category_filters['cat'][$i]) || (is_single() && in_category($category_filters['cat'][$i]))) { |
| 42 |
$exclude_items[] = $category_filters['tag'][$i]; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
$tags = wp_tag_cloud('smallest=' . $smallest_font . '&largest=' . $largest_font . '&number=' . $number . '&order=' . $order . '&format=array&exclude=' . implode(',', $exclude_items)); |
| 47 |
|
| 48 |
$out = ''; |
| 49 |
$out_style = ' |
| 50 |
#' . $plugin_name . ' a, #' . $plugin_name . ' a:visited { text-decoration:none; } |
| 51 |
#' . $plugin_name . ' a:hover { text-decoration:none; color:' . $hover_color . '; }'; |
| 52 |
if (is_array($tags)) { |
| 53 |
foreach ($tags as $num => $tag) { |
| 54 |
$i = $num + 1; |
| 55 |
$out .= '<span id="' . $plugin_name . '-el-' . $i . '">' . $tag . '</span> '; |
| 56 |
|
| 57 |
$the_color = ($words_color == '') ? '#' . str_pad(dechex(rand(0, 4096)), 3, '0', STR_PAD_LEFT) : $words_color; |
| 58 |
$out_style .= ' |
| 59 |
#' . $plugin_name . '-el-' . $i . ' a, #' . $plugin_name . '-el-' . $i . ' a:visited { color:' . $the_color . '; }'; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return ' |
| 64 |
<div id="' . $plugin_name . '">' . $out . '</div> |
| 65 |
<style> |
| 66 |
' . $out_style . ' |
| 67 |
</style>'; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
////////// |
| 72 |
|
| 73 |
|
| 74 |
/** |
| 75 |
* CategorizedTagCloudWidget Class |
| 76 |
*/ |
| 77 |
class CategorizedTagCloudWidget extends WP_Widget |
| 78 |
{ |
| 79 |
private |
| 80 |
/** @type {string} */ |
| 81 |
$languagePath; |
| 82 |
|
| 83 |
/** constructor */ |
| 84 |
function __construct() |
| 85 |
{ |
| 86 |
$this->languagePath = basename(dirname(__FILE__)) . '/lang'; |
| 87 |
load_plugin_textdomain('categorized-tag-cloud', 'false', $this->languagePath); |
| 88 |
|
| 89 |
$this->options = [ |
| 90 |
[ |
| 91 |
'name' => 'title', 'label' => 'Title:', |
| 92 |
'type' => 'text' |
| 93 |
], |
| 94 |
[ |
| 95 |
'name' => 'words_number', 'label' => 'How many tags to show:', |
| 96 |
'type' => 'text' |
| 97 |
], |
| 98 |
[ |
| 99 |
'name' => 'words_color', 'label' => 'Word color (random if not entered):', |
| 100 |
'type' => 'text' |
| 101 |
], |
| 102 |
[ |
| 103 |
'name' => 'hover_color', 'label' => 'Hover color (black if not entered):', |
| 104 |
'type' => 'text' |
| 105 |
], |
| 106 |
[ |
| 107 |
'name' => 'smallest_font', 'label' => 'Smallest font size (default is 7):', |
| 108 |
'type' => 'text' |
| 109 |
], |
| 110 |
[ |
| 111 |
'name' => 'largest_font', 'label' => 'Largest font size (default is 14):', |
| 112 |
'type' => 'text' |
| 113 |
], |
| 114 |
[ |
| 115 |
'name' => 'order', 'label' => 'Order:', |
| 116 |
'type' => 'radio', 'values' => ['' => 'Random', 'a' => 'Alphanumeric'], |
| 117 |
], |
| 118 |
[ |
| 119 |
'label' => 'Category filters', |
| 120 |
'type' => 'separator' |
| 121 |
], |
| 122 |
[ |
| 123 |
'type' => 'category_filters' |
| 124 |
], |
| 125 |
[ |
| 126 |
'type' => 'donate' |
| 127 |
], |
| 128 |
]; |
| 129 |
|
| 130 |
$control_ops = ['width' => 500]; |
| 131 |
parent::__construct(false, 'Categorized Tag Cloud', [], $control_ops); |
| 132 |
} |
| 133 |
|
| 134 |
function widget($args, $instance) |
| 135 |
{ |
| 136 |
extract($args); |
| 137 |
$title = apply_filters('widget_title', $instance['title']); |
| 138 |
echo $before_widget; |
| 139 |
if ($title) echo $before_title . $title . $after_title; |
| 140 |
echo categorized_tag_cloud($instance) . $after_widget; |
| 141 |
} |
| 142 |
|
| 143 |
function update($new_instance, $old_instance) |
| 144 |
{ |
| 145 |
$instance = $old_instance; |
| 146 |
|
| 147 |
foreach ($this->options as $val) { |
| 148 |
if ($val['type'] == 'text' || $val['type'] == 'radio') { |
| 149 |
$instance[$val['name']] = strip_tags($new_instance[$val['name']]); |
| 150 |
} else if ($val['type'] == 'checkbox') { |
| 151 |
$instance[$val['name']] = ($new_instance[$val['name']] == 'on') ? true : false; |
| 152 |
} |
| 153 |
|
| 154 |
// CATEGORY FILTERS |
| 155 |
$instance['category_filters'] = ''; |
| 156 |
|
| 157 |
if (isset($_POST['categorized-tag-cloud-num-filters']) && is_numeric($_POST['categorized-tag-cloud-num-filters'])) { |
| 158 |
$instance['category_filters'] = []; |
| 159 |
for ($i = 0; $i < $_POST['categorized-tag-cloud-num-filters']; $i++) { |
| 160 |
if ($_POST['categorized-tag-cloud-cat-' . $i] == '' && $_POST['categorized-tag-cloud-tag-' . $i] == '') { |
| 161 |
continue; |
| 162 |
} |
| 163 |
$instance['category_filters']['cat'][] = esc_html($_POST['categorized-tag-cloud-cat-' . $i]); |
| 164 |
$instance['category_filters']['tag'][] = esc_html($_POST['categorized-tag-cloud-tag-' . $i]); |
| 165 |
} |
| 166 |
$instance['category_filters'] = json_encode($instance['category_filters']); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
return $instance; |
| 171 |
} |
| 172 |
|
| 173 |
function form($instance) |
| 174 |
{ |
| 175 |
if (empty($instance)) { |
| 176 |
$instance['title'] = 'Categorized Tag Cloud'; |
| 177 |
$instance['words_number'] = 20; |
| 178 |
$instance['words_color'] = ''; |
| 179 |
$instance['smallest_font'] = '7'; |
| 180 |
$instance['largest_font'] = '14'; |
| 181 |
$instance['horizontal_spread'] = '60'; |
| 182 |
$instance['vertical_spread'] = '60'; |
| 183 |
$instance['order'] = ''; |
| 184 |
} |
| 185 |
|
| 186 |
foreach ($this->options as $val) { |
| 187 |
if ($val['type'] == 'separator') { |
| 188 |
if (isset($val['label']) && $val['label'] != '') { |
| 189 |
echo '<h3>' . __($val['label'], 'categorized-tag-cloud') . '</h3>'; |
| 190 |
} else { |
| 191 |
echo '<hr />'; |
| 192 |
} |
| 193 |
if (isset($val['notes']) && $val['notes'] != '') { |
| 194 |
echo '<div class="description">' . $val['notes'] . '</div>'; |
| 195 |
} |
| 196 |
} else if ($val['type'] == 'category_filters') { |
| 197 |
$category_filters = (array) json_decode($instance['category_filters']); |
| 198 |
if (!is_array($category_filters)) { |
| 199 |
$category_filters = []; |
| 200 |
} |
| 201 |
if (!isset($category_filters['cat']) || !is_array($category_filters['cat'])) { |
| 202 |
$category_filters['cat'] = []; |
| 203 |
} |
| 204 |
echo '<input name="categorized-tag-cloud-num-filters" type="hidden" value="' . (count($category_filters) + 2) . '" />'; |
| 205 |
echo '<table> |
| 206 |
<tr> |
| 207 |
<th>' . __('category slug', 'categorized-tag-cloud') . '</th> |
| 208 |
<th style="min-width:300px">' . __('excluded tags id, comma separated', 'categorized-tag-cloud') . '</th> |
| 209 |
</tr>'; |
| 210 |
for ($i = 0; $i < count($category_filters['cat']); $i++) { |
| 211 |
echo ' |
| 212 |
<tr> |
| 213 |
<td><input type="text" name="categorized-tag-cloud-cat-' . $i . '" value="' . $category_filters['cat'][$i] . '" /></td> |
| 214 |
<td><input type="text" name="categorized-tag-cloud-tag-' . $i . '" value="' . $category_filters['tag'][$i] . '" style="min-width:300px" /></td></tr>'; |
| 215 |
} |
| 216 |
for ($j = $i; $j < ($i + 2); $j++) { |
| 217 |
echo ' |
| 218 |
<tr> |
| 219 |
<td><input type="text" name="categorized-tag-cloud-cat-' . $j . '" /></td> |
| 220 |
<td><input type="text" name="categorized-tag-cloud-tag-' . $j . '" style="min-width:300px" /></td></tr>'; |
| 221 |
} |
| 222 |
echo '</table>'; |
| 223 |
} else if ($val['type'] == 'donate') { |
| 224 |
echo '<p style="text-align:center; font-weight:bold;"> |
| 225 |
' . __('Do you like it? I\'m supporting it, please support me!', 'categorized-tag-cloud') . '<br /> |
| 226 |
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=giu%40formikaio%2eit&item_name=WhileTrue¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted" target="_blank"> |
| 227 |
<img alt="PayPal - The safer, easier way to pay online!" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" > |
| 228 |
</a> |
| 229 |
</p>'; |
| 230 |
} else if ($val['type'] == 'text') { |
| 231 |
echo '<p> |
| 232 |
<label for="' . $this->get_field_id($val['name']) . '">' . __($val['label'], 'categorized-tag-cloud') . '</label> |
| 233 |
'; |
| 234 |
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']]) . '" />'; |
| 235 |
echo '</p>'; |
| 236 |
} else if ($val['type'] == 'radio') { |
| 237 |
echo '<p> |
| 238 |
<label for="' . $this->get_field_id($val['name']) . '">' . __($val['label'], 'categorized-tag-cloud') . '</label> |
| 239 |
'; |
| 240 |
foreach ($val['values'] as $k => $v) { |
| 241 |
$checked = ($instance[$val['name']] == $k) ? 'checked="checked"' : ''; |
| 242 |
echo ' <input id="' . $this->get_field_id($val['name']) . '" name="' . $this->get_field_name($val['name']) . '" value="' . $k . '" type="radio" ' . $checked . ' /> ' . $v . ' '; |
| 243 |
} |
| 244 |
echo '</p>'; |
| 245 |
} else if ($val['type'] == 'checkbox') { |
| 246 |
echo '<p> |
| 247 |
<label for="' . $this->get_field_id($val['name']) . '">' . __($val['label'], 'categorized-tag-cloud') . '</label> |
| 248 |
'; |
| 249 |
$checked = ($instance[$val['name']]) ? 'checked="checked"' : ''; |
| 250 |
echo '<input id="' . $this->get_field_id($val['name']) . '" name="' . $this->get_field_name($val['name']) . '" type="checkbox" ' . $checked . ' />'; |
| 251 |
echo '</p>'; |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
} // class CategorizedTagCloudWidget |
| 256 |
|
| 257 |
// register CategorizedTagCloudWidget widget |
| 258 |
add_action('widgets_init', function () { |
| 259 |
return register_widget("CategorizedTagCloudWidget"); |
| 260 |
}); |
| 261 |
|