1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Database; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
6
|
|
|
use GeminiLabs\SiteReviews\Helper; |
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Polylang; |
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Rating; |
11
|
|
|
use WP_Query; |
12
|
|
|
|
13
|
|
|
class QueryBuilder |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Build a WP_Query meta_query/tax_query. |
17
|
|
|
* @return array |
18
|
|
|
*/ |
19
|
|
|
public function buildQuery(array $keys = [], array $values = []) |
20
|
|
|
{ |
21
|
|
|
$queries = []; |
22
|
|
|
foreach ($keys as $key) { |
23
|
|
|
if (!array_key_exists($key, $values)) { |
24
|
|
|
continue; |
25
|
|
|
} |
26
|
|
|
$methodName = Helper::buildMethodName($key, __FUNCTION__); |
27
|
|
|
if (!method_exists($this, $methodName)) { |
28
|
|
|
continue; |
29
|
|
|
} |
30
|
|
|
$query = call_user_func([$this, $methodName], $values[$key]); |
31
|
|
|
if (is_array($query)) { |
32
|
|
|
$queries[] = $query; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
return $queries; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function buildSqlLines(array $values, array $conditions) |
42
|
|
|
{ |
43
|
|
|
$string = ''; |
44
|
|
|
$values = array_filter($values); |
45
|
|
|
foreach ($conditions as $key => $value) { |
46
|
|
|
if (!isset($values[$key])) { |
47
|
|
|
continue; |
48
|
|
|
} |
49
|
|
|
$values[$key] = implode(',', (array) $values[$key]); |
50
|
|
|
$string.= Str::contains($value, '%s') |
51
|
|
|
? sprintf($value, strval($values[$key])) |
52
|
|
|
: $value; |
53
|
|
|
} |
54
|
|
|
return $string; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Build a SQL 'OR' string from an array. |
59
|
|
|
* @param string|array $values |
60
|
|
|
* @param string $sprintfFormat |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function buildSqlOr($values, $sprintfFormat) |
64
|
|
|
{ |
65
|
|
|
if (!is_array($values)) { |
66
|
|
|
$values = explode(',', $values); |
67
|
|
|
} |
68
|
|
|
$values = array_filter(array_map('trim', (array) $values)); |
69
|
|
|
$values = array_map(function ($value) use ($sprintfFormat) { |
70
|
|
|
return sprintf($sprintfFormat, $value); |
71
|
|
|
}, $values); |
72
|
|
|
return implode(' OR ', $values); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Search SQL filter for matching against post title only. |
77
|
|
|
* @see http://wordpress.stackexchange.com/a/11826/1685 |
78
|
|
|
* @param string $search |
79
|
|
|
* @return string |
80
|
|
|
* @filter posts_search |
81
|
|
|
*/ |
82
|
|
|
public function filterSearchByTitle($search, WP_Query $query) |
83
|
|
|
{ |
84
|
|
|
if (empty($search) || empty($query->get('search_terms'))) { |
85
|
|
|
return $search; |
86
|
|
|
} |
87
|
|
|
global $wpdb; |
88
|
|
|
$n = empty($query->get('exact')) |
89
|
|
|
? '%' |
90
|
|
|
: ''; |
91
|
|
|
$search = []; |
92
|
|
|
foreach ((array) $query->get('search_terms') as $term) { |
93
|
|
|
$search[] = $wpdb->prepare("{$wpdb->posts}.post_title LIKE %s", $n.$wpdb->esc_like($term).$n); |
94
|
|
|
} |
95
|
|
|
if (!is_user_logged_in()) { |
96
|
|
|
$search[] = "{$wpdb->posts}.post_password = ''"; |
97
|
|
|
} |
98
|
|
|
return ' AND '.implode(' AND ', $search); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the current page number from the global query. |
103
|
|
|
* @param bool $isEnabled |
104
|
|
|
* @return int |
105
|
|
|
*/ |
106
|
|
|
public function getPaged($isEnabled = true) |
107
|
|
|
{ |
108
|
|
|
return $isEnabled |
109
|
|
|
? max(1, intval(filter_input(INPUT_GET, glsr()->constant('PAGED_QUERY_VAR')))) |
110
|
|
|
: 1; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $value |
115
|
|
|
* @return void|array |
116
|
|
|
*/ |
117
|
|
|
protected function buildQueryAssignedTo($value) |
118
|
|
|
{ |
119
|
|
|
if (!empty($value)) { |
120
|
|
|
$postIds = Arr::convertStringToArray($value, 'is_numeric'); |
121
|
|
|
return [ |
122
|
|
|
'compare' => 'IN', |
123
|
|
|
'key' => '_assigned_to', |
124
|
|
|
'value' => glsr(Polylang::class)->getPostIds($postIds), |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param array $value |
131
|
|
|
* @return void|array |
132
|
|
|
*/ |
133
|
|
|
protected function buildQueryCategory($value) |
134
|
|
|
{ |
135
|
|
|
if (!empty($value)) { |
136
|
|
|
return [ |
137
|
|
|
'field' => 'term_id', |
138
|
|
|
'taxonomy' => Application::TAXONOMY, |
139
|
|
|
'terms' => $value, |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $value |
146
|
|
|
* @return void|array |
147
|
|
|
*/ |
148
|
|
|
protected function buildQueryEmail($value) |
149
|
|
|
{ |
150
|
|
|
if (!empty($value)) { |
151
|
|
|
return [ |
152
|
|
|
'key' => '_email', |
153
|
|
|
'value' => $value, |
154
|
|
|
]; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $value |
160
|
|
|
* @return void|array |
161
|
|
|
*/ |
162
|
|
|
protected function buildQueryIpAddress($value) |
163
|
|
|
{ |
164
|
|
|
if (!empty($value)) { |
165
|
|
|
return [ |
166
|
|
|
'key' => '_ip_address', |
167
|
|
|
'value' => $value, |
168
|
|
|
]; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $value |
174
|
|
|
* @return void|array |
175
|
|
|
*/ |
176
|
|
|
protected function buildQueryRating($value) |
177
|
|
|
{ |
178
|
|
|
if (is_numeric($value) |
179
|
|
|
&& in_array(intval($value), range(1, glsr()->constant('MAX_RATING', Rating::class)))) { |
180
|
|
|
return [ |
181
|
|
|
'compare' => '>=', |
182
|
|
|
'key' => '_rating', |
183
|
|
|
'value' => $value, |
184
|
|
|
]; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $value |
190
|
|
|
* @return void|array |
191
|
|
|
*/ |
192
|
|
|
protected function buildQueryType($value) |
193
|
|
|
{ |
194
|
|
|
if (!in_array($value, ['', 'all'])) { |
195
|
|
|
return [ |
196
|
|
|
'key' => '_review_type', |
197
|
|
|
'value' => $value, |
198
|
|
|
]; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|