PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.39
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.39
51.1.86 51.1.84 51.1.85 51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 51.1.45 All 40 releases
king-addons / includes / controls / Ajax_Select2 / Ajax_Select2_API.php

Ajax_Select2_API.php in King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder 51.1.39, at includes/controls/Ajax_Select2/Ajax_Select2_API.php

314 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php /** @noinspection PhpUnused, SpellCheckingInspection, DuplicatedCode */
2
3 namespace King_Addons\AJAX_Select2;
4
5 use King_Addons\Core;
6 use WP_Query;
7 use WP_User_Query;
8
9 if (!defined('ABSPATH')) {
10 exit; // Exit if accessed directly.
11 }
12
13 class Ajax_Select2_API
14 {
15 public function __construct()
16 {
17 $this->init();
18 }
19
20 public function init(): void
21 {
22 add_action('rest_api_init', function () {
23 register_rest_route(
24 'kingaddons/v1/ajaxselect2',
25 '/(?P<action>\w+)/',
26 [
27 'methods' => 'GET',
28 'callback' => [$this, 'callback'],
29 'permission_callback' => '__return_true'
30 ]
31 );
32 });
33 }
34
35 public function callback($request)
36 {
37 return $this->{$request['action']}($request);
38 }
39
40 public function getElementorTemplates($request): ?array
41 {
42 if (!current_user_can('edit_posts')) return null;
43
44 $args = [
45 'post_type' => 'elementor_library',
46 'post_status' => 'publish',
47 'meta_key' => '_elementor_template_type',
48 'meta_value' => ['page', 'section', 'container'],
49 'numberposts' => 10
50 ];
51
52 if (isset($request['s'])) {
53 $args['s'] = $request['s'];
54 }
55
56 $options = [];
57 $the_query = new WP_Query($args);
58
59 if ($the_query->have_posts()) {
60 while ($the_query->have_posts()) {
61 $the_query->the_post();
62 $options[] = [
63 'id' => get_the_ID(),
64 'text' => html_entity_decode(get_the_title()),
65 ];
66 }
67 }
68
69 wp_reset_postdata();
70
71 return ['results' => $options];
72 }
73
74 public function getPostsByPostType($request): ?array
75 {
76 if (!current_user_can('edit_posts')) return null;
77
78 $post_type = $request['query_slug'] ?? '';
79
80 $args = [
81 'post_type' => $post_type,
82 'post_status' => $post_type === 'attachment' ? 'any' : 'publish',
83 'posts_per_page' => 15,
84 ];
85
86 if (isset($request['ids'])) {
87 $args['post__in'] = explode(',', $request['ids']);
88 }
89
90 if (isset($request['s'])) {
91 $args['s'] = $request['s'];
92 }
93
94 $query = new WP_Query($args);
95 $options = [];
96
97 if ($query->have_posts()) {
98 while ($query->have_posts()) {
99 $query->the_post();
100 $options[] = [
101 'id' => get_the_ID(),
102 'text' => html_entity_decode(get_the_title()),
103 ];
104 }
105 }
106
107 wp_reset_postdata();
108 return ['results' => $options];
109 }
110
111 public function getPostTypeTaxonomies($request): ?array
112 {
113 if (!current_user_can('edit_posts')) return null;
114
115 $post_type = $request['query_slug'] ?? '';
116
117 $taxonomies = get_object_taxonomies($post_type, 'objects');
118 $options = [];
119
120 if ($taxonomies) {
121 foreach ($taxonomies as $taxonomy) {
122
123 if (isset($request['s']) && stripos($taxonomy->label, $request['s']) === false) {
124 continue;
125 }
126
127 if (isset($request['ids'])) {
128 $ids = explode(',', $request['ids'] ?: '99999999');
129 if (!in_array($taxonomy->name, $ids)) {
130 continue;
131 }
132 }
133
134 $options[] = [
135 'id' => $taxonomy->name,
136 'text' => $taxonomy->label,
137 ];
138 }
139 }
140
141 return ['results' => $options];
142 }
143
144 public function getCustomMetaKeys($request)
145 {
146 if (!current_user_can('edit_posts')) return null;
147
148 $post_types = Core::getCustomTypes('post', false);
149 $data = [];
150
151 foreach ($post_types as $slug => $name) {
152 $posts = get_posts(['post_type' => $slug, 'posts_per_page' => -1]);
153 $metaKeys = [];
154
155 foreach ($posts as $post) {
156 $keys = get_post_custom_keys($post->ID) ?: [];
157 $keys = array_filter($keys, fn($k) => $k[0] !== '_');
158 $metaKeys = array_merge($metaKeys, $keys);
159 }
160
161 $data[$slug] = array_unique($metaKeys);
162 }
163
164 $mergedKeys = array_values(
165 array_unique(
166 array_merge([], ...array_values($data))
167 )
168 );
169
170 $filtered = array_filter($mergedKeys, function ($key) use ($request) {
171 return !isset($request['s']) || strpos($key, $request['s']) !== false;
172 });
173
174 $options = array_map(fn($k) => ['id' => $k, 'text' => $k], $filtered);
175
176 return ['results' => $options];
177 }
178
179 public function getUsers($request)
180 {
181 if (!current_user_can('edit_posts')) return null;
182
183 $args = [
184 'number' => 15,
185 'blog_id' => 0,
186 ];
187
188 if (!empty($request['ids'])) {
189 $args['include'] = array_map('intval', explode(',', $request['ids']));
190 }
191
192 if (!empty($request['s'])) {
193 $args['search'] = '*' . $request['s'] . '*';
194 }
195
196 $results = (new WP_User_Query($args))->get_results();
197
198 $options = array_map(
199 fn($user) => ['id' => $user->ID, 'text' => $user->display_name],
200 $results ?: []
201 );
202
203 wp_reset_postdata();
204
205 return ['results' => $options];
206 }
207
208 public function getTaxonomies($request)
209 {
210 if (!current_user_can('edit_posts')) return null;
211
212 $tax = $request['query_slug'] ?? '';
213 $args = [
214 'orderby' => 'name',
215 'order' => 'DESC',
216 'hide_empty' => true,
217 'number' => 10,
218 ];
219
220 if (isset($request['ids'])) {
221 $args['include'] = explode(',', $request['ids'] ?: '99999999');
222 }
223
224 if (!empty($request['s'])) {
225 $args['name__like'] = $request['s'];
226 }
227
228 $terms = get_terms($tax, $args);
229 $options = array_map(function ($term) {
230 return [
231 'id' => $term->term_id,
232 'text' => $term->name,
233 ];
234 }, $terms);
235
236 wp_reset_postdata();
237
238 return ['results' => $options];
239 }
240
241 public function getCustomMetaKeysProduct($request)
242 {
243 if (!current_user_can('edit_posts')) return null;
244
245 $options = [];
246 $merged_meta_keys = [];
247 $post_types = Core::getCustomTypes('post', false);
248
249 foreach ($post_types as $slug => $name) {
250 $posts = get_posts(['post_type' => $slug, 'posts_per_page' => -1]);
251 foreach ($posts as $post) {
252 $meta_keys = get_post_custom_keys($post->ID);
253 if ($meta_keys) {
254 foreach ($meta_keys as $key) {
255 if ('_' !== substr($key, 0, 1)) {
256 $merged_meta_keys[] = $key;
257 }
258 }
259 }
260 }
261 }
262
263 $merged_meta_keys = array_values(array_unique($merged_meta_keys));
264 foreach ($merged_meta_keys as $key) {
265 if (empty($request['s']) || false !== strpos($key, $request['s'])) {
266 $options[] = [
267 'id' => $key,
268 'text' => $key,
269 ];
270 }
271 }
272
273 $product_attributes = [];
274 $products_query = new WP_Query([
275 'post_type' => 'product',
276 'posts_per_page' => -1,
277 ]);
278
279 if ($products_query->have_posts()) {
280 while ($products_query->have_posts()) {
281 $products_query->the_post();
282
283 if (class_exists('WooCommerce')) {
284 if (function_exists('wc_get_product')) {
285 /** @noinspection PhpUndefinedFunctionInspection */
286
287 $product = wc_get_product(get_the_ID());
288
289 foreach ($product->get_attributes() as $attribute) {
290 $product_attributes[$attribute->get_name()] = true;
291 }
292
293 }
294 }
295
296 }
297 wp_reset_postdata();
298 }
299
300 foreach (array_keys($product_attributes) as $attribute_name) {
301 $options[] = [
302 'id' => $attribute_name,
303 'text' => $attribute_name,
304 ];
305 }
306
307 return [
308 'results' => $options,
309 ];
310 }
311
312 }
313
314 new Ajax_Select2_API();