auxin-elements
/
includes
/
elementor
/
modules
/
query-control
/
controls
/
group-control-query.php
group-control-posts.php
4 years ago
group-control-query.php
6 years ago
group-control-related.php
6 years ago
query.php
6 years ago
group-control-query.php
543 lines
| 1 | <?php |
| 2 | namespace Auxin\Plugin\CoreElements\Elementor\Modules\QueryControl\Controls; |
| 3 | |
| 4 | use Elementor\Controls_Manager; |
| 5 | use Elementor\Group_Control_Base; |
| 6 | |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | exit; // Exit if accessed directly |
| 9 | } |
| 10 | |
| 11 | class Group_Control_Query extends Group_Control_Base { |
| 12 | |
| 13 | protected static $presets; |
| 14 | protected static $fields; |
| 15 | |
| 16 | public static function get_type() { |
| 17 | return 'aux-query-group'; |
| 18 | } |
| 19 | |
| 20 | protected function init_args( $args ) { |
| 21 | parent::init_args( $args ); |
| 22 | $args = $this->get_args(); |
| 23 | static::$fields = $this->init_fields_by_name( $args['name'] ); |
| 24 | } |
| 25 | |
| 26 | protected function init_fields() { |
| 27 | $args = $this->get_args(); |
| 28 | |
| 29 | return $this->init_fields_by_name( $args['name'] ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Build the group-controls array |
| 34 | * Note: this method completely overrides any settings done in Group_Control_Posts |
| 35 | * @param string $name |
| 36 | * |
| 37 | * @return array |
| 38 | */ |
| 39 | protected function init_fields_by_name( $name ) { |
| 40 | $fields = []; |
| 41 | |
| 42 | $name .= '_'; |
| 43 | |
| 44 | $fields['post_type'] = [ |
| 45 | 'label' => __( 'Source', 'auxin-elements' ), |
| 46 | 'type' => Controls_Manager::SELECT, |
| 47 | 'options' => [ |
| 48 | 'by_id' => __( 'Manual Selection', 'auxin-elements' ), |
| 49 | 'current_query' => __( 'Current Query', 'auxin-elements' ), |
| 50 | ], |
| 51 | ]; |
| 52 | |
| 53 | $fields['query_args'] = [ |
| 54 | 'type' => Controls_Manager::TABS, |
| 55 | ]; |
| 56 | |
| 57 | $tabs_wrapper = $name . 'query_args'; |
| 58 | $include_wrapper = $name . 'query_include'; |
| 59 | $exclude_wrapper = $name . 'query_exclude'; |
| 60 | |
| 61 | $fields['query_include'] = [ |
| 62 | 'type' => Controls_Manager::TAB, |
| 63 | 'label' => __( 'Include', 'auxin-elements' ), |
| 64 | 'tabs_wrapper' => $tabs_wrapper, |
| 65 | 'condition' => [ |
| 66 | 'post_type!' => [ |
| 67 | 'current_query', |
| 68 | 'by_id', |
| 69 | ], |
| 70 | ], |
| 71 | ]; |
| 72 | |
| 73 | $fields['posts_ids'] = [ |
| 74 | 'label' => __( 'Search & Select', 'auxin-elements' ), |
| 75 | 'type' => 'aux-query', |
| 76 | 'post_type' => '', |
| 77 | 'options' => [], |
| 78 | 'label_block' => true, |
| 79 | 'multiple' => true, |
| 80 | 'filter_type' => 'by_id', |
| 81 | 'condition' => [ |
| 82 | 'post_type' => 'by_id', |
| 83 | ], |
| 84 | 'tabs_wrapper' => $tabs_wrapper, |
| 85 | 'inner_tab' => $include_wrapper, |
| 86 | 'export' => false, |
| 87 | ]; |
| 88 | |
| 89 | $fields['include'] = [ |
| 90 | 'label' => __( 'Include By', 'auxin-elements' ), |
| 91 | 'type' => Controls_Manager::SELECT2, |
| 92 | 'multiple' => true, |
| 93 | 'options' => [ |
| 94 | 'terms' => __( 'Term', 'auxin-elements' ), |
| 95 | 'authors' => __( 'Author', 'auxin-elements' ), |
| 96 | ], |
| 97 | 'condition' => [ |
| 98 | 'post_type!' => [ |
| 99 | 'by_id', |
| 100 | 'current_query', |
| 101 | ], |
| 102 | ], |
| 103 | 'label_block' => true, |
| 104 | 'tabs_wrapper' => $tabs_wrapper, |
| 105 | 'inner_tab' => $include_wrapper, |
| 106 | ]; |
| 107 | |
| 108 | $fields['include_term_ids'] = [ |
| 109 | 'label' => __( 'Term', 'auxin-elements' ), |
| 110 | 'type' => 'aux-query', |
| 111 | 'post_type' => '', |
| 112 | 'options' => [], |
| 113 | 'label_block' => true, |
| 114 | 'multiple' => true, |
| 115 | 'filter_type' => 'cpt_taxonomies', |
| 116 | 'group_prefix' => $name, |
| 117 | 'include_type' => true, |
| 118 | 'condition' => [ |
| 119 | 'include' => 'terms', |
| 120 | 'post_type!' => [ |
| 121 | 'by_id', |
| 122 | 'current_query', |
| 123 | ], |
| 124 | ], |
| 125 | 'tabs_wrapper' => $tabs_wrapper, |
| 126 | 'inner_tab' => $include_wrapper, |
| 127 | ]; |
| 128 | |
| 129 | $fields['include_authors'] = [ |
| 130 | 'label' => __( 'Author', 'auxin-elements' ), |
| 131 | 'label_block' => true, |
| 132 | 'type' => 'aux-query', |
| 133 | 'multiple' => true, |
| 134 | 'default' => [], |
| 135 | 'options' => [], |
| 136 | 'filter_type' => 'author', |
| 137 | 'condition' => [ |
| 138 | 'include' => 'authors', |
| 139 | 'post_type!' => [ |
| 140 | 'by_id', |
| 141 | 'current_query', |
| 142 | ], |
| 143 | ], |
| 144 | 'tabs_wrapper' => $tabs_wrapper, |
| 145 | 'inner_tab' => $include_wrapper, |
| 146 | 'export' => false, |
| 147 | ]; |
| 148 | |
| 149 | $fields['query_exclude'] = [ |
| 150 | 'type' => Controls_Manager::TAB, |
| 151 | 'label' => __( 'Exclude', 'auxin-elements' ), |
| 152 | 'tabs_wrapper' => $tabs_wrapper, |
| 153 | 'condition' => [ |
| 154 | 'post_type!' => [ |
| 155 | 'by_id', |
| 156 | 'current_query', |
| 157 | ], |
| 158 | ], |
| 159 | ]; |
| 160 | |
| 161 | $fields['exclude'] = [ |
| 162 | 'label' => __( 'Exclude By', 'auxin-elements' ), |
| 163 | 'type' => Controls_Manager::SELECT2, |
| 164 | 'multiple' => true, |
| 165 | 'options' => [ |
| 166 | 'current_post' => __( 'Current Post', 'auxin-elements' ), |
| 167 | 'manual_selection' => __( 'Manual Selection', 'auxin-elements' ), |
| 168 | 'terms' => __( 'Term', 'auxin-elements' ), |
| 169 | 'authors' => __( 'Author', 'auxin-elements' ), |
| 170 | ], |
| 171 | 'condition' => [ |
| 172 | 'post_type!' => [ |
| 173 | 'by_id', |
| 174 | 'current_query', |
| 175 | ], |
| 176 | ], |
| 177 | 'label_block' => true, |
| 178 | 'tabs_wrapper' => $tabs_wrapper, |
| 179 | 'inner_tab' => $exclude_wrapper, |
| 180 | ]; |
| 181 | |
| 182 | $fields['exclude_ids'] = [ |
| 183 | 'label' => __( 'Search & Select', 'auxin-elements' ), |
| 184 | 'type' => 'aux-query', |
| 185 | 'post_type' => '', |
| 186 | 'options' => [], |
| 187 | 'label_block' => true, |
| 188 | 'multiple' => true, |
| 189 | 'filter_type' => 'by_id', |
| 190 | 'condition' => [ |
| 191 | 'exclude' => 'manual_selection', |
| 192 | 'post_type!' => [ |
| 193 | 'by_id', |
| 194 | 'current_query', |
| 195 | ], |
| 196 | ], |
| 197 | 'tabs_wrapper' => $tabs_wrapper, |
| 198 | 'inner_tab' => $exclude_wrapper, |
| 199 | 'export' => false, |
| 200 | ]; |
| 201 | |
| 202 | $fields['exclude_term_ids'] = [ |
| 203 | 'label' => __( 'Term', 'auxin-elements' ), |
| 204 | 'type' => 'aux-query', |
| 205 | 'post_type' => '', |
| 206 | 'options' => [], |
| 207 | 'label_block' => true, |
| 208 | 'multiple' => true, |
| 209 | 'filter_type' => 'cpt_taxonomies', |
| 210 | 'group_prefix' => $name, |
| 211 | 'include_type' => true, |
| 212 | 'condition' => [ |
| 213 | 'exclude' => 'terms', |
| 214 | 'post_type!' => [ |
| 215 | 'by_id', |
| 216 | 'current_query', |
| 217 | ], |
| 218 | ], |
| 219 | 'tabs_wrapper' => $tabs_wrapper, |
| 220 | 'inner_tab' => $exclude_wrapper, |
| 221 | 'export' => false, |
| 222 | ]; |
| 223 | |
| 224 | $fields['exclude_authors'] = [ |
| 225 | 'label' => __( 'Author', 'auxin-elements' ), |
| 226 | 'type' => 'aux-query', |
| 227 | 'post_type' => '', |
| 228 | 'options' => [], |
| 229 | 'label_block' => true, |
| 230 | 'multiple' => true, |
| 231 | 'filter_type' => 'author', |
| 232 | 'include_type' => true, |
| 233 | 'condition' => [ |
| 234 | 'exclude' => 'authors', |
| 235 | 'post_type!' => [ |
| 236 | 'by_id', |
| 237 | 'current_query', |
| 238 | ], |
| 239 | ], |
| 240 | 'tabs_wrapper' => $tabs_wrapper, |
| 241 | 'inner_tab' => $exclude_wrapper, |
| 242 | 'export' => false, |
| 243 | ]; |
| 244 | |
| 245 | $fields['avoid_duplicates'] = [ |
| 246 | 'label' => __( 'Avoid Duplicates', 'auxin-elements' ), |
| 247 | 'type' => Controls_Manager::SWITCHER, |
| 248 | 'default' => '', |
| 249 | 'description' => __( 'Set to Yes to avoid duplicate posts from showing up. This only effects the frontend.', 'auxin-elements' ), |
| 250 | 'tabs_wrapper' => $tabs_wrapper, |
| 251 | 'inner_tab' => $exclude_wrapper, |
| 252 | 'condition' => [ |
| 253 | 'post_type!' => [ |
| 254 | 'by_id', |
| 255 | 'current_query', |
| 256 | ], |
| 257 | ], |
| 258 | ]; |
| 259 | |
| 260 | $fields['offset'] = [ |
| 261 | 'label' => __( 'Offset', 'auxin-elements' ), |
| 262 | 'type' => Controls_Manager::NUMBER, |
| 263 | 'default' => 0, |
| 264 | 'condition' => [ |
| 265 | 'post_type!' => [ |
| 266 | 'by_id', |
| 267 | 'current_query', |
| 268 | ], |
| 269 | ], |
| 270 | 'description' => __( 'Use this setting to skip over posts (e.g. \'2\' to skip over 2 posts).', 'auxin-elements' ), |
| 271 | 'tabs_wrapper' => $tabs_wrapper, |
| 272 | 'inner_tab' => $exclude_wrapper, |
| 273 | ]; |
| 274 | |
| 275 | $fields['select_date'] = [ |
| 276 | 'label' => __( 'Date', 'auxin-elements' ), |
| 277 | 'type' => Controls_Manager::SELECT, |
| 278 | 'post_type' => '', |
| 279 | 'options' => [ |
| 280 | 'anytime' => __( 'All', 'auxin-elements' ), |
| 281 | 'today' => __( 'Past Day', 'auxin-elements' ), |
| 282 | 'week' => __( 'Past Week', 'auxin-elements' ), |
| 283 | 'month' => __( 'Past Month', 'auxin-elements' ), |
| 284 | 'quarter' => __( 'Past Quarter', 'auxin-elements' ), |
| 285 | 'year' => __( 'Past Year', 'auxin-elements' ), |
| 286 | 'exact' => __( 'Custom', 'auxin-elements' ), |
| 287 | ], |
| 288 | 'default' => 'anytime', |
| 289 | 'label_block' => false, |
| 290 | 'multiple' => false, |
| 291 | 'filter_type' => 'date', |
| 292 | 'include_type' => true, |
| 293 | 'condition' => [ |
| 294 | 'post_type!' => [ |
| 295 | 'by_id', |
| 296 | 'current_query', |
| 297 | ], |
| 298 | ], |
| 299 | 'separator' => 'before', |
| 300 | ]; |
| 301 | |
| 302 | $fields['date_before'] = [ |
| 303 | 'label' => __( 'Before', 'auxin-elements' ), |
| 304 | 'type' => Controls_Manager::DATE_TIME, |
| 305 | 'post_type' => '', |
| 306 | 'label_block' => false, |
| 307 | 'multiple' => false, |
| 308 | 'filter_type' => 'date', |
| 309 | 'include_type' => true, |
| 310 | 'placeholder' => __( 'Choose', 'auxin-elements' ), |
| 311 | 'condition' => [ |
| 312 | 'select_date' => 'exact', |
| 313 | 'post_type!' => [ |
| 314 | 'by_id', |
| 315 | 'current_query', |
| 316 | ], |
| 317 | ], |
| 318 | 'description' => __( 'Setting a ‘Before’ date will show all the posts published until the chosen date (inclusive).', 'auxin-elements' ), |
| 319 | ]; |
| 320 | |
| 321 | $fields['date_after'] = [ |
| 322 | 'label' => __( 'After', 'auxin-elements' ), |
| 323 | 'type' => Controls_Manager::DATE_TIME, |
| 324 | 'post_type' => '', |
| 325 | 'label_block' => false, |
| 326 | 'multiple' => false, |
| 327 | 'filter_type' => 'date', |
| 328 | 'include_type' => true, |
| 329 | 'placeholder' => __( 'Choose', 'auxin-elements' ), |
| 330 | 'condition' => [ |
| 331 | 'select_date' => 'exact', |
| 332 | 'post_type!' => [ |
| 333 | 'by_id', |
| 334 | 'current_query', |
| 335 | ], |
| 336 | ], |
| 337 | 'description' => __( 'Setting an ‘After’ date will show all the posts published since the chosen date (inclusive).', 'auxin-elements' ), |
| 338 | ]; |
| 339 | |
| 340 | $fields['orderby'] = [ |
| 341 | 'label' => __( 'Order By', 'auxin-elements' ), |
| 342 | 'type' => Controls_Manager::SELECT, |
| 343 | 'default' => 'post_date', |
| 344 | 'options' => [ |
| 345 | 'post_date' => __( 'Date', 'auxin-elements' ), |
| 346 | 'post_title' => __( 'Title', 'auxin-elements' ), |
| 347 | 'menu_order' => __( 'Menu Order', 'auxin-elements' ), |
| 348 | 'rand' => __( 'Random', 'auxin-elements' ), |
| 349 | ], |
| 350 | 'condition' => [ |
| 351 | 'post_type!' => 'current_query', |
| 352 | ], |
| 353 | ]; |
| 354 | |
| 355 | $fields['order'] = [ |
| 356 | 'label' => __( 'Order', 'auxin-elements' ), |
| 357 | 'type' => Controls_Manager::SELECT, |
| 358 | 'default' => 'desc', |
| 359 | 'options' => [ |
| 360 | 'asc' => __( 'ASC', 'auxin-elements' ), |
| 361 | 'desc' => __( 'DESC', 'auxin-elements' ), |
| 362 | ], |
| 363 | 'condition' => [ |
| 364 | 'post_type!' => 'current_query', |
| 365 | ], |
| 366 | ]; |
| 367 | |
| 368 | $fields['posts_per_page'] = [ |
| 369 | 'label' => __( 'Posts Per Page', 'auxin-elements' ), |
| 370 | 'type' => Controls_Manager::NUMBER, |
| 371 | 'default' => 3, |
| 372 | 'condition' => [ |
| 373 | 'post_type!' => 'current_query', |
| 374 | ], |
| 375 | ]; |
| 376 | |
| 377 | $fields['ignore_sticky_posts'] = [ |
| 378 | 'label' => __( 'Ignore Sticky Posts', 'auxin-elements' ), |
| 379 | 'type' => Controls_Manager::SWITCHER, |
| 380 | 'default' => 'yes', |
| 381 | 'condition' => [ |
| 382 | 'post_type' => 'post', |
| 383 | ], |
| 384 | 'description' => __( 'Sticky-posts ordering is visible on frontend only', 'auxin-elements' ), |
| 385 | ]; |
| 386 | |
| 387 | $fields['query_id'] = [ |
| 388 | 'label' => __( 'Query ID', 'auxin-elements' ), |
| 389 | 'type' => Controls_Manager::TEXT, |
| 390 | 'default' => '', |
| 391 | 'description' => __( 'Give your Query a custom unique id to allow server side filtering', 'auxin-elements' ), |
| 392 | 'separator' => 'before', |
| 393 | ]; |
| 394 | |
| 395 | static::init_presets(); |
| 396 | |
| 397 | return $fields; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Presets: filter controls subsets to be be used by the specific Group_Control_Query instance. |
| 402 | * |
| 403 | * Possible values: |
| 404 | * 'full' : (default) all presets |
| 405 | * 'include' : the 'include' tab - by id, by taxonomy, by author |
| 406 | * 'exclude': the 'exclude' tab - by id, by taxonomy, by author |
| 407 | * 'advanced_exclude': extend the 'exclude' preset with 'avoid-duplicates' & 'offset' |
| 408 | * 'date': date query controls |
| 409 | * 'pagination': posts per-page |
| 410 | * 'order': sort & ordering controls |
| 411 | * 'query_id': allow saving a specific query for future usage. |
| 412 | * |
| 413 | * Usage: |
| 414 | * full: build a Group_Controls_Query with all possible controls, |
| 415 | * when 'full' is passed, the Group_Controls_Query will ignore all other preset values. |
| 416 | * $this->add_group_control( |
| 417 | * Group_Control_Query::get_type(), |
| 418 | * [ |
| 419 | * ... |
| 420 | * 'presets' => [ 'full' ], |
| 421 | * ... |
| 422 | * ] ); |
| 423 | * |
| 424 | * Subset: build a Group_Controls_Query with subset of the controls, |
| 425 | * in the following example, the Query controls will set only the 'include' & 'date' query args. |
| 426 | * $this->add_group_control( |
| 427 | * Group_Control_Query::get_type(), |
| 428 | * [ |
| 429 | * ... |
| 430 | * 'presets' => [ 'include', 'date' ], |
| 431 | * ... |
| 432 | * ] ); |
| 433 | */ |
| 434 | protected static function init_presets() { |
| 435 | |
| 436 | $tabs = [ |
| 437 | 'query_args', |
| 438 | 'query_include', |
| 439 | 'query_exclude', |
| 440 | ]; |
| 441 | |
| 442 | static::$presets['include'] = array_merge( $tabs, [ |
| 443 | 'include', |
| 444 | 'include_ids', |
| 445 | 'include_term_ids', |
| 446 | 'include_authors', |
| 447 | ] ); |
| 448 | |
| 449 | static::$presets['exclude'] = array_merge( $tabs, [ |
| 450 | 'exclude', |
| 451 | 'exclude_ids', |
| 452 | 'exclude_term_ids', |
| 453 | 'exclude_authors', |
| 454 | ] ); |
| 455 | |
| 456 | static::$presets['advanced_exclude'] = array_merge( static::$presets['exclude'], [ |
| 457 | 'avoid_duplicates', |
| 458 | 'offset', |
| 459 | ] ); |
| 460 | |
| 461 | static::$presets['date'] = [ |
| 462 | 'select_date', |
| 463 | 'date_before', |
| 464 | 'date_after', |
| 465 | ]; |
| 466 | |
| 467 | static::$presets['pagination'] = [ |
| 468 | 'posts_per_page', |
| 469 | 'ignore_sticky_posts', |
| 470 | ]; |
| 471 | |
| 472 | static::$presets['order'] = [ |
| 473 | 'orderby', |
| 474 | 'order', |
| 475 | ]; |
| 476 | |
| 477 | static::$presets['query_id'] = [ |
| 478 | 'query_id', |
| 479 | ]; |
| 480 | } |
| 481 | |
| 482 | private function filter_by_presets( $presets, $fields ) { |
| 483 | |
| 484 | if ( in_array( 'full', $presets, true ) ) { |
| 485 | return $fields; |
| 486 | } |
| 487 | |
| 488 | $control_ids = []; |
| 489 | foreach ( static::$presets as $key => $preset ) { |
| 490 | $control_ids = array_merge( $control_ids, $preset ); |
| 491 | } |
| 492 | |
| 493 | foreach ( $presets as $preset ) { |
| 494 | if ( array_key_exists( $preset, static::$presets ) ) { |
| 495 | $control_ids = array_diff( $control_ids, static::$presets[ $preset ] ); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | foreach ( $control_ids as $remove ) { |
| 500 | unset( $fields[ $remove ] ); |
| 501 | } |
| 502 | |
| 503 | return $fields; |
| 504 | |
| 505 | } |
| 506 | |
| 507 | protected function prepare_fields( $fields ) { |
| 508 | |
| 509 | $args = $this->get_args(); |
| 510 | |
| 511 | if ( ! empty( $args['presets'] ) ) { |
| 512 | $fields = $this->filter_by_presets( $args['presets'], $fields ); |
| 513 | } |
| 514 | |
| 515 | $post_type_args = []; |
| 516 | if ( ! empty( $args['post_type'] ) ) { |
| 517 | $post_type_args['post_type'] = $args['post_type']; |
| 518 | } |
| 519 | |
| 520 | $post_types = auxin_get_public_post_types( $post_type_args ); |
| 521 | |
| 522 | $fields['post_type']['options'] = array_merge( $post_types, $fields['post_type']['options'] ); |
| 523 | $fields['post_type']['default'] = key( $post_types ); |
| 524 | $fields['posts_ids']['object_type'] = array_keys( $post_types ); |
| 525 | |
| 526 | //skip parent, go directly to grandparent |
| 527 | return Group_Control_Base::prepare_fields( $fields ); |
| 528 | } |
| 529 | |
| 530 | protected function get_child_default_args() { |
| 531 | $args = parent::get_child_default_args(); |
| 532 | $args['presets'] = [ 'full' ]; |
| 533 | |
| 534 | return $args; |
| 535 | } |
| 536 | |
| 537 | protected function get_default_options() { |
| 538 | return [ |
| 539 | 'popover' => false, |
| 540 | ]; |
| 541 | } |
| 542 | } |
| 543 |