| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class to build up query args. |
| 4 |
* |
| 5 |
* @package RadiusTheme\SB |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace RadiusTheme\SB\Models; |
| 9 |
|
| 10 |
// Do not allow directly accessing this file. |
| 11 |
use RadiusTheme\SB\Helpers\Fns; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit( 'This script cannot be accessed directly.' ); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Query Args Class |
| 19 |
*/ |
| 20 |
class QueryArgs { |
| 21 |
|
| 22 |
/** |
| 23 |
* Query Args. |
| 24 |
* |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
private $args = []; |
| 28 |
|
| 29 |
/** |
| 30 |
* Meta values. |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
private $meta = []; |
| 35 |
|
| 36 |
/** |
| 37 |
* Method to build args |
| 38 |
* |
| 39 |
* @param array $meta Meta values. |
| 40 |
* @param string $type Query type. |
| 41 |
* @param bool $isCarousel Layout type. |
| 42 |
* |
| 43 |
* @return array |
| 44 |
*/ |
| 45 |
public function buildArgs( array $meta, string $type = 'product', bool $isCarousel = false ) { |
| 46 |
$this->meta = $meta; |
| 47 |
|
| 48 |
if ( 'product' === $type ) { |
| 49 |
// Post Type. |
| 50 |
$this->getPostType(); |
| 51 |
|
| 52 |
// Building Args. |
| 53 |
$this |
| 54 |
->post_params() |
| 55 |
->order_params() |
| 56 |
->pagination_params( $isCarousel ) |
| 57 |
->tax_params(); |
| 58 |
|
| 59 |
} elseif ( 'wc_product' === $type ) { |
| 60 |
$this |
| 61 |
->wc_post_params() |
| 62 |
->wc_order_params() |
| 63 |
->wc_pagination_params( $isCarousel ) |
| 64 |
->wc_tax_params(); |
| 65 |
|
| 66 |
} else { |
| 67 |
$this |
| 68 |
->get_tax_type() |
| 69 |
->cat_params() |
| 70 |
->cat_order_params(); |
| 71 |
} |
| 72 |
|
| 73 |
return apply_filters( 'rtsb/elements/' . $type . '_query_args', $this->args, $this->meta ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Post type. |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
private function getPostType() { |
| 82 |
$this->args['post_type'] = [ rtsb()->post_type ]; |
| 83 |
$this->args['post_status'] = 'draft'; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Post type. |
| 88 |
* |
| 89 |
* @return QueryArgs |
| 90 |
*/ |
| 91 |
private function get_tax_type() { |
| 92 |
$this->args['taxonomy'] = [ 'product_cat' ]; |
| 93 |
$this->args['post_status'] = 'publish'; |
| 94 |
$this->args['hierarchical'] = 1; |
| 95 |
|
| 96 |
return $this; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Post parameters. |
| 101 |
* |
| 102 |
* @return QueryArgs |
| 103 |
*/ |
| 104 |
private function post_params() { |
| 105 |
$post_in = isset( $this->meta['post_in'] ) ? sanitize_text_field( implode( ', ', $this->meta['post_in'] ) ) : null; |
| 106 |
$post_not_in = isset( $this->meta['post_not_in'] ) ? sanitize_text_field( implode( ', ', $this->meta['post_not_in'] ) ) : null; |
| 107 |
$author_in = isset( $this->meta['author_in'] ) ? sanitize_text_field( implode( ', ', $this->meta['author_in'] ) ) : null; |
| 108 |
$limit = ( ( empty( $this->meta['limit'] ) || '-1' === $this->meta['limit'] ) ? 10000000 : absint( $this->meta['limit'] ) ); |
| 109 |
$offset = ! empty( $this->meta['offset'] ) ? absint( $this->meta['offset'] ) : 0; |
| 110 |
|
| 111 |
if ( $post_in ) { |
| 112 |
$post_in = explode( ',', $post_in ); |
| 113 |
$this->args['post__in'] = $post_in; |
| 114 |
} |
| 115 |
|
| 116 |
if ( $post_not_in ) { |
| 117 |
$post_not_in = explode( ',', $post_not_in ); |
| 118 |
$this->args['post__not_in'] = $post_not_in; |
| 119 |
} |
| 120 |
|
| 121 |
if ( $author_in ) { |
| 122 |
$author_in = explode( ',', $author_in ); |
| 123 |
$this->args['author__in'] = $author_in; |
| 124 |
} |
| 125 |
|
| 126 |
$this->args['posts_per_page'] = $limit; |
| 127 |
|
| 128 |
if ( $offset ) { |
| 129 |
$this->args['offset'] = $offset; |
| 130 |
} |
| 131 |
|
| 132 |
return $this; |
| 133 |
} |
| 134 |
/** |
| 135 |
* WC Post parameters. |
| 136 |
* |
| 137 |
* @return QueryArgs |
| 138 |
*/ |
| 139 |
private function wc_post_params() { |
| 140 |
$post_in = isset( $this->meta['post_in'] ) ? sanitize_text_field( implode( ',', $this->meta['post_in'] ) ) : null; |
| 141 |
$post_not_in = isset( $this->meta['post_not_in'] ) ? sanitize_text_field( implode( ', ', $this->meta['post_not_in'] ) ) : null; |
| 142 |
$author_in = isset( $this->meta['author_in'] ) ? sanitize_text_field( implode( ',', $this->meta['author_in'] ) ) : null; |
| 143 |
$limit = ( ( empty( $this->meta['limit'] ) || '-1' === $this->meta['limit'] ) ? 10000000 : absint( $this->meta['limit'] ) ); |
| 144 |
$offset = ! empty( $this->meta['offset'] ) ? absint( $this->meta['offset'] ) : 0; |
| 145 |
|
| 146 |
if ( $post_in ) { |
| 147 |
$post_in = explode( ',', $post_in ); |
| 148 |
$this->args['include'] = $post_in; |
| 149 |
} |
| 150 |
|
| 151 |
if ( $post_not_in ) { |
| 152 |
$post_not_in = explode( ',', $post_not_in ); |
| 153 |
$this->args['exclude'] = $post_not_in; |
| 154 |
} |
| 155 |
|
| 156 |
if ( $author_in ) { |
| 157 |
$this->args['author'] = $author_in; |
| 158 |
} |
| 159 |
|
| 160 |
$this->args['limit'] = $limit; |
| 161 |
|
| 162 |
if ( $offset ) { |
| 163 |
$this->args['offset'] = $offset; |
| 164 |
} |
| 165 |
|
| 166 |
return $this; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Category parameters. |
| 171 |
* |
| 172 |
* @return QueryArgs |
| 173 |
*/ |
| 174 |
private function cat_params() { |
| 175 |
$cats_in = isset( $this->meta['include_cats'] ) ? sanitize_text_field( implode( ', ', $this->meta['include_cats'] ) ) : null; |
| 176 |
$cat_ids_in = isset( $this->meta['select_cat_ids'] ) ? esc_html( $this->meta['select_cat_ids'] ) : null; |
| 177 |
$cats_not_in = isset( $this->meta['exclude_cats'] ) ? sanitize_text_field( implode( ', ', $this->meta['exclude_cats'] ) ) : null; |
| 178 |
$limit = ( ( empty( $this->meta['cats_limit'] ) || '-1' === $this->meta['cats_limit'] ) ? 10000000 : absint( $this->meta['cats_limit'] ) ); |
| 179 |
|
| 180 |
if ( $cats_in ) { |
| 181 |
$cats_in = explode( ',', $cats_in ); |
| 182 |
} |
| 183 |
|
| 184 |
if ( $cat_ids_in ) { |
| 185 |
$cats_in = explode( ',', $cat_ids_in ); |
| 186 |
} |
| 187 |
|
| 188 |
if ( ! $this->meta['show_uncategorized'] ) { |
| 189 |
$cats_not_in .= ',' . get_option( 'default_product_cat', 0 ); |
| 190 |
} |
| 191 |
|
| 192 |
if ( $cats_not_in ) { |
| 193 |
$cats_not_in = array_filter( explode( ',', $cats_not_in ) ); |
| 194 |
$this->args['exclude'] = $cats_not_in; |
| 195 |
} |
| 196 |
|
| 197 |
$this->args['number'] = $limit; |
| 198 |
$this->args['hide_empty'] = $this->meta['show_empty'] ? 0 : 1; |
| 199 |
|
| 200 |
if ( ! $this->meta['show_subcats'] ) { |
| 201 |
$this->args['parent'] = 0; |
| 202 |
} |
| 203 |
|
| 204 |
switch ( $this->meta['display_cat_by'] ) { |
| 205 |
case 'specific_parent': |
| 206 |
unset( $this->args['exclude'] ); |
| 207 |
unset( $this->args['parent'] ); |
| 208 |
|
| 209 |
if ( $this->meta['show_top_level_cats'] ) { |
| 210 |
$this->args['parent'] = absint( $this->meta['select_parent_cat'] ); |
| 211 |
} else { |
| 212 |
$this->args['child_of'] = absint( $this->meta['select_parent_cat'] ); |
| 213 |
} |
| 214 |
break; |
| 215 |
case 'cat_ids': |
| 216 |
case 'selection': |
| 217 |
unset( $this->args['exclude'] ); |
| 218 |
unset( $this->args['parent'] ); |
| 219 |
|
| 220 |
$this->args['include'] = $cats_in; |
| 221 |
break; |
| 222 |
default: |
| 223 |
break; |
| 224 |
} |
| 225 |
|
| 226 |
return $this; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Order & Orderby parameters. |
| 231 |
* |
| 232 |
* @return QueryArgs |
| 233 |
*/ |
| 234 |
private function order_params() { |
| 235 |
$order_by = ( isset( $this->meta['order_by'] ) ? esc_html( $this->meta['order_by'] ) : null ); |
| 236 |
$order = ( isset( $this->meta['order'] ) ? esc_html( $this->meta['order'] ) : null ); |
| 237 |
|
| 238 |
if ( $order ) { |
| 239 |
$this->args['order'] = $order; |
| 240 |
} |
| 241 |
|
| 242 |
if ( $order_by ) { |
| 243 |
$this->args['orderby'] = $order_by; |
| 244 |
} |
| 245 |
|
| 246 |
return $this; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* WC Order & Orderby parameters. |
| 251 |
* |
| 252 |
* @return QueryArgs |
| 253 |
*/ |
| 254 |
private function wc_order_params() { |
| 255 |
$order_by = ( isset( $this->meta['order_by'] ) ? esc_html( $this->meta['order_by'] ) : null ); |
| 256 |
$order = ( isset( $this->meta['order'] ) ? esc_html( $this->meta['order'] ) : null ); |
| 257 |
|
| 258 |
if ( $order ) { |
| 259 |
$this->args['order'] = $order; |
| 260 |
} |
| 261 |
|
| 262 |
if ( $order_by ) { |
| 263 |
$this->args['orderby'] = $order_by; |
| 264 |
|
| 265 |
if ( 'menu_order' === $order_by ) { |
| 266 |
$this->args['orderby'] = 'menu_order title'; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
return $this; |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Order & Orderby parameters. |
| 275 |
* |
| 276 |
* @return QueryArgs |
| 277 |
*/ |
| 278 |
private function cat_order_params() { |
| 279 |
$order_by = ( isset( $this->meta['cats_order_by'] ) ? esc_html( $this->meta['cats_order_by'] ) : 'name' ); |
| 280 |
$order = ( isset( $this->meta['cats_order'] ) ? esc_html( $this->meta['cats_order'] ) : 'DESC' ); |
| 281 |
|
| 282 |
if ( $order ) { |
| 283 |
$this->args['order'] = $order; |
| 284 |
} |
| 285 |
|
| 286 |
if ( $order_by ) { |
| 287 |
$this->args['orderby'] = $order_by; |
| 288 |
} |
| 289 |
|
| 290 |
if ( 'menu_order' === $order_by ) { |
| 291 |
$this->args['menu_order'] = $order; |
| 292 |
} |
| 293 |
|
| 294 |
return $this; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Pagination parameters. |
| 299 |
* |
| 300 |
* @param bool $isCarousel Layout type. |
| 301 |
* |
| 302 |
* @return QueryArgs |
| 303 |
*/ |
| 304 |
private function pagination_params( $isCarousel ) { |
| 305 |
$pagination = ! empty( $this->meta['pagination'] ); |
| 306 |
$limit = ( ( empty( $this->meta['limit'] ) || '-1' === $this->meta['limit'] ) ? 10000000 : absint( $this->meta['limit'] ) ); |
| 307 |
|
| 308 |
if ( $pagination ) { |
| 309 |
$posts_per_page = ( ! empty( $this->meta['posts_per_page'] ) ? absint( $this->meta['posts_per_page'] ) : $limit ); |
| 310 |
|
| 311 |
if ( $posts_per_page > $limit ) { |
| 312 |
$posts_per_page = $limit; |
| 313 |
} |
| 314 |
|
| 315 |
$this->args['posts_per_page'] = $posts_per_page; |
| 316 |
|
| 317 |
if ( is_front_page() ) { |
| 318 |
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1; |
| 319 |
} else { |
| 320 |
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; |
| 321 |
} |
| 322 |
|
| 323 |
$offset = $posts_per_page * ( (int) $paged - 1 ); |
| 324 |
$this->args['paged'] = $paged; |
| 325 |
|
| 326 |
if ( absint( $this->args['posts_per_page'] ) > $limit - $offset ) { |
| 327 |
$this->args['posts_per_page'] = $limit - $offset; |
| 328 |
$this->args['offset'] = $offset; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
if ( $isCarousel ) { |
| 333 |
$this->args['posts_per_page'] = $limit; |
| 334 |
} |
| 335 |
|
| 336 |
return $this; |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Pagination parameters. |
| 341 |
* |
| 342 |
* @param bool $isCarousel Layout type. |
| 343 |
* |
| 344 |
* @return QueryArgs |
| 345 |
*/ |
| 346 |
private function wc_pagination_params( $isCarousel ) { |
| 347 |
$pagination = ! empty( $this->meta['pagination'] ); |
| 348 |
$limit = ( ( empty( $this->meta['limit'] ) || '-1' === $this->meta['limit'] ) ? 10000000 : absint( $this->meta['limit'] ) ); |
| 349 |
|
| 350 |
if ( $pagination ) { |
| 351 |
unset( $this->args['offset'] ); |
| 352 |
|
| 353 |
$posts_per_page = ( ! empty( $this->meta['posts_per_page'] ) ? absint( $this->meta['posts_per_page'] ) : $limit ); |
| 354 |
|
| 355 |
if ( $posts_per_page > $limit ) { |
| 356 |
$posts_per_page = $limit; |
| 357 |
} |
| 358 |
|
| 359 |
$this->args['paginate'] = true; |
| 360 |
$this->args['limit'] = $posts_per_page; |
| 361 |
|
| 362 |
if ( is_front_page() ) { |
| 363 |
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1; |
| 364 |
} else { |
| 365 |
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; |
| 366 |
} |
| 367 |
|
| 368 |
$offset = $posts_per_page * ( (int) $paged - 1 ); |
| 369 |
$this->args['page'] = $paged; |
| 370 |
|
| 371 |
if ( absint( $this->args['limit'] ) > $limit - $offset ) { |
| 372 |
$this->args['limit'] = $limit - $offset; |
| 373 |
$this->args['offset'] = $offset; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
if ( $isCarousel ) { |
| 378 |
$this->args['limit'] = $limit; |
| 379 |
} |
| 380 |
|
| 381 |
return $this; |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* Taxonomy parameters. |
| 386 |
* |
| 387 |
* @return QueryArgs |
| 388 |
*/ |
| 389 |
private function tax_params() { |
| 390 |
$categories = isset( $this->meta['categories'] ) ? array_filter( $this->meta['categories'] ) : []; |
| 391 |
$tags = isset( $this->meta['tags'] ) ? array_filter( $this->meta['tags'] ) : []; |
| 392 |
$attributes = isset( $this->meta['attributes'] ) ? array_filter( $this->meta['attributes'] ) : []; |
| 393 |
$taxQ = []; |
| 394 |
|
| 395 |
if ( is_array( $categories ) && ! empty( $categories ) ) { |
| 396 |
$taxQ[] = [ |
| 397 |
'taxonomy' => 'product_cat', |
| 398 |
'field' => 'term_id', |
| 399 |
'terms' => $categories, |
| 400 |
'operator' => 'IN', |
| 401 |
]; |
| 402 |
} |
| 403 |
|
| 404 |
if ( ! empty( $tags ) && is_array( $tags ) ) { |
| 405 |
$taxQ[] = [ |
| 406 |
'taxonomy' => 'product_tag', |
| 407 |
'field' => 'term_id', |
| 408 |
'terms' => $tags, |
| 409 |
'operator' => 'IN', |
| 410 |
]; |
| 411 |
} |
| 412 |
|
| 413 |
if ( ! empty( $attributes ) && is_array( $attributes ) ) { |
| 414 |
$attribute_tax = null; |
| 415 |
|
| 416 |
foreach ( $attributes as $atts ) { |
| 417 |
$attribute_tax = get_term( $atts )->taxonomy; |
| 418 |
|
| 419 |
$taxQ[] = [ |
| 420 |
'taxonomy' => $attribute_tax, |
| 421 |
'field' => 'term_id', |
| 422 |
'terms' => [ $atts ], |
| 423 |
'operator' => 'IN', |
| 424 |
]; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
if ( count( $taxQ ) >= 2 ) { |
| 429 |
$taxQ['relation'] = $this->meta['relation']; |
| 430 |
} |
| 431 |
|
| 432 |
if ( ! empty( $taxQ ) ) { |
| 433 |
$this->args['tax_query'] = $taxQ; |
| 434 |
} |
| 435 |
|
| 436 |
return $this; |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* Taxonomy parameters. |
| 441 |
* |
| 442 |
* @return QueryArgs |
| 443 |
*/ |
| 444 |
private function wc_tax_params() { |
| 445 |
$categories = isset( $this->meta['categories'] ) ? array_filter( $this->meta['categories'] ) : []; |
| 446 |
$tags = isset( $this->meta['tags'] ) ? array_filter( $this->meta['tags'] ) : []; |
| 447 |
$attributes = isset( $this->meta['attributes'] ) ? array_filter( $this->meta['attributes'] ) : []; |
| 448 |
|
| 449 |
if ( ! empty( $categories ) ) { |
| 450 |
$this->args['product_category_id'] = $categories; |
| 451 |
} |
| 452 |
|
| 453 |
if ( ! empty( $tags ) ) { |
| 454 |
$this->args['product_tag_id'] = $tags; |
| 455 |
} |
| 456 |
|
| 457 |
if ( ! empty( $attributes ) ) { |
| 458 |
$this->args['product_attribute_id'] = $attributes; |
| 459 |
} |
| 460 |
|
| 461 |
return $this; |
| 462 |
} |
| 463 |
} |
| 464 |
|