PluginProbe
UiCore Elements – Free widgets and templates for Elementor / 1.0.16
UiCore Elements – Free widgets and templates for Elementor v1.0.16
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.0.16, at includes/controls/class-query.php

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