kk Star Ratings - Version 1.3

Version Description

  • Added a widget. Now you can show top rated posts in your sidebar :).

=

Download this release

Release Info

Developer bhittani
Plugin Icon 128x128 kk Star Ratings
Version 1.3
Comparing to
See all releases

Code changes from version 1.2 to 1.3

Files changed (6) hide show
  1. ajax/kk-ratings-ajax.php +4 -1
  2. js/kk-ratings.js +18 -10
  3. kk-ratings.php +19 -2
  4. readme.txt +9 -1
  5. screenshot-3.png +0 -0
  6. widget.php +86 -0
ajax/kk-ratings-ajax.php CHANGED
@@ -22,12 +22,14 @@ if(isset($_POST['id']) && ((!strcmp($_POST['op'], 'get') && $Ratings['ratings'])
22
  update_post_meta($_POST['id'], '_kk_ratings_ratings', $Ratings['ratings'] + $_POST['stars']);
23
  update_post_meta($_POST['id'], '_kk_ratings_casts', $Ratings['casts'] + 1);
24
  update_post_meta($_POST['id'], '_kk_ratings_ips', $Ratings['ips'].'|'.$userip);
 
25
  }
26
  else
27
  {
28
  update_post_meta($_POST['id'], '_kk_ratings_ratings', $_POST['stars']);
29
  update_post_meta($_POST['id'], '_kk_ratings_casts', 1);
30
  update_post_meta($_POST['id'], '_kk_ratings_ips', $userip);
 
31
  }
32
  }
33
 
@@ -35,6 +37,7 @@ if(isset($_POST['id']) && ((!strcmp($_POST['op'], 'get') && $Ratings['ratings'])
35
  $Ratings['ratings'] = get_post_meta($_POST['id'], '_kk_ratings_ratings', true);
36
  $Ratings['casts'] = get_post_meta($_POST['id'], '_kk_ratings_casts', true);
37
  $Ratings['ips'] = get_post_meta($_POST['id'], '_kk_ratings_ips', true);
 
38
 
39
  // Percentage
40
  $Ratings['per'] = round((($Ratings['ratings']/$Ratings['casts'])/5)*100);
@@ -44,7 +47,7 @@ if(isset($_POST['id']) && ((!strcmp($_POST['op'], 'get') && $Ratings['ratings'])
44
  $Ratings['open'] = (in_array($userip, $Ip) && $kkratings_options['unique'])? 'no' : 'yes';
45
 
46
  // Legend
47
- $Ratings['avg'] = round($Ratings['ratings']/$Ratings['casts'],1).'/5';
48
  $Ratings['legend'] = $kkratings_options['legend'];
49
  $Ratings['legend'] = str_replace('[total]',$Ratings['casts'], $Ratings['legend']);
50
  $Ratings['legend'] = str_replace('[avg]',$Ratings['avg'], $Ratings['legend']);
22
  update_post_meta($_POST['id'], '_kk_ratings_ratings', $Ratings['ratings'] + $_POST['stars']);
23
  update_post_meta($_POST['id'], '_kk_ratings_casts', $Ratings['casts'] + 1);
24
  update_post_meta($_POST['id'], '_kk_ratings_ips', $Ratings['ips'].'|'.$userip);
25
+ update_post_meta($_POST['id'], '_kk_ratings_avg', round(($Ratings['ratings']/$Ratings['casts']),1));
26
  }
27
  else
28
  {
29
  update_post_meta($_POST['id'], '_kk_ratings_ratings', $_POST['stars']);
30
  update_post_meta($_POST['id'], '_kk_ratings_casts', 1);
31
  update_post_meta($_POST['id'], '_kk_ratings_ips', $userip);
32
+ update_post_meta($_POST['id'], '_kk_ratings_avg', $_POST['stars']);
33
  }
34
  }
35
 
37
  $Ratings['ratings'] = get_post_meta($_POST['id'], '_kk_ratings_ratings', true);
38
  $Ratings['casts'] = get_post_meta($_POST['id'], '_kk_ratings_casts', true);
39
  $Ratings['ips'] = get_post_meta($_POST['id'], '_kk_ratings_ips', true);
40
+ $Ratings['avg'] = get_post_meta($_POST['id'], '_kk_ratings_avg', true).'/5';
41
 
42
  // Percentage
43
  $Ratings['per'] = round((($Ratings['ratings']/$Ratings['casts'])/5)*100);
47
  $Ratings['open'] = (in_array($userip, $Ip) && $kkratings_options['unique'])? 'no' : 'yes';
48
 
49
  // Legend
50
+
51
  $Ratings['legend'] = $kkratings_options['legend'];
52
  $Ratings['legend'] = str_replace('[total]',$Ratings['casts'], $Ratings['legend']);
53
  $Ratings['legend'] = str_replace('[avg]',$Ratings['avg'], $Ratings['legend']);
js/kk-ratings.js CHANGED
@@ -81,15 +81,16 @@ jQuery(document).ready( function($){
81
  }
82
  $('.stars-turned-on', obj).fadeIn(1000);
83
  });
84
-
85
  var id = $("span:eq(0)",obj).html();
 
86
  $(obj).addClass('open');
87
  $.ajax({
88
  type: "POST",
89
  url: settings.path+"ajax/kk-ratings-ajax.php",
90
  data: '_ajax_nonce='+settings.nonce+'&root='+settings.root+"&op=get&id="+id,
91
  success: function(msg){
92
-
93
  msg = msg.replace(/^\s+|\s+$/g,"");
94
  var response = msg.split('|||');
95
  if(response[0]=='SUCCESS')
@@ -107,20 +108,23 @@ jQuery(document).ready( function($){
107
  }, 'slow', function(){
108
  $('.stars-turned-on',obj).animate({
109
  'width' : per+'%'
110
- }, 'slow', function(){
111
-
112
- });
113
  });
114
  }
115
 
116
 
117
  $('.casting-desc',obj).text(legend);
118
-
119
  if(open=='no')
120
  {
121
  $(obj).removeClass('open');
122
  $('.stars-turned-on',obj).addClass('stars-turned-strict');
123
- }
 
 
 
 
 
124
  }
125
  else
126
  {
@@ -140,6 +144,7 @@ jQuery(document).ready( function($){
140
  var starsTT = starsT.split('-');
141
  var stars = parseInt(starsTT[1]);
142
  var id = $("span:eq(0)",obj).text();
 
143
  $.ajax({
144
  type: "POST",
145
  url: settings.path+"ajax/kk-ratings-ajax.php",
@@ -166,6 +171,11 @@ jQuery(document).ready( function($){
166
  obj.removeClass('open');
167
  $('.stars-turned-on',obj).addClass('stars-turned-strict');
168
  }
 
 
 
 
 
169
  }
170
  else
171
  {
@@ -190,9 +200,7 @@ jQuery(document).ready( function($){
190
  }, 'slow', function(){
191
  $('.stars-turned-on',obj).animate({
192
  'width' : percentage+'%'
193
- }, 'slow', function(){
194
-
195
- });
196
  });
197
  }
198
  });
81
  }
82
  $('.stars-turned-on', obj).fadeIn(1000);
83
  });
84
+
85
  var id = $("span:eq(0)",obj).html();
86
+
87
  $(obj).addClass('open');
88
  $.ajax({
89
  type: "POST",
90
  url: settings.path+"ajax/kk-ratings-ajax.php",
91
  data: '_ajax_nonce='+settings.nonce+'&root='+settings.root+"&op=get&id="+id,
92
  success: function(msg){
93
+
94
  msg = msg.replace(/^\s+|\s+$/g,"");
95
  var response = msg.split('|||');
96
  if(response[0]=='SUCCESS')
108
  }, 'slow', function(){
109
  $('.stars-turned-on',obj).animate({
110
  'width' : per+'%'
111
+ }, 'slow');
 
 
112
  });
113
  }
114
 
115
 
116
  $('.casting-desc',obj).text(legend);
117
+
118
  if(open=='no')
119
  {
120
  $(obj).removeClass('open');
121
  $('.stars-turned-on',obj).addClass('stars-turned-strict');
122
+ }
123
+ else
124
+ {
125
+ $(obj).addClass('open');
126
+ $('.stars-turned-on',obj).removeClass('stars-turned-strict');
127
+ }
128
  }
129
  else
130
  {
144
  var starsTT = starsT.split('-');
145
  var stars = parseInt(starsTT[1]);
146
  var id = $("span:eq(0)",obj).text();
147
+
148
  $.ajax({
149
  type: "POST",
150
  url: settings.path+"ajax/kk-ratings-ajax.php",
171
  obj.removeClass('open');
172
  $('.stars-turned-on',obj).addClass('stars-turned-strict');
173
  }
174
+ else
175
+ {
176
+ obj.addClass('open');
177
+ $('.stars-turned-on',obj).removeClass('stars-turned-strict');
178
+ }
179
  }
180
  else
181
  {
200
  }, 'slow', function(){
201
  $('.stars-turned-on',obj).animate({
202
  'width' : percentage+'%'
203
+ }, 'slow');
 
 
204
  });
205
  }
206
  });
kk-ratings.php CHANGED
@@ -3,8 +3,8 @@
3
  /*
4
  Plugin Name: kk Star Ratings
5
  Plugin URI: http://wakeusup.com/2011/05/kk-star-ratings/
6
- Description: A clean, animated and sweat ratings feature for your blog <strong>With kk Star Ratings, you can allow your blog posts to be rated by your blog visitors</strong>. There are some useful options you can set to customize this plugin. You can do all that after installing the plugin and then visiting the <a href="options-general.php?page=kk-ratings_options">Plugin Settings</a>.
7
- Version: 1.2
8
  Author: Kamal Khan
9
  Author URI: http://bhittani.com
10
  License: GPLv2 or later
@@ -120,6 +120,21 @@ if(!class_exists('kk_Ratings') && !isset($kkratings) && !function_exists('kk_sta
120
  $this->options['init_msg'] = 'Be the first to rate it!'; // string
121
  $this->update_options();
122
  endif;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  }
124
  public function admin_init()
125
  {
@@ -296,6 +311,8 @@ if(!class_exists('kk_Ratings') && !isset($kkratings) && !function_exists('kk_sta
296
  return $kkratings->kk_star_rating($pid);
297
  }
298
 
 
 
299
  endif;
300
 
301
  ?>
3
  /*
4
  Plugin Name: kk Star Ratings
5
  Plugin URI: http://wakeusup.com/2011/05/kk-star-ratings/
6
+ Description: A clean, animated and sweat ratings feature for your blog <strong>With kk Star Ratings, you can allow your blog posts to be rated by your blog visitors</strong>. <strong>It also includes a widget</strong> which you can add to your sidebar to show the top rated post. There are some useful options you can set to customize this plugin. You can do all that after installing and activating the plugin and then visiting the <a href="options-general.php?page=kk-ratings_options">Plugin Settings</a>.
7
+ Version: 1.3
8
  Author: Kamal Khan
9
  Author URI: http://bhittani.com
10
  License: GPLv2 or later
120
  $this->options['init_msg'] = 'Be the first to rate it!'; // string
121
  $this->update_options();
122
  endif;
123
+
124
+ // add meta_key for avg. Required for backward compatibility
125
+ global $wpdb;
126
+ $table = $wpdb->prefix . 'postmeta';
127
+ $posts = $wpdb->get_results("SELECT a.ID, b.meta_key, b.meta_value FROM " . $wpdb->posts . " a, $table b WHERE a.ID=b.post_id AND (b.meta_key='_kk_ratings_ratings' OR b.meta_key='_kk_ratings_casts') ORDER BY a.ID ASC");
128
+ $wrap = array();
129
+ foreach ($posts as $post)
130
+ {
131
+ $wrap[$post->ID]['id'] = $post->ID;
132
+ $wrap[$post->ID][$post->meta_key] = $post->meta_value;
133
+ }
134
+ foreach($wrap as $p)
135
+ {
136
+ update_post_meta($p['id'], '_kk_ratings_avg', round($p['_kk_ratings_ratings']/$p['_kk_ratings_casts'],1));
137
+ }
138
  }
139
  public function admin_init()
140
  {
311
  return $kkratings->kk_star_rating($pid);
312
  }
313
 
314
+ require_once('widget.php');
315
+
316
  endif;
317
 
318
  ?>
readme.txt CHANGED
@@ -11,7 +11,7 @@ Requires at least: 3.0
11
 
12
  Tested up to: 3.2.1
13
 
14
- Stable tag: 1.2
15
 
16
 
17
  kk Star Ratings allows blog visitors to involve and interact more effectively with your website by rating posts.
@@ -27,6 +27,8 @@ As the name states, this plugin displays a 5 star ratings in your posts/pages.
27
 
28
  It has quite cool hover fade effects and animations.
29
 
 
 
30
  A settings page is also available where you can adjust the settings. You can:
31
 
32
 
@@ -79,6 +81,9 @@ First of all make sure you have selected where to display the ratings in the set
79
  2. The Output
80
 
81
 
 
 
 
82
  == Changelog ==
83
 
84
  = 1.1 =
@@ -87,5 +92,8 @@ First of all make sure you have selected where to display the ratings in the set
87
  = 1.2 =
88
  * Added possibility to show ratings of any post anywhere in your theme files.
89
 
 
 
 
90
 
91
  == Upgrade Notice ==
11
 
12
  Tested up to: 3.2.1
13
 
14
+ Stable tag: 1.3
15
 
16
 
17
  kk Star Ratings allows blog visitors to involve and interact more effectively with your website by rating posts.
27
 
28
  It has quite cool hover fade effects and animations.
29
 
30
+ Version 1.3+ also inludes a widget so you can show top rated posts in your sidebar as well.
31
+
32
  A settings page is also available where you can adjust the settings. You can:
33
 
34
 
81
  2. The Output
82
 
83
 
84
+ 3. The Widget
85
+
86
+
87
  == Changelog ==
88
 
89
  = 1.1 =
92
  = 1.2 =
93
  * Added possibility to show ratings of any post anywhere in your theme files.
94
 
95
+ = 1.3 =
96
+ * Added a widget. Now you can show top rated posts in your sidebar :).
97
+
98
 
99
  == Upgrade Notice ==
screenshot-3.png ADDED
Binary file
widget.php ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ // Make sure class does not already exist (Playing safe).
4
+ if(!class_exists('kk_Ratings_Widget') && !function_exists('kk_ratings_widget_init')) :
5
+
6
+ class kk_Ratings_Widget extends WP_Widget
7
+ {
8
+ // Runs when OBJECT DECLARED (Instanciated)
9
+ public function kk_Ratings_Widget()
10
+ {
11
+ $widget_options = array(
12
+ 'classname' => 'kk-star-ratings-widget',
13
+ 'description' => 'Show top rated posts'
14
+ );
15
+ parent::WP_Widget('kk_Ratings_Widget', 'kk Star Ratings', $widget_options);
16
+ }
17
+ // Outputs USER INTERFACE
18
+ public function widget($args, $instance)
19
+ {
20
+ extract( $args, EXTR_SKIP );
21
+ $title = ( !empty($instance['title']) ) ? $instance['title'] : 'Top Posts';
22
+ $total = ( !empty($instance['noofposts']) ) ? $instance['noofposts'] : '5';
23
+ $sr = ($instance['showrating']) ? true : false;
24
+
25
+ echo $before_widget;
26
+ echo $before_title . $title . $after_title;
27
+
28
+ // OUTPUT starts
29
+ global $wpdb;
30
+ $table = $wpdb->prefix . 'postmeta';
31
+ $posts = $wpdb->get_results("SELECT a.ID, a.post_title, b.meta_value AS 'ratings' FROM " . $wpdb->posts . " a, $table b WHERE a.post_status='publish' AND a.ID=b.post_id AND b.meta_key='_kk_ratings_avg' ORDER BY b.meta_value DESC LIMIT $total");
32
+ echo '<ul>';
33
+ foreach ($posts as $post)
34
+ {
35
+ echo "<li><a href='".get_permalink($post->ID)."'>".$post->post_title."</a>";
36
+ if($sr)
37
+ {
38
+ echo " <span style='font-size:10px;'>(".$post->ratings."/5)</span>";
39
+ }
40
+ echo "</li>";
41
+ }
42
+ echo '</ul>';
43
+ // OUTPUT ends
44
+
45
+ echo $after_widget;
46
+ }
47
+ // Updates OPTIONS
48
+ /*
49
+ public function update()
50
+ {
51
+
52
+ }
53
+ */
54
+ // The option FORM
55
+ public function form( $instance )
56
+ {
57
+ ?>
58
+ <p>
59
+ <label for="<?php echo $this->get_field_id('title'); ?>">Title:
60
+ <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>
61
+ </p>
62
+ <p>
63
+ <label for="<?php echo $this->get_field_id('noofposts'); ?>">No of Posts:
64
+ <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>
65
+ </p>
66
+ <p>
67
+ <label for="<?php echo $this->get_field_id('showrating'); ?>">Show Average?:
68
+ <select id="<?php echo $this->get_field_id('showrating'); ?>" name="<?php echo $this->get_field_name('showrating'); ?>">
69
+ <option value="0" <?php if(!esc_attr($instance['showrating'])){echo "selected='selected'";} ?>>No</option>
70
+ <option value="1" <?php if(esc_attr($instance['showrating'])){echo "selected='selected'";} ?>>Yes</option>
71
+ </select>
72
+ </label>
73
+ </p>
74
+ <?php
75
+ }
76
+ }
77
+
78
+ function kk_ratings_widget_init()
79
+ {
80
+ register_widget('kk_Ratings_Widget');
81
+ }
82
+ add_action('widgets_init', 'kk_ratings_widget_init');
83
+
84
+
85
+ endif;
86
+ ?>