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