kk Star Ratings - Version 3.0.1

Version Description

Download this release

Release Info

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

Code changes from version 3.0.0 to 3.0.1

index.php CHANGED
@@ -8,7 +8,7 @@
8
  * Author URI: http://bhittani.com
9
  * Text Domain: kk-star-ratings
10
  * Domain Path: /languages
11
- * Version: 3.0.0
12
  * License: GPLv2 or later
13
  *
14
  * @package Bhittani\StarRating
@@ -21,7 +21,7 @@ if (! defined('ABSPATH')) {
21
 
22
  define('KKSR_FILE', __FILE__);
23
  define('KKSR_PLUGIN', plugin_basename(KKSR_FILE));
24
- define('KKSR_VERSION', '3.0.0');
25
  define('KKSR_PREFIX', 'kksr_');
26
  define('KKSR_SLUG', 'kk-star-ratings');
27
  define('KKSR_LABEL', 'kk Star Ratings');
8
  * Author URI: http://bhittani.com
9
  * Text Domain: kk-star-ratings
10
  * Domain Path: /languages
11
+ * Version: 3.0.1
12
  * License: GPLv2 or later
13
  *
14
  * @package Bhittani\StarRating
21
 
22
  define('KKSR_FILE', __FILE__);
23
  define('KKSR_PLUGIN', plugin_basename(KKSR_FILE));
24
+ define('KKSR_VERSION', '3.0.1');
25
  define('KKSR_PREFIX', 'kksr_');
26
  define('KKSR_SLUG', 'kk-star-ratings');
27
  define('KKSR_LABEL', 'kk Star Ratings');
languages/kk-star-ratings.pot CHANGED
@@ -4,7 +4,7 @@ msgid ""
4
  msgstr ""
5
  "Project-Id-Version: \n"
6
  "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/kk-star-ratings\n"
7
- "POT-Creation-Date: 2019-07-18 00:51:01+00:00\n"
8
  "MIME-Version: 1.0\n"
9
  "Content-Type: text/plain; charset=utf-8\n"
10
  "Content-Transfer-Encoding: 8bit\n"
@@ -227,18 +227,18 @@ msgstr ""
227
  msgid "A rating is required to vote."
228
  msgstr ""
229
 
230
- #: src/functions.php:328
231
  msgid "You can only rate between 0 and %s."
232
  msgstr ""
233
 
234
- #: src/functions.php:427
235
  msgid "General"
236
  msgstr ""
237
 
238
- #: src/functions.php:428
239
  msgid "Rich Snippets"
240
  msgstr ""
241
 
242
- #: src/functions.php:429
243
  msgid "Appearance"
244
  msgstr ""
4
  msgstr ""
5
  "Project-Id-Version: \n"
6
  "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/kk-star-ratings\n"
7
+ "POT-Creation-Date: 2019-07-18 06:33:19+00:00\n"
8
  "MIME-Version: 1.0\n"
9
  "Content-Type: text/plain; charset=utf-8\n"
10
  "Content-Transfer-Encoding: 8bit\n"
227
  msgid "A rating is required to vote."
228
  msgstr ""
229
 
230
+ #: src/functions.php:337
231
  msgid "You can only rate between 0 and %s."
232
  msgstr ""
233
 
234
+ #: src/functions.php:438
235
  msgid "General"
236
  msgstr ""
237
 
238
+ #: src/functions.php:439
239
  msgid "Rich Snippets"
240
  msgstr ""
241
 
242
+ #: src/functions.php:440
243
  msgid "Appearance"
244
  msgstr ""
readme.txt CHANGED
@@ -5,7 +5,7 @@ Tags: star ratings, votings, rate posts, ajax ratings, infinite stars, unlimited
5
  Requires at least: 4.5
6
  Requires PHP: 5.5.9
7
  Tested up to: 5.2.2
8
- Stable tag: 3.0.0
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
5
  Requires at least: 4.5
6
  Requires PHP: 5.5.9
7
  Tested up to: 5.2.2
8
+ Stable tag: 3.0.1
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
src/activate.php CHANGED
@@ -15,7 +15,7 @@ register_activation_hook(KKSR_FILE, KKSR_NAMESPACE.'activate'); function activat
15
  {
16
  $previousVersion = getOption('ver', false);
17
 
18
- if ($previousVersion && version_compare($previousVersion, '3.0.0', '<')) {
19
  // We are upgrading, lets normalize the previous options.
20
  upgradeOptions();
21
  // Also normalize all the previous post ratings.
15
  {
16
  $previousVersion = getOption('ver', false);
17
 
18
+ if ($previousVersion && version_compare($previousVersion, '3.0.1', '<')) {
19
  // We are upgrading, lets normalize the previous options.
20
  upgradeOptions();
21
  // Also normalize all the previous post ratings.
src/functions.php CHANGED
@@ -132,43 +132,30 @@ function upgradeOptions(array $merge = [])
132
  function upgradeRatings()
133
  {
134
  global $wpdb;
135
- $postMetaTable = $wpdb->prefix.'postmeta';
136
 
137
  // Normalize ratings.
138
 
139
  $stars = getOption('stars');
140
 
141
  $rows = $wpdb->get_results("
142
- SELECT a.ID, b.meta_value as ratings
143
- FROM {$wpdb->posts} a, {$postMetaTable} b
144
- WHERE a.ID=b.post_id AND b.meta_key='_kksr_ratings'
 
 
145
  ");
146
 
147
  foreach ($rows as $row) {
148
  update_post_meta(
149
  $row->ID,
150
  '_'.prefix('ratings'),
151
- toNormalizedRatings($row->ratings, $stars)
152
  );
153
  }
154
 
155
- // Casts => Count.
156
-
157
- $rows = $wpdb->get_results("
158
- SELECT a.ID, b.meta_value as casts
159
- FROM {$wpdb->posts} a, {$postMetaTable} b
160
- WHERE a.ID=b.post_id AND b.meta_key='_kksr_casts'
161
- ");
162
-
163
- foreach ($rows as $row) {
164
- update_post_meta($row->ID, '_'.prefix('count'), $row->casts);
165
- }
166
-
167
- $wpdb->delete($postMetaTable, ['meta_key' => '_kksr_casts']);
168
-
169
  // Truncate IP addresses.
170
 
171
- $wpdb->delete($postMetaTable, ['meta_key' => '_kksr_ips']);
172
  }
173
 
174
  function canVote($p = null)
@@ -299,6 +286,28 @@ function calculateWidth($score, $size = null, $pad = 4)
299
  return $score * $size + $score * $pad;
300
  }
301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
302
  function extractPosition($position = null)
303
  {
304
  $position = $position ?: getOption('position');
@@ -333,11 +342,13 @@ function vote($idOrPost, $rating)
333
  $ratings = (float) get_post_meta($id, '_'.prefix('ratings'), true);
334
  $ratings += toNormalizedRatings($rating, $stars);
335
 
336
- $count = (int) get_post_meta($id, '_'.prefix('count'), true);
337
  $count += 1;
338
 
339
  update_post_meta($id, '_'.prefix('ratings'), $ratings);
340
- update_post_meta($id, '_'.prefix('count'), $count);
 
 
341
 
342
  do_action(prefix('vote'), $id, $rating);
343
 
@@ -376,7 +387,7 @@ function collect($limit = 5, $taxonomyId = null, $offset = 0)
376
  WHERE
377
  posts.post_status = 'publish'
378
  AND CAST(postmeta_count.meta_value AS UNSIGNED) != 0
379
- AND postmeta_count.meta_key = '_kksr_count'
380
  AND postmeta_ratings.meta_key = '_kksr_ratings'
381
  ";
382
 
132
  function upgradeRatings()
133
  {
134
  global $wpdb;
 
135
 
136
  // Normalize ratings.
137
 
138
  $stars = getOption('stars');
139
 
140
  $rows = $wpdb->get_results("
141
+ SELECT posts.ID, postmeta_avg.meta_value as avg, postmeta_casts.meta_value as casts
142
+ FROM {$wpdb->posts} posts
143
+ JOIN {$wpdb->postmeta} postmeta_avg ON posts.ID = postmeta_avg.post_id
144
+ JOIN {$wpdb->postmeta} postmeta_casts ON posts.ID = postmeta_casts.post_id
145
+ WHERE postmeta_avg.meta_key = '_kksr_avg' AND postmeta_casts.meta_key = '_kksr_casts'
146
  ");
147
 
148
  foreach ($rows as $row) {
149
  update_post_meta(
150
  $row->ID,
151
  '_'.prefix('ratings'),
152
+ scoreToRatings($row->avg, $row->casts, $stars)
153
  );
154
  }
155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  // Truncate IP addresses.
157
 
158
+ $wpdb->delete($wpdb->postmeta, ['meta_key' => '_kksr_ips']);
159
  }
160
 
161
  function canVote($p = null)
286
  return $score * $size + $score * $pad;
287
  }
288
 
289
+ function scoreToRatings($score, $count, $from = 5, $to = 5)
290
+ {
291
+ $to = (int) $to;
292
+ $from = (int) $from;
293
+ $count = (int) $count;
294
+ $score = (float) $score;
295
+
296
+ if ($from <= 0 || $to <= 0) {
297
+ return 0;
298
+ }
299
+
300
+ if ($score < 0) {
301
+ $score = 1;
302
+ }
303
+
304
+ if ($score > $from) {
305
+ $score = $from;
306
+ }
307
+
308
+ return (float) round($score * $count / ($from / $to), 0, PHP_ROUND_HALF_DOWN);
309
+ }
310
+
311
  function extractPosition($position = null)
312
  {
313
  $position = $position ?: getOption('position');
342
  $ratings = (float) get_post_meta($id, '_'.prefix('ratings'), true);
343
  $ratings += toNormalizedRatings($rating, $stars);
344
 
345
+ $count = (int) get_post_meta($id, '_'.prefix('casts'), true);
346
  $count += 1;
347
 
348
  update_post_meta($id, '_'.prefix('ratings'), $ratings);
349
+ update_post_meta($id, '_'.prefix('casts'), $count);
350
+ // For legacy reasons.
351
+ update_post_meta($id, '_'.prefix('avg'), calculateScore($ratings, $count));
352
 
353
  do_action(prefix('vote'), $id, $rating);
354
 
387
  WHERE
388
  posts.post_status = 'publish'
389
  AND CAST(postmeta_count.meta_value AS UNSIGNED) != 0
390
+ AND postmeta_count.meta_key = '_kksr_casts'
391
  AND postmeta_ratings.meta_key = '_kksr_ratings'
392
  ";
393
 
src/markup.php CHANGED
@@ -31,7 +31,7 @@ add_filter('the_content', KKSR_NAMESPACE.'markup'); function markup($content, $f
31
  $stars = (int) getOption('stars');
32
  list($placement, $alignment) = extractPosition();
33
  $total = get_post_meta($id, '_kksr_ratings', true);
34
- $count = (int) get_post_meta($id, '_kksr_count', true);
35
  $score = calculateScore($total, $count, $stars);
36
  $percent = calculatePercentage($total, $count);
37
  $width = calculateWidth($score, $size);
@@ -51,5 +51,5 @@ add_filter('kksr_score', KKSR_NAMESPACE.'scoreFilter'); function scoreFilter($sc
51
 
52
  add_filter('kksr_count', KKSR_NAMESPACE.'countFilter', 10); function countFilter($count)
53
  {
54
- return str_pad($count, 2, 0, STR_PAD_LEFT);
55
  }
31
  $stars = (int) getOption('stars');
32
  list($placement, $alignment) = extractPosition();
33
  $total = get_post_meta($id, '_kksr_ratings', true);
34
+ $count = (int) get_post_meta($id, '_kksr_casts', true);
35
  $score = calculateScore($total, $count, $stars);
36
  $percent = calculatePercentage($total, $count);
37
  $width = calculateWidth($score, $size);
51
 
52
  add_filter('kksr_count', KKSR_NAMESPACE.'countFilter', 10); function countFilter($count)
53
  {
54
+ return str_pad($count, 2, 0, is_rtl() ? STR_PAD_RIGHT : STR_PAD_LEFT);
55
  }
src/structure.php CHANGED
@@ -24,7 +24,7 @@ add_action('wp_head', KKSR_NAMESPACE.'structuredData'); function structuredData(
24
  global $post;
25
 
26
  $id = $post->ID;
27
- $count = (int) get_post_meta($id, '_kksr_count', true);
28
 
29
  if (! $count) {
30
  return;
24
  global $post;
25
 
26
  $id = $post->ID;
27
+ $count = (int) get_post_meta($id, '_kksr_casts', true);
28
 
29
  if (! $count) {
30
  return;
views/markup.php CHANGED
@@ -1,4 +1,4 @@
1
- <div data-id="<?php echo $id; ?>"
2
  class="kk-star-ratings <?php echo (isset($disabled) && $disabled) ? 'kksr-disable' : ''; ?> kksr-<?php echo isset($placement) ? $placement : 'top'; ?> kksr-<?php echo isset($alignment) ? $alignment : 'left'; ?><?php echo (isset($isRtl) && $isRtl) ? ' kksr-rtl' : ''; ?>">
3
  <?php
4
  ob_start();
1
+ <div data-id="<?php echo $id; ?>" data-score="<?php echo $score; ?>" data-count="<?php echo $count; ?>"
2
  class="kk-star-ratings <?php echo (isset($disabled) && $disabled) ? 'kksr-disable' : ''; ?> kksr-<?php echo isset($placement) ? $placement : 'top'; ?> kksr-<?php echo isset($alignment) ? $alignment : 'left'; ?><?php echo (isset($isRtl) && $isRtl) ? ' kksr-rtl' : ''; ?>">
3
  <?php
4
  ob_start();