PluginProbe
Ultimate Store Kit – Store Builder Addons for Elementor, WooCommerce Store Builder, EDD Store Builder / 2.0.5
Ultimate Store Kit – Store Builder Addons for Elementor, WooCommerce Store Builder, EDD Store Builder v2.0.5
3.1.4 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 All 93 releases
ultimate-store-kit / includes / controls / select-input / dynamic-select-input-module.php

dynamic-select-input-module.php in Ultimate Store Kit – Store Builder Addons for Elementor, WooCommerce Store Builder, EDD Store Builder 2.0.5, at includes/controls/select-input/dynamic-select-input-module.php

370 lines 7.5 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 if ($query == 'terms') {
60 $data = $this->getTerms();
61 } else if ($query == 'authors') {
62 $data = $this->getAuthors();
63 } else if ($query == 'authors_role') {
64 $data = $this->getAuthorRoles();
65 } else if ($query == 'only_post') {
66 $data = $this->getOnlyPosts();
67 } else {
68 $data = $this->getPosts();
69 }
70
71 wp_send_json_success($data);
72 } catch (Exception $e) {
73 wp_send_json_error($e->getMessage());
74 }
75
76 die();
77 }
78
79 /**
80 * Get Post Type
81 * @return string
82 */
83 protected function getPostType() {
84 return isset($_POST['post_type']) ? sanitize_text_field($_POST['post_type']) : '';
85 }
86
87 /**
88 * @return string[]|\WP_Post_Type[]
89 */
90 protected function getAllPublicPostTypes() {
91 return array_values(get_post_types(['public' => true]));
92 }
93
94 /**
95 * @return string
96 */
97 protected function getSearchQuery() {
98 return isset($_POST['search_text']) ? sanitize_text_field($_POST['search_text']) : '';
99 }
100
101 /**
102 * @return array|mixed
103 */
104 protected function getselecedIds() {
105
106 return isset($_POST['ids']) ? sanitize_text_field($_POST['ids']) : [];
107 }
108
109
110 /**
111 * @param string $taxonomy
112 *
113 * @return mixed|string
114 */
115 public function getTaxonomyName($taxonomy = '') {
116 $taxonomies = get_taxonomies(['public' => true], 'objects');
117 $taxonomies = array_column($taxonomies, 'label', 'name');
118
119 return isset($taxonomies[$taxonomy]) ? $taxonomies[$taxonomy] : '';
120 }
121
122 /**
123 * @return string[]|\WP_Taxonomy[]
124 */
125 public function getAllPublicTaxonomies() {
126 return array_values(get_taxonomies(['public' => true]));
127 }
128
129 /**
130 * Get Post Query Data
131 *
132 * @return array
133 */
134 public function getPosts() {
135
136 $include = $this->getselecedIds();
137 $searchText = $this->getSearchQuery();
138
139 $args = [];
140
141 if ($this->getPostType() && $this->getPostType() !== '_related_post_type') {
142 $args['post_type'] = $this->getPostType();
143 } else {
144 $args['post_type'] = $this->getAllPublicPostTypes();
145 }
146
147 if (!empty($include)) {
148 $args['post__in'] = $include;
149 $args['posts_per_page'] = count($include);
150 } else {
151 $args['posts_per_page'] = 20;
152 }
153
154 if ($searchText) {
155 $args['s'] = $searchText;
156 }
157
158 $query = new WP_Query($args);
159 $results = [];
160 foreach ($query->posts as $post) {
161 $post_type_obj = get_post_type_object($post->post_type);
162 if (!empty($data['include_type'])) {
163 $text = $post_type_obj->labels->name . ': ' . $post->post_title;
164 } else {
165 $text = ($post_type_obj->hierarchical) ? $this->get_post_name_with_parents($post) : $post->post_title;
166 }
167
168 $results[] = [
169 'id' => $post->ID,
170 'text' => esc_html($text),
171 ];
172 }
173
174 return $results;
175 }
176
177 public function getOnlyPosts() {
178 $include = $this->getselecedIds();
179 $searchText = $this->getSearchQuery();
180
181 $args = [];
182
183 $args['post_type'] = 'post';
184
185
186 if (!empty($include)) {
187 $args['post__in'] = $include;
188 $args['posts_per_page'] = count($include);
189 } else {
190 $args['posts_per_page'] = 20;
191 }
192
193 if ($searchText) {
194 $args['s'] = $searchText;
195 }
196
197 $query = new WP_Query($args);
198 $results = [];
199 foreach ($query->posts as $post) {
200 $post_type_obj = get_post_type_object($post->post_type);
201 if (!empty($data['include_type'])) {
202 $text = $post_type_obj->labels->name . ': ' . $post->post_title;
203 } else {
204 $text = ($post_type_obj->hierarchical) ? $this->get_post_name_with_parents($post) : $post->post_title;
205 }
206
207 $results[] = [
208 'id' => $post->ID,
209 'text' => esc_html($text),
210 ];
211 }
212
213 return $results;
214 }
215
216 private function get_post_name_with_parents($post, $max = 3) {
217 if (0 === $post->post_parent) {
218 return $post->post_title;
219 }
220 $separator = is_rtl() ? ' < ' : ' > ';
221 $test_post = $post;
222 $names = [];
223 while ($test_post->post_parent > 0) {
224 $test_post = get_post($test_post->post_parent);
225 if (!$test_post) {
226 break;
227 }
228 $names[] = $test_post->post_title;
229 }
230
231 $names = array_reverse($names);
232 if (count($names) < ($max)) {
233 return implode($separator, $names) . $separator . $post->post_title;
234 }
235
236 $name_string = '';
237 for ($i = 0; $i < ($max - 1); $i++) {
238 $name_string .= $names[$i] . $separator;
239 }
240
241 return $name_string . '...' . $separator . $post->post_title;
242 }
243
244 /**
245 * Get Terms query data
246 *
247 * @return array
248 */
249 public function getTerms() {
250 $search_text = $this->getSearchQuery();
251 $taxonomies = $this->getAllPublicTaxonomies();
252 $include = $this->getselecedIds();
253
254 if ($this->getPostType() == '_related_post_type') {
255 $post_type = 'any';
256 } elseif ($this->getPostType()) {
257 $post_type = $this->getPostType();
258 }
259 $post_taxonomies = get_object_taxonomies($post_type);
260 $taxonomies = array_intersect($post_taxonomies, $taxonomies);
261 $data = [];
262
263 if (empty($taxonomies)) {
264 return $data;
265 }
266
267 $args = [
268 'taxonomy' => $taxonomies,
269 'hide_empty' => false,
270 ];
271
272 if (!empty($include)) {
273 $args['include'] = $include;
274 }
275
276 if ($search_text) {
277 $args['number'] = 20;
278 $args['search'] = $search_text;
279 }
280 $terms = get_terms($args);
281
282 if (is_wp_error($terms) || empty($terms)) {
283 return $data;
284 }
285
286 foreach ($terms as $term) {
287 $label = $term->name;
288 $taxonomy_name = $this->getTaxonomyName($term->taxonomy);
289
290 if ($taxonomy_name) {
291 $label = "{$taxonomy_name}: {$label}";
292 }
293
294 $data[] = [
295 'id' => $term->term_taxonomy_id,
296 'text' => $label,
297 ];
298 }
299
300 return $data;
301 }
302
303 /**
304 * Get Authors query Data
305 *
306 * @return array
307 */
308 public function getAuthors() {
309 $include = $this->getselecedIds();
310 $search_text = $this->getSearchQuery();
311
312 $args = [
313 'fields' => ['ID', 'display_name'],
314 'orderby' => 'display_name',
315 ];
316
317 if (!empty($include)) {
318 $args['include'] = $include;
319 }
320
321 if ($search_text) {
322 $args['number'] = 20;
323 $args['search'] = "*$search_text*";
324 }
325
326 $users = get_users($args);
327
328 $data = [];
329
330 if (empty($users)) {
331 return $data;
332 }
333
334 foreach ($users as $user) {
335 $data[] = [
336 'id' => $user->ID,
337 'text' => $user->display_name,
338 ];
339 }
340
341 return $data;
342 }
343
344 /**
345 * Get Authors Roles query Data
346 *
347 * @return array
348 */
349 public function getAuthorRoles() {
350 global $wp_roles;
351
352 $all_roles = $wp_roles->roles;
353 $roles = [];
354 foreach ($all_roles as $key => $role) {
355 $roles[] = [
356 'id' => $key,
357 'text' => $role['name'],
358 ];
359 }
360
361 return $roles;
362 }
363 }
364
365 function Dynamic_Select_Input_Module() {
366 return Dynamic_Select_Input_Module::get_instance();
367 }
368
369 Dynamic_Select_Input_Module()->init();
370