← All changes
|
includes/controls/group-query/group-control-query.php
+85
-29
4.0.9
→
4.5.6
View file →
| @@ -24,8 +24,38 @@ | ||
| 24 | 24 | ] |
| 25 | 25 | ); |
| 26 | 26 | |
| 27 | 27 | $this->add_control( |
| 28 | + 'posts_source_description', | |
| 29 | + [ | |
| 30 | + 'label' => '', | |
| 31 | + 'type' => Controls_Manager::RAW_HTML, | |
| 32 | + 'raw' => __('This current query may not be display posts in the editor. Please save and refresh the page to see the posts.', 'ultimate-post-kit'), | |
| 33 | + 'content_classes' => 'elementor-panel-alert elementor-panel-alert-info', | |
| 34 | + 'condition' => [ | |
| 35 | + 'posts_source' => 'current_query', | |
| 36 | + ], | |
| 37 | + ] | |
| 38 | + ); | |
| 39 | + | |
| 40 | + $this->add_control( | |
| 41 | + 'posts_source_description_2', | |
| 42 | + [ | |
| 43 | + 'label' => '', | |
| 44 | + 'type' => Controls_Manager::RAW_HTML, | |
| 45 | + 'raw' => sprintf( | |
| 46 | + /* translators: 1: HTML link to options-reading.php */ | |
| 47 | + __('Posts per page is global for current query. Set <strong>"Blog pages show at most"</strong> from settings page of WordPress. Go to <a href="%s" target="_blank" rel="noopener">Settings > Reading</a>.', 'ultimate-post-kit'), | |
| 48 | + esc_url(admin_url('options-reading.php')) | |
| 49 | + ), | |
| 50 | + 'content_classes' => 'elementor-panel-alert elementor-panel-alert-info', | |
| 51 | + 'condition' => [ | |
| 52 | + 'posts_source' => 'current_query', | |
| 53 | + ], | |
| 54 | + ] | |
| 55 | + ); | |
| 56 | + | |
| 57 | + $this->add_control( | |
| 28 | 58 | 'posts_selected_ids', |
| 29 | 59 | [ |
| 30 | 60 | 'label' => __('Search & Select', 'ultimate-post-kit'), |
| 31 | 61 | 'type' => Dynamic_Select::TYPE, |
| @@ -358,8 +388,9 @@ | ||
| 358 | 388 | * Set Feature Images |
| 359 | 389 | */ |
| 360 | 390 | |
| 361 | 391 | if ($this->get_settings_for_display('posts_only_with_featured_image') === 'yes') { |
| 392 | + // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Elementor widget query built from user-configured controls; expected behaviour. | |
| 362 | 393 | $args['meta_key'] = '_thumbnail_id'; |
| 363 | 394 | } |
| 364 | 395 | |
| 365 | 396 | /** |
| @@ -439,8 +470,9 @@ | ||
| 439 | 470 | ) { |
| 440 | 471 | $args['ignore_sticky_posts'] = true; |
| 441 | 472 | |
| 442 | 473 | if (in_array('current_post', $exclude_by)) { |
| 474 | + // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in -- Elementor widget query built from user-configured controls; expected behaviour. | |
| 443 | 475 | $args['post__not_in'] = [get_the_ID()]; |
| 444 | 476 | } |
| 445 | 477 | } |
| 446 | 478 | |
| @@ -471,8 +503,9 @@ | ||
| 471 | 503 | $args['post_type'] = get_post_type($related_post_id); |
| 472 | 504 | |
| 473 | 505 | $exclude_by = $this->getGroupControlQueryParamBy('exclude'); |
| 474 | 506 | if (in_array('current_post', $exclude_by)) { |
| 507 | + // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in -- Elementor widget query built from user-configured controls; expected behaviour. | |
| 475 | 508 | $args['post__not_in'] = [get_the_ID()]; |
| 476 | 509 | } |
| 477 | 510 | |
| 478 | 511 | /** |
| @@ -505,8 +538,9 @@ | ||
| 505 | 538 | } |
| 506 | 539 | |
| 507 | 540 | if (in_array('manual_selection', $exclude_by)) { |
| 508 | 541 | $exclude_ids = $settings['posts_exclude_ids']; |
| 542 | + // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in -- Elementor widget query built from user-configured controls; expected behaviour. | |
| 509 | 543 | $args['post__not_in'] = array_merge($current_post, wp_parse_id_list($exclude_ids)); |
| 510 | 544 | } |
| 511 | 545 | |
| 512 | 546 | /** |
| @@ -520,15 +554,21 @@ | ||
| 520 | 554 | $args = $this->getTermsArgs($args, $settings); |
| 521 | 555 | } |
| 522 | 556 | |
| 523 | 557 | if ($this->get_settings_for_display('query_id')) { |
| 558 | + $args['upk_widget_query'] = true; | |
| 524 | 559 | add_action('pre_get_posts', [$this, 'pre_get_posts_query_filter']); |
| 525 | 560 | } |
| 526 | 561 | |
| 527 | 562 | // fixing custom offset |
| 528 | 563 | ## https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination |
| 529 | - add_action('pre_get_posts', [$this, 'fix_query'], 1); | |
| 530 | - add_filter('found_posts', [$this, 'prefix_adjust_offset_pagination'], 1, 2); | |
| 564 | + // Only register the fixers when this widget actually uses an offset, and | |
| 565 | + // scope them to this widget's own query so they never affect other | |
| 566 | + // queries (e.g. another post widget rendered later on the same page). | |
| 567 | + if (!empty($args['offset_to_fix'])) { | |
| 568 | + add_action('pre_get_posts', [$this, 'fix_query'], 1); | |
| 569 | + add_filter('found_posts', [$this, 'prefix_adjust_offset_pagination'], 1, 2); | |
| 570 | + } | |
| 531 | 571 | |
| 532 | 572 | return $args; |
| 533 | 573 | } |
| 534 | 574 | |
| @@ -603,8 +643,9 @@ | ||
| 603 | 643 | } |
| 604 | 644 | } |
| 605 | 645 | |
| 606 | 646 | if (!empty($terms_query)) { |
| 647 | + // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Elementor widget query built from user-configured controls; expected behaviour. | |
| 607 | 648 | $args['tax_query'] = $terms_query; |
| 608 | 649 | $args['tax_query']['relation'] = 'AND'; |
| 609 | 650 | } |
| 610 | 651 | |
| @@ -626,8 +667,9 @@ | ||
| 626 | 667 | * @return array|mixed |
| 627 | 668 | */ |
| 628 | 669 | private function getGroupControlQueryParamBy($by = 'exclude') { |
| 629 | 670 | $mapBy = [ |
| 671 | + // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- Elementor widget query built from user-configured controls; expected behaviour. | |
| 630 | 672 | 'exclude' => 'posts_exclude_by', |
| 631 | 673 | 'include' => 'posts_include_by', |
| 632 | 674 | ]; |
| 633 | 675 | |
| @@ -690,49 +732,63 @@ | ||
| 690 | 732 | /** |
| 691 | 733 | * @param WP_Query $query fix the offset |
| 692 | 734 | */ |
| 693 | 735 | |
| 694 | - function fix_query(&$query) { | |
| 736 | + function fix_query( &$query ) { | |
| 695 | 737 | |
| 696 | - $settings = $this->get_settings_for_display(); | |
| 738 | + // Only touch this widget's own query. The flag travels with the query | |
| 739 | + // args, so unrelated queries (other widgets, the main loop, etc.) are | |
| 740 | + // left untouched even if this callback is still attached. | |
| 741 | + if ( ! isset( $query->query_vars['offset_to_fix'] ) ) { | |
| 742 | + return; | |
| 743 | + } | |
| 697 | 744 | |
| 698 | - $query->set('order', $settings['posts_order']); | |
| 699 | - $query->set('orderby', $settings['posts_orderby']); | |
| 745 | + // Self-remove so this only applies to a single query and never leaks | |
| 746 | + // into subsequent queries on the same request. | |
| 747 | + remove_action( 'pre_get_posts', [ $this, 'fix_query' ], 1 ); | |
| 700 | 748 | |
| 701 | - if (isset($query->query_vars['offset_to_fix'])) { | |
| 749 | + $offset = (int) $query->query_vars['offset_to_fix']; | |
| 702 | 750 | |
| 703 | - if ($query->is_paged) { | |
| 704 | - $page_offset = $query->query_vars['offset_to_fix'] + (($query->query_vars['paged'] - 1) * $query->query_vars['posts_per_page']); | |
| 705 | - $query->set('offset', $page_offset); | |
| 706 | - } else { | |
| 707 | - $query->set('offset', $query->query_vars['offset_to_fix']); | |
| 708 | - } | |
| 751 | + $posts_per_page = isset( $query->query_vars['posts_per_page'] ) | |
| 752 | + ? (int) $query->query_vars['posts_per_page'] | |
| 753 | + : (int) get_option( 'posts_per_page', 10 ); | |
| 754 | + | |
| 755 | + if ( $query->is_paged ) { | |
| 756 | + $page_offset = $offset + ( ( $query->query_vars['paged'] - 1 ) * $posts_per_page ); | |
| 757 | + $query->set( 'offset', $page_offset ); | |
| 709 | 758 | } else { |
| 710 | - if ($query->is_paged) { | |
| 711 | - $page_offset = (int) $settings['posts_offset'] + (($query->query_vars['paged'] - 1) * $query->query_vars['posts_per_page']); | |
| 712 | - $query->set('offset', $page_offset); | |
| 713 | - } else { | |
| 714 | - $query->set('offset', $settings['posts_offset']); | |
| 715 | - } | |
| 759 | + $query->set( 'offset', $offset ); | |
| 716 | 760 | } |
| 717 | 761 | } |
| 718 | 762 | |
| 719 | - public function prefix_adjust_offset_pagination($found_posts, $query) { | |
| 763 | + public function prefix_adjust_offset_pagination( $found_posts, $query ) { | |
| 720 | 764 | |
| 721 | - if (isset($query->query_vars['offset_to_fix'])) { | |
| 722 | - $offset_to_fix = intval($query->query_vars['offset_to_fix']); | |
| 765 | + if ( ! isset( $query->query_vars['offset_to_fix'] ) ) { | |
| 766 | + return $found_posts; | |
| 767 | + } | |
| 723 | 768 | |
| 724 | - if ($offset_to_fix) { | |
| 725 | - $found_posts -= $offset_to_fix; | |
| 726 | - } | |
| 769 | + remove_filter( 'found_posts', [ $this, 'prefix_adjust_offset_pagination' ], 1 ); | |
| 770 | + | |
| 771 | + $offset_to_fix = intval( $query->query_vars['offset_to_fix'] ); | |
| 772 | + | |
| 773 | + if ( $offset_to_fix ) { | |
| 774 | + $found_posts -= $offset_to_fix; | |
| 727 | 775 | } |
| 728 | 776 | |
| 729 | 777 | return $found_posts; |
| 730 | 778 | } |
| 731 | 779 | |
| 732 | - public function pre_get_posts_query_filter($wp_query) { | |
| 733 | - if ($this) { | |
| 734 | - $query_id = $this->get_settings_for_display('query_id'); | |
| 735 | - do_action("ultimate_post_kit_pro/query/{$query_id}", $wp_query, $this); | |
| 780 | + public function pre_get_posts_query_filter( $wp_query ) { | |
| 781 | + | |
| 782 | + if ( empty( $wp_query->get( 'upk_widget_query' ) ) ) { | |
| 783 | + return; | |
| 784 | + } | |
| 785 | + | |
| 786 | + remove_action( 'pre_get_posts', [ $this, 'pre_get_posts_query_filter' ] ); | |
| 787 | + | |
| 788 | + $query_id = $this->get_settings_for_display( 'query_id' ); | |
| 789 | + | |
| 790 | + if ( $query_id ) { | |
| 791 | + do_action( "ultimate_post_kit_pro/query/{$query_id}", $wp_query, $this ); | |
| 736 | 792 | } |
| 737 | 793 | } |
| 738 | 794 | } |