forked from surveywp/kk-star-ratings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.php
103 lines (95 loc) · 4.02 KB
/
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
<?php
// Make sure class does not already exist (Playing safe) and that the get function exists
if(!class_exists('BhittaniPlugin_kkStarRatings_Widget') && function_exists('kk_star_ratings_get')) :
class BhittaniPlugin_kkStarRatings_Widget extends WP_Widget
{
// Runs when OBJECT DECLARED (Instanciated)
public function __construct()
{
$widget_options = array(
'classname' => 'kk-star-ratings-widget',
'description' => 'Show top rated posts'
);
parent::__construct('BhittaniPlugin_kkStarRatings_Widget', 'kk Star Ratings', $widget_options);
}
// Outputs USER INTERFACE
public function widget($args, $instance)
{
extract( $args, EXTR_SKIP );
$title = ( !empty($instance['title']) ) ? $instance['title'] : 'Top Posts';
$total = ( !empty($instance['noofposts']) ) ? $instance['noofposts'] : '5';
$category = ( $instance['category'] ) ? $instance['category'] : false;
$sr = ($instance['showrating']) ? true : false;
echo $before_widget;
echo $before_title . $title . $after_title;
// OUTPUT starts
$posts = kk_star_ratings_get($total, $category);
echo '<ul>';
foreach ($posts as $post)
{
echo "<li><a href='".get_permalink($post->ID)."'>".$post->post_title."</a>";
if($sr)
{
$best = get_option('kksr_stars');
echo " <span style='font-size:10px;'>(".$post->ratings."/".$best.")</span>";
}
echo "</li>";
}
echo '</ul>';
// OUTPUT ends
echo $after_widget;
}
// Updates OPTIONS
/*
public function update()
{
}
*/
// The option FORM
public function form( $instance )
{
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Title:
<input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr(!empty($instance['title'])?$instance['title']: 'Top Posts'); ?>" /></label>
</p>
<p>
<label for="<?php echo $this->get_field_id('noofposts'); ?>">No of Posts:
<input id="<?php echo $this->get_field_id('noofposts'); ?>" name="<?php echo $this->get_field_name('noofposts'); ?>" type="text" value="<?php echo esc_attr(!empty($instance['noofposts'])?$instance['noofposts']: '5'); ?>" size="3" /></label>
</p>
<p>
<label for="<?php echo $this->get_field_id('showrating'); ?>">Show Average?:
<select id="<?php echo $this->get_field_id('showrating'); ?>" name="<?php echo $this->get_field_name('showrating'); ?>">
<option value="0" <?php if(isset($instance['showrating']) && !esc_attr($instance['showrating'])){echo "selected='selected'";} ?>>No</option>
<option value="1" <?php if(isset($instance['showrating']) && esc_attr($instance['showrating'])){echo "selected='selected'";} ?>>Yes</option>
</select>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('category'); ?>">Filter by Category:
<select id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>">
<option value="0">Select</option>
<?php
foreach(get_categories(array()) as $category)
{
echo '<option value="'.$category->term_id.'"';
if(isset($instance['category']) && esc_attr($instance['category'])==$category->term_id)
echo ' selected="selected"';
echo '>'.$category->name.'</option>';
}
?>
</select>
</label>
</p>
<?php
}
}
if(!function_exists('kk_star_ratings_widget_init'))
{
function kk_star_ratings_widget_init()
{
register_widget('BhittaniPlugin_kkStarRatings_Widget');
}
add_action('widgets_init', 'kk_star_ratings_widget_init');
}
endif;