|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Database; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Database\CountsManager; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Database\GlobalCountsManager; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Database\PostCountsManager; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Database\TermCountsManager; |
|
11
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
12
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
13
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
|
14
|
|
|
use GeminiLabs\SiteReviews\Review; |
|
15
|
|
|
use WP_Post; |
|
16
|
|
|
|
|
17
|
|
|
class ReviewController extends Controller |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Triggered when a category is added to a review. |
|
21
|
|
|
* |
|
22
|
|
|
* @param int $postId |
|
23
|
|
|
* @param array $terms |
|
24
|
|
|
* @param array $newTTIds |
|
25
|
|
|
* @param string $taxonomy |
|
26
|
|
|
* @param bool $append |
|
27
|
|
|
* @param array $oldTTIds |
|
28
|
|
|
* @return void |
|
29
|
|
|
* @action set_object_terms |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function onAfterChangeCategory($postId, $terms, $newTTIds, $taxonomy, $append, $oldTTIds) |
|
32
|
|
|
{ |
|
33
|
1 |
|
sort($newTTIds); |
|
34
|
1 |
|
sort($oldTTIds); |
|
35
|
1 |
|
if ($newTTIds === $oldTTIds || !$this->isReviewPostId($postId)) { |
|
36
|
1 |
|
return; |
|
37
|
|
|
} |
|
38
|
|
|
$review = glsr_get_review($postId); |
|
39
|
|
|
if ('publish' !== $review->status) { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
$ignoredIds = array_intersect($oldTTIds, $newTTIds); |
|
43
|
|
|
$decreasedIds = array_diff($oldTTIds, $ignoredIds); |
|
44
|
|
|
$increasedIds = array_diff($newTTIds, $ignoredIds); |
|
45
|
|
|
if ($review->term_ids = glsr(Database::class)->getTermIds($decreasedIds, 'term_taxonomy_id')) { |
|
46
|
|
|
glsr(TermCountsManager::class)->decrease($review); |
|
47
|
|
|
} |
|
48
|
|
|
if ($review->term_ids = glsr(Database::class)->getTermIds($increasedIds, 'term_taxonomy_id')) { |
|
49
|
|
|
glsr(TermCountsManager::class)->increase($review); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Triggered when an existing review is approved|unapproved. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $oldStatus |
|
57
|
|
|
* @param string $newStatus |
|
58
|
|
|
* @param \WP_Post $post |
|
59
|
|
|
* @return void |
|
60
|
|
|
* @action transition_post_status |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function onAfterChangeStatus($newStatus, $oldStatus, $post) |
|
63
|
|
|
{ |
|
64
|
1 |
|
if (Application::POST_TYPE != Arr::get($post, 'post_type') |
|
65
|
1 |
|
|| in_array($oldStatus, ['new', $newStatus])) { |
|
66
|
1 |
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
$review = glsr_get_review($post); |
|
69
|
|
|
if ('publish' == $post->post_status) { |
|
70
|
|
|
glsr(CountsManager::class)->increaseAll($review); |
|
71
|
|
|
} else { |
|
72
|
|
|
glsr(CountsManager::class)->decreaseAll($review); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Triggered when a review is first created. |
|
78
|
|
|
* |
|
79
|
|
|
* @return void |
|
80
|
|
|
* @action site-reviews/review/created |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function onAfterCreate(Review $review) |
|
83
|
|
|
{ |
|
84
|
1 |
|
if ('publish' !== $review->status) { |
|
85
|
|
|
return; |
|
86
|
|
|
} |
|
87
|
1 |
|
glsr(GlobalCountsManager::class)->increase($review); |
|
88
|
1 |
|
glsr(PostCountsManager::class)->increase($review); |
|
89
|
1 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Triggered when a review is deleted. |
|
93
|
|
|
* |
|
94
|
|
|
* @param int $postId |
|
95
|
|
|
* @return void |
|
96
|
|
|
* @action before_delete_post |
|
97
|
|
|
*/ |
|
98
|
|
|
public function onBeforeDelete($postId) |
|
99
|
|
|
{ |
|
100
|
|
|
if (!$this->isReviewPostId($postId)) { |
|
101
|
|
|
return; |
|
102
|
|
|
} |
|
103
|
|
|
$review = glsr_get_review($postId); |
|
104
|
|
|
if ('trash' !== $review->status) { // do not run for trashed posts |
|
105
|
|
|
glsr(CountsManager::class)->decreaseAll($review); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Triggered when a review's rating, assigned_to, or review_type is changed. |
|
111
|
|
|
* |
|
112
|
|
|
* @param int $metaId |
|
113
|
|
|
* @param int $postId |
|
114
|
|
|
* @param string $metaKey |
|
115
|
|
|
* @param mixed $metaValue |
|
116
|
|
|
* @return void |
|
117
|
|
|
* @action update_postmeta |
|
118
|
|
|
*/ |
|
119
|
|
|
public function onBeforeUpdate($metaId, $postId, $metaKey, $metaValue) |
|
120
|
|
|
{ |
|
121
|
|
|
if (!$this->isReviewPostId($postId)) { |
|
122
|
|
|
return; |
|
123
|
|
|
} |
|
124
|
|
|
$metaKey = Str::removePrefix('_', $metaKey); |
|
125
|
|
|
if (!in_array($metaKey, ['assigned_to', 'rating', 'review_type'])) { |
|
126
|
|
|
return; |
|
127
|
|
|
} |
|
128
|
|
|
$review = glsr_get_review($postId); |
|
129
|
|
|
if ($review->$metaKey == $metaValue) { |
|
130
|
|
|
return; |
|
131
|
|
|
} |
|
132
|
|
|
$method = Helper::buildMethodName($metaKey, 'onBeforeChange'); |
|
133
|
|
|
call_user_func([$this, $method], $review, $metaValue); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Triggered by the onBeforeUpdate method. |
|
138
|
|
|
* |
|
139
|
|
|
* @param string|int $assignedTo |
|
140
|
|
|
* @return void |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function onBeforeChangeAssignedTo(Review $review, $assignedTo) |
|
143
|
|
|
{ |
|
144
|
|
|
glsr(PostCountsManager::class)->decrease($review); |
|
145
|
|
|
$review->assigned_to = $assignedTo; |
|
146
|
|
|
glsr(PostCountsManager::class)->increase($review); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Triggered by the onBeforeUpdate method. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string|int $rating |
|
153
|
|
|
* @return void |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function onBeforeChangeRating(Review $review, $rating) |
|
156
|
|
|
{ |
|
157
|
|
|
glsr(CountsManager::class)->decreaseAll($review); |
|
158
|
|
|
$review->rating = $rating; |
|
159
|
|
|
glsr(CountsManager::class)->increaseAll($review); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Triggered by the onBeforeUpdate method. |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $reviewType |
|
166
|
|
|
* @return void |
|
167
|
|
|
*/ |
|
168
|
|
|
protected function onBeforeChangeReviewType(Review $review, $reviewType) |
|
169
|
|
|
{ |
|
170
|
|
|
glsr(CountsManager::class)->decreaseAll($review); |
|
171
|
|
|
$review->review_type = $reviewType; |
|
172
|
|
|
glsr(CountsManager::class)->increaseAll($review); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|