PluginProbe
Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets / 4.2.1
Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets v4.2.1
4.5.4 4.2.1 4.2.2 4.2.3 4.5.0 4.5.2 4.5.3 4.2.0 4.1.18 4.1.17 4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.0.9 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 All 146 releases
ultimate-post-kit / includes / controls / select-input / dynamic-select-input-module.php

dynamic-select-input-module.php in Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets 4.2.1, at includes/controls/select-input/dynamic-select-input-module.php

302 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace UltimatePostKit\Includes\Controls\SelectInput;
4
5 defined('ABSPATH') || die();
6 class UltimatePostKit_Dynamic_Select_Input_Module {
7
8 const ACTION = '';
9
10 private static $instance = null;
11
12 /**
13 * Returns the instance.
14 *
15 * @return object
16 * @since 1.0.0
17 */
18 public static function get_instance() {
19 // If the single instance hasn't been set, set it now.
20 if (null == self::$instance) {
21 self::$instance = new self;
22 }
23
24 return self::$instance;
25 }
26
27 /**
28 * Init method
29 */
30
31 /**
32 * Constructor.
33 */
34 public function init() {
35 add_action('wp_ajax_upk_dynamic_select_input_data', array($this, 'getSelectInputData'));
36 }
37
38 /**
39 * get Ajax Data
40 */
41 public function getSelectInputData() {
42 $nonce = isset($_POST['security']) ? sanitize_text_field($_POST['security']) : '';
43
44 try {
45 if (!wp_verify_nonce($nonce, 'upk_dynamic_select')) {
46 throw new \Exception('Invalid request');
47 }
48
49 if (!current_user_can('edit_posts')) {
50 throw new \Exception('Unauthorized request');
51 }
52
53 $query = isset($_POST['query']) ? sanitize_text_field($_POST['query']) : '';
54
55 if ($query == 'terms') {
56 $data = $this->getTerms();
57 } else if ($query == 'authors') {
58 $data = $this->getAuthors();
59 } else {
60 $data = $this->getPosts();
61 }
62
63 wp_send_json_success($data);
64 } catch (\Exception $e) {
65 wp_send_json_error($e->getMessage());
66 }
67
68 die();
69 }
70
71 /**
72 * Get Post Type
73 * @return string
74 */
75 protected function getPostType() {
76 return isset($_POST['post_type']) ? sanitize_text_field($_POST['post_type']) : '';
77 }
78
79 /**
80 * @return string[]|\WP_Post_Type[]
81 */
82 protected function getAllPublicPostTypes() {
83 return array_values(get_post_types(['public' => true]));
84 }
85
86 /**
87 * @return string
88 */
89 protected function getSearchQuery() {
90 return isset($_POST['search_text']) ? sanitize_text_field($_POST['search_text']) : '';
91 }
92
93 /**
94 * @return array|mixed
95 */
96 protected function getselecedIds() {
97 return isset($_POST['ids']) ? sanitize_text_field($_POST['ids']) : [];
98 }
99
100
101 /**
102 * @param string $taxonomy
103 *
104 * @return mixed|string
105 */
106 public function getTaxonomyName($taxonomy = '') {
107 $taxonomies = get_taxonomies(['public' => true], 'objects');
108 $taxonomies = array_column($taxonomies, 'label', 'name');
109
110 return isset($taxonomies[$taxonomy]) ? $taxonomies[$taxonomy] : '';
111 }
112
113 /**
114 * @return string[]|\WP_Taxonomy[]
115 */
116 protected function getAllPublicTaxonomies() {
117 return array_values(get_taxonomies(['public' => true]));
118 }
119
120 /**
121 * Get Post Query Data
122 *
123 * @return array
124 */
125 public function getPosts() {
126 $include = $this->getselecedIds();
127 $searchText = $this->getSearchQuery();
128
129 $args = [];
130
131 $args['post_status'] = 'publish';
132
133 if ($this->getPostType()) {
134 $args['post_type'] = $this->getPostType();
135 } else {
136 $args['post_type'] = $this->getAllPublicPostTypes();
137 }
138 if (!empty($include)) {
139 $args['post__in'] = $include;
140 $args['posts_per_page'] = count($include);
141 } else {
142 $args['posts_per_page'] = -1;
143 }
144 if ($searchText) {
145 $args['s'] = $searchText;
146 }
147
148 $query = new \WP_Query($args);
149 $results = [];
150 foreach ($query->posts as $post) {
151 $post_type_obj = get_post_type_object($post->post_type);
152 if (!empty($data['include_type'])) {
153 $text = $post_type_obj->labels->name . ': ' . $post->post_title;
154 } else {
155 $text = ($post_type_obj->hierarchical) ? $this->get_post_name_with_parents($post) : $post->post_title;
156 }
157
158 $results[] = [
159 'id' => $post->ID,
160 'text' => esc_html($text),
161 ];
162 }
163
164 return $results;
165 }
166
167 private function get_post_name_with_parents($post, $max = 3) {
168 if (0 === $post->post_parent) {
169 return $post->post_title;
170 }
171 $separator = is_rtl() ? ' < ' : ' > ';
172 $test_post = $post;
173 $names = [];
174 while ($test_post->post_parent > 0) {
175 $test_post = get_post($test_post->post_parent);
176 if (!$test_post) {
177 break;
178 }
179 $names[] = $test_post->post_title;
180 }
181
182 $names = array_reverse($names);
183 if (count($names) < ($max)) {
184 return implode($separator, $names) . $separator . $post->post_title;
185 }
186
187 $name_string = '';
188 for ($i = 0; $i < ($max - 1); $i++) {
189 $name_string .= $names[$i] . $separator;
190 }
191 return $name_string . '...' . $separator . $post->post_title;
192 }
193
194 /**
195 * Get Terms query data
196 *
197 * @return array
198 */
199 public function getTerms() {
200 $search_text = $this->getSearchQuery();
201 $taxonomies = $this->getAllPublicTaxonomies();
202 $include = $this->getselecedIds();
203
204 if ($this->getPostType() == '_ultimate_post_kit_pro_related_post_type') {
205 $post_type = $this->getAllPublicPostTypes();
206 } elseif ($this->getPostType()) {
207 $post_type = $this->getPostType();
208 }
209
210 $post_taxonomies = get_object_taxonomies($post_type);
211 $taxonomies = array_intersect($post_taxonomies, $taxonomies);
212 $data = [];
213
214 if (empty($taxonomies)) {
215 return $data;
216 }
217
218 $args = [
219 'taxonomy' => $taxonomies,
220 'hide_empty' => true,
221 ];
222
223 if (!empty($include)) {
224 $args['include'] = $include;
225 }
226
227 if ($search_text) {
228 $args['number'] = 20;
229 $args['search'] = $search_text;
230 }
231
232 $terms = get_terms($args);
233
234 if (is_wp_error($terms) || empty($terms)) {
235 return $data;
236 }
237
238 foreach ($terms as $term) {
239 $label = $term->name;
240 $taxonomy_name = $this->getTaxonomyName($term->taxonomy);
241
242 if ($taxonomy_name) {
243 $label = "{$taxonomy_name}: {$label}";
244 }
245
246 $data[] = [
247 'id' => $term->term_taxonomy_id,
248 'text' => $label,
249 ];
250 }
251
252 return $data;
253 }
254
255 /**
256 * Get Authors query Data
257 *
258 * @return array
259 */
260 public function getAuthors() {
261 $include = $this->getselecedIds();
262 $search_text = $this->getSearchQuery();
263
264 $args = [
265 'fields' => ['ID', 'display_name'],
266 'orderby' => 'display_name',
267 ];
268
269 if (!empty($include)) {
270 $args['include'] = $include;
271 }
272
273 if ($search_text) {
274 $args['number'] = 20;
275 $args['search'] = "*$search_text*";
276 }
277
278 $users = get_users($args);
279
280 $data = [];
281
282 if (empty($users)) {
283 return $data;
284 }
285
286 foreach ($users as $user) {
287 $data[] = [
288 'id' => $user->ID,
289 'text' => $user->display_name,
290 ];
291 }
292
293 return $data;
294 }
295 }
296
297 function ultimatePostKit_dynamic_select_input_module() {
298
299 return UltimatePostKit_Dynamic_Select_Input_Module::get_instance();
300 }
301 ultimatePostKit_dynamic_select_input_module()->init();
302