PluginProbe
UiCore Elements – Free widgets and templates for Elementor / 1.2.2
UiCore Elements – Free widgets and templates for Elementor v1.2.2
1.3.17 1.3.16 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 All 41 releases
uicore-elements / includes / controls / class-query.php

class-query.php in UiCore Elements – Free widgets and templates for Elementor 1.2.2, at includes/controls/class-query.php

208 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace UiCoreElements\Controls;
3
4 use UiCoreElements\Helper;
5 use Elementor\Control_Select2;
6
7 defined('ABSPATH') || exit();
8
9 class Query extends Control_Select2
10 {
11 const CONTROL_ID = 'elements_query';
12
13 public function get_type()
14 {
15 return self::CONTROL_ID;
16 }
17
18 /**
19 * Get query args for a given post type query
20 *
21 * @param string $control_id The control slug, should be '{post-type}-filter'. Eg: `product-filter`.
22 * @param array $settings The control settings array.
23 * @param bool $is_product If the args are for woocommerce products. Default is false.
24 */
25 public static function get_query_args($control_id, $settings, $is_product = false)
26 {
27 // get post type
28 $post_type = $settings[$control_id . '_post_type'];
29
30 // Add extra settings
31 $defaults = [
32 $control_id . '_post_type' => $post_type,
33 $control_id . '_posts_ids' => [],
34 'orderby' => 'date',
35 'order' => 'desc',
36 ];
37 $settings = wp_parse_args($settings, $defaults);
38
39 $paged = self::get_queried_page( $settings );
40
41 // Build query args
42 $query_args = [
43 'post_type' => $post_type,
44 'orderby' => $settings['orderby'],
45 'order' => $settings['order'],
46 'post_status' => 'publish', // Hide drafts/private posts for admins
47 'paged' => $paged,
48 'ignore_sticky_posts' => true
49 ];
50
51 // Get posts per page
52 $query_args['posts_per_page'] = isset($settings['item_limit'])
53 ? $settings['item_limit']['size']
54 : Helper::get_framework_visible_posts($post_type);
55
56 // Update posts quantity to woo requirements
57 if ($is_product) {
58 $query_args['limit'] = $query_args['posts_per_page'];
59 unset($query_args['posts_per_page']);
60 unset($query_args['post_type']);
61 }
62
63 // Offset arg
64 if( isset($settings['offset']) && !empty($settings['offset']['size']) ){
65 $query_args['offset'] = $settings['offset']['size'];
66 }
67
68 // Sticky arg
69 if( isset( $settings['sticky'] ) && $settings['sticky'] ){
70 $query_args['ignore_sticky_posts'] = false;
71 }
72
73 //
74 $queried_filters = self::get_queried_filters($settings, $post_type, $control_id);
75 if ( !empty($queried_filters['tax_query']) ) {
76 $query_args['tax_query'] = $queried_filters['tax_query'];
77 }
78
79 //Enable for data analysis
80 // error_log( __FILE__ . '@' . __LINE__ );
81 // error_log( __METHOD__);
82 // \error_log(print_r($query_args, true));
83 // \error_log("-----------------");
84
85 return $query_args;
86 }
87
88 /**
89 * Get the current page value in a query.
90 *
91 * @param array $settings The control settings array.
92 */
93 public static function get_queried_page( $settings )
94 {
95 if( !isset($settings['__current_page']) ){
96 if (get_query_var('paged')) {
97 $paged = get_query_var('paged');
98
99 } elseif (get_query_var('page')) {
100 $paged = get_query_var('page');
101
102 } else {
103 $paged = 1;
104 }
105
106 } else {
107 $paged = $settings['__current_page'];
108 }
109
110 return $paged;
111 }
112
113 /**
114 * Build the `tax_query` args to work with filter component.
115 *
116 * @param array $settings The control settings array.
117 * @param string $post_type The post type slug.
118 * @param string $control_id The control slug. Can be 'posts-type' or 'product-filter'.
119 *
120 * @return array The query args.
121 */
122 public static function get_queried_filters($settings, $post_type, $control_id)
123 {
124 $args = [
125 'post_type' => $post_type,
126 'tax_query' => [],
127 ];
128
129 if (
130 isset($settings['post_filtering']) &&
131 $settings['post_filtering'] &&
132 isset($_GET['tax']) && //phpcs:ignore WordPress.Security.NonceVerification.Recommended
133 isset($_GET['term']) //phpcs:ignore WordPress.Security.NonceVerification.Recommended
134 ) {
135
136 $args['tax_query'][] = [
137 'taxonomy' => sanitize_text_field( wp_unslash($_GET['tax'])), //phpcs:ignore WordPress.Security.NonceVerification.Recommended
138 'field' => 'term_id',
139 'terms' => intval($_GET['term']), //phpcs:ignore WordPress.Security.NonceVerification.Recommended
140 ];
141
142 } else {
143 $taxonomies = get_object_taxonomies($post_type, 'objects');
144
145 foreach ($taxonomies as $object) {
146 $setting_key = $control_id . '_' . $object->name . '_ids';
147
148 if ( !empty($settings[$setting_key]) ) {
149 $terms_list = $settings[$setting_key];
150
151 if ( !is_array($terms_list) ) {
152 $terms_list = explode(',', $terms_list);
153 }
154
155 $args['tax_query'][] = [
156 'taxonomy' => $object->name,
157 'field' => 'term_id',
158 'terms' => array_map('intval', $terms_list),
159 ];
160 }
161 }
162 }
163
164 // Set `AND` relation if we're working with multiple tax queries
165 if ( count($args['tax_query']) > 1 ) {
166 $args['tax_query']['relation'] = 'AND';
167 };
168
169 return $args;
170 }
171
172 /**
173 * Get all products from a given product query and return the total amount of pages for it. Only for woo product queries.
174 *
175 * TODO: investigate more performatic approaches, not only we're running a query for the second time,
176 * but we're using -1, wich means a big CPU and memory usage.
177 */
178 public static function get_total_pages($default_query)
179 {
180 // Set non-limit posts and a light return type
181 $calc_args = [
182 'limit' => '-1',
183 'return' => 'ids'
184 ];
185 $calc_args = array_merge($default_query, $calc_args);
186
187 // Makes sure we have a limit set
188 if( isset($default_query['limit']) || isset($default_query['posts_per_page']) ) {
189 $limit = isset($default_query['limit'])
190 ? $default_query['limit']
191 : $default_query['posts_per_page'];
192
193 // Get limit from framework otherwise
194 } else {
195 $limit = Helper::get_framework_visible_posts('product');
196 }
197
198 // Get total pages value
199 $total_products = wc_get_products($calc_args);
200 $total = ceil(count($total_products) / $limit);
201
202 return $total;
203 }
204
205 }
206
207 \Elementor\Plugin::$instance->controls_manager->register_control('elements_query', new Query());
208