kk Star Ratings - Version 5.2.1

Version Description

Download this release

Release Info

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

Code changes from version 5.2.0 to 5.2.1

CHANGELOG.md CHANGED
@@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/)
6
  and this project adheres to [Semantic Versioning](http://semver.org/).
7
 
 
 
 
 
 
 
 
 
 
 
8
  ## [5.1.4] - 2021-11-01
9
 
10
  ### Updated
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/)
6
  and this project adheres to [Semantic Versioning](http://semver.org/).
7
 
8
+ ## [5.2.1] - 2021-11-06
9
+
10
+ ### Fixed
11
+ - Unique voting issue due to previous changes is now fixed.
12
+
13
+ ## [5.2.0] - 2021-11-02
14
+
15
+ ### Updated
16
+ - Deprecated the use of migrations in favor of migrating posts when accessed.
17
+
18
  ## [5.1.4] - 2021-11-01
19
 
20
  ### Updated
index.php CHANGED
@@ -10,7 +10,7 @@
10
  * Author URI: http://bhittani.com
11
  * Text Domain: kk-star-ratings
12
  * Domain Path: /languages
13
- * Version: 5.2.0
14
  * License: GPLv2 or later
15
  */
16
 
10
  * Author URI: http://bhittani.com
11
  * Text Domain: kk-star-ratings
12
  * Domain Path: /languages
13
+ * Version: 5.2.1
14
  * License: GPLv2 or later
15
  */
16
 
languages/kk-star-ratings.pot CHANGED
@@ -2,14 +2,14 @@
2
  # This file is distributed under the same license as the kk Star Ratings plugin.
3
  msgid ""
4
  msgstr ""
5
- "Project-Id-Version: kk Star Ratings 5.1.4\n"
6
  "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/kk-star-ratings\n"
7
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
8
  "Language-Team: LANGUAGE <LL@li.org>\n"
9
  "MIME-Version: 1.0\n"
10
  "Content-Type: text/plain; charset=UTF-8\n"
11
  "Content-Transfer-Encoding: 8bit\n"
12
- "POT-Creation-Date: 2021-11-01T17:48:38+00:00\n"
13
  "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
14
  "X-Generator: WP-CLI 2.4.0\n"
15
  "X-Domain: kk-star-ratings\n"
@@ -61,10 +61,6 @@ msgstr ""
61
  msgid "The ratings are readonly."
62
  msgstr ""
63
 
64
- #: src/core/filters/validate.php:35
65
- msgid "Under maintenance."
66
- msgstr ""
67
-
68
  #: src/core/filters/validate.php:39
69
  msgid "Not allowed at the moment."
70
  msgstr ""
@@ -340,10 +336,6 @@ msgctxt "label"
340
  msgid "Disable"
341
  msgstr ""
342
 
343
- #: src/core/wp/actions/admin_notices.php:26
344
- msgid "%s has pending migrations. These migrations are running in the background and the ratings will remain disabled until the migrations have finished."
345
- msgstr ""
346
-
347
  #: src/core/wp/actions/wp_ajax_kk-star-ratings.php:30
348
  #: src/core/wp/actions/wp_ajax_kksr-migrations.php:28
349
  msgid "This action is forbidden."
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 5.2.1\n"
6
  "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/kk-star-ratings\n"
7
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
8
  "Language-Team: LANGUAGE <LL@li.org>\n"
9
  "MIME-Version: 1.0\n"
10
  "Content-Type: text/plain; charset=UTF-8\n"
11
  "Content-Transfer-Encoding: 8bit\n"
12
+ "POT-Creation-Date: 2021-11-06T12:00:03+00:00\n"
13
  "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
14
  "X-Generator: WP-CLI 2.4.0\n"
15
  "X-Domain: kk-star-ratings\n"
61
  msgid "The ratings are readonly."
62
  msgstr ""
63
 
 
 
 
 
64
  #: src/core/filters/validate.php:39
65
  msgid "Not allowed at the moment."
66
  msgstr ""
336
  msgid "Disable"
337
  msgstr ""
338
 
 
 
 
 
339
  #: src/core/wp/actions/wp_ajax_kk-star-ratings.php:30
340
  #: src/core/wp/actions/wp_ajax_kksr-migrations.php:28
341
  msgid "This action is forbidden."
readme.txt CHANGED
@@ -6,7 +6,7 @@ Tags: star ratings, votings, rate posts, ajax ratings, infinite stars, unlimited
6
  Requires at least: 5.0
7
  Requires PHP: 7.2
8
  Tested up to: 5.8.1
9
- Stable tag: 5.2.0
10
  License: GPLv2 or later
11
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
12
 
6
  Requires at least: 5.0
7
  Requires PHP: 7.2
8
  Tested up to: 5.8.1
9
+ Stable tag: 5.2.1
10
  License: GPLv2 or later
11
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
12
 
src/classes/Migration.php CHANGED
@@ -12,11 +12,20 @@
12
  namespace Bhittani\StarRating\classes;
13
 
14
  use function Bhittani\StarRating\functions\autoload_class;
 
15
 
16
  autoload_class(Stack::class);
17
 
18
  class Migration extends Stack
19
  {
 
 
 
 
 
 
 
 
20
  /** @var callable */
21
  protected $cron;
22
 
@@ -30,7 +39,7 @@ class Migration extends Stack
30
  public function isBusy(): bool
31
  {
32
  return ! $this->isEmpty()
33
- && $this->bottom()['status'] == 'working';
34
  }
35
 
36
  public function isPending(): bool
@@ -38,19 +47,56 @@ class Migration extends Stack
38
  return ! ($this->isEmpty() || $this->isBusy());
39
  }
40
 
41
- public function create(string $tag, $payload): self
42
  {
43
- $this->push([
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  'payload' => $payload,
45
  'status' => 'pending',
46
  'tag' => $tag,
47
  'times' => 0,
48
  'timestamp' => time(),
 
49
  ]);
50
 
51
  return $this;
52
  }
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  public function replace($migration): self
55
  {
56
  $this->shift();
@@ -111,7 +157,7 @@ class Migration extends Stack
111
  * 8 -> The migration is already being processed.
112
  * 16 -> The migration is not current.
113
  */
114
- public function migrate(string $tag, callable $fn): int
115
  {
116
  if ($this->isEmpty()) {
117
  return 4;
@@ -119,24 +165,23 @@ class Migration extends Stack
119
 
120
  $migration = $this->bottom();
121
 
122
- if ($migration['tag'] !== $tag) {
 
 
123
  return 16;
124
  }
125
 
126
- if ($migration['status'] == 'working') {
127
- $seconds = time() - $migration['timestamp'];
128
 
129
- if ($seconds <= 60) {
130
  return 8;
131
  }
132
  }
133
 
134
- $migration['status'] = 'working';
135
- $migration['timestamp'] = time();
136
-
137
- $this->replace($migration)->persist();
138
 
139
- $value = $fn($migration['payload']);
140
 
141
  if (is_null($value)) {
142
  $this->remove()->persist();
@@ -144,12 +189,7 @@ class Migration extends Stack
144
  return 1;
145
  }
146
 
147
- $migration['payload'] = $value;
148
- $migration['status'] = 'pending';
149
- $migration['timestamp'] = time();
150
- $migration['times'] = $migration['times'] + 1;
151
-
152
- $this->replace($migration)->persist();
153
 
154
  return 2;
155
  }
12
  namespace Bhittani\StarRating\classes;
13
 
14
  use function Bhittani\StarRating\functions\autoload_class;
15
+ use InvalidArgumentException;
16
 
17
  autoload_class(Stack::class);
18
 
19
  class Migration extends Stack
20
  {
21
+ /**
22
+ * @see https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
23
+ * @see https://regex101.com/r/vkijKf/1/
24
+ *
25
+ * @var string
26
+ */
27
+ public const VERSION_REGEX = '/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/';
28
+
29
  /** @var callable */
30
  protected $cron;
31
 
39
  public function isBusy(): bool
40
  {
41
  return ! $this->isEmpty()
42
+ && $this->bottom()->status == 'working';
43
  }
44
 
45
  public function isPending(): bool
47
  return ! ($this->isEmpty() || $this->isBusy());
48
  }
49
 
50
+ public function flush(): self
51
  {
52
+ while (! $this->isEmpty()) {
53
+ $this->pop();
54
+ }
55
+
56
+ return $this;
57
+ }
58
+
59
+ public function create(string $tag, string $version, $payload): self
60
+ {
61
+ if (! preg_match(static::VERSION_REGEX, $version)) {
62
+ throw new InvalidArgumentException("Can not create a migration. The string '{$version}' is not a valid semver.");
63
+ }
64
+
65
+ $this->push((object) [
66
  'payload' => $payload,
67
  'status' => 'pending',
68
  'tag' => $tag,
69
  'times' => 0,
70
  'timestamp' => time(),
71
+ 'version' => $version,
72
  ]);
73
 
74
  return $this;
75
  }
76
 
77
+ /** @param bool|null $touch */
78
+ public function patch(string $status, $payload = null, $touch = null): self
79
+ {
80
+ $migration = $this->bottom();
81
+
82
+ $migration->status = $status;
83
+ $migration->timestamp = time();
84
+
85
+ if (! is_null($payload)) {
86
+ $migration->payload = $payload;
87
+
88
+ if (is_null($touch)) {
89
+ $touch = true;
90
+ }
91
+ }
92
+
93
+ if ($touch) {
94
+ ++$migration->times;
95
+ }
96
+
97
+ return $this->replace($migration);
98
+ }
99
+
100
  public function replace($migration): self
101
  {
102
  $this->shift();
157
  * 8 -> The migration is already being processed.
158
  * 16 -> The migration is not current.
159
  */
160
+ public function migrate(string $tag, string $version, callable $fn): int
161
  {
162
  if ($this->isEmpty()) {
163
  return 4;
165
 
166
  $migration = $this->bottom();
167
 
168
+ if ($migration->tag !== $tag
169
+ || $migration->version !== $version
170
+ ) {
171
  return 16;
172
  }
173
 
174
+ if ($migration->status == 'working') {
175
+ $seconds = time() - $migration->timestamp;
176
 
177
+ if ($seconds <= (60 * 5)) {
178
  return 8;
179
  }
180
  }
181
 
182
+ $this->patch('working')->persist();
 
 
 
183
 
184
+ $value = $fn($migration->payload);
185
 
186
  if (is_null($value)) {
187
  $this->remove()->persist();
189
  return 1;
190
  }
191
 
192
+ $this->patch('pending', $value)->persist();
 
 
 
 
 
193
 
194
  return 2;
195
  }
src/classes/Stack.php CHANGED
@@ -27,12 +27,12 @@ class Stack extends SplStack
27
  $this->storeKey = $storeKey;
28
 
29
  if ($serialized = $store($storeKey)) {
30
- $this->unserialize(base64_decode($serialized));
31
  }
32
  }
33
 
34
  public function persist(): void
35
  {
36
- ($this->store)([$this->storeKey => base64_encode($this->serialize())]);
37
  }
38
  }
27
  $this->storeKey = $storeKey;
28
 
29
  if ($serialized = $store($storeKey)) {
30
+ $this->unserialize($serialized);
31
  }
32
  }
33
 
34
  public function persist(): void
35
  {
36
+ ($this->store)([$this->storeKey => $this->serialize()]);
37
  }
38
  }
src/core/actions/downgrade.php CHANGED
@@ -23,10 +23,8 @@ function downgrade(string $version, string $previous): void
23
  $migrations = migrations();
24
 
25
  while (! $migrations->isEmpty()
26
- // Pop until version matches.
27
- && (substr($tag = $migrations->top()['tag'], 0, 1) === 'v')
28
- && ($mtag = substr(explode('/', $tag, 2)[0], 1))
29
- && version_compare($mtag, $version, '!=')) {
30
  $migrations->pop();
31
  }
32
 
23
  $migrations = migrations();
24
 
25
  while (! $migrations->isEmpty()
26
+ && version_compare($migrations->top()->version, $version, '!=')
27
+ ) {
 
 
28
  $migrations->pop();
29
  }
30
 
src/core/actions/init.php CHANGED
@@ -19,6 +19,7 @@ if (! defined('KK_STAR_RATINGS')) {
19
  exit();
20
  }
21
 
 
22
  function init(array $config): void
23
  {
24
  // if (migrations()->isPending()) {
19
  exit();
20
  }
21
 
22
+ /** At this point, the plugin is ready (and activated). */
23
  function init(array $config): void
24
  {
25
  // if (migrations()->isPending()) {
src/core/actions/upgrade.php CHANGED
@@ -12,6 +12,8 @@
12
  namespace Bhittani\StarRating\core\actions;
13
 
14
  use function Bhittani\StarRating\core\functions\migrations;
 
 
15
  use function Bhittani\StarRating\core\functions\upgrade_options;
16
 
17
  if (! defined('KK_STAR_RATINGS')) {
@@ -25,52 +27,27 @@ function upgrade(string $version, string $previous): void
25
  upgrade_options();
26
  }
27
 
28
- if (version_compare($previous, '5.1.4', '<')) {
29
- $migrations = migrations();
30
-
31
- if (! $migrations->isEmpty()
32
- && ($migration = $migrations->bottom())
33
- && $migration['tag'] == 'v5.1.0/posts'
34
- ) {
35
- $migration['payload']['paged'] = 1;
36
- $migration['payload']['posts_per_page'] = 5;
37
- $migrations->replace($migration)->persist();
38
- }
39
  }
40
 
41
  $migrations = migrations();
42
 
43
- $pendingMigrations = [];
44
-
45
- foreach (kksr('core.migrations') as $tag => $options) {
46
- $mtag = (string) substr(explode('/', $tag, 2)[0], 1);
47
-
48
- if (version_compare($mtag, $previous, '>')
49
- && version_compare($mtag, $version, '<=')
50
- && ($migrations->isEmpty()
51
- || (
52
- ($ttag = (string) substr(explode('/', $migrations->top()['tag'], 2)[0], 1))
53
- && version_compare($mtag, $ttag, '>')
54
- )
55
  )
56
  ) {
57
- $pendingMigrations[] = compact('tag', 'options') + ['version' => $mtag];
 
 
 
 
58
  }
59
  }
60
 
61
- // Will already be sorted, but lets be damn sure!
62
- usort($pendingMigrations, function ($a, $b) {
63
- return version_compare($a['version'], $b['version']);
64
- });
65
-
66
- foreach ($pendingMigrations as $pendingMigration) {
67
- [$_, $payloadFn] = $pendingMigration['options']();
68
-
69
- $migrations->create(
70
- $pendingMigration['tag'],
71
- $payloadFn($version, $previous)
72
- );
73
- }
74
-
75
  $migrations->persist();
76
  }
12
  namespace Bhittani\StarRating\core\actions;
13
 
14
  use function Bhittani\StarRating\core\functions\migrations;
15
+ use function Bhittani\StarRating\core\functions\migrators;
16
+ use function Bhittani\StarRating\core\functions\option;
17
  use function Bhittani\StarRating\core\functions\upgrade_options;
18
 
19
  if (! defined('KK_STAR_RATINGS')) {
27
  upgrade_options();
28
  }
29
 
30
+ if (version_compare($previous, '5.2.1', '<')) {
31
+ option(['migrations' => '']);
 
 
 
 
 
 
 
 
 
32
  }
33
 
34
  $migrations = migrations();
35
 
36
+ foreach (migrators() as $migrator) {
37
+ if (version_compare($migrator->semver, $previous, '>')
38
+ && version_compare($migrator->semver, $version, '<=')
39
+ && (
40
+ $migrations->isEmpty()
41
+ || version_compare($migrator->semver, $migrations->top()->version, '>')
 
 
 
 
 
 
42
  )
43
  ) {
44
+ $migrations->create(
45
+ $migrator->tag,
46
+ $migrator->semver,
47
+ $migrator->payload($version, $previous)
48
+ );
49
  }
50
  }
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  $migrations->persist();
53
  }
src/core/config.php CHANGED
@@ -31,7 +31,7 @@ return [
31
  'actions' => autoload($ns.'actions', $path.'actions'),
32
  'filters' => autoload($ns.'filters', $path.'filters'),
33
  'functions' => autoload($ns.'functions', $path.'functions'),
34
- 'migrations' => autoload($ns.'migrations', $path.'migrations'),
35
  'wp' => [
36
  'actions' => autoload($ns.'wp\actions', $path.'wp/actions'),
37
  'filters' => autoload($ns.'wp\filters', $path.'wp/filters'),
@@ -48,6 +48,8 @@ return [
48
  'status_*' => '',
49
  ],
50
  'options' => [
 
 
51
  // General
52
  'enable' => true,
53
  'exclude_categories' => [],
31
  'actions' => autoload($ns.'actions', $path.'actions'),
32
  'filters' => autoload($ns.'filters', $path.'filters'),
33
  'functions' => autoload($ns.'functions', $path.'functions'),
34
+ 'migrators' => autoload($ns.'migrators', $path.'migrators'),
35
  'wp' => [
36
  'actions' => autoload($ns.'wp\actions', $path.'wp/actions'),
37
  'filters' => autoload($ns.'wp\filters', $path.'wp/filters'),
48
  'status_*' => '',
49
  ],
50
  'options' => [
51
+ // Internal
52
+ 'activated' => false,
53
  // General
54
  'enable' => true,
55
  'exclude_categories' => [],
src/core/functions/migrate.php CHANGED
@@ -18,7 +18,6 @@ if (! defined('KK_STAR_RATINGS')) {
18
 
19
  /**
20
  * @return int
21
- * 0 -> Could not process any migration.
22
  * 1 -> The migration was processed and completed.
23
  * 2 -> The migration was processed but still pending.
24
  * 4 -> There are no migrations available.
@@ -27,18 +26,23 @@ if (! defined('KK_STAR_RATINGS')) {
27
  */
28
  function migrate(): int
29
  {
30
- if (migrations()->isEmpty()) {
31
- return 4;
32
- }
33
 
34
- foreach (kksr('core.migrations') as $tag => $options) {
35
- [$fn] = $options();
36
- $code = migrations()->migrate($tag, $fn);
 
 
 
 
37
 
38
- if (in_array($code, [1, 2, 4, 8])) {
39
- return $code;
 
40
  }
 
 
41
  }
42
 
43
- return isset($code) ? $code : 0;
44
  }
18
 
19
  /**
20
  * @return int
 
21
  * 1 -> The migration was processed and completed.
22
  * 2 -> The migration was processed but still pending.
23
  * 4 -> There are no migrations available.
26
  */
27
  function migrate(): int
28
  {
29
+ $migrations = migrations();
 
 
30
 
31
+ while (! $migrations->isEmpty()) {
32
+ foreach (migrators() as $migrator) {
33
+ $code = $migrations->migrate(
34
+ $migrator->tag,
35
+ $migrator->semver,
36
+ $migrator->handler
37
+ );
38
 
39
+ if (in_array($code, [1, 2, 4, 8])) {
40
+ return $code;
41
+ }
42
  }
43
+
44
+ $migrations->remove()->persist();
45
  }
46
 
47
+ return 4;
48
  }
src/core/functions/migrators.php ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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\core\functions;
13
+
14
+ use function Bhittani\StarRating\functions\migrators as base_migrators;
15
+
16
+ if (! defined('KK_STAR_RATINGS')) {
17
+ http_response_code(404);
18
+ exit();
19
+ }
20
+
21
+ function migrators(): array
22
+ {
23
+ static $migrators;
24
+
25
+ return is_null($migrators)
26
+ ? ($migrators = base_migrators(kksr('core.migrators')))
27
+ : $migrators;
28
+ }
src/core/index.php CHANGED
@@ -19,5 +19,9 @@ kksr(['core' => require __DIR__.'/config.php']);
19
  require_once __DIR__.'/hooks.php';
20
  require_once __DIR__.'/hydrate.php';
21
 
22
- register_activation_hook(KK_STAR_RATINGS, kksr('core.wp.functions.activate'));
23
  register_deactivation_hook(KK_STAR_RATINGS, kksr('core.wp.functions.deactivate'));
 
 
 
 
 
19
  require_once __DIR__.'/hooks.php';
20
  require_once __DIR__.'/hydrate.php';
21
 
 
22
  register_deactivation_hook(KK_STAR_RATINGS, kksr('core.wp.functions.deactivate'));
23
+
24
+ // We aren't using `register_activation_hook` because it is buggy
25
+ // and does not get called when the plugin is implictly updated.
26
+ // The activation will be handled when the plugin is loaded.
27
+ // register_activation_hook(KK_STAR_RATINGS, kksr('core.wp.functions.activate'));
src/core/{migrations → migrators}/index.php RENAMED
File without changes
src/core/{migrations → migrators}/v5.1.0/index.php RENAMED
File without changes
src/core/{migrations → migrators}/v5.1.0/posts.php RENAMED
File without changes
src/core/wp/actions/plugins_loaded.php CHANGED
@@ -12,7 +12,6 @@
12
  namespace Bhittani\StarRating\core\wp\actions;
13
 
14
  use function Bhittani\StarRating\core\functions\action;
15
- use function Bhittani\StarRating\core\functions\option;
16
  use function Bhittani\StarRating\core\wp\functions\activate;
17
 
18
  if (! defined('KK_STAR_RATINGS')) {
@@ -22,9 +21,10 @@ if (! defined('KK_STAR_RATINGS')) {
22
 
23
  function plugins_loaded()
24
  {
25
- if (version_compare(kksr('version'), option('ver'), '!=')) {
26
- activate();
27
- }
28
 
 
29
  action('init', kksr());
30
  }
12
  namespace Bhittani\StarRating\core\wp\actions;
13
 
14
  use function Bhittani\StarRating\core\functions\action;
 
15
  use function Bhittani\StarRating\core\wp\functions\activate;
16
 
17
  if (! defined('KK_STAR_RATINGS')) {
21
 
22
  function plugins_loaded()
23
  {
24
+ // We aren't using `register_activation_hook` because it is buggy
25
+ // and does not get called when the plugin is implictly updated.
26
+ activate();
27
 
28
+ // Let everyone know that the plugin is loaded and initialized.
29
  action('init', kksr());
30
  }
src/core/wp/actions/the_post.php CHANGED
@@ -1,73 +1,72 @@
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\core\wp\actions;
13
-
14
- use WP_Post;
15
- use WP_Query;
16
-
17
- use function Bhittani\StarRating\core\functions\meta_prefix;
18
- use function Bhittani\StarRating\core\functions\option;
19
- use function Bhittani\StarRating\core\functions\post_meta;
20
- use function Bhittani\StarRating\functions\cast;
21
-
22
- if (! defined('KK_STAR_RATINGS')) {
23
- http_response_code(404);
24
- exit();
25
- }
26
-
27
- function the_post(WP_Post $post, WP_Query $query): void
28
- {
29
- $id = $post->ID;
30
-
31
- if (metadata_exists('post', $id, meta_prefix('avg'))) {
32
- // v5
33
- // if (metadata_exists('post', $id, meta_prefix('count_default'))) {
34
- // $count = max((int) post_meta($id, 'count_default'), 0);
35
- // }
36
-
37
- // // v5
38
- // if (metadata_exists('post', $id, meta_prefix('ratings_default'))) {
39
- // $ratings = max((float) post_meta($id, 'ratings_default'), 0);
40
- // }
41
-
42
- // < v5
43
- // if (! isset($count)) {
44
- $count = max((int) post_meta($id, 'casts'), 0);
45
- // }
46
-
47
- // v3, v4
48
- if (/*! isset($ratings) && */metadata_exists('post', $id, meta_prefix('ratings'))) {
49
- $ratings = max((float) post_meta($id, 'ratings'), 0);
50
- }
51
-
52
- // < v3
53
- if (! isset($ratings)) {
54
- $stars = max((int) option('stars', 5), 1);
55
- $avg = min(max((float) post_meta($id, 'avg'), 0), $stars);
56
- // 4 * 3 / 5 * 5 => 12
57
- // 8 * 3 / 10 * 5 => 12
58
- $ratings = cast($avg, 5, $stars) * $count; // Reset to base 5.
59
- // $ratings = $avg * $count / $stars * 5; // Reset to base 5.
60
- }
61
-
62
- // Calculate fresh average.
63
- $avg = $count ? ($ratings / $count) : 0;
64
-
65
- post_meta($id, [
66
- 'avg' => $avg,
67
- 'casts' => $count,
68
- 'avg_default' => $avg,
69
- 'count_default' => $count,
70
- 'ratings_default' => $ratings,
71
- ]);
72
- }
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\core\wp\actions;
13
+
14
+ use function Bhittani\StarRating\core\functions\meta_prefix;
15
+ use function Bhittani\StarRating\core\functions\option;
16
+ use function Bhittani\StarRating\core\functions\post_meta;
17
+ use function Bhittani\StarRating\functions\cast;
18
+ use WP_Post;
19
+ use WP_Query;
20
+
21
+ if (! defined('KK_STAR_RATINGS')) {
22
+ http_response_code(404);
23
+ exit();
24
+ }
25
+
26
+ function the_post(WP_Post $post, WP_Query $query): void
27
+ {
28
+ $id = $post->ID;
29
+
30
+ if (metadata_exists('post', $id, meta_prefix('avg'))) {
31
+ // v5
32
+ // if (metadata_exists('post', $id, meta_prefix('count_default'))) {
33
+ // $count = max((int) post_meta($id, 'count_default'), 0);
34
+ // }
35
+
36
+ // // v5
37
+ // if (metadata_exists('post', $id, meta_prefix('ratings_default'))) {
38
+ // $ratings = max((float) post_meta($id, 'ratings_default'), 0);
39
+ // }
40
+
41
+ // < v5
42
+ // if (! isset($count)) {
43
+ $count = max((int) post_meta($id, 'casts'), 0);
44
+ // }
45
+
46
+ // v3, v4
47
+ if (/*! isset($ratings) && */ metadata_exists('post', $id, meta_prefix('ratings'))) {
48
+ $ratings = max((float) post_meta($id, 'ratings'), 0);
49
+ }
50
+
51
+ // < v3
52
+ if (! isset($ratings)) {
53
+ $stars = max((int) option('stars', 5), 1);
54
+ $avg = min(max((float) post_meta($id, 'avg'), 0), $stars);
55
+ // 4 * 3 / 5 * 5 => 12
56
+ // 8 * 3 / 10 * 5 => 12
57
+ $ratings = cast($avg, 5, $stars) * $count; // Reset to base 5.
58
+ // $ratings = $avg * $count / $stars * 5; // Reset to base 5.
59
+ }
60
+
61
+ // Calculate fresh average.
62
+ $avg = $count ? ($ratings / $count) : 0;
63
+
64
+ post_meta($id, [
65
+ 'avg' => $avg,
66
+ 'casts' => $count,
67
+ 'avg_default' => $avg,
68
+ 'count_default' => $count,
69
+ 'ratings_default' => $ratings,
70
+ ]);
71
+ }
72
+ }
 
src/core/wp/functions/activate.php CHANGED
@@ -24,17 +24,17 @@ function activate(): void
24
  $version = kksr('version');
25
  $previous = option('ver');
26
 
27
- if (! $previous) {
28
- action('install', $version);
29
- } elseif (version_compare($version, $previous, '<')) {
30
- action('downgrade', $version, $previous);
31
- } elseif (version_compare($version, $previous, '>')) {
32
- action('upgrade', $version, $previous);
33
- }
34
-
35
  if (version_compare($version, $previous, '!=')) {
 
 
 
 
 
 
 
 
36
  action('activate', $version, $previous);
37
- }
38
 
39
- option(['ver' => $version]);
 
40
  }
24
  $version = kksr('version');
25
  $previous = option('ver');
26
 
 
 
 
 
 
 
 
 
27
  if (version_compare($version, $previous, '!=')) {
28
+ if (! $previous) {
29
+ action('install', $version);
30
+ } elseif (version_compare($version, $previous, '<')) {
31
+ action('downgrade', $version, $previous);
32
+ } elseif (version_compare($version, $previous, '>')) {
33
+ action('upgrade', $version, $previous);
34
+ }
35
+
36
  action('activate', $version, $previous);
 
37
 
38
+ option(['ver' => $version]);
39
+ }
40
  }
src/functions/migrators.php ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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\functions;
13
+
14
+ if (! defined('KK_STAR_RATINGS')) {
15
+ http_response_code(404);
16
+ exit();
17
+ }
18
+
19
+ function migrators(array $data): array
20
+ {
21
+ $migrators = [];
22
+
23
+ foreach ($data as $filename => $options) {
24
+ $parts = explode('/', $filename, 2);
25
+ $semver = substr($parts[0], 1);
26
+ $tag = $parts[1];
27
+
28
+ [$handler, $payload] = $options();
29
+
30
+ $migrators[] = (object) compact('handler', 'payload', 'semver', 'tag');
31
+ }
32
+
33
+ usort($migrators, function ($a, $b) {
34
+ if ($a->semver === $b->semver) {
35
+ return $a->tag <=> $b->tag;
36
+ }
37
+
38
+ return version_compare($a->semver, $b->semver);
39
+ });
40
+
41
+ return $migrators;
42
+ }
src/functions/post_meta.php CHANGED
@@ -49,7 +49,7 @@ function post_meta($id, $keyOrMeta, $default = null, string $prefix = null, arra
49
  continue;
50
  }
51
 
52
- add_post_meta($id, $prefix.$key, $value, true);
53
  }
54
 
55
  return $keyOrMeta;
49
  continue;
50
  }
51
 
52
+ add_post_meta($id, $prefix.$key, $value);
53
  }
54
 
55
  return $keyOrMeta;