/
hot-topics-widget.php
120 lines (113 loc) · 3.91 KB
/
hot-topics-widget.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
<?php
/*
* Plugin Name: Hot Topics Widget
* Plugin URI: http://github.com/BryanH/Hot-Topics-Widget
* Description: Displays a clickable Hot Topics list
* Version: 1.10
* Author: Bryan Hanks, PMP
* Author URI: http://www.chron.com/apps/adbxh0/wordpress/
* License: GPLv3
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class Widget_Hot_Topics extends WP_Widget {
function Widget_Hot_Topics() {
/* Widget settings. */
$widget_ops = array (
'classname' => 'hot_topics',
'description' => __('Displays a clickable Hot Topics list', 'hot_topics')
);
$control_ops = array (
'width' => 150,
'height' => 150,
'id_base' => 'hot_topics'
);
$this->WP_Widget('hot_topics', __('Hot Topics', 'hot_topics'), $widget_ops, $control_ops);
}
/**
* Public View
*/
function widget($args, $instance) {
extract($args);
/* Our variables from the widget settings. */
$title = apply_filters('widget_title', $instance['title']);
$topics = $instance['topics'];
/* REQUIRED */
echo $before_widget;
/* 'before' and 'after' are REQUIRED */
if ($title) {
echo $before_title . $title . $after_title . ' ';
}
/* Display array separated by delimiter
* TODO: make delimiter a configurable option?
* */
if ($topics && isset ($topics)) {
foreach ($topics as & $topic) {
$category_id = get_cat_ID($topic);
if (0 < $category_id) {
// modified the category link for the niche sites "site category" functionality
// @TODO: this should change in the future as we fix the niche site theme
$category = get_category($category_id);
$category_link = get_bloginfo('url') . '/category/' . $category->slug ;
$topic = '<a href="' . $category_link . '" title="' . $topic . '">' . $topic . '</a>';
} else {
// topic is not a link
}
}
echo implode(' | ', $topics);
}
/* REQUIRED */
echo $after_widget;
}
/**
* Save/update settings
*/
function update($new_instance, $old_instance) {
$instance = $old_instance;
/* Strip tags for title and name to remove HTML (important for text inputs). */
$instance['title'] = strip_tags(trim($new_instance['title']));
$topics = explode("\n", strip_tags(trim($new_instance['topics'])));
$instance['topics'] = $topics;
return $instance;
}
/**
* Widget options form
*/
function form($instance) {
$defaults = array (
'title' => __('Hot Topics', 'hot_topics'),
'topics' => array (
"General",
"Featured",
"News"
)
);
$instance = wp_parse_args((array) $instance, $defaults);
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Caption:', 'hybrid'); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'topics' ); ?>"><?php _e('Topics (one per line):', 'hot_topics'); ?></label><br />
<textarea id="<?php echo $this->get_field_id( 'topics' ); ?>" name="<?php echo $this->get_field_name( 'topics' ); ?>" rows="6" cols="15" style="width:100%"><?php echo implode("\n", $instance['topics'] ); ?></textarea>
</p>
<?php
}
/**
* Register widget
*/
function register() {
register_widget('Widget_Hot_Topics');
}
}
add_action('widgets_init', array ('Widget_Hot_Topics', 'register'));
?>