kk Star Ratings - Version 4.0.0

Version Description

Download this release

Release Info

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

Code changes from version 3.1.2 to 4.0.0

Files changed (64) hide show
  1. LICENSE.txt +1 -0
  2. index.php +67 -29
  3. languages/index.php +0 -5
  4. languages/kk-star-ratings.pot +121 -170
  5. public/css/index.php +4 -3
  6. public/css/kk-star-ratings.css +62 -118
  7. public/css/kk-star-ratings.css.php +0 -31
  8. public/index.php +4 -3
  9. public/js/index.php +4 -3
  10. public/js/kk-star-ratings.js +52 -46
  11. public/svg/active.svg +1 -0
  12. public/svg/inactive.svg +1 -0
  13. public/svg/index.php +6 -0
  14. public/svg/selected.svg +1 -0
  15. readme.txt +12 -63
  16. src/activate.php +81 -23
  17. src/admin.php +111 -83
  18. src/admin/appearance.php +29 -70
  19. src/admin/general.php +77 -80
  20. src/admin/index.php +0 -14
  21. src/admin/rich-snippets.php +40 -23
  22. src/ajax.php +36 -51
  23. src/assets.php +62 -0
  24. src/config.php +49 -0
  25. src/enqueue.php +0 -76
  26. src/functions.php +0 -550
  27. src/global.php +39 -0
  28. src/hook.php +59 -0
  29. src/index.php +0 -14
  30. src/legacy.php +78 -11
  31. src/markup.php +0 -64
  32. src/metabox.php +67 -36
  33. src/post.php +215 -0
  34. src/response.php +90 -0
  35. src/shortcode.php +35 -15
  36. src/structure.php +0 -47
  37. src/validate.php +53 -0
  38. src/view.php +73 -0
  39. views/active-star.php +1 -0
  40. views/active-stars.php +7 -0
  41. views/admin/content.php +13 -0
  42. views/admin/contents.php +0 -5
  43. views/admin/fields/checkbox.php +10 -5
  44. views/admin/fields/code.php +9 -0
  45. views/admin/fields/index.php +0 -5
  46. views/admin/fields/number.php +12 -0
  47. views/admin/fields/radio.php +10 -4
  48. views/admin/fields/select.php +12 -6
  49. views/admin/fields/text.php +9 -5
  50. views/admin/fields/textarea.php +9 -0
  51. views/admin/index.php +18 -11
  52. views/admin/social.php +28 -4
  53. views/admin/tabs.php +0 -6
  54. views/admin/tabs/index.php +0 -7
  55. views/inactive-star.php +1 -0
  56. views/inactive-stars.php +7 -0
  57. views/index.php +4 -3
  58. views/legend.php +14 -3
  59. views/markup.php +6 -26
  60. views/metabox/content.php +36 -0
  61. views/metabox/index.php +1 -31
  62. views/star.php +1 -19
  63. views/stars.php +4 -11
  64. views/structured-data.php +0 -14
LICENSE.txt CHANGED
@@ -1,4 +1,5 @@
1
  kk Star Ratings - WordPress Plugin
 
2
  Copyright (C) 2011-2019 Kamal Khan
3
 
4
  This program is free software; you can redistribute it and/or
1
  kk Star Ratings - WordPress Plugin
2
+
3
  Copyright (C) 2011-2019 Kamal Khan
4
 
5
  This program is free software; you can redistribute it and/or
index.php CHANGED
@@ -2,46 +2,84 @@
2
 
3
  /**
4
  * Plugin Name: kk Star Ratings
 
 
5
  * Plugin URI: https://github.com/kamalkhan/kk-star-ratings
6
  * Description: Allow blog visitors to involve and interact more effectively with your website by rating posts.
7
  * Author: Kamal Khan
8
  * Author URI: http://bhittani.com
9
  * Text Domain: kk-star-ratings
10
  * Domain Path: /languages
11
- * Version: 3.1.2
12
  * License: GPLv2 or later
13
- *
14
- * @package Bhittani\StarRating
15
  */
16
 
 
 
17
  if (! defined('ABSPATH')) {
18
  http_response_code(404);
19
  die();
20
  }
21
 
22
- define('KKSR_FILE', __FILE__);
23
- define('KKSR_PLUGIN', plugin_basename(KKSR_FILE));
24
- define('KKSR_VERSION', '3.1.2');
25
- define('KKSR_PREFIX', 'kksr_');
26
- define('KKSR_SLUG', 'kk-star-ratings');
27
- define('KKSR_LABEL', 'kk Star Ratings');
28
- define('KKSR_SHORTCODE', 'kkstarratings');
29
- define('KKSR_NAMESPACE', 'Bhittani\StarRating\\');
30
- define('KKSR_PATH', plugin_dir_path(KKSR_FILE));
31
- define('KKSR_PATH_SRC', KKSR_PATH . 'src/');
32
- define('KKSR_PATH_VIEWS', KKSR_PATH . 'views/');
33
- define('KKSR_PATH_PUBLIC', KKSR_PATH . 'public/');
34
- define('KKSR_URI', plugin_dir_url(KKSR_FILE) . 'public/');
35
-
36
- require_once KKSR_PATH . 'freemius.php';
37
-
38
- require_once KKSR_PATH_SRC . 'functions.php';
39
- require_once KKSR_PATH_SRC . 'activate.php';
40
- require_once KKSR_PATH_SRC . 'enqueue.php';
41
- require_once KKSR_PATH_SRC . 'markup.php';
42
- require_once KKSR_PATH_SRC . 'admin.php';
43
- require_once KKSR_PATH_SRC . 'metabox.php';
44
- require_once KKSR_PATH_SRC . 'ajax.php';
45
- require_once KKSR_PATH_SRC . 'structure.php';
46
- require_once KKSR_PATH_SRC . 'shortcode.php';
47
- require_once KKSR_PATH_SRC . 'legacy.php';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  /**
4
  * Plugin Name: kk Star Ratings
5
+ * Plugin Slug: kk-star-ratings
6
+ * Plugin Nick: kksr
7
  * Plugin URI: https://github.com/kamalkhan/kk-star-ratings
8
  * Description: Allow blog visitors to involve and interact more effectively with your website by rating posts.
9
  * Author: Kamal Khan
10
  * Author URI: http://bhittani.com
11
  * Text Domain: kk-star-ratings
12
  * Domain Path: /languages
13
+ * Version: 4.0.0
14
  * License: GPLv2 or later
 
 
15
  */
16
 
17
+ namespace Bhittani\StarRating;
18
+
19
  if (! defined('ABSPATH')) {
20
  http_response_code(404);
21
  die();
22
  }
23
 
24
+ define('KK_STAR_RATINGS', __FILE__);
25
+
26
+ if (file_exists($freemius = __DIR__.'/freemius.php')) {
27
+ require_once $freemius;
28
+ }
29
+
30
+ require_once __DIR__.'/src/config.php';
31
+
32
+ config([
33
+ 'views' => __DIR__.'/views/',
34
+ 'shortcode' => 'kkstarratings',
35
+ 'options' => [
36
+ // General
37
+ 'enable' => true,
38
+ 'strategies' => ['guests'],
39
+ 'manual_control' => [],
40
+ 'exclude_locations' => ['home', 'archives'],
41
+ 'exclude_categories' => [],
42
+ 'position' => 'top-left',
43
+ // Appearance
44
+ 'stars' => 5,
45
+ 'size' => 24,
46
+ 'greet' => 'Rate this [type]',
47
+ // Rich snippets
48
+ 'grs' => true,
49
+ 'sd' => <<<HTML
50
+ {
51
+ "@context": "https://schema.org/",
52
+ "@type": "CreativeWorkSeries",
53
+ "name": "[title]",
54
+ "aggregateRating": {
55
+ "@type": "AggregateRating",
56
+ "ratingValue": "[score]",
57
+ "bestRating": "[best]",
58
+ "ratingCount": "[count]"
59
+ }
60
+ }
61
+ HTML
62
+ ],
63
+ ]);
64
+
65
+ require_once __DIR__.'/src/global.php';
66
+ require_once __DIR__.'/src/hook.php';
67
+ require_once __DIR__.'/src/ajax.php';
68
+ require_once __DIR__.'/src/view.php';
69
+ require_once __DIR__.'/src/post.php';
70
+ require_once __DIR__.'/src/response.php';
71
+ require_once __DIR__.'/src/legacy.php';
72
+
73
+ if (is_admin()) {
74
+ require_once __DIR__.'/src/activate.php';
75
+ require_once __DIR__.'/src/admin.php';
76
+ require_once __DIR__.'/src/metabox.php';
77
+ } else {
78
+ require_once __DIR__.'/src/validate.php';
79
+ require_once __DIR__.'/src/shortcode.php';
80
+ require_once __DIR__.'/src/assets.php';
81
+ }
82
+
83
+ add_action('plugins_loaded', function () {
84
+ do_action(prefix('init'));
85
+ });
languages/index.php DELETED
@@ -1,5 +0,0 @@
1
- <?php
2
-
3
- http_response_code(404);
4
-
5
- die();
 
 
 
 
 
languages/kk-star-ratings.pot CHANGED
@@ -1,293 +1,244 @@
1
  # Copyright (C) 2019 Kamal Khan
2
- # This file is distributed under the GPLv2 or later.
3
  msgid ""
4
  msgstr ""
5
- "Project-Id-Version: kk Star Ratings 3.1.2\n"
6
- "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/index\n"
7
- "POT-Creation-Date: 2019-08-16 15:27:23+00:00\n"
8
- "MIME-Version: 1.0\n"
9
- "Content-Type: text/plain; charset=utf-8\n"
10
- "Content-Transfer-Encoding: 8bit\n"
11
- "PO-Revision-Date: 2019-MO-DA HO:MI+ZONE\n"
12
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
13
  "Language-Team: LANGUAGE <LL@li.org>\n"
14
- "X-Generator: grunt-wp-i18n 0.5.4\n"
15
- "X-Poedit-KeywordsList: "
16
- "__;_e;_x:1,2c;_ex:1,2c;_n:1,2;_nx:1,2,4c;_n_noop:1,2;_nx_noop:1,2,3c;esc_"
17
- "attr__;esc_html__;esc_attr_e;esc_html_e;esc_attr_x:1,2c;esc_html_x:1,2c;\n"
18
- "Language: en\n"
19
- "Plural-Forms: nplurals=2; plural=(n != 1);\n"
20
- "X-Poedit-Country: United States\n"
21
- "X-Poedit-SourceCharset: UTF-8\n"
22
- "X-Poedit-Basepath: ../\n"
23
- "X-Poedit-SearchPath-0: .\n"
24
- "X-Poedit-Bookmarks: \n"
25
- "X-Textdomain-Support: yes\n"
26
-
27
- #: src/admin/appearance.php:19
28
- msgid "Stars"
29
  msgstr ""
30
 
31
- #: src/admin/appearance.php:22
32
- msgid "Total number of stars."
33
  msgstr ""
34
 
35
- #: src/admin/appearance.php:29
36
- msgid "Size"
37
  msgstr ""
38
 
39
- #: src/admin/appearance.php:32
40
- msgid "Size of a single star."
41
  msgstr ""
42
 
43
- #: src/admin/appearance.php:39
44
- msgid "Fill color"
45
  msgstr ""
46
 
47
- #: src/admin/appearance.php:42
48
- msgid "Fill color of stars."
 
 
 
 
 
49
  msgstr ""
50
 
51
- #: src/admin/appearance.php:49
52
- msgid "Stroke color"
 
 
53
  msgstr ""
54
 
55
- #: src/admin/appearance.php:52
56
- msgid "Stroke color of stars."
57
  msgstr ""
58
 
59
- #: src/admin/appearance.php:59
60
- msgid "Active fill color"
 
61
  msgstr ""
62
 
63
- #: src/admin/appearance.php:62
64
- msgid "Fill color of active stars."
65
  msgstr ""
66
 
67
- #: src/admin/appearance.php:69
68
- msgid "Active stroke color"
69
  msgstr ""
70
 
71
- #: src/admin/appearance.php:72
72
- msgid "Stroke color of active stars."
73
  msgstr ""
74
 
75
- #: src/admin/appearance.php:79
76
- msgid "Hover fill color"
77
  msgstr ""
78
 
79
- #: src/admin/appearance.php:82
80
- msgid "Fill color of hover stars."
81
  msgstr ""
82
 
83
- #: src/admin/appearance.php:89
84
- msgid "Hover stroke color"
85
  msgstr ""
86
 
87
- #: src/admin/appearance.php:92
88
- msgid "Stroke color of hover stars."
89
  msgstr ""
90
 
91
- #: src/admin/general.php:49
92
- msgid "Posts"
93
  msgstr ""
94
 
95
- #: src/admin/general.php:50
96
- msgid "Pages"
 
97
  msgstr ""
98
 
99
- #: src/admin/general.php:57 src/admin/rich-snippets.php:18
100
- #: views/metabox/index.php:20
101
- msgid "Status"
 
 
 
 
 
 
 
 
 
 
 
102
  msgstr ""
103
 
104
- #: src/admin/general.php:58 src/admin/rich-snippets.php:19
 
 
 
 
 
 
 
 
 
 
 
 
105
  msgid "Active"
106
  msgstr ""
107
 
108
- #: src/admin/general.php:62
109
  msgid "Globally activate/deactivate the star ratings."
110
  msgstr ""
111
 
112
- #: src/admin/general.php:69
113
  msgid "Strategies"
114
  msgstr ""
115
 
116
- #: src/admin/general.php:71
117
  msgid "Select the voting strategies."
118
  msgstr ""
119
 
120
- #: src/admin/general.php:78
121
  msgid "Allow voting in archives"
122
  msgstr ""
123
 
124
- #: src/admin/general.php:85
125
  msgid "Allow guests to vote"
126
  msgstr ""
127
 
128
- #: src/admin/general.php:92
129
  msgid "Unique votes (based on IP Address)"
130
  msgstr ""
131
 
132
- #: src/admin/general.php:104
133
  msgid "Manual Control"
134
  msgstr ""
135
 
136
- #: src/admin/general.php:106
137
- msgid ""
138
- "Select the post types that should not auto embed the<br>markup and will be "
139
- "manually controlled by the theme.<br>E.g. Using %s in your template."
140
  msgstr ""
141
 
142
- #: src/admin/general.php:123
143
  msgid "Disable Locations"
144
  msgstr ""
145
 
146
- #: src/admin/general.php:125
147
  msgid "Select the locations where the star ratings should be excluded."
148
  msgstr ""
149
 
150
- #: src/admin/general.php:137
151
  msgid "Home page"
152
  msgstr ""
153
 
154
- #: src/admin/general.php:141
155
  msgid "Archives"
156
  msgstr ""
157
 
158
- #: src/admin/general.php:153
159
  msgid "Disable Categories"
160
  msgstr ""
161
 
162
- #: src/admin/general.php:160
163
- msgid ""
164
- "Exclude star ratings from posts belonging to the selected "
165
- "categories.<br>Use <strong>cmd/ctrl + click</strong> to select/deselect "
166
- "multiple categories."
167
  msgstr ""
168
 
169
- #: src/admin/general.php:167
170
  msgid "Default Position"
171
  msgstr ""
172
 
173
- #: src/admin/general.php:169
174
  msgid "Choose a default position."
175
  msgstr ""
176
 
177
- #: src/admin/general.php:173
178
  msgid "Top Left"
179
  msgstr ""
180
 
181
- #: src/admin/general.php:180
182
  msgid "Top Center"
183
  msgstr ""
184
 
185
- #: src/admin/general.php:187
186
  msgid "Top Right"
187
  msgstr ""
188
 
189
- #: src/admin/general.php:194
190
  msgid "Bottom Left"
191
  msgstr ""
192
 
193
- #: src/admin/general.php:201
194
  msgid "Bottom Center"
195
  msgstr ""
196
 
197
- #: src/admin/general.php:208
198
  msgid "Bottom Right"
199
  msgstr ""
200
 
201
- #: src/admin/rich-snippets.php:23
202
- msgid "Activate/deactivate rich snippets."
203
- msgstr ""
204
-
205
- #: src/admin/rich-snippets.php:29
206
- msgid "Context"
207
  msgstr ""
208
 
209
- #: src/admin/rich-snippets.php:32
210
- msgid "Structured data context."
211
  msgstr ""
212
 
213
- #: src/admin/rich-snippets.php:38
214
- msgid "Type"
215
- msgstr ""
216
-
217
- #: src/admin/rich-snippets.php:41
218
- msgid "Structured data type."
219
- msgstr ""
220
-
221
- #: src/ajax.php:22
222
- msgid "This action is forbidden."
223
- msgstr ""
224
-
225
- #: src/ajax.php:29
226
- msgid "Unauthorized."
227
- msgstr ""
228
-
229
- #: src/ajax.php:35
230
- msgid "An id is required to vote."
231
- msgstr ""
232
-
233
- #: src/ajax.php:49
234
- msgid "Not allowed to vote."
235
- msgstr ""
236
-
237
- #: src/ajax.php:55
238
- msgid "A rating is required to vote."
239
- msgstr ""
240
-
241
- #: src/functions.php:372
242
- msgid "You can only rate between 0 and %s."
243
- msgstr ""
244
-
245
- #: src/functions.php:511
246
- msgid "General"
247
- msgstr ""
248
-
249
- #: src/functions.php:512
250
- msgid "Rich Snippets"
251
- msgstr ""
252
-
253
- #: src/functions.php:513
254
- msgid "Appearance"
255
- msgstr ""
256
-
257
- #: views/metabox/index.php:13
258
- msgid "Reset Ratings"
259
- msgstr ""
260
-
261
- #: views/metabox/index.php:26
262
- msgid "Auto"
263
- msgstr ""
264
-
265
- #: views/metabox/index.php:30
266
- msgid "Enable"
267
- msgstr ""
268
-
269
- #: views/metabox/index.php:34
270
- msgid "Disable"
271
  msgstr ""
272
 
273
- #. Plugin Name of the plugin/theme
274
- msgid "kk Star Ratings"
275
  msgstr ""
276
 
277
- #. Plugin URI of the plugin/theme
278
- msgid "https://github.com/kamalkhan/kk-star-ratings"
279
  msgstr ""
280
 
281
- #. Description of the plugin/theme
282
- msgid ""
283
- "Allow blog visitors to involve and interact more effectively with your "
284
- "website by rating posts."
285
  msgstr ""
286
 
287
- #. Author of the plugin/theme
288
- msgid "Kamal Khan"
289
  msgstr ""
290
-
291
- #. Author URI of the plugin/theme
292
- msgid "http://bhittani.com"
293
- msgstr ""
1
  # Copyright (C) 2019 Kamal Khan
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.0.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: 2019-10-26T15:44:59+00:00\n"
13
+ "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
14
+ "X-Generator: WP-CLI 2.1.0\n"
15
+ "X-Domain: kk-star-ratings\n"
16
+
17
+ #. Plugin Name of the plugin
18
+ msgid "kk Star Ratings"
 
 
 
 
 
19
  msgstr ""
20
 
21
+ #. Plugin URI of the plugin
22
+ msgid "https://github.com/kamalkhan/kk-star-ratings"
23
  msgstr ""
24
 
25
+ #. Description of the plugin
26
+ msgid "Allow blog visitors to involve and interact more effectively with your website by rating posts."
27
  msgstr ""
28
 
29
+ #. Author of the plugin
30
+ msgid "Kamal Khan"
31
  msgstr ""
32
 
33
+ #. Author URI of the plugin
34
+ msgid "http://bhittani.com"
35
  msgstr ""
36
 
37
+ #: views/legend.php:9
38
+ msgid "vote"
39
+ msgid_plural "votes"
40
+ msgstr[0] ""
41
+
42
+ #: views/metabox/content.php:12
43
+ msgid "Reset Ratings"
44
  msgstr ""
45
 
46
+ #: views/metabox/content.php:19
47
+ #: src/admin/rich-snippets.php:30
48
+ #: src/admin/general.php:50
49
+ msgid "Status"
50
  msgstr ""
51
 
52
+ #: views/metabox/content.php:25
53
+ msgid "Auto"
54
  msgstr ""
55
 
56
+ #: views/metabox/content.php:29
57
+ #: src/admin/rich-snippets.php:31
58
+ msgid "Enable"
59
  msgstr ""
60
 
61
+ #: views/metabox/content.php:33
62
+ msgid "Disable"
63
  msgstr ""
64
 
65
+ #: src/ajax.php:27
66
+ msgid "This action is forbidden."
67
  msgstr ""
68
 
69
+ #: src/ajax.php:35
70
+ msgid "An id is required to vote."
71
  msgstr ""
72
 
73
+ #: src/ajax.php:46
74
+ msgid "You are not allowed to vote."
75
  msgstr ""
76
 
77
+ #: src/ajax.php:54
78
+ msgid "A rating is required to vote."
79
  msgstr ""
80
 
81
+ #: src/admin/rich-snippets.php:38
82
+ msgid "Enable/disable rich snippets."
83
  msgstr ""
84
 
85
+ #: src/admin/rich-snippets.php:43
86
+ msgid "Content"
87
  msgstr ""
88
 
89
+ #: src/admin/rich-snippets.php:47
90
+ msgid "Provide the %s structure."
91
  msgstr ""
92
 
93
+ #: src/admin/rich-snippets.php:48
94
+ #: src/admin/appearance.php:31
95
+ msgid "The following variables are available:"
96
  msgstr ""
97
 
98
+ #: src/admin/rich-snippets.php:49
99
+ msgid "%s Post title."
100
+ msgstr ""
101
+
102
+ #: src/admin/rich-snippets.php:50
103
+ msgid "%s Average post ratings."
104
+ msgstr ""
105
+
106
+ #: src/admin/rich-snippets.php:51
107
+ msgid "%s Number of votes casted."
108
+ msgstr ""
109
+
110
+ #: src/admin/rich-snippets.php:52
111
+ msgid "%s Total amount of stars."
112
  msgstr ""
113
 
114
+ #: src/admin/rich-snippets.php:54
115
+ msgid "You may <a href=\"%s\" target=\"_blank\">test the snippet here</a>."
116
+ msgstr ""
117
+
118
+ #: src/admin/general.php:27
119
+ msgid "Posts"
120
+ msgstr ""
121
+
122
+ #: src/admin/general.php:28
123
+ msgid "Pages"
124
+ msgstr ""
125
+
126
+ #: src/admin/general.php:51
127
  msgid "Active"
128
  msgstr ""
129
 
130
+ #: src/admin/general.php:58
131
  msgid "Globally activate/deactivate the star ratings."
132
  msgstr ""
133
 
134
+ #: src/admin/general.php:64
135
  msgid "Strategies"
136
  msgstr ""
137
 
138
+ #: src/admin/general.php:66
139
  msgid "Select the voting strategies."
140
  msgstr ""
141
 
142
+ #: src/admin/general.php:73
143
  msgid "Allow voting in archives"
144
  msgstr ""
145
 
146
+ #: src/admin/general.php:80
147
  msgid "Allow guests to vote"
148
  msgstr ""
149
 
150
+ #: src/admin/general.php:87
151
  msgid "Unique votes (based on IP Address)"
152
  msgstr ""
153
 
154
+ #: src/admin/general.php:98
155
  msgid "Manual Control"
156
  msgstr ""
157
 
158
+ #: src/admin/general.php:100
159
+ msgid "Select the post types that should not auto embed the<br>markup and will be manually controlled by the theme.<br>E.g. Using %s in your template."
 
 
160
  msgstr ""
161
 
162
+ #: src/admin/general.php:116
163
  msgid "Disable Locations"
164
  msgstr ""
165
 
166
+ #: src/admin/general.php:118
167
  msgid "Select the locations where the star ratings should be excluded."
168
  msgstr ""
169
 
170
+ #: src/admin/general.php:130
171
  msgid "Home page"
172
  msgstr ""
173
 
174
+ #: src/admin/general.php:134
175
  msgid "Archives"
176
  msgstr ""
177
 
178
+ #: src/admin/general.php:146
179
  msgid "Disable Categories"
180
  msgstr ""
181
 
182
+ #: src/admin/general.php:158
183
+ msgid "Exclude star ratings from posts belonging to the selected categories.<br>Use <strong>cmd/ctrl + click</strong> to select/deselect multiple categories."
 
 
 
184
  msgstr ""
185
 
186
+ #: src/admin/general.php:164
187
  msgid "Default Position"
188
  msgstr ""
189
 
190
+ #: src/admin/general.php:166
191
  msgid "Choose a default position."
192
  msgstr ""
193
 
194
+ #: src/admin/general.php:170
195
  msgid "Top Left"
196
  msgstr ""
197
 
198
+ #: src/admin/general.php:177
199
  msgid "Top Center"
200
  msgstr ""
201
 
202
+ #: src/admin/general.php:184
203
  msgid "Top Right"
204
  msgstr ""
205
 
206
+ #: src/admin/general.php:191
207
  msgid "Bottom Left"
208
  msgstr ""
209
 
210
+ #: src/admin/general.php:198
211
  msgid "Bottom Center"
212
  msgstr ""
213
 
214
+ #: src/admin/general.php:205
215
  msgid "Bottom Right"
216
  msgstr ""
217
 
218
+ #: src/admin/appearance.php:26
219
+ msgid "Greeting text"
 
 
 
 
220
  msgstr ""
221
 
222
+ #: src/admin/appearance.php:30
223
+ msgid "Text that will be displayed when no votes have been casted."
224
  msgstr ""
225
 
226
+ #: src/admin/appearance.php:32
227
+ msgid "%s Post type."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
  msgstr ""
229
 
230
+ #: src/admin/appearance.php:38
231
+ msgid "Stars"
232
  msgstr ""
233
 
234
+ #: src/admin/appearance.php:42
235
+ msgid "Total number of stars."
236
  msgstr ""
237
 
238
+ #: src/admin/appearance.php:47
239
+ msgid "Size"
 
 
240
  msgstr ""
241
 
242
+ #: src/admin/appearance.php:51
243
+ msgid "Size of a single star."
244
  msgstr ""
 
 
 
 
public/css/index.php CHANGED
@@ -1,5 +1,6 @@
1
  <?php
2
 
3
- http_response_code(404);
4
-
5
- die();
 
1
  <?php
2
 
3
+ if (! defined('ABSPATH')) {
4
+ http_response_code(404);
5
+ die();
6
+ }
public/css/kk-star-ratings.css CHANGED
@@ -1,178 +1,122 @@
1
  .kk-star-ratings {
2
- display: -webkit-box !important;
3
- display: -webkit-flex !important;
4
- display: -ms-flexbox !important;
5
- display: flex !important;
6
  -webkit-box-align: center;
7
  -webkit-align-items: center;
8
  -ms-flex-align: center;
9
  align-items: center;
 
10
  }
11
 
12
- .kk-star-ratings.kksr-top {
13
  margin-bottom: 2rem;
 
 
 
 
14
  }
15
 
16
- .kk-star-ratings.kksr-bottom {
17
- /* margin-top: 2rem; */
18
- margin: 2rem 0;
 
 
 
19
  }
20
 
21
- .kk-star-ratings.kksr-center {
 
 
 
 
 
 
 
22
  -webkit-box-pack: center;
23
  -webkit-justify-content: center;
24
  -ms-flex-pack: center;
25
  justify-content: center;
26
  }
27
 
28
- .kk-star-ratings.kksr-right {
29
  -webkit-box-pack: flex-end;
30
  -webkit-justify-content: flex-end;
31
  -ms-flex-pack: flex-end;
32
  justify-content: flex-end;
33
  }
34
 
 
 
 
 
35
  .kk-star-ratings .kksr-stars {
 
36
  position: relative;
37
- display: table;
38
- line-height: 0;
39
  }
40
 
41
- .kk-star-ratings .kksr-stars .kksr-inactive-stars .kksr-star,
42
- .kk-star-ratings .kksr-stars .kksr-active-stars .kksr-star {
43
- padding-right: 4px;
44
- }
45
- .kk-star-ratings .kksr-stars .kksr-inactive-stars .kksr-star:last-child,
46
- .kk-star-ratings .kksr-stars .kksr-active-stars .kksr-star:last-child {
47
- padding-right: 0;
48
  }
49
 
50
- .kk-star-ratings .kksr-stars .kksr-active-stars {
 
51
  position: absolute;
52
  top: 0;
53
  left: 0;
54
- overflow: hidden;
55
  }
56
 
57
  .kk-star-ratings .kksr-stars .kksr-star {
 
58
  cursor: pointer;
59
- display: table-cell;
60
  }
61
 
62
- .kk-star-ratings .kksr-stars .kksr-star svg {
63
- pointer-events: none;
64
- -webkit-transition: all .2s ease-in-out;
65
- transition: all .2s ease-in-out;
66
- }
67
-
68
- .kk-star-ratings.kksr-disable .kksr-stars .kksr-star {
69
  cursor: default;
70
  }
71
 
72
- .kk-star-ratings:not(.kksr-disable) .kksr-stars:hover .kksr-active-stars {
73
- display: none;
 
 
74
  }
75
 
76
- /* Legend */
77
 
78
- .kk-star-ratings .kksr-legend {
79
- color: #fff;
80
- border-radius: 10rem;
81
- margin-right: 8px;
82
- font-family: monospace;
83
- display: -webkit-box;
84
- display: -webkit-flex;
85
- display: -ms-flexbox;
86
- display: flex;
87
- -webkit-box-align: center;
88
- -webkit-align-items: center;
89
- -ms-flex-align: center;
90
- align-items: center;
91
  }
92
 
93
- .kk-star-ratings .kksr-legend .kksr-legend-score {
94
- font-weight: bold;
95
- padding: 0 8px;
 
96
  }
97
 
98
- .kk-star-ratings .kksr-legend .kksr-legend-meta {
99
- background-color: #fff;
100
- border-radius: 0 10rem 10rem 0;
101
- margin-right: 2px;
102
- padding: 0 6px;
103
  }
104
 
105
- /* Size */
106
- .kk-star-ratings .kksr-stars .kksr-star svg {
107
- width: 24px;
108
- height: 24px;
109
  }
110
 
111
- /* Colors */
112
-
113
- .kk-star-ratings .kksr-stars .kksr-star svg,
114
- .kk-star-ratings:not(.kksr-disable) .kksr-stars .kksr-star:hover ~ .kksr-star svg {
115
- fill: #fff;
116
- stroke: #333;
117
- }
118
- .kk-star-ratings .kksr-stars .kksr-active-stars .kksr-star svg {
119
- fill: #ddd;
120
- stroke: #333;
121
- }
122
- .kk-star-ratings:not(.kksr-disable) .kksr-stars:hover .kksr-star svg {
123
- fill: #f2fa6f;
124
- stroke: #333;
125
- }
126
  .kk-star-ratings .kksr-legend {
127
- background-color: #333;
128
- }
129
- .kk-star-ratings .kksr-legend .kksr-legend-meta {
130
- color: #333;
 
131
  }
132
 
133
  /* RTL */
134
 
135
- .kk-star-ratings.kksr-rtl {
136
- direction: ltr;
137
- }
138
-
139
- /* .kk-star-ratings.kksr-rtl.kksr-right {
140
- -webkit-box-pack: flex-start;
141
- -webkit-justify-content: flex-start;
142
- -ms-flex-pack: flex-start;
143
- justify-content: flex-start;
144
- } */
145
-
146
- /* RTL - stars */
147
-
148
- /* .kk-star-ratings.kksr-rtl .kksr-stars .kksr-star {
149
- padding-left: 4px;
150
- padding-right: 0;
151
- }
152
-
153
- .kk-star-ratings.kksr-rtl .kksr-stars .kksr-active-stars {
154
- top: 0;
155
  right: 0;
156
  }
157
-
158
- .kk-star-ratings.kksr-rtl .kksr-stars .kksr-inactive-stars .kksr-star,
159
- .kk-star-ratings.kksr-rtl .kksr-stars .kksr-active-stars .kksr-star {
160
- padding-left: 4px;
161
- padding-right: 0;
162
- }
163
- .kk-star-ratings.kksr-rtl .kksr-stars .kksr-inactive-stars .kksr-star:last-child,
164
- .kk-star-ratings.kksr-rtl .kksr-stars .kksr-active-stars .kksr-star:last-child {
165
- padding-left: 0;
166
- } */
167
-
168
- /* RTL - legend */
169
-
170
- /* .kk-star-ratings.kksr-rtl .kksr-legend {
171
- margin-left: 8px;
172
- margin-right: 0;
173
- }
174
- .kk-star-ratings.kksr-rtl .kksr-legend .kksr-legend-meta {
175
- margin-left: 0;
176
- margin-right: 2px;
177
- border-radius: 10rem 0 0 10rem;
178
- } */
1
  .kk-star-ratings {
2
+ display: -webkit-inline-box !important;
3
+ display: -webkit-inline-flex !important;
4
+ display: -ms-inline-flexbox !important;
5
+ display: inline-flex !important;
6
  -webkit-box-align: center;
7
  -webkit-align-items: center;
8
  -ms-flex-align: center;
9
  align-items: center;
10
+ vertical-align: text-top;
11
  }
12
 
13
+ .kk-star-ratings.kksr-valign-top {
14
  margin-bottom: 2rem;
15
+ display: -webkit-box !important;
16
+ display: -webkit-flex !important;
17
+ display: -ms-flexbox !important;
18
+ display: flex !important;
19
  }
20
 
21
+ .kk-star-ratings.kksr-valign-bottom {
22
+ margin-top: 2rem;
23
+ display: -webkit-box !important;
24
+ display: -webkit-flex !important;
25
+ display: -ms-flexbox !important;
26
+ display: flex !important;
27
  }
28
 
29
+ .kk-star-ratings.kksr-align-left {
30
+ -webkit-box-pack: flex-start;
31
+ -webkit-justify-content: flex-start;
32
+ -ms-flex-pack: flex-start;
33
+ justify-content: flex-start;
34
+ }
35
+
36
+ .kk-star-ratings.kksr-align-center {
37
  -webkit-box-pack: center;
38
  -webkit-justify-content: center;
39
  -ms-flex-pack: center;
40
  justify-content: center;
41
  }
42
 
43
+ .kk-star-ratings.kksr-align-right {
44
  -webkit-box-pack: flex-end;
45
  -webkit-justify-content: flex-end;
46
  -ms-flex-pack: flex-end;
47
  justify-content: flex-end;
48
  }
49
 
50
+ .kk-star-ratings .kksr-muted {
51
+ opacity: .5;
52
+ }
53
+
54
  .kk-star-ratings .kksr-stars {
55
+ /* display: flex; */
56
  position: relative;
 
 
57
  }
58
 
59
+ .kk-star-ratings .kksr-stars .kksr-stars-active,
60
+ .kk-star-ratings .kksr-stars .kksr-stars-inactive {
61
+ display: flex;
 
 
 
 
62
  }
63
 
64
+ .kk-star-ratings .kksr-stars .kksr-stars-active {
65
+ overflow: hidden;
66
  position: absolute;
67
  top: 0;
68
  left: 0;
 
69
  }
70
 
71
  .kk-star-ratings .kksr-stars .kksr-star {
72
+ /* display: inline-flex; */
73
  cursor: pointer;
 
74
  }
75
 
76
+ .kk-star-ratings.kksr-disabled .kksr-stars .kksr-star {
 
 
 
 
 
 
77
  cursor: default;
78
  }
79
 
80
+ .kk-star-ratings .kksr-stars .kksr-star .kksr-icon {
81
+ /* fill: currentColor;
82
+ stroke: currentColor; */
83
+ transition: .3s all;
84
  }
85
 
86
+ /* Images */
87
 
88
+ .kk-star-ratings:not(.kksr-disabled) .kksr-stars:hover .kksr-stars-active {
89
+ width: 0 !important;
 
 
 
 
 
 
 
 
 
 
 
90
  }
91
 
92
+ .kk-star-ratings .kksr-stars .kksr-star .kksr-icon,
93
+ .kk-star-ratings:not(.kksr-disabled) .kksr-stars .kksr-star:hover ~ .kksr-star .kksr-icon {
94
+ /* background-image: url("/wp-content/plugins/kk-star-ratings/public/gray.png"); */
95
+ background-image: url("/wp-content/plugins/kk-star-ratings/public/svg/inactive.svg");
96
  }
97
 
98
+ .kk-star-ratings .kksr-stars .kksr-stars-active .kksr-star .kksr-icon {
99
+ /* background-image: url("/wp-content/plugins/kk-star-ratings/public/orange.png"); */
100
+ background-image: url("/wp-content/plugins/kk-star-ratings/public/svg/active.svg");
 
 
101
  }
102
 
103
+ .kk-star-ratings.kksr-disabled .kksr-stars .kksr-stars-active .kksr-star .kksr-icon,
104
+ .kk-star-ratings:not(.kksr-disabled) .kksr-stars:hover .kksr-star .kksr-icon {
105
+ /* background-image: url("/wp-content/plugins/kk-star-ratings/public/yellow.png"); */
106
+ background-image: url("/wp-content/plugins/kk-star-ratings/public/svg/selected.svg");
107
  }
108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  .kk-star-ratings .kksr-legend {
110
+ margin-left: .75rem;
111
+ margin-right: .75rem;
112
+ font-size: 90%;
113
+ opacity: .8;
114
+ line-height: 1;
115
  }
116
 
117
  /* RTL */
118
 
119
+ [dir="rtl"] .kk-star-ratings .kksr-stars .kksr-stars-active {
120
+ left: auto;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  right: 0;
122
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
public/css/kk-star-ratings.css.php DELETED
@@ -1,31 +0,0 @@
1
- /* Size */
2
- .kk-star-ratings .kksr-stars .kksr-star svg {
3
- width: <?php echo $size; ?>px;
4
- height: <?php echo $size; ?>px;
5
- }
6
-
7
- /* Colors */
8
-
9
- .kk-star-ratings .kksr-stars .kksr-star svg,
10
- .kk-star-ratings:not(.kksr-disable) .kksr-stars .kksr-star:hover ~ .kksr-star svg {
11
- fill: <?php echo $colors['default']['fill']; ?>;
12
- stroke: <?php echo $colors['default']['stroke']; ?>;
13
- }
14
-
15
- .kk-star-ratings .kksr-stars .kksr-active-stars .kksr-star svg {
16
- fill: <?php echo $colors['active']['fill']; ?>;
17
- stroke: <?php echo $colors['active']['stroke']; ?>;
18
- }
19
-
20
- .kk-star-ratings:not(.kksr-disable) .kksr-stars:hover .kksr-star svg {
21
- fill: <?php echo $colors['hover']['fill']; ?>;
22
- stroke: <?php echo $colors['hover']['stroke']; ?>;
23
- }
24
-
25
- .kk-star-ratings .kksr-legend {
26
- background-color: <?php echo $colors['active']['stroke']; ?>;
27
- }
28
-
29
- .kk-star-ratings .kksr-legend .kksr-legend-meta {
30
- color: <?php echo $colors['active']['stroke']; ?>;
31
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
public/index.php CHANGED
@@ -1,5 +1,6 @@
1
  <?php
2
 
3
- http_response_code(404);
4
-
5
- die();
 
1
  <?php
2
 
3
+ if (! defined('ABSPATH')) {
4
+ http_response_code(404);
5
+ die();
6
+ }
public/js/index.php CHANGED
@@ -1,5 +1,6 @@
1
  <?php
2
 
3
- http_response_code(404);
4
-
5
- die();
 
1
  <?php
2
 
3
+ if (! defined('ABSPATH')) {
4
+ http_response_code(404);
5
+ die();
6
+ }
public/js/kk-star-ratings.js CHANGED
@@ -1,61 +1,67 @@
1
- jQuery(document).ready(function ($) {
2
- $('.kk-star-ratings').each(function (i, el) {
3
- var $el = $(el);
4
- var $activeStars = $('.kksr-active-stars', $el);
5
- var $legend = $('.kksr-legend', $el);
6
- var $score = $('.kksr-legend-score', $legend);
7
- var $count = $('.kksr-legend-meta', $legend);
8
 
9
- var busy = false;
 
 
10
 
11
- $el.on('click', function (e) {
12
- if (busy || $el.hasClass('kksr-disable')) {
 
13
  return;
14
  }
15
 
16
- busy = true;
17
-
18
- var id = $el.data('id');
19
-
20
- if (! id) {
21
- return busy = false;
22
- }
23
-
24
- var star = e.target;
25
- var $star = $(star);
26
- var rating = $star.data('star');
27
-
28
- if (! rating) {
29
- return busy = false;
30
- }
31
 
32
  $.ajax({
33
  type: 'POST',
34
  url: kk_star_ratings.endpoint,
35
- data: {
36
- id: id,
37
- rating: rating,
38
- action: 'kk-star-ratings',
39
- nonce: kk_star_ratings.nonce
40
- },
41
- dataType: 'json',
42
- success: function (response) {
43
- $activeStars.width(response.width + 'px');
44
- $score.text(response.score);
45
- $count.text(response.count);
46
- $legend.show();
47
- if (response.disable) {
48
- $el.addClass('kksr-disable');
49
- }
50
- console.log('success', response);
51
- },
52
- error: function (xhr) {
53
- console.log('error', xhr, xhr.responseJSON);
54
- },
55
  complete: function () {
56
- busy = false;
57
  }
58
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  });
 
 
 
 
60
  });
61
  });
1
+ "use strict";
 
 
 
 
 
 
2
 
3
+ jQuery(document).ready(function ($) {
4
+ function apply ($el, options) {
5
+ var options = options || { isBusy: false };
6
 
7
+ function ajax(data, successCallback, errorCallback)
8
+ {
9
+ if (options.isBusy || $el.hasClass('kksr-disabled')) {
10
  return;
11
  }
12
 
13
+ options.isBusy = true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  $.ajax({
16
  type: 'POST',
17
  url: kk_star_ratings.endpoint,
18
+ data: Object.assign({
19
+ nonce: kk_star_ratings.nonce,
20
+ action: kk_star_ratings.action
21
+ }, data),
22
+ error: errorCallback,
23
+ success: successCallback,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  complete: function () {
25
+ options.isBusy = false;
26
  }
27
  });
28
+ }
29
+
30
+ function onClick(e) {
31
+ var $star = $(this);
32
+
33
+ ajax({
34
+ id: $el.data('id'),
35
+ slug: $el.data('slug'),
36
+ score: $star.data('star'),
37
+ best: $('[data-star]', $el).length
38
+ }, function (response, status, xhr) {
39
+ var $newEl = $(response);
40
+ $newEl.addClass($el.attr('class'));
41
+ $el.replaceWith($newEl);
42
+ destroy();
43
+ apply($newEl, options);
44
+ }, function (xhr, status, err) {
45
+ if (xhr.responseJSON && xhr.responseJSON.error) {
46
+ console.error(xhr.responseJSON.error);
47
+ }
48
+ });
49
+ }
50
+
51
+ function destroy() {
52
+ $('[data-star]', $el).each(function () {
53
+ $(this).off('click', onClick);
54
+ });
55
+
56
+ $el.remove();
57
+ }
58
+
59
+ $('[data-star]', $el).each(function () {
60
+ $(this).on('click', onClick);
61
  });
62
+ }
63
+
64
+ $('.kk-star-ratings').each(function () {
65
+ apply($(this))
66
  });
67
  });
public/svg/active.svg ADDED
@@ -0,0 +1 @@
 
1
+ <svg fill="#ff8c00" stroke="tomato" stroke-width=".5" viewBox="0 0 26 26" xmlns="http://www.w3.org/2000/svg"><path d="m12 .587 3.668 7.568 8.332 1.151-6.064 5.828 1.48 8.279-7.416-3.967-7.417 3.967 1.481-8.279-6.064-5.828 8.332-1.151z"/></svg>
public/svg/inactive.svg ADDED
@@ -0,0 +1 @@
 
1
+ <svg fill="#ebe8e8" stroke="#ddd" stroke-width="1" viewBox="0 0 26 26" xmlns="http://www.w3.org/2000/svg"><path d="m12 .587 3.668 7.568 8.332 1.151-6.064 5.828 1.48 8.279-7.416-3.967-7.417 3.967 1.481-8.279-6.064-5.828 8.332-1.151z"/></svg>
public/svg/index.php ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if (! defined('ABSPATH')) {
4
+ http_response_code(404);
5
+ die();
6
+ }
public/svg/selected.svg ADDED
@@ -0,0 +1 @@
 
1
+ <svg fill="#ff0" stroke="#fa8072" viewBox="0 0 26 26" xmlns="http://www.w3.org/2000/svg"><path d="m12 .587 3.668 7.568 8.332 1.151-6.064 5.828 1.48 8.279-7.416-3.967-7.417 3.967 1.481-8.279-6.064-5.828 8.332-1.151z"/></svg>
readme.txt CHANGED
@@ -3,9 +3,9 @@ Contributors: bhittani
3
  Donate link: https://github.com/kamalkhan/kk-star-ratings
4
  Tags: star ratings, votings, rate posts, ajax ratings, infinite stars, unlimited stars, google rich snippets, structured data, SEO, SERP
5
  Requires at least: 4.5
6
- Requires PHP: 5.5.9
7
- Tested up to: 5.2.2
8
- Stable tag: 3.1.2
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
@@ -13,7 +13,7 @@ License URI: http://www.gnu.org/licenses/gpl-2.0.html
13
 
14
  kk Star Ratings allows blog visitors to involve and interact more effectively with your website by rating posts.
15
 
16
- This plugin has been renewed from the ground up as of v3 covered by unit tests.
17
 
18
 
19
 
@@ -92,66 +92,12 @@ The source of this plugin is located at [Github](https://github.com/kamalkhan/kk
92
 
93
  == Changelog ==
94
 
95
- = 3.1.1 =
96
 
97
- **Fixed **
98
- - GitHub PR #84: Voting is now disabled on the current page when unique IP is enforced.
99
 
100
- **Changed**
101
- - GitHub PR #83: Trim extra spacing in the legend.
102
-
103
- = 3.1.0 =
104
-
105
- **Added**
106
- - Bottom margin added when bottom position in effect.
107
- - Ability to reset ratings for individual posts/pages.
108
- - Enable/disable star ratings for individual posts/pages.
109
- - Take manual control of the auto embedded markup to avoid duplication when using in a template.
110
-
111
- **Fixed**
112
- - Markup is now hidden under AMP.
113
- - Assets are now enqueued when manually/forcefully loading the markup via template function.
114
-
115
- **Changed**
116
- - Default colors have been updated.
117
- - Default size is now 22px instead of 24px.
118
- - Structured data now uses ratingCount instead of reviewCount.
119
-
120
- **Removed**
121
- - Nothing
122
-
123
- **Deprecated**
124
- - Nothing
125
-
126
- **Security**
127
- - Nothing
128
-
129
- = 3.0.0 =
130
-
131
- **Added**
132
- - Optionally allow guests to vote.
133
- - Ratings can also be included in publicly accessible custom posts.
134
-
135
- **Fixed**
136
- - Google rich snippets.
137
- - AJAX call on every load causing high CPU usage.
138
-
139
- **Changed**
140
- - Stars are now based on svg.
141
- - Appearance has been simplified.
142
- - Html based structured data has been replaced by json based structured data.
143
- - kk_star_ratings_get function no longer includes the post_title key.
144
-
145
- **Removed**
146
- - Labels have been removed.
147
- - Top posts widget has been removed in favor of a future addon.
148
- - Admin table colum has been removed in favor of a future addon.
149
-
150
- **Deprecated**
151
- - Nothing
152
-
153
- **Security**
154
- - Nothing
155
 
156
  = 2.x =
157
  [Archived](.github/CHANGELOG-v2.md)
@@ -161,7 +107,10 @@ The source of this plugin is located at [Github](https://github.com/kamalkhan/kk
161
 
162
  == Upgrade Notice ==
163
 
164
- = 3.0.0 =
 
 
 
165
  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.
166
 
167
 
3
  Donate link: https://github.com/kamalkhan/kk-star-ratings
4
  Tags: star ratings, votings, rate posts, ajax ratings, infinite stars, unlimited stars, google rich snippets, structured data, SEO, SERP
5
  Requires at least: 4.5
6
+ Requires PHP: 5.6
7
+ Tested up to: 5.2.4
8
+ Stable tag: 4.0.0
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
13
 
14
  kk Star Ratings allows blog visitors to involve and interact more effectively with your website by rating posts.
15
 
16
+ This plugin has been renewed from the ground up as of v4.
17
 
18
 
19
 
92
 
93
  == Changelog ==
94
 
95
+ = 4.x =
96
 
97
+ - [View Changelog](CHANGELOG.md)
 
98
 
99
+ = 3.x =
100
+ [Archived](.github/CHANGELOG-v3.md)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
  = 2.x =
103
  [Archived](.github/CHANGELOG-v2.md)
107
 
108
  == Upgrade Notice ==
109
 
110
+ = 4.x =
111
+ 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.
115
 
116
 
src/activate.php CHANGED
@@ -5,37 +5,95 @@
5
  *
6
  * (c) Kamal Khan <shout@bhittani.com>
7
  *
8
- * This source file is subject to the GPL v2 license that
9
- * is bundled with this source code in the file LICENSE.
10
  */
11
 
12
  namespace Bhittani\StarRating;
13
 
14
- register_activation_hook(KKSR_FILE, KKSR_NAMESPACE.'activate'); function activate()
 
 
 
 
 
 
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.
22
- upgradeRatings();
23
  }
24
 
25
- // if (! $previousVersion) {
26
- // // We have never used any version of this plugin.
27
- // // Save the default options.
28
- // saveOptions(getDefaultOptions());
29
- // } else if (version_compare($previousVersion, '3.0.0', '<')) {
30
- // // We were using a legacy version of the plugin.
31
- // // Synchronize the options.
32
- // saveOptions(getOptions());
33
- // }
34
-
35
- if (! $previousVersion || version_compare($previousVersion, '3.0.0', '<')) {
36
- saveOptions(getOptions());
 
 
 
 
 
 
37
  }
 
 
 
 
 
 
 
 
 
 
38
 
39
- // Save the new version.
40
- saveOption('ver', KKSR_VERSION);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  }
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
  namespace Bhittani\StarRating;
13
 
14
+ if (! defined('ABSPATH')) {
15
+ http_response_code(404);
16
+ die();
17
+ }
18
+
19
+ register_activation_hook(config('file'), __NAMESPACE__.'\activate');
20
+ function activate()
21
  {
22
+ do_plugin_action('activate', config('version'), get_option(prefix('ver')));
23
+
24
+ update_option(prefix('ver'), config('version'));
25
+ }
26
 
27
+ add_plugin_action('activate', __NAMESPACE__.'\upgrade_options', 9, 2);
28
+ function upgrade_options($version, $previous)
29
+ {
30
+ if ($previous && version_compare($previous, '3.0.1', '<')) {
31
+ return;
32
  }
33
 
34
+ $options = [
35
+ 'strategies' => get_option(prefix('strategies'), array_filter([
36
+ 'guests',
37
+ get_option(prefix('unique')) ? 'unique' : null,
38
+ get_option(prefix('disable_in_archives'), true) ? null : 'archives',
39
+ ])),
40
+ 'exclude_locations' => get_option(prefix('exclude_locations'), array_filter([
41
+ get_option(prefix('show_in_home'), true) ? null : 'home',
42
+ get_option(prefix('show_in_posts'), true) ? null : 'post',
43
+ get_option(prefix('show_in_pages'), true) ? null : 'page',
44
+ get_option(prefix('show_in_archives'), true) ? null : 'archives',
45
+ ])),
46
+ 'exclude_categories' => is_array($exludedCategories = get_option(prefix('exclude_categories'), []))
47
+ ? $exludedCategories : array_map('trim', explode(',', $exludedCategories)),
48
+ ];
49
+
50
+ foreach ($options as $key => $value) {
51
+ update_option(prefix($key), $value);
52
  }
53
+ }
54
+
55
+ add_plugin_action('activate', __NAMESPACE__.'\upgrade_post_ratings', 9, 2);
56
+ function upgrade_post_ratings($version, $previous)
57
+ {
58
+ if ($previous && version_compare($previous, '3.0.1', '<')) {
59
+ return;
60
+ }
61
+
62
+ global $wpdb;
63
 
64
+ // Truncate IP addresses.
65
+ $wpdb->delete($wpdb->postmeta, ['meta_key' => meta_prefix('ips')]);
66
+
67
+ // Normalize post ratings.
68
+
69
+ $rows = $wpdb->get_results("
70
+ SELECT posts.ID, postmeta_avg.meta_value as avg, postmeta_casts.meta_value as casts
71
+ FROM {$wpdb->posts} posts
72
+ JOIN {$wpdb->postmeta} postmeta_avg ON posts.ID = postmeta_avg.post_id
73
+ JOIN {$wpdb->postmeta} postmeta_casts ON posts.ID = postmeta_casts.post_id
74
+ WHERE postmeta_avg.meta_key = '_kksr_avg' AND postmeta_casts.meta_key = '_kksr_casts'
75
+ ");
76
+
77
+ $stars = get_option(prefix('stars'));
78
+ $stars = max((int) $stars, 1);
79
+
80
+ foreach ($rows as $row) {
81
+ $count = max((int) $row->casts, 0);
82
+ $score = min(max($row->avg, 0), $stars);
83
+ $ratings = $score * $count / $stars * 5;
84
+
85
+ update_post_meta(
86
+ $row->ID,
87
+ meta_prefix('ratings'),
88
+ round($ratings, 0, PHP_ROUND_HALF_DOWN)
89
+ );
90
+ }
91
+ }
92
+
93
+ add_plugin_action('activate', __NAMESPACE__.'\sync_options', 9, 2);
94
+ function sync_options($version, $previous)
95
+ {
96
+ foreach (config('options') as $key => $value) {
97
+ update_option(prefix($key), get_option(prefix($key), $value));
98
+ }
99
  }
src/admin.php CHANGED
@@ -5,143 +5,171 @@
5
  *
6
  * (c) Kamal Khan <shout@bhittani.com>
7
  *
8
- * This source file is subject to the GPL v2 license that
9
- * is bundled with this source code in the file LICENSE.
10
  */
11
 
12
  namespace Bhittani\StarRating;
13
 
14
- add_action('admin_menu', KKSR_NAMESPACE.'admin'); function admin()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  {
16
  add_menu_page(
17
- KKSR_LABEL,
18
- KKSR_LABEL,
19
  'manage_options',
20
- KKSR_SLUG,
21
- KKSR_NAMESPACE.'adminCallback',
22
  'dashicons-star-filled'
23
  );
24
- } function adminCallback()
 
 
25
  {
26
- $label = KKSR_LABEL;
27
- $version = KKSR_VERSION;
28
 
29
- ob_start();
30
- include KKSR_PATH_VIEWS.'admin/index.php';
31
- $html = ob_get_clean();
32
 
33
- echo apply_filters(prefix('settings'), $html);
 
 
 
 
 
 
 
 
 
 
34
  }
35
 
36
- add_action(prefix('setting_tabs'), KKSR_NAMESPACE.'adminTabs'); function adminTabs()
 
37
  {
38
- $tabs = getAdminTabs();
39
- $active = getActiveAdminTab();
40
-
41
- ob_start();
42
- include KKSR_PATH_VIEWS.'admin/tabs.php';
43
- echo ob_get_clean();
44
  }
45
 
46
- add_action(prefix('setting_contents'), KKSR_NAMESPACE.'adminContents'); function adminContents()
 
47
  {
48
- if (! ($active = getActiveAdminTab())) {
49
- return;
50
  }
51
 
52
- ob_start();
53
 
54
- if (file_exists($file = KKSR_PATH_VIEWS.'admin/tabs/'.$active.'.php')) {
55
- include $file;
56
- } else {
57
- include KKSR_PATH_VIEWS.'admin/contents.php';
58
- }
59
-
60
- echo ob_get_clean();
61
  }
62
 
63
- add_action('admin_init', KKSR_NAMESPACE.'adminFields'); function adminFields()
 
64
  {
65
- if (! ($active = getActiveAdminTab())) {
 
 
66
  return;
67
  }
68
 
69
- add_settings_section('default', null, null, KKSR_SLUG);
70
 
71
- $fields = [];
72
 
73
- if (file_exists($file = KKSR_PATH_SRC.'admin/'.$active.'.php')) {
74
- $fields = array_merge($fields, (array) require $file);
75
- }
76
-
77
- $fields = apply_filters(prefix('setting_fields'), $fields, $active);
78
- $fields = apply_filters(prefix('setting_fields:'.$active), $fields);
79
 
80
  foreach ($fields as $field) {
81
- $field = apply_filters(prefix('setting_field'), $field);
82
-
83
- if (isset($field['field'])) {
84
- $field = apply_filters(prefix('setting_field:'.$field['field']), $field);
85
- } elseif (isset($field['fields'])) {
86
- foreach ($field['fields'] as &$childField) {
87
- $childField = apply_filters(prefix('setting_field:'.$childField['field']), $childField);
88
- }
89
- }
90
-
91
- $field = apply_filters(prefix('setting_input:'.$field['name']), $field);
92
-
93
  register_setting(
94
- KKSR_SLUG,
95
  $field['name'],
96
- isset($field['filter']) ? $field['filter'] : null
 
 
97
  );
98
 
99
  add_settings_field(
100
- $field['id'],
101
  $field['title'],
102
- KKSR_NAMESPACE.'fieldCallback',
103
- KKSR_SLUG,
104
  'default',
105
  $field
106
  );
107
  }
108
- } function fieldCallback($args)
 
 
109
  {
110
- extract($args);
 
111
 
112
- ob_start();
 
 
 
113
 
114
- if (isset($fields)) {
115
- $br = '<br><br>';
 
116
 
117
- for ($i = 0; $i < count($fields); ++$i) {
118
- echo $i != 0 ? $br : '';
119
- fieldCallback($fields[$i]);
 
120
  }
121
- if (isset($help)) {
122
- echo $br;
123
- }
124
- } elseif (isset($args['field'])
125
- && file_exists($file = KKSR_PATH_VIEWS.'admin/fields/'.$args['field'].'.php')
126
- ) {
127
- include $file;
128
  }
 
129
 
130
- if (isset($help)) {
131
- echo '<p class="description">'.$help.'</p>';
 
 
 
132
  }
133
 
134
- $html = ob_get_clean();
 
135
 
136
- $html = apply_filters(prefix('setting_field_html'), $html, $args);
 
 
 
 
 
137
 
138
- if (isset($args['field'])) {
139
- $html = apply_filters(prefix('setting_field_html:'.$args['field']), $html, $args);
 
 
140
  }
 
141
 
142
- if (isset($args['name'])) {
143
- $html = apply_filters(prefix('setting_input_html:'.$args['name']), $html, $args);
 
 
 
144
  }
145
 
146
- echo $html;
147
  }
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
  namespace Bhittani\StarRating;
13
 
14
+ if (! defined('ABSPATH')) {
15
+ http_response_code(404);
16
+ die();
17
+ }
18
+
19
+ use RuntimeException;
20
+
21
+ function get_admin_tabs()
22
+ {
23
+ $tabs = apply_plugin_filters('admin_tabs', []);
24
+ $keys = array_keys($tabs);
25
+ $active = (isset($_GET['tab']) && $_GET['tab']) ? $_GET['tab'] : reset($keys);
26
+ $active = apply_plugin_filters('active_admin_tab', $active);
27
+
28
+ return [$tabs, $active];
29
+ }
30
+
31
+ add_action('admin_menu', __NAMESPACE__.'\admin');
32
+ function admin()
33
  {
34
  add_menu_page(
35
+ config('name'),
36
+ config('name'),
37
  'manage_options',
38
+ config('slug'),
39
+ __NAMESPACE__.'\admin_callback',
40
  'dashicons-star-filled'
41
  );
42
+ }
43
+
44
+ function admin_callback()
45
  {
46
+ list($tabs, $active) = get_admin_tabs();
 
47
 
48
+ $content = apply_plugin_filters('admin_content', '', $active);
 
 
49
 
50
+ if ($active) {
51
+ $content = apply_plugin_filters('admin_content.'.$active, $content);
52
+ }
53
+
54
+ echo view('admin.index', [
55
+ 'label' => config('name'),
56
+ 'version' => config('version'),
57
+ 'tabs' => $tabs,
58
+ 'active' => $active,
59
+ 'content' => $content,
60
+ ]);
61
  }
62
 
63
+ add_plugin_filter('admin_tabs', __NAMESPACE__.'\admin_tabs', 9);
64
+ function admin_tabs($tabs)
65
  {
66
+ return $tabs + [
67
+ 'general' => 'General',
68
+ 'appearance' => 'Appearance',
69
+ 'rich-snippets' => 'Rich Snippets',
70
+ ];
 
71
  }
72
 
73
+ add_plugin_filter('admin_content', __NAMESPACE__.'\admin_content', 9, 2);
74
+ function admin_content($content, $active)
75
  {
76
+ if (! $active) {
77
+ return $content;
78
  }
79
 
80
+ $slug = config('slug');
81
 
82
+ return view(["admin.tabs.{$active}", 'admin.content'], compact('active', 'slug'));
 
 
 
 
 
 
83
  }
84
 
85
+ add_action('admin_init', __NAMESPACE__.'\settings_callback');
86
+ function settings_callback()
87
  {
88
+ list($tabs, $active) = get_admin_tabs();
89
+
90
+ if (! $active) {
91
  return;
92
  }
93
 
94
+ $slug = config('slug');
95
 
96
+ add_settings_section('default', null, null, $slug);
97
 
98
+ $fields = apply_plugin_filters('setting_fields', [], $active);
99
+ $fields = apply_plugin_filters('setting_fields.'.$active, $fields);
 
 
 
 
100
 
101
  foreach ($fields as $field) {
 
 
 
 
 
 
 
 
 
 
 
 
102
  register_setting(
103
+ $slug,
104
  $field['name'],
105
+ [
106
+ 'sanitize_callback' => isset($field['filter']) ? $field['filter'] : null,
107
+ ]
108
  );
109
 
110
  add_settings_field(
111
+ $field['name'],
112
  $field['title'],
113
+ __NAMESPACE__.'\setting_field_callback',
114
+ $slug,
115
  'default',
116
  $field
117
  );
118
  }
119
+ }
120
+
121
+ function setting_field_callback($field)
122
  {
123
+ if (isset($field['fields'])) {
124
+ $input = apply_plugin_filters('setting_field', '', $field);
125
 
126
+ foreach ($field['fields'] as $field) {
127
+ setting_field_callback($field);
128
+ echo '<br><br>';
129
+ }
130
 
131
+ echo $input;
132
+ } else {
133
+ $input = apply_plugin_filters('setting_field', '', $field);
134
 
135
+ if (isset($field['type']) && $field['type']) {
136
+ echo apply_plugin_filters('setting_field.'.$field['type'], $input, $field);
137
+ } else {
138
+ echo $input;
139
  }
 
 
 
 
 
 
 
140
  }
141
+ }
142
 
143
+ add_plugin_filter('setting_fields', __NAMESPACE__.'\setting_fields', 9, 2);
144
+ function setting_fields($fields, $active)
145
+ {
146
+ if (is_file($file = __DIR__.'/admin/'.$active.'.php')) {
147
+ return array_merge($fields, (array) require $file);
148
  }
149
 
150
+ return $fields;
151
+ }
152
 
153
+ add_plugin_filter('setting_field', __NAMESPACE__.'\setting_field', 9, 2);
154
+ function setting_field($input, $payload)
155
+ {
156
+ if ($input || ! isset($payload['type']) || ! $payload['type']) {
157
+ return $input;
158
+ }
159
 
160
+ try {
161
+ return view('admin.fields.'.$payload['type'], $payload);
162
+ } catch (RuntimeException $e) {
163
+ return $input;
164
  }
165
+ }
166
 
167
+ add_plugin_filter('setting_field', __NAMESPACE__.'\setting_field_help', 11, 2);
168
+ function setting_field_help($input, $payload)
169
+ {
170
+ if (! isset($payload['help']) || ! $payload['help']) {
171
+ return $input;
172
  }
173
 
174
+ return $input.'<p class="description">'.$payload['help'].'</p>';
175
  }
src/admin/appearance.php CHANGED
@@ -5,90 +5,49 @@
5
  *
6
  * (c) Kamal Khan <shout@bhittani.com>
7
  *
8
- * This source file is subject to the GPL v2 license that
9
- * is bundled with this source code in the file LICENSE.
10
  */
11
 
12
  namespace Bhittani\StarRating;
13
 
 
 
 
 
 
 
 
 
 
14
  return [
15
  [
16
- 'field' => 'text',
 
 
 
 
 
 
 
 
 
 
 
17
  'type' => 'number',
18
- 'id' => prefix('stars'),
19
  'title' => __('Stars', 'kk-star-ratings'),
20
- 'name' => prefix('stars'),
21
- 'value' => getOption('stars'),
 
22
  'help' => __('Total number of stars.', 'kk-star-ratings'),
23
  ],
24
 
25
  [
26
- 'field' => 'text',
27
  'type' => 'number',
28
- 'id' => prefix('size'),
29
  'title' => __('Size', 'kk-star-ratings'),
30
- 'name' => prefix('size'),
31
- 'value' => getOption('size'),
 
32
  'help' => __('Size of a single star.', 'kk-star-ratings'),
33
  ],
34
-
35
- [
36
- 'field' => 'text',
37
- 'type' => 'color',
38
- 'id' => prefix('fill_color_star'),
39
- 'title' => __('Fill color', 'kk-star-ratings'),
40
- 'name' => prefix('fill_color_star'),
41
- 'value' => getOption('fill_color_star'),
42
- 'help' => __('Fill color of stars.', 'kk-star-ratings'),
43
- ],
44
-
45
- [
46
- 'field' => 'text',
47
- 'type' => 'color',
48
- 'id' => prefix('stroke_color_star'),
49
- 'title' => __('Stroke color', 'kk-star-ratings'),
50
- 'name' => prefix('stroke_color_star'),
51
- 'value' => getOption('stroke_color_star'),
52
- 'help' => __('Stroke color of stars.', 'kk-star-ratings'),
53
- ],
54
-
55
- [
56
- 'field' => 'text',
57
- 'type' => 'color',
58
- 'id' => prefix('fill_color_active_star'),
59
- 'title' => __('Active fill color', 'kk-star-ratings'),
60
- 'name' => prefix('fill_color_active_star'),
61
- 'value' => getOption('fill_color_active_star'),
62
- 'help' => __('Fill color of active stars.', 'kk-star-ratings'),
63
- ],
64
-
65
- [
66
- 'field' => 'text',
67
- 'type' => 'color',
68
- 'id' => prefix('stroke_color_active_star'),
69
- 'title' => __('Active stroke color', 'kk-star-ratings'),
70
- 'name' => prefix('stroke_color_active_star'),
71
- 'value' => getOption('stroke_color_active_star'),
72
- 'help' => __('Stroke color of active stars.', 'kk-star-ratings'),
73
- ],
74
-
75
- [
76
- 'field' => 'text',
77
- 'type' => 'color',
78
- 'id' => prefix('fill_color_hover_star'),
79
- 'title' => __('Hover fill color', 'kk-star-ratings'),
80
- 'name' => prefix('fill_color_hover_star'),
81
- 'value' => getOption('fill_color_hover_star'),
82
- 'help' => __('Fill color of hover stars.', 'kk-star-ratings'),
83
- ],
84
-
85
- [
86
- 'field' => 'text',
87
- 'type' => 'color',
88
- 'id' => prefix('stroke_color_hover_star'),
89
- 'title' => __('Hover stroke color', 'kk-star-ratings'),
90
- 'name' => prefix('stroke_color_hover_star'),
91
- 'value' => getOption('stroke_color_hover_star'),
92
- 'help' => __('Stroke color of hover stars.', 'kk-star-ratings'),
93
- ],
94
  ];
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
  namespace Bhittani\StarRating;
13
 
14
+ if (! defined('ABSPATH')) {
15
+ http_response_code(404);
16
+ die();
17
+ }
18
+
19
+ $size = [prefix('size'), get_option(prefix('size'))];
20
+ $stars = [prefix('stars'), get_option(prefix('stars'))];
21
+ $greet = [prefix('greet'), get_option(prefix('greet'))];
22
+
23
  return [
24
  [
25
+ 'type' => 'text',
26
+ 'title' => __('Greeting text', 'kk-star-ratings'),
27
+ 'name' => $greet[0],
28
+ 'value' => $greet[1],
29
+ 'help' => implode('<br>', [
30
+ __('Text that will be displayed when no votes have been casted.', 'kk-star-ratings').'<br>',
31
+ __('The following variables are available:', 'kk-star-ratings').'<br>',
32
+ sprintf(__('%s Post type.', 'kk-star-ratings'), '<code>[type]</code>'),
33
+ ]),
34
+ ],
35
+
36
+ [
37
  'type' => 'number',
 
38
  'title' => __('Stars', 'kk-star-ratings'),
39
+ 'name' => $stars[0],
40
+ 'value' => $stars[1],
41
+ 'min' => 1,
42
  'help' => __('Total number of stars.', 'kk-star-ratings'),
43
  ],
44
 
45
  [
 
46
  'type' => 'number',
 
47
  'title' => __('Size', 'kk-star-ratings'),
48
+ 'name' => $size[0],
49
+ 'value' => $size[1],
50
+ 'min' => 1,
51
  'help' => __('Size of a single star.', 'kk-star-ratings'),
52
  ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  ];
src/admin/general.php CHANGED
@@ -5,94 +5,89 @@
5
  *
6
  * (c) Kamal Khan <shout@bhittani.com>
7
  *
8
- * This source file is subject to the GPL v2 license that
9
- * is bundled with this source code in the file LICENSE.
10
  */
11
 
12
  namespace Bhittani\StarRating;
13
 
14
- $enabled = (bool) getOption('enable');
15
- $position = getOption('position');
16
- $manuallyControlled = getOption('manual_control');
17
- $excludedLocations = getOption('exclude_locations');
18
- $strategies = getOption('strategies');
19
- $excludedCategories = getOption('exclude_categories', []);
20
- $excludedCategories = is_array($excludedCategories) ? $excludedCategories : [];
21
-
22
- $categories = get_terms([
23
- 'taxonomy' => 'category',
24
- 'hide_empty' => false,
25
- 'parent' => 0,
26
- ]);
27
-
28
- $categoriesOptions = [];
29
- foreach ($categories as $category) {
30
- $categoriesOptions[] = [
31
- 'label' => $category->name,
32
- 'value' => $category->term_id,
33
- 'selected' => in_array($category->term_id, (array) $excludedCategories),
34
- ];
35
  }
36
 
37
- $postTypes = [];
 
 
 
 
 
38
 
39
- $customPostTypes = get_post_types(['publicly_queryable' => true, '_builtin' => false], 'objects');
 
 
 
40
 
41
- foreach ($customPostTypes as $postType) {
 
 
 
42
  $postTypes[] = [
43
  'value' => $postType->name,
44
  'label' => $postType->labels->name,
45
  ];
46
  }
47
 
48
- $postTypes = array_merge([
49
- ['value' => 'post', 'label' => __('Posts', 'kk-star-ratings')],
50
- ['value' => 'page', 'label' => __('Pages', 'kk-star-ratings')],
51
- ], $postTypes);
 
52
 
53
  return [
54
  [
55
- 'field' => 'checkbox',
56
- 'id' => prefix('enable'),
57
  'title' => __('Status', 'kk-star-ratings'),
58
  'label' => __('Active', 'kk-star-ratings'),
59
- 'name' => prefix('enable'),
60
  'value' => true,
61
- 'checked' => $enabled,
 
 
 
62
  'help' => __('Globally activate/deactivate the star ratings.', 'kk-star-ratings'),
63
  ],
64
 
65
  // Strategies
66
 
67
  [
68
- 'id' => prefix('strategies'),
69
  'title' => __('Strategies', 'kk-star-ratings'),
70
- 'name' => prefix('strategies'),
71
  'help' => __('Select the voting strategies.', 'kk-star-ratings'),
72
  'filter' => function ($values) {
73
  return (array) $values;
74
  },
75
  'fields' => [
76
  [
77
- 'field' => 'checkbox',
78
  'label' => __('Allow voting in archives', 'kk-star-ratings'),
79
- 'name' => prefix('strategies[]'),
80
  'value' => 'archives',
81
- 'checked' => in_array('archives', $strategies),
82
  ],
83
  [
84
- 'field' => 'checkbox',
85
  'label' => __('Allow guests to vote', 'kk-star-ratings'),
86
- 'name' => prefix('strategies[]'),
87
  'value' => 'guests',
88
- 'checked' => in_array('guests', $strategies),
89
  ],
90
  [
91
- 'field' => 'checkbox',
92
  'label' => __('Unique votes (based on IP Address)', 'kk-star-ratings'),
93
- 'name' => prefix('strategies[]'),
94
  'value' => 'unique',
95
- 'checked' => in_array('unique', $strategies),
96
  ],
97
  ],
98
  ],
@@ -100,17 +95,16 @@ return [
100
  // Manual Control
101
 
102
  [
103
- 'id' => prefix('manual_control'),
104
  'title' => __('Manual Control', 'kk-star-ratings'),
105
- 'name' => prefix('manual_control'),
106
  'help' => sprintf(__('Select the post types that should not auto embed the<br>markup and will be manually controlled by the theme.<br>E.g. Using %s in your template.', 'kk-star-ratings'), '<code>echo kk_star_ratings();</code>'),
107
  'filter' => function ($values) {
108
  return (array) $values;
109
  },
110
  'fields' => array_map(function ($field) use ($manuallyControlled) {
111
- $field['field'] = 'checkbox';
112
- $field['name'] = prefix('manual_control[]');
113
- $field['checked'] = in_array($field['value'], $manuallyControlled);
114
 
115
  return $field;
116
  }, $postTypes),
@@ -119,17 +113,16 @@ return [
119
  // Locations
120
 
121
  [
122
- 'id' => prefix('exclude_locations'),
123
  'title' => __('Disable Locations', 'kk-star-ratings'),
124
- 'name' => prefix('exclude_locations'),
125
  'help' => __('Select the locations where the star ratings should be excluded.', 'kk-star-ratings'),
126
  'filter' => function ($values) {
127
  return (array) $values;
128
  },
129
  'fields' => array_map(function ($field) use ($excludedLocations) {
130
- $field['field'] = 'checkbox';
131
- $field['name'] = prefix('exclude_locations[]');
132
- $field['checked'] = in_array($field['value'], $excludedLocations);
133
 
134
  return $field;
135
  }, array_merge([
@@ -148,67 +141,71 @@ return [
148
  // Categories
149
 
150
  [
151
- 'field' => 'select',
152
- 'id' => prefix('exclude_categories'),
153
- 'title' => __('Disable Categories', 'kk-star-ratings'),
154
- 'name' => prefix('exclude_categories'),
155
  'multiple' => true,
 
 
156
  'filter' => function ($values) {
157
  return (array) $values;
158
  },
159
- 'options' => $categoriesOptions,
 
 
 
 
 
 
160
  'help' => __('Exclude star ratings from posts belonging to the selected categories.<br>Use <strong>cmd/ctrl + click</strong> to select/deselect multiple categories.', 'kk-star-ratings'),
161
  ],
162
 
163
  // Position
164
 
165
  [
166
- 'id' => prefix('position'),
167
  'title' => __('Default Position', 'kk-star-ratings'),
168
- 'name' => prefix('position'),
169
  'help' => __('Choose a default position.', 'kk-star-ratings'),
170
  'fields' => [
171
  [
172
- 'field' => 'radio',
173
  'label' => __('Top Left', 'kk-star-ratings'),
174
- 'name' => prefix('position'),
175
  'value' => 'top-left',
176
- 'checked' => $position == 'top-left',
177
  ],
178
  [
179
- 'field' => 'radio',
180
  'label' => __('Top Center', 'kk-star-ratings'),
181
- 'name' => prefix('position'),
182
  'value' => 'top-center',
183
- 'checked' => $position == 'top-center',
184
  ],
185
  [
186
- 'field' => 'radio',
187
  'label' => __('Top Right', 'kk-star-ratings'),
188
- 'name' => prefix('position'),
189
  'value' => 'top-right',
190
- 'checked' => $position == 'top-right',
191
  ],
192
  [
193
- 'field' => 'radio',
194
  'label' => __('Bottom Left', 'kk-star-ratings'),
195
- 'name' => prefix('position'),
196
  'value' => 'bottom-left',
197
- 'checked' => $position == 'bottom-left',
198
  ],
199
  [
200
- 'field' => 'radio',
201
  'label' => __('Bottom Center', 'kk-star-ratings'),
202
- 'name' => prefix('position'),
203
  'value' => 'bottom-center',
204
- 'checked' => $position == 'bottom-center',
205
  ],
206
  [
207
- 'field' => 'radio',
208
  'label' => __('Bottom Right', 'kk-star-ratings'),
209
- 'name' => prefix('position'),
210
  'value' => 'bottom-right',
211
- 'checked' => $position == 'bottom-right',
212
  ],
213
  ],
214
  ],
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
  namespace Bhittani\StarRating;
13
 
14
+ if (! defined('ABSPATH')) {
15
+ http_response_code(404);
16
+ die();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  }
18
 
19
+ $enable = [prefix('enable'), get_option(prefix('enable'))];
20
+ $position = [prefix('position'), get_option(prefix('position'))];
21
+ $strategies = [prefix('strategies'), (array) get_option(prefix('strategies'), [])];
22
+ $manuallyControlled = [prefix('manual_control'), (array) get_option(prefix('manual_control'), [])];
23
+ $excludedLocations = [prefix('exclude_locations'), (array) get_option(prefix('exclude_locations'), [])];
24
+ $excludedCategories = [prefix('exclude_categories'), (array) get_option(prefix('exclude_categories'), [])];
25
 
26
+ $postTypes = [
27
+ ['value' => 'post', 'label' => __('Posts', 'kk-star-ratings')],
28
+ ['value' => 'page', 'label' => __('Pages', 'kk-star-ratings')],
29
+ ];
30
 
31
+ foreach (get_post_types([
32
+ 'publicly_queryable' => true,
33
+ '_builtin' => false,
34
+ ], 'objects') as $postType) {
35
  $postTypes[] = [
36
  'value' => $postType->name,
37
  'label' => $postType->labels->name,
38
  ];
39
  }
40
 
41
+ $categories = get_terms([
42
+ 'taxonomy' => 'category',
43
+ 'hide_empty' => false,
44
+ 'parent' => 0,
45
+ ]);
46
 
47
  return [
48
  [
49
+ 'type' => 'checkbox',
 
50
  'title' => __('Status', 'kk-star-ratings'),
51
  'label' => __('Active', 'kk-star-ratings'),
52
+ 'name' => $enable[0],
53
  'value' => true,
54
+ 'filter' => function ($bool) {
55
+ return (string) $bool;
56
+ },
57
+ 'checked' => checked($enable[1], '1', false),
58
  'help' => __('Globally activate/deactivate the star ratings.', 'kk-star-ratings'),
59
  ],
60
 
61
  // Strategies
62
 
63
  [
 
64
  'title' => __('Strategies', 'kk-star-ratings'),
65
+ 'name' => $strategies[0],
66
  'help' => __('Select the voting strategies.', 'kk-star-ratings'),
67
  'filter' => function ($values) {
68
  return (array) $values;
69
  },
70
  'fields' => [
71
  [
72
+ 'type' => 'checkbox',
73
  'label' => __('Allow voting in archives', 'kk-star-ratings'),
74
+ 'name' => $strategies[0].'[]',
75
  'value' => 'archives',
76
+ 'checked' => in_array('archives', $strategies[1]),
77
  ],
78
  [
79
+ 'type' => 'checkbox',
80
  'label' => __('Allow guests to vote', 'kk-star-ratings'),
81
+ 'name' => $strategies[0].'[]',
82
  'value' => 'guests',
83
+ 'checked' => in_array('guests', $strategies[1]),
84
  ],
85
  [
86
+ 'type' => 'checkbox',
87
  'label' => __('Unique votes (based on IP Address)', 'kk-star-ratings'),
88
+ 'name' => $strategies[0].'[]',
89
  'value' => 'unique',
90
+ 'checked' => in_array('unique', $strategies[1]),
91
  ],
92
  ],
93
  ],
95
  // Manual Control
96
 
97
  [
 
98
  'title' => __('Manual Control', 'kk-star-ratings'),
99
+ 'name' => $manuallyControlled[0],
100
  'help' => sprintf(__('Select the post types that should not auto embed the<br>markup and will be manually controlled by the theme.<br>E.g. Using %s in your template.', 'kk-star-ratings'), '<code>echo kk_star_ratings();</code>'),
101
  'filter' => function ($values) {
102
  return (array) $values;
103
  },
104
  'fields' => array_map(function ($field) use ($manuallyControlled) {
105
+ $field['type'] = 'checkbox';
106
+ $field['name'] = $manuallyControlled[0].'[]';
107
+ $field['checked'] = in_array($field['value'], $manuallyControlled[1]);
108
 
109
  return $field;
110
  }, $postTypes),
113
  // Locations
114
 
115
  [
 
116
  'title' => __('Disable Locations', 'kk-star-ratings'),
117
+ 'name' => $excludedLocations[0],
118
  'help' => __('Select the locations where the star ratings should be excluded.', 'kk-star-ratings'),
119
  'filter' => function ($values) {
120
  return (array) $values;
121
  },
122
  'fields' => array_map(function ($field) use ($excludedLocations) {
123
+ $field['type'] = 'checkbox';
124
+ $field['name'] = $excludedLocations[0].'[]';
125
+ $field['checked'] = in_array($field['value'], $excludedLocations[1]);
126
 
127
  return $field;
128
  }, array_merge([
141
  // Categories
142
 
143
  [
144
+ 'type' => 'select',
 
 
 
145
  'multiple' => true,
146
+ 'title' => __('Disable Categories', 'kk-star-ratings'),
147
+ 'name' => $excludedCategories[0],
148
  'filter' => function ($values) {
149
  return (array) $values;
150
  },
151
+ 'options' => array_map(function ($category) use ($excludedCategories) {
152
+ return [
153
+ 'label' => $category->name,
154
+ 'value' => $category->term_id,
155
+ 'selected' => in_array($category->term_id, $excludedCategories[1]),
156
+ ];
157
+ }, $categories),
158
  'help' => __('Exclude star ratings from posts belonging to the selected categories.<br>Use <strong>cmd/ctrl + click</strong> to select/deselect multiple categories.', 'kk-star-ratings'),
159
  ],
160
 
161
  // Position
162
 
163
  [
 
164
  'title' => __('Default Position', 'kk-star-ratings'),
165
+ 'name' => $position[0],
166
  'help' => __('Choose a default position.', 'kk-star-ratings'),
167
  'fields' => [
168
  [
169
+ 'type' => 'radio',
170
  'label' => __('Top Left', 'kk-star-ratings'),
171
+ 'name' => $position[0],
172
  'value' => 'top-left',
173
+ 'checked' => checked($position[1], 'top-left', false),
174
  ],
175
  [
176
+ 'type' => 'radio',
177
  'label' => __('Top Center', 'kk-star-ratings'),
178
+ 'name' => $position[0],
179
  'value' => 'top-center',
180
+ 'checked' => checked($position[1], 'top-center', false),
181
  ],
182
  [
183
+ 'type' => 'radio',
184
  'label' => __('Top Right', 'kk-star-ratings'),
185
+ 'name' => $position[0],
186
  'value' => 'top-right',
187
+ 'checked' => checked($position[1], 'top-right', false),
188
  ],
189
  [
190
+ 'type' => 'radio',
191
  'label' => __('Bottom Left', 'kk-star-ratings'),
192
+ 'name' => $position[0],
193
  'value' => 'bottom-left',
194
+ 'checked' => checked($position[1], 'bottom-left', false),
195
  ],
196
  [
197
+ 'type' => 'radio',
198
  'label' => __('Bottom Center', 'kk-star-ratings'),
199
+ 'name' => $position[0],
200
  'value' => 'bottom-center',
201
+ 'checked' => checked($position[1], 'bottom-center', false),
202
  ],
203
  [
204
+ 'type' => 'radio',
205
  'label' => __('Bottom Right', 'kk-star-ratings'),
206
+ 'name' => $position[0],
207
  'value' => 'bottom-right',
208
+ 'checked' => checked($position[1], 'bottom-right', false),
209
  ],
210
  ],
211
  ],
src/admin/index.php DELETED
@@ -1,14 +0,0 @@
1
- <?php
2
-
3
- /*
4
- * This file is part of bhittani/kk-star-ratings.
5
- *
6
- * (c) Kamal Khan <shout@bhittani.com>
7
- *
8
- * This source file is subject to the GPL v2 license that
9
- * is bundled with this source code in the file LICENSE.
10
- */
11
-
12
- http_response_code(404);
13
-
14
- die();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/admin/rich-snippets.php CHANGED
@@ -5,39 +5,56 @@
5
  *
6
  * (c) Kamal Khan <shout@bhittani.com>
7
  *
8
- * This source file is subject to the GPL v2 license that
9
- * is bundled with this source code in the file LICENSE.
10
  */
11
 
12
  namespace Bhittani\StarRating;
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  return [
15
  [
16
- 'field' => 'checkbox',
17
- 'id' => prefix('grs'),
18
  'title' => __('Status', 'kk-star-ratings'),
19
- 'label' => __('Active', 'kk-star-ratings'),
20
- 'name' => prefix('grs'),
21
  'value' => true,
22
- 'checked' => (bool) getOption('grs'),
23
- 'help' => __('Activate/deactivate rich snippets.', 'kk-star-ratings'),
24
- ],
25
-
26
- [
27
- 'field' => 'text',
28
- 'id' => prefix('sd_context'),
29
- 'title' => __('Context', 'kk-star-ratings'),
30
- 'name' => prefix('sd_context'),
31
- 'value' => getOption('sd_context'),
32
- 'help' => __('Structured data context.', 'kk-star-ratings'),
33
  ],
34
 
35
  [
36
- 'field' => 'text',
37
- 'id' => prefix('sd_type'),
38
- 'title' => __('Type', 'kk-star-ratings'),
39
- 'name' => prefix('sd_type'),
40
- 'value' => getOption('sd_type'),
41
- 'help' => __('Structured data type.', 'kk-star-ratings'),
 
 
 
 
 
 
 
 
 
 
 
42
  ],
43
  ];
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
  namespace Bhittani\StarRating;
13
 
14
+ if (! defined('ABSPATH')) {
15
+ http_response_code(404);
16
+ die();
17
+ }
18
+
19
+ $grs = [prefix('grs'), get_option(prefix('grs'))];
20
+ $sd = [prefix('sd'), get_option(prefix('sd'))];
21
+
22
+ $snippet = str_replace('[title]', 'Post title', $sd[1]);
23
+ $snippet = str_replace('[best]', '5', $snippet);
24
+ $snippet = str_replace('[score]', '4.3', $snippet);
25
+ $snippet = str_replace('[count]', '20', $snippet);
26
+
27
  return [
28
  [
29
+ 'type' => 'checkbox',
 
30
  'title' => __('Status', 'kk-star-ratings'),
31
+ 'label' => __('Enable', 'kk-star-ratings'),
32
+ 'name' => $grs[0],
33
  'value' => true,
34
+ 'filter' => function ($bool) {
35
+ return (string) $bool;
36
+ },
37
+ 'checked' => checked($grs[1], '1', false),
38
+ 'help' => __('Enable/disable rich snippets.', 'kk-star-ratings'),
 
 
 
 
 
 
39
  ],
40
 
41
  [
42
+ 'type' => 'code',
43
+ 'title' => __('Content', 'kk-star-ratings'),
44
+ 'name' => $sd[0],
45
+ 'value' => $sd[1],
46
+ 'help' => implode('<br>', [
47
+ sprintf(__('Provide the %s structure.', 'kk-star-ratings'), '<code>ld+json</code>').'<br>',
48
+ __('The following variables are available:', 'kk-star-ratings').'<br>',
49
+ sprintf(__('%s Post title.', 'kk-star-ratings'), '<code>[title]</code>'),
50
+ sprintf(__('%s Average post ratings.', 'kk-star-ratings'), '<code>[score]</code>'),
51
+ sprintf(__('%s Number of votes casted.', 'kk-star-ratings'), '<code>[count]</code>'),
52
+ sprintf(__('%s Total amount of stars.', 'kk-star-ratings'), '<code>[best]</code>'),
53
+ '<br>'.sprintf(
54
+ __('You may <a href="%s" target="_blank">test the snippet here</a>.', 'kk-star-ratings'),
55
+ 'https://search.google.com/structured-data/testing-tool?'
56
+ .http_build_query(['code' => $snippet])
57
+ ),
58
+ ]),
59
  ],
60
  ];
src/ajax.php CHANGED
@@ -5,77 +5,62 @@
5
  *
6
  * (c) Kamal Khan <shout@bhittani.com>
7
  *
8
- * This source file is subject to the GPL v2 license that
9
- * is bundled with this source code in the file LICENSE.
10
  */
11
 
12
  namespace Bhittani\StarRating;
13
 
14
- add_action('wp_ajax_'.KKSR_SLUG, KKSR_NAMESPACE.'ajax');
15
- add_action('wp_ajax_nopriv_'.KKSR_SLUG, KKSR_NAMESPACE.'ajax'); function ajax()
16
- {
17
- header('Content-Type: application/json; charset=utf-8', true);
18
-
19
- if (! check_ajax_referer(KKSR_SLUG, 'nonce', false)) {
20
- status_header(403);
21
-
22
- return wp_die(json_encode(['error' => __('This action is forbidden.', 'kk-star-ratings')]));
23
- }
24
 
25
- // Are guests allowed?
26
- if (! is_user_logged_in() && ! in_array('guests', getOption('strategies'))) {
27
- status_header(401);
 
 
 
28
 
29
- return wp_die(json_encode(['error' => __('Unauthorized.', 'kk-star-ratings')]));
 
 
30
  }
31
 
32
  if (! isset($_POST['id'])) {
33
- status_header(406);
34
 
35
- return wp_die(json_encode(['error' => __('An id is required to vote.', 'kk-star-ratings')]));
 
 
36
  }
37
 
38
  $id = $_POST['id'];
 
39
 
40
- $ip = md5($_SERVER['REMOTE_ADDR']);
41
- $ips = get_post_meta($id, '_kksr_ips');
42
-
43
- // Is the IP address unique?
44
- if (in_array('unique', getOption('strategies'))
45
- && in_array($ip, $ips)
46
- ) {
47
- status_header(403);
48
 
49
- return wp_die(json_encode(['error' => __('Not allowed to vote.', 'kk-star-ratings')]));
 
 
50
  }
51
 
52
- if (! isset($_POST['rating'])) {
53
- status_header(406);
54
-
55
- return wp_die(json_encode(['error' => __('A rating is required to vote.', 'kk-star-ratings')]));
56
- }
57
-
58
- $rating = $_POST['rating'];
59
-
60
- try {
61
- list($ratings, $count) = vote($id, $rating);
62
- } catch (\Exception $e) {
63
- status_header(406);
64
-
65
- return wp_die(json_encode(['error' => $e->getMessage()]));
66
- }
67
 
68
- if (! in_array($ip, $ips)) {
69
- update_post_meta($id, '_kksr_ips', $ip);
 
70
  }
71
 
72
- status_header(201);
 
 
 
73
 
74
- $disable = in_array('unique', getOption('strategies'));
75
- $count = apply_filters('kksr_count', $count);
76
- $score = apply_filters('kksr_score', calculateScore($ratings, $count, getOption('stars')));
77
- $percentage = apply_filters('kksr_percentage', calculatePercentage($ratings, $count));
78
- $width = apply_filters('kksr_width', calculateWidth($score));
79
 
80
- wp_die(json_encode(compact('percentage', 'score', 'count', 'width', 'disable')));
81
  }
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
  namespace Bhittani\StarRating;
13
 
14
+ if (! defined('ABSPATH')) {
15
+ http_response_code(404);
16
+ die();
17
+ }
 
 
 
 
 
 
18
 
19
+ add_action('wp_ajax_'.config('slug'), __NAMESPACE__.'\ajax');
20
+ add_action('wp_ajax_nopriv_'.config('slug'), __NAMESPACE__.'\ajax');
21
+ function ajax()
22
+ {
23
+ if (! check_ajax_referer(config('slug').'-ajax', 'nonce', false)) {
24
+ header('Content-Type: application/json; charset=utf-8', true, 403);
25
 
26
+ return wp_die(json_encode([
27
+ 'error' => __('This action is forbidden.', 'kk-star-ratings'),
28
+ ]));
29
  }
30
 
31
  if (! isset($_POST['id'])) {
32
+ header('Content-Type: application/json; charset=utf-8', true, 406);
33
 
34
+ return wp_die(json_encode([
35
+ 'error' => __('An id is required to vote.', 'kk-star-ratings'),
36
+ ]));
37
  }
38
 
39
  $id = $_POST['id'];
40
+ $slug = $_POST['slug'];
41
 
42
+ if (! apply_plugin_filters('can_vote', true, $id, $slug)) {
43
+ header('Content-Type: application/json; charset=utf-8', true, 401);
 
 
 
 
 
 
44
 
45
+ return wp_die(json_encode([
46
+ 'error' => __('You are not allowed to vote.', 'kk-star-ratings'),
47
+ ]));
48
  }
49
 
50
+ if (! isset($_POST['score'])) {
51
+ header('Content-Type: application/json; charset=utf-8', true, 406);
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
+ return wp_die(json_encode([
54
+ 'error' => __('A rating is required to vote.', 'kk-star-ratings'),
55
+ ]));
56
  }
57
 
58
+ $best = $_POST['best'] ?: get_option(prefix('stars'));
59
+ $best = max((int) $best, 1);
60
+ $score = $_POST['score'];
61
+ $score = min(max((int) $score, 1), $best);
62
 
63
+ do_plugin_action('vote', $score, $best, $id, $slug);
 
 
 
 
64
 
65
+ wp_die(response(compact('id', 'slug', 'best'), false), 201);
66
  }
src/assets.php ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ namespace Bhittani\StarRating;
13
+
14
+ if (! defined('ABSPATH')) {
15
+ http_response_code(404);
16
+ die();
17
+ }
18
+
19
+ add_action('wp_enqueue_scripts', __NAMESPACE__.'\styles');
20
+ function styles($hook)
21
+ {
22
+ if (! get_option(prefix('enable'))) {
23
+ return;
24
+ }
25
+
26
+ wp_enqueue_style(
27
+ config('slug'),
28
+ config('url').'public/css/kk-star-ratings.css',
29
+ [],
30
+ config('version')
31
+ );
32
+
33
+ // wp_add_inline_style(config('slug'), '');
34
+ }
35
+
36
+ add_action('wp_enqueue_scripts', __NAMESPACE__.'\scripts');
37
+ function scripts($hook)
38
+ {
39
+ if (! get_option(prefix('enable'))) {
40
+ return;
41
+ }
42
+
43
+ wp_enqueue_script(
44
+ config('slug'),
45
+ config('url').'public/js/kk-star-ratings.js',
46
+ ['jquery'],
47
+ config('version'),
48
+ true
49
+ );
50
+
51
+ wp_localize_script(
52
+ config('slug'),
53
+ str_replace('-', '_', config('slug')),
54
+ [
55
+ 'action' => config('slug'),
56
+ 'endpoint' => admin_url('admin-ajax.php'),
57
+ 'nonce' => wp_create_nonce(config('slug').'-ajax'),
58
+ ]
59
+ );
60
+
61
+ // wp_add_inline_script(config('slug'), '');
62
+ }
src/config.php ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ namespace Bhittani\StarRating;
13
+
14
+ if (! defined('ABSPATH')) {
15
+ http_response_code(404);
16
+ die();
17
+ }
18
+
19
+ function config($keyOrValues = null, $default = null)
20
+ {
21
+ static $config = [];
22
+
23
+ if (! $config) {
24
+ $file = KK_STAR_RATINGS;
25
+
26
+ $url = plugin_dir_url($file);
27
+ $path = plugin_dir_path($file);
28
+ $signature = plugin_basename($file);
29
+
30
+ $meta = get_file_data($file, [
31
+ 'name' => 'Plugin Name',
32
+ 'nick' => 'Plugin Nick',
33
+ 'slug' => 'Plugin Slug',
34
+ 'version' => 'Version',
35
+ ]);
36
+
37
+ $config = compact('file', 'signature', 'url', 'path') + $meta;
38
+ }
39
+
40
+ if (is_array($keyOrValues)) {
41
+ return $config = $keyOrValues + $config;
42
+ }
43
+
44
+ if (is_null($keyOrValues)) {
45
+ return $config;
46
+ }
47
+
48
+ return isset($config[$keyOrValues]) ? $config[$keyOrValues] : $default;
49
+ }
src/enqueue.php DELETED
@@ -1,76 +0,0 @@
1
- <?php
2
-
3
- /*
4
- * This file is part of bhittani/kk-star-ratings.
5
- *
6
- * (c) Kamal Khan <shout@bhittani.com>
7
- *
8
- * This source file is subject to the GPL v2 license that
9
- * is bundled with this source code in the file LICENSE.
10
- */
11
-
12
- namespace Bhittani\StarRating;
13
-
14
- add_action('wp_enqueue_scripts', KKSR_NAMESPACE.'styles'); function styles($force = false)
15
- {
16
- if ($force || isValidRequest()) {
17
- wp_enqueue_style(
18
- KKSR_SLUG,
19
- KKSR_URI.'css/kk-star-ratings.css',
20
- [],
21
- KKSR_VERSION
22
- );
23
- }
24
- }
25
-
26
- add_action('wp_enqueue_scripts', KKSR_NAMESPACE.'scripts'); function scripts($force = false)
27
- {
28
- if ($force || isValidRequest()) {
29
- wp_enqueue_script(
30
- KKSR_SLUG,
31
- KKSR_URI.'js/kk-star-ratings.js',
32
- ['jquery'],
33
- KKSR_VERSION,
34
- true
35
- );
36
-
37
- wp_localize_script(
38
- KKSR_SLUG,
39
- str_replace('-', '_', KKSR_SLUG),
40
- [
41
- 'nonce' => wp_create_nonce(KKSR_SLUG),
42
- 'endpoint' => admin_url('admin-ajax.php'),
43
- ]
44
- );
45
- }
46
- }
47
-
48
- add_action('wp_enqueue_scripts', KKSR_NAMESPACE.'stylesheet'); function stylesheet($force = false)
49
- {
50
- if (! ($force || isValidRequest())) {
51
- return;
52
- }
53
-
54
- $size = getOption('size');
55
-
56
- $colors = [
57
- 'default' => [
58
- 'fill' => getOption('fill_color_star'),
59
- 'stroke' => getOption('stroke_color_star'),
60
- ],
61
- 'active' => [
62
- 'fill' => getOption('fill_color_active_star'),
63
- 'stroke' => getOption('stroke_color_active_star'),
64
- ],
65
- 'hover' => [
66
- 'fill' => getOption('fill_color_hover_star'),
67
- 'stroke' => getOption('stroke_color_hover_star'),
68
- ],
69
- ];
70
-
71
- ob_start();
72
- include KKSR_PATH_PUBLIC.'css/kk-star-ratings.css.php';
73
- $stylesheet = ob_get_clean();
74
-
75
- wp_add_inline_style(KKSR_SLUG, apply_filters(prefix('stylesheet'), $stylesheet));
76
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/functions.php DELETED
@@ -1,550 +0,0 @@
1
- <?php
2
-
3
- /*
4
- * This file is part of bhittani/kk-star-ratings.
5
- *
6
- * (c) Kamal Khan <shout@bhittani.com>
7
- *
8
- * This source file is subject to the GPL v2 license that
9
- * is bundled with this source code in the file LICENSE.
10
- */
11
-
12
- namespace Bhittani\StarRating;
13
-
14
- function options()
15
- {
16
- return [
17
- 'ver' => KKSR_VERSION,
18
- // General
19
- 'enable' => true,
20
- 'position' => 'top-left',
21
- 'manual_control' => [],
22
- 'exclude_locations' => [],
23
- 'exclude_categories' => [],
24
- 'strategies' => ['guests', 'unique'],
25
- // Rich Snippets
26
- 'grs' => true,
27
- 'sd_type' => 'CreativeWork',
28
- 'sd_context' => 'https://schema.org/',
29
- // Appearance
30
- 'stars' => 5,
31
- 'size' => 22,
32
- 'fill_color_star' => '#ffffff',
33
- 'stroke_color_star' => '#555555',
34
- 'fill_color_active_star' => '#fb9005',
35
- 'stroke_color_active_star' => '#2c1901',
36
- 'fill_color_hover_star' => '#fffb00',
37
- 'stroke_color_hover_star' => '#201f00',
38
- ];
39
- }
40
-
41
- function stripPrefix($key)
42
- {
43
- return strpos($key, KKSR_PREFIX) === 0 ? substr($key, 5) : $key;
44
- }
45
-
46
- function prefix($key)
47
- {
48
- return KKSR_PREFIX.stripPrefix($key);
49
- }
50
-
51
- function getDefaultOption($key, $fallback = null)
52
- {
53
- $options = options();
54
-
55
- $value = array_key_exists($key, $options) ? $options[$key] : $fallback;
56
-
57
- $value = apply_filters(prefix('default_option:'.$key), $value);
58
-
59
- return apply_filters(prefix('default_option'), $value, $key);
60
- }
61
-
62
- function getDefaultOptions($key = null, $fallback = null)
63
- {
64
- return is_null($key)
65
- ? apply_filters(prefix('default_options'), options())
66
- : getDefaultOption($key, $fallback);
67
- }
68
-
69
- function getOption($key, $default = null)
70
- {
71
- if (($value = get_option(prefix($key))) === false) {
72
- $value = is_null($default) ? getDefaultOption($key) : $default;
73
- }
74
-
75
- $value = apply_filters(prefix('option:'.$key), $value);
76
-
77
- return apply_filters(prefix('option'), $value, $key);
78
- }
79
-
80
- function getOptions($key = null, $default = null)
81
- {
82
- if (! is_null($key)) {
83
- return getOption($key, $default);
84
- }
85
-
86
- $options = [];
87
-
88
- foreach (array_keys(getDefaultOptions()) as $key) {
89
- $options[$key] = getOption($key);
90
- }
91
-
92
- return apply_filters(prefix('options'), $options);
93
- }
94
-
95
- function saveOption($key, $value)
96
- {
97
- update_option(prefix($key), $value);
98
-
99
- return $value;
100
- }
101
-
102
- function saveOptions(array $options)
103
- {
104
- foreach ($options as $key => $value) {
105
- saveOption($key, $value);
106
- }
107
-
108
- return $options;
109
- }
110
-
111
- function upgradeOptions(array $merge = [])
112
- {
113
- saveOptions(array_merge_recursive([
114
- // General
115
- 'strategies' => getOption('strategies', array_filter([
116
- 'guests',
117
- getOption('unique', true) ? 'unique' : null,
118
- getOption('disable_in_archives', true) ? null : 'archives',
119
- ])),
120
- 'exclude_locations' => getOption('exclude_locations', array_filter([
121
- getOption('show_in_home', true) ? null : 'home',
122
- getOption('show_in_posts', true) ? null : 'post',
123
- getOption('show_in_pages', true) ? null : 'page',
124
- getOption('show_in_archives', true) ? null : 'archives',
125
- ])),
126
- 'exclude_categories' => is_array($exludedCategories = getOption('exclude_categories', []))
127
- ? $exludedCategories : array_map('trim', explode(',', $exludedCategories)),
128
- // Rich Snippets
129
- // ...
130
- ], $merge));
131
- }
132
-
133
- function upgradeRatings()
134
- {
135
- global $wpdb;
136
-
137
- // Normalize ratings.
138
-
139
- $stars = getOption('stars');
140
-
141
- $rows = $wpdb->get_results("
142
- SELECT posts.ID, postmeta_avg.meta_value as avg, postmeta_casts.meta_value as casts
143
- FROM {$wpdb->posts} posts
144
- JOIN {$wpdb->postmeta} postmeta_avg ON posts.ID = postmeta_avg.post_id
145
- JOIN {$wpdb->postmeta} postmeta_casts ON posts.ID = postmeta_casts.post_id
146
- WHERE postmeta_avg.meta_key = '_kksr_avg' AND postmeta_casts.meta_key = '_kksr_casts'
147
- ");
148
-
149
- foreach ($rows as $row) {
150
- update_post_meta(
151
- $row->ID,
152
- '_'.prefix('ratings'),
153
- scoreToRatings($row->avg, $row->casts, $stars)
154
- );
155
- }
156
-
157
- // Truncate IP addresses.
158
-
159
- $wpdb->delete($wpdb->postmeta, ['meta_key' => '_kksr_ips']);
160
- }
161
-
162
- function canVote($p = null)
163
- {
164
- global $post;
165
- $p = $p ?: $post;
166
-
167
- $filterTag = prefix('can_vote');
168
- $strategies = getOption('strategies');
169
-
170
- // Archives and voting in archives is not allowed.
171
- if (is_archive() && ! in_array('archives', $strategies)) {
172
- return apply_filters($filterTag, false, $p);
173
- }
174
-
175
- // Not authenticated and guests are not allowed to vote.
176
- if (! is_user_logged_in() && ! in_array('guests', $strategies)) {
177
- return apply_filters($filterTag, false, $p);
178
- }
179
-
180
- // Unique ips are enforced.
181
- if (in_array('unique', $strategies)) {
182
- $ips = get_post_meta($p->ID, '_'.prefix('ips'));
183
-
184
- // Not a unique IP address.
185
- if (in_array(md5($_SERVER['REMOTE_ADDR']), $ips)) {
186
- return apply_filters($filterTag, false, $p);
187
- }
188
- }
189
-
190
- return apply_filters($filterTag, true, $p);
191
- }
192
-
193
- // function force($bool = true)
194
- // {
195
- // static $isForced;
196
-
197
- // if (! is_null($bool)) {
198
- // $isForced = (bool) $bool;
199
- // }
200
-
201
- // return (bool) $isForced;
202
- // }
203
-
204
- // function isForced()
205
- // {
206
- // return force(null);
207
- // }
208
-
209
- function isValidPost($p = null)
210
- {
211
- $bail = $p === false;
212
-
213
- global $post;
214
- $p = $p ?: $post;
215
- $p = is_object($p) ? $p : get_post($p);
216
-
217
- $filterTag = prefix('is_valid_post');
218
-
219
- if (! getOption('enable')) {
220
- // Not globally enabled.
221
- return apply_filters($filterTag, false, $p);
222
- }
223
-
224
- // if (isForced()) {
225
- // // Manually forced.
226
- // return apply_filters($filterTag, true, $p);
227
- // }
228
-
229
- if ($bail && (
230
- has_shortcode($p->post_content, KKSR_SHORTCODE)
231
- || has_shortcode($p->post_content, 'kkratings')
232
- )) {
233
- return apply_filters($filterTag, false, $p);
234
- }
235
-
236
- if ($status = get_post_meta($p->ID, '_'.prefix('status'), true)) {
237
- // Exclusive status.
238
- return apply_filters($filterTag, $status == 'enable', $p);
239
- }
240
-
241
- $categories = array_map(function ($category) {
242
- return $category->term_id;
243
- }, get_the_category($p->ID));
244
-
245
- $excludedCategories = getOption('exclude_categories', []);
246
- $excludedCategories = is_array($excludedCategories) ? $excludedCategories : [];
247
-
248
- $categoriesDiff = array_diff($categories, $excludedCategories);
249
-
250
- $bool = ($type = get_post_type($p))
251
- // post does not belong to an excluded category.
252
- && count($categories) == count($categoriesDiff)
253
- // post type is not an excluded location.
254
- && ! in_array($type, getOption('exclude_locations'));
255
-
256
- return apply_filters($filterTag, $bool, $p);
257
- }
258
-
259
- function isValidRequest($p = null)
260
- {
261
- $filterTag = prefix('is_valid_request');
262
-
263
- if (! getOption('enable')) {
264
- // Not globally enabled.
265
- return apply_filters($filterTag, false);
266
- }
267
-
268
- // if (isForced()) {
269
- // // Manually forced.
270
- // return apply_filters($filterTag, true);
271
- // }
272
-
273
- $bool =
274
- // home or front page AND home is not an excluded location.
275
- (! in_array('home', getOption('exclude_locations')) && (is_front_page() || is_home()))
276
- // archives AND archives is not an excluded location.
277
- || (! in_array('archives', getOption('exclude_locations')) && is_archive())
278
- // singular AND (exclusively enabled OR (post does not belong to an excluded category AND post type is not an excluded location)).
279
- || (is_singular() && isValidPost($p));
280
-
281
- return apply_filters($filterTag, $bool);
282
- }
283
-
284
- // Calculations
285
-
286
- function toNormalizedRatings($ratings, $from = 5, $to = 5)
287
- {
288
- $to = (int) $to;
289
- $from = (int) $from;
290
- $ratings = (float) $ratings;
291
-
292
- return $ratings / $from * $to; // $ratings / ($from / $to);
293
- }
294
-
295
- function calculateScore($total, $count, $from = 5, $to = 5)
296
- {
297
- $to = (int) $to;
298
- $from = (int) $from;
299
- $count = (float) $count;
300
- $total = (float) $total;
301
-
302
- return $count ? round(($total / $count) * ($from / $to), 1, PHP_ROUND_HALF_DOWN) : 0;
303
- }
304
-
305
- // We will neglect $from but here for consistency!
306
- function calculatePercentage($total, $count, $from = 5, $to = 5)
307
- {
308
- $to = (int) $to;
309
- $from = (int) $from;
310
- $count = (float) $count;
311
- $total = (float) $total;
312
-
313
- return $count ? round($total / $count / $to * 100, 2, PHP_ROUND_HALF_DOWN) : 0;
314
- }
315
-
316
- function calculateWidth($score, $size = null, $pad = 4)
317
- {
318
- $score = (float) $score;
319
- $size = (int) ($size ?: getOption('size'));
320
-
321
- return $score * $size + $score * $pad;
322
- }
323
-
324
- function scoreToRatings($score, $count, $from = 5, $to = 5)
325
- {
326
- $to = (int) $to;
327
- $from = (int) $from;
328
- $count = (int) $count;
329
- $score = (float) $score;
330
-
331
- if ($from <= 0 || $to <= 0) {
332
- return 0;
333
- }
334
-
335
- if ($score < 0) {
336
- $score = 1;
337
- }
338
-
339
- if ($score > $from) {
340
- $score = $from;
341
- }
342
-
343
- return (float) round($score * $count / ($from / $to), 0, PHP_ROUND_HALF_DOWN);
344
- }
345
-
346
- function extractPosition($position = null)
347
- {
348
- $position = $position ?: getOption('position');
349
-
350
- $placement = 'top';
351
- $alignment = 'left';
352
-
353
- if (strpos($position, 'top-') === 0) {
354
- $placement = 'top';
355
- $alignment = substr($position, 4);
356
- } elseif (strpos($position, 'bottom-') === 0) {
357
- $placement = 'bottom';
358
- $alignment = substr($position, 7);
359
- }
360
-
361
- return [$placement, $alignment];
362
- }
363
-
364
- function vote($idOrPost, $rating)
365
- {
366
- $stars = (int) getOption('stars');
367
- $rating = apply_filters(prefix('rating'), (float) $rating);
368
- $id = is_object($idOrPost) ? $idOrPost->ID : $idOrPost;
369
-
370
- if ($rating < 0 || $rating > $stars) {
371
- throw new \Exception(sprintf(
372
- __('You can only rate between 0 and %s.', 'kk-star-ratings'),
373
- $stars
374
- ));
375
- }
376
-
377
- $ratings = (float) get_post_meta($id, '_'.prefix('ratings'), true);
378
- $ratings += toNormalizedRatings($rating, $stars);
379
-
380
- $count = (int) get_post_meta($id, '_'.prefix('casts'), true);
381
- $count += 1;
382
-
383
- update_post_meta($id, '_'.prefix('ratings'), $ratings);
384
- update_post_meta($id, '_'.prefix('casts'), $count);
385
- // For legacy reasons.
386
- update_post_meta($id, '_'.prefix('avg'), calculateScore($ratings, $count, $stars));
387
-
388
- do_action(prefix('vote'), $id, $rating);
389
-
390
- return [$ratings, $count];
391
- }
392
-
393
- function queue($post = null)
394
- {
395
- // static $queued;
396
-
397
- // if ($queued) {
398
- // return;
399
- // }
400
-
401
- styles(true);
402
- scripts(true);
403
- stylesheet(true);
404
-
405
- $sd = function () use ($post) {
406
- echo structuredData(true, $post);
407
- };
408
-
409
- if (! has_action('wp_footer', $sd)) {
410
- add_action('wp_footer', $sd);
411
- }
412
-
413
- // $queued = true;
414
- }
415
-
416
- function get($post = null, $force = null)
417
- {
418
- $force = is_null($force) ? ($post ? true : false) : $force;
419
-
420
- if ($force) {
421
- queue($post);
422
- }
423
-
424
- return markup(null, $force, $post);
425
- }
426
-
427
- function quick($atts, $shortcode = '')
428
- {
429
- extract(shortcode_atts(['id' => null, 'force' => null], $atts, $shortcode));
430
-
431
- $force = is_null($force)
432
- ? (in_array('force', (array) $atts) ? true : null)
433
- : (in_array($force, ['null', 'false']) ? false : (bool) $force);
434
-
435
- return get($id ?: false, $force);
436
- }
437
-
438
- function collect($limit = 5, $taxonomyId = null, $offset = 0)
439
- {
440
- global $wpdb;
441
- $postsTable = $wpdb->posts;
442
- $postMetaTable = $wpdb->prefix.'postmeta';
443
- $base = getOption('stars') / 5;
444
-
445
- $querySelect = "
446
- SELECT
447
- posts.ID,
448
- ROUND(postmeta_ratings.meta_value / postmeta_count.meta_value * %f, 1) score
449
- FROM {$postsTable} posts
450
- ";
451
-
452
- $queryJoins = "
453
- JOIN {$postMetaTable} postmeta_ratings
454
- ON posts.ID = postmeta_ratings.post_id
455
- JOIN {$postMetaTable} postmeta_count
456
- ON posts.ID = postmeta_count.post_id
457
- ";
458
-
459
- $queryConditions = "
460
- WHERE
461
- posts.post_status = 'publish'
462
- AND CAST(postmeta_count.meta_value AS UNSIGNED) != 0
463
- AND postmeta_count.meta_key = '_kksr_casts'
464
- AND postmeta_ratings.meta_key = '_kksr_ratings'
465
- ";
466
-
467
- $queryOrder = '
468
- ORDER BY
469
- score DESC,
470
- CAST(postmeta_count.meta_value AS UNSIGNED) DESC
471
- ';
472
-
473
- $queryLimit = 'LIMIT %d, %d';
474
-
475
- $queryArgs = [$base, $offset, $limit];
476
-
477
- if ($taxonomyId) {
478
- $termTaxonomyTable = $wpdb->prefix.'term_taxonomy';
479
- $termRelationshipsTable = $wpdb->prefix.'term_relationships';
480
-
481
- $queryJoins .= "
482
- JOIN {$termRelationshipsTable} term_relations
483
- ON posts.ID = term_relations.object_id
484
- JOIN {$termTaxonomyTable} term_taxonomies
485
- ON term_relations.term_taxonomy_id = term_taxonomies.term_taxonomy_id
486
- ";
487
-
488
- $queryConditions .= '
489
- AND term_taxonomies.term_id=%d
490
- ';
491
-
492
- $queryArgs = [$base, $taxonomyId, $offset, $limit];
493
- }
494
-
495
- $query = $querySelect
496
- .PHP_EOL.$queryJoins
497
- .PHP_EOL.$queryConditions
498
- .PHP_EOL.$queryOrder
499
- .PHP_EOL.$queryLimit;
500
-
501
- $preparedQuery = call_user_func_array([$wpdb, 'prepare'], array_merge([$query], $queryArgs));
502
-
503
- return $wpdb->get_results($preparedQuery);
504
- }
505
-
506
- // Admin
507
-
508
- function getAdminTabs()
509
- {
510
- return apply_filters(prefix('admin_tabs'), [
511
- 'general' => __('General', 'kk-star-ratings'),
512
- 'rich-snippets' => __('Rich Snippets', 'kk-star-ratings'),
513
- 'appearance' => __('Appearance', 'kk-star-ratings'),
514
- ]);
515
- }
516
-
517
- function getDefaultAdminTab()
518
- {
519
- return apply_filters(prefix('default_admin_tab'), 'general');
520
- }
521
-
522
- function getActiveAdminTab()
523
- {
524
- $filterTag = prefix('active_admin_tab');
525
-
526
- $defaultTab = getDefaultAdminTab();
527
-
528
- if (! isset($_GET['tab'])) {
529
- return apply_filters($filterTag, $defaultTab);
530
- }
531
-
532
- $tab = $_GET['tab'];
533
-
534
- if (empty($tab)) {
535
- return apply_filters($filterTag, $defaultTab);
536
- }
537
-
538
- $tabs = getAdminTabs();
539
-
540
- if (isset($tabs[$tab])) {
541
- return apply_filters($filterTag, $tab);
542
- }
543
-
544
- return apply_filters($filterTag, null);
545
- }
546
-
547
- function isActiveAdminTab($tab)
548
- {
549
- return apply_filters(prefix('is_active_admin_tab'), $tab == getActiveAdminTab());
550
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/global.php ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ namespace Bhittani\StarRating;
13
+
14
+ if (! defined('ABSPATH')) {
15
+ http_response_code(404);
16
+ die();
17
+ }
18
+
19
+ function prefix($str)
20
+ {
21
+ $prefix = config('nick').'_';
22
+
23
+ if (strpos($str, $prefix) === 0) {
24
+ return $str;
25
+ }
26
+
27
+ return $prefix.$str;
28
+ }
29
+
30
+ function meta_prefix($str)
31
+ {
32
+ $prefix = '_'.config('nick').'_';
33
+
34
+ if (strpos($str, $prefix) === 0) {
35
+ return $str;
36
+ }
37
+
38
+ return $prefix.$str;
39
+ }
src/hook.php ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ namespace Bhittani\StarRating;
13
+
14
+ if (! defined('ABSPATH')) {
15
+ http_response_code(404);
16
+ die();
17
+ }
18
+
19
+ function add_plugin_filter($tag, callable $callback, $priority = 10, $acceptedArgs = 1)
20
+ {
21
+ $tag = config('nick').'::filter.'.$tag;
22
+
23
+ return add_filter($tag, $callback, $priority, $acceptedArgs);
24
+ }
25
+
26
+ function apply_plugin_filters($tag, ...$arguments)
27
+ {
28
+ $tag = config('nick').'::filter.'.$tag;
29
+
30
+ return apply_filters($tag, ...$arguments);
31
+ }
32
+
33
+ function remove_plugin_filter($tag, callable $callback, $priority = 10)
34
+ {
35
+ $tag = config('nick').'::filter.'.$tag;
36
+
37
+ return remove_filter($tag, $callback, $priority);
38
+ }
39
+
40
+ function add_plugin_action($tag, callable $callback, $priority = 10, $acceptedArgs = 1)
41
+ {
42
+ $tag = config('nick').'::action.'.$tag;
43
+
44
+ return add_action($tag, $callback, $priority, $acceptedArgs);
45
+ }
46
+
47
+ function do_plugin_action($tag, ...$arguments)
48
+ {
49
+ $tag = config('nick').'::action.'.$tag;
50
+
51
+ return do_action($tag, ...$arguments);
52
+ }
53
+
54
+ function remove_plugin_action($tag, callable $callback, $priority = 10)
55
+ {
56
+ $tag = config('nick').'::action.'.$tag;
57
+
58
+ return remove_action($tag, $callback, $priority);
59
+ }
src/index.php DELETED
@@ -1,14 +0,0 @@
1
- <?php
2
-
3
- /*
4
- * This file is part of bhittani/kk-star-ratings.
5
- *
6
- * (c) Kamal Khan <shout@bhittani.com>
7
- *
8
- * This source file is subject to the GPL v2 license that
9
- * is bundled with this source code in the file LICENSE.
10
- */
11
-
12
- http_response_code(404);
13
-
14
- die();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/legacy.php CHANGED
@@ -5,27 +5,94 @@
5
  *
6
  * (c) Kamal Khan <shout@bhittani.com>
7
  *
8
- * This source file is subject to the GPL v2 license that
9
- * is bundled with this source code in the file LICENSE.
10
  */
11
 
 
 
 
 
 
12
  if (! function_exists('kk_star_ratings')) {
13
- function kk_star_ratings($postOrForce = null, $force = null)
14
  {
15
- $force = is_null($force) ? (is_bool($postOrForce) ? $postOrForce : null) : $force;
 
 
 
 
16
 
17
- return \Bhittani\StarRating\get(is_bool($postOrForce) ? null : $postOrForce, $force);
18
  }
19
  }
20
 
21
  if (! function_exists('kk_star_ratings_get')) {
22
  function kk_star_ratings_get($limit = 5, $taxonomyId = null, $offset = 0)
23
  {
24
- return \Bhittani\StarRating\collect($limit, $taxonomyId, $offset);
25
- }
26
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- add_shortcode('kkratings', KKSR_NAMESPACE.'legacyShortcode'); function legacyShortcode($atts)
29
- {
30
- return quick($atts, 'kkratings');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  }
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
+ if (! defined('ABSPATH')) {
13
+ http_response_code(404);
14
+ die();
15
+ }
16
+
17
  if (! function_exists('kk_star_ratings')) {
18
+ function kk_star_ratings($idOrPost = null)
19
  {
20
+ $id = null;
21
+
22
+ if ($idOrPost) {
23
+ $id = is_object($idOrPost) ? $idOrPost->ID : $idOrPost;
24
+ }
25
 
26
+ return Bhittani\StarRating\response(compact('id'), false);
27
  }
28
  }
29
 
30
  if (! function_exists('kk_star_ratings_get')) {
31
  function kk_star_ratings_get($limit = 5, $taxonomyId = null, $offset = 0)
32
  {
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';
72
+ $termRelationshipsTable = $wpdb->prefix.'term_relationships';
73
+
74
+ $queryJoins .= "
75
+ JOIN {$termRelationshipsTable} term_relations
76
+ ON posts.ID = term_relations.object_id
77
+ JOIN {$termTaxonomyTable} term_taxonomies
78
+ ON term_relations.term_taxonomy_id = term_taxonomies.term_taxonomy_id
79
+ ";
80
+
81
+ $queryConditions .= '
82
+ AND term_taxonomies.term_id=%d
83
+ ';
84
+
85
+ $queryArgs = [$base, $taxonomyId, $offset, $limit];
86
+ }
87
+
88
+ $query = $querySelect
89
+ .PHP_EOL.$queryJoins
90
+ .PHP_EOL.$queryConditions
91
+ .PHP_EOL.$queryOrder
92
+ .PHP_EOL.$queryLimit;
93
+
94
+ $preparedQuery = $wpdb->prepare($query, ...$queryArgs);
95
+
96
+ return $wpdb->get_results($preparedQuery);
97
+ }
98
  }
src/markup.php DELETED
@@ -1,64 +0,0 @@
1
- <?php
2
-
3
- /*
4
- * This file is part of bhittani/kk-star-ratings.
5
- *
6
- * (c) Kamal Khan <shout@bhittani.com>
7
- *
8
- * This source file is subject to the GPL v2 license that
9
- * is bundled with this source code in the file LICENSE.
10
- */
11
-
12
- namespace Bhittani\StarRating;
13
-
14
- add_filter('the_content', KKSR_NAMESPACE.'markup'); function markup($content, $force = false, $p = null)
15
- {
16
- global $post;
17
- $p = $p ?: $post;
18
- $p = is_object($p) ? $p : get_post($p);
19
-
20
- if (! $force && ! (isValidRequest($p) && isValidPost($p))) {
21
- return $content;
22
- }
23
-
24
- if (! $force && (
25
- has_shortcode($content, KKSR_SHORTCODE)
26
- || has_shortcode($content, 'kkratings')
27
- )) {
28
- return $content;
29
- }
30
-
31
- if (! is_null($content) && in_array(get_post_type($p), getOption('manual_control'))) {
32
- return $content;
33
- }
34
-
35
- $id = $p->ID;
36
- $isRtl = is_rtl();
37
- $size = (int) getOption('size');
38
- $stars = (int) getOption('stars');
39
- list($placement, $alignment) = extractPosition();
40
- $total = get_post_meta($id, '_kksr_ratings', true);
41
- $count = (int) get_post_meta($id, '_kksr_casts', true);
42
- $score = calculateScore($total, $count, $stars);
43
- $percent = calculatePercentage($total, $count);
44
- $width = calculateWidth($score, $size);
45
- $disabled = ! canVote($p);
46
-
47
- ob_start();
48
- include KKSR_PATH_VIEWS.'markup.php';
49
- $markup = ob_get_clean();
50
-
51
- return $placement === 'bottom' ? ($content.$markup) : ($markup.$content);
52
- }
53
-
54
- add_filter('kksr_score', KKSR_NAMESPACE.'scoreFilter'); function scoreFilter($score)
55
- {
56
- return number_format($score, 1);
57
- }
58
-
59
- add_filter('kksr_count', KKSR_NAMESPACE.'countFilter', 10); function countFilter($count)
60
- {
61
- // return str_pad($count, 2, 0, is_rtl() ? STR_PAD_RIGHT : STR_PAD_LEFT);
62
-
63
- return str_pad($count, 2, 0, STR_PAD_LEFT);
64
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/metabox.php CHANGED
@@ -5,55 +5,67 @@
5
  *
6
  * (c) Kamal Khan <shout@bhittani.com>
7
  *
8
- * This source file is subject to the GPL v2 license that
9
- * is bundled with this source code in the file LICENSE.
10
  */
11
 
12
  namespace Bhittani\StarRating;
13
 
14
- add_action('add_meta_boxes', KKSR_NAMESPACE.'metabox', 10, 2); function metabox($type, $post)
 
 
 
 
 
 
15
  {
16
- $customPostTypes = get_post_types(['publicly_queryable' => true, '_builtin' => false], 'names');
17
- $postTypes = array_merge(['post', 'page'], $customPostTypes);
18
 
19
- $count = get_post_meta($post->ID, '_'.prefix('casts'), true);
20
- $ratings = get_post_meta($post->ID, '_'.prefix('ratings'), true);
21
- $score = calculateScore($ratings, $count, getOption('stars'));
22
 
23
- $icon = '<span class="dashicons dashicons-star-empty" style="margin-right: .25rem; font-size: 18px;"></span>';
24
 
25
- $legend = '';
26
 
27
- if ($score) {
28
- $legend = "
29
- <span style=\"float:right;color:#666;\">
30
- {$score}
31
- <span style=\"font-weight:normal;color:#ddd;\">/</span>
32
- <span style=\"font-weight:normal;color:#aaa;\">{$count}</span>
33
- </span>
34
- ";
 
35
  }
36
 
37
- add_meta_box(KKSR_SLUG, $icon.KKSR_LABEL.$legend, KKSR_NAMESPACE.'echoMetabox', $postTypes, 'side');
 
 
 
 
 
 
 
 
 
38
  }
39
 
40
- function echoMetabox($post)
41
  {
42
- wp_nonce_field(basename(__FILE__), KKSR_SLUG.'-metabox-nonce');
43
 
44
- $resetFieldName = '__'.prefix('reset');
45
- $statusFieldName = '_'.prefix('status');
46
- $status = get_post_meta($post->ID, $statusFieldName, true);
47
 
48
- ob_start();
49
- include KKSR_PATH_VIEWS.'metabox/index.php';
50
- echo ob_get_clean();
51
  }
52
 
53
- add_action('save_post', KKSR_NAMESPACE.'saveMetabox'); function saveMetabox($id)
 
54
  {
55
- if ((! isset($_POST[KKSR_SLUG.'-metabox-nonce']))
56
- || (! wp_verify_nonce($_POST[KKSR_SLUG.'-metabox-nonce'], basename(__FILE__)))
57
  || (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
58
  || (! current_user_can('edit_post', $id))
59
  // || (is_multisite() && ms_is_switched())
@@ -62,13 +74,32 @@ add_action('save_post', KKSR_NAMESPACE.'saveMetabox'); function saveMetabox($id)
62
  return;
63
  }
64
 
65
- update_post_meta($id, '_'.prefix('status'), $_POST['_'.prefix('status')]);
 
66
 
67
- if (checked($_POST['__'.prefix('reset')], '1')) {
68
- delete_post_meta($id, '_kksr_avg');
69
- delete_post_meta($id, '_'.prefix('casts'));
70
- delete_post_meta($id, '_'.prefix('ratings'));
 
 
 
 
 
 
 
 
 
 
 
71
  }
72
 
73
- do_action('kksr_save_metabox');
 
 
 
 
 
 
 
74
  }
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
  namespace Bhittani\StarRating;
13
 
14
+ if (! defined('ABSPATH')) {
15
+ http_response_code(404);
16
+ die();
17
+ }
18
+
19
+ add_action('add_meta_boxes', __NAMESPACE__.'\metabox', 10, 2);
20
+ function metabox($type, $post)
21
  {
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
 
30
+ $legend = '';
31
 
32
+ if ($score) {
33
+ $legend = "
34
+ <span style=\"float:right;color:#666;\">
35
+ {$score}
36
+ <span style=\"font-weight:normal;color:#ddd;\">/</span>
37
+ <span style=\"font-weight:normal;color:#aaa;\">{$count}</span>
38
+ </span>
39
+ ";
40
+ }
41
  }
42
 
43
+ $customPostTypes = get_post_types(['publicly_queryable' => true, '_builtin' => false], 'names');
44
+ $postTypes = array_merge(['post', 'page'], $customPostTypes);
45
+
46
+ add_meta_box(
47
+ config('slug'),
48
+ $icon.config('name').$legend,
49
+ __NAMESPACE__.'\metabox_callback',
50
+ $postTypes,
51
+ 'side'
52
+ );
53
  }
54
 
55
+ function metabox_callback($post)
56
  {
57
+ wp_nonce_field(basename(__FILE__), config('slug').'-metabox');
58
 
59
+ $content = apply_plugin_filters('metabox', '', $post);
 
 
60
 
61
+ echo view('metabox.index', compact('content'));
 
 
62
  }
63
 
64
+ add_action('save_post', __NAMESPACE__.'\save_metabox');
65
+ function save_metabox($id)
66
  {
67
+ if ((! isset($_POST[config('slug').'-metabox']))
68
+ || (! wp_verify_nonce($_POST[config('slug').'-metabox'], basename(__FILE__)))
69
  || (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
70
  || (! current_user_can('edit_post', $id))
71
  // || (is_multisite() && ms_is_switched())
74
  return;
75
  }
76
 
77
+ do_plugin_action('save_metabox', $id);
78
+ }
79
 
80
+ add_plugin_filter('metabox', __NAMESPACE__.'\metabox_content', 9, 2);
81
+ function metabox_content($content, $post)
82
+ {
83
+ $resetFieldName = meta_prefix('reset');
84
+ $statusFieldName = meta_prefix('status');
85
+ $status = get_post_meta($post->ID, $statusFieldName, true);
86
+
87
+ return $content.view('metabox.content', compact('status', 'statusFieldName', 'resetFieldName'));
88
+ }
89
+
90
+ add_plugin_action('save_metabox', __NAMESPACE__.'\save_default_metabox', 9);
91
+ function save_default_metabox($id)
92
+ {
93
+ if (isset($_POST[meta_prefix('status')])) {
94
+ update_post_meta($id, meta_prefix('status'), $_POST[meta_prefix('status')]);
95
  }
96
 
97
+ if (isset($_POST[meta_prefix('reset')])
98
+ && checked($_POST[meta_prefix('reset')], '1', false)
99
+ ) {
100
+ delete_post_meta($id, meta_prefix('ref'));
101
+ delete_post_meta($id, meta_prefix('avg'));
102
+ delete_post_meta($id, meta_prefix('casts'));
103
+ delete_post_meta($id, meta_prefix('ratings'));
104
+ }
105
  }
src/post.php ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ namespace Bhittani\StarRating;
13
+
14
+ if (! defined('ABSPATH')) {
15
+ http_response_code(404);
16
+ die();
17
+ }
18
+
19
+ add_filter('the_content', __NAMESPACE__.'\content_filter', 10);
20
+ function content_filter($content)
21
+ {
22
+ $shortcode = config('shortcode');
23
+
24
+ if (has_shortcode($content, $shortcode)
25
+ // Legacy support
26
+ || has_shortcode($content, 'kkratings')
27
+ ) {
28
+ return $content;
29
+ }
30
+
31
+ // if (! validate()) {
32
+ // return $content;
33
+ // }
34
+
35
+ if (in_array(get_post_type(), (array) get_option(prefix('manual_control'), []))) {
36
+ return $content;
37
+ }
38
+
39
+ $align = 'left';
40
+ $valign = 'top';
41
+
42
+ $position = get_option(prefix('position'));
43
+
44
+ if (strpos($position, 'top-') === 0) {
45
+ $valign = 'top';
46
+ $align = substr($position, 4);
47
+ } elseif (strpos($position, 'bottom-') === 0) {
48
+ $valign = 'bottom';
49
+ $align = substr($position, 7);
50
+ }
51
+
52
+ $shortcode = "[{$shortcode} force=\"false\" valign=\"{$valign}\" align=\"{$align}\"]";
53
+
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
+ update_post_meta($id, meta_prefix('ref'), $ip, $ip);
188
+ }
189
+
190
+ add_action('wp_head', __NAMESPACE__.'\structured_data');
191
+ function structured_data()
192
+ {
193
+ if (! get_option(prefix('enable'))) {
194
+ return;
195
+ }
196
+
197
+ if (is_singular() && get_option(prefix('grs'))) {
198
+ $id = get_post_field('ID');
199
+ $title = 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">';
206
+ $sd = get_option(prefix('sd'));
207
+ $sd = str_replace('[title]', $title, $sd);
208
+ $sd = str_replace('[best]', $best, $sd);
209
+ $sd = str_replace('[count]', $count, $sd);
210
+ $sd = str_replace('[score]', $score, $sd);
211
+ echo $sd;
212
+ echo '</script>';
213
+ }
214
+ }
215
+ }
src/response.php ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ namespace Bhittani\StarRating;
13
+
14
+ if (! defined('ABSPATH')) {
15
+ http_response_code(404);
16
+ die();
17
+ }
18
+
19
+ function response(array $payload = [], $validate = true)
20
+ {
21
+ $payload = array_merge([
22
+ 'id' => null,
23
+ 'slug' => null,
24
+ 'score' => null,
25
+ 'count' => null,
26
+ 'align' => null,
27
+ 'valign' => null,
28
+ 'force' => true,
29
+ 'disabled' => false,
30
+ 'size' => get_option(prefix('size')),
31
+ 'best' => get_option(prefix('stars')),
32
+ 'greet' => get_option(prefix('greet')),
33
+ ], array_filter($payload, function ($value) {
34
+ return ! is_null($value);
35
+ }));
36
+
37
+ if (! get_option(prefix('enable'))) {
38
+ return '';
39
+ }
40
+
41
+ if (! $payload['id'] && ! $payload['slug']) {
42
+ $payload['id'] = get_post_field('ID');
43
+ }
44
+
45
+ $force = $payload['force'] ?: (! $validate);
46
+
47
+ if (! $force
48
+ && ! validate(true, $payload['id'], $payload['slug'])
49
+ ) {
50
+ return;
51
+ }
52
+
53
+ if (! $payload['id']) {
54
+ $payload['disabled'] = true;
55
+ }
56
+
57
+ if ($payload['score']) {
58
+ $payload['disabled'] = true;
59
+ } elseif ($payload['id']) {
60
+ $payload['score'] = apply_plugin_filters('score', $payload['score'], $payload['best'], $payload['id'], $payload['slug']);
61
+ }
62
+
63
+ if ($payload['count']) {
64
+ $payload['disabled'] = true;
65
+ } elseif ($payload['id']) {
66
+ $payload['count'] = apply_plugin_filters('count', $payload['count'], $payload['id'], $payload['slug']);
67
+ }
68
+
69
+ if (! $payload['disabled']) {
70
+ $payload['disabled'] = ! apply_plugin_filters('can_vote', ! $payload['disabled'], $payload['id'], $payload['slug']);
71
+ }
72
+
73
+ if (! $payload['id'] || $payload['disabled']) {
74
+ $payload['greet'] = '';
75
+ }
76
+
77
+ if ($payload['id'] && ! $payload['disabled']) {
78
+ $payload['greet'] = apply_plugin_filters('greet', $payload['greet'], $payload['id'], $payload['slug']);
79
+ }
80
+
81
+ $payload['best'] = max((int) $payload['best'], 1);
82
+ $payload['count'] = max((int) $payload['count'], 0);
83
+ $payload['score'] = min(max($payload['score'], 0), $payload['best']);
84
+
85
+ $percentage = $payload['score'] / $payload['best'] * 100;
86
+ $percentage = round($percentage, 2, PHP_ROUND_HALF_DOWN);
87
+ $payload['percentage'] = min(max($percentage, 0), 100);
88
+
89
+ return view('markup', $payload);
90
+ }
src/shortcode.php CHANGED
@@ -5,25 +5,45 @@
5
  *
6
  * (c) Kamal Khan <shout@bhittani.com>
7
  *
8
- * This source file is subject to the GPL v2 license that
9
- * is bundled with this source code in the file LICENSE.
10
  */
11
 
12
  namespace Bhittani\StarRating;
13
 
14
- add_shortcode(KKSR_SHORTCODE, KKSR_NAMESPACE.'shortcode'); function shortcode($atts)
15
- {
16
- return quick($atts, KKSR_SHORTCODE);
17
  }
18
 
19
- // add_filter('shortcode_atts_kkratings', KKSR_NAMESPACE.'filterShortcode');
20
- // add_filter('shortcode_atts_'.KKSR_SHORTCODE, KKSR_NAMESPACE.'filterShortcode');
21
- // function filterShortcode($atts)
22
- // {
23
- // // if ($atts['id']) {
24
- // // queue();
25
- // // }
26
- // die(var_dump($atts));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- // return $atts;
29
- // }
 
 
 
 
 
 
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
  namespace Bhittani\StarRating;
13
 
14
+ if (! defined('ABSPATH')) {
15
+ http_response_code(404);
16
+ die();
17
  }
18
 
19
+ add_shortcode(config('shortcode'), __NAMESPACE__.'\shortcode');
20
+ // Legacy support.
21
+ add_shortcode('kkratings', __NAMESPACE__.'\shortcode');
22
+ function shortcode($attrs, $content, $tag)
23
+ {
24
+ $attrs = (array) $attrs;
25
+
26
+ foreach ($attrs as $key => &$value) {
27
+ if (is_numeric($key)) {
28
+ $attrs[$value] = true;
29
+ unset($attrs[$key]);
30
+ }
31
+ if ($value === 'false') {
32
+ $value = false;
33
+ }
34
+ if ($value === 'true') {
35
+ $value = true;
36
+ }
37
+ if ($value === 'null') {
38
+ $value = null;
39
+ }
40
+ }
41
 
42
+ $attrs = shortcode_atts(array_fill_keys([
43
+ 'id', 'slug', 'score', 'count', 'best',
44
+ 'size', 'align', 'valign', 'disabled',
45
+ 'greet', 'force',
46
+ ], null), $attrs, $tag);
47
+
48
+ return response($attrs);
49
+ }
src/structure.php DELETED
@@ -1,47 +0,0 @@
1
- <?php
2
-
3
- /*
4
- * This file is part of bhittani/kk-star-ratings.
5
- *
6
- * (c) Kamal Khan <shout@bhittani.com>
7
- *
8
- * This source file is subject to the GPL v2 license that
9
- * is bundled with this source code in the file LICENSE.
10
- */
11
-
12
- namespace Bhittani\StarRating;
13
-
14
- add_action('wp_head', KKSR_NAMESPACE.'structuredData'); function structuredData($force = false, $p = null)
15
- {
16
- if (! getOption('grs')) {
17
- return;
18
- }
19
-
20
- global $post;
21
- $p = $p ?: $post;
22
- $p = is_object($p) ? $p : get_post($p);
23
-
24
- if (! (($force || isValidRequest($p)) && is_singular())) {
25
- return;
26
- }
27
-
28
- $id = $p->ID;
29
- $count = (int) get_post_meta($id, '_kksr_casts', true);
30
-
31
- if (! $count) {
32
- return;
33
- }
34
-
35
- $stars = (int) getOption('stars');
36
- $total = get_post_meta($id, '_kksr_ratings', true);
37
- $score = calculateScore($total, $count, $stars);
38
- $type = getOption('sd_type');
39
- $context = getOption('sd_context');
40
- $name = get_the_title($id);
41
-
42
- ob_start();
43
- include KKSR_PATH_VIEWS.'structured-data.php';
44
- $html = ob_get_clean();
45
-
46
- echo apply_filters(prefix('structured_data'), $html, $p);
47
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/validate.php ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ namespace Bhittani\StarRating;
13
+
14
+ if (! defined('ABSPATH')) {
15
+ http_response_code(404);
16
+ die();
17
+ }
18
+
19
+ function validate($default = true, $id = null, $slug = null)
20
+ {
21
+ return (bool) apply_plugin_filters('validate', $default, $id, $slug);
22
+ }
23
+
24
+ add_plugin_filter('validate', __NAMESPACE__.'\validate_request', 9, 3);
25
+ function validate_request($bool, $id, $slug)
26
+ {
27
+ $excludedLocations = (array) get_option(prefix('exclude_locations'), []);
28
+
29
+ if ((is_front_page() || is_home())
30
+ && in_array('home', $excludedLocations)
31
+ ) {
32
+ return false;
33
+ }
34
+
35
+ if (is_archive()
36
+ && in_array('archives', $excludedLocations)
37
+ ) {
38
+ return false;
39
+ }
40
+
41
+ return $bool;
42
+ }
43
+
44
+ add_plugin_filter('can_vote', __NAMESPACE__.'\can_vote', 9, 3);
45
+ function can_vote($bool, $id, $slug)
46
+ {
47
+ if (! is_user_logged_in()
48
+ && ! in_array('guests', (array) get_option(prefix('strategies'), []))) {
49
+ return false;
50
+ }
51
+
52
+ return $bool;
53
+ }
src/view.php ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ namespace Bhittani\StarRating;
13
+
14
+ use SplStack;
15
+ use RuntimeException;
16
+
17
+ if (! defined('ABSPATH')) {
18
+ http_response_code(404);
19
+ die();
20
+ }
21
+
22
+ function view($templates, array $payload = [])
23
+ {
24
+ foreach ((array) $templates as $template) {
25
+ $template = str_replace('.', '/', $template);
26
+
27
+ if (strpos($template, '/php') === strlen($template) - 4) {
28
+ $template = substr($template, 0, -4);
29
+ }
30
+
31
+ $filename = $template.'.php';
32
+
33
+ $filepath = config('views').ltrim($filename, '\/');
34
+
35
+ if (is_file($filepath)) {
36
+ $name = str_replace('/', '.', $template);
37
+
38
+ $content = get_view($filepath, $payload);
39
+
40
+ $content = apply_plugin_filters('view', $content, $name);
41
+
42
+ return apply_plugin_filters('view:'.$name, $content);
43
+ }
44
+ }
45
+
46
+ throw new RuntimeException('None of the templates exist');
47
+ }
48
+
49
+ function get_view($__file__, array $__payload__ = [])
50
+ {
51
+ static $__cascade__;
52
+
53
+ if (! is_file($__file__)) {
54
+ throw new RuntimeException("View {$__file__} does not exist");
55
+ }
56
+
57
+ $__cascade__ = $__cascade__ ?: new SplStack();
58
+
59
+ $__cascade__->push(array_merge(
60
+ $__cascade__->isEmpty() ? [] : $__cascade__->top(),
61
+ $__payload__
62
+ ));
63
+
64
+ extract($__cascade__->top());
65
+
66
+ ob_start();
67
+
68
+ include $__file__;
69
+
70
+ $__cascade__->pop();
71
+
72
+ return ob_get_clean();
73
+ }
views/active-star.php ADDED
@@ -0,0 +1 @@
 
1
+ <?= \Bhittani\StarRating\view('star') ?>
views/active-stars.php ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
1
+ <div class="kksr-stars-active" style="width: <?= $percentage ?>%;">
2
+ <?php for ($i = 1; $i <= $best; $i++) : ?>
3
+ <div class="kksr-star">
4
+ <?= \Bhittani\StarRating\view('active-star') ?>
5
+ </div>
6
+ <?php endfor; ?>
7
+ </div>
views/admin/content.php ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if (! defined('ABSPATH')) {
3
+ http_response_code(404);
4
+ die();
5
+ }
6
+ ?>
7
+
8
+ <form method="POST" action="options.php?tab=<?= $active; ?>" style="margin: 2rem;">
9
+ <?php submit_button(); ?>
10
+ <?php settings_fields($slug); ?>
11
+ <?php do_settings_sections($slug); ?>
12
+ <?php submit_button(); ?>
13
+ </form>
views/admin/contents.php DELETED
@@ -1,5 +0,0 @@
1
- <form action="options.php?tab=<?php echo $active; ?>" method="POST" style="margin: 2rem;">
2
- <?php settings_fields(KKSR_SLUG); ?>
3
- <?php do_settings_sections(KKSR_SLUG); ?>
4
- <?php submit_button(); ?>
5
- </form>
 
 
 
 
 
views/admin/fields/checkbox.php CHANGED
@@ -1,7 +1,12 @@
1
- <label>
2
- <input type="checkbox" name="<?php echo $name; ?>" value="<?php echo $value; ?>"
3
- <?php echo isset($id) ? ('id="'.$id.'"') : ''; ?>
4
- <?php echo $checked ? 'checked="checked"' : ''; ?>>
 
 
5
 
6
- <?php echo isset($label) ? $label : ''; ?>
 
 
 
7
  </label>
1
+ <?php
2
+ if (! defined('ABSPATH')) {
3
+ http_response_code(404);
4
+ die();
5
+ }
6
+ ?>
7
 
8
+ <label>
9
+ <input type="checkbox" name="<?= $name ?>" value="<?= $value ?>"
10
+ <?= $checked ? 'checked="checked"' : '' ?>>
11
+ <?= $label ?>
12
  </label>
views/admin/fields/code.php ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if (! defined('ABSPATH')) {
3
+ http_response_code(404);
4
+ die();
5
+ }
6
+ ?>
7
+
8
+ <textarea rows="15" cols="50" name="<?= $name ?>"
9
+ style="font-family: monospace; padding: .5rem;"><?= $value ?></textarea>
views/admin/fields/index.php DELETED
@@ -1,5 +0,0 @@
1
- <?php
2
-
3
- http_response_code(404);
4
-
5
- die();
 
 
 
 
 
views/admin/fields/number.php ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if (! defined('ABSPATH')) {
3
+ http_response_code(404);
4
+ die();
5
+ }
6
+ ?>
7
+
8
+ <input type="number" name="<?= $name ?>" value="<?= $value ?>"
9
+ <?= isset($min) ? "min=\"{$min}\"" : '' ?>
10
+ <?= isset($max) ? "max=\"{$max}\"" : '' ?>
11
+ <?= isset($step) ? "step=\"{$step}\"" : '' ?>
12
+ style="width: 5rem;">
views/admin/fields/radio.php CHANGED
@@ -1,7 +1,13 @@
 
 
 
 
 
 
 
1
  <label>
2
- <input type="radio" name="<?php echo $name; ?>" value="<?php echo $value; ?>"
3
- <?php echo isset($id) ? ('id="'.$id.'"') : ''; ?>
4
- <?php echo $checked ? 'checked="checked"' : ''; ?>>
5
 
6
- <?php echo isset($label) ? $label : ''; ?>
7
  </label>
1
+ <?php
2
+ if (! defined('ABSPATH')) {
3
+ http_response_code(404);
4
+ die();
5
+ }
6
+ ?>
7
+
8
  <label>
9
+ <input type="radio" name="<?= $name ?>" value="<?= $value ?>"
10
+ <?= $checked ? 'checked="checked"' : '' ?>>
 
11
 
12
+ <?= $label ?>
13
  </label>
views/admin/fields/select.php CHANGED
@@ -1,11 +1,17 @@
1
- <select name="<?php echo $name; ?><?php echo (isset($multiple) && $multiple) ? '[]' : ''; ?>"
 
 
 
 
 
 
 
2
  style="min-width: 15rem; padding: .5rem;"
3
- <?php echo isset($id) ? ('id="'.$id.'"') : ''; ?>
4
- <?php echo isset($multiple) ? ('multiple="multiple"') : ''; ?>>
5
  <?php foreach ($options as $option) : ?>
6
- <option value="<?php echo $option['value']; ?>"
7
- <?php echo $option['selected'] ? 'selected="selected"' : ''; ?>>
8
- <?php echo $option['label']; ?>
9
  </option>
10
  <?php endforeach; ?>
11
  </select>
1
+ <?php
2
+ if (! defined('ABSPATH')) {
3
+ http_response_code(404);
4
+ die();
5
+ }
6
+ ?>
7
+
8
+ <select name="<?= $name ?><?= (isset($multiple) && $multiple) ? '[]' : '' ?>"
9
  style="min-width: 15rem; padding: .5rem;"
10
+ <?= (isset($multiple) && $multiple) ? 'multiple="multiple"' : '' ?>>
 
11
  <?php foreach ($options as $option) : ?>
12
+ <option value="<?= $option['value'] ?>"
13
+ <?= $option['selected'] ? 'selected="selected"' : '' ?>>
14
+ <?= $option['label'] ?>
15
  </option>
16
  <?php endforeach; ?>
17
  </select>
views/admin/fields/text.php CHANGED
@@ -1,5 +1,9 @@
1
- <input type="<?php echo isset($type) ? $type : 'text' ?>"
2
- id="<?php echo $id; ?>"
3
- name="<?php echo $name; ?>"
4
- value="<?php echo $value; ?>"
5
- style="min-width: 160px; min-height: 28px;">
 
 
 
 
1
+ <?php
2
+ if (! defined('ABSPATH')) {
3
+ http_response_code(404);
4
+ die();
5
+ }
6
+ ?>
7
+
8
+ <input name="<?= $name ?>" value="<?= $value ?>"
9
+ style="width: 15rem;">
views/admin/fields/textarea.php ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if (! defined('ABSPATH')) {
3
+ http_response_code(404);
4
+ die();
5
+ }
6
+ ?>
7
+
8
+ <textarea rows="15" cols="50" name="<?= $name ?>"
9
+ style="padding: .5rem;"><?= $value ?></textarea>
views/admin/index.php CHANGED
@@ -9,21 +9,28 @@
9
  <?php settings_errors(); ?>
10
 
11
  <h1>
12
- <?php echo $label; ?>
13
- <small style="margin-left: .5rem; font-size: 80%; font-family: monospace; letter-spacing: -2px; color: gray;">
14
- <?php echo $version; ?>
 
 
 
 
 
15
  </small>
16
  </h1>
17
 
18
- <?php
19
- ob_start();
20
- include KKSR_PATH_VIEWS.'admin/social.php';
21
- echo ob_get_clean();
22
- ?>
23
-
24
  <h2 class="nav-tab-wrapper">
25
- <?php do_action('kksr_setting_tabs'); ?>
 
 
 
 
 
 
 
 
26
  </h2>
27
 
28
- <?php do_action('kksr_setting_contents'); ?>
29
  </div>
9
  <?php settings_errors(); ?>
10
 
11
  <h1>
12
+ <?= $label; ?>
13
+ <small style="
14
+ color: gray;
15
+ font-size: 80%;
16
+ margin-left: .5rem;
17
+ letter-spacing: -2px;
18
+ font-family: monospace;">
19
+ <?= $version; ?>
20
  </small>
21
  </h1>
22
 
 
 
 
 
 
 
23
  <h2 class="nav-tab-wrapper">
24
+ <?php foreach ($tabs as $tab => $label) : ?>
25
+ <a class="nav-tab <?= $tab === $active ? 'nav-tab-active' : ''; ?>"
26
+ href="<?= admin_url('admin.php?page='.$_GET['page'].'&tab='.$tab); ?>">
27
+ <?= $label; ?>
28
+ </a>
29
+ <?php endforeach; ?>
30
+ <div style="float: left; margin-left: 10px;">
31
+ <?= \Bhittani\StarRating\view('admin.social') ?>
32
+ </div>
33
  </h2>
34
 
35
+ <?= $content ?>
36
  </div>
views/admin/social.php CHANGED
@@ -1,9 +1,21 @@
1
- <div style="display: flex; height: 20px; margin: 5px; 0;">
 
 
 
 
 
 
 
2
  <!-- Github -->
3
  <script async defer src="https://buttons.github.io/buttons.js"></script>
4
 
5
  <div style="float: left; margin-right: 5px;">
6
- <a class="github-button" href="https://github.com/kamalkhan/kk-star-ratings" data-icon="octicon-star" data-show-count="true" aria-label="Star kamalkhan/kk-star-ratings on GitHub">
 
 
 
 
 
7
  kk Star Ratings
8
  </a>
9
  </div>
@@ -29,7 +41,11 @@
29
  </script>
30
 
31
  <div style="float: left; margin-right: 5px;">
32
- <a class="twitter-share-button" href="https://github.com/kamalkhan/kk-star-ratings">
 
 
 
 
33
  Tweet
34
  </a>
35
  </div>
@@ -48,5 +64,13 @@
48
 
49
  <div id="fb-root" style="float: left; margin-right: 5px;"></div>
50
 
51
- <div class="fb-like" data-href="https://github.com/kamalkhan/kk-star-ratings" data-width="167" data-layout="button_count" data-action="like" data-show-faces="true" data-share="true"></div>
 
 
 
 
 
 
 
 
52
  </div>
1
+ <?php
2
+ if (! defined('ABSPATH')) {
3
+ http_response_code(404);
4
+ die();
5
+ }
6
+ ?>
7
+
8
+ <div style="display: flex;">
9
  <!-- Github -->
10
  <script async defer src="https://buttons.github.io/buttons.js"></script>
11
 
12
  <div style="float: left; margin-right: 5px;">
13
+ <a class="github-button"
14
+ href="https://github.com/kamalkhan/kk-star-ratings"
15
+ data-icon="octicon-star"
16
+ data-show-count="true"
17
+ data-size="large"
18
+ aria-label="Star kamalkhan/kk-star-ratings on GitHub">
19
  kk Star Ratings
20
  </a>
21
  </div>
41
  </script>
42
 
43
  <div style="float: left; margin-right: 5px;">
44
+ <a class="twitter-share-button"
45
+ data-size="large"
46
+ data-text="kk Star Ratings is awesome."
47
+ data-url="https://github.com/kamalkhan/kk-star-ratings"
48
+ href="https://twitter.com/intent/tweet">
49
  Tweet
50
  </a>
51
  </div>
64
 
65
  <div id="fb-root" style="float: left; margin-right: 5px;"></div>
66
 
67
+ <div class="fb-like"
68
+ data-href="https://github.com/kamalkhan/kk-star-ratings"
69
+ data-width="167"
70
+ data-layout="button_count"
71
+ data-action="like"
72
+ data-size="large"
73
+ data-show-faces="true"
74
+ data-share="true">
75
+ </div>
76
  </div>
views/admin/tabs.php DELETED
@@ -1,6 +0,0 @@
1
- <?php foreach ($tabs as $tab => $label) : ?>
2
- <a class="nav-tab <?php echo $tab == $active ? 'nav-tab-active' : ''; ?>"
3
- href="<?php echo admin_url('admin.php?page=' . KKSR_SLUG . '&tab=' . $tab); ?>">
4
- <?php echo $label; ?>
5
- </a>
6
- <?php endforeach; ?>
 
 
 
 
 
 
views/admin/tabs/index.php DELETED
@@ -1,7 +0,0 @@
1
- <?php
2
-
3
- // You can create tab specific files in this directory to override the default tab view.
4
-
5
- http_response_code(404);
6
-
7
- die();
 
 
 
 
 
 
 
views/inactive-star.php ADDED
@@ -0,0 +1 @@
 
1
+ <?= \Bhittani\StarRating\view('star') ?>
views/inactive-stars.php ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
1
+ <div class="kksr-stars-inactive">
2
+ <?php for ($i = 1; $i <= $best; $i++) : ?>
3
+ <div class="kksr-star" data-star="<?= $i ?>">
4
+ <?= \Bhittani\StarRating\view('inactive-star') ?>
5
+ </div>
6
+ <?php endfor; ?>
7
+ </div>
views/index.php CHANGED
@@ -1,5 +1,6 @@
1
  <?php
2
 
3
- http_response_code(404);
4
-
5
- die();
 
1
  <?php
2
 
3
+ if (! defined('ABSPATH')) {
4
+ http_response_code(404);
5
+ die();
6
+ }
views/legend.php CHANGED
@@ -1,4 +1,15 @@
1
- <div class="kksr-legend" style="<?php echo $count ? '' : 'display: none; ' ?>line-height:<?php echo $size; ?>px;font-size:<?php echo $size/1.5; ?>px">
2
- <div class="kksr-legend-score"><?php echo apply_filters('kksr_score', $score); ?></div>
3
- <div class="kksr-legend-meta"><?php echo apply_filters('kksr_count', $count); ?></div>
 
 
 
 
 
 
 
 
 
 
 
4
  </div>
1
+ <div class="kksr-legend">
2
+ <?php if ($count) : ?>
3
+ <strong class="kksr-score"><?= $score ?></strong>
4
+ <span class="kksr-muted">/</span>
5
+ <strong><?= $best ?></strong>
6
+ <span class="kksr-muted">(</span>
7
+ <strong class="kksr-count"><?= $count ?></strong>
8
+ <span class="kksr-muted">
9
+ <?= _n('vote', 'votes', $count, 'kk-star-ratings') ?>
10
+ </span>
11
+ <span class="kksr-muted">)</span>
12
+ <?php else : ?>
13
+ <span class="kksr-muted"><?= $greet ?></span>
14
+ <?php endif; ?>
15
  </div>
views/markup.php CHANGED
@@ -1,27 +1,7 @@
1
- <div data-id="<?php echo $id; ?>" data-score="<?php echo $score; ?>" data-count="<?php echo $count; ?>"
2
- style="display:none;"
3
- 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' : ''; ?>">
4
- <?php
5
- ob_start();
6
- include KKSR_PATH_VIEWS.'legend.php';
7
- echo apply_filters('kksr_legend', ob_get_clean(), $score, $count);
8
- ?>
9
- <div class="kksr-stars">
10
- <div class="kksr-inactive-stars">
11
- <?php
12
- ob_start();
13
- $active = false;
14
- include KKSR_PATH_VIEWS.'stars.php';
15
- echo apply_filters('kksr_stars', apply_filters('kksr_stars_inactive', ob_get_clean(), $size, $stars), $size, $stars);
16
- ?>
17
- </div>
18
- <div class="kksr-active-stars" style="width: <?php echo apply_filters('kksr_width', $width); ?>px;">
19
- <?php
20
- ob_start();
21
- $active = true;
22
- include KKSR_PATH_VIEWS.'stars.php';
23
- echo apply_filters('kksr_stars', apply_filters('kksr_stars_active', ob_get_clean(), $size, $stars), $size, $stars);
24
- ?>
25
- </div>
26
- </div>
27
  </div>
1
+ <div style="display: none;"
2
+ class="kk-star-ratings <?= $valign ? ("kksr-valign-{$valign}") : '' ?> <?= $align ? ("kksr-align-{$align}") : '' ?> <?= $disabled ? 'kksr-disabled' : '' ?>"
3
+ data-id="<?= $id ?>"
4
+ data-slug="<?= $slug ?>">
5
+ <?= \Bhittani\StarRating\view('stars') ?>
6
+ <?= \Bhittani\StarRating\view('legend') ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  </div>
views/metabox/content.php ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if (! defined('ABSPATH')) {
3
+ http_response_code(404);
4
+ die();
5
+ }
6
+ ?>
7
+
8
+ <div class='components-base-control__field'>
9
+ <div style="margin-top: 1rem;">
10
+ <label class="components-base-control__label" style="margin-top: .75rem; margin-bottom: .25rem;">
11
+ <input type="checkbox" name="<?= $resetFieldName; ?>" value="1">
12
+ <?php _e('Reset Ratings', 'kk-star-ratings'); ?>
13
+ </label>
14
+ </div>
15
+ </div>
16
+
17
+ <div class='components-base-control__field'>
18
+ <label class="components-base-control__label" style="margin-top: .75rem; margin-bottom: .25rem;">
19
+ <strong><?php _e('Status', 'kk-star-ratings'); ?></strong>
20
+ </label>
21
+
22
+ <div style="margin-top: 1rem;">
23
+ <label class="components-base-control__label" style="margin-top: .5rem; margin-bottom: .25rem;">
24
+ <input type="radio" name="<?= $statusFieldName; ?>" value="" <?php checked($status, ''); ?>>
25
+ <?php _e('Auto', 'kk-star-ratings'); ?>
26
+ </label>
27
+ <label class="components-base-control__label" style="margin-top: .5rem; margin-bottom: .25rem;">
28
+ <input type="radio" name="<?= $statusFieldName; ?>" value="enable" <?php checked($status, 'enable'); ?>>
29
+ <?php _e('Enable', 'kk-star-ratings'); ?>
30
+ </label>
31
+ <label class="components-base-control__label" style="margin-top: .5rem; margin-bottom: .25rem;">
32
+ <input type="radio" name="<?= $statusFieldName; ?>" value="disable" <?php checked($status, 'disable'); ?>>
33
+ <?php _e('Disable', 'kk-star-ratings'); ?>
34
+ </label>
35
+ </div>
36
+ </div>
views/metabox/index.php CHANGED
@@ -6,35 +6,5 @@
6
  ?>
7
 
8
  <div class="components-base-control">
9
- <div class='components-base-control__field'>
10
- <div style="margin-top: 1rem;">
11
- <label class="components-base-control__label" style="margin-top: .75rem; margin-bottom: .25rem;">
12
- <input type="checkbox" name="<?php echo $resetFieldName; ?>" value="1">
13
- <?php _e('Reset Ratings', 'kk-star-ratings'); ?>
14
- </label>
15
- </div>
16
- </div>
17
-
18
- <div class='components-base-control__field'>
19
- <label class="components-base-control__label" style="margin-top: .75rem; margin-bottom: .25rem;">
20
- <strong><?php _e('Status', 'kk-star-ratings'); ?></strong>
21
- </label>
22
-
23
- <div style="margin-top: 1rem;">
24
- <label class="components-base-control__label" style="margin-top: .5rem; margin-bottom: .25rem;">
25
- <input type="radio" name="<?php echo $statusFieldName; ?>" value="" <?php checked($status, ''); ?>>
26
- <?php _e('Auto', 'kk-star-ratings'); ?>
27
- </label>
28
- <label class="components-base-control__label" style="margin-top: .5rem; margin-bottom: .25rem;">
29
- <input type="radio" name="<?php echo $statusFieldName; ?>" value="enable" <?php checked($status, 'enable'); ?>>
30
- <?php _e('Enable', 'kk-star-ratings'); ?>
31
- </label>
32
- <label class="components-base-control__label" style="margin-top: .5rem; margin-bottom: .25rem;">
33
- <input type="radio" name="<?php echo $statusFieldName; ?>" value="disable" <?php checked($status, 'disable'); ?>>
34
- <?php _e('Disable', 'kk-star-ratings'); ?>
35
- </label>
36
- </div>
37
-
38
- <?php do_action('kksr_metabox'); ?>
39
- </div>
40
  </div>
6
  ?>
7
 
8
  <div class="components-base-control">
9
+ <?= $content ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  </div>
views/star.php CHANGED
@@ -1,19 +1 @@
1
- <?php
2
- /*
3
- <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50">
4
- <path d="M25 1l6 17h18L35 29l5 17-15-10-15 10 5-17L1 18h18z"/>
5
- </svg>
6
- */
7
- ?>
8
-
9
- <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
10
- <path d="M12 .587l3.668 7.568L24 9.306l-6.064 5.828 1.48 8.279L12 19.446l-7.417 3.967 1.481-8.279L0 9.306l8.332-1.151z"/>
11
- </svg>
12
-
13
- <?php
14
- /*
15
- <svg xmlns="http://www.w3.org/2000/svg" stroke-width="2" viewBox="0 0 100 100">
16
- <path d="M26.439 95.601c-5.608 2.949-9.286.276-8.216-5.968l4.5-26.237-19.061-18.58C-.875 40.393.53 36.07 6.799 35.159l26.343-3.829L44.923 7.46c2.804-5.682 7.35-5.682 10.154 0l11.78 23.87L93.2 35.159c6.27.911 7.674 5.234 3.138 9.657L77.277 63.397l4.501 26.237c1.07 6.244-2.608 8.916-8.216 5.968L50 83.215 26.439 95.601z"/>
17
- </svg>
18
- */
19
- ?>
1
+ <div class="kksr-icon" style="width: <?= $size ?>px; height: <?= $size ?>px;"></div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
views/stars.php CHANGED
@@ -1,11 +1,4 @@
1
- <?php
2
- ob_start();
3
- include KKSR_PATH_VIEWS.'star.php';
4
- $star = apply_filters('kksr_star', apply_filters('kksr_star_'.($active ? 'active' : 'inactive'), ob_get_clean(), $size), $size);
5
-
6
- for ($i = 1; $i <= $stars; $i++) :
7
- ?>
8
- <div data-star="<?php echo $i; ?>" class="kksr-star">
9
- <?php echo $star; ?>
10
- </div>
11
- <?php endfor; ?>
1
+ <div class="kksr-stars">
2
+ <?= \Bhittani\StarRating\view('inactive-stars') ?>
3
+ <?= \Bhittani\StarRating\view('active-stars') ?>
4
+ </div>
 
 
 
 
 
 
 
views/structured-data.php DELETED
@@ -1,14 +0,0 @@
1
- <script type="application/ld+json">
2
- {
3
- "@context": "<?php echo $context; ?>",
4
- "@type": "<?php echo $type; ?>",
5
- "name": "<?php echo $name; ?>",
6
- "aggregateRating": {
7
- "@type": "AggregateRating",
8
- "ratingValue": "<?php echo $score; ?>",
9
- "ratingCount": "<?php echo $count; ?>",
10
- "bestRating": "<?php echo $stars; ?>",
11
- "worstRating": "1"
12
- }
13
- }
14
- </script>