|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Database; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Commands\CreateReview; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Database; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Defaults\CreateReviewDefaults; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
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
|
|
|
public function assignPost(Review $review, $postId) |
|
20
|
|
|
{ |
|
21
|
|
|
return glsr(Database::class)->insertRaw(glsr(Query::class)->table('assigned_posts'), [ |
|
22
|
|
|
'is_published' => 'publish' === get_post_status($postId), |
|
23
|
|
|
'post_id' => $postId, |
|
24
|
|
|
'rating_id' => $review->rating_id, |
|
25
|
|
|
]); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param int $termId |
|
30
|
|
|
* @return int|false |
|
31
|
|
|
*/ |
|
32
|
|
|
public function assignTerm(Review $review, $termId) |
|
33
|
|
|
{ |
|
34
|
|
|
return glsr(Database::class)->insertRaw(glsr(Query::class)->table('assigned_terms'), [ |
|
35
|
|
|
'rating_id' => $review->rating_id, |
|
36
|
|
|
'term_id' => $termId, |
|
37
|
|
|
]); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param int $userId |
|
42
|
|
|
* @return int|false |
|
43
|
|
|
*/ |
|
44
|
|
|
public function assignUser(Review $review, $userId) |
|
45
|
|
|
{ |
|
46
|
|
|
return glsr(Database::class)->insertRaw(glsr(Query::class)->table('assigned_users'), [ |
|
47
|
|
|
'rating_id' => $review->rating_id, |
|
48
|
|
|
'user_id' => $userId, |
|
49
|
|
|
]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param int $reviewId |
|
54
|
|
|
* @return Review |
|
55
|
|
|
*/ |
|
56
|
|
|
public function get($reviewId) |
|
57
|
|
|
{ |
|
58
|
|
|
$review = glsr(Query::class)->review($reviewId); |
|
59
|
|
|
glsr()->action('get/review', $review, $reviewId); |
|
60
|
|
|
return $review; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return Reviews |
|
65
|
|
|
*/ |
|
66
|
|
|
public function reviews(array $args = []) |
|
67
|
|
|
{ |
|
68
|
|
|
$reviews = glsr(Query::class)->reviews($args); |
|
69
|
|
|
$total = $this->total($args, $reviews); |
|
70
|
|
|
glsr()->action('get/reviews', $reviews, $args); |
|
71
|
|
|
return new Reviews($reviews, $total, $args); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return int |
|
76
|
|
|
*/ |
|
77
|
|
|
public function total(array $args = [], array $reviews = []) |
|
78
|
|
|
{ |
|
79
|
|
|
return glsr(Query::class)->totalReviews($args, $reviews); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param int $postId |
|
84
|
|
|
* @return int|false |
|
85
|
|
|
*/ |
|
86
|
|
|
public function unassignPost(Review $review, $postId) |
|
87
|
|
|
{ |
|
88
|
|
|
return glsr(Database::class)->delete(glsr(Query::class)->table('assigned_posts'), [ |
|
89
|
|
|
'post_id' => $postId, |
|
90
|
|
|
'rating_id' => $review->rating_id, |
|
91
|
|
|
]); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param int $termId |
|
96
|
|
|
* @return int|false |
|
97
|
|
|
*/ |
|
98
|
|
|
public function unassignTerm(Review $review, $termId) |
|
99
|
|
|
{ |
|
100
|
|
|
return glsr(Database::class)->delete(glsr(Query::class)->table('assigned_terms'), [ |
|
101
|
|
|
'rating_id' => $review->rating_id, |
|
102
|
|
|
'term_id' => $termId, |
|
103
|
|
|
]); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param int $termId |
|
108
|
|
|
* @return int|false |
|
109
|
|
|
*/ |
|
110
|
|
|
public function unassignUser(Review $review, $userId) |
|
111
|
|
|
{ |
|
112
|
|
|
return glsr(Database::class)->delete(glsr(Query::class)->table('assigned_users'), [ |
|
113
|
|
|
'rating_id' => $review->rating_id, |
|
114
|
|
|
'user_id' => $userId, |
|
115
|
|
|
]); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param array[]|string $termIds |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
|
|
public function normalizeTermIds($termIds) |
|
127
|
|
|
{ |
|
128
|
|
|
$termIds = Arr::convertFromString($termIds); |
|
129
|
|
|
foreach ($termIds as &$termId) { |
|
130
|
|
|
$term = term_exists($termId, glsr()->taxonomy); // get the term from a term slug |
|
131
|
|
|
$termId = Arr::get($term, 'term_id', 0); |
|
132
|
|
|
} |
|
133
|
|
|
return Arr::uniqueInt($termIds); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
// -[ ] insert review (rating) |
|
138
|
|
|
// -[ ] update review (rating) |
|
139
|
|
|
// -[ ] delete review (rating) |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @return false|Review |
|
203
|
|
|
*/ |
|
204
|
|
|
public function create(CreateReview $command) |
|
205
|
|
|
{ |
|
206
|
|
|
$reviewValues = glsr(CreateReviewDefaults::class)->restrict((array) $command); |
|
207
|
|
|
$reviewValues = glsr()->filterArray('create/review-values', $reviewValues, $command); |
|
208
|
|
|
$reviewValues = Arr::prefixKeys($reviewValues); |
|
209
|
|
|
$postValues = [ |
|
210
|
|
|
'comment_status' => 'closed', |
|
211
|
|
|
'meta_input' => $reviewValues, |
|
212
|
|
|
'ping_status' => 'closed', |
|
213
|
|
|
'post_content' => $reviewValues['_content'], |
|
214
|
|
|
'post_date' => $reviewValues['_date'], |
|
215
|
|
|
'post_date_gmt' => get_gmt_from_date($reviewValues['_date']), |
|
216
|
|
|
'post_name' => uniqid($reviewValues['_review_type']), |
|
217
|
|
|
'post_status' => $this->getNewPostStatus($reviewValues, $command->blacklisted), |
|
218
|
|
|
'post_title' => $reviewValues['_title'], |
|
219
|
|
|
'post_type' => Application::POST_TYPE, |
|
220
|
|
|
]; |
|
221
|
|
|
$postId = wp_insert_post($postValues, true); |
|
222
|
|
|
if (is_wp_error($postId)) { |
|
223
|
|
|
glsr_log()->error($postId->get_error_message())->debug($postValues); |
|
224
|
|
|
return false; |
|
225
|
|
|
} |
|
226
|
|
|
$post = get_post($postId); |
|
|
|
|
|
|
227
|
|
|
glsr()->action('review/creating', $post, $command); |
|
228
|
|
|
$this->setTerms($post->ID, $command->category); |
|
|
|
|
|
|
229
|
|
|
$review = $this->get($post); |
|
|
|
|
|
|
230
|
|
|
glsr()->action('review/created', $review, $command); |
|
231
|
|
|
return $review; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @param string $metaReviewId |
|
236
|
|
|
* @return void |
|
237
|
|
|
*/ |
|
238
|
|
|
public function delete($metaReviewId) |
|
239
|
|
|
{ |
|
240
|
|
|
if ($postId = $this->getPostId($metaReviewId)) { |
|
|
|
|
|
|
241
|
|
|
wp_delete_post($postId, true); |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* @param bool $isBlacklisted |
|
248
|
|
|
* @return string |
|
249
|
|
|
*/ |
|
250
|
|
|
protected function getNewPostStatus(array $reviewValues, $isBlacklisted) |
|
251
|
|
|
{ |
|
252
|
|
|
$requireApproval = glsr(OptionManager::class)->getBool('settings.general.require.approval'); |
|
253
|
|
|
return 'local' == $reviewValues['_review_type'] && ($requireApproval || $isBlacklisted) |
|
254
|
|
|
? 'pending' |
|
255
|
|
|
: 'publish'; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* @param int $postId |
|
260
|
|
|
* @param string $termIds |
|
261
|
|
|
* @return void |
|
262
|
|
|
*/ |
|
263
|
|
|
protected function setTerms($postId, $termIds) |
|
264
|
|
|
{ |
|
265
|
|
|
$termIds = $this->normalizeTermIds($termIds); |
|
266
|
|
|
if (empty($termIds)) { |
|
267
|
|
|
return; |
|
268
|
|
|
} |
|
269
|
|
|
$termTaxonomyIds = wp_set_object_terms($postId, $termIds, Application::TAXONOMY); |
|
270
|
|
|
if (is_wp_error($termTaxonomyIds)) { |
|
271
|
|
|
glsr_log()->error($termTaxonomyIds->get_error_message()); |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|