PluginProbe
Ultimate Store Kit / 3.0.9
Ultimate Store Kit v3.0.9
3.0.8 3.0.9 3.1.0 3.1.2 3.1.3 3.0.7 3.0.5 3.0.4 3.0.3 3.0.2 trunk 1.5.0 1.5.1 1.5.2 1.6.1 1.6.2 1.6.3 1.6.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 92 releases
ultimate-store-kit / includes / controls / select-input / dynamic-select-input-module.php

dynamic-select-input-module.php in Ultimate Store Kit 3.0.9, at includes/controls/select-input/dynamic-select-input-module.php

454 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace UltimateStoreKit\Includes\Controls\SelectInput;
4
5 use Exception;
6 use WP_Query;
7
8 defined('ABSPATH') || die();
9
10 class Dynamic_Select_Input_Module {
11
12 const ACTION = '';
13
14 private static $instance = null;
15
16 /**
17 * Returns the instance.
18 *
19 * @return object
20 * @since 1.0.0
21 */
22 public static function get_instance() {
23 // If the single instance hasn't been set, set it now.
24 if (null == self::$instance) {
25 self::$instance = new self;
26 }
27
28 return self::$instance;
29 }
30
31 /**
32 * Init method
33 */
34
35 /**
36 * Constructor.
37 */
38 public function init() {
39 add_action('wp_ajax_ultimate_store_kit_dynamic_select_input_data', array($this, 'getSelectInputData'));
40 }
41
42 /**
43 * get Ajax Data
44 */
45 public function getSelectInputData() {
46 $nonce = isset($_POST['security']) ? sanitize_text_field($_POST['security']) : '';
47
48 try {
49 if (!wp_verify_nonce($nonce, 'usk_dynamic_select')) {
50 throw new Exception('Invalid request');
51 }
52
53 if (!current_user_can('edit_posts')) {
54 throw new Exception('Unauthorized request');
55 }
56
57 $query = isset($_POST['query']) ? sanitize_text_field($_POST['query']) : '';
58
59 switch ($query) {
60 case 'posts':
61 $data = $this->getPosts();
62 break;
63 case 'terms':
64 $data = $this->getTerms();
65 break;
66 case 'authors':
67 $data = $this->getAuthors();
68 break;
69 case 'authors_role':
70 $data = $this->getAuthorRoles();
71 break;
72 case 'only_post':
73 $data = $this->getOnlyPosts();
74 break;
75 case 'product_cat':
76 $data = $this->getProductTerms($query);
77 break;
78 case 'product_tag':
79 $data = $this->getProductTerms($query);
80 break;
81 case 'product_attributes':
82 $data = $this->getProductAttributes();
83 break;
84 case 'product_brand':
85 $data = $this->getProductTerms($query);
86 break;
87 default:
88 $data = $this->getPosts();
89 break;
90 }
91
92 wp_send_json_success($data);
93 } catch (Exception $e) {
94 wp_send_json_error($e->getMessage());
95 }
96
97 die();
98 }
99
100 /**
101 * Get Post Type
102 * @return string
103 */
104 protected function getPostType() {
105 return isset($_POST['post_type']) ? sanitize_text_field($_POST['post_type']) : '';
106 }
107
108 /**
109 * @return string[]|\WP_Post_Type[]
110 */
111 protected function getAllPublicPostTypes() {
112 return array_values(get_post_types(['public' => true]));
113 }
114
115 /**
116 * @return string
117 */
118 protected function getSearchQuery() {
119 return isset($_POST['search_text']) ? sanitize_text_field($_POST['search_text']) : '';
120 }
121
122 /**
123 * @return array|mixed
124 */
125 protected function getselecedIds() {
126
127 return isset($_POST['ids']) ? sanitize_text_field($_POST['ids']) : [];
128 }
129
130
131 /**
132 * @param string $taxonomy
133 *
134 * @return mixed|string
135 */
136 public function getTaxonomyName($taxonomy = '') {
137 $taxonomies = get_taxonomies(['public' => true], 'objects');
138 $taxonomies = array_column($taxonomies, 'label', 'name');
139
140 return isset($taxonomies[$taxonomy]) ? $taxonomies[$taxonomy] : '';
141 }
142
143 /**
144 * @return string[]|\WP_Taxonomy[]
145 */
146 public function getAllPublicTaxonomies() {
147 return array_values(get_taxonomies(['public' => true]));
148 }
149
150 /**
151 * Get Post Query Data
152 *
153 * @return array
154 */
155 public function getPosts() {
156
157 $include = $this->getselecedIds();
158 $searchText = $this->getSearchQuery();
159
160 $args = [];
161
162 if ($this->getPostType() && $this->getPostType() !== '_related_post_type') {
163 $args['post_type'] = $this->getPostType();
164 } else {
165 $args['post_type'] = $this->getAllPublicPostTypes();
166 }
167
168 if (!empty($include)) {
169 $args['post__in'] = $include;
170 $args['posts_per_page'] = count($include);
171 } else {
172 $args['posts_per_page'] = 20;
173 }
174
175 if ($searchText) {
176 $args['s'] = $searchText;
177 }
178
179 $query = new WP_Query($args);
180 $results = [];
181 foreach ($query->posts as $post) {
182 $post_type_obj = get_post_type_object($post->post_type);
183 if (!empty($data['include_type'])) {
184 $text = $post_type_obj->labels->name . ': ' . $post->post_title;
185 } else {
186 $text = ($post_type_obj->hierarchical) ? $this->get_post_name_with_parents($post) : $post->post_title;
187 }
188
189 $results[] = [
190 'id' => $post->ID,
191 'text' => esc_html($text),
192 ];
193 }
194
195 return $results;
196 }
197
198 public function getOnlyPosts() {
199 $include = $this->getselecedIds();
200 $searchText = $this->getSearchQuery();
201
202 $args = [];
203
204 $args['post_type'] = 'post';
205
206
207 if (!empty($include)) {
208 $args['post__in'] = $include;
209 $args['posts_per_page'] = count($include);
210 } else {
211 $args['posts_per_page'] = 20;
212 }
213
214 if ($searchText) {
215 $args['s'] = $searchText;
216 }
217
218 $query = new WP_Query($args);
219 $results = [];
220 foreach ($query->posts as $post) {
221 $post_type_obj = get_post_type_object($post->post_type);
222 if (!empty($data['include_type'])) {
223 $text = $post_type_obj->labels->name . ': ' . $post->post_title;
224 } else {
225 $text = ($post_type_obj->hierarchical) ? $this->get_post_name_with_parents($post) : $post->post_title;
226 }
227
228 $results[] = [
229 'id' => $post->ID,
230 'text' => esc_html($text),
231 ];
232 }
233
234 return $results;
235 }
236
237 private function get_post_name_with_parents($post, $max = 3) {
238 if (0 === $post->post_parent) {
239 return $post->post_title;
240 }
241 $separator = is_rtl() ? ' < ' : ' > ';
242 $test_post = $post;
243 $names = [];
244 while ($test_post->post_parent > 0) {
245 $test_post = get_post($test_post->post_parent);
246 if (!$test_post) {
247 break;
248 }
249 $names[] = $test_post->post_title;
250 }
251
252 $names = array_reverse($names);
253 if (count($names) < ($max)) {
254 return implode($separator, $names) . $separator . $post->post_title;
255 }
256
257 $name_string = '';
258 for ($i = 0; $i < ($max - 1); $i++) {
259 $name_string .= $names[$i] . $separator;
260 }
261
262 return $name_string . '...' . $separator . $post->post_title;
263 }
264
265 /**
266 * Get Terms query data
267 *
268 * @return array
269 */
270 public function getTerms() {
271 $search_text = $this->getSearchQuery();
272 $taxonomies = $this->getAllPublicTaxonomies();
273 $include = $this->getselecedIds();
274
275 if ($this->getPostType() == '_related_post_type') {
276 $post_type = 'any';
277 } elseif ($this->getPostType()) {
278 $post_type = $this->getPostType();
279 }
280 $post_taxonomies = get_object_taxonomies($post_type);
281 $taxonomies = array_intersect($post_taxonomies, $taxonomies);
282 $data = [];
283
284 if (empty($taxonomies)) {
285 return $data;
286 }
287
288 $args = [
289 'taxonomy' => $taxonomies,
290 'hide_empty' => true,
291 ];
292
293 if (!empty($include)) {
294 $args['include'] = $include;
295 }
296
297 if ($search_text) {
298 $args['number'] = 20;
299 $args['search'] = $search_text;
300 }
301 $terms = get_terms($args);
302
303 if (is_wp_error($terms) || empty($terms)) {
304 return $data;
305 }
306
307 foreach ($terms as $term) {
308 $label = $term->name;
309 $taxonomy_name = $this->getTaxonomyName($term->taxonomy);
310
311 if ($taxonomy_name) {
312 $label = "{$taxonomy_name}: {$label}";
313 }
314
315 $data[] = [
316 'id' => $term->term_taxonomy_id,
317 'text' => $label,
318 ];
319 }
320
321 return $data;
322 }
323
324 /**
325 * Get Authors query Data
326 *
327 * @return array
328 */
329 public function getAuthors() {
330 $include = $this->getselecedIds();
331 $search_text = $this->getSearchQuery();
332
333 $args = [
334 'fields' => ['ID', 'display_name'],
335 'orderby' => 'display_name',
336 ];
337
338 if (!empty($include)) {
339 $args['include'] = $include;
340 }
341
342 if ($search_text) {
343 $args['number'] = 20;
344 $args['search'] = "*$search_text*";
345 }
346
347 $users = get_users($args);
348
349 $data = [];
350
351 if (empty($users)) {
352 return $data;
353 }
354
355 foreach ($users as $user) {
356 $data[] = [
357 'id' => $user->ID,
358 'text' => $user->display_name,
359 ];
360 }
361
362 return $data;
363 }
364
365 /**
366 * Get Authors Roles query Data
367 *
368 * @return array
369 */
370 public function getAuthorRoles() {
371 global $wp_roles;
372
373 $all_roles = $wp_roles->roles;
374 $roles = [];
375 foreach ($all_roles as $key => $role) {
376 $roles[] = [
377 'id' => $key,
378 'text' => $role['name'],
379 ];
380 }
381
382 return $roles;
383 }
384
385 /**
386 * Get Product Categories query Data
387 *
388 * @return array
389 */
390 public function getProductTerms($taxonomy = 'product_cat') {
391 $include = $this->getselecedIds();
392 $search_text = $this->getSearchQuery();
393 $args = [
394 'taxonomy' => $taxonomy,
395 'hide_empty' => true,
396 ];
397
398 if (!empty($include)) {
399 $args['include'] = $include;
400 }
401
402 if ($search_text) {
403 $args['number'] = 20;
404 $args['search'] = $search_text;
405 }
406 $terms = get_terms($args);
407
408 if (is_wp_error($terms) || empty($terms)) {
409 return [];
410 }
411
412 foreach ($terms as $term) {
413 $data[] = [
414 'id' => $term->term_taxonomy_id,
415 'text' => $term->name,
416 ];
417 }
418 return $data;
419 }
420
421 /**
422 * Get Product Attributes query Data
423 *
424 * @return array
425 */
426 public function getProductAttributes(): array {
427 $search_text = $this->getSearchQuery();
428 // Get all global product attributes
429 $attribute_taxonomies = wc_get_attribute_taxonomies();
430
431 // Map the attributes to the desired structure
432 $data = [];
433
434 foreach ($attribute_taxonomies as $attribute) {
435 $data[] = [
436 'id' => $attribute->attribute_id,
437 'text' => $attribute->attribute_label,
438 ];
439 }
440
441 $data = array_filter($data, function ($item) use ($search_text) {
442 return stripos($item['text'], $search_text) !== false;
443 });
444 $data = array_slice($data, 0, 20); // Limit to 20 results
445 return $data;
446 }
447 }
448
449 function Dynamic_Select_Input_Module() {
450 return Dynamic_Select_Input_Module::get_instance();
451 }
452
453 Dynamic_Select_Input_Module()->init();
454