| 1 |
<?php |
| 2 |
|
| 3 |
namespace UltimateStoreKit\Includes\Controls\GroupQuery; |
| 4 |
|
| 5 |
use Elementor\Controls_Manager; |
| 6 |
use UltimateStoreKit\Includes\Controls\SelectInput\Dynamic_Select; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
trait Group_Control_Query { |
| 13 |
|
| 14 |
public function register_query_builder_controls() { |
| 15 |
|
| 16 |
$this->add_control( |
| 17 |
'product_source', |
| 18 |
[ |
| 19 |
'label' => __( 'Source', 'ultimate-store-kit' ), |
| 20 |
'type' => Controls_Manager::SELECT, |
| 21 |
'options' => $this->getGroupControlQueryPostTypes(), |
| 22 |
'default' => 'product', |
| 23 |
] |
| 24 |
); |
| 25 |
$this->add_control( |
| 26 |
'product_limit', |
| 27 |
[ |
| 28 |
'label' => esc_html__( 'Product Limit', 'ultimate-store-kit' ), |
| 29 |
'type' => Controls_Manager::NUMBER, |
| 30 |
'default' => 9, |
| 31 |
] |
| 32 |
); |
| 33 |
|
| 34 |
$this->add_control( |
| 35 |
'product_selected_ids', |
| 36 |
[ |
| 37 |
'label' => __( 'Search & Select', 'ultimate-store-kit' ), |
| 38 |
'type' => Dynamic_Select::TYPE, |
| 39 |
'multiple' => true, |
| 40 |
'label_block' => true, |
| 41 |
'query_args' => [ |
| 42 |
'query' => 'posts', |
| 43 |
], |
| 44 |
'condition' => [ |
| 45 |
'product_source' => 'manual_selection', |
| 46 |
] |
| 47 |
] |
| 48 |
); |
| 49 |
|
| 50 |
$this->start_controls_tabs( |
| 51 |
'tabs_product_include_exclude', |
| 52 |
[ |
| 53 |
'condition' => [ |
| 54 |
'product_source!' => [ 'manual_selection', 'current_query' ], |
| 55 |
] |
| 56 |
] |
| 57 |
); |
| 58 |
|
| 59 |
$this->start_controls_tab( |
| 60 |
'tab_product_include', |
| 61 |
[ |
| 62 |
'label' => __( 'Include', 'ultimate-store-kit' ), |
| 63 |
'condition' => [ |
| 64 |
'product_source!' => [ 'manual_selection', 'current_query' ], |
| 65 |
] |
| 66 |
] |
| 67 |
); |
| 68 |
|
| 69 |
$this->add_control( |
| 70 |
'product_include_by', |
| 71 |
[ |
| 72 |
'label' => __( 'Include By', 'ultimate-store-kit' ), |
| 73 |
'type' => Controls_Manager::SELECT2, |
| 74 |
'multiple' => true, |
| 75 |
'label_block' => true, |
| 76 |
'options' => [ |
| 77 |
'authors' => __( 'Authors', 'ultimate-store-kit' ), |
| 78 |
'terms' => __( 'Terms', 'ultimate-store-kit' ), |
| 79 |
], |
| 80 |
'condition' => [ |
| 81 |
'product_source!' => [ 'manual_selection', 'current_query' ], |
| 82 |
] |
| 83 |
] |
| 84 |
); |
| 85 |
|
| 86 |
$this->add_control( |
| 87 |
'product_include_author_ids', |
| 88 |
[ |
| 89 |
'label' => __( 'Authors', 'ultimate-store-kit' ), |
| 90 |
'type' => Dynamic_Select::TYPE, |
| 91 |
'multiple' => true, |
| 92 |
'label_block' => true, |
| 93 |
'query_args' => [ |
| 94 |
'query' => 'authors', |
| 95 |
], |
| 96 |
'condition' => [ |
| 97 |
'product_include_by' => 'authors', |
| 98 |
'product_source!' => [ 'manual_selection', 'current_query' ], |
| 99 |
] |
| 100 |
] |
| 101 |
); |
| 102 |
|
| 103 |
$this->add_control( |
| 104 |
'product_include_term_ids', |
| 105 |
[ |
| 106 |
'label' => __( 'Terms', 'ultimate-store-kit' ), |
| 107 |
'description' => __( 'Terms are items in a taxonomy. The available taxonomies are: Categories, Tags, Formats and custom taxonomies.', 'ultimate-store-kit' ), |
| 108 |
'type' => Dynamic_Select::TYPE, |
| 109 |
'multiple' => true, |
| 110 |
'label_block' => true, |
| 111 |
'placeholder' => __( 'Type and select terms', 'ultimate-store-kit' ), |
| 112 |
'query_args' => [ |
| 113 |
'query' => 'terms', |
| 114 |
'widget_props' => [ |
| 115 |
'post_type' => 'product_source' |
| 116 |
] |
| 117 |
], |
| 118 |
'condition' => [ |
| 119 |
'product_include_by' => 'terms', |
| 120 |
'product_source!' => [ 'manual_selection', 'current_query', '_related_post_type' ], |
| 121 |
] |
| 122 |
] |
| 123 |
); |
| 124 |
|
| 125 |
$this->end_controls_tab(); |
| 126 |
|
| 127 |
$this->start_controls_tab( |
| 128 |
'tab_product_exclude', |
| 129 |
[ |
| 130 |
'label' => __( 'Exclude', 'ultimate-store-kit' ), |
| 131 |
'condition' => [ |
| 132 |
'product_source!' => [ 'manual_selection', 'current_query' ], |
| 133 |
] |
| 134 |
] |
| 135 |
); |
| 136 |
|
| 137 |
$this->add_control( |
| 138 |
'product_exclude_by', |
| 139 |
[ |
| 140 |
'label' => __( 'Exclude By', 'ultimate-store-kit' ), |
| 141 |
'type' => Controls_Manager::SELECT2, |
| 142 |
'multiple' => true, |
| 143 |
'label_block' => true, |
| 144 |
'options' => [ |
| 145 |
'authors' => __( 'Authors', 'ultimate-store-kit' ), |
| 146 |
'current_post' => __( 'Current Post', 'ultimate-store-kit' ), |
| 147 |
'manual_selection' => __( 'Manual Selection', 'ultimate-store-kit' ), |
| 148 |
'terms' => __( 'Terms', 'ultimate-store-kit' ), |
| 149 |
], |
| 150 |
'condition' => [ |
| 151 |
'product_source!' => [ 'manual_selection', 'current_query' ], |
| 152 |
] |
| 153 |
] |
| 154 |
); |
| 155 |
|
| 156 |
$this->add_control( |
| 157 |
'posts_exclude_ids', |
| 158 |
[ |
| 159 |
'label' => __( 'Search & Select', 'ultimate-store-kit' ), |
| 160 |
'type' => Dynamic_Select::TYPE, |
| 161 |
'multiple' => true, |
| 162 |
'label_block' => true, |
| 163 |
'query_args' => [ |
| 164 |
'query' => 'posts', |
| 165 |
'widget_props' => [ |
| 166 |
'post_type' => 'product_source' |
| 167 |
] |
| 168 |
], |
| 169 |
'condition' => [ |
| 170 |
'product_source!' => [ 'manual_selection', 'current_query' ], |
| 171 |
'product_exclude_by' => 'manual_selection', |
| 172 |
] |
| 173 |
] |
| 174 |
); |
| 175 |
|
| 176 |
$this->add_control( |
| 177 |
'product_exclude_author_ids', |
| 178 |
[ |
| 179 |
'label' => __( 'Authors', 'ultimate-store-kit' ), |
| 180 |
'type' => Dynamic_Select::TYPE, |
| 181 |
'multiple' => true, |
| 182 |
'label_block' => true, |
| 183 |
'query_args' => [ |
| 184 |
'query' => 'authors', |
| 185 |
], |
| 186 |
'condition' => [ |
| 187 |
'product_exclude_by' => 'authors', |
| 188 |
'product_source!' => [ 'manual_selection', 'current_query' ], |
| 189 |
] |
| 190 |
] |
| 191 |
); |
| 192 |
|
| 193 |
$this->add_control( |
| 194 |
'product_exclude_term_ids', |
| 195 |
[ |
| 196 |
'label' => __( 'Terms', 'ultimate-store-kit' ), |
| 197 |
'description' => __( 'Terms are items in a taxonomy. The available taxonomies are: Categories, Tags, Formats and custom taxonomies.', 'ultimate-store-kit' ), |
| 198 |
'type' => Dynamic_Select::TYPE, |
| 199 |
'multiple' => true, |
| 200 |
'label_block' => true, |
| 201 |
'placeholder' => __( 'Type and select terms', 'ultimate-store-kit' ), |
| 202 |
'query_args' => [ |
| 203 |
'query' => 'terms', |
| 204 |
'widget_props' => [ |
| 205 |
'post_type' => 'product_source' |
| 206 |
] |
| 207 |
], |
| 208 |
'condition' => [ |
| 209 |
'product_exclude_by' => 'terms', |
| 210 |
'product_source!' => [ 'manual_selection', 'current_query', '_related_post_type' ], |
| 211 |
] |
| 212 |
] |
| 213 |
); |
| 214 |
|
| 215 |
$this->end_controls_tab(); |
| 216 |
$this->end_controls_tabs(); |
| 217 |
|
| 218 |
$this->add_control( |
| 219 |
'product_divider', |
| 220 |
[ |
| 221 |
'type' => Controls_Manager::DIVIDER, |
| 222 |
'condition' => [ |
| 223 |
'product_source!' => 'current_query', |
| 224 |
] |
| 225 |
] |
| 226 |
); |
| 227 |
$this->add_control( |
| 228 |
'product_select_date', |
| 229 |
[ |
| 230 |
'label' => __( 'Date', 'ultimate-store-kit' ), |
| 231 |
'type' => Controls_Manager::SELECT, |
| 232 |
'default' => 'anytime', |
| 233 |
'options' => [ |
| 234 |
'anytime' => __( 'All', 'ultimate-store-kit' ), |
| 235 |
'today' => __( 'Past Day', 'ultimate-store-kit' ), |
| 236 |
'week' => __( 'Past Week', 'ultimate-store-kit' ), |
| 237 |
'month' => __( 'Past Month', 'ultimate-store-kit' ), |
| 238 |
'quarter' => __( 'Past Quarter', 'ultimate-store-kit' ), |
| 239 |
'year' => __( 'Past Year', 'ultimate-store-kit' ), |
| 240 |
'exact' => __( 'Custom', 'ultimate-store-kit' ), |
| 241 |
], |
| 242 |
'condition' => [ |
| 243 |
'product_source!' => 'current_query', |
| 244 |
] |
| 245 |
] |
| 246 |
); |
| 247 |
|
| 248 |
$this->add_control( |
| 249 |
'product_date_before', |
| 250 |
[ |
| 251 |
'label' => __( 'Before', 'ultimate-store-kit' ), |
| 252 |
'type' => Controls_Manager::DATE_TIME, |
| 253 |
'description' => __( 'Setting a ‘Before’ date will show all the posts published until the chosen date (inclusive).', 'ultimate-store-kit' ), |
| 254 |
'condition' => [ |
| 255 |
'product_select_date' => 'exact', |
| 256 |
'product_source!' => 'current_query', |
| 257 |
] |
| 258 |
] |
| 259 |
); |
| 260 |
|
| 261 |
$this->add_control( |
| 262 |
'product_date_after', |
| 263 |
[ |
| 264 |
'label' => __( 'After', 'ultimate-store-kit' ), |
| 265 |
'type' => Controls_Manager::DATE_TIME, |
| 266 |
'description' => __( 'Setting an ‘After’ date will show all the posts published since the chosen date (inclusive).', 'ultimate-store-kit' ), |
| 267 |
'condition' => [ |
| 268 |
'product_select_date' => 'exact', |
| 269 |
'product_source!' => 'current_query', |
| 270 |
] |
| 271 |
] |
| 272 |
); |
| 273 |
|
| 274 |
$this->add_control( |
| 275 |
'product_orderby', |
| 276 |
[ |
| 277 |
'label' => __( 'Order By', 'ultimate-store-kit' ), |
| 278 |
'type' => Controls_Manager::SELECT, |
| 279 |
'default' => 'date', |
| 280 |
'options' => [ |
| 281 |
'title' => __( 'Title', 'ultimate-store-kit' ), |
| 282 |
'ID' => __( 'ID', 'ultimate-store-kit' ), |
| 283 |
'date' => __( 'Date', 'ultimate-store-kit' ), |
| 284 |
'author' => __( 'Author', 'ultimate-store-kit' ), |
| 285 |
'comment_count' => __( 'Comment Count', 'ultimate-store-kit' ), |
| 286 |
'menu_order' => __( 'Menu Order', 'ultimate-store-kit' ), |
| 287 |
'modified' => __( 'Modified', 'ultimate-store-kit' ), |
| 288 |
'rand' => __( 'Random', 'ultimate-store-kit' ), |
| 289 |
'price' => __( 'Price', 'ultimate-store-kit' ), |
| 290 |
'sales' => __( 'Sales', 'ultimate-store-kit' ), |
| 291 |
] |
| 292 |
] |
| 293 |
); |
| 294 |
$this->add_control( |
| 295 |
'product_order', |
| 296 |
[ |
| 297 |
'label' => __( 'Order', 'ultimate-store-kit' ), |
| 298 |
'type' => Controls_Manager::SELECT, |
| 299 |
'default' => 'desc', |
| 300 |
'options' => [ |
| 301 |
'asc' => __( 'ASC', 'ultimate-store-kit' ), |
| 302 |
'desc' => __( 'DESC', 'ultimate-store-kit' ), |
| 303 |
], |
| 304 |
'condition' => [ |
| 305 |
'product_source!' => 'current_query', |
| 306 |
] |
| 307 |
] |
| 308 |
); |
| 309 |
// Others Features |
| 310 |
} |
| 311 |
|
| 312 |
public function register_controls_wc_additional() { |
| 313 |
$this->add_control( |
| 314 |
'product_show_only', |
| 315 |
[ |
| 316 |
'label' => esc_html__( 'Show Product', 'ultimate-store-kit' ), |
| 317 |
'type' => Controls_Manager::SELECT, |
| 318 |
'default' => 'all', |
| 319 |
'options' => [ |
| 320 |
'all' => esc_html__( 'All Products', 'ultimate-store-kit' ), |
| 321 |
'onsale' => esc_html__( 'On Sale', 'ultimate-store-kit' ), |
| 322 |
'featured' => esc_html__( 'Featured', 'ultimate-store-kit' ), |
| 323 |
] |
| 324 |
] |
| 325 |
); |
| 326 |
$this->add_control( |
| 327 |
'product_hide_free', |
| 328 |
[ |
| 329 |
'label' => esc_html__( 'Hide Free Product', 'ultimate-store-kit' ), |
| 330 |
'type' => Controls_Manager::SWITCHER, |
| 331 |
|
| 332 |
] |
| 333 |
); |
| 334 |
$this->add_control( |
| 335 |
'product_hide_out_stock', |
| 336 |
[ |
| 337 |
'label' => esc_html__( 'Hide Out of Stock', 'ultimate-store-kit' ), |
| 338 |
'type' => Controls_Manager::SWITCHER, |
| 339 |
] |
| 340 |
); |
| 341 |
$this->add_control( |
| 342 |
'product_only_with_featured_image', |
| 343 |
[ |
| 344 |
'label' => __( 'Only Featured Image Post', 'ultimate-store-kit' ), |
| 345 |
'description' => __( 'Enable to display posts only when featured image is present.', 'ultimate-store-kit' ), |
| 346 |
'type' => Controls_Manager::SWITCHER, |
| 347 |
'return_value' => 'yes', |
| 348 |
'condition' => [ |
| 349 |
'product_source!' => 'current_query', |
| 350 |
] |
| 351 |
] |
| 352 |
); |
| 353 |
$this->add_control( |
| 354 |
'query_id', |
| 355 |
[ |
| 356 |
'label' => __( 'Query ID', 'ultimate-store-kit' ), |
| 357 |
'description' => __( 'Give your Query a custom unique id to allow server side filtering', 'ultimate-store-kit' ), |
| 358 |
'type' => Controls_Manager::TEXT, |
| 359 |
'separator' => 'before', |
| 360 |
] |
| 361 |
); |
| 362 |
} |
| 363 |
|
| 364 |
private function setMetaQueryArgs() { |
| 365 |
|
| 366 |
$args = []; |
| 367 |
|
| 368 |
if ( 'current_query' === $this->getGroupControlQueryPostType() ) { |
| 369 |
$args['posts_per_page'] = $this->get_settings( 'product_limit' ); |
| 370 |
return $args; |
| 371 |
} |
| 372 |
|
| 373 |
$args['paged'] = max( 1, get_query_var( 'paged' ), get_query_var( 'page' ) ); |
| 374 |
$args['posts_per_page'] = $this->get_settings( 'product_limit' ); |
| 375 |
$args['order'] = $this->get_settings( 'product_order' ); |
| 376 |
// $args['orderby'] = $this->get_settings('product_orderby'); |
| 377 |
|
| 378 |
if ( 'product' === $this->getGroupControlQueryPostType() ) { |
| 379 |
$product_visibility_term_ids = wc_get_product_visibility_term_ids(); |
| 380 |
if ( 'yes' == $this->get_settings( 'product_hide_free' ) ) { |
| 381 |
$args['meta_query'][] = [ |
| 382 |
'key' => '_price', |
| 383 |
'value' => 0, |
| 384 |
'compare' => '>', |
| 385 |
'type' => 'DECIMAL', |
| 386 |
]; |
| 387 |
} |
| 388 |
|
| 389 |
if ( 'yes' == $this->get_settings( 'product_hide_out_stock' ) ) { |
| 390 |
$args['tax_query'][] = [ |
| 391 |
[ |
| 392 |
'taxonomy' => 'product_visibility', |
| 393 |
'field' => 'term_taxonomy_id', |
| 394 |
'terms' => $product_visibility_term_ids['outofstock'], |
| 395 |
'operator' => 'NOT IN', |
| 396 |
], |
| 397 |
]; // WPCS: slow query ok. |
| 398 |
} |
| 399 |
switch ( $this->get_settings( 'product_show_only' ) ) { |
| 400 |
case 'featured': |
| 401 |
$args['tax_query'][] = [ |
| 402 |
'taxonomy' => 'product_visibility', |
| 403 |
'field' => 'term_taxonomy_id', |
| 404 |
'terms' => $product_visibility_term_ids['featured'], |
| 405 |
]; |
| 406 |
break; |
| 407 |
case 'onsale': |
| 408 |
$product_ids_on_sale = wc_get_product_ids_on_sale(); |
| 409 |
$product_ids_on_sale[] = 0; |
| 410 |
$args['post__in'] = $product_ids_on_sale; |
| 411 |
break; |
| 412 |
} |
| 413 |
switch ( $this->get_settings( 'product_orderby' ) ) { |
| 414 |
case 'price': |
| 415 |
$args['meta_key'] = '_price'; // WPCS: slow query ok. |
| 416 |
$args['orderby'] = 'meta_value_num'; |
| 417 |
break; |
| 418 |
case 'sales': |
| 419 |
$args['meta_key'] = 'total_sales'; // WPCS: slow query ok. |
| 420 |
$args['orderby'] = 'meta_value_num'; |
| 421 |
break; |
| 422 |
default: |
| 423 |
$args['orderby'] = $this->get_settings( 'product_orderby' ); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
/** |
| 430 |
* Set Feature Images |
| 431 |
*/ |
| 432 |
if ( $this->get_settings( 'product_only_with_featured_image' ) === 'yes' ) { |
| 433 |
$args['meta_key'] = '_thumbnail_id'; |
| 434 |
} |
| 435 |
|
| 436 |
/** |
| 437 |
* Set Date |
| 438 |
*/ |
| 439 |
|
| 440 |
$selected_date = $this->get_settings( 'product_select_date' ); |
| 441 |
|
| 442 |
if ( ! empty( $selected_date ) ) { |
| 443 |
$date_query = []; |
| 444 |
|
| 445 |
switch ( $selected_date ) { |
| 446 |
case 'today': |
| 447 |
$date_query['after'] = '-1 day'; |
| 448 |
break; |
| 449 |
|
| 450 |
case 'week': |
| 451 |
$date_query['after'] = '-1 week'; |
| 452 |
break; |
| 453 |
|
| 454 |
case 'month': |
| 455 |
$date_query['after'] = '-1 month'; |
| 456 |
break; |
| 457 |
|
| 458 |
case 'quarter': |
| 459 |
$date_query['after'] = '-3 month'; |
| 460 |
break; |
| 461 |
|
| 462 |
case 'year': |
| 463 |
$date_query['after'] = '-1 year'; |
| 464 |
break; |
| 465 |
|
| 466 |
case 'exact': |
| 467 |
$after_date = $this->get_settings( 'product_date_after' ); |
| 468 |
if ( ! empty( $after_date ) ) { |
| 469 |
$date_query['after'] = $after_date; |
| 470 |
} |
| 471 |
|
| 472 |
$before_date = $this->get_settings( 'product_date_before' ); |
| 473 |
if ( ! empty( $before_date ) ) { |
| 474 |
$date_query['before'] = $before_date; |
| 475 |
} |
| 476 |
|
| 477 |
$date_query['inclusive'] = true; |
| 478 |
break; |
| 479 |
} |
| 480 |
|
| 481 |
if ( ! empty( $date_query ) ) { |
| 482 |
$args['date_query'] = $date_query; |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
return $args; |
| 487 |
} |
| 488 |
|
| 489 |
protected function getGroupControlQueryArgs() { |
| 490 |
|
| 491 |
$settings = $this->get_settings(); |
| 492 |
$args = $this->setMetaQueryArgs(); |
| 493 |
$args['post_status'] = 'publish'; |
| 494 |
$args['suppress_filters'] = false; |
| 495 |
$exclude_by = $this->getGroupControlQueryParamBy( 'exclude' ); |
| 496 |
|
| 497 |
// /** |
| 498 |
// * Set Ignore Sticky |
| 499 |
// */ |
| 500 |
// if ( |
| 501 |
// $this->getGroupControlQueryPostType() === 'post' |
| 502 |
// && $this->get_settings('posts_ignore_sticky_posts') === 'yes' |
| 503 |
// ) { |
| 504 |
// $args['ignore_sticky_posts'] = true; |
| 505 |
|
| 506 |
// if (in_array('current_post', $exclude_by)) { |
| 507 |
// $args['post__not_in'] = [get_the_ID()]; |
| 508 |
// } |
| 509 |
// } |
| 510 |
|
| 511 |
|
| 512 |
if ( $this->getGroupControlQueryPostType() === 'manual_selection' ) { |
| 513 |
/** |
| 514 |
* Set Including Manually |
| 515 |
*/ |
| 516 |
$selected_ids = $this->get_settings( 'product_selected_ids' ); |
| 517 |
$selected_ids = wp_parse_id_list( $selected_ids ); |
| 518 |
$args['post_type'] = 'product'; |
| 519 |
if ( ! empty( $selected_ids ) ) { |
| 520 |
$args['post__in'] = $selected_ids; |
| 521 |
} |
| 522 |
$args['ignore_sticky_posts'] = 1; |
| 523 |
} elseif ( 'current_query' === $this->getGroupControlQueryPostType() ) { |
| 524 |
/** |
| 525 |
* Make Current Query |
| 526 |
*/ |
| 527 |
$wp_query = $GLOBALS['wp_query']->query_vars; |
| 528 |
|
| 529 |
if ( isset( $wp_query['usk-template-builder'] ) && 'shop-page' === $wp_query['usk-template-builder'] || isset( $_GET['usk-template'] ) || \Elementor\Plugin::$instance->editor->is_edit_mode() ) { |
| 530 |
$args['post_type'] = 'product'; |
| 531 |
} else { |
| 532 |
$args['post_type'] = 'product'; |
| 533 |
} |
| 534 |
|
| 535 |
$args['paged'] = 1; |
| 536 |
|
| 537 |
$page = max( 1, get_query_var( 'paged' ), get_query_var( 'page' ) ); |
| 538 |
$page = absint( empty( $_GET['product-page'] ) ? $page : $_GET['product-page'] ); |
| 539 |
$args['paged'] = $page; |
| 540 |
|
| 541 |
if ( isset( $wp_query['product_cat'] ) ) { |
| 542 |
$args['product_cat'] = $wp_query['product_cat']; |
| 543 |
} |
| 544 |
|
| 545 |
$args['posts_per_page'] = $this->get_settings( 'product_limit' ); |
| 546 |
$args = apply_filters( 'ultimate_store_kit/query/get_query_args/current_query', $args ); |
| 547 |
|
| 548 |
} elseif ( '_related_post_type' === $this->getGroupControlQueryPostType() ) { |
| 549 |
/** |
| 550 |
* Set Related Query |
| 551 |
*/ |
| 552 |
$post_id = get_queried_object_id(); |
| 553 |
$related_post_id = is_singular() && ( 0 !== $post_id ) ? $post_id : null; |
| 554 |
$args['post_type'] = get_post_type( $related_post_id ); |
| 555 |
|
| 556 |
$include_by = $this->getGroupControlQueryParamBy( 'include' ); |
| 557 |
if ( in_array( 'authors', $include_by ) ) { |
| 558 |
$args['author__in'] = wp_parse_id_list( $settings['product_include_author_ids'] ); |
| 559 |
} else { |
| 560 |
$args['author__in'] = get_post_field( 'post_author', $related_post_id ); |
| 561 |
} |
| 562 |
|
| 563 |
$exclude_by = $this->getGroupControlQueryParamBy( 'exclude' ); |
| 564 |
if ( in_array( 'authors', $exclude_by ) ) { |
| 565 |
$args['author__not_in'] = wp_parse_id_list( $settings['product_exclude_author_ids'] ); |
| 566 |
} |
| 567 |
|
| 568 |
if ( in_array( 'current_post', $exclude_by ) ) { |
| 569 |
$args['post__not_in'] = [ get_the_ID() ]; |
| 570 |
} |
| 571 |
|
| 572 |
$args['ignore_sticky_posts'] = 1; |
| 573 |
$args = apply_filters( 'ultimate_store_kit/query/get_query_args/related_query', $args ); |
| 574 |
} else { |
| 575 |
|
| 576 |
/** |
| 577 |
* Set Post Type |
| 578 |
*/ |
| 579 |
$args['post_type'] = $this->getGroupControlQueryPostType(); |
| 580 |
|
| 581 |
/** |
| 582 |
* Set Exclude Post |
| 583 |
*/ |
| 584 |
$exclude_by = $this->getGroupControlQueryParamBy( 'exclude' ); |
| 585 |
$current_post = []; |
| 586 |
|
| 587 |
if ( in_array( 'current_post', $exclude_by ) && is_singular() ) { |
| 588 |
$current_post = [ get_the_ID() ]; |
| 589 |
} |
| 590 |
|
| 591 |
if ( in_array( 'manual_selection', $exclude_by ) ) { |
| 592 |
$exclude_ids = $settings['posts_exclude_ids']; |
| 593 |
$args['post__not_in'] = array_merge( $current_post, wp_parse_id_list( $exclude_ids ) ); |
| 594 |
} |
| 595 |
/** |
| 596 |
* Set Authors |
| 597 |
*/ |
| 598 |
$include_by = $this->getGroupControlQueryParamBy( 'include' ); |
| 599 |
$exclude_by = $this->getGroupControlQueryParamBy( 'exclude' ); |
| 600 |
$include_users = []; |
| 601 |
$exclude_users = []; |
| 602 |
|
| 603 |
if ( in_array( 'authors', $include_by ) ) { |
| 604 |
$include_users = wp_parse_id_list( $settings['product_include_author_ids'] ); |
| 605 |
} |
| 606 |
|
| 607 |
if ( in_array( 'authors', $exclude_by ) ) { |
| 608 |
$exclude_users = wp_parse_id_list( $settings['product_exclude_author_ids'] ); |
| 609 |
$include_users = array_diff( $include_users, $exclude_users ); |
| 610 |
} |
| 611 |
|
| 612 |
if ( ! empty( $include_users ) ) { |
| 613 |
$args['author__in'] = $include_users; |
| 614 |
} |
| 615 |
|
| 616 |
if ( ! empty( $exclude_users ) ) { |
| 617 |
$args['author__not_in'] = $exclude_users; |
| 618 |
; |
| 619 |
} |
| 620 |
|
| 621 |
/** |
| 622 |
* Set Taxonomy |
| 623 |
*/ |
| 624 |
$include_by = $this->getGroupControlQueryParamBy( 'include' ); |
| 625 |
$exclude_by = $this->getGroupControlQueryParamBy( 'exclude' ); |
| 626 |
$include_terms = []; |
| 627 |
$exclude_terms = []; |
| 628 |
$terms_query = []; |
| 629 |
|
| 630 |
if ( in_array( 'terms', $include_by ) ) { |
| 631 |
$include_terms = wp_parse_id_list( $settings['product_include_term_ids'] ); |
| 632 |
} |
| 633 |
|
| 634 |
if ( in_array( 'terms', $exclude_by ) ) { |
| 635 |
$exclude_terms = wp_parse_id_list( $settings['product_exclude_term_ids'] ); |
| 636 |
$include_terms = array_diff( $include_terms, $exclude_terms ); |
| 637 |
} |
| 638 |
|
| 639 |
if ( ! empty( $include_terms ) ) { |
| 640 |
$tax_terms_map = $this->mapGroupControlQuery( $include_terms ); |
| 641 |
|
| 642 |
foreach ( $tax_terms_map as $tax => $terms ) { |
| 643 |
$terms_query[] = [ |
| 644 |
'taxonomy' => $tax, |
| 645 |
'field' => 'term_id', |
| 646 |
'terms' => $terms, |
| 647 |
'operator' => 'IN', |
| 648 |
]; |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
if ( ! empty( $exclude_terms ) ) { |
| 653 |
$tax_terms_map = $this->mapGroupControlQuery( $exclude_terms ); |
| 654 |
|
| 655 |
foreach ( $tax_terms_map as $tax => $terms ) { |
| 656 |
$terms_query[] = [ |
| 657 |
'taxonomy' => $tax, |
| 658 |
'field' => 'term_id', |
| 659 |
'terms' => $terms, |
| 660 |
'operator' => 'NOT IN', |
| 661 |
]; |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
if ( ! empty( $terms_query ) ) { |
| 666 |
$args['tax_query'] = $terms_query; |
| 667 |
$args['tax_query']['relation'] = 'AND'; |
| 668 |
} |
| 669 |
|
| 670 |
$page = max( 1, get_query_var( 'paged' ), get_query_var( 'page' ) ); |
| 671 |
$page = absint( empty( $_GET['product-page'] ) ? $page : $_GET['product-page'] ); |
| 672 |
$args['paged'] = $page; |
| 673 |
} |
| 674 |
|
| 675 |
$query_id = $this->get_settings( 'query_id' ); |
| 676 |
if ( ! empty( $query_id ) ) { |
| 677 |
add_action( 'pre_get_posts', [ $this, 'pre_get_posts_query_filter' ] ); |
| 678 |
} |
| 679 |
|
| 680 |
return $args; |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
/** |
| 685 |
* @return mixed |
| 686 |
*/ |
| 687 |
private function getGroupControlQueryPostType() { |
| 688 |
return $this->get_settings( 'product_source' ); |
| 689 |
} |
| 690 |
|
| 691 |
/** |
| 692 |
* Get Query Params by args |
| 693 |
* |
| 694 |
* @param string $by |
| 695 |
* |
| 696 |
* @return array|mixed |
| 697 |
*/ |
| 698 |
private function getGroupControlQueryParamBy( $by = 'exclude' ) { |
| 699 |
$mapBy = [ |
| 700 |
'exclude' => 'product_exclude_by', |
| 701 |
'include' => 'product_include_by', |
| 702 |
]; |
| 703 |
|
| 704 |
$setting = $this->get_settings( $mapBy[ $by ] ); |
| 705 |
|
| 706 |
return ( ! empty( $setting ) ? $setting : [] ); |
| 707 |
} |
| 708 |
|
| 709 |
/** |
| 710 |
* @param array $term_ids |
| 711 |
* |
| 712 |
* @return array |
| 713 |
*/ |
| 714 |
private function mapGroupControlQuery( $term_ids = [] ) { |
| 715 |
$terms = get_terms( |
| 716 |
[ |
| 717 |
'term_taxonomy_id' => $term_ids, |
| 718 |
'hide_empty' => false, |
| 719 |
] |
| 720 |
); |
| 721 |
|
| 722 |
$tax_terms_map = []; |
| 723 |
|
| 724 |
foreach ( $terms as $term ) { |
| 725 |
$taxonomy = $term->taxonomy; |
| 726 |
$tax_terms_map[ $taxonomy ][] = $term->term_id; |
| 727 |
} |
| 728 |
|
| 729 |
return $tax_terms_map; |
| 730 |
} |
| 731 |
|
| 732 |
/** |
| 733 |
* @return array|string[]|\WP_Post_Type[] |
| 734 |
*/ |
| 735 |
private function getGroupControlQueryPostTypes() { |
| 736 |
|
| 737 |
$post_types = [ |
| 738 |
'product' => __( 'Product', 'ultimate-store-kit' ), |
| 739 |
'manual_selection' => __( 'Manual Selection', 'ultimate-store-kit' ), |
| 740 |
'current_query' => __( 'Current Query', 'ultimate-store-kit' ), |
| 741 |
'_related_post_type' => __( 'Related', 'ultimate-store-kit' ), |
| 742 |
]; |
| 743 |
|
| 744 |
// $post_types = array_merge($post_types, $extra_types); |
| 745 |
|
| 746 |
return $post_types; |
| 747 |
} |
| 748 |
|
| 749 |
public function pre_get_posts_query_filter( $wp_query ) { |
| 750 |
if ( $this ) { |
| 751 |
$query_id = $this->get_settings( 'query_id' ); |
| 752 |
do_action( "ultimate_store_kit/query/{$query_id}", $wp_query, $this ); |
| 753 |
} |
| 754 |
} |
| 755 |
} |
| 756 |
|