kk Star Ratings - Version 4.2.0

Version Description

Download this release

Release Info

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

Code changes from version 4.1.10 to 4.2.0

index.php CHANGED
@@ -10,12 +10,14 @@
10
  * Author URI: http://bhittani.com
11
  * Text Domain: kk-star-ratings
12
  * Domain Path: /languages
13
- * Version: 4.1.10
14
  * License: GPLv2 or later
15
  */
16
 
17
  namespace Bhittani\StarRating;
18
 
 
 
19
  if (! defined('ABSPATH')) {
20
  http_response_code(404);
21
  die();
@@ -82,6 +84,10 @@ if (is_admin()) {
82
  require_once __DIR__.'/src/assets.php';
83
  }
84
 
 
 
 
 
85
  add_action('plugins_loaded', function () {
86
  do_action(prefix('init'));
87
  });
10
  * Author URI: http://bhittani.com
11
  * Text Domain: kk-star-ratings
12
  * Domain Path: /languages
13
+ * Version: 4.2.0
14
  * License: GPLv2 or later
15
  */
16
 
17
  namespace Bhittani\StarRating;
18
 
19
+ use kkStarRatings;
20
+
21
  if (! defined('ABSPATH')) {
22
  http_response_code(404);
23
  die();
84
  require_once __DIR__.'/src/assets.php';
85
  }
86
 
87
+ require_once __DIR__.'/kkStarRatings.php';
88
+
89
+ new kkStarRatings('');
90
+
91
  add_action('plugins_loaded', function () {
92
  do_action(prefix('init'));
93
  });
kkStarRatings.php ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /*
4
+ * This file is part of bhittani/kk-star-ratings.
5
+ *
6
+ * (c) Kamal Khan <shout@bhittani.com>
7
+ *
8
+ * For the full copyright and license information, please view
9
+ * the LICENSE file that was distributed with this source code.
10
+ */
11
+
12
+ use function Bhittani\StarRating\prefix;
13
+ use function Bhittani\StarRating\meta_prefix;
14
+ use function Bhittani\StarRating\add_plugin_action;
15
+ use function Bhittani\StarRating\add_plugin_filter;
16
+
17
+ class kkStarRatings
18
+ {
19
+ protected $slugs = [];
20
+
21
+ public function __construct($slugs)
22
+ {
23
+ $this->slugs = (array) $slugs;
24
+
25
+ add_plugin_filter('count', [$this, 'countFilter'], 9, 3);
26
+ add_plugin_filter('score', [$this, 'scoreFilter'], 9, 4);
27
+ add_plugin_filter('greet', [$this, 'greetFilter'], 9, 3);
28
+ add_plugin_filter('validate', [$this, 'validateFilter'], 9, 3);
29
+ add_plugin_filter('can_vote', [$this, 'canVoteFilter'], 9, 3);
30
+ add_plugin_action('vote', [$this, 'voteAction'], 9, 4);
31
+ }
32
+
33
+ public function countFilter($count, $id, $slug)
34
+ {
35
+ if (! in_array($slug, $this->slugs)) {
36
+ return $count;
37
+ }
38
+
39
+ return max(0, (int) get_post_meta($id, $this->metaPrefix('casts', $slug), true));
40
+ }
41
+
42
+ public function scoreFilter($score, $best, $id, $slug)
43
+ {
44
+ if (! in_array($slug, $this->slugs)) {
45
+ return $score;
46
+ }
47
+
48
+ $count = $this->countFilter(null, $id, $slug);
49
+
50
+ if (! $count) {
51
+ return 0;
52
+ }
53
+
54
+ $counter = (float) get_post_meta($id, $this->metaPrefix('ratings', $slug), true);
55
+ $score = $counter / $count / 5 * $best;
56
+ $score = round($score, 1, PHP_ROUND_HALF_DOWN);
57
+
58
+ return min(max($score, 0), $best);
59
+ }
60
+
61
+ public function greetFilter($greet, $id, $slug)
62
+ {
63
+ if (! in_array($slug, $this->slugs)) {
64
+ return $greet;
65
+ }
66
+
67
+ $type = get_post_type($id);
68
+
69
+ if (! $type) {
70
+ return $greet;
71
+ }
72
+
73
+ return str_replace('[type]', $type, $greet);
74
+ }
75
+
76
+ public function validateFilter($bool, $id, $slug)
77
+ {
78
+ if (! in_array($slug, $this->slugs)) {
79
+ return $bool;
80
+ }
81
+
82
+ if (! $id) {
83
+ return $bool;
84
+ }
85
+
86
+ $status = get_post_meta($id, $this->metaPrefix('status', $slug), true);
87
+
88
+ if ($status == 'enable') {
89
+ return true;
90
+ }
91
+
92
+ if ($status == 'disable') {
93
+ return false;
94
+ }
95
+
96
+ $categories = array_map(function ($category) {
97
+ return $category->term_id;
98
+ }, get_the_category($id));
99
+
100
+ $excludedCategories = (array) get_option(prefix('exclude_categories'), []);
101
+
102
+ if (count($categories) !== count(array_diff($categories, $excludedCategories))) {
103
+ return false;
104
+ }
105
+
106
+ if (in_array(get_post_type($id), (array) get_option(prefix('exclude_locations')))) {
107
+ return false;
108
+ }
109
+
110
+ return $bool;
111
+ }
112
+
113
+ public function canVoteFilter($bool, $id, $slug)
114
+ {
115
+ if (! in_array($slug, $this->slugs)) {
116
+ return $bool;
117
+ }
118
+
119
+ $strategies = (array) get_option(prefix('strategies'), []);
120
+
121
+ if (is_archive() && ! in_array('archives', $strategies)) {
122
+ return false;
123
+ }
124
+
125
+ if (in_array('unique', $strategies)
126
+ && in_array(md5($_SERVER['REMOTE_ADDR']), get_post_meta($id, $this->metaPrefix('ref', $slug)))
127
+ ) {
128
+ return false;
129
+ }
130
+
131
+ return $bool;
132
+ }
133
+
134
+ public function voteAction($score, $best, $id, $slug)
135
+ {
136
+ if (! in_array($slug, $this->slugs)) {
137
+ return;
138
+ }
139
+
140
+ $count = $this->countFilter(null, $id, $slug);
141
+ $counter = (float) get_post_meta($id, $this->metaPrefix('ratings', $slug), true);
142
+
143
+ ++$count;
144
+ $counter += $score / $best * 5;
145
+
146
+ update_post_meta($id, $this->metaPrefix('casts', $slug), $count);
147
+ update_post_meta($id, $this->metaPrefix('ratings', $slug), $counter);
148
+ // Legacy support.
149
+ update_post_meta($id, $this->metaPrefix('avg', $slug), $counter / $count);
150
+
151
+ $ip = md5($_SERVER['REMOTE_ADDR']);
152
+
153
+ add_post_meta($id, $this->metaPrefix('ref', $slug), $ip);
154
+ }
155
+
156
+ protected function metaPrefix($str, $slug = null)
157
+ {
158
+ $prefix = meta_prefix($str);
159
+
160
+ if ($slug) {
161
+ $prefix .= '_'.$slug;
162
+ }
163
+
164
+ return $prefix;
165
+ }
166
+ }
languages/kk-star-ratings.pot CHANGED
@@ -2,14 +2,14 @@
2
  # This file is distributed under the same license as the kk Star Ratings plugin.
3
  msgid ""
4
  msgstr ""
5
- "Project-Id-Version: kk Star Ratings 4.1.10\n"
6
  "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/kk-star-ratings\n"
7
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
8
  "Language-Team: LANGUAGE <LL@li.org>\n"
9
  "MIME-Version: 1.0\n"
10
  "Content-Type: text/plain; charset=UTF-8\n"
11
  "Content-Transfer-Encoding: 8bit\n"
12
- "POT-Creation-Date: 2021-04-18T19:06:27+00:00\n"
13
  "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
14
  "X-Generator: WP-CLI 2.3.0\n"
15
  "X-Domain: kk-star-ratings\n"
2
  # This file is distributed under the same license as the kk Star Ratings plugin.
3
  msgid ""
4
  msgstr ""
5
+ "Project-Id-Version: kk Star Ratings 4.2.0\n"
6
  "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/kk-star-ratings\n"
7
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
8
  "Language-Team: LANGUAGE <LL@li.org>\n"
9
  "MIME-Version: 1.0\n"
10
  "Content-Type: text/plain; charset=UTF-8\n"
11
  "Content-Transfer-Encoding: 8bit\n"
12
+ "POT-Creation-Date: 2021-04-19T16:54:15+00:00\n"
13
  "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
14
  "X-Generator: WP-CLI 2.3.0\n"
15
  "X-Domain: kk-star-ratings\n"
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.6
7
  Tested up to: 5.7.1
8
- Stable tag: 4.1.8
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
@@ -32,7 +32,7 @@ kk Star Ratings is a widely used star rating plugin for wordpress. Here are some
32
  - Full control via options page. You can,
33
 
34
  - Enable or disable globally.
35
-
36
  - Disable star ratings in posts that belong to certain categories.
37
 
38
  - Choose where to show the star ratings. It can be on the **homepage**, in **archives**, in **posts**, in **pages** and/or in **custom post types**.
@@ -52,7 +52,7 @@ kk Star Ratings is a widely used star rating plugin for wordpress. Here are some
52
  - Adjust the amount of stars.
53
 
54
  - Adjust the colors of the stars.
55
-
56
  - And much more...
57
 
58
  == Installation ==
@@ -112,3 +112,5 @@ It is seamless to upgrade from v3 to v4.
112
 
113
  = 3.x =
114
  All previous ratings and options will be preserved. However, since v3 has been renewed from scratch, we do not support downgrading to v2 after moving from v2 to v3.
 
 
5
  Requires at least: 4.5
6
  Requires PHP: 5.6
7
  Tested up to: 5.7.1
8
+ Stable tag: 4.2.0
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
32
  - Full control via options page. You can,
33
 
34
  - Enable or disable globally.
35
+
36
  - Disable star ratings in posts that belong to certain categories.
37
 
38
  - Choose where to show the star ratings. It can be on the **homepage**, in **archives**, in **posts**, in **pages** and/or in **custom post types**.
52
  - Adjust the amount of stars.
53
 
54
  - Adjust the colors of the stars.
55
+
56
  - And much more...
57
 
58
  == Installation ==
112
 
113
  = 3.x =
114
  All previous ratings and options will be preserved. However, since v3 has been renewed from scratch, we do not support downgrading to v2 after moving from v2 to v3.
115
+
116
+
src/legacy.php CHANGED
@@ -33,39 +33,40 @@ if (! function_exists('kk_star_ratings_get')) {
33
  global $wpdb;
34
  $postsTable = $wpdb->posts;
35
  $postMetaTable = $wpdb->prefix.'postmeta';
36
- $base = get_option(Bhittani\StarRating\prefix('stars')) / 5;
 
 
 
 
 
 
37
 
38
  $querySelect = "
39
  SELECT
40
  posts.ID,
41
- ROUND(postmeta_ratings.meta_value / postmeta_count.meta_value * %f, 1) score
 
 
42
  FROM {$postsTable} posts
43
  ";
44
 
45
- $queryJoins = "
46
- JOIN {$postMetaTable} postmeta_ratings
47
- ON posts.ID = postmeta_ratings.post_id
48
- JOIN {$postMetaTable} postmeta_count
49
- ON posts.ID = postmeta_count.post_id
50
- ";
51
-
52
  $queryConditions = "
53
  WHERE
54
  posts.post_status = 'publish'
55
- AND CAST(postmeta_count.meta_value AS UNSIGNED) != 0
56
- AND postmeta_count.meta_key = '_kksr_casts'
57
  AND postmeta_ratings.meta_key = '_kksr_ratings'
58
  ";
59
 
60
  $queryOrder = '
61
  ORDER BY
62
  score DESC,
63
- CAST(postmeta_count.meta_value AS UNSIGNED) DESC
64
  ';
65
 
66
  $queryLimit = 'LIMIT %d, %d';
67
 
68
- $queryArgs = [$base, $offset, $limit];
69
 
70
  if ($taxonomyId) {
71
  $termTaxonomyTable = $wpdb->prefix.'term_taxonomy';
@@ -82,7 +83,7 @@ if (! function_exists('kk_star_ratings_get')) {
82
  AND term_taxonomies.term_id=%d
83
  ';
84
 
85
- $queryArgs = [$base, $taxonomyId, $offset, $limit];
86
  }
87
 
88
  $query = $querySelect
33
  global $wpdb;
34
  $postsTable = $wpdb->posts;
35
  $postMetaTable = $wpdb->prefix.'postmeta';
36
+
37
+ $queryJoins = "
38
+ JOIN {$postMetaTable} postmeta_ratings
39
+ ON posts.ID = postmeta_ratings.post_id
40
+ JOIN {$postMetaTable} postmeta_casts
41
+ ON posts.ID = postmeta_casts.post_id
42
+ ";
43
 
44
  $querySelect = "
45
  SELECT
46
  posts.ID,
47
+ postmeta_ratings.meta_value AS ratings,
48
+ postmeta_casts.meta_value AS casts,
49
+ ROUND(CAST(postmeta_ratings.meta_value AS FLOAT) / CAST(postmeta_casts.meta_value AS DOUBLE), 1) AS score
50
  FROM {$postsTable} posts
51
  ";
52
 
 
 
 
 
 
 
 
53
  $queryConditions = "
54
  WHERE
55
  posts.post_status = 'publish'
56
+ AND CAST(postmeta_casts.meta_value AS UNSIGNED) != 0
57
+ AND postmeta_casts.meta_key = '_kksr_casts'
58
  AND postmeta_ratings.meta_key = '_kksr_ratings'
59
  ";
60
 
61
  $queryOrder = '
62
  ORDER BY
63
  score DESC,
64
+ CAST(postmeta_casts.meta_value AS UNSIGNED) DESC
65
  ';
66
 
67
  $queryLimit = 'LIMIT %d, %d';
68
 
69
+ $queryArgs = [$offset, $limit];
70
 
71
  if ($taxonomyId) {
72
  $termTaxonomyTable = $wpdb->prefix.'term_taxonomy';
83
  AND term_taxonomies.term_id=%d
84
  ';
85
 
86
+ $queryArgs = [$taxonomyId, $offset, $limit];
87
  }
88
 
89
  $query = $querySelect
src/metabox.php CHANGED
@@ -22,8 +22,9 @@ function metabox($type, $post)
22
  $icon = $legend = '';
23
 
24
  if ($post) {
25
- $count = count_filter(null, $post->ID, null);
26
- $score = score_filter(null, get_option(prefix('stars')), $post->ID, null);
 
27
 
28
  $icon = '<span class="dashicons dashicons-star-empty" style="margin-right: .25rem; font-size: 18px;"></span>';
29
 
22
  $icon = $legend = '';
23
 
24
  if ($post) {
25
+ $best = max((int) get_option(prefix('stars')), 1);
26
+ $count = apply_plugin_filters('count', null, $post->ID, null);
27
+ $score = apply_plugin_filters('score', null, $best, $post->ID, null);
28
 
29
  $icon = '<span class="dashicons dashicons-star-empty" style="margin-right: .25rem; font-size: 18px;"></span>';
30
 
src/post.php CHANGED
@@ -54,139 +54,6 @@ function content_filter($content)
54
  return $valign == 'top' ? ($shortcode.$content) : ($content.$shortcode);
55
  }
56
 
57
- add_plugin_filter('count', __NAMESPACE__.'\count_filter', 9, 3);
58
- function count_filter($count, $id, $slug)
59
- {
60
- if ($slug) {
61
- return $count;
62
- }
63
-
64
- $count = (int) get_post_meta($id, meta_prefix('casts'), true);
65
-
66
- return max($count, 0);
67
- }
68
-
69
- add_plugin_filter('score', __NAMESPACE__.'\score_filter', 9, 4);
70
- function score_filter($score, $best, $id, $slug)
71
- {
72
- if ($slug) {
73
- return $score;
74
- }
75
-
76
- $count = count_filter(null, $id, null);
77
- $counter = (float) get_post_meta($id, meta_prefix('ratings'), true);
78
-
79
- if (! $count) {
80
- return 0;
81
- }
82
-
83
- $score = $counter / $count / 5 * $best;
84
- $score = round($score, 1, PHP_ROUND_HALF_DOWN);
85
-
86
- return min(max($score, 0), $best);
87
- }
88
-
89
- add_plugin_filter('greet', __NAMESPACE__.'\greet_filter', 9, 3);
90
- function greet_filter($greet, $id, $slug)
91
- {
92
- if ($slug) {
93
- return $greet;
94
- }
95
-
96
- $type = get_post_type($id);
97
-
98
- if (! $type) {
99
- return $greet;
100
- }
101
-
102
- return str_replace('[type]', $type, $greet);
103
- }
104
-
105
- add_plugin_filter('validate', __NAMESPACE__.'\validate_post', 9, 3);
106
- function validate_post($bool, $id, $slug)
107
- {
108
- if ($slug || ! $id) {
109
- return $bool;
110
- }
111
-
112
- // $id = get_post_field('ID');
113
-
114
- // if (! $id) {
115
- // return $bool;
116
- // }
117
-
118
- $status = get_post_meta($id, meta_prefix('status'), true);
119
-
120
- if ($status == 'enable') {
121
- return true;
122
- }
123
-
124
- if ($status == 'disable') {
125
- return false;
126
- }
127
-
128
- $categories = array_map(function ($category) {
129
- return $category->term_id;
130
- }, get_the_category($id));
131
-
132
- $excludedCategories = (array) get_option(prefix('exclude_categories'), []);
133
-
134
- if (count($categories) !== count(array_diff($categories, $excludedCategories))) {
135
- return false;
136
- }
137
-
138
- if (in_array(get_post_type($id), (array) get_option(prefix('exclude_locations')))) {
139
- return false;
140
- }
141
-
142
- return $bool;
143
- }
144
-
145
- add_plugin_filter('can_vote', __NAMESPACE__.'\can_vote_post', 9, 3);
146
- function can_vote_post($bool, $id, $slug)
147
- {
148
- if ($slug) {
149
- return $bool;
150
- }
151
-
152
- $strategies = (array) get_option(prefix('strategies'), []);
153
-
154
- if (is_archive() && ! in_array('archives', $strategies)) {
155
- return false;
156
- }
157
-
158
- if (in_array('unique', $strategies)
159
- && in_array(md5($_SERVER['REMOTE_ADDR']), get_post_meta($id, meta_prefix('ref')))
160
- ) {
161
- return false;
162
- }
163
-
164
- return $bool;
165
- }
166
-
167
- add_plugin_action('vote', __NAMESPACE__.'\vote_post', 9, 4);
168
- function vote_post($score, $best, $id, $slug)
169
- {
170
- if ($slug) {
171
- return;
172
- }
173
-
174
- $count = count_filter(null, $id, null);
175
- $counter = (float) get_post_meta($id, meta_prefix('ratings'), true);
176
-
177
- ++$count;
178
- $counter += $score / $best * 5;
179
-
180
- update_post_meta($id, meta_prefix('casts'), $count);
181
- update_post_meta($id, meta_prefix('ratings'), $counter);
182
- // Legacy support.
183
- update_post_meta($id, meta_prefix('avg'), $counter / $count);
184
-
185
- $ip = md5($_SERVER['REMOTE_ADDR']);
186
-
187
- add_post_meta($id, meta_prefix('ref'), $ip);
188
- }
189
-
190
  add_action('wp_head', __NAMESPACE__.'\structured_data');
191
  function structured_data()
192
  {
@@ -198,8 +65,8 @@ function structured_data()
198
  $id = get_post_field('ID');
199
  $title = htmlentities(get_post_field('post_title'));
200
  $best = max((int) get_option(prefix('stars')), 1);
201
- $count = count_filter(null, $id, null);
202
- $score = score_filter(null, $best, $id, null);
203
 
204
  if ($score) {
205
  echo '<script type="application/ld+json">';
54
  return $valign == 'top' ? ($shortcode.$content) : ($content.$shortcode);
55
  }
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  add_action('wp_head', __NAMESPACE__.'\structured_data');
58
  function structured_data()
59
  {
65
  $id = get_post_field('ID');
66
  $title = htmlentities(get_post_field('post_title'));
67
  $best = max((int) get_option(prefix('stars')), 1);
68
+ $count = apply_plugin_filters('count', null, $id, null);
69
+ $score = apply_plugin_filters('score', null, $best, $id, null);
70
 
71
  if ($score) {
72
  echo '<script type="application/ld+json">';
src/response.php CHANGED
@@ -38,7 +38,7 @@ function response(array $payload = [], $validate = true)
38
  return '';
39
  }
40
 
41
- if (! $payload['id'] && ! $payload['slug']) {
42
  $payload['id'] = get_post_field('ID');
43
  }
44
 
38
  return '';
39
  }
40
 
41
+ if (! $payload['id']/* && ! $payload['slug']*/) {
42
  $payload['id'] = get_post_field('ID');
43
  }
44