auxin-elements
/
includes
/
elementor
/
modules
/
query-control
/
classes
/
elementor-related-query.php
elementor-related-query.php
152 lines
| 1 | <?php |
| 2 | namespace Auxin\Plugin\CoreElements\Elementor\Modules\QueryControl\Classes; |
| 3 | |
| 4 | use Elementor\Widget_Base; |
| 5 | |
| 6 | class Elementor_Related_Query extends Elementor_Post_Query { |
| 7 | private $fallback_args; |
| 8 | private $related_post_id; |
| 9 | |
| 10 | /** |
| 11 | * Elementor_Post_Query constructor. |
| 12 | * |
| 13 | * @param Widget_Base $widget |
| 14 | * @param string $group_query_name |
| 15 | * @param array $query_args |
| 16 | * @param array $fallback_args |
| 17 | */ |
| 18 | public function __construct( $widget, $group_query_name, $query_args = [], $fallback_args = [] ) { |
| 19 | parent::__construct( $widget, $group_query_name, $query_args ); |
| 20 | $this->related_post_id = -1; |
| 21 | $this->fallback_args = $fallback_args; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * 1) build query args |
| 26 | * 2) invoke callback to fine-tune query-args |
| 27 | * 3) generate WP_Query object |
| 28 | * 4) if no results & fallback is set, generate a new WP_Query with fallback args |
| 29 | * 5) return WP_Query |
| 30 | * |
| 31 | * @return \WP_Query |
| 32 | */ |
| 33 | public function get_query() { |
| 34 | $query = parent::get_query(); |
| 35 | |
| 36 | if ( ! $query->post_count && $this->is_valid_fallback() ) { |
| 37 | $query = $this->get_fallback_query( $query ); |
| 38 | } |
| 39 | |
| 40 | return $query; |
| 41 | } |
| 42 | |
| 43 | protected function get_fallback_query( $original_query ) { |
| 44 | $this->set_fallback_query_args(); |
| 45 | $this->set_fallback_arg_by_settings( 'posts_per_page', $original_query->query_vars['posts_per_page'] ); |
| 46 | $this->fallback_args = apply_filters( 'elementor/query/fallback_query_args', $this->fallback_args, $this->widget ); |
| 47 | |
| 48 | return new \WP_Query( $this->fallback_args ); |
| 49 | } |
| 50 | |
| 51 | private function is_valid_fallback() { |
| 52 | $related_callback = $this->get_widget_settings( 'related_fallback' ); |
| 53 | if ( empty( $related_callback ) ) { |
| 54 | return false; |
| 55 | } |
| 56 | $valid = false; |
| 57 | switch ( $this->get_widget_settings( 'related_fallback' ) ) { |
| 58 | case 'fallback_recent': |
| 59 | $valid = true; |
| 60 | break; |
| 61 | case 'fallback_by_id': |
| 62 | $fallback_id = $this->get_widget_settings( 'fallback_ids' ); |
| 63 | if ( ! empty( $fallback_id ) ) { |
| 64 | $valid = true; |
| 65 | } |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | return $valid; |
| 70 | } |
| 71 | |
| 72 | protected function set_common_args() { |
| 73 | parent::set_common_args(); |
| 74 | $post_id = get_queried_object_id(); |
| 75 | $this->related_post_id = is_singular() && ( 0 !== $post_id ) ? $post_id : null; |
| 76 | $this->query_args['post_type'] = get_post_type( $this->related_post_id ); |
| 77 | } |
| 78 | |
| 79 | protected function set_post_exclude_args() { |
| 80 | parent::set_post_exclude_args(); |
| 81 | |
| 82 | if ( $this->related_post_id ) { |
| 83 | $post_not_in = isset( $this->query_args['post__not_in'] ) ? $this->query_args['post__not_in'] : []; |
| 84 | $post_not_in[] = $this->related_post_id; |
| 85 | $this->query_args['post__not_in'] = $post_not_in; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | protected function build_terms_query_include( $control_id ) { |
| 90 | /** |
| 91 | * Build tax_query for the "related posts" query: |
| 92 | * 1) find the list of taxonomies associated with the current-post |
| 93 | * 2) extract the ids for each taxonomy |
| 94 | * 3) build tax_query array accordingly |
| 95 | * |
| 96 | */ |
| 97 | if ( null === $this->get_widget_settings( 'include' ) || null === $this->get_widget_settings( 'related_taxonomies' ) || ! $this->maybe_in_array( 'terms', $this->get_widget_settings( 'include' ) ) ) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | $taxonomies = $this->get_widget_settings( 'related_taxonomies' ); |
| 102 | $terms = []; |
| 103 | if ( is_string( $taxonomies ) ) { |
| 104 | $terms[ $taxonomies ] = wp_get_post_terms( $this->related_post_id, $taxonomies, [ 'fields' => 'tt_ids' ] ); |
| 105 | } else { |
| 106 | foreach ( $taxonomies as $taxonomy ) { |
| 107 | $terms[ $taxonomy ] = wp_get_post_terms( $this->related_post_id, $taxonomy, [ 'fields' => 'tt_ids' ] ); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | $this->insert_tax_query( $terms, false ); |
| 112 | } |
| 113 | |
| 114 | protected function set_author_args() { |
| 115 | if ( ! $this->maybe_in_array( 'authors', $this->get_widget_settings( 'include' ) ) ) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | $this->query_args['author__in'] = get_post_field( 'post_author', $this->related_post_id ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @param string $key |
| 124 | * @param mixed $value |
| 125 | * @param string $control_name |
| 126 | */ |
| 127 | private function set_fallback_arg_by_settings( $key, $value, $control_name = '' ) { |
| 128 | if ( empty( $this->fallback_args[ $key ] ) ) { |
| 129 | $settings = $this->widget->get_settings(); |
| 130 | $this->fallback_args[ $key ] = ( '' === $control_name || empty( $settings[ $this->prefix . $control_name ] ) ) ? $value : $settings[ $this->prefix . $control_name ]; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | protected function set_fallback_query_args() { |
| 135 | $this->set_fallback_arg_by_settings( 'ignore_sticky_posts', true ); |
| 136 | $this->set_fallback_arg_by_settings( 'post_status', 'publish' ); |
| 137 | |
| 138 | $post_types = Utils::get_public_post_types(); |
| 139 | $post_types = array_keys( $post_types ); |
| 140 | |
| 141 | $this->set_fallback_arg_by_settings( 'post_type', $post_types ); |
| 142 | |
| 143 | if ( 'fallback_by_id' === $this->get_widget_settings( 'related_fallback' ) ) { |
| 144 | $this->set_fallback_arg_by_settings( 'post__in', [ 0 ], 'fallback_ids' ); |
| 145 | $this->set_fallback_arg_by_settings( 'orderby', 'rand' ); |
| 146 | } else { //recent posts |
| 147 | $this->set_fallback_arg_by_settings( 'orderby', 'date' ); |
| 148 | $this->set_fallback_arg_by_settings( 'order', 'DESC' ); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 |