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