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 |