|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Database; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Rating; |
|
11
|
|
|
use GeminiLabs\SiteReviews\Review; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @property array $args |
|
15
|
|
|
* @property \wpdb $db |
|
16
|
|
|
*/ |
|
17
|
|
|
class Query |
|
18
|
|
|
{ |
|
19
|
|
|
use Sql; |
|
20
|
|
|
|
|
21
|
15 |
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
15 |
|
global $wpdb; |
|
24
|
15 |
|
$this->db = $wpdb; |
|
25
|
15 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @return array |
|
29
|
|
|
*/ |
|
30
|
|
|
public function export(array $args = []) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->setArgs($args); |
|
33
|
|
|
return glsr(Database::class)->dbGetResults($this->queryExport(), ARRAY_A); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param int $postId |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
|
|
public function hasRevisions($postId) |
|
41
|
|
|
{ |
|
42
|
|
|
return (int) glsr(Database::class)->dbGetVar($this->queryHasRevisions($postId)) > 0; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function import(array $args = []) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->setArgs($args); |
|
51
|
|
|
return glsr(Database::class)->dbGetResults($this->queryImport(), ARRAY_A); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
6 |
|
public function ratings(array $args = []) |
|
58
|
|
|
{ |
|
59
|
6 |
|
$this->setArgs($args, $unset = ['orderby']); |
|
60
|
6 |
|
$results = glsr(Database::class)->dbGetResults($this->queryRatings(), ARRAY_A); |
|
61
|
6 |
|
return $this->normalizeRatings($results); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $metaType |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
public function ratingsFor($metaType, array $args = []) |
|
69
|
|
|
{ |
|
70
|
|
|
$method = Helper::buildMethodName($metaType, 'queryRatingsFor'); |
|
71
|
|
|
if (!method_exists($this, $method)) { |
|
72
|
|
|
return []; |
|
73
|
|
|
} |
|
74
|
|
|
$this->setArgs($args, $unset = ['orderby']); |
|
75
|
|
|
$results = glsr(Database::class)->dbGetResults($this->$method(), ARRAY_A); |
|
76
|
|
|
return $this->normalizeRatingsByAssignedId($results); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param bool $bypassCache |
|
81
|
|
|
* @param int $postId |
|
82
|
|
|
* @return Review |
|
83
|
|
|
*/ |
|
84
|
13 |
|
public function review($postId, $bypassCache = false) |
|
85
|
|
|
{ |
|
86
|
13 |
|
$reviewId = Cast::toInt($postId); |
|
87
|
|
|
$review = Helper::ifTrue($bypassCache, null, function () use ($reviewId) { |
|
88
|
13 |
|
return glsr(Cache::class)->get($reviewId, 'reviews'); |
|
89
|
13 |
|
}); |
|
90
|
13 |
|
if (!$review instanceof Review) { |
|
91
|
13 |
|
$result = glsr(Database::class)->dbGetRow($this->queryReviews($reviewId), OBJECT); |
|
92
|
13 |
|
$review = new Review($result); |
|
93
|
13 |
|
if ($review->isValid()) { |
|
94
|
13 |
|
glsr(Cache::class)->store($review->ID, 'reviews', $review); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
13 |
|
return $review; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function reviews(array $args = [], array $postIds = []) |
|
104
|
|
|
{ |
|
105
|
1 |
|
$this->setArgs($args); |
|
106
|
1 |
|
if (empty($postIds)) { |
|
107
|
|
|
// We previously used a subquery here, but MariaDB doesn't support it. |
|
108
|
1 |
|
$postIds = glsr(Database::class)->dbGetCol($this->queryReviewIds()); |
|
109
|
|
|
} |
|
110
|
1 |
|
$reviewIds = implode(',', Arr::uniqueInt(Cast::toArray($postIds))); |
|
111
|
1 |
|
$reviewIds = Str::fallback($reviewIds, '0'); // if there are no review IDs, default to 0 |
|
112
|
1 |
|
$results = glsr(Database::class)->dbGetResults($this->queryReviews($reviewIds), OBJECT); |
|
113
|
1 |
|
foreach ($results as &$result) { |
|
114
|
1 |
|
$result = new Review($result); |
|
115
|
1 |
|
glsr(Cache::class)->store($result->ID, 'reviews', $result); |
|
116
|
|
|
} |
|
117
|
1 |
|
return $results; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param int $postId |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
|
|
public function revisionIds($postId) |
|
125
|
|
|
{ |
|
126
|
|
|
return glsr(Database::class)->dbGetCol($this->queryRevisionIds($postId)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
8 |
|
public function setArgs(array $args = [], array $unset = []) |
|
133
|
|
|
{ |
|
134
|
8 |
|
$args = (new NormalizeQueryArgs($args))->toArray(); |
|
135
|
8 |
|
foreach ($unset as $key) { |
|
136
|
7 |
|
$args[$key] = ''; |
|
137
|
|
|
} |
|
138
|
8 |
|
$this->args = $args; |
|
139
|
8 |
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return int |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function totalReviews(array $args = [], array $reviews = []) |
|
145
|
|
|
{ |
|
146
|
1 |
|
$this->setArgs($args, $unset = ['orderby']); |
|
147
|
1 |
|
if (empty($this->sqlLimit()) && !empty($reviews)) { |
|
148
|
|
|
return count($reviews); |
|
149
|
|
|
} |
|
150
|
1 |
|
return (int) glsr(Database::class)->dbGetVar($this->queryTotalReviews()); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return array |
|
155
|
|
|
*/ |
|
156
|
6 |
|
protected function normalizeRatings(array $ratings = []) |
|
157
|
|
|
{ |
|
158
|
6 |
|
$normalized = []; |
|
159
|
6 |
|
foreach ($ratings as $result) { |
|
160
|
6 |
|
$type = $result['type']; |
|
161
|
6 |
|
if (!array_key_exists($type, $normalized)) { |
|
162
|
6 |
|
$normalized[$type] = glsr(Rating::class)->emptyArray(); |
|
163
|
|
|
} |
|
164
|
6 |
|
$normalized[$type] = Arr::set($normalized[$type], $result['rating'], $result['count']); |
|
165
|
|
|
} |
|
166
|
6 |
|
return $normalized; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return array |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function normalizeRatingsByAssignedId(array $ratings = []) |
|
173
|
|
|
{ |
|
174
|
|
|
$normalized = []; |
|
175
|
|
|
foreach ($ratings as $result) { |
|
176
|
|
|
$id = $result['ID']; |
|
177
|
|
|
unset($result['ID']); |
|
178
|
|
|
if (!array_key_exists($id, $normalized)) { |
|
179
|
|
|
$normalized[$id] = []; |
|
180
|
|
|
} |
|
181
|
|
|
$normalized[$id][] = $result; |
|
182
|
|
|
} |
|
183
|
|
|
return array_map([$this, 'normalizeRatings'], $normalized); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @return string |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function queryExport() |
|
190
|
|
|
{ |
|
191
|
|
|
return $this->sql(" |
|
192
|
|
|
SELECT r.*, |
|
193
|
|
|
GROUP_CONCAT(DISTINCT apt.post_id) AS post_ids, |
|
194
|
|
|
GROUP_CONCAT(DISTINCT aut.user_id) AS user_ids |
|
195
|
|
|
FROM {$this->table('ratings')} AS r |
|
196
|
|
|
LEFT JOIN {$this->table('assigned_posts')} AS apt ON r.ID = apt.rating_id |
|
197
|
|
|
LEFT JOIN {$this->table('assigned_users')} AS aut ON r.ID = aut.rating_id |
|
198
|
|
|
GROUP BY r.ID |
|
199
|
|
|
ORDER BY r.ID |
|
200
|
|
|
{$this->sqlLimit()} |
|
201
|
|
|
{$this->sqlOffset()} |
|
202
|
|
|
"); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @return string |
|
207
|
|
|
*/ |
|
208
|
|
|
protected function queryHasRevisions($reviewId) |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->sql($this->db->prepare(" |
|
211
|
|
|
SELECT COUNT(*) |
|
212
|
|
|
FROM {$this->db->posts} |
|
213
|
|
|
WHERE post_type = 'revision' AND post_parent = %d |
|
214
|
|
|
", $reviewId)); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @return string |
|
219
|
|
|
*/ |
|
220
|
|
|
protected function queryImport() |
|
221
|
|
|
{ |
|
222
|
|
|
return $this->sql($this->db->prepare(" |
|
223
|
|
|
SELECT m.post_id, m.meta_value |
|
224
|
|
|
FROM {$this->db->postmeta} AS m |
|
225
|
|
|
INNER JOIN {$this->db->posts} AS p ON m.post_id = p.ID |
|
226
|
|
|
WHERE p.post_type = %s AND m.meta_key = %s |
|
227
|
|
|
ORDER BY m.meta_id |
|
228
|
|
|
{$this->sqlLimit()} |
|
229
|
|
|
{$this->sqlOffset()} |
|
230
|
|
|
", glsr()->post_type, glsr()->export_key)); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @return string |
|
235
|
|
|
*/ |
|
236
|
6 |
|
protected function queryRatings() |
|
237
|
|
|
{ |
|
238
|
6 |
|
return $this->sql(" |
|
239
|
|
|
SELECT r.rating, r.type, COUNT(r.rating) AS count |
|
240
|
6 |
|
FROM {$this->table('ratings')} AS r |
|
241
|
6 |
|
{$this->sqlJoin()} |
|
242
|
6 |
|
{$this->sqlWhere()} |
|
243
|
|
|
GROUP BY r.type, r.rating |
|
244
|
|
|
"); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @return string |
|
249
|
|
|
*/ |
|
250
|
|
|
public function queryRatingsForPostmeta() |
|
251
|
|
|
{ |
|
252
|
|
|
return $this->sql(" |
|
253
|
|
|
SELECT apt.post_id AS ID, r.rating, r.type, COUNT(r.rating) AS count |
|
254
|
|
|
FROM {$this->table('ratings')} AS r |
|
255
|
|
|
INNER JOIN {$this->table('assigned_posts')} AS apt ON r.ID = apt.rating_id |
|
256
|
|
|
WHERE 1=1 |
|
257
|
|
|
{$this->clauseAndStatus()} |
|
258
|
|
|
{$this->clauseAndType()} |
|
259
|
|
|
GROUP BY r.type, r.rating, apt.post_id |
|
260
|
|
|
"); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* @return string |
|
265
|
|
|
*/ |
|
266
|
|
|
protected function queryRatingsForTermmeta() |
|
267
|
|
|
{ |
|
268
|
|
|
return $this->sql(" |
|
269
|
|
|
SELECT att.term_id AS ID, r.rating, r.type, COUNT(r.rating) AS count |
|
270
|
|
|
FROM {$this->table('ratings')} AS r |
|
271
|
|
|
INNER JOIN {$this->table('assigned_terms')} AS att ON r.ID = att.rating_id |
|
272
|
|
|
WHERE 1=1 |
|
273
|
|
|
{$this->clauseAndStatus()} |
|
274
|
|
|
{$this->clauseAndType()} |
|
275
|
|
|
GROUP BY r.type, r.rating, att.term_id |
|
276
|
|
|
"); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* @return string |
|
281
|
|
|
*/ |
|
282
|
|
|
protected function queryRatingsForUsermeta() |
|
283
|
|
|
{ |
|
284
|
|
|
return $this->sql(" |
|
285
|
|
|
SELECT aut.user_id AS ID, r.rating, r.type, COUNT(r.rating) AS count |
|
286
|
|
|
FROM {$this->table('ratings')} AS r |
|
287
|
|
|
INNER JOIN {$this->table('assigned_users')} AS aut ON r.ID = aut.rating_id |
|
288
|
|
|
WHERE 1=1 |
|
289
|
|
|
{$this->clauseAndStatus()} |
|
290
|
|
|
{$this->clauseAndType()} |
|
291
|
|
|
GROUP BY r.type, r.rating, aut.user_id |
|
292
|
|
|
"); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* @return string |
|
297
|
|
|
*/ |
|
298
|
1 |
|
protected function queryReviewIds() |
|
299
|
|
|
{ |
|
300
|
1 |
|
return $this->sql(" |
|
301
|
|
|
SELECT r.review_id |
|
302
|
1 |
|
FROM {$this->table('ratings')} AS r |
|
303
|
1 |
|
{$this->sqlJoin()} |
|
304
|
1 |
|
{$this->sqlWhere()} |
|
305
|
|
|
GROUP BY r.review_id |
|
306
|
1 |
|
{$this->sqlOrderBy()} |
|
307
|
1 |
|
{$this->sqlLimit()} |
|
308
|
1 |
|
{$this->sqlOffset()} |
|
309
|
|
|
"); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* @param int|string $reviewIds |
|
314
|
|
|
* @return string |
|
315
|
|
|
*/ |
|
316
|
13 |
|
protected function queryReviews($reviewIds) |
|
317
|
|
|
{ |
|
318
|
13 |
|
$orderBy = !empty($this->args['order']) ? $this->sqlOrderBy() : ''; |
|
319
|
13 |
|
return $this->sql(" |
|
320
|
|
|
SELECT |
|
321
|
|
|
r.*, |
|
322
|
|
|
p.post_author AS author_id, |
|
323
|
|
|
p.post_date AS date, |
|
324
|
|
|
p.post_content AS content, |
|
325
|
|
|
p.post_title AS title, |
|
326
|
|
|
p.post_status AS status, |
|
327
|
|
|
GROUP_CONCAT(DISTINCT apt.post_id) AS post_ids, |
|
328
|
|
|
GROUP_CONCAT(DISTINCT att.term_id) AS term_ids, |
|
329
|
|
|
GROUP_CONCAT(DISTINCT aut.user_id) AS user_ids |
|
330
|
13 |
|
FROM {$this->table('ratings')} AS r |
|
331
|
13 |
|
INNER JOIN {$this->db->posts} AS p ON r.review_id = p.ID |
|
332
|
13 |
|
LEFT JOIN {$this->table('assigned_posts')} AS apt ON r.ID = apt.rating_id |
|
333
|
13 |
|
LEFT JOIN {$this->table('assigned_terms')} AS att ON r.ID = att.rating_id |
|
334
|
13 |
|
LEFT JOIN {$this->table('assigned_users')} AS aut ON r.ID = aut.rating_id |
|
335
|
13 |
|
WHERE r.review_id IN ({$reviewIds}) |
|
336
|
|
|
GROUP BY r.ID |
|
337
|
13 |
|
{$orderBy} |
|
338
|
|
|
"); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* @return string |
|
343
|
|
|
*/ |
|
344
|
|
|
protected function queryRevisionIds($reviewId) |
|
345
|
|
|
{ |
|
346
|
|
|
return $this->sql($this->db->prepare(" |
|
347
|
|
|
SELECT ID |
|
348
|
|
|
FROM {$this->db->posts} |
|
349
|
|
|
WHERE post_type = 'revision' AND post_parent = %d |
|
350
|
|
|
", $reviewId)); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* @return string |
|
355
|
|
|
*/ |
|
356
|
1 |
|
protected function queryTotalReviews() |
|
357
|
|
|
{ |
|
358
|
1 |
|
return $this->sql(" |
|
359
|
|
|
SELECT COUNT(DISTINCT r.ID) AS count |
|
360
|
1 |
|
FROM {$this->table('ratings')} AS r |
|
361
|
1 |
|
{$this->sqlJoin()} |
|
362
|
1 |
|
{$this->sqlWhere()} |
|
363
|
|
|
"); |
|
364
|
|
|
} |
|
365
|
|
|
} |
|
366
|
|
|
|