1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Database; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Commands\CreateReview; |
6
|
|
|
use GeminiLabs\SiteReviews\Database; |
7
|
|
|
use GeminiLabs\SiteReviews\Defaults\RatingDefaults; |
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
9
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
10
|
|
|
use GeminiLabs\SiteReviews\Review; |
11
|
|
|
use GeminiLabs\SiteReviews\Reviews; |
12
|
|
|
|
13
|
|
|
class ReviewManager |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param int $postId |
17
|
|
|
* @return int|false |
18
|
|
|
*/ |
19
|
2 |
|
public function assignPost(Review $review, $postId) |
20
|
|
|
{ |
21
|
|
|
$where = [ |
22
|
2 |
|
'is_published' => 'publish' === get_post_status($postId), |
|
|
|
|
23
|
2 |
|
'post_id' => $postId, |
24
|
2 |
|
'rating_id' => $review->rating_id, |
25
|
|
|
]; |
26
|
2 |
|
if ($result = glsr(Database::class)->insert('assigned_posts', $where)) { |
27
|
2 |
|
glsr(Cache::class)->delete($review->ID, 'reviews'); |
28
|
2 |
|
glsr(CountManager::class)->posts($postId); |
29
|
|
|
} |
30
|
2 |
|
return $result; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param int $termId |
35
|
|
|
* @return int|false |
36
|
|
|
*/ |
37
|
2 |
|
public function assignTerm(Review $review, $termId) |
38
|
|
|
{ |
39
|
|
|
$where = [ |
40
|
2 |
|
'rating_id' => $review->rating_id, |
41
|
2 |
|
'term_id' => $termId, |
42
|
|
|
]; |
43
|
2 |
|
if ($result = glsr(Database::class)->insert('assigned_terms', $where)) { |
44
|
2 |
|
glsr(Cache::class)->delete($review->ID, 'reviews'); |
45
|
2 |
|
glsr(CountManager::class)->terms($termId); |
46
|
|
|
} |
47
|
2 |
|
return $result; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param int $userId |
52
|
|
|
* @return int|false |
53
|
|
|
*/ |
54
|
2 |
|
public function assignUser(Review $review, $userId) |
55
|
|
|
{ |
56
|
|
|
$where = [ |
57
|
2 |
|
'rating_id' => $review->rating_id, |
58
|
2 |
|
'user_id' => $userId, |
59
|
|
|
]; |
60
|
2 |
|
if ($result = glsr(Database::class)->insert('assigned_users', $where)) { |
61
|
2 |
|
glsr(Cache::class)->delete($review->ID, 'reviews'); |
62
|
2 |
|
glsr(CountManager::class)->users($userId); |
63
|
|
|
} |
64
|
2 |
|
return $result; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return false|Review |
69
|
|
|
*/ |
70
|
13 |
|
public function create(CreateReview $command) |
71
|
|
|
{ |
72
|
13 |
|
if ($postId = $this->createRaw($command)) { |
73
|
13 |
|
$review = $this->get($postId); |
74
|
13 |
|
if ($review->isValid()) { |
75
|
13 |
|
glsr()->action('review/created', $review, $command); |
76
|
13 |
|
return $this->get($review->ID); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return false|int |
84
|
|
|
*/ |
85
|
13 |
|
public function createRaw(CreateReview $command) |
86
|
|
|
{ |
87
|
13 |
|
$values = glsr()->args($command->toArray()); // this filters the values |
88
|
|
|
$postValues = [ |
89
|
13 |
|
'comment_status' => 'closed', |
90
|
13 |
|
'ping_status' => 'closed', |
91
|
13 |
|
'post_content' => $values->content, |
92
|
13 |
|
'post_date' => $values->date, |
93
|
13 |
|
'post_name' => uniqid($values->type), |
94
|
13 |
|
'post_status' => $this->postStatus($values->type, $values->blacklisted), |
95
|
13 |
|
'post_title' => $values->title, |
96
|
13 |
|
'post_type' => glsr()->post_type, |
97
|
|
|
]; |
98
|
13 |
|
$postId = wp_insert_post($postValues, true); |
|
|
|
|
99
|
13 |
|
if (is_wp_error($postId)) { |
|
|
|
|
100
|
|
|
glsr_log()->error($postId->get_error_message())->debug($postValues); |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
13 |
|
glsr()->action('review/create', $postId, $command); |
104
|
13 |
|
return $postId; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param int $reviewId |
109
|
|
|
* @return int|false |
110
|
|
|
*/ |
111
|
|
|
public function delete($reviewId) |
112
|
|
|
{ |
113
|
|
|
glsr(Cache::class)->delete($reviewId, 'reviews'); |
114
|
|
|
return glsr(Database::class)->delete('ratings', [ |
115
|
|
|
'review_id' => $reviewId, |
116
|
|
|
]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param int $reviewId |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
|
|
public function deleteRevisions($reviewId) |
124
|
|
|
{ |
125
|
|
|
$revisionIds = glsr(Query::class)->revisionIds($reviewId); |
126
|
|
|
foreach ($revisionIds as $revisionId) { |
127
|
|
|
wp_delete_post_revision($revisionId); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param int $reviewId |
133
|
|
|
* @return Review |
134
|
|
|
*/ |
135
|
13 |
|
public function get($reviewId) |
136
|
|
|
{ |
137
|
13 |
|
$review = glsr(Query::class)->review($reviewId); |
138
|
13 |
|
glsr()->action('get/review', $review, $reviewId); |
139
|
13 |
|
return $review; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return Reviews |
144
|
|
|
*/ |
145
|
1 |
|
public function reviews(array $args = []) |
146
|
|
|
{ |
147
|
1 |
|
$args = (new NormalizePaginationArgs($args))->toArray(); |
148
|
1 |
|
$results = glsr(Query::class)->reviews($args); |
149
|
1 |
|
$total = $this->total($args, $results); |
150
|
1 |
|
$reviews = new Reviews($results, $total, $args); |
151
|
1 |
|
glsr()->action('get/reviews', $reviews, $args); |
152
|
1 |
|
return $reviews; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return int |
157
|
|
|
*/ |
158
|
1 |
|
public function total(array $args = [], array $reviews = []) |
159
|
|
|
{ |
160
|
1 |
|
return glsr(Query::class)->totalReviews($args, $reviews); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param int $postId |
165
|
|
|
* @return int|false |
166
|
|
|
*/ |
167
|
1 |
|
public function unassignPost(Review $review, $postId) |
168
|
|
|
{ |
169
|
|
|
$where = [ |
170
|
1 |
|
'post_id' => $postId, |
171
|
1 |
|
'rating_id' => $review->rating_id, |
172
|
|
|
]; |
173
|
1 |
|
if ($result = glsr(Database::class)->delete('assigned_posts', $where)) { |
174
|
1 |
|
glsr(Cache::class)->delete($review->ID, 'reviews'); |
175
|
1 |
|
glsr(CountManager::class)->posts($postId); |
176
|
|
|
} |
177
|
1 |
|
return $result; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param int $termId |
182
|
|
|
* @return int|false |
183
|
|
|
*/ |
184
|
1 |
|
public function unassignTerm(Review $review, $termId) |
185
|
|
|
{ |
186
|
|
|
$where = [ |
187
|
1 |
|
'rating_id' => $review->rating_id, |
188
|
1 |
|
'term_id' => $termId, |
189
|
|
|
]; |
190
|
1 |
|
if ($result = glsr(Database::class)->delete('assigned_terms', $where)) { |
191
|
1 |
|
glsr(Cache::class)->delete($review->ID, 'reviews'); |
192
|
1 |
|
glsr(CountManager::class)->terms($termId); |
193
|
|
|
} |
194
|
1 |
|
return $result; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param int $userId |
199
|
|
|
* @return int|false |
200
|
|
|
*/ |
201
|
1 |
|
public function unassignUser(Review $review, $userId) |
202
|
|
|
{ |
203
|
|
|
$where = [ |
204
|
1 |
|
'rating_id' => $review->rating_id, |
205
|
1 |
|
'user_id' => $userId, |
206
|
|
|
]; |
207
|
1 |
|
if ($result = glsr(Database::class)->delete('assigned_users', $where)) { |
208
|
1 |
|
glsr(Cache::class)->delete($review->ID, 'reviews'); |
209
|
1 |
|
glsr(CountManager::class)->users($userId); |
210
|
|
|
} |
211
|
1 |
|
return $result; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param int $reviewId |
216
|
|
|
* @return int|bool |
217
|
|
|
*/ |
218
|
|
|
public function update($reviewId, array $data = []) |
219
|
|
|
{ |
220
|
|
|
glsr(Cache::class)->delete($reviewId, 'reviews'); |
221
|
|
|
$defaults = glsr(RatingDefaults::class)->restrict($data); |
222
|
|
|
if ($data = array_intersect_key($data, $defaults)) { |
223
|
|
|
return glsr(Database::class)->update('ratings', $data, [ |
224
|
|
|
'review_id' => $reviewId, |
225
|
|
|
]); |
226
|
|
|
} |
227
|
|
|
return 0; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param int $reviewId |
232
|
|
|
* @return void |
233
|
|
|
*/ |
234
|
|
|
public function updateCustom($reviewId, array $data = []) |
235
|
|
|
{ |
236
|
|
|
$fields = glsr()->config('forms/metabox-fields'); |
237
|
|
|
$defaults = glsr(RatingDefaults::class)->defaults(); |
238
|
|
|
$customKeys = array_keys(array_diff_key($fields, $defaults)); |
239
|
|
|
if ($data = shortcode_atts(array_fill_keys($customKeys, ''), $data)) { |
|
|
|
|
240
|
|
|
$data = Arr::prefixKeys($data, 'custom_'); |
241
|
|
|
foreach ($data as $metaKey => $metaValue) { |
242
|
|
|
glsr(Database::class)->metaSet($reviewId, $metaKey, $metaValue); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param int $postId |
249
|
|
|
* @param bool $isPublished |
250
|
|
|
* @return int|bool |
251
|
|
|
*/ |
252
|
|
|
public function updateAssignedPost($postId, $isPublished) |
253
|
|
|
{ |
254
|
|
|
$isPublished = wp_validate_boolean($isPublished); |
|
|
|
|
255
|
|
|
$postId = Cast::toInt($postId); |
256
|
|
|
return glsr(Database::class)->update('assigned_posts', |
257
|
|
|
['is_published' => $isPublished], |
258
|
|
|
['post_id' => $postId] |
259
|
|
|
); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param string $reviewType |
264
|
|
|
* @param bool $isBlacklisted |
265
|
|
|
* @return string |
266
|
|
|
*/ |
267
|
13 |
|
protected function postStatus($reviewType, $isBlacklisted) |
268
|
|
|
{ |
269
|
13 |
|
$requireApproval = glsr(OptionManager::class)->getBool('settings.general.require.approval'); |
270
|
13 |
|
return 'local' == $reviewType && ($requireApproval || $isBlacklisted) |
271
|
1 |
|
? 'pending' |
272
|
13 |
|
: 'publish'; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|