|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Database; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Modules\Rating; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Review; |
|
7
|
|
|
|
|
8
|
|
|
class PostCountsManager |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var CountsManager |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $manager; |
|
14
|
|
|
|
|
15
|
1 |
|
public function __construct() |
|
16
|
|
|
{ |
|
17
|
1 |
|
$this->manager = glsr(CountsManager::class); |
|
18
|
1 |
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param int $postId |
|
22
|
|
|
* @return array |
|
23
|
|
|
*/ |
|
24
|
|
|
public function build($postId) |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->manager->buildCounts([ |
|
27
|
|
|
'post_ids' => [$postId], |
|
28
|
|
|
]); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return void |
|
33
|
|
|
*/ |
|
34
|
|
|
public function decrease(Review $review) |
|
35
|
|
|
{ |
|
36
|
|
|
if (empty($counts = $this->get($review->assigned_to))) { |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
$this->update($review->assigned_to, |
|
40
|
|
|
$this->manager->decreaseRating($counts, $review->review_type, $review->rating) |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param int $postId |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function get($postId) |
|
49
|
|
|
{ |
|
50
|
|
|
return array_filter((array) get_post_meta($postId, CountsManager::META_COUNT, true)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return void |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function increase(Review $review) |
|
57
|
|
|
{ |
|
58
|
1 |
|
if (!(get_post($review->assigned_to) instanceof \WP_Post)) { |
|
59
|
1 |
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
$counts = $this->get($review->assigned_to); |
|
62
|
|
|
$counts = empty($counts) |
|
63
|
|
|
? $this->build($review->assigned_to) |
|
64
|
|
|
: $this->manager->increaseRating($counts, $review->review_type, $review->rating); |
|
65
|
|
|
$this->update($review->assigned_to, $counts); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param int $postId |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
public function update($postId, array $reviewCounts) |
|
73
|
|
|
{ |
|
74
|
|
|
$ratingCounts = $this->manager->flatten($reviewCounts); |
|
75
|
|
|
update_post_meta($postId, CountsManager::META_COUNT, $reviewCounts); |
|
76
|
|
|
update_post_meta($postId, CountsManager::META_AVERAGE, glsr(Rating::class)->getAverage($ratingCounts)); |
|
77
|
|
|
update_post_meta($postId, CountsManager::META_RANKING, glsr(Rating::class)->getRanking($ratingCounts)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function updateAll() |
|
84
|
|
|
{ |
|
85
|
1 |
|
glsr(SqlQueries::class)->deletePostCountMetaKeys(); |
|
86
|
1 |
|
$postIds = glsr(SqlQueries::class)->getReviewsMeta('assigned_to'); |
|
87
|
1 |
|
foreach ($postIds as $postId) { |
|
88
|
|
|
$this->update($postId, $this->build($postId)); |
|
89
|
|
|
} |
|
90
|
1 |
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|