PluginProbe ʕ •ᴥ•ʔ
Shortcodes and extra features for Phlox theme / 2.5.13
Shortcodes and extra features for Phlox theme v2.5.13
2.17.21 2.17.20 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.6 1.0.9 1.1.0 1.3.0 1.3.1 1.3.10 1.3.14 1.3.2 1.3.3 1.3.6 1.4.0 1.4.1 1.4.2 1.5.0 1.5.2 1.6.0 1.6.2 1.6.4 1.7.0 1.7.2 2.10.0 2.10.1 2.10.3 2.10.5 2.10.7 2.10.8 2.10.9 2.11.0 2.11.1 2.11.2 2.12.0 2.14.0 2.15.0 2.15.2 2.15.4 2.15.5 2.15.6 2.15.7 2.15.8 2.15.9 2.16.0 2.16.1 2.16.2 2.16.3 2.16.4 2.17.0 2.17.1 2.17.12 2.17.13 2.17.14 2.17.15 2.17.16 2.17.2 2.17.3 2.17.4 2.17.5 2.17.6 2.17.8 2.17.9 2.4.12 2.4.13 2.4.14 2.4.16 2.4.18 2.4.19 2.4.9 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.14 2.5.15 2.5.16 2.5.17 2.5.19 2.5.2 2.5.20 2.5.3 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.10 2.6.12 2.6.13 2.6.14 2.6.15 2.6.16 2.6.17 2.6.19 2.6.2 2.6.20 2.6.4 2.6.5 2.6.7 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.9 2.9.0 2.9.12 2.9.14 2.9.15 2.9.16 2.9.17 2.9.18 2.9.19 2.9.2 2.9.20 2.9.21 2.9.22 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8
auxin-elements / includes / elementor / modules / query-control / classes / elementor-post-query.php
auxin-elements / includes / elementor / modules / query-control / classes Last commit date
elementor-post-query.php 6 years ago elementor-related-query.php 6 years ago
elementor-post-query.php
371 lines
1 <?php
2 namespace Auxin\Plugin\CoreElements\Elementor\Modules\QueryControl\Classes;
3
4 use Elementor\Widget_Base;
5 use Auxin\Plugin\CoreElements\Elementor\Modules\QueryControl\Module;
6
7 class Elementor_Post_Query {
8 protected $widget;
9 protected $query_args;
10 protected $prefix;
11 protected $widget_settings;
12
13 /**
14 * Elementor_Post_Query constructor.
15 *
16 * @param Widget_Base $widget
17 * @param string $group_query_name
18 * @param array $query_args
19 */
20 public function __construct( $widget, $group_query_name, $query_args = [] ) {
21 $this->widget = $widget;
22 $this->prefix = $group_query_name . '_';
23 $this->query_args = $query_args;
24
25 $settings = $this->widget->get_settings();
26 $defaults = $this->get_query_defaults();
27
28 $this->widget_settings = wp_parse_args( $settings, $defaults );
29 }
30
31 /**
32 * 1) build query args
33 * 2) invoke callback to fine-tune query-args
34 * 3) generate WP_Query object
35 * 4) if no results & fallback is set, generate a new WP_Query with fallback args
36 * 5) return WP_Query
37 *
38 * @return \WP_Query
39 */
40 public function get_query() {
41 $this->get_query_args();
42
43 $offset_control = $this->get_widget_settings( 'offset' );
44
45 $query_id = $this->get_widget_settings( 'query_id' );
46 if ( ! empty( $query_id ) ) {
47 add_action( 'pre_get_posts', [ $this, 'pre_get_posts_query_filter' ] );
48 }
49
50 if ( 0 < $offset_control ) {
51 add_action( 'pre_get_posts', [ $this, 'fix_query_offset' ], 1 );
52 add_filter( 'found_posts', [ $this, 'fix_query_found_posts' ], 1, 2 );
53 }
54
55 $query = new \WP_Query( $this->query_args );
56
57 remove_action( 'pre_get_posts', [ $this, 'pre_get_posts_query_filter' ] );
58 remove_action( 'pre_get_posts', [ $this, 'fix_query_offset' ], 1 );
59 remove_filter( 'found_posts', [ $this, 'fix_query_found_posts' ], 1 );
60
61 Module::add_to_avoid_list( wp_list_pluck( $query->posts, 'ID' ) );
62
63 return $query;
64 }
65
66 protected function get_query_defaults() {
67 $defaults = [
68 $this->prefix . 'post_type' => 'post',
69 $this->prefix . 'posts_ids' => [],
70 $this->prefix . 'orderby' => 'date',
71 $this->prefix . 'order' => 'desc',
72 $this->prefix . 'offset' => 0,
73 $this->prefix . 'posts_per_page' => 3,
74 ];
75
76 return $defaults;
77 }
78
79 public function get_query_args() {
80 $post_type = $this->get_widget_settings( 'post_type' );
81
82 if ( 'current_query' === $post_type ) {
83 $current_query_vars = $GLOBALS['wp_query']->query_vars;
84
85 $current_query_vars = apply_filters( 'elementor/query/get_query_args/current_query', $current_query_vars );
86 $this->query_args = $current_query_vars;
87 return $current_query_vars;
88 }
89
90 $this->set_common_args();
91 $this->set_order_args();
92 $this->set_pagination_args();
93 $this->set_post_include_args();
94
95 if ( 'by_id' !== $post_type ) {
96
97 $this->set_post_exclude_args();
98 $this->set_avoid_duplicates();
99 $this->set_terms_args();
100 $this->set_author_args();
101 $this->set_date_args();
102 }
103
104 $this->query_args = apply_filters( 'elementor/query/query_args', $this->query_args, $this->widget );
105
106 return $this->query_args;
107 }
108
109 protected function set_pagination_args() {
110 $this->set_query_arg( 'posts_per_page', $this->get_widget_settings( 'posts_per_page' ) );
111 $sticky_post = $this->get_widget_settings( 'ignore_sticky_posts' ) ? true : false;
112 $this->set_query_arg( 'ignore_sticky_posts', $sticky_post );
113 }
114
115 protected function set_common_args() {
116 $this->query_args['post_status'] = 'publish'; // Hide drafts/private posts for admins
117
118 $post_type = $this->get_widget_settings( 'post_type' );
119 if ( 'by_id' === $post_type ) {
120 $post_types = auxin_get_public_post_types();
121 $this->query_args['post_type'] = array_keys( $post_types );
122 } else {
123 $this->query_args['post_type'] = $post_type;
124 }
125 }
126
127 protected function set_post_include_args() {
128
129 if ( 'by_id' === $this->get_widget_settings( 'post_type' ) ) {
130
131 $this->set_query_arg( 'post__in', $this->get_widget_settings( 'posts_ids' ) );
132
133 if ( empty( $this->query_args['post__in'] ) ) {
134 // If no selection - return an empty query
135 $this->query_args['post__in'] = [ 0 ];
136 }
137 }
138 }
139
140 protected function set_post_exclude_args() {
141
142 $exclude = $this->get_widget_settings( 'exclude' );
143
144 if ( empty( $exclude ) ) {
145 return;
146 }
147
148 $post__not_in = [];
149
150 if ( $this->maybe_in_array( 'current_post', $exclude ) ) {
151 if ( is_singular() ) {
152 $post__not_in[] = get_queried_object_id();
153 }
154 }
155
156 $exclude_ids = $this->get_widget_settings( 'exclude_ids' );
157 if ( $this->maybe_in_array( 'manual_selection', $exclude ) && ! empty( $exclude_ids ) ) {
158 $post__not_in = array_merge( $post__not_in, $exclude_ids );
159 }
160
161 $this->set_query_arg( 'post__not_in', $post__not_in );
162 }
163
164 protected function set_avoid_duplicates() {
165 if ( 'yes' === $this->get_widget_settings( 'avoid_duplicates' ) ) {
166 $post__not_in = isset( $this->query_args['post__not_in'] ) ? $this->query_args['post__not_in'] : [];
167 $post__not_in = array_merge( $post__not_in, Module::$displayed_ids );
168 $this->set_query_arg( 'post__not_in', $post__not_in );
169 }
170 }
171
172 protected function set_terms_args() {
173
174 $post_type = $this->get_widget_settings( 'post_type' );
175
176 if ( 'by_id' === $post_type ) {
177 return;
178 }
179 $this->build_terms_query_include( 'include_term_ids' );
180 $this->build_terms_query_exclude( 'exclude_term_ids' );
181 }
182
183 protected function build_terms_query_include( $control_id ) {
184 $this->build_terms_query( 'include', $control_id );
185 }
186
187 protected function build_terms_query_exclude( $control_id ) {
188 $this->build_terms_query( 'exclude', $control_id, true );
189 }
190
191 protected function build_terms_query( $tab_id, $control_id, $exclude = false ) {
192 $tab_id = $this->get_widget_settings( $tab_id );
193 $settings_terms = $this->get_widget_settings( $control_id );
194 if ( empty( $tab_id ) || empty( $settings_terms ) || ! $this->maybe_in_array( 'terms', $tab_id ) ) {
195 return;
196 }
197
198 $terms = [];
199
200 // Switch to term_id in order to get all term children (sub-categories):
201 foreach ( $settings_terms as $id ) {
202 $term_data = get_term_by( 'term_taxonomy_id', $id );
203 if ( false !== $term_data ) {
204 $taxonomy = $term_data->taxonomy;
205 $terms[ $taxonomy ][] = $id;
206 }
207 }
208 $this->insert_tax_query( $terms, $exclude );
209 }
210
211 protected function insert_tax_query( $terms, $exclude ) {
212 $tax_query = [];
213 foreach ( $terms as $taxonomy => $ids ) {
214 $query = [
215 'taxonomy' => $taxonomy,
216 'field' => 'term_taxonomy_id',
217 'terms' => $ids,
218 ];
219
220 if ( $exclude ) {
221 $query['operator'] = 'NOT IN';
222 }
223
224 $tax_query[] = $query;
225 }
226
227 if ( empty( $tax_query ) ) {
228 return;
229 }
230
231 if ( empty( $this->query_args['tax_query'] ) ) {
232 $this->query_args['tax_query'] = $tax_query;
233 } else {
234 $this->query_args['tax_query']['relation'] = 'AND';
235 $this->query_args['tax_query'][] = $tax_query;
236 }
237 }
238
239 protected function set_author_args() {
240
241 $include_authors = $this->get_widget_settings( 'include_authors' );
242 if ( ! empty( $include_authors ) && $this->maybe_in_array( 'authors', $this->get_widget_settings( 'include' ) ) ) {
243 $this->set_query_arg( 'author__in', $include_authors );
244 }
245
246 $exclude_authors = $this->get_widget_settings( 'exclude_authors' );
247 if ( ! empty( $exclude_authors ) && $this->maybe_in_array( 'authors', $this->get_widget_settings( 'exclude' ) ) ) {
248 //exclude only if not explicitly included
249 if ( empty( $this->query_args['author__in'] ) ) {
250 $this->set_query_arg( 'author__not_in', $exclude_authors );
251 }
252 }
253 }
254
255 protected function set_order_args() {
256 $order = $this->get_widget_settings( 'order' );
257 if ( ! empty( $order ) ) {
258 $this->set_query_arg( 'orderby', $this->get_widget_settings( 'orderby' ) );
259 $this->set_query_arg( 'order', $this->get_widget_settings( 'order' ) );
260 }
261 }
262
263 protected function set_date_args() {
264
265 $select_date = $this->get_widget_settings( 'select_date' );
266 if ( ! empty( $select_date ) ) {
267 $date_query = [];
268 switch ( $select_date ) {
269 case 'today':
270 $date_query['after'] = '-1 day';
271 break;
272 case 'week':
273 $date_query['after'] = '-1 week';
274 break;
275 case 'month':
276 $date_query['after'] = '-1 month';
277 break;
278 case 'quarter':
279 $date_query['after'] = '-3 month';
280 break;
281 case 'year':
282 $date_query['after'] = '-1 year';
283 break;
284 case 'exact':
285 $after_date = $this->get_widget_settings( 'date_after' );
286 if ( ! empty( $after_date ) ) {
287 $date_query['after'] = $after_date;
288 }
289 $before_date = $this->get_widget_settings( 'date_before' );
290 if ( ! empty( $before_date ) ) {
291 $date_query['before'] = $before_date;
292 }
293 $date_query['inclusive'] = true;
294 break;
295 }
296
297 $this->set_query_arg( 'date_query', $date_query );
298 }
299 }
300
301 /**\
302 * @param string $control_name
303 *
304 * @return mixed|null
305 */
306 protected function get_widget_settings( $control_name ) {
307 $control_name = $this->prefix . $control_name;
308 return isset( $this->widget_settings[ $control_name ] ) ? $this->widget_settings[ $control_name ] : null;
309 }
310
311 /**
312 * @param string $key
313 * @param mixed $value
314 */
315 protected function set_query_arg( $key, $value ) {
316 if ( ! isset( $this->query_args[ $key ] ) ) {
317 $this->query_args[ $key ] = $value;
318 }
319 }
320
321 /**
322 * @param string $value
323 * @param mixed $maybe_array
324 *
325 * @return bool
326 */
327 protected function maybe_in_array( $value, $maybe_array ) {
328 return is_array( $maybe_array ) ? in_array( $value, $maybe_array, true ) : $value === $maybe_array;
329 }
330
331 /**
332 * @param \WP_Query $wp_query
333 */
334 public function pre_get_posts_query_filter( $wp_query ) {
335 if ( $this->widget ) {
336 $query_id = $this->get_widget_settings( 'query_id' );
337 $widget_name = $this->widget->get_name();
338 do_action( "elementor/query/{$query_id}", $wp_query, $this->widget );
339 }
340 }
341
342 /**
343 * @param \WP_Query $query
344 */
345 public function fix_query_offset( &$query ) {
346 $offset = $this->get_widget_settings( 'offset' );
347
348 if ( $offset && $query->is_paged ) {
349 $query->query_vars['offset'] = $offset + ( ( $query->query_vars['paged'] - 1 ) * $query->query_vars['posts_per_page'] );
350 } else {
351 $query->query_vars['offset'] = $offset;
352 }
353 }
354
355 /**
356 * @param int $found_posts
357 * @param \WP_Query $query
358 *
359 * @return int
360 */
361 public function fix_query_found_posts( $found_posts, $query ) {
362 $offset = $this->get_widget_settings( 'offset' );
363
364 if ( $offset ) {
365 $found_posts -= $offset;
366 }
367
368 return $found_posts;
369 }
370 }
371