|
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); // return a fresh copy of the review |
|
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 |
|
'meta_input' => ['_submitted' => $command->request->toArray()], // save the original submitted request in metadata |
|
91
|
13 |
|
'ping_status' => 'closed', |
|
92
|
13 |
|
'post_content' => $values->content, |
|
93
|
13 |
|
'post_date' => $values->date, |
|
94
|
13 |
|
'post_date_gmt' => $values->date_gmt, |
|
95
|
13 |
|
'post_name' => uniqid($values->type), |
|
96
|
13 |
|
'post_status' => $this->postStatus($values->type, $values->blacklisted), |
|
97
|
13 |
|
'post_title' => $values->title, |
|
98
|
13 |
|
'post_type' => glsr()->post_type, |
|
99
|
|
|
]; |
|
100
|
13 |
|
$postId = wp_insert_post($postValues, true); |
|
101
|
13 |
|
if (is_wp_error($postId)) { |
|
102
|
|
|
glsr_log()->error($postId->get_error_message())->debug($postValues); |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
13 |
|
glsr()->action('review/create', $postId, $command); |
|
106
|
13 |
|
return $postId; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param int $reviewId |
|
111
|
|
|
* @return int|false |
|
112
|
|
|
*/ |
|
113
|
|
|
public function delete($reviewId) |
|
114
|
|
|
{ |
|
115
|
|
|
glsr(Cache::class)->delete($reviewId, 'reviews'); |
|
116
|
|
|
return glsr(Database::class)->delete('ratings', [ |
|
117
|
|
|
'review_id' => $reviewId, |
|
118
|
|
|
]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param int $reviewId |
|
123
|
|
|
* @return void |
|
124
|
|
|
*/ |
|
125
|
|
|
public function deleteRevisions($reviewId) |
|
126
|
|
|
{ |
|
127
|
|
|
$revisionIds = glsr(Query::class)->revisionIds($reviewId); |
|
128
|
|
|
foreach ($revisionIds as $revisionId) { |
|
129
|
|
|
wp_delete_post_revision($revisionId); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param int $reviewId |
|
135
|
|
|
* @return Review |
|
136
|
|
|
*/ |
|
137
|
13 |
|
public function get($reviewId) |
|
138
|
|
|
{ |
|
139
|
13 |
|
$review = glsr(Query::class)->review($reviewId); |
|
140
|
13 |
|
glsr()->action('get/review', $review, $reviewId); |
|
141
|
13 |
|
return $review; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return Reviews |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function reviews(array $args = []) |
|
148
|
|
|
{ |
|
149
|
1 |
|
$args = (new NormalizePaginationArgs($args))->toArray(); |
|
150
|
1 |
|
$results = glsr(Query::class)->reviews($args); |
|
151
|
1 |
|
$total = $this->total($args, $results); |
|
152
|
1 |
|
$reviews = new Reviews($results, $total, $args); |
|
153
|
1 |
|
glsr()->action('get/reviews', $reviews, $args); |
|
154
|
1 |
|
return $reviews; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return int |
|
159
|
|
|
*/ |
|
160
|
1 |
|
public function total(array $args = [], array $reviews = []) |
|
161
|
|
|
{ |
|
162
|
1 |
|
return glsr(Query::class)->totalReviews($args, $reviews); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param int $postId |
|
167
|
|
|
* @return int|false |
|
168
|
|
|
*/ |
|
169
|
1 |
|
public function unassignPost(Review $review, $postId) |
|
170
|
|
|
{ |
|
171
|
|
|
$where = [ |
|
172
|
1 |
|
'post_id' => $postId, |
|
173
|
1 |
|
'rating_id' => $review->rating_id, |
|
174
|
|
|
]; |
|
175
|
1 |
|
if ($result = glsr(Database::class)->delete('assigned_posts', $where)) { |
|
176
|
1 |
|
glsr(Cache::class)->delete($review->ID, 'reviews'); |
|
177
|
1 |
|
glsr(CountManager::class)->posts($postId); |
|
178
|
|
|
} |
|
179
|
1 |
|
return $result; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param int $termId |
|
184
|
|
|
* @return int|false |
|
185
|
|
|
*/ |
|
186
|
1 |
|
public function unassignTerm(Review $review, $termId) |
|
187
|
|
|
{ |
|
188
|
|
|
$where = [ |
|
189
|
1 |
|
'rating_id' => $review->rating_id, |
|
190
|
1 |
|
'term_id' => $termId, |
|
191
|
|
|
]; |
|
192
|
1 |
|
if ($result = glsr(Database::class)->delete('assigned_terms', $where)) { |
|
193
|
1 |
|
glsr(Cache::class)->delete($review->ID, 'reviews'); |
|
194
|
1 |
|
glsr(CountManager::class)->terms($termId); |
|
195
|
|
|
} |
|
196
|
1 |
|
return $result; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param int $userId |
|
201
|
|
|
* @return int|false |
|
202
|
|
|
*/ |
|
203
|
1 |
|
public function unassignUser(Review $review, $userId) |
|
204
|
|
|
{ |
|
205
|
|
|
$where = [ |
|
206
|
1 |
|
'rating_id' => $review->rating_id, |
|
207
|
1 |
|
'user_id' => $userId, |
|
208
|
|
|
]; |
|
209
|
1 |
|
if ($result = glsr(Database::class)->delete('assigned_users', $where)) { |
|
210
|
1 |
|
glsr(Cache::class)->delete($review->ID, 'reviews'); |
|
211
|
1 |
|
glsr(CountManager::class)->users($userId); |
|
212
|
|
|
} |
|
213
|
1 |
|
return $result; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @param int $reviewId |
|
218
|
|
|
* @return int|bool |
|
219
|
|
|
*/ |
|
220
|
|
|
public function update($reviewId, array $data = []) |
|
221
|
|
|
{ |
|
222
|
|
|
glsr(Cache::class)->delete($reviewId, 'reviews'); |
|
223
|
|
|
$defaults = glsr(RatingDefaults::class)->restrict($data); |
|
224
|
|
|
if ($data = array_intersect_key($data, $defaults)) { |
|
225
|
|
|
return glsr(Database::class)->update('ratings', $data, [ |
|
226
|
|
|
'review_id' => $reviewId, |
|
227
|
|
|
]); |
|
228
|
|
|
} |
|
229
|
|
|
return 0; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @param int $reviewId |
|
234
|
|
|
* @return void |
|
235
|
|
|
*/ |
|
236
|
|
|
public function updateCustom($reviewId, array $data = []) |
|
237
|
|
|
{ |
|
238
|
|
|
$fields = glsr()->config('forms/metabox-fields'); |
|
239
|
|
|
$defaults = glsr(RatingDefaults::class)->defaults(); |
|
240
|
|
|
$customKeys = array_keys(array_diff_key($fields, $defaults)); |
|
241
|
|
|
if ($data = shortcode_atts(array_fill_keys($customKeys, ''), $data)) { |
|
242
|
|
|
$data = Arr::prefixKeys($data, 'custom_'); |
|
243
|
|
|
foreach ($data as $metaKey => $metaValue) { |
|
244
|
|
|
glsr(Database::class)->metaSet($reviewId, $metaKey, $metaValue); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @param int $postId |
|
251
|
|
|
* @param bool $isPublished |
|
252
|
|
|
* @return int|bool |
|
253
|
|
|
*/ |
|
254
|
|
|
public function updateAssignedPost($postId, $isPublished) |
|
255
|
|
|
{ |
|
256
|
|
|
$isPublished = wp_validate_boolean($isPublished); |
|
257
|
|
|
$postId = Cast::toInt($postId); |
|
258
|
|
|
return glsr(Database::class)->update('assigned_posts', |
|
259
|
|
|
['is_published' => $isPublished], |
|
260
|
|
|
['post_id' => $postId] |
|
261
|
|
|
); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @param string $reviewType |
|
266
|
|
|
* @param bool $isBlacklisted |
|
267
|
|
|
* @return string |
|
268
|
|
|
*/ |
|
269
|
13 |
|
protected function postStatus($reviewType, $isBlacklisted) |
|
270
|
|
|
{ |
|
271
|
13 |
|
$requireApproval = glsr(OptionManager::class)->getBool('settings.general.require.approval'); |
|
272
|
13 |
|
return 'local' == $reviewType && ($requireApproval || $isBlacklisted) |
|
273
|
1 |
|
? 'pending' |
|
274
|
13 |
|
: 'publish'; |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
|