| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* TaxoPress widget class |
| 5 |
* |
| 6 |
*/ |
| 7 |
class SimpleTags_Widget extends WP_Widget { |
| 8 |
/** |
| 9 |
* Constructor widget |
| 10 |
* |
| 11 |
* @return void |
| 12 |
* @author WebFactory Ltd |
| 13 |
*/ |
| 14 |
public function __construct() { |
| 15 |
parent::__construct( 'simpletags', esc_html__( 'Tag Cloud (TaxoPress Legacy)', 'simple-tags' ), |
| 16 |
array( |
| 17 |
'classname' => 'widget-simpletags', |
| 18 |
'description' => esc_html__( '[DEPRECATED] - Your most used tags in cloud format with dynamic color and many options', 'simple-tags' ) |
| 19 |
) |
| 20 |
); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Check if taxonomy exist and return it, otherwise return default post tags. |
| 25 |
* |
| 26 |
* @param array $instance |
| 27 |
* |
| 28 |
* @return string |
| 29 |
* @author WebFactory Ltd |
| 30 |
*/ |
| 31 |
public static function _get_current_taxonomy( $instance ) { |
| 32 |
if ( ! empty( $instance['taxonomy'] ) && taxonomy_exists( $instance['taxonomy'] ) ) { |
| 33 |
return $instance['taxonomy']; |
| 34 |
} |
| 35 |
|
| 36 |
return 'post_tag'; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Default settings for widget |
| 41 |
* |
| 42 |
* @return array |
| 43 |
* @author WebFactory Ltd |
| 44 |
*/ |
| 45 |
public static function get_fields() { |
| 46 |
return array( |
| 47 |
'taxonomy' => 'post_tag', |
| 48 |
'title' => esc_html__( 'Tag cloud', 'simple-tags' ), |
| 49 |
'max' => 45, |
| 50 |
'selectionby' => 'count', |
| 51 |
'selection' => 'desc', |
| 52 |
'orderby' => 'random', |
| 53 |
'order' => 'asc', |
| 54 |
'smini' => 8, |
| 55 |
'smax' => 22, |
| 56 |
'unit' => 'pt', |
| 57 |
'format' => 'flat', |
| 58 |
'color' => 1, |
| 59 |
'cmini' => '#CCCCCC', |
| 60 |
'cmax' => '#000000', |
| 61 |
'xformat' => '', |
| 62 |
'adv_usage' => '' |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Method for theme render |
| 68 |
* |
| 69 |
* @param array $args |
| 70 |
* @param array $instance |
| 71 |
* |
| 72 |
* @return void |
| 73 |
* @author WebFactory Ltd |
| 74 |
*/ |
| 75 |
public function widget( $args, $instance ) { |
| 76 |
extract( $args ); |
| 77 |
|
| 78 |
$current_taxonomy = self::_get_current_taxonomy( $instance ); |
| 79 |
|
| 80 |
// Build or not the name of the widget |
| 81 |
if ( ! empty( $instance['title'] ) ) { |
| 82 |
$title = $instance['title']; |
| 83 |
} else { |
| 84 |
if ( 'post_tag' == $current_taxonomy ) { |
| 85 |
$title = esc_html__( 'Tags', 'simple-tags' ); |
| 86 |
} else { |
| 87 |
$tax = get_taxonomy( $current_taxonomy ); |
| 88 |
if ( isset( $tax->labels ) ) { |
| 89 |
$title = $tax->labels->name; |
| 90 |
} else { |
| 91 |
$title = $tax->name; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
| 96 |
|
| 97 |
// Set values and clean it |
| 98 |
foreach ( (array) self::get_fields() as $field => $field_value ) { |
| 99 |
${$field} = isset($instance[$field]) ? trim($instance[$field]) : ''; |
| 100 |
} |
| 101 |
|
| 102 |
$param = ''; |
| 103 |
|
| 104 |
// Selection |
| 105 |
$param .= ( ! empty( $selectionby ) ) ? '&selectionby=' . $selectionby : '&selectionby=count'; |
| 106 |
$param .= ( ! empty( $selection ) ) ? '&selection=' . $selection : '&selection=desc'; |
| 107 |
|
| 108 |
// Order |
| 109 |
$param .= ( ! empty( $orderby ) ) ? '&orderby=' . $orderby : '&orderby=random'; |
| 110 |
$param .= ( ! empty( $order ) ) ? '&order=' . $order : '&order=asc'; |
| 111 |
|
| 112 |
// Max tags |
| 113 |
if ( (int) $max != 0 ) { |
| 114 |
$param .= '&number=' . $max; |
| 115 |
} |
| 116 |
|
| 117 |
// Size Mini |
| 118 |
if ( (int) $smini != 0 ) { |
| 119 |
$param .= '&smallest=' . $smini; |
| 120 |
} |
| 121 |
|
| 122 |
// Size Maxi |
| 123 |
if ( (int) $smax != 0 ) { |
| 124 |
$param .= '&largest=' . $smax; |
| 125 |
} |
| 126 |
|
| 127 |
// Unit |
| 128 |
if ( ! empty( $unit ) ) { |
| 129 |
$param .= '&unit=' . $unit; |
| 130 |
} |
| 131 |
|
| 132 |
// Format |
| 133 |
if ( ! empty( $format ) ) { |
| 134 |
$param .= '&format=' . $format; |
| 135 |
} |
| 136 |
|
| 137 |
// Use color ? |
| 138 |
if ( (int) $color == 0 ) { |
| 139 |
$param .= '&color=false'; |
| 140 |
} |
| 141 |
|
| 142 |
// Color mini |
| 143 |
if ( ! empty( $cmini ) ) { |
| 144 |
$param .= '&mincolor=' . $cmini; |
| 145 |
} |
| 146 |
|
| 147 |
// Color Max |
| 148 |
if ( ! empty( $cmax ) ) { |
| 149 |
$param .= '&maxcolor=' . $cmax; |
| 150 |
} |
| 151 |
|
| 152 |
// Xformat |
| 153 |
if ( ! empty( $xformat ) ) { |
| 154 |
$param .= '&xformat=' . $xformat; |
| 155 |
} |
| 156 |
|
| 157 |
// Advanced usage |
| 158 |
if ( ! empty( $adv_usage ) ) { |
| 159 |
if ( substr( $adv_usage, 0, 1 ) != '&' ) { |
| 160 |
$param .= '&'; |
| 161 |
} |
| 162 |
|
| 163 |
$param .= $adv_usage; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
// Taxonomy |
| 168 |
$param .= '&taxonomy=' . $current_taxonomy; |
| 169 |
|
| 170 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 171 |
echo $before_widget; |
| 172 |
if ( ! empty( $title ) ) { |
| 173 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 174 |
echo $before_title . $title . $after_title; |
| 175 |
} |
| 176 |
st_tag_cloud( apply_filters( 'simple-tags-widget', 'title=' . $param, $instance ) ); // Use Widgets title and no ST title !! |
| 177 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 178 |
echo $after_widget; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Update widget settings |
| 183 |
* |
| 184 |
* @param array $new_instance |
| 185 |
* @param array $old_instance |
| 186 |
* |
| 187 |
* @return array |
| 188 |
* @author WebFactory Ltd |
| 189 |
*/ |
| 190 |
public function update( $new_instance, $old_instance ) { |
| 191 |
$instance = $old_instance; |
| 192 |
|
| 193 |
foreach ( (array) self::get_fields() as $field => $field_value ) { |
| 194 |
$instance[ $field ] = $new_instance[ $field ]; |
| 195 |
} |
| 196 |
|
| 197 |
return $instance; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Admin form for widgets |
| 202 |
* |
| 203 |
* @param array $instance |
| 204 |
* |
| 205 |
* @return void |
| 206 |
* @author WebFactory Ltd |
| 207 |
*/ |
| 208 |
public function form( $instance ) { |
| 209 |
//Defaults |
| 210 |
$instance = wp_parse_args( (array) $instance, self::get_fields() ); |
| 211 |
?> |
| 212 |
<p style="color:red;"><?php esc_html_e( 'This widget is no longer being updated. Please use the "Tag Cloud (TaxoPress Shortcode)" widget instead.', 'simple-tags' ); ?></p> |
| 213 |
|
| 214 |
<p> |
| 215 |
<label for="<?php echo esc_attr($this->get_field_id( 'title' )); ?>"> |
| 216 |
<?php esc_html_e( 'Title:', 'simple-tags' ); ?> |
| 217 |
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id( 'title' )); ?>" |
| 218 |
name="<?php echo esc_attr($this->get_field_name( 'title' )); ?>" |
| 219 |
value="<?php echo esc_attr( $instance['title'] ); ?>"/> |
| 220 |
</label> |
| 221 |
</p> |
| 222 |
|
| 223 |
<p> |
| 224 |
<label for="<?php echo esc_attr($this->get_field_id( 'max' )); ?>"> |
| 225 |
<?php esc_html_e( 'Max tags to display: (default: 45)', 'simple-tags' ); ?> |
| 226 |
<input class="widefat" size="20" type="text" id="<?php echo esc_attr($this->get_field_id( 'max' )); ?>" |
| 227 |
name="<?php echo esc_attr($this->get_field_name( 'max' )); ?>" |
| 228 |
value="<?php echo esc_attr( $instance['max'] ); ?>"/> |
| 229 |
</label> |
| 230 |
</p> |
| 231 |
|
| 232 |
<p> |
| 233 |
<label for="<?php echo esc_attr($this->get_field_id( 'selectionby' )); ?>"> |
| 234 |
<?php esc_html_e( 'Order by for DB selection tags:', 'simple-tags' ); ?> |
| 235 |
<select id="<?php echo esc_attr($this->get_field_id( 'selectionby' )); ?>" |
| 236 |
name="<?php echo esc_attr($this->get_field_name( 'selectionby' )); ?>"> |
| 237 |
<option <?php selected( esc_attr($instance['selectionby']), 'name' ); ?> |
| 238 |
value="name"><?php esc_html_e( 'Name', 'simple-tags' ); ?></option> |
| 239 |
<option <?php selected( $instance['selectionby'], 'slug' ); ?> |
| 240 |
value="slug"><?php esc_html_e( 'Slug', 'simple-tags' ); ?></option> |
| 241 |
<option <?php selected( $instance['selectionby'], 'term_group' ); ?> |
| 242 |
value="term_group"><?php esc_html_e( 'Term group', 'simple-tags' ); ?></option> |
| 243 |
<option <?php selected( $instance['selectionby'], 'count' ); ?> |
| 244 |
value="count"><?php esc_html_e( 'Counter (default)', 'simple-tags' ); ?></option> |
| 245 |
<option <?php selected( $instance['selectionby'], 'random' ); ?> |
| 246 |
value="random"><?php esc_html_e( 'Random', 'simple-tags' ); ?></option> |
| 247 |
</select> |
| 248 |
</label> |
| 249 |
</p> |
| 250 |
|
| 251 |
<p> |
| 252 |
<label for="<?php echo esc_attr($this->get_field_id( 'selection' )); ?>"> |
| 253 |
<?php esc_html_e( 'Order for DB selection tags:', 'simple-tags' ); ?> |
| 254 |
<select id="<?php echo esc_attr($this->get_field_id( 'selection' )); ?>" |
| 255 |
name="<?php echo esc_attr($this->get_field_name( 'selection' )); ?>"> |
| 256 |
<option <?php selected( $instance['selection'], 'asc' ); ?> |
| 257 |
value="asc"><?php esc_html_e( 'ASC', 'simple-tags' ); ?></option> |
| 258 |
<option <?php selected( $instance['selection'], 'desc' ); ?> |
| 259 |
value="desc"><?php esc_html_e( 'DESC (default)', 'simple-tags' ); ?></option> |
| 260 |
</select> |
| 261 |
</label> |
| 262 |
</p> |
| 263 |
|
| 264 |
<p> |
| 265 |
<label for="<?php echo esc_attr($this->get_field_id( 'orderby' )); ?>"> |
| 266 |
<?php esc_html_e( 'Order by for display tags:', 'simple-tags' ); ?> |
| 267 |
<select id="<?php echo esc_attr($this->get_field_id( 'orderby' )); ?>" |
| 268 |
name="<?php echo esc_attr($this->get_field_name( 'orderby' )); ?>"> |
| 269 |
<option <?php selected( esc_attr($instance['orderby']), 'name' ); ?> |
| 270 |
value="name"><?php esc_html_e( 'Name', 'simple-tags' ); ?></option> |
| 271 |
<option <?php selected( esc_attr($instance['orderby']), 'count' ); ?> |
| 272 |
value="count"><?php esc_html_e( 'Counter', 'simple-tags' ); ?></option> |
| 273 |
<option <?php selected( esc_attr($instance['orderby']), 'random' ); ?> |
| 274 |
value="random"><?php esc_html_e( 'Random (default)', 'simple-tags' ); ?></option> |
| 275 |
</select> |
| 276 |
</label> |
| 277 |
</p> |
| 278 |
|
| 279 |
<p> |
| 280 |
<label for="<?php echo esc_attr($this->get_field_id( 'order' )); ?>"> |
| 281 |
<?php esc_html_e( 'Order for display tags:', 'simple-tags' ); ?> |
| 282 |
<select id="<?php echo esc_attr($this->get_field_id( 'order' )); ?>" |
| 283 |
name="<?php echo esc_attr($this->get_field_name( 'order' )); ?>"> |
| 284 |
<option <?php selected( esc_attr($instance['order']), 'asc' ); ?> |
| 285 |
value="asc"><?php esc_html_e( 'ASC', 'simple-tags' ); ?></option> |
| 286 |
<option <?php selected( esc_attr($instance['order']), 'desc' ); ?> |
| 287 |
value="desc"><?php esc_html_e( 'DESC (default)', 'simple-tags' ); ?></option> |
| 288 |
</select> |
| 289 |
</label> |
| 290 |
</p> |
| 291 |
|
| 292 |
<p> |
| 293 |
<label for="<?php echo esc_attr($this->get_field_id( 'smini' )); ?>"> |
| 294 |
<?php esc_html_e( 'Font size mini: (default: 8)', 'simple-tags' ); ?> |
| 295 |
<input class="widefat" size="20" type="text" id="<?php echo esc_attr($this->get_field_id( 'smini' )); ?>" |
| 296 |
name="<?php echo esc_attr($this->get_field_name( 'smini' )); ?>" |
| 297 |
value="<?php echo esc_attr( $instance['smini'] ); ?>"/> |
| 298 |
</label> |
| 299 |
</p> |
| 300 |
|
| 301 |
<p> |
| 302 |
<label for="<?php echo esc_attr($this->get_field_id( 'smax' )); ?>"> |
| 303 |
<?php esc_html_e( 'Font size max: (default: 22)', 'simple-tags' ); ?> |
| 304 |
<input class="widefat" size="20" type="text" id="<?php echo esc_attr($this->get_field_id( 'smax' )); ?>" |
| 305 |
name="<?php echo esc_attr($this->get_field_name( 'smax' )); ?>" |
| 306 |
value="<?php echo esc_attr( $instance['smax'] ); ?>"/> |
| 307 |
</label> |
| 308 |
</p> |
| 309 |
|
| 310 |
<p> |
| 311 |
<label for="<?php echo esc_attr($this->get_field_id( 'unit' )); ?>"> |
| 312 |
<?php esc_html_e( 'Unit font size:', 'simple-tags' ); ?> |
| 313 |
<select id="<?php echo esc_attr($this->get_field_id( 'unit' )); ?>" |
| 314 |
name="<?php echo esc_attr($this->get_field_name( 'unit' )); ?>"> |
| 315 |
<option <?php selected( esc_attr($instance['unit']), 'pt' ); ?> |
| 316 |
value="pt"><?php esc_html_e( 'Point (default)', 'simple-tags' ); ?></option> |
| 317 |
<option <?php selected( esc_attr($instance['unit']), 'px' ); ?> |
| 318 |
value="px"><?php esc_html_e( 'Pixel', 'simple-tags' ); ?></option> |
| 319 |
<option <?php selected( esc_attr($instance['unit']), 'em' ); ?> |
| 320 |
value="em"><?php esc_html_e( 'Em', 'simple-tags' ); ?></option> |
| 321 |
<option <?php selected( esc_attr($instance['unit']), '%' ); ?> |
| 322 |
value="%"><?php esc_html_e( 'Pourcent', 'simple-tags' ); ?></option> |
| 323 |
</select> |
| 324 |
</label> |
| 325 |
</p> |
| 326 |
|
| 327 |
<p> |
| 328 |
<label for="<?php echo esc_attr($this->get_field_id( 'format' )); ?>"> |
| 329 |
<?php esc_html_e( 'Format:', 'simple-tags' ); ?> |
| 330 |
<select id="<?php echo esc_attr($this->get_field_id( 'format' )); ?>" |
| 331 |
name="<?php echo esc_attr($this->get_field_name( 'format' )); ?>"> |
| 332 |
<option <?php selected( esc_attr($instance['format']), 'flat' ); ?> |
| 333 |
value="flat"><?php esc_html_e( 'Flat (default)', 'simple-tags' ); ?></option> |
| 334 |
<option <?php selected( esc_attr($instance['format']), 'list' ); ?> |
| 335 |
value="list"><?php esc_html_e( 'List (UL/LI)', 'simple-tags' ); ?></option> |
| 336 |
</select> |
| 337 |
</label> |
| 338 |
</p> |
| 339 |
|
| 340 |
<p> |
| 341 |
<label for="<?php echo esc_attr($this->get_field_id( 'color' )); ?>"> |
| 342 |
<input class="checkbox" type="checkbox" id="<?php echo esc_attr($this->get_field_id( 'color' )); ?>" |
| 343 |
name="<?php echo esc_attr($this->get_field_name( 'color' )); ?>" <?php checked( (int) $instance['color'], 1 ); ?> |
| 344 |
value="1"/> |
| 345 |
<?php esc_html_e( 'Use auto color cloud:', 'simple-tags' ); ?> |
| 346 |
</label> |
| 347 |
</p> |
| 348 |
|
| 349 |
<p> |
| 350 |
<label for="<?php echo esc_attr($this->get_field_id( 'cmini' )); ?>"> |
| 351 |
<?php esc_html_e( 'Font color mini: (default: #CCCCCC)', 'simple-tags' ); ?> |
| 352 |
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id( 'cmini' )); ?>" |
| 353 |
name="<?php echo esc_attr($this->get_field_name( 'cmini' )); ?>" |
| 354 |
value="<?php echo esc_attr( $instance['cmini'] ); ?>"/> |
| 355 |
</label> |
| 356 |
</p> |
| 357 |
|
| 358 |
<p> |
| 359 |
<label for="<?php echo esc_attr($this->get_field_id( 'cmax' )); ?>"> |
| 360 |
<?php esc_html_e( 'Font color max: (default: #000000)', 'simple-tags' ); ?> |
| 361 |
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id( 'cmax' )); ?>" |
| 362 |
name="<?php echo esc_attr($this->get_field_name( 'cmax' )); ?>" |
| 363 |
value="<?php echo esc_attr( $instance['cmax'] ); ?>"/> |
| 364 |
</label> |
| 365 |
</p> |
| 366 |
|
| 367 |
<p> |
| 368 |
<label for="<?php echo esc_attr($this->get_field_id( 'xformat' )); ?>"> |
| 369 |
<?php esc_html_e( 'Tag link format:', 'simple-tags' ); ?><br/> |
| 370 |
<input class="widefat" style="width: 100% !important;" type="text" |
| 371 |
id="<?php echo esc_attr($this->get_field_id( 'xformat' )); ?>" |
| 372 |
name="<?php echo esc_attr($this->get_field_name( 'xformat' )); ?>" |
| 373 |
value="<?php echo esc_attr( $instance['xformat'] ); ?>"/> |
| 374 |
</label> |
| 375 |
</p> |
| 376 |
|
| 377 |
<p> |
| 378 |
<label for="<?php echo esc_attr($this->get_field_id( 'adv_usage' )); ?>"> |
| 379 |
<?php esc_html_e( 'Advanced usage:', 'simple-tags' ); ?><br/> |
| 380 |
<input class="adv_usage" style="width: 100% !important;" type="text" |
| 381 |
id="<?php echo esc_attr($this->get_field_id( 'adv_usage' )); ?>" |
| 382 |
name="<?php echo esc_attr($this->get_field_name( 'adv_usage' )); ?>" |
| 383 |
value="<?php echo esc_attr( $instance['adv_usage'] ); ?>"/> |
| 384 |
</label> |
| 385 |
</p> |
| 386 |
|
| 387 |
<p> |
| 388 |
<label for="<?php echo esc_attr($this->get_field_id( 'taxonomy' )); ?>"> |
| 389 |
<?php esc_html_e( "What to show", 'simple-tags' ); ?><br/> |
| 390 |
<select id="<?php echo esc_attr($this->get_field_id( 'taxonomy' )); ?>" |
| 391 |
name="<?php echo esc_attr($this->get_field_name( 'taxonomy' )); ?>" style="width:100%;"> |
| 392 |
<?php |
| 393 |
foreach ( get_object_taxonomies( 'post' ) as $_taxonomy ) { |
| 394 |
$tax = get_taxonomy( $_taxonomy ); |
| 395 |
if ( ! $tax->show_tagcloud || empty( $tax->labels->name ) ) { |
| 396 |
continue; |
| 397 |
} |
| 398 |
|
| 399 |
echo '<option ' . selected( esc_attr($instance['taxonomy']), esc_attr($tax->name), false ) . ' value="' . esc_attr( $tax->name ) . '">' . esc_html( $tax->labels->name ) . '</option>'; |
| 400 |
} |
| 401 |
?> |
| 402 |
</select> |
| 403 |
</label> |
| 404 |
</p> |
| 405 |
<?php |
| 406 |
} |
| 407 |
} |