PluginProbe
UiCore Elements – Free widgets and templates for Elementor / 1.3.17
UiCore Elements – Free widgets and templates for Elementor v1.3.17
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
← All changes | includes/controls/class-query.php +53 -26 1.2.21.3.17 View file →
@@ -1,5 +1,6 @@
1 1 <?php
2 +
2 3 namespace UiCoreElements\Controls;
3 4
4 5 use UiCoreElements\Helper;
5 6 use Elementor\Control_Select2;
@@ -35,15 +36,15 @@
35 36 'order' => 'desc',
36 37 ];
37 38 $settings = wp_parse_args($settings, $defaults);
38 39
39 - $paged = self::get_queried_page( $settings );
40 + $paged = self::get_queried_page($settings);
40 41
41 42 // Build query args
42 43 $query_args = [
43 44 'post_type' => $post_type,
44 - 'orderby' => $settings['orderby'],
45 - 'order' => $settings['order'],
45 + 'orderby' => $settings['item_order_by'] ?? 'date',
46 + 'order' => $settings['item_order'] ?? 'desc',
46 47 'post_status' => 'publish', // Hide drafts/private posts for admins
47 48 'paged' => $paged,
48 49 'ignore_sticky_posts' => true
49 50 ];
@@ -48,11 +49,9 @@
48 49 'ignore_sticky_posts' => true
49 50 ];
50 51
51 52 // 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);
53 + $query_args['posts_per_page'] = self::get_posts_per_page($post_type, $settings);
55 54
56 55 // Update posts quantity to woo requirements
57 56 if ($is_product) {
58 57 $query_args['limit'] = $query_args['posts_per_page'];
@@ -60,20 +59,20 @@
60 59 unset($query_args['post_type']);
61 60 }
62 61
63 62 // Offset arg
64 - if( isset($settings['offset']) && !empty($settings['offset']['size']) ){
63 + if (isset($settings['offset']) && !empty($settings['offset']['size'])) {
65 64 $query_args['offset'] = $settings['offset']['size'];
66 65 }
67 66
68 67 // Sticky arg
69 - if( isset( $settings['sticky'] ) && $settings['sticky'] ){
68 + if (isset($settings['sticky']) && $settings['sticky']) {
70 69 $query_args['ignore_sticky_posts'] = false;
71 70 }
72 71
73 72 //
74 73 $queried_filters = self::get_queried_filters($settings, $post_type, $control_id);
75 - if ( !empty($queried_filters['tax_query']) ) {
74 + if (!empty($queried_filters['tax_query'])) {
76 75 $query_args['tax_query'] = $queried_filters['tax_query'];
77 76 }
78 77
79 78 //Enable for data analysis
@@ -85,25 +84,38 @@
85 84 return $query_args;
86 85 }
87 86
88 87 /**
88 + * Get the amount of posts per page from a given widget settings, with fallback to framework and wordpress.
89 + * @param string $post_type The post type slug.
90 + * @param array $settings The control settings array.
91 + *
92 + * @return int The posts per page value.
93 + */
94 + public static function get_posts_per_page($post_type, $settings)
95 + {
96 + if (isset($settings['item_limit'])) {
97 + return $settings['item_limit']['size'];
98 + }
99 +
100 + return Helper::get_framework_visible_posts($post_type);
101 + }
102 +
103 + /**
89 104 * Get the current page value in a query.
90 105 *
91 106 * @param array $settings The control settings array.
92 107 */
93 - public static function get_queried_page( $settings )
108 + public static function get_queried_page($settings)
94 109 {
95 - if( !isset($settings['__current_page']) ){
110 + if (!isset($settings['__current_page'])) {
96 111 if (get_query_var('paged')) {
97 112 $paged = get_query_var('paged');
98 -
99 113 } elseif (get_query_var('page')) {
100 114 $paged = get_query_var('page');
101 -
102 115 } else {
103 116 $paged = 1;
104 117 }
105 -
106 118 } else {
107 119 $paged = $settings['__current_page'];
108 120 }
109 121
@@ -125,21 +137,23 @@
125 137 'post_type' => $post_type,
126 138 'tax_query' => [],
127 139 ];
128 140
141 + $term = self::get_query_term_compatibility('term');
142 + $tax = self::get_query_term_compatibility('tax');
143 +
129 144 if (
130 145 isset($settings['post_filtering']) &&
131 146 $settings['post_filtering'] &&
132 - isset($_GET['tax']) && //phpcs:ignore WordPress.Security.NonceVerification.Recommended
133 - isset($_GET['term']) //phpcs:ignore WordPress.Security.NonceVerification.Recommended
134 - ) {
147 + isset($tax) &&
148 + isset($term)
149 + ) {
135 150
136 151 $args['tax_query'][] = [
137 - 'taxonomy' => sanitize_text_field( wp_unslash($_GET['tax'])), //phpcs:ignore WordPress.Security.NonceVerification.Recommended
152 + 'taxonomy' => sanitize_text_field(wp_unslash($tax)), //phpcs:ignore WordPress.Security.NonceVerification.Recommended
138 153 'field' => 'term_id',
139 - 'terms' => intval($_GET['term']), //phpcs:ignore WordPress.Security.NonceVerification.Recommended
154 + 'terms' => intval($term), //phpcs:ignore WordPress.Security.NonceVerification.Recommended
140 155 ];
141 -
142 156 } else {
143 157 $taxonomies = get_object_taxonomies($post_type, 'objects');
144 158
145 159 foreach ($taxonomies as $object) {
@@ -144,12 +158,12 @@
144 158
145 159 foreach ($taxonomies as $object) {
146 160 $setting_key = $control_id . '_' . $object->name . '_ids';
147 161
148 - if ( !empty($settings[$setting_key]) ) {
162 + if (!empty($settings[$setting_key])) {
149 163 $terms_list = $settings[$setting_key];
150 164
151 - if ( !is_array($terms_list) ) {
165 + if (!is_array($terms_list)) {
152 166 $terms_list = explode(',', $terms_list);
153 167 }
154 168
155 169 $args['tax_query'][] = [
@@ -161,9 +175,9 @@
161 175 }
162 176 }
163 177
164 178 // Set `AND` relation if we're working with multiple tax queries
165 - if ( count($args['tax_query']) > 1 ) {
179 + if (count($args['tax_query']) > 1) {
166 180 $args['tax_query']['relation'] = 'AND';
167 181 };
168 182
169 183 return $args;
@@ -169,8 +183,22 @@
169 183 return $args;
170 184 }
171 185
172 186 /**
187 + * Get the query terms from URL params, checking for both 'ui_{arg}' and '{arg}' params.
188 + */
189 + public static function get_query_term_compatibility(string $arg)
190 + {
191 + if (isset($_GET['ui_' . $arg])) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
192 + return $_GET['ui_' . $arg]; //phpcs:ignore WordPress.Security.NonceVerification.Recommended
193 + }
194 + if (isset($_GET[$arg])) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
195 + return $_GET[$arg]; //phpcs:ignore WordPress.Security.NonceVerification.Recommended
196 + }
197 + return null;
198 + }
199 +
200 + /**
173 201 * Get all products from a given product query and return the total amount of pages for it. Only for woo product queries.
174 202 *
175 203 * TODO: investigate more performatic approaches, not only we're running a query for the second time,
176 204 * but we're using -1, wich means a big CPU and memory usage.
@@ -184,14 +212,14 @@
184 212 ];
185 213 $calc_args = array_merge($default_query, $calc_args);
186 214
187 215 // Makes sure we have a limit set
188 - if( isset($default_query['limit']) || isset($default_query['posts_per_page']) ) {
216 + if (isset($default_query['limit']) || isset($default_query['posts_per_page'])) {
189 217 $limit = isset($default_query['limit'])
190 218 ? $default_query['limit']
191 219 : $default_query['posts_per_page'];
192 220
193 - // Get limit from framework otherwise
221 + // Get limit from framework otherwise
194 222 } else {
195 223 $limit = Helper::get_framework_visible_posts('product');
196 224 }
197 225
@@ -200,8 +228,7 @@
200 228 $total = ceil(count($total_products) / $limit);
201 229
202 230 return $total;
203 231 }
204 -
205 232 }
206 233
207 234 \Elementor\Plugin::$instance->controls_manager->register_control('elements_query', new Query());