-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccessible-tag-cloud.php
281 lines (249 loc) · 7.36 KB
/
accessible-tag-cloud.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/**
* Plugin Name: Accessible Tag Cloud
* Plugin URI: https://github.com/sunlix/accessible-tag-cloud
* Description: Accessible Tag Cloud Widget
* Version: 1.4
* Author: Toon Van de Putte | Sven Schüring
* Author URI: http://www.automaton.be/ | http://www.sunlix.de
*/
/**
* Add function to widgets_init that'll load our widget.
* @since 0.1
*/
add_action('widgets_init', 'acc_tag_cloud_load_widgets');
/**
* Register our widget.
*
* @since 0.1
*/
function acc_tag_cloud_load_widgets ()
{
register_widget('Acc_tag_cloud');
}
/*
* function to add css to the hidden parts in the tag cloud
*/
function acc_tag_cloud_hidecss ($styling = 'default', $code = 'class="acc_tag_cloud_screenreader"')
{
if ($styling == 'inline')
{
return 'style="height:0;left:-9000px;position:absolute;width:0;"';
}
elseif ($styling == 'owncode')
{
return $code;
}
else
{
return 'class="acc_tag_cloud_screenreader"';
}
}
/*
* function to add default screenreader class to head
*/
function acc_tag_cloud_defaultcss ()
{
$strHtml = '<style type="text/css">
.acc_tag_cloud_screenreader {
height:0;
left:-9000px;
position:absolute;
width:0;
}
</style>';
echo $strHtml;
}
add_action('wp_head', 'acc_tag_cloud_defaultcss');
/**
* Widget class.
* This class handles everything that needs to be handled with the widget:
* the settings, form, display, and update. Nice!
*
* @since 0.1
*/
class Acc_tag_cloud extends WP_Widget
{
/**
* Widget setup.
*/
function Acc_tag_cloud ()
{
/* Widget settings. */
$widget_ops = array(
'classname' => 'widget_acc_tag_cloud',
'description' => __('Display an accessible tag cloud in the sidebar', 'acc-tag-cloud')
);
/* Widget control settings. */
$control_ops = array(
'width' => 300,
'height' => 350,
'id_base' => 'acc-tag-cloud-widget'
);
/* Create the widget. */
$this->WP_Widget('acc-tag-cloud-widget', __('Accessible Tag Cloud Widget', 'acc-tag-cloud'), $widget_ops, $control_ops);
}
/**
* How to display the widget on the screen.
*/
function widget ($args, $instance)
{
extract($args);
/* Our variables from the widget settings. */
$title = apply_filters('widget_title', $instance['title']);
$smallest = $instance['smallest'];
$biggest = $instance['biggest'];
$unit = $instance['unit'];
$taxonomy = $instance['taxonomy'];
$count = $instance['count'];
if (!$count)
{
$count=10;
}
if (!empty($instance['title']))
{
$title = $instance['title'];
}
else
{
if ('post_tag' == $current_taxonomy)
{
$title = __('Tags');
}
else
{
$tax = get_taxonomy($current_taxonomy);
$title = $tax->label;
}
}
$title = apply_filters('widget_title', $title, $instance, $this->id_base);
$terms = get_terms($taxonomy, array(
'number' => $count,
'orderby' => 'count',
'order' => 'DESC'
));
// wild cloud
asort($terms);
$sizerange = $biggest - $smallest;
$lowestcount = $terms[0]->count;
$highestcount = 0;
foreach ($terms as $term)
{
if ($term->count < $lowestcount)
{
$lowestcount = $term->count;
}
elseif ($term->count > $highestcount)
{
$highestcount = $term->count;
}
}
$countdiff = $highestcount - $lowestcount;
if ($countdiff == 0)
{
foreach ($terms as $term)
{
$term->size = $smallest + ($sizerange / 2);
}
}
else
{
foreach ($terms as $term)
{
$term->size = $smallest + (($term->count - $lowestcount) / $countdiff) * $sizerange;
}
}
$tagcloud = '';
foreach ($terms as $term)
{
$tagcloud .= '<li style="font-size:'. $term->size . $unit .';"><a href="'. get_term_link($term, $taxonomy) .'">';
$tagcloud .= '<span class="hideme">Zum Schlagwort ‘</span>'. $term->name .'<span class="hideme">’ gibt es '. $term->count .' Beiträge</span></a></li> ';
}
// Before widget (defined by themes).
echo $before_widget;
if ($title)
{
echo $before_title . $title . $after_title;
}
echo('<ul class="tagcloud tagcloud-accessible tagcloud-tax-'. $taxonomy .'">');
echo($tagcloud);
echo('</ul>');
// After widget (defined by themes).
echo $after_widget;
}
/**
* Update the widget settings.
*/
function update($new_instance, $old_instance)
{
$instance = $old_instance;
/* Strip tags to remove HTML (important for text inputs). */
$instance['title'] = strip_tags($new_instance['title']);
$instance['smallest'] = strip_tags($new_instance['smallest']);
$instance['biggest'] = strip_tags($new_instance['biggest']);
$instance['taxonomy'] = strip_tags($new_instance['taxonomy']);
$instance['unit'] = strip_tags($new_instance['unit']);
$instance['count'] = strip_tags($new_instance['count']);
return $instance;
}
/**
* Displays the widget settings controls on the widget panel.
* Make use of the get_field_id() and get_field_name() function
* when creating your form elements. This handles the confusing stuff.
*/
function _get_current_taxonomy($instance)
{
if (!empty($instance['taxonomy']) && is_taxonomy($instance['taxonomy']))
{
return $instance['taxonomy'];
}
return 'post_tag';
}
function form( $instance )
{
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php if (isset ( $instance['title'])) {echo esc_attr( $instance['title'] );} ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('smallest'); ?>"><?php _e('Smallest:') ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('smallest'); ?>" name="<?php echo $this->get_field_name('smallest'); ?>" value="<?php echo $instance['smallest']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('biggest'); ?>"><?php _e('Biggest:') ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('biggest'); ?>" name="<?php echo $this->get_field_name('biggest'); ?>" value="<?php echo $instance['biggest']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Number of terms:') ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" value="<?php echo $instance['count']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('unit'); ?>"><?php _e('Size unit:') ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('unit'); ?>" name="<?php echo $this->get_field_name('unit'); ?>">
<option value="em" <?php selected('em', $instance['unit']) ?>>em</option>
<option value="percentage" <?php selected('percentage', $instance['unit']) ?>>percentage</option>
<option value="px" <?php selected('px', $instance['unit']) ?>>px</option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('taxonomy'); ?>"><?php _e('Taxonomy:') ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('taxonomy'); ?>" name="<?php echo $this->get_field_name('taxonomy'); ?>">
<?php
foreach (get_object_taxonomies('post') as $taxonomy) :
$tax = get_taxonomy($taxonomy);
if (!$tax->show_tagcloud || empty($tax->label))
{
continue;
}
?>
<option value="<?php echo esc_attr($taxonomy) ?>" <?php selected(esc_attr($taxonomy), $instance['taxonomy']) ?>><?php echo $tax->label ?></option>
<?php
endforeach;
?>
</select>
</p>
<?php
}
}
?>