1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Database; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Arguments; |
6
|
|
|
use GeminiLabs\SiteReviews\Defaults\ReviewsDefaults; |
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
9
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Sanitizer; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @property int[] $assigned_posts; |
14
|
|
|
* @property int[] $assigned_terms; |
15
|
|
|
* @property int[] $assigned_users; |
16
|
|
|
* @property string|array $date; |
17
|
|
|
* @property string $email; |
18
|
|
|
* @property string $ip_address; |
19
|
|
|
* @property int $offset; |
20
|
|
|
* @property string $order; |
21
|
|
|
* @property string $orderby; |
22
|
|
|
* @property int $page; |
23
|
|
|
* @property string $pagination; |
24
|
|
|
* @property int $per_page; |
25
|
|
|
* @property int[] $post__in; |
26
|
|
|
* @property int[] $post__not_in; |
27
|
|
|
* @property int $rating; |
28
|
|
|
* @property string $rating_field; |
29
|
|
|
* @property string $status; |
30
|
|
|
* @property string $terms; |
31
|
|
|
* @property string $type; |
32
|
|
|
* @property int[] $user__in; |
33
|
|
|
* @property int[] $user__not_in; |
34
|
|
|
*/ |
35
|
|
|
class NormalizeQueryArgs extends Arguments |
36
|
|
|
{ |
37
|
21 |
|
public function __construct(array $args = []) |
38
|
|
|
{ |
39
|
21 |
|
$args = glsr(ReviewsDefaults::class)->restrict($args); |
40
|
21 |
|
$args['assigned_posts'] = glsr(PostManager::class)->normalizeIds($args['assigned_posts']); |
41
|
21 |
|
$args['assigned_terms'] = glsr(TaxonomyManager::class)->normalizeIds($args['assigned_terms']); |
42
|
21 |
|
$args['assigned_users'] = glsr(UserManager::class)->normalizeIds($args['assigned_users']); |
43
|
21 |
|
$args['date'] = $this->normalizeDate($args['date']); |
44
|
21 |
|
$args['order'] = Str::restrictTo('ASC,DESC,', sanitize_key($args['order']), 'DESC'); // include an empty value |
45
|
21 |
|
$args['orderby'] = $this->normalizeOrderBy($args['orderby']); |
46
|
21 |
|
$args['status'] = $this->normalizeStatus($args['status']); |
47
|
21 |
|
$args['user__in'] = glsr(UserManager::class)->normalizeIds($args['user__in']); |
48
|
21 |
|
$args['user__not_in'] = glsr(UserManager::class)->normalizeIds($args['user__not_in']); |
49
|
21 |
|
parent::__construct($args); |
50
|
21 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string|array $value |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
21 |
|
protected function normalizeDate($value) |
57
|
|
|
{ |
58
|
21 |
|
$date = array_fill_keys(['after', 'before', 'day', 'inclusive', 'month', 'year'], ''); |
59
|
21 |
|
$timestamp = strtotime(Cast::toString($value)); |
60
|
21 |
|
if (false !== $timestamp) { |
61
|
|
|
$date['year'] = date('Y', $timestamp); |
62
|
|
|
$date['month'] = date('n', $timestamp); |
63
|
|
|
$date['day'] = date('j', $timestamp); |
64
|
|
|
return $date; |
65
|
|
|
} |
66
|
21 |
|
$date['after'] = glsr(Sanitizer::class)->sanitizeDate(Arr::get($value, 'after')); |
67
|
21 |
|
$date['before'] = glsr(Sanitizer::class)->sanitizeDate(Arr::get($value, 'before')); |
68
|
21 |
|
if (!empty(array_filter($date))) { |
69
|
|
|
$date['inclusive'] = Cast::toBool(Arr::get($value, 'inclusive')) ? '=' : ''; |
70
|
|
|
} |
71
|
21 |
|
return $date; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $value |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
21 |
|
protected function normalizeOrderBy($value) |
79
|
|
|
{ |
80
|
21 |
|
$value = strtolower($value); |
81
|
21 |
|
$orderBy = Str::restrictTo('author,comment_count,date,date_gmt,id,menu_order,none,random,rating', $value, 'date'); |
82
|
21 |
|
if ('id' === $orderBy) { |
83
|
|
|
return 'p.ID'; |
84
|
|
|
} |
85
|
21 |
|
if (in_array($orderBy, ['comment_count', 'menu_order'])) { |
86
|
|
|
return Str::prefix($orderBy, 'p.'); |
87
|
|
|
} |
88
|
21 |
|
if (in_array($orderBy, ['author', 'date', 'date_gmt'])) { |
89
|
21 |
|
return Str::prefix($orderBy, 'p.post_'); |
90
|
|
|
} |
91
|
|
|
if (in_array($orderBy, ['rating'])) { |
92
|
|
|
return Str::prefix($orderBy, 'r.'); |
93
|
|
|
} |
94
|
|
|
return $orderBy; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $value |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
21 |
|
protected function normalizeStatus($value) |
102
|
|
|
{ |
103
|
|
|
$statuses = [ |
104
|
21 |
|
'all' => '', |
105
|
|
|
'approved' => '1', |
106
|
|
|
'pending' => '0', |
107
|
|
|
'publish' => '1', |
108
|
|
|
'unapproved' => '0', |
109
|
|
|
]; |
110
|
21 |
|
$status = Str::restrictTo(array_keys($statuses), $value, 'approved', $strict = true); |
111
|
21 |
|
return $statuses[$status]; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|