PluginProbe
UiCore Elements – Free widgets and templates for Elementor / 1.0.11
UiCore Elements – Free widgets and templates for Elementor v1.0.11
1.3.18 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 All 42 releases
uicore-elements / includes / controls / class-query.php

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

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