|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Database; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Arguments; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Defaults\ReviewsDefaults; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
|
10
|
|
|
|
|
11
|
|
|
class NormalizeQueryArgs extends Arguments |
|
12
|
|
|
{ |
|
13
|
|
|
public $assigned_to; |
|
14
|
|
|
public $category; |
|
15
|
|
|
public $offset; |
|
16
|
|
|
public $order; |
|
17
|
|
|
public $orderby; |
|
18
|
|
|
public $page; |
|
19
|
|
|
public $per_page; |
|
20
|
|
|
public $post__in; |
|
21
|
|
|
public $post__not_in; |
|
22
|
|
|
public $rating; |
|
23
|
|
|
public $type; |
|
24
|
|
|
public $user; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(array $args = []) |
|
27
|
|
|
{ |
|
28
|
|
|
$args = glsr(ReviewsDefaults::class)->merge($args); |
|
29
|
|
|
$args['assigned_to'] = Arr::uniqueInt(Arr::consolidate($args['assigned_to'])); |
|
30
|
|
|
$args['category'] = $this->normalizeTermIds($args['category']); |
|
31
|
|
|
$args['offset'] = absint(filter_var($args['offset'], FILTER_SANITIZE_NUMBER_INT)); |
|
32
|
|
|
$args['order'] = Str::restrictTo('ASC,DESC,', sanitize_key($args['order']), 'DESC'); // include an empty value |
|
33
|
|
|
$args['orderby'] = $this->normalizeOrderBy($args['orderby']); |
|
34
|
|
|
$args['page'] = absint($args['page']); |
|
35
|
|
|
$args['per_page'] = absint($args['per_page']); // "0" and "-1" = all |
|
36
|
|
|
$args['post__in'] = Arr::uniqueInt(Arr::consolidate($args['post__in'])); |
|
37
|
|
|
$args['post__not_in'] = Arr::uniqueInt(Arr::consolidate($args['post__not_in'])); |
|
38
|
|
|
$args['rating'] = absint(filter_var($args['rating'], FILTER_SANITIZE_NUMBER_INT)); |
|
39
|
|
|
$args['type'] = sanitize_key($args['type']); |
|
40
|
|
|
$args['user'] = $this->normalizeUserIds(Arr::consolidate($args['user'])); |
|
41
|
|
|
parent::__construct($args); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function normalizeOrderBy($orderBy) |
|
48
|
|
|
{ |
|
49
|
|
|
$orderBy = Str::restrictTo('author,comment_count,date,ID,menu_order,none,rand,relevance', $orderBy, 'date'); |
|
50
|
|
|
if (in_array($orderBy, ['comment_count', 'ID', 'menu_order'])) { |
|
51
|
|
|
return Str::prefix('p.', $orderBy); |
|
52
|
|
|
} |
|
53
|
|
|
if (in_array($orderBy, ['author', 'date'])) { |
|
54
|
|
|
return Str::prefix('p.post_', $orderBy); |
|
55
|
|
|
} |
|
56
|
|
|
return $orderBy; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param array[]|string $termIds |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
|
|
public function normalizeTermIds($termIds) |
|
64
|
|
|
{ |
|
65
|
|
|
return glsr(ReviewManager::class)->normalizeTermIds($termIds); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function normalizeUserIds(array $users) |
|
72
|
|
|
{ |
|
73
|
|
|
$userIds = []; |
|
74
|
|
|
foreach ($users as $userId) { |
|
75
|
|
|
if (!is_numeric($userId)) { |
|
76
|
|
|
$userId = Helper::castToInt(username_exists($userId)); |
|
77
|
|
|
} |
|
78
|
|
|
$userIds[] = $userId; |
|
79
|
|
|
} |
|
80
|
|
|
return Arr::uniqueInt($userIds); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|