PluginProbe
WDesignKit – AI Templates, Widget Builder & MCP Workflow / 2.6.2
WDesignKit – AI Templates, Widget Builder & MCP Workflow v2.6.2
2.6.6 2.6.5 2.6.4 2.6.3 2.6.2 2.6.1 2.6.0 2.5.5 2.5.4 2.5.3 2.5.2 2.5.1 2.5.0 2.4.0 2.3.3 2.3.2 2.3.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 All 128 releases
wdesignkit / includes / admin / api / class-wdkit-code-snippet.php

class-wdkit-code-snippet.php in WDesignKit – AI Templates, Widget Builder & MCP Workflow 2.6.2, at includes/admin/api/class-wdkit-code-snippet.php

916 lines 31.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The file that defines the core plugin class
4 *
5 * @link https://posimyth.com/
6 * @since 2.0.0
7 *
8 * @package Wdesignkit
9 * @subpackage Wdesignkit/includes
10 */
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Wdkit_Code_Snippet class
18 *
19 * @since 2.0.0
20 */
21 if ( ! class_exists( 'Wdkit_Code_Snippet' ) ) {
22
23 /**
24 * Wdkit_Code_Snippet class
25 *
26 * @since 2.0.0
27 */
28 class Wdkit_Code_Snippet {
29
30 /**
31 * Member Variable
32 *
33 * @var instance
34 */
35 private static $instance;
36
37 /**
38 * Member Variable
39 *
40 * @var staring $wdkit_api
41 */
42 public $wdkit_api = WDKIT_SERVER_API_URL . 'api/wp/';
43
44 /**
45 * Get instance
46 *
47 * @return object
48 */
49 public static function get_instance() {
50 if ( null === self::$instance ) {
51 self::$instance = new self();
52 }
53 return self::$instance;
54 }
55
56 /**
57 * Constructor
58 */
59 public function __construct() {
60 add_action( 'wp_ajax_wdkit_code_snippet', array( $this, 'wdkit_code_snippet' ) );
61 }
62
63 /**
64 * Main API Call for Code Snippet
65 */
66 public function wdkit_code_snippet() {
67 check_ajax_referer( 'wdkit_nonce', 'kit_nonce' );
68
69 if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
70 wp_send_json_error( array( 'content' => __( 'Insufficient permissions.', 'wdesignkit' ) ) );
71 }
72 $type = isset( $_POST['type'] ) ? strtolower( sanitize_text_field( wp_unslash( $_POST['type'] ) ) ) : false;
73
74 if ( ! $type ) {
75 $data = array(
76 'success' => false,
77 'message' => __( 'Something went wrong.', 'wdesignkit' ),
78 );
79 }
80
81 switch ( $type ) {
82 case 'code_snippet_filter':
83 $data = $this->wdkit_code_snippet_filter();
84 break;
85
86 case 'filter_opt':
87 $data = $this->wdkit_code_snippet_filter_opt();
88 break;
89
90 case 'existing_snippet':
91 $data = $this->wdkit_code_snippet_existing_snippet();
92 break;
93
94 case 'favorite_unfavorite':
95 $data = $this->wdkit_snippet_favorite_unfavorite();
96 break;
97
98 case 'delete_code_snippet':
99 $data = $this->wdkit_delete_code_snippet();
100 break;
101
102 case 'save_code_snippet':
103 $data = $this->wkit_save_code_snippet();
104 break;
105
106 case 'get_user_snippets':
107 $data = $this->wkit_get_user_snippets();
108 break;
109
110 case 'get_user_snippets_favorite':
111 $data = $this->wkit_get_user_snippets_favorite();
112 break;
113
114 case 'download_code_snippet':
115 $data = $this->wkit_download_code_snippet();
116 break;
117
118 case 'get_snippet_info':
119 $data = $this->wdkit_get_snippet_info();
120 break;
121
122 case 'get_snippet_kit':
123 $data = $this->wdkit_get_snippet_kit();
124 break;
125
126 case 'get_user_roles':
127 $data = $this->wdkit_get_user_roles();
128 break;
129
130 case 'snippet_bundle_count':
131 $data = $this->wdkit_snippet_bundle_count();
132 break;
133
134 default:
135 break;
136 }
137
138 wp_send_json( $data );
139 wp_die();
140 }
141
142 /**
143 * Code snippet filter
144 *
145 * @return array
146 */
147 public function wdkit_code_snippet_filter() {
148
149 $current_page = isset( $_POST['CurrentPage'] ) ? sanitize_text_field( wp_unslash( $_POST['CurrentPage'] ) ) : 1;
150 $par_page = isset( $_POST['ParPage'] ) ? sanitize_text_field( wp_unslash( $_POST['ParPage'] ) ) : 1;
151 $search = isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '';
152 $free_pro = isset( $_POST['free_pro'] ) ? sanitize_text_field( wp_unslash( $_POST['free_pro'] ) ) : '';
153 $category = isset( $_POST['category'] ) ? sanitize_text_field( wp_unslash( $_POST['category'] ) ) : '';
154 $status = isset( $_POST['status'] ) ? sanitize_text_field( wp_unslash( $_POST['status'] ) ) : '';
155 $plugins = isset( $_POST['plugins'] ) ? sanitize_text_field( wp_unslash( $_POST['plugins'] ) ) : '';
156 $plugin_id_exclude = isset( $_POST['plugin_id_exclude'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin_id_exclude'] ) ) : '';
157 $tags = isset( $_POST['tags'] ) ? sanitize_text_field( wp_unslash( $_POST['tags'] ) ) : '';
158 $snippet_type = isset( $_POST['snippet_type'] ) ? sanitize_text_field( wp_unslash( $_POST['snippet_type'] ) ) : '';
159
160 $data = array(
161 'CurrentPage' => $current_page,
162 'ParPage' => $par_page,
163 );
164
165 if ( ! empty( $search ) ) {
166 $data['search'] = $search;
167 }
168
169 if ( ! empty( $category ) ) {
170 $data['terms_id'] = $category;
171 }
172
173 if ( ! empty( $free_pro ) ) {
174 $data['free_pro'] = $free_pro;
175 }
176
177 if ( ! empty( $status ) ) {
178 $data['status'] = $status;
179 }
180
181 if ( ! empty( $plugins ) ) {
182 $data['plugin_id'] = $plugins;
183 }
184
185 if ( ! empty( $plugin_id_exclude ) ) {
186 $data['plugin_id_exclude'] = $plugin_id_exclude;
187 }
188
189 if ( ! empty( $tags ) ) {
190 $data['tags'] = $tags;
191 }
192
193 if ( ! empty( $snippet_type ) && 'all' !== $snippet_type ) {
194 $data['type'] = $snippet_type;
195 }
196
197 // snippet/browse/list.
198 $response = $this->wkit_api_call( $data, 'snippet/browse/list' );
199
200 if ( $response['success'] ) {
201 $data = array(
202 'success' => true,
203 'data' => $response,
204 );
205 } else {
206 $data = array(
207 'success' => false,
208 'message' => __( 'Failed to fetch snippet.', 'wdesignkit' ),
209 );
210 }
211
212 return $data;
213 }
214
215 /**
216 * Snippet Dynamic Options
217 */
218 public function wdkit_code_snippet_filter_opt() {
219
220 $data = array(
221 'type' => isset( $_POST['opt_type'] ) ? sanitize_text_field( wp_unslash( $_POST['opt_type'] ) ) : 'terms,plugin,tag',
222 'search_tag' => isset( $_POST['search_tag'] ) ? sanitize_text_field( wp_unslash( $_POST['search_tag'] ) ) : '',
223 'url_tag_id' => isset( $_POST['tags'] ) ? sanitize_text_field( wp_unslash( $_POST['tags'] ) ) : '',
224 );
225
226 // snippet/browse/filter.
227 $response = $this->wkit_api_call( $data, 'snippet/browse/filter' );
228
229 if ( $response['success'] ) {
230 $data = array(
231 'success' => true,
232 'data' => $response,
233 );
234 } else {
235 $data = array(
236 'success' => false,
237 'message' => __( 'Failed to fetch filter options.', 'wdesignkit' ),
238 );
239 }
240
241 return $data;
242 }
243
244 /**
245 * Code snippet existing snippet
246 *
247 * @return array
248 */
249 public function wdkit_code_snippet_existing_snippet() {
250 $data = array(
251 'token' => isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '',
252 'search' => isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '',
253 'CurrentPage' => isset( $_POST['CurrentPage'] ) ? sanitize_text_field( wp_unslash( $_POST['CurrentPage'] ) ) : 1,
254 'ParPage' => isset( $_POST['ParPage'] ) ? sanitize_text_field( wp_unslash( $_POST['ParPage'] ) ) : 1,
255 );
256 $response = $this->wkit_api_call( $data, 'snippet/save/get_exist' );
257
258 if ( $response['success'] ) {
259 $data = array(
260 'success' => true,
261 'data' => $response,
262 );
263 } else {
264 $data = array(
265 'success' => false,
266 'message' => __( 'Failed to fetch existing snippet.', 'wdesignkit' ),
267 );
268 }
269
270 return $data;
271 }
272
273 /**
274 * Get post title by ID
275 *
276 * @return array
277 */
278 public function wdkit_get_snippet_info() {
279 $post_id = isset( $_POST['post_id'] ) ? sanitize_text_field( wp_unslash( $_POST['post_id'] ) ) : 0;
280
281 if ( $post_id ) {
282
283 /** File based snippet data */
284 $file_code_list = null;
285
286 if ( class_exists( 'Nexter_Code_Snippets_File_Based' ) ) {
287 $file_based = new Nexter_Code_Snippets_File_Based();
288 $file_code_list = $file_based->getSnippetData( $post_id );
289 }
290
291 if ( ! empty( $file_code_list ) && is_array( $file_code_list ) ) {
292
293 $snippet_data = $file_code_list;
294
295 return array(
296 'success' => true,
297 'source' => 'file',
298 'data' => array(
299 'title' => ! empty( $snippet_data['name'] ) ? $snippet_data['name'] : ucwords( str_replace( '-', ' ', $post_id ) ),
300
301 'description' => ! empty( $snippet_data['description'] ) ? $snippet_data['description'] : '',
302 ),
303 );
304 }
305
306 /** Post based snippet data */
307 $post = get_post( $post_id );
308
309 if ( $post ) {
310 $data = array(
311 'success' => true,
312 'data' => array(
313 'title' => $post->post_title,
314 'description' => get_post_meta( $post_id, 'nxt-code-note', true ),
315 'id' => $post->ID,
316 ),
317 );
318 } else {
319 $data = array(
320 'success' => false,
321 'message' => __( 'Post not found.', 'wdesignkit' ),
322 );
323 }
324 } else {
325 $data = array(
326 'success' => false,
327 'message' => __( 'Invalid post ID.', 'wdesignkit' ),
328 );
329 }
330
331 return $data;
332 }
333
334 /**
335 * Snippet Favorite/Unfavorite.
336 *
337 * @return array
338 */
339 public function wdkit_snippet_favorite_unfavorite() {
340 $data = array(
341 'token' => isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '',
342 'snippet_id' => isset( $_POST['snippet_id'] ) ? sanitize_text_field( wp_unslash( $_POST['snippet_id'] ) ) : '',
343 'type' => isset( $_POST['favorite_type'] ) ? sanitize_text_field( wp_unslash( $_POST['favorite_type'] ) ) : '',
344 );
345
346 $response = $this->wkit_api_call( $data, 'snippet/favorite' );
347
348 if ( $response['success'] ) {
349 $data = $response;
350 } else {
351 $data = array(
352 'success' => false,
353 'message' => __( 'Failed to favorite/unfavorite snippet.', 'wdesignkit' ),
354 );
355 }
356
357 return $data;
358 }
359
360 /**
361 * Snippet Delete
362 */
363 public function wdkit_delete_code_snippet() {
364 $data = array(
365 'token' => isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '',
366 'id' => isset( $_POST['id'] ) ? sanitize_text_field( wp_unslash( $_POST['id'] ) ) : '',
367 );
368 $response = $this->wkit_api_call( $data, 'snippet/delete' );
369
370 if ( $response['success'] ) {
371 $data = $response;
372 } else {
373 $data = array(
374 'success' => false,
375 'message' => __( 'Failed to delete snippet.', 'wdesignkit' ),
376 );
377 }
378
379 return $data;
380 }
381
382 /**
383 * Snippet Save
384 */
385 public function wkit_save_code_snippet() {
386 $post_id = isset( $_POST['post_id'] ) ? sanitize_text_field( wp_unslash( $_POST['post_id'] ) ) : 0;
387 $description = isset( $_POST['description'] ) ? sanitize_text_field( wp_unslash( $_POST['description'] ) ) : '';
388
389 if ( empty( $post_id ) ) {
390 $response = array(
391 'success' => false,
392 'message' => esc_html__( 'Data Not Found', 'wdesignkit' ),
393 'description' => esc_html__( 'Post ID Not Found', 'wdesignkit' ),
394 );
395
396 return $response;
397 }
398
399 /** File based snippet data */
400 $file_code_list = null;
401
402 if ( class_exists( 'Nexter_Code_Snippets_File_Based' ) ) {
403 $file_based = new Nexter_Code_Snippets_File_Based();
404 $file_code_list = $file_based->getSnippetData( $post_id );
405 }
406
407 /** Get file based snippet data */
408 if ( ! empty( $file_code_list ) && is_array( $file_code_list ) ) {
409
410 $type = $file_code_list['type'] ?? '';
411 $get_data = array(
412 'id' => $post_id,
413 'name' => $file_code_list['name'] ?? '',
414 'description' => ( ! empty( $description ) ) ? $description : ( $file_code_list['description'] ?? '' ),
415 'type' => $type,
416 'post_type' => $file_code_list['post_type'] ?? '',
417 'tags' => $file_code_list['tags'] ?? '',
418 'codeExecute' => $file_code_list['codeExecute'] ?? '',
419 'status' => $file_code_list['status'] ?? '',
420 'langCode' => $file_code_list['langCode'] ?? '',
421 'htmlHooks' => $file_code_list['htmlHooks'] ?? '',
422 'hooksPriority' => $file_code_list['hooksPriority'] ?? '',
423 'include_data' => $file_code_list['include_data'] ?? '',
424 'exclude_data' => $file_code_list['exclude_data'] ?? '',
425 'in_sub_data' => $file_code_list['in_sub_data'] ?? '',
426 'ex_sub_data' => $file_code_list['ex_sub_data'] ?? '',
427
428 'word_count' => $file_code_list['word_count'] ?? '',
429 'word_interval' => $file_code_list['word_interval'] ?? '',
430 'post_number' => $file_code_list['post_number'] ?? '',
431 'css_selector' => $file_code_list['css_selector'] ?? '',
432 'element_index' => $file_code_list['element_index'] ?? '',
433
434 'insertion' => $file_code_list['insertion'] ?? '',
435 'location' => $file_code_list['location'] ?? '',
436 'customname' => $file_code_list['customname'] ?? '',
437 'compresscode' => $file_code_list['compresscode'] ?? '',
438 'startDate' => $file_code_list['startDate'] ?? '',
439 'endDate' => $file_code_list['endDate'] ?? '',
440 'shortcodeattr' => $file_code_list['shortcodeattr'] ?? '',
441 'smart_conditional_logic' => $file_code_list['smart_conditional_logic'] ?? '',
442 'php_hidden_execute' => $file_code_list['php_hidden_execute'] ?? '',
443 );
444
445 } else {
446 /** Get post based snippet data */
447 $type = get_post_meta( $post_id, 'nxt-code-type', true );
448
449 $get_data = array(
450 'id' => $post_id,
451 'name' => get_the_title( $post_id ),
452 'description' => ( ! empty( $description ) ) ? $description : ( get_post_meta( $post_id, 'nxt-code-note', true ) ?? '' ),
453 'type' => $type,
454 'post_type' => get_post_type( $post_id ),
455 'tags' => get_post_meta( $post_id, 'nxt-code-tags', true ),
456 'codeExecute' => get_post_meta( $post_id, 'nxt-code-execute', true ),
457 'status' => get_post_meta( $post_id, 'nxt-code-status', true ),
458 'langCode' => get_post_meta( $post_id, 'nxt-' . $type . '-code', true ),
459 'htmlHooks' => get_post_meta( $post_id, 'nxt-code-html-hooks', true ),
460 'hooksPriority' => get_post_meta( $post_id, 'nxt-code-hooks-priority', true ),
461 'include_data' => get_post_meta( $post_id, 'nxt-add-display-rule', true ),
462 'exclude_data' => get_post_meta( $post_id, 'nxt-exclude-display-rule', true ),
463 'in_sub_data' => get_post_meta( $post_id, 'nxt-in-sub-rule', true ),
464 'ex_sub_data' => get_post_meta( $post_id, 'nxt-ex-sub-rule', true ),
465 // Word-based insertion settings.
466 'word_count' => get_post_meta( $post_id, 'nxt-insert-word-count', true ),
467 'word_interval' => get_post_meta( $post_id, 'nxt-insert-word-interval', true ),
468 'post_number' => get_post_meta( $post_id, 'nxt-post-number', true ),
469 // CSS Selector settings.
470 'css_selector' => get_post_meta( $post_id, 'nxt-css-selector', true ),
471 'element_index' => get_post_meta( $post_id, 'nxt-element-index', true ),
472 // Missing fields that should be exported.
473 'insertion' => get_post_meta( $post_id, 'nxt-code-insertion', true ),
474 'location' => get_post_meta( $post_id, 'nxt-code-location', true ),
475 'customname' => get_post_meta( $post_id, 'nxt-code-customname', true ),
476 'compresscode' => get_post_meta( $post_id, 'nxt-code-compresscode', true ),
477 'startDate' => get_post_meta( $post_id, 'nxt-code-startdate', true ),
478 'endDate' => get_post_meta( $post_id, 'nxt-code-enddate', true ),
479 'shortcodeattr' => get_post_meta( $post_id, 'nxt-code-shortcodeattr', true ),
480 'smart_conditional_logic' => get_post_meta( $post_id, 'nxt-smart-conditional-logic', true ),
481 'php_hidden_execute' => get_post_meta( $post_id, 'nxt-code-php-hidden-execute', true ),
482 );
483 }
484 $array_data = array(
485 'token' => isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '',
486 'type' => isset( $_POST['stype'] ) ? sanitize_text_field( wp_unslash( $_POST['stype'] ) ) : '',
487 'title' => isset( $_POST['title'] ) ? sanitize_text_field( $_POST['title'] ) : '',
488 'plugin_id' => isset( $_POST['plugin'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) : '',
489 'terms_id' => isset( $_POST['category'] ) ? sanitize_text_field( wp_unslash( $_POST['category'] ) ) : '',
490 'data' => wp_json_encode(
491 array(
492 'generator' => 'WDesignKit Export v' . WDKIT_VERSION,
493 'date_created' => current_time( 'Y-m-d H:i' ),
494 'snippets' => array( $get_data ),
495 )
496 ),
497 );
498
499 $stype = isset( $_POST['stype'] ) ? sanitize_text_field( wp_unslash( $_POST['stype'] ) ) : 'new';
500 $save_type = ( ! empty( $stype ) && 'existing' === $stype ) ? 'exist' : 'new';
501 if ( isset( $_POST['snippet_id'] ) && ! empty( $_POST['snippet_id'] ) && 'exist' === $save_type ) {
502 $array_data['id'] = sanitize_text_field( wp_unslash( $_POST['snippet_id'] ) );
503 }
504
505 if ( 'new' === $save_type ) {
506 $array_data['w_unique'] = isset( $_POST['w_unique'] ) ? sanitize_text_field( wp_unslash( $_POST['w_unique'] ) ) : '';
507 $array_data['code_type'] = $type;
508 $array_data['post_content'] = $description;
509 }
510
511 $response = $this->wkit_api_call( $array_data, 'snippet/save/' . $save_type );
512
513 if ( $response['success'] ) {
514 $data = array(
515 'success' => true,
516 'data' => $response,
517 );
518 } else {
519 $data = array(
520 'success' => false,
521 'message' => __( 'Failed to save snippet.', 'wdesignkit' ),
522 );
523 }
524
525 return $data;
526 }
527
528 /**
529 * Get Snippet Kit
530 */
531 public function wdkit_get_snippet_kit() {
532 $data = array();
533
534 $kit_id = isset( $_POST['kit_id'] ) ? sanitize_text_field( wp_unslash( $_POST['kit_id'] ) ) : '';
535 if ( empty( $kit_id ) ) {
536 $data = array(
537 'success' => false,
538 'message' => __( 'Kit ID is required.', 'wdesignkit' ),
539 );
540 return $data;
541 }
542
543 $response = $this->wkit_api_call( $data, 'snippet/kit/' . $kit_id );
544
545 if ( $response['success'] ) {
546 $data = array(
547 'success' => true,
548 'data' => $response,
549 );
550 } else {
551 $data = array(
552 'success' => false,
553 'message' => __( 'Failed to fetch snippet kit.', 'wdesignkit' ),
554 );
555 }
556
557 return $data;
558 }
559
560 /**
561 * Get Users Snippets
562 */
563 public function wkit_get_user_snippets() {
564 $array_data = array(
565 'CurrentPage' => isset( $_POST['CurrentPage'] ) ? sanitize_text_field( wp_unslash( $_POST['CurrentPage'] ) ) : 1,
566 'ParPage' => isset( $_POST['ParPage'] ) ? sanitize_text_field( wp_unslash( $_POST['ParPage'] ) ) : '',
567 'search' => isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '',
568 'token' => isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '',
569 );
570
571 $response = $this->wkit_api_call( $array_data, 'snippet/mysnippets' );
572
573 if ( $response['success'] ) {
574 $data = array(
575 'success' => true,
576 'data' => $response,
577 );
578 } else {
579 $data = array(
580 'success' => false,
581 'message' => __( 'Failed to fetch snippet.', 'wdesignkit' ),
582 );
583 }
584
585 return $data;
586 }
587
588 /**
589 * Get User Snippets Favorite
590 */
591 public function wkit_get_user_snippets_favorite() {
592 $array_data = array(
593 'CurrentPage' => isset( $_POST['CurrentPage'] ) ? sanitize_text_field( wp_unslash( $_POST['CurrentPage'] ) ) : 1,
594 'ParPage' => isset( $_POST['ParPage'] ) ? sanitize_text_field( wp_unslash( $_POST['ParPage'] ) ) : 24,
595 'token' => isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '',
596 'search' => isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '',
597 );
598 $response = $this->wkit_api_call( $array_data, 'snippet/favorite/get' );
599 if ( $response['success'] ) {
600 $data = array(
601 'success' => true,
602 'data' => $response,
603 );
604 } else {
605 $data = array(
606 'success' => false,
607 'message' => __( 'Failed to fetch snippet favorite.', 'wdesignkit' ),
608 );
609 }
610 return $data;
611 }
612
613 /**
614 * Snippet Download
615 */
616 public function wkit_download_code_snippet() {
617 $array_data = array(
618 'id' => isset( $_POST['id'] ) ? sanitize_text_field( wp_unslash( $_POST['id'] ) ) : '',
619 'unique_id' => get_option( 'wdkit_unique_id' ) ?? '',
620 );
621
622 $w_unique = isset( $_POST['w_unique'] ) ? sanitize_text_field( wp_unslash( $_POST['w_unique'] ) ) : '';
623 $free_pro = isset( $_POST['free_pro'] ) ? sanitize_text_field( wp_unslash( $_POST['free_pro'] ) ) : '';
624
625 $response = array();
626 if ( isset( $_POST['token'] ) && ! empty( $_POST['token'] ) ) {
627 $array_data['token'] = sanitize_text_field( wp_unslash( $_POST['token'] ) );
628 // Check if Nexter Pro extension is activated and has valid license.
629 if ( defined( 'NXT_PRO_EXT' ) && class_exists( 'Nexter_Pro_Ext_Activate' ) && 'pro' === $free_pro ) {
630 $array_data['pro_active'] = 'yes';
631 }
632
633 $response = $this->wkit_api_call( $array_data, 'snippet/import' );
634 } else {
635 $response = $this->wkit_api_call( $array_data, 'snippet/import/free' );
636 }
637
638 // Check if API call was successful.
639 if ( ! $response || ! isset( $response['success'] ) || ! $response['success'] ) {
640 $data = array(
641 'success' => false,
642 'data' => array(
643 'message' => __( 'Failed to download snippet from server.', 'wdesignkit' ),
644 ),
645 );
646 }
647
648 if ( $response && isset( $response['data'] ) && 'error' === $response['data'] ) {
649 $data = array(
650 'success' => false,
651 'data' => array(
652 'message' => $response['message'] ?? __( 'Failed to download snippet from server.', 'wdesignkit' ),
653 ),
654 );
655 }
656
657 if ( $response && $response['content'] ) {
658 $json = json_decode( $response['content'], true );
659
660 if ( ! $json || empty( $json['snippets'] ) || ! is_array( $json['snippets'] ) ) {
661 $data = array(
662 'success' => false,
663 'data' => array(
664 'message' => __( 'Invalid snippet file.', 'wdesignkit' ),
665 ),
666 );
667 }
668
669 $snippet = $json['snippets'][0];
670
671 // Decode HTML entities in the snippet name so imported titles show
672 // "&" instead of the raw entity "&#038;". The cloud content stores the
673 // name HTML-encoded, and Nexter's file-based import uses it as-is.
674 if ( isset( $snippet['name'] ) ) {
675 $snippet['name'] = html_entity_decode( (string) $snippet['name'], ENT_QUOTES, 'UTF-8' );
676 }
677
678 if ( empty( $snippet['post_type'] ) || 'nxt-code-snippet' !== $snippet['post_type'] ) {
679 $data = array(
680 'success' => false,
681 'data' => array(
682 'message' => __( 'Invalid snippet type.', 'wdesignkit' ),
683 ),
684 );
685 }
686
687 // Initialize file_based handler if available.
688 $file_based = null;
689 if ( class_exists( 'Nexter_Code_Snippets_File_Based' ) ) {
690 $file_based = new Nexter_Code_Snippets_File_Based();
691 }
692
693 // Use hook to import snippet via nexter-extension function.
694 $import_result = apply_filters( 'nexter_before_import_snippet_file_based', null, $snippet, $file_based );
695
696 // If hook didn't handle it, try calling the function directly.
697 if ( null === $import_result && class_exists( 'Nexter_Builder_Code_Snippets_Render' ) ) {
698 $render_instance = Nexter_Builder_Code_Snippets_Render::get_instance();
699 if ( method_exists( $render_instance, 'import_single_snippet_file_based' ) ) {
700 $import_result = $render_instance->import_single_snippet_file_based( $snippet, $file_based );
701 }
702 }
703
704 // Handle import result.
705 if ( is_wp_error( $import_result ) ) {
706 $data = array(
707 'success' => false,
708 'data' => array(
709 'message' => $import_result->get_error_message(),
710 ),
711 );
712 } elseif ( isset( $import_result['success'] ) && $import_result['success'] ) {
713 // Update index if file-based import was successful.
714 if ( $file_based && method_exists( $file_based, 'snippetIndexData' ) ) {
715 $file_based->snippetIndexData();
716 }
717
718 $data = array(
719 'success' => true,
720 'data' => array(
721 'message' => __( 'Snippet imported.', 'wdesignkit' ),
722 'post_id' => $import_result['id'],
723 'name' => $import_result['name'],
724 ),
725 );
726 } else {
727 // Fallback to old post-based method if file-based failed.
728 $post_id = wp_insert_post(
729 array(
730 'post_title' => sanitize_text_field( $snippet['name'] ),
731 'post_type' => 'nxt-code-snippet',
732 'post_status' => 'publish',
733 )
734 );
735
736 if ( is_wp_error( $post_id ) ) {
737 $data = array(
738 'success' => false,
739 'data' => array(
740 'message' => __( 'Failed to insert snippet.', 'wdesignkit' ),
741 ),
742 );
743 } else {
744 $tags = ( isset( $snippet['tags'] ) && ! empty( $snippet['tags'] ) )
745 ? array_map( 'sanitize_text_field', is_array( $snippet['tags'] ) ? $snippet['tags'] : explode( ',', $snippet['tags'] ) ) : array();
746
747 // Save meta fields.
748 update_post_meta( $post_id, 'nxt-code-type', sanitize_text_field( wp_unslash( $snippet['type'] ?? '' ) ) );
749 update_post_meta( $post_id, 'nxt-code-note', sanitize_text_field( wp_unslash( $snippet['description'] ?? '' ) ) );
750 update_post_meta( $post_id, 'nxt-code-tags', $tags );
751 update_post_meta( $post_id, 'nxt-code-execute', sanitize_text_field( wp_unslash( $snippet['codeExecute'] ?? '' ) ) );
752 update_post_meta( $post_id, 'nxt-code-status', intval( $snippet['status'] ?? 0 ) );
753
754 update_post_meta( $post_id, 'nxt-' . $snippet['type'] . '-code', wp_unslash( $snippet['langCode'] ?? '' ) );
755 update_post_meta( $post_id, 'nxt-code-html-hooks', wp_unslash( $snippet['htmlHooks'] ?? '' ) );
756 update_post_meta( $post_id, 'nxt-code-hooks-priority', sanitize_text_field( wp_unslash( $snippet['hooksPriority'] ?? '' ) ) );
757 update_post_meta( $post_id, 'nxt-add-display-rule', sanitize_text_field( wp_unslash( $snippet['include_data'] ?? '' ) ) );
758 update_post_meta( $post_id, 'nxt-exclude-display-rule', sanitize_text_field( wp_unslash( $snippet['exclude_data'] ?? '' ) ) );
759 update_post_meta( $post_id, 'nxt-in-sub-rule', sanitize_text_field( wp_unslash( $snippet['in_sub_data'] ?? '' ) ) );
760 update_post_meta( $post_id, 'nxt-ex-sub-rule', sanitize_text_field( wp_unslash( $snippet['ex_sub_data'] ?? '' ) ) );
761
762 // Word-based insertion settings.
763 update_post_meta( $post_id, 'nxt-insert-word-count', sanitize_text_field( wp_unslash( $snippet['word_count'] ?? 100 ) ) );
764 update_post_meta( $post_id, 'nxt-insert-word-interval', sanitize_text_field( wp_unslash( $snippet['word_interval'] ?? 200 ) ) );
765 update_post_meta( $post_id, 'nxt-post-number', sanitize_text_field( wp_unslash( $snippet['post_number'] ?? 1 ) ) );
766
767 // CSS Selector settings.
768 update_post_meta( $post_id, 'nxt-css-selector', wp_unslash( $snippet['css_selector'] ?? '' ) );
769 update_post_meta( $post_id, 'nxt-element-index', sanitize_text_field( wp_unslash( $snippet['element_index'] ?? 0 ) ) );
770
771 // Additional save options.
772 update_post_meta( $post_id, 'nxt-code-insertion', sanitize_text_field( wp_unslash( $snippet['insertion'] ?? '' ) ) );
773 update_post_meta( $post_id, 'nxt-code-location', sanitize_text_field( wp_unslash( $snippet['location'] ?? '' ) ) );
774 update_post_meta( $post_id, 'nxt-code-customname', sanitize_text_field( wp_unslash( $snippet['customname'] ?? '' ) ) );
775 update_post_meta( $post_id, 'nxt-code-compresscode', sanitize_text_field( wp_unslash( $snippet['compresscode'] ?? '' ) ) );
776 update_post_meta( $post_id, 'nxt-code-startdate', sanitize_text_field( wp_unslash( $snippet['startDate'] ?? '' ) ) );
777 update_post_meta( $post_id, 'nxt-code-enddate', sanitize_text_field( wp_unslash( $snippet['endDate'] ?? '' ) ) );
778 update_post_meta( $post_id, 'nxt-code-shortcodeattr', wp_unslash( $snippet['shortcodeattr'] ?? '' ) );
779 update_post_meta( $post_id, 'nxt-smart-conditional-logic', wp_unslash( $snippet['smart_conditional_logic'] ?? '' ) );
780 update_post_meta( $post_id, 'nxt-code-php-hidden-execute', sanitize_text_field( wp_unslash( $snippet['php_hidden_execute'] ?? '' ) ) );
781 update_post_meta( $post_id, 'nxt-wdkit-unique-sid', $w_unique );
782
783 $data = array(
784 'success' => true,
785 'data' => array(
786 'message' => __( 'Snippet imported.', 'wdesignkit' ),
787 'post_id' => $post_id,
788 ),
789 );
790 }
791 }
792 } else {
793 $data = array(
794 'success' => false,
795 'data' => array(
796 'message' => __( 'No snippet content received from server.', 'wdesignkit' ),
797 ),
798 );
799 }
800
801 return $data;
802 }
803
804 /**
805 * Get user role
806 */
807 public function wdkit_get_user_roles() {
808 $data = array(
809 'success' => true,
810 'data' => array(
811 'wp_user_can_manage_options' => current_user_can( 'manage_options' ),
812 'wp_user_roles' => wp_get_current_user()->roles,
813 ),
814 );
815
816 return $data;
817 }
818
819 /**
820 *
821 * This Function is used for API call
822 *
823 * @since 1.2.4
824 *
825 * @param array $data give array.
826 * @param array $name store data.
827 */
828 public function wkit_api_call( $data, $name ) {
829 $u_r_l = $this->wdkit_api;
830
831 if ( empty( $u_r_l ) ) {
832 return array(
833 'massage' => esc_html__( 'API Not Found', 'wdesignkit' ),
834 'success' => false,
835 );
836 }
837
838 $args = array(
839 'method' => 'POST',
840 'body' => $data,
841 'timeout' => 100,
842 );
843 $response = wp_remote_post( $u_r_l . $name, $args );
844
845 if ( is_wp_error( $response ) ) {
846 $error_message = $response->get_error_message();
847
848 /* Translators: %s is a placeholder for the error message */
849 $error_message = sprintf( esc_html__( 'API request error: %s', 'wdesignkit' ), esc_html( $error_message ) );
850
851 return array(
852 'massage' => $error_message,
853 'success' => false,
854 );
855 }
856
857 $status_code = wp_remote_retrieve_response_code( $response );
858 if ( 200 === $status_code ) {
859 return json_decode( wp_remote_retrieve_body( $response ), true );
860 }
861
862 $error_message = sprintf( 'Server error: %d', esc_html( $status_code ) );
863
864 if ( isset( $error_data->message ) ) {
865 $error_message .= ' (' . $error_data->message . ')';
866 }
867
868 return array(
869 'massage' => $error_message,
870 'status' => $status_code,
871 'success' => false,
872 );
873 }
874
875 /**
876 * Increment snippet bundle view or download count.
877 *
878 * @return array
879 */
880 public function wdkit_snippet_bundle_count() {
881 $token = isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '';
882 $id = isset( $_POST['id'] ) ? sanitize_text_field( wp_unslash( $_POST['id'] ) ) : '';
883 $action_type = isset( $_POST['action_type'] ) ? sanitize_text_field( wp_unslash( $_POST['action_type'] ) ) : '';
884
885 if ( empty( $token ) || empty( $id ) || empty( $action_type ) ) {
886 return array(
887 'success' => false,
888 'message' => __( 'Missing required parameters.', 'wdesignkit' ),
889 );
890 }
891
892 $array_data = array(
893 'token' => $token,
894 'id' => $id,
895 'action_type' => $action_type,
896 );
897
898 $response = $this->wkit_api_call( $array_data, 'snippet/bundle/count' );
899
900 if ( ! empty( $response['success'] ) ) {
901 return array(
902 'success' => true,
903 'message' => isset( $response['message'] ) ? $response['message'] : __( 'Bundle Count Incremented', 'wdesignkit' ),
904 );
905 }
906
907 return array(
908 'success' => false,
909 'message' => isset( $response['massage'] ) ? $response['massage'] : __( 'Failed to increment bundle count.', 'wdesignkit' ),
910 );
911 }
912 }
913
914 Wdkit_Code_Snippet::get_instance();
915 }
916