PluginProbe ʕ •ᴥ•ʔ
Download Manager / trunk
Download Manager vtrunk
3.3.62 3.3.61 3.3.60 3.3.59 3.3.58 3.3.57 3.3.56 trunk 2.1.3 2.3.0 2.5.96 2.5.97 2.6.2 2.6.96 2.8.3 2.9.99 3.0.4 3.1.05 3.1.07 3.1.08 3.1.11 3.1.12 3.1.14 3.1.17 3.1.18 3.1.22 3.1.23 3.1.24 3.1.25 3.1.26 3.1.27 3.1.28 3.2.04 3.2.13 3.2.14 3.2.16 3.2.18 3.2.19 3.2.21 3.2.22 3.2.23 3.2.24 3.2.25 3.2.27 3.2.28 3.2.29 3.2.30 3.2.31 3.2.32 3.2.33 3.2.34 3.2.35 3.2.37 3.2.38 3.2.39 3.2.40 3.2.41 3.2.42 3.2.43 3.2.44 3.2.45 3.2.46 3.2.47 3.2.48 3.2.49 3.2.50 3.2.51 3.2.52 3.2.53 3.2.54 3.2.55 3.2.56 3.2.57 3.2.58 3.2.59 3.2.60 3.2.61 3.2.63 3.2.64 3.2.65 3.2.66 3.2.67 3.2.68 3.2.69 3.2.70 3.2.71 3.2.72 3.2.73 3.2.74 3.2.75 3.2.76 3.2.77 3.2.78 3.2.79 3.2.80 3.2.81 3.2.82 3.2.83 3.2.84 3.2.85 3.2.86 3.2.87 3.2.88 3.2.89 3.2.90 3.2.91 3.2.92 3.2.93 3.2.94 3.2.95 3.2.96 3.2.97 3.2.98 3.2.99 3.3.00 3.3.01 3.3.02 3.3.03 3.3.04 3.3.05 3.3.06 3.3.07 3.3.08 3.3.09 3.3.10 3.3.11 3.3.12 3.3.13 3.3.14 3.3.15 3.3.16 3.3.17 3.3.18 3.3.19 3.3.20 3.3.21 3.3.22 3.3.23 3.3.24 3.3.25 3.3.26 3.3.27 3.3.28 3.3.29 3.3.30 3.3.31 3.3.32 3.3.33 3.3.34 3.3.35 3.3.36 3.3.37 3.3.38 3.3.39 3.3.40 3.3.41 3.3.42 3.3.43 3.3.44 3.3.45 3.3.46 3.3.47 3.3.48 3.3.49 3.3.50 3.3.51 3.3.52 3.3.53 3.3.54 3.3.55
download-manager / src / __ / Query.php
download-manager / src / __ Last commit date
HTML 1 year ago views 5 months ago Apply.php 6 months ago Cron.php 1 year ago CronJob.php 7 months ago CronJobs.php 2 months ago Crypt.php 1 month ago DownloadStats.php 5 months ago Email.php 4 days ago EmailCron.php 1 year ago FileSystem.php 1 year ago Installer.php 5 hours ago Messages.php 1 year ago Query.php 4 months ago Session.php 5 hours ago Settings.php 4 years ago SimpleMath.php 4 years ago TempStorage.php 5 hours ago Template.php 5 months ago UI.php 6 months ago Updater.php 4 years ago UserAgent.php 2 years ago __.php 1 month ago __MailUI.php 3 years ago
Query.php
440 lines
1 <?php
2 /**
3 * Query class for wpdm packages
4 */
5
6 namespace WPDM\__;
7
8
9 class Query
10 {
11 /**
12 * @var string Query only wpdm packages
13 */
14 private $post_type = "wpdmpro";
15 /**
16 * @var array Query parameters
17 */
18 public $params = [];
19 /**
20 * @var array Query $result
21 */
22 public $result;
23 /**
24 * @var array Found packages
25 */
26 public $packages = [];
27 /**
28 * @var int Total count
29 */
30 public $count = 0;
31
32 /**
33 * @var string WPDM Tag
34 */
35 public $tag_tax = WPDM_TAG;
36
37 public $tax_relation = null;
38
39
40 function __construct()
41 {
42
43 }
44
45 /**
46 * Static factory method for convenient instantiation
47 * @return Query
48 */
49 static function create()
50 {
51 return new self();
52 }
53
54 /**
55 * Reset query for reuse
56 * @return $this
57 */
58 function reset()
59 {
60 $this->params = [];
61 $this->result = null;
62 $this->packages = [];
63 $this->count = 0;
64 $this->tax_relation = null;
65 return $this;
66 }
67
68 /**
69 * @param int $items_per_page
70 */
71 function items_per_page($items_per_page = 10)
72 {
73 if($items_per_page)
74 $this->params['posts_per_page'] = $items_per_page;
75 return $this;
76 }
77
78 /**
79 * @param string $order_field
80 * @param string $order
81 */
82 function sort($order_field = 'date', $order = 'DESC')
83 {
84
85 if($order_field === 'downloads') $order_field = 'download_count';
86 if($order_field === 'views') $order_field = 'view_count';
87 if(in_array($order_field, ['update_date', 'updated'])) $order_field = 'modified';
88
89 $order_fields = ['__wpdm_download_count', '__wpdm_view_count', '__wpdm_package_size_b'];
90 if (!in_array("__wpdm_" . $order_field, $order_fields)) {
91 $this->params['orderby'] = $order_field;
92 $this->params['order'] = $order;
93 } else {
94 $this->params['orderby'] = 'meta_value_num';
95 $this->params['meta_key'] = "__wpdm_" . $order_field;
96 $this->params['order'] = $order;
97 }
98 return $this;
99
100 }
101
102 /**
103 * @param $taxonomy
104 * @param $terms
105 * @param string $field
106 * @param string $operator
107 * @param false $include_children
108 */
109 function taxonomy($taxonomy, $terms, $field = 'slug', $operator = 'IN', $include_children = null)
110 {
111 if (!isset($this->params['tax_query']) || !is_array($this->params['tax_query'])) $this->params['tax_query'] = [];
112 if (!is_array($terms)) $terms = explode(",", $terms);
113 $tax_query = [
114 'taxonomy' => $taxonomy,
115 'field' => $field,
116 'terms' => $terms,
117 ];
118 if ($include_children !== null && $include_children !== '')
119 $tax_query['include_children'] = $include_children;
120 if ($operator !== 'IN')
121 $tax_query['operator'] = $operator;
122 if( isset( $tax_query['operator'] ) && $tax_query['operator'] === '' )
123 $tax_query['operator'] = 'IN';
124 if ($taxonomy === 'wpdmcategory') {
125 array_unshift($this->params['tax_query'], $tax_query);
126 } else
127 $this->params['tax_query'][] = $tax_query;
128 return $this;
129 }
130
131 /**
132 * @param string $relation
133 */
134 function taxonomy_relation($relation = 'OR')
135 {
136 $this->tax_relation = $relation;
137 return $this;
138 }
139
140 /**
141 * @param null $categories
142 * @param string $field
143 * @param string $operator
144 * @param false $include_children
145 */
146 function categories($categories = null, $field = 'slug', $operator = 'IN', $include_children = false)
147 {
148 if ($categories) {
149 $this->taxonomy('wpdmcategory', $categories, $field, $operator, $include_children);
150 }
151 return $this;
152 }
153
154 /**
155 * Exclude categories
156 * @param null $categories
157 * @param string $field
158 */
159 function xcats($categories = null, $field = 'slug')
160 {
161 if ($categories) {
162 if($field === 'slug') {
163 if(!is_array($categories)) $categories = explode(",", $categories);
164 // Batch lookup instead of N+1 queries
165 $terms = get_terms([
166 'taxonomy' => 'wpdmcategory',
167 'slug' => $categories,
168 'fields' => 'ids',
169 'hide_empty' => false,
170 ]);
171 if (!is_wp_error($terms) && !empty($terms)) {
172 $categories = $terms;
173 }
174 $field = 'term_id';
175 }
176 $this->taxonomy('wpdmcategory', $categories, $field, 'NOT IN');
177 }
178 return $this;
179 }
180
181 /**
182 * @param null $tags
183 * @param string $field
184 * @param string $operator
185 */
186 function tags($tags = null, $field = 'slug', $operator = 'IN')
187 {
188 if ($tags) {
189 $this->taxonomy($this->tag_tax, $tags, $field, $operator);
190 }
191 return $this;
192 }
193
194 /**
195 * @param null $tags
196 * @param string $field
197 */
198 function tag__and($tags = null, $field = 'slug')
199 {
200 if ($tags) {
201 $this->taxonomy($this->tag_tax, $tags, $field, 'AND');
202 }
203 return $this;
204 }
205
206 /**
207 * @param null $tags
208 */
209 function tag_slug__and($tags = null)
210 {
211 if ($tags) {
212 $this->taxonomy($this->tag_tax, $tags, 'slug', 'AND');
213 }
214 return $this;
215 }
216
217 /**
218 * @param null $tags
219 * @param string $field
220 */
221 function tag__in($tags = null, $field = 'slug')
222 {
223 if ($tags) {
224 $this->taxonomy($this->tag_tax, $tags, $field, 'IN');
225 }
226 return $this;
227 }
228
229 /**
230 * @param null $tags
231 */
232 function tag_slug__in($tags = null)
233 {
234 if ($tags) {
235 $this->taxonomy($this->tag_tax, $tags, 'slug', 'IN');
236 }
237 return $this;
238 }
239
240 /**
241 * @param null $tags
242 * @param string $field
243 */
244 function tag__not_in($tags = null, $field = 'slug')
245 {
246 if ($tags) {
247 $this->taxonomy($this->tag_tax, $tags, $field, 'NOT IN');
248 }
249 return $this;
250 }
251
252 /**
253 * @param $key
254 * @param $value
255 * @param string $compare
256 */
257 function meta($key, $value, $compare = 'LIKE')
258 {
259 if (!isset($this->params['meta_query']) || !is_array($this->params['meta_query'])) $this->params['meta_query'] = [];
260 $this->params['meta_query'][] = [
261 'key' => $key,
262 'value' => $value,
263 'compare' => $compare
264 ];
265 return $this;
266 }
267
268 /**
269 * @param string $relation
270 */
271 function meta_relation($relation = 'OR')
272 {
273 $this->params['meta_query']['relation'] = $relation;
274 return $this;
275 }
276
277 /**
278 * From date filter
279 * @param null $date
280 * @param bool $modified Use post_modified_gmt instead of post_date
281 * @return $this
282 */
283 function from_date($date = null, $modified = false)
284 {
285 if($date)
286 {
287 $date = explode("-", $date);
288 $this->params['date_query']['inclusive'] = true;
289 if($modified)
290 $this->params['date_query']['column'] = 'post_modified_gmt';
291 $this->params['date_query']['after']['year'] = $date[0];
292 if(isset($date[1]))
293 $this->params['date_query']['after']['month'] = $date[1];
294 if(isset($date[2]))
295 $this->params['date_query']['after']['day'] = $date[2];
296 }
297 return $this;
298 }
299
300 /**
301 * To date filter
302 * @param null $date
303 * @param bool $modified Use post_modified_gmt instead of post_date
304 * @return $this
305 */
306 function to_date($date = null, $modified = false)
307 {
308 if($date)
309 {
310 $date = explode("-", $date);
311 $this->params['date_query']['inclusive'] = true;
312 if($modified)
313 $this->params['date_query']['column'] = 'post_modified_gmt';
314 $this->params['date_query']['before']['year'] = $date[0];
315 if(isset($date[1]))
316 $this->params['date_query']['before']['month'] = $date[1];
317 if(isset($date[2]))
318 $this->params['date_query']['before']['day'] = $date[2];
319 }
320 return $this;
321 }
322
323 /**
324 * @param $field
325 * @param $value
326 */
327 function filter($field, $value)
328 {
329 $this->params[$field] = $value;
330 return $this;
331 }
332
333 /**
334 * @param $keyword
335 * @return $this
336 */
337 function s($keyword)
338 {
339 $this->params['s'] = $keyword;
340 return $this;
341 }
342
343 /**
344 * @param $keyword
345 * @return $this
346 */
347 function search($keyword)
348 {
349 $this->params['s'] = $keyword;
350 return $this;
351 }
352
353 /**
354 * @param $paged
355 * @return $this
356 */
357 function paged($paged)
358 {
359 if ($paged <= 1) return $this;
360 $this->params['paged'] = $paged;
361 return $this;
362 }
363
364 /**
365 * @param $author_id
366 * @return $this
367 */
368 function author($author_id)
369 {
370 $this->params['author'] = $author_id;
371 return $this;
372 }
373
374 /**
375 * @param $author_id
376 * @return $this
377 */
378 function post_status($status)
379 {
380 $this->params['post_status'] = $status;
381 return $this;
382 }
383
384 /**
385 * @param $author_name
386 * @return $this
387 */
388 function author_name($author_name)
389 {
390 $this->params['author_name'] = $author_name;
391 return $this;
392 }
393
394 /**
395 * @param $author__not_in
396 * @return $this
397 */
398 function author__not_in($author__not_in)
399 {
400 $this->params['author__not_in'] = $author__not_in;
401 return $this;
402 }
403
404 /**
405 * @return $this
406 */
407 function process()
408 {
409 if($this->tax_relation && isset($this->params['tax_query']) && is_array($this->params['tax_query']))
410 $this->params['tax_query'] = ['relation' => $this->tax_relation] + $this->params['tax_query'];
411
412 $this->params = apply_filters('wpdm_packages_query_params', $this->params);
413 $this->params['post_type'] = $this->post_type;
414 $this->params['suppress_filters'] = true;
415 $this->result = new \WP_Query($this->params);
416 $this->packages = $this->result->posts;
417 $this->count = $this->result->found_posts;
418
419 // Prime caches for better performance when accessing package data
420 if (!empty($this->packages)) {
421 $post_ids = wp_list_pluck($this->packages, 'ID');
422 update_postmeta_cache($post_ids);
423 update_object_term_cache($post_ids, $this->post_type);
424 }
425
426 wp_reset_postdata();
427 return $this;
428 }
429
430 /**
431 * @return array
432 */
433 function packages()
434 {
435 return $this->packages;
436 }
437
438
439 }
440