PluginProbe
WDesignKit – AI Templates, Widget Builder & MCP Workflow for WordPress / 1.0.7
WDesignKit – AI Templates, Widget Builder & MCP Workflow for WordPress v1.0.7
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 1.0.18 All 127 releases
wdesignkit / includes / admin / class-api.php

class-api.php in WDesignKit – AI Templates, Widget Builder & MCP Workflow for WordPress 1.0.7, at includes/admin/class-api.php

2,836 lines 91.5 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 1.0.0
7 *
8 * @package Wdesignkit
9 * @subpackage Wdesignkit/includes
10 */
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 use wdkit\Wdkit_Wdesignkit;
17 use wdkit\WdKit_enqueue\Wdkit_Enqueue;
18 use wdkit\wdkit_datahooks\Wdkit_Data_Hooks;
19
20 if ( ! class_exists( 'Wdkit_Api_Call' ) ) {
21
22 /**
23 * Main classs call for all api
24 *
25 * @link https://posimyth.com/
26 * @since 1.0.0
27 */
28 class Wdkit_Api_Call {
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_SITE_URL . 'api/wp/';
43
44 /**
45 * Member Variable
46 *
47 * @var staring $widget_folder_u_r_l
48 */
49 public $widget_folder_u_r_l = '';
50
51 /**
52 * Member Variable
53 *
54 * @var staring $e_msg_login
55 */
56 public $e_msg_login = 'Login Error: Check your details and try again.';
57
58 /**
59 * Member Variable
60 *
61 * @var staring $e_desc_login
62 */
63 public $e_desc_login = 'Invalid Login Details';
64
65 /**
66 * Initiator
67 */
68 public static function get_instance() {
69 if ( ! isset( self::$instance ) ) {
70 self::$instance = new self();
71 }
72
73 return self::$instance;
74 }
75
76 /**
77 * Define the core functionality of the plugin.
78 */
79 public function __construct() {
80 add_action( 'wp_ajax_get_wdesignkit', array( $this, 'wdkit_api_call' ) );
81 }
82
83 /**
84 * Error JSON message
85 *
86 * @param array $data give array.
87 * @param string $status api code number.
88 * */
89 public function wdkit_error_msg( $data = null, $status = null ) {
90 wp_send_json_error( $data );
91 wp_die();
92 }
93
94 /**
95 * Success JSON message
96 *
97 * @param array $data give array.
98 * @param string $status api code number.
99 * */
100 public function wdkit_success_msg( $data = null, $status = null ) {
101 wp_send_json_success( $data, $status );
102 wp_die();
103 }
104
105 /**
106 * Get Wdkit Api Call Ajax.
107 */
108 public function wdkit_api_call() {
109
110 check_ajax_referer( 'wdkit_nonce', 'kit_nonce' );
111
112 if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
113 wp_send_json_error( array( 'content' => __( 'Insufficient permissions.', 'wdesignkit' ) ) );
114 }
115
116 $type = isset( $_POST['type'] ) ? strtolower( sanitize_text_field( wp_unslash( $_POST['type'] ) ) ) : false;
117 if ( ! $type ) {
118 $this->wdkit_error_msg( __( 'Something went wrong.', 'wdesignkit' ) );
119 }
120
121 switch ( $type ) {
122 case 'wkit_login':
123 $data = $this->wdkit_login();
124 break;
125 case 'api_login':
126 $data = $this->wdkit_api_login();
127 break;
128 case 'social_login':
129 $data = $this->wdkit_social_login();
130 break;
131 case 'wkit_meta_data':
132 $data = $this->wdkit_meta_data();
133 break;
134 case 'get_user_info':
135 $id = isset( $_POST['id'] ) ? strtolower( sanitize_text_field( wp_unslash( $_POST['id'] ) ) ) : false;
136 $data = $this->wdkit_get_user_info();
137 break;
138 case 'browse_page':
139 $data = $this->wdkit_browse_page();
140 break;
141 case 'widget_browse_page':
142 $data = $this->wdkit_widget_browse_page();
143 break;
144 case 'kit_template':
145 $id = isset( $_POST['id'] ) ? strtolower( sanitize_text_field( wp_unslash( $_POST['id'] ) ) ) : false;
146 $data = $this->wdkit_template();
147 break;
148 case 'template_remove':
149 $data = $this->wdkit_template_remove();
150 break;
151 case 'save_template':
152 $data = $this->wdkit_put_save_template();
153 break;
154 case 'find_template':
155 $data = $this->wdkit_find_existing_template();
156 break;
157 case 'update_template':
158 $data = $this->wdkit_update_template();
159 break;
160 case 'manage_favorite':
161 $data = $this->wdkit_manage_favorite();
162 break;
163 case 'check_plugins_depends':
164 $data = $this->wdkit_check_plugins_depends();
165 break;
166 case 'install_plugins_depends':
167 $data = $this->wdkit_install_plugins_depends();
168 break;
169 case 'import_template':
170 $data = $this->wdkit_import_template();
171 break;
172 case 'import_multi_template':
173 $data = $this->wdkit_import_multi_template();
174 break;
175 case 'import_kit_template':
176 $data = $this->wdkit_import_kit_template();
177 break;
178 case 'shared_with_me':
179 $data = $this->wdkit_shared_with_me();
180 break;
181 case 'manage_workspace':
182 $data = $this->wdkit_manage_workspace();
183 break;
184 case 'wkit_manage_widget_workspace':
185 $data = $this->wdkit_manage_widget_workspace();
186 break;
187 case 'wkit_activate_key':
188 $data = $this->wdkit_activate_key();
189 break;
190 case 'wkit_get_widget_list':
191 $data = $this->wdkit_get_widget_list();
192 break;
193 case 'wkit_manage_widget_category':
194 $data = $this->wdkit_manage_widget_category();
195 break;
196 case 'wkit_create_widget':
197 $data = $this->wdkit_create_widget();
198 break;
199 case 'wkit_export_widget':
200 $data = $this->wdkit_export_widget();
201 break;
202 case 'wkit_import_widget':
203 $data = $this->wdkit_import_widget();
204 break;
205 case 'wkit_delete_widget':
206 $data = $this->wdkit_delete_widget();
207 break;
208 case 'wkit_download_widget':
209 $data = $this->wdkit_download_widget();
210 break;
211 case 'wkit_public_download_widget':
212 $data = $this->wdkit_public_download_widget();
213 break;
214 case 'wkit_add_widget':
215 $data = $this->wdkit_add_widget();
216 break;
217 case 'wkit_favourite_widget':
218 $data = $this->wdkit_favourite_widget();
219 break;
220 case 'wkit_setting_panel':
221 $data = $this->wdkit_setting_panel();
222 break;
223 case 'media_import':
224 $data = $this->wdkit_media_import();
225 break;
226 case 'active_licence':
227 $data = $this->wdkit_activate_licence();
228 break;
229 case 'delete_licence':
230 $data = $this->wdkit_delete_licence_key();
231 break;
232 case 'sync_licence':
233 $data = $this->wdkit_sync_licence_key();
234 break;
235 case 'wkit_logout':
236 $data = $this->wdkit_logout();
237 break;
238 }
239
240 $this->wdkit_success_msg( $data );
241 }
242
243 /**
244 *
245 * This Function is used for API call
246 *
247 * @since 1.0.0
248 *
249 * @param array $data give array.
250 * @param array $name store data.
251 */
252 protected function wkit_api_call( $data, $name ) {
253 $u_r_l = $this->wdkit_api;
254
255 if ( empty( $u_r_l ) ) {
256 return array(
257 'massage' => esc_html__( 'API Not Found', 'wdesignkit' ),
258 'success' => false,
259 );
260 }
261
262 $args = array(
263 'method' => 'POST',
264 'body' => $data,
265 'timeout' => 100,
266 );
267 $response = wp_remote_post( $u_r_l . $name, $args );
268
269 if ( is_wp_error( $response ) ) {
270 $error_message = $response->get_error_message();
271
272 /* Translators: %s is a placeholder for the error message */
273 $error_message = printf( esc_html__( 'API request error: %s', 'wdesignkit' ), esc_html( $error_message ) );
274
275 return array(
276 'massage' => $error_message,
277 'success' => false,
278 );
279 }
280
281 $status_code = wp_remote_retrieve_response_code( $response );
282 if ( 200 === $status_code ) {
283
284 return array(
285 'data' => json_decode( wp_remote_retrieve_body( $response ) ),
286 'massage' => esc_html__( 'Success', 'wdesignkit' ),
287 'status' => $status_code,
288 'success' => true,
289 );
290 }
291
292 $error_message = printf( 'Server error: %d', esc_html( $status_code ) );
293
294 if ( isset( $error_data->message ) ) {
295 $error_message .= ' (' . $error_data->message . ')';
296 }
297
298 return array(
299 'massage' => $error_message,
300 'status' => $status_code,
301 'success' => false,
302 );
303 }
304
305 /**
306 * This Function is used for API call
307 *
308 * @since 1.0.0
309 *
310 * @param string $user_key Dynamic key.
311 * @param string $user_email User email.
312 * @param string $token User token.
313 */
314 protected function wdkit_set_time_out( $user_key, $user_email, $token, $login_type = '' ) {
315
316 if ( 'normal' === $login_type ) {
317 set_transient(
318 'wdkit_auth_' . $user_key,
319 array(
320 'user_email' => sanitize_email( $user_email ),
321 'token' => $token,
322 ),
323 7776000
324 );
325 } else {
326 set_transient(
327 'wdkit_auth_' . $user_key,
328 array(
329 'user_email' => sanitize_email( $user_email ),
330 'token' => $token,
331 ),
332 86400
333 );
334 }
335 }
336
337 /**
338 *
339 * It is Use for user login with email and password.
340 *
341 * @since 1.0.0
342 */
343 protected function wdkit_login() {
344 $user_email = isset( $_POST['user_email'] ) ? strtolower( sanitize_email( wp_unslash( $_POST['user_email'] ) ) ) : false;
345 $user_password = isset( $_POST['user_password'] ) ? sanitize_text_field( wp_unslash( $_POST['user_password'] ) ) : false;
346 $login_type = isset( $_POST['login_type'] ) ? sanitize_text_field( wp_unslash( $_POST['login_type'] ) ) : false;
347 $user_key = strstr( $user_email, '@', true );
348 $response = '';
349
350 delete_transient( 'wdkit_auth_' . $user_key );
351
352 $get_login = get_transient( 'wdkit_auth_' . $user_key );
353
354 if ( ! empty( $user_email ) && ! empty( $user_password ) && false === $get_login ) {
355 $response = WDesignKit_Data_Query::get_data(
356 'login',
357 array(
358 'user_email' => $user_email,
359 'password' => $user_password,
360 )
361 );
362
363 if ( ! empty( $response ) && ! empty( $response['success'] ) ) {
364 if ( ! empty( $response['message'] ) && ! empty( $response['token'] ) ) {
365 if ( false === get_transient( 'wdkit_auth_' . $user_key ) ) {
366 $this->wdkit_set_time_out( $user_key, $user_email, $response['token'], $login_type );
367 }
368 }
369 }
370 } elseif ( ! empty( $get_login ) && ! empty( $get_login['token'] ) ) {
371 $response = array_merge(
372 array(
373 'success' => true,
374 'message' => esc_html__( 'Success! Login successful.', 'wdesignkit' ),
375 'description' => esc_html__( 'Login successful. Keep it up!', 'wdesignkit' ),
376 ),
377 $get_login
378 );
379 }
380
381 wp_send_json( $response );
382 wp_die();
383 }
384
385 /**
386 *
387 * This Function is used for Login with Api (token)
388 *
389 * @version 1.0.0
390 * @access public
391 */
392 protected function wdkit_api_login() {
393 $user_token = isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '';
394 $admin_path = isset( $_POST['plugin_domain'] ) ? esc_url_raw( wp_unslash( $_POST['plugin_domain'] ) ) : '';
395 $login_type = isset( $_POST['login_type'] ) ? sanitize_text_field( wp_unslash( $_POST['login_type'] ) ) : '';
396
397 if ( empty( $user_token ) ) {
398 $result = array(
399 'success' => false,
400 'token' => '',
401 'data' => array(
402 'message' => $this->e_msg_login,
403 'description' => $this->e_desc_login,
404 ),
405 );
406
407 wp_send_json( $result );
408 wp_die();
409 }
410
411 $array_data = array(
412 'token' => $user_token,
413 'plugin_domain' => $admin_path,
414 );
415
416 $response = $this->wkit_api_call( $array_data, 'login/api' );
417 $success = ! empty( $response['success'] ) ? is_bool( $response['success'] ) : false;
418
419 if ( empty( $success ) ) {
420 $result = array(
421 'data' => $response,
422 'token' => '',
423 'success' => false,
424 );
425
426 wp_send_json( $result );
427 wp_die();
428 }
429
430 $response = json_decode( wp_json_encode( $response['data'] ), true );
431 $user_email = ! empty( $response['user']['user_email'] ) ? sanitize_email( $response['user']['user_email'] ) : '';
432 $user_key = strstr( $user_email, '@', true );
433
434 $this->wdkit_set_time_out( $user_key, $user_email, $user_token, $login_type );
435
436 $result = array(
437 'success' => true,
438 'data' => $response,
439 'token' => $user_token,
440 );
441
442 wp_send_json( $result );
443 wp_die();
444 }
445
446 /**
447 *
448 * This Function is used for social Login
449 *
450 * @version 1.0.0
451 * @access public
452 */
453 protected function wdkit_social_login() {
454 $user_state = isset( $_POST['state'] ) ? sanitize_text_field( wp_unslash( $_POST['state'] ) ) : '';
455 $login_type = isset( $_POST['login_type'] ) ? sanitize_text_field( wp_unslash( $_POST['login_type'] ) ) : '';
456
457 $array_data = array( 'state' => $user_state );
458
459 $response = $this->wkit_api_call( $array_data, 'login/ip' );
460 $success = ! empty( $response['success'] ) ? $response['success'] : false;
461
462 if ( empty( $success ) ) {
463 $result = array(
464 'data' => $response,
465 'success' => false,
466 );
467
468 wp_send_json( $result );
469 wp_die();
470 }
471
472 $response = json_decode( wp_json_encode( $response['data'] ), true );
473 $user_email = ! empty( $response['user']['user_email'] ) ? sanitize_email( $response['user']['user_email'] ) : '';
474 $user_token = ! empty( $response['token'] ) ? sanitize_text_field( $response['token'] ) : '';
475 $user_key = strstr( $user_email, '@', true );
476
477 if ( ! empty( $response ) && ! empty( $user_token ) ) {
478 $this->wdkit_set_time_out( $user_key, $user_email, $user_token, $login_type );
479 }
480
481 $result = array(
482 'data' => $response,
483 'token' => $user_token,
484 );
485
486 wp_send_json( $result );
487 wp_die();
488 }
489
490 /**
491 *
492 * It is Use for get meta data for non login user
493 *
494 * @since 1.0.0\
495 */
496 protected function wdkit_meta_data() {
497 $type = isset( $_POST['meta_type'] ) ? sanitize_text_field( wp_unslash( $_POST['meta_type'] ) ) : '';
498 $data = array( 'type' => $type );
499
500 $response = $this->wkit_api_call( $data, 'meta' );
501 $success = ! empty( $response['success'] ) ? $response['success'] : false;
502 $status = ! empty( $response['status'] ) ? (int) $response['status'] : 400;
503
504 if ( empty( $success ) ) {
505 $result = array(
506 'plugin' => array(),
507 'builder' => array(),
508 'category' => array(),
509 'widgetscategory' => array(),
510 'tags' => array(),
511 'widgetbuilder' => array(),
512
513 'message' => esc_html__( 'Server Error', 'wdesignkit' ),
514 'description' => esc_html__( 'Server Error, Data Not Found', 'wdesignkit' ),
515 'success' => false,
516 );
517
518 wp_send_json( $result );
519 wp_die();
520 }
521
522 $get_data_one = ! empty( $response['data'] ) ? $response['data'] : array();
523 $statuscode = array( 'HTTP_CODE' => $status );
524
525 $final = json_decode( wp_json_encode( $get_data_one ), true );
526
527 $final['Setting'] = self::wkit_get_settings_panel();
528
529 $final = array(
530 'data' => $final,
531 );
532
533 wp_send_json( $final );
534 wp_die();
535 }
536
537 /**
538 *
539 * It is Use for get all info of user.
540 *
541 * @since 1.0.0
542 * @access public
543 */
544 protected function wdkit_get_user_info() {
545 $email = isset( $_POST['email'] ) ? strtolower( sanitize_email( wp_unslash( $_POST['email'] ) ) ) : false;
546 $builder = isset( $_POST['builder'] ) ? strtolower( sanitize_text_field( wp_unslash( $_POST['builder'] ) ) ) : '';
547
548 $response = array();
549
550 if ( empty( $email ) ) {
551 $response = array(
552 'success' => false,
553 'message' => $this->e_msg_login,
554 'description' => $this->e_desc_login,
555 );
556
557 wp_send_json( $response );
558 wp_die();
559 }
560
561 $token = $this->wdkit_login_user_token( $email );
562 $args = array(
563 'token' => $token,
564 'builder' => $builder,
565 );
566
567 $response = WDesignKit_Data_Query::get_data( 'get_user_info', $args );
568
569 $status = ( ! empty( $response['status'] ) ) ? sanitize_text_field( $response['status'] ) : 'error';
570 $email = isset( $_POST['email'] ) ? strtolower( sanitize_email( wp_unslash( $_POST['email'] ) ) ) : false;
571
572 /**Condtion user for user logout & expire token*/
573 if ( 'Token is Expired' === $status || 'Authorization Token not found' === $status ) {
574 delete_transient( 'wdkit_auth_' . $email );
575 }
576
577 $response['Setting'] = self::wkit_get_settings_panel();
578
579 $response = array(
580 'data' => $response,
581 'success' => true,
582 );
583
584 wp_send_json( $response );
585 wp_die();
586 }
587
588 /**
589 * Browse Page Filter
590 *
591 * @since 1.0.0
592 */
593 protected function wdkit_browse_page() {
594 $args = $this->wdkit_parse_args( $_POST );
595
596 $response = WDesignKit_Data_Query::get_data( 'browse_page', $args );
597
598 wp_send_json( $response );
599 wp_die();
600 }
601
602 /**
603 *
604 * It is Use to get data for widget browse page
605 *
606 * @since 1.0.0
607 * @access public
608 */
609 protected function wdkit_widget_browse_page() {
610 $array_data = array(
611 'CurrentPage' => isset( $_POST['page'] ) ? (int) $_POST['page'] : 1,
612 'builder' => isset( $_POST['buildertype'] ) ? sanitize_text_field( wp_unslash( $_POST['buildertype'] ) ) : '',
613 'category' => isset( $_POST['category'] ) ? sanitize_text_field( wp_unslash( $_POST['category'] ) ) : '',
614 'ParPage' => isset( $_POST['perpage'] ) ? (int) $_POST['perpage'] : 12,
615 'search' => isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '',
616 'free_pro' => isset( $_POST['free_pro'] ) ? sanitize_text_field( wp_unslash( $_POST['free_pro'] ) ) : '',
617 );
618
619 $response = $this->wkit_api_call( $array_data, 'browse_widget' );
620 $success = ! empty( $response['success'] ) ? $response['success'] : false;
621
622 if ( empty( $success ) ) {
623 $response = array(
624 'success' => false,
625 'message' => esc_html__( 'Data Not Found', 'wdesignkit' ),
626 'description' => esc_html__( 'Widget List Not Found', 'wdesignkit' ),
627
628 'widgets' => array(),
629 'widgetscount' => 0,
630 'showwidgets' => 0,
631 );
632
633 wp_send_json( $response );
634 wp_die();
635 }
636
637 $response = json_decode( wp_json_encode( $response['data'] ), true );
638
639 wp_send_json( $response );
640 wp_die();
641 }
642
643 /**
644 *
645 * It is Use for get template from kit.
646 *
647 * @since 1.0.0
648 */
649 protected function wdkit_template() {
650 $args = $this->wdkit_parse_args( $_POST );
651
652 $response = WDesignKit_Data_Query::get_data( 'kit_template', $args );
653
654 wp_send_json( $response );
655 wp_die();
656 }
657
658 /**
659 *
660 * It is Use for remove or delete template.
661 *
662 * @since 1.0.0
663 * @access public
664 */
665 protected function wdkit_template_remove() {
666 $args = $this->wdkit_parse_args( $_POST );
667
668 $user_email = strtolower( sanitize_email( $args['email'] ) );
669 $response = '';
670
671 if ( empty( $user_email ) || empty( $args['template_id'] ) ) {
672 $response = response()->json(
673 array(
674 'message' => $this->e_msg_login,
675 'description' => $this->e_desc_login,
676 'success' => true,
677 )
678 );
679
680 wp_send_json( $response );
681 wp_die();
682 }
683
684 $args['token'] = $this->wdkit_login_user_token( $user_email );
685
686 unset( $user_email );
687 $response = WDesignKit_Data_Query::get_data( 'template_remove', $args );
688
689 wp_send_json( $response );
690 wp_die();
691 }
692
693 /**
694 *
695 * It is Use for save template from page builder.
696 *
697 * @since 1.0.0
698 */
699 protected function wdkit_put_save_template() {
700 $email = isset( $_POST['email'] ) ? strtolower( sanitize_email( wp_unslash( $_POST['email'] ) ) ) : false;
701 $response = '';
702
703 if ( empty( $email ) ) {
704 $response = array(
705 'id' => 0,
706 'editpage' => '',
707 'message' => $this->e_msg_login,
708 'description' => $this->e_desc_login,
709 'success' => false,
710 );
711
712 wp_send_json( $response );
713 wp_die();
714 }
715
716 $args = $this->wdkit_parse_args( $_POST );
717 $args['token'] = $this->wdkit_login_user_token( $email );
718 unset( $args['email'] );
719
720 global $post;
721
722 $post_id = get_the_ID();
723 $custom_fields = array();
724 if ( ! empty( $post_id ) ) {
725 $meta_fields = get_post_custom( get_the_ID() );
726
727 foreach ( $meta_fields as $key => $value ) {
728 if ( str_contains( $key, 'nxt-' ) ) {
729 $custom_fields[ $key ] = $value;
730 }
731 }
732
733 if ( ! empty( $custom_fields ) ) {
734 $data = json_decode( $args['data'], true );
735 $data['custom_meta'] = $custom_fields;
736 $args['data'] = wp_json_encode( $data );
737 }
738 }
739
740 $response = WDesignKit_Data_Query::get_data( 'save_template', $args );
741
742 wp_send_json( $response );
743 wp_die();
744 }
745
746 /**
747 *
748 * It is For Find User Existing template List.
749 *
750 * @since 1.0.6
751 */
752 protected function wdkit_find_existing_template() {
753 $array_data = array(
754 'search' => isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '',
755 'token' => isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '',
756 'type' => isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '',
757 'u_id' => isset( $_POST['u_id'] ) ? sanitize_text_field( wp_unslash( $_POST['u_id'] ) ) : '',
758 'builder' => isset( $_POST['builder'] ) ? sanitize_text_field( wp_unslash( $_POST['builder'] ) ) : '',
759 'parpage' => isset( $_POST['parpage'] ) ? sanitize_text_field( wp_unslash( $_POST['parpage'] ) ) : 12,
760 );
761
762 $response = $this->wkit_api_call( $array_data, 'existing_template' );
763
764 wp_send_json( $response );
765 wp_die();
766 }
767
768 /**
769 *
770 * It is For Update User Existing template List.
771 *
772 * @since 1.0.6
773 */
774 protected function wdkit_update_template() {
775 $array_data = array(
776 'data' => isset( $_POST['data'] ) ? wp_unslash( $_POST['data'] ) : '',
777 'token' => isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '',
778 'type' => isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '',
779 'id' => isset( $_POST['id'] ) ? sanitize_text_field( wp_unslash( $_POST['id'] ) ) : '',
780 );
781
782 $response = $this->wkit_api_call( $array_data, 'existing_template' );
783
784 wp_send_json( $response );
785 wp_die();
786 }
787
788 /**
789 *
790 * It is Use for manage favourite template.
791 *
792 * @since 1.0.0
793 */
794 protected function wdkit_manage_favorite() {
795 $template_id = isset( $_POST['template_id'] ) ? strtolower( sanitize_text_field( wp_unslash( $_POST['template_id'] ) ) ) : 0;
796 $email = isset( $_POST['email'] ) ? strtolower( sanitize_email( wp_unslash( $_POST['email'] ) ) ) : false;
797
798 if ( empty( $email ) || empty( $template_id ) ) {
799 $response = array(
800 'message' => $this->e_msg_login,
801 'description' => $this->e_desc_login,
802 'success' => false,
803 );
804
805 wp_send_json( $response );
806 wp_die();
807 }
808
809 $args = $this->wdkit_parse_args( $_POST );
810 $args['token'] = $this->wdkit_login_user_token( $email );
811
812 unset( $args['email'] );
813 $response = WDesignKit_Data_Query::get_data( 'manage_favorite', $args );
814
815 wp_send_json( $response );
816 wp_die();
817 }
818
819 /**
820 *
821 * It is Use for Check Plugin Dependency of template.
822 *
823 * @since 1.0.0
824 */
825 protected function wdkit_check_plugins_depends() {
826 $plugins = isset( $_POST['plugins'] ) ? json_decode( sanitize_text_field( wp_unslash( $_POST['plugins'] ) ) ) : array();
827 $update_plugin = array();
828
829 if ( empty( $plugins ) || ! is_array( $plugins ) ) {
830 $this->wdkit_error_msg( array( 'plugins' => 'No Plugins' ) );
831 }
832
833 $all_plugins = $this->get_plugins();
834 foreach ( $plugins as $plugin ) {
835 $pluginslug = ! empty( $plugin->plugin_slug ) ? sanitize_text_field( wp_unslash( $plugin->plugin_slug ) ) : '';
836 $free_pro = ! empty( $plugin->freepro ) ? sanitize_text_field( wp_unslash( $plugin->freepro ) ) : '0';
837
838 if ( is_null( $pluginslug ) ) {
839 $plugin->status = 'warning';
840 $update_plugin[] = $plugin;
841
842 continue;
843 }
844
845 if ( ! is_plugin_active( $pluginslug ) ) {
846 if ( isset( $free_pro ) && '1' === $free_pro ) {
847 if ( ! isset( $all_plugins[ $pluginslug ] ) ) {
848 $plugin->status = 'manually';
849 } else {
850 $plugin->status = 'inactive';
851 }
852 } else {
853 $plugin->status = 'inactive';
854 }
855
856 $update_plugin[] = $plugin;
857 } elseif ( is_plugin_active( $pluginslug ) ) {
858 $plugin->status = 'active';
859 $update_plugin[] = $plugin;
860 }
861 }
862
863 $this->wdkit_success_msg( array( 'plugins' => $update_plugin ) );
864 }
865
866 /**
867 *
868 * It is Use for Install dependent plugin.
869 *
870 * @since 1.0.0
871 */
872 protected function wdkit_install_plugins_depends() {
873 $plugins = isset( $_POST['plugins'] ) ? json_decode( sanitize_text_field( wp_unslash( $_POST['plugins'] ) ), true ) : array();
874
875 $responce = Wdkit_Depends_Installer::get_instance()->wdkit_install_plugin( $plugins );
876
877 wp_send_json( $responce );
878 wp_die();
879 }
880
881 /**
882 *
883 * It is Use for get plugin list.
884 *
885 * @since 1.0.0
886 */
887 private function get_plugins() {
888 if ( ! function_exists( 'get_plugins' ) ) {
889 require_once \ABSPATH . 'wp-admin/includes/plugin.php';
890 }
891
892 return get_plugins();
893 }
894
895 /**
896 * Get Download Template Content
897 *
898 * @since 1.0.0
899 */
900 protected function wdkit_import_template() {
901 $args = $this->wdkit_parse_args( $_POST );
902
903 if ( empty( $args['email'] ) ) {
904 $response = array(
905 'content' => '',
906 'message' => esc_html__( 'Invalid import', 'wdesignkit' ),
907 'description' => esc_html__( 'Invalid import: Check your details and try again.', 'wdesignkit' ),
908 'success' => true,
909 );
910
911 wp_send_json( $response );
912 wp_die();
913 }
914
915 $response = '';
916 if ( empty( $args['email'] ) || empty( $args['template_id'] ) ) {
917 $result = array(
918 'content' => '',
919 'message' => esc_html__( 'Invalid import', 'wdesignkit' ),
920 'description' => esc_html__( 'Invalid import: Check your details and try again.', 'wdesignkit' ),
921 'success' => false,
922 );
923
924 wp_send_json( $response );
925 wp_die();
926 }
927
928 $args['token'] = $this->wdkit_login_user_token( $args['email'] );
929
930 unset( $args['email'] );
931 $response = WDesignKit_Data_Query::get_data( 'import_template', $args );
932 $custom_meta = isset( $_POST['custom_meta'] ) ? sanitize_text_field( wp_unslash( $_POST['custom_meta'] ) ) : false;
933
934 /** Custom meta Field */
935 if ( ! empty( $custom_meta ) && 'true' === $custom_meta && ! empty( $response ) && ! empty( $response['content'] ) ) {
936
937 $res_content = json_decode( $response['content'], true );
938 if ( isset( $res_content['custom_meta'] ) && ! empty( $res_content['custom_meta'] ) ) {
939 $meta_data = $res_content['custom_meta'];
940
941 if ( ! empty( $meta_data ) ) {
942 foreach ( $meta_data as $meta_key => $meta_val ) {
943 if ( ! empty( $meta_val[0] ) && is_serialized( $meta_val[0] ) ) {
944 $meta_val[0] = maybe_unserialize( $meta_val[0] );
945 }
946
947 if ( get_post_meta( get_the_ID(), $meta_key, true ) === '' ) {
948 add_post_meta( get_the_ID(), $meta_key, $meta_val[0] );
949 } else {
950 update_post_meta( get_the_ID(), $meta_key, $meta_val[0] );
951 }
952 }
953 }
954 }
955 }
956
957 wp_send_json( $response );
958 wp_die();
959 }
960
961 /**
962 * This Function is Use For Media Import
963 *
964 * @since 1.0.0
965 *
966 * @param array $content store media content.
967 * @param string $editor it is check editor.
968 */
969 public function wdkit_media_import( $content = array(), $editor = '' ) {
970
971 if ( empty( $content ) && empty( $editor ) ) {
972 $args = $this->wdkit_parse_args( $_POST );
973 $content = ! empty( $args['content'] ) ? json_decode( $args['content'], true ) : array();
974 } else {
975 $args = array(
976 'content' => $content,
977 'editor' => $editor,
978 );
979 $content = ! empty( $args['content'] ) ? $args['content'] : array();
980
981 if ( 'elementor' === $args['editor'] ) {
982 $content = json_decode( $content, true );
983 }
984 }
985
986 if ( ! class_exists( 'Wdkit_Import_Images' ) ) {
987 require_once WDKIT_INCLUDES . 'admin/class-wdkit-import-images.php';
988 }
989
990 if ( ! empty( $args['editor'] ) && 'gutenberg' === $args['editor'] && ! empty( $content ) ) {
991 $media_import = array( $content );
992 $media_import = self::blocks_import_media_copy_content( $media_import );
993 $content = $media_import[0];
994 } elseif ( ! empty( $args['editor'] ) && 'elementor' === $args['editor'] && ! empty( $content ) ) {
995 $media_import = array( $content );
996 $media_import = self::widgets_elements_id_change( $media_import );
997 $media_import = self::widgets_import_media_copy_content( $media_import );
998 $content = $media_import[0];
999 }
1000
1001 return $content;
1002 }
1003
1004 /**
1005 * Widgets elements data
1006 *
1007 * @since 1.0.0
1008 * @param string $media_import it is store media data.
1009 */
1010 protected static function widgets_elements_id_change( $media_import ) {
1011 if ( did_action( 'elementor/loaded' ) ) {
1012 return \Elementor\Plugin::instance()->db->iterate_data(
1013 $media_import,
1014 function ( $element ) {
1015 $element['id'] = \Elementor\Utils::generate_random_string();
1016 return $element;
1017 }
1018 );
1019 } else {
1020 return $media_import;
1021 }
1022 }
1023
1024 /**
1025 * Widgets Media import copy content.
1026 *
1027 * @since 1.0.0
1028 *
1029 * @param string $media_import it is store media data.
1030 */
1031 protected static function widgets_import_media_copy_content( $media_import ) {
1032 if ( did_action( 'elementor/loaded' ) ) {
1033
1034 return \Elementor\Plugin::instance()->db->iterate_data(
1035 $media_import,
1036 function ( $element_data ) {
1037 $elements = \Elementor\Plugin::instance()->elements_manager->create_element_instance( $element_data );
1038
1039 if ( ! $elements ) {
1040 return null;
1041 }
1042
1043 return self::widgets_element_import_start( $elements );
1044 }
1045 );
1046 } else {
1047 return $media_import;
1048 }
1049 }
1050
1051 /**
1052 * Start element copy content for media import.
1053 *
1054 * @since 1.0.0
1055 *
1056 * @param string \Elementor\Controls_Stack $element it is store elementor data.
1057 */
1058 protected static function widgets_element_import_start( \Elementor\Controls_Stack $element ) {
1059 $get_element_instance = $element->get_data();
1060 $tp_mi_on_fun = 'on_import';
1061
1062 if ( method_exists( $element, $tp_mi_on_fun ) ) {
1063 $get_element_instance = $element->{$tp_mi_on_fun}( $get_element_instance );
1064 }
1065
1066 foreach ( $element->get_controls() as $get_control ) {
1067 $control_type = \Elementor\Plugin::instance()->controls_manager->get_control( $get_control['type'] );
1068 $control_name = $get_control['name'];
1069
1070 if ( ! $control_type ) {
1071 return $get_element_instance;
1072 }
1073
1074 if ( method_exists( $control_type, $tp_mi_on_fun ) ) {
1075 $get_element_instance['settings'][ $control_name ] = $control_type->{$tp_mi_on_fun}( $element->get_settings( $control_name ), $get_control );
1076 }
1077 }
1078
1079 return $get_element_instance;
1080 }
1081
1082 /**
1083 * Blocks Recursively data
1084 *
1085 * @param string $data_import gutenber data import.
1086 */
1087 public static function blocks_import_media_copy_content( $data_import ) {
1088 if ( ! empty( $data_import ) ) {
1089 foreach ( $data_import[0] as $key => $val ) {
1090 if ( array_key_exists( 'blockName', $val ) && ( empty( $val['blockName'] ) || null === $val['blockName'] || empty( $val['blockName'] ) || ' ' === $val['blockName'] ) ) {
1091 unset( $data_import[0][ $key ] );
1092 }
1093 }
1094 }
1095
1096 return self::blocks_array_recursively_data(
1097 $data_import,
1098 function ( $block_data ) {
1099 $elements = self::blocks_data_instance( $block_data );
1100 return $elements;
1101 }
1102 );
1103 }
1104
1105 /**
1106 * Blocks Recursively data
1107 *
1108 * @param array $data store data.
1109 * @param string $callback store data.
1110 * @param string $args store data.
1111 */
1112 public static function blocks_array_recursively_data( $data, $callback, $args = array() ) {
1113 if ( ( isset( $data['name'] ) && ! empty( $data['name'] ) ) || ( isset( $data['blockName'] ) && ! empty( $data['blockName'] ) ) ) {
1114 if ( ! empty( $data['innerBlocks'] ) ) {
1115 $data['innerBlocks'] = self::blocks_array_recursively_data( $data['innerBlocks'], $callback, $args );
1116 }
1117
1118 return call_user_func( $callback, $data, $args );
1119 }
1120
1121 if ( ! empty( $data ) ) {
1122 $data = (array) $data;
1123 foreach ( $data as $block_key => $block_value ) {
1124 $block_data = self::blocks_array_recursively_data( $data[ $block_key ], $callback, $args );
1125
1126 if ( null === $block_data ) {
1127 continue;
1128 }
1129
1130 $data[ $block_key ] = $block_data;
1131 }
1132 }
1133
1134 return $data;
1135 }
1136
1137 /**
1138 * Check Blocks data media Url
1139 *
1140 * @param array $block_data store data.
1141 * @param array $args store data.
1142 * @param array $block_args store data.
1143 */
1144 public static function blocks_data_instance( array $block_data, array $args = array(), $block_args = null ) {
1145
1146 if ( ( isset( $block_data['name'] ) && isset( $block_data['clientId'] ) && isset( $block_data['attributes'] ) ) || ( isset( $block_data['blockName'] ) && isset( $block_data['attrs'] ) && ! empty( $block_data['attrs'] ) ) ) {
1147 $blocks_attr = isset( $block_data['attributes'] ) ? $block_data['attributes'] : ( isset( $block_data['attrs'] ) ? $block_data['attrs'] : array() );
1148 foreach ( $blocks_attr as $block_key => $block_val ) {
1149 if ( isset( $block_val['url'] ) && isset( $block_val['id'] ) && ! empty( $block_val['url'] ) ) {
1150 $new_media = Wdkit_Import_Images::wdkit_Import_media( $block_val );
1151 $blocks_attr[ $block_key ] = $new_media;
1152 } elseif ( isset( $block_val['url'] ) && ! empty( $block_val['url'] ) && preg_match( '/\.(jpg|png|jpeg|gif|svg|webp)$/', $block_val['url'] ) ) {
1153 $new_media = Wdkit_Import_Images::wdkit_Import_media( $block_val );
1154 $blocks_attr[ $block_key ] = $new_media;
1155 } elseif ( is_array( $block_val ) && ! empty( $block_val ) ) {
1156 if ( ! array_key_exists( 'md', $block_val ) && ! array_key_exists( 'openTypography', $block_val ) && ! array_key_exists( 'openBorder', $block_val ) && ! array_key_exists( 'openShadow', $block_val ) && ! array_key_exists( 'openFilter', $block_val ) ) {
1157 foreach ( $block_val as $key => $val ) {
1158 if ( is_array( $val ) && ! empty( $val ) ) {
1159
1160 if ( isset( $val['url'] ) && ( isset( $val['Id'] ) || isset( $val['id'] ) ) && ! empty( $val['url'] ) ) {
1161 $new_media = Wdkit_Import_Images::wdkit_Import_media( $val );
1162 $blocks_attr[ $block_key ][ $key ] = $new_media;
1163 } elseif ( isset( $val['url'] ) && ! empty( $val['url'] ) && preg_match( '/\.(jpg|png|jpeg|gif|svg|webp)$/', $val['url'] ) ) {
1164 $new_media = Wdkit_Import_Images::wdkit_Import_media( $val );
1165 $blocks_attr[ $block_key ][ $key ] = $new_media;
1166 } else {
1167 foreach ( $val as $sub_key => $sub_val ) {
1168 if ( isset( $sub_val['url'] ) && ( isset( $sub_val['Id'] ) || isset( $sub_val['id'] ) ) && ! empty( $sub_val['url'] ) ) {
1169 $new_media = Wdkit_Import_Images::wdkit_Import_media( $sub_val );
1170 $blocks_attr[ $block_key ][ $key ][ $sub_key ] = $new_media;
1171 } elseif ( isset( $sub_val['url'] ) && ! empty( $sub_val['url'] ) && preg_match( '/\.(jpg|png|jpeg|gif|svg|webp)$/', $sub_val['url'] ) ) {
1172 $new_media = Wdkit_Import_Images::wdkit_Import_media( $sub_val );
1173 $blocks_attr[ $block_key ][ $key ][ $sub_key ] = $new_media;
1174 } elseif ( is_array( $sub_val ) && ! empty( $sub_val ) ) {
1175 foreach ( $sub_val as $sub_key1 => $sub_val1 ) {
1176 if ( isset( $sub_val1['url'] ) && ( isset( $sub_val1['Id'] ) || isset( $sub_val1['id'] ) ) && ! empty( $sub_val1['url'] ) ) {
1177 $new_media = Wdkit_Import_Images::wdkit_Import_media( $sub_val1 );
1178 $blocks_attr[ $block_key ][ $key ][ $sub_key ][ $sub_key1 ] = $new_media;
1179 } elseif ( isset( $sub_val1['url'] ) && ! empty( $sub_val1['url'] ) && preg_match( '/\.(jpg|png|jpeg|gif|svg|webp)$/', $sub_val1['url'] ) ) {
1180 $new_media = Wdkit_Import_Images::wdkit_Import_media( $sub_val1 );
1181 $blocks_attr[ $block_key ][ $key ][ $sub_key ][ $sub_key1 ] = $new_media;
1182 }
1183 }
1184 }
1185 }
1186 }
1187 }
1188 }
1189 }
1190 }
1191 }
1192 if ( isset( $block_data['attributes'] ) ) {
1193 $block_data['attributes'] = $blocks_attr;
1194 } elseif ( isset( $block_data['attrs'] ) ) {
1195 $block_data['attrs'] = $blocks_attr;
1196 }
1197 }
1198
1199 return $block_data;
1200 }
1201
1202 /**
1203 * Kit Template Import Pages/Sections
1204 *
1205 * @since 1.0.0
1206 * */
1207 protected function wdkit_import_kit_template() {
1208
1209 if ( ! current_user_can( 'manage_options' ) ) {
1210 return false;
1211 }
1212
1213 $page_section = ! empty( $_POST['page_section'] ) ? sanitize_text_field( wp_unslash( $_POST['page_section'] ) ) : '';
1214
1215 if ( isset( $page_section ) ) {
1216 $args['page_section'] = ! empty( $page_section ) ? sanitize_text_field( wp_unslash( $page_section ) ) : '';
1217 }
1218
1219 $template_ids = ! empty( $_POST['template_ids'] ) ? json_decode( sanitize_text_field( wp_unslash( $_POST['template_ids'] ) ), true ) : array();
1220 $email = ! empty( $_POST['email'] ) ? strtolower( sanitize_email( wp_unslash( $_POST['email'] ) ) ) : '';
1221 $editor = isset( $_POST['editor'] ) ? sanitize_text_field( wp_unslash( $_POST['editor'] ) ) : '';
1222
1223 if ( empty( $email ) || empty( $template_ids ) ) {
1224 $output = array(
1225 'message' => esc_html__( 'Invalid import', 'wdesignkit' ),
1226 'description' => esc_html__( 'Invalid import: Check your details and try again.', 'wdesignkit' ),
1227 'success' => false,
1228 );
1229
1230 wp_send_json( $output );
1231 wp_die();
1232 }
1233
1234 /**Not Usefull*/
1235 if ( isset( $_POST['select'] ) ) {
1236 $args['post_type'] = ! empty( $_POST['select'] ) ? sanitize_text_field( wp_unslash( $_POST['select'] ) ) : '';
1237 }
1238
1239 $args['custom_meta'] = isset( $_POST['custom_meta'] ) ? sanitize_text_field( wp_unslash( $_POST['custom_meta'] ) ) : false;
1240 $args['editor'] = $editor;
1241
1242 $token = $this->wdkit_login_user_token( $email );
1243
1244 $temp_args = array(
1245 'token' => $token,
1246 'template_id' => $template_ids['id'],
1247 'editor' => $editor,
1248 );
1249
1250 $response = WDesignKit_Data_Query::get_data( 'import_template', $temp_args );
1251 $output = array();
1252
1253 if ( 'error' === $response['content'] ) {
1254 wp_send_json( $response );
1255 wp_die();
1256 } else {
1257 $output[ $template_ids['id'] ] = $this->import_page_section_content( $args, $template_ids['id'], $response, $template_ids );
1258 $output['message'] = $response['message'];
1259 $output['description'] = $response['description'];
1260 $output['success'] = $response['success'];
1261 }
1262
1263 wp_send_json( $output );
1264 wp_die();
1265 }
1266
1267 /**
1268 * Import single template and section from plugin only
1269 * */
1270 protected function wdkit_import_multi_template() {
1271 $args = $this->wdkit_parse_args( $_POST );
1272
1273 if ( ! current_user_can( 'manage_options' ) ) {
1274 return false;
1275 }
1276
1277 if ( empty( $_POST['template_ids'] ) ) {
1278 $output = array(
1279 'data' => array(),
1280 'message' => esc_html__( 'Invalid import', 'wdesignkit' ),
1281 'description' => esc_html__( 'Invalid import: Check your details and try again.', 'wdesignkit' ),
1282 'success' => false,
1283 );
1284
1285 wp_send_json( $output );
1286 wp_die();
1287 }
1288
1289 if ( isset( $_POST['template_ids'] ) ) {
1290 $args['template_ids'] = ! empty( $_POST['template_ids'] ) ? json_decode( sanitize_text_field( wp_unslash( $_POST['template_ids'] ) ), true ) : array();
1291 }
1292
1293 if ( isset( $_POST['page_section'] ) ) {
1294 $args['page_section'] = ! empty( $_POST['page_section'] ) ? sanitize_text_field( wp_unslash( $_POST['page_section'] ) ) : '';
1295 }
1296
1297 if ( isset( $_POST['select'] ) ) {
1298 $args['post_type'] = ! empty( $_POST['select'] ) ? sanitize_text_field( wp_unslash( $_POST['select'] ) ) : '';
1299 }
1300
1301 $args['custom_meta'] = isset( $_POST['custom_meta'] ) ? sanitize_text_field( wp_unslash( $_POST['custom_meta'] ) ) : false;
1302 if ( ! empty( $args['template_ids'] ) && ! empty( $args['page_section'] ) ) {
1303 $output = array();
1304 if ( is_array( $args['template_ids'] ) ) {
1305 foreach ( $args['template_ids'] as $key => $value ) {
1306 if ( ! empty( $value['id'] ) ) {
1307 $token = $this->wdkit_login_user_token( $args['email'] );
1308 $temp_args = array(
1309 'token' => $token,
1310 'template_id' => $value['id'],
1311 'editor' => $args['editor'],
1312 );
1313
1314 $response = WDesignKit_Data_Query::get_data( 'import_template', $temp_args );
1315
1316 if ( 'error' === $response['content'] ) {
1317 wp_send_json( $response );
1318 wp_die();
1319 } else {
1320 $output[ $value['id'] ] = $this->import_page_section_content( $args, $value['id'], $response, $value );
1321 $output['message'] = $response['message'];
1322 $output['description'] = $response['description'];
1323 $output['success'] = $response['success'];
1324 }
1325 }
1326 }
1327 } else {
1328 $token = $this->wdkit_login_user_token( $args['email'] );
1329 $temp_args = array(
1330 'token' => $token,
1331 'template_id' => $args['template_ids'],
1332 'editor' => $args['editor'],
1333 );
1334
1335 $response = WDesignKit_Data_Query::get_data( 'import_template', $temp_args );
1336
1337 if ( 'error' === $response['content'] ) {
1338 wp_send_json( $response );
1339 wp_die();
1340 } else {
1341 $output[ $args['template_ids'] ] = $this->import_page_section_content( $args, $args['template_ids'], $response );
1342 $output['message'] = $response['message'];
1343 $output['description'] = $response['description'];
1344 $output['success'] = $response['success'];
1345 }
1346 }
1347
1348 $output['success'] = true;
1349
1350 wp_send_json( $output );
1351 wp_die();
1352 }
1353 }
1354
1355 /**
1356 * Import single template and section from plugin only
1357 *
1358 * @param array $args store data.
1359 * @param array $template_id store data.
1360 * @param array $data store data.
1361 * @param array $temp_data store data.
1362 * */
1363 private function import_page_section_content( $args, $template_id, $data, $temp_data = array() ) {
1364 $enqueue_instance = new Wdkit_Enqueue();
1365 $get_post_type = $enqueue_instance->wdkit_get_post_type_list();
1366
1367 $post_type = ! empty( $temp_data['type'] ) ? sanitize_text_field( wp_unslash( $temp_data['type'] ) ) : 'page';
1368
1369 if ( ! array_key_exists( $post_type, $get_post_type ) ) {
1370 $post_type = 'page';
1371 }
1372
1373 if ( ! empty( $data ) && ! empty( $data['content'] ) && ! empty( $template_id ) && ! empty( $post_type ) && current_user_can( 'manage_options' ) ) {
1374 $post_content = json_decode( $data['content'] );
1375 $post_title = isset( $post_content->title ) ? sanitize_text_field( $post_content->title ) : '';
1376 $file_type = isset( $post_content->file_type ) ? sanitize_text_field( $post_content->file_type ) : '';
1377 $content = isset( $post_content->content ) ? wp_slash( $post_content->content ) : '';
1378
1379 if ( 'gutenberg' === $args['editor'] || ( 'wdkit' === $args['editor'] && ! empty( $file_type ) && 'wp_block' === $file_type ) ) {
1380 if ( empty( $content ) ) {
1381 wp_send_json(
1382 array(
1383 'template_id' => $template_id,
1384 'message' => 'Content is Empty.',
1385 )
1386 );
1387 wp_die();
1388 } elseif ( ! empty( $content ) && ! empty( $file_type ) && 'wp_block' === $file_type ) {
1389 $parse_blocks = parse_blocks( stripslashes( $content ) );
1390
1391 $editor = ( 'wdkit' === $args['editor'] ) ? 'gutenberg' : $args['editor'];
1392 $content = $this->wdkit_media_import( $parse_blocks, $editor );
1393 $content = addslashes( serialize_blocks( $content ) );
1394
1395 $inserted_post = wp_insert_post(
1396 array(
1397 'post_status' => 'publish',
1398 'post_type' => $post_type,
1399 'post_title' => $post_title,
1400 'post_content' => $content,
1401 )
1402 );
1403
1404 if ( is_wp_error( $inserted_post ) ) {
1405 wp_send_json(
1406 array(
1407 'import_failed' => $inserted_post->get_error_message(),
1408 'code' => $inserted_post->get_error_code(),
1409 )
1410 );
1411 wp_die();
1412 }
1413
1414 if ( ! empty( $args['custom_meta'] ) && 'true' == $args['custom_meta'] ) {
1415 $custom_meta = isset( $post_content->custom_meta ) ? json_decode( wp_json_encode( $post_content->custom_meta ), true ) : '';
1416 if ( ! empty( $custom_meta ) ) {
1417 foreach ( $custom_meta as $meta_key => $meta_val ) {
1418 if ( isset( $meta_val[0] ) && ! empty( $meta_val[0] ) && is_serialized( $meta_val[0] ) ) {
1419 $meta_val[0] = maybe_unserialize( $meta_val[0] );
1420 }
1421
1422 if ( '' === get_post_meta( $inserted_post, $meta_key, true ) && isset( $meta_val[0] ) ) {
1423 add_post_meta( $inserted_post, $meta_key, $meta_val[0] );
1424 }
1425 }
1426 }
1427 }
1428
1429 return array(
1430 'title' => get_the_title( $inserted_post ),
1431 'edit_link' => get_edit_post_link( $inserted_post, 'internal' ),
1432 'view' => get_permalink( $inserted_post ),
1433 );
1434 }
1435 } elseif ( 'elementor' === $args['editor'] || ( 'wdkit' === $args['editor'] && ! empty( $file_type ) && 'elementor' === $file_type ) ) {
1436 if ( did_action( 'elementor/loaded' ) ) {
1437 if ( empty( $content ) ) {
1438 wp_send_json(
1439 array(
1440 'template_id' => $template_id,
1441 'message' => 'Content is Empty.',
1442 )
1443 );
1444 wp_die();
1445 } elseif ( ! empty( $content ) && ! empty( $file_type ) && 'elementor' === $file_type ) {
1446 $post_attributes = array(
1447 'post_title' => $post_title,
1448 'post_type' => $post_type,
1449 'post_status' => 'publish',
1450 );
1451
1452 if ( 'elementor_library' === $post_type ) {
1453 $el_type = ( isset( $post_content->el_type ) && ! empty( $post_content->el_type ) ) ? sanitize_text_field( $post_content->el_type ) : 'page';
1454 $new_document = \Elementor\Plugin::$instance->documents->create(
1455 $el_type,
1456 $post_attributes
1457 );
1458 } else {
1459 $new_document = \Elementor\Plugin::$instance->documents->create(
1460 $post_attributes['post_type'],
1461 $post_attributes
1462 );
1463 }
1464
1465 if ( is_wp_error( $new_document ) ) {
1466 wp_send_json(
1467 array(
1468 'import_failed' => $new_document->get_error_message(),
1469 'code' => $new_document->get_error_code(),
1470 )
1471 );
1472 wp_die();
1473 }
1474
1475 $settings = ( isset( $post_content->settings ) && ! empty( $post_content->settings ) ) ? json_decode( wp_json_encode( $post_content->settings ), true ) : array();
1476
1477 $content = wp_json_encode( $content );
1478 $content = $this->wdkit_media_import( $content, $file_type );
1479
1480 $new_document->save(
1481 array(
1482 'elements' => $content,
1483 'settings' => ! empty( $settings ) ? $settings : array(),
1484 )
1485 );
1486
1487 $inserted_id = $new_document->get_main_id();
1488
1489 if ( ! empty( $args['custom_meta'] ) && 'true' == $args['custom_meta'] ) {
1490 $custom_meta = isset( $post_content->custom_meta ) ? json_decode( wp_json_encode( $post_content->custom_meta ), true ) : '';
1491 if ( ! empty( $custom_meta ) ) {
1492 foreach ( $custom_meta as $meta_key => $meta_val ) {
1493 if ( ! empty( $meta_val[0] ) && is_serialized( $meta_val[0] ) ) {
1494 $meta_val[0] = maybe_unserialize( $meta_val[0] );
1495 }
1496 if ( '' === get_post_meta( $inserted_id, $meta_key, true ) ) {
1497 add_post_meta( $inserted_id, $meta_key, $meta_val[0] );
1498 }
1499 }
1500 }
1501 }
1502
1503 return array(
1504 'title' => get_the_title( $inserted_id ),
1505 'edit_link' => get_edit_post_link( $inserted_id, 'internal' ),
1506 'view' => get_permalink( $inserted_id ),
1507 );
1508 }
1509 } else {
1510 wp_send_json(
1511 array(
1512 'template_id' => $template_id,
1513 'message' => esc_html__( 'Relevant Page Builder not installed or activated', 'wdesignkit' ),
1514 )
1515 );
1516 wp_die();
1517 }
1518 }
1519 }
1520 }
1521
1522 /**
1523 * Share with Me Template and widgets
1524 *
1525 * @since 1.0.0
1526 */
1527 protected function wdkit_shared_with_me() {
1528 $data = isset( $_POST['api_info'] ) ? json_decode( stripslashes( sanitize_text_field( wp_unslash( $_POST['api_info'] ) ) ) ) : '';
1529
1530 $array_data = array(
1531 'token' => isset( $data->token ) ? sanitize_text_field( $data->token ) : '',
1532 'type' => isset( $data->type ) ? sanitize_text_field( wp_unslash( $data->type ) ) : '',
1533 'ParPage' => isset( $data->par_page ) ? (int) $data->par_page : 12,
1534 'CurrentPage' => isset( $data->current_page ) ? (int) $data->current_page : 1,
1535 'builder' => isset( $data->builder ) ? sanitize_text_field( wp_unslash( $data->builder ) ) : '',
1536 );
1537
1538 $response = $this->wkit_api_call( $array_data, 'shared_with_me' );
1539 $success = ! empty( $response['success'] ) ? $response['success'] : false;
1540
1541 wp_send_json( $response );
1542 wp_die();
1543 }
1544
1545 /**
1546 * Add new WorkSpace
1547 *
1548 * @since 1.0.0
1549 */
1550 protected function wdkit_manage_workspace() {
1551 $args = $this->wdkit_parse_args( $_POST );
1552
1553 $user_email = ! empty( $args['email'] ) ? strtolower( sanitize_email( $args['email'] ) ) : '';
1554 $current_wid = ! empty( $_POST['current_wid'] ) ? strtolower( sanitize_text_field( $_POST['current_wid'] ) ) : '';
1555
1556 $args['current_wid'] = $current_wid;
1557
1558 if ( empty( $user_email ) ) {
1559 $response = array(
1560 'message' => $this->e_msg_login,
1561 'description' => $this->e_desc_login,
1562 'success' => false,
1563 );
1564
1565 wp_send_json( $response );
1566 wp_die();
1567 }
1568
1569 $args['token'] = $this->wdkit_login_user_token( $user_email );
1570 unset( $user_email );
1571
1572 $response = WDesignKit_Data_Query::get_data( 'manage_workspace', $args );
1573
1574 return $response;
1575 }
1576
1577 /**
1578 *
1579 * It is Use for manage workspace
1580 *
1581 * @since 1.0.0
1582 */
1583 protected function wdkit_manage_widget_workspace() {
1584 $Workspace_info = isset( $_POST['workspace_info'] ) ? sanitize_text_field( wp_unslash( $_POST['workspace_info'] ) ) : array();
1585 $data = isset( $Workspace_info ) ? json_decode( stripslashes( $Workspace_info ) ) : array();
1586
1587 $array_data = array(
1588 'token' => isset( $data->token ) ? sanitize_text_field( $data->token ) : '',
1589 'wstype' => isset( $data->type ) ? sanitize_text_field( $data->type ) : '',
1590 'widget_id' => isset( $data->widget_id ) ? (int) $data->widget_id : '',
1591 'wid' => isset( $data->wid ) ? (int) $data->wid : '',
1592 'current_wid' => isset( $data->current_wid ) ? (int) $data->current_wid : '',
1593 );
1594
1595 $response = $this->wkit_api_call( $array_data, 'manage_workspace' );
1596
1597 wp_send_json( $response['data'] );
1598 wp_die();
1599 }
1600
1601 /**
1602 *
1603 * It is Use for manage api key page
1604 *
1605 * @since 1.0.0
1606 */
1607 protected function wdkit_activate_key() {
1608 $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : '';
1609 $response = '';
1610
1611 if ( empty( $user_email ) ) {
1612 $response = array(
1613 'message' => $this->e_msg_login,
1614 'description' => $this->e_desc_login,
1615 'success' => false,
1616 );
1617
1618 wp_send_json( $response );
1619 wp_die();
1620 }
1621
1622 $token = $this->wdkit_login_user_token( $email );
1623 $apikey = isset( $_POST['apikey'] ) ? sanitize_key( wp_unslash( $_POST['apikey'] ) ) : '';
1624 $product = isset( $_POST['product'] ) ? sanitize_text_field( wp_unslash( $_POST['product'] ) ) : '';
1625 $product_action = isset( $_POST['product_action'] ) ? sanitize_text_field( wp_unslash( $_POST['product_action'] ) ) : 'activate';
1626 if ( ! empty( $token ) && ! empty( $product ) && ! empty( $product_action ) ) {
1627 $args = array(
1628 'token' => $token,
1629 'product' => $product,
1630 'product_action' => $product_action,
1631 );
1632
1633 if ( 'activate' === $product_action ) {
1634 $args['apikey'] = $apikey;
1635 $args['site_url'] = home_url();
1636 }
1637
1638 $response = Wdkit_Data_Hooks::get_data( 'wkit_activate_key', $args );
1639 }
1640
1641 wp_send_json( $response );
1642 wp_die();
1643 }
1644
1645 /**
1646 *
1647 * It is Use for get local widget list.
1648 *
1649 * @since 1.0.0
1650 */
1651 protected function wdkit_get_widget_list() {
1652 $builder = array();
1653 $a_c_s_d_s_c = array();
1654 $j_s_o_n_array = array();
1655
1656 if ( Wdkit_Wdesignkit::wdkit_is_compatible( 'bricks' ) ) {
1657 array_push( $builder, 'bricks' );
1658 }
1659
1660 if ( Wdkit_Wdesignkit::wdkit_is_compatible( 'elementor' ) ) {
1661 array_push( $builder, 'elementor' );
1662 }
1663
1664 if ( Wdkit_Wdesignkit::wdkit_is_compatible( 'gutenberg' ) ) {
1665 array_push( $builder, 'gutenberg' );
1666 }
1667
1668 foreach ( $builder as $key => $name ) {
1669 $elementor_dir = WDKIT_BUILDER_PATH . '/' . $name;
1670
1671 if ( ! empty( $elementor_dir ) && is_dir( $elementor_dir ) ) {
1672 $elementor_list = scandir( $elementor_dir );
1673 $elementor_list = array_diff( $elementor_list, array( '.', '..' ) );
1674
1675 if ( ! empty( $elementor_list ) ) {
1676 foreach ( $elementor_list as $key => $value ) {
1677 $a_c_s_d_s_c[ filemtime( "{$elementor_dir}/{$value}" ) ]['data'] = $value;
1678 $a_c_s_d_s_c[ filemtime( "{$elementor_dir}/{$value}" ) ]['builder'] = $name;
1679 }
1680 }
1681 }
1682 }
1683
1684 ksort( $a_c_s_d_s_c );
1685 $a_c_s_d_s_c = array_reverse( $a_c_s_d_s_c );
1686
1687 foreach ( $a_c_s_d_s_c as $key => $value ) {
1688 $elementor_dir = WDKIT_BUILDER_PATH . '/' . $value['builder'];
1689
1690 if ( file_exists( "{$elementor_dir}/{$value['data']}" ) && is_dir( "{$elementor_dir}/{$value['data']}" ) ) {
1691 $sub_dir = scandir( "{$elementor_dir}/{$value['data']}" );
1692 $sub = array_diff( $sub_dir, array( '.', '..' ) );
1693
1694 foreach ( $sub as $sub_dir_value ) {
1695 $file = new SplFileInfo( $sub_dir_value );
1696 $check_ext = $file->getExtension();
1697 $ext = pathinfo( $sub_dir_value, PATHINFO_EXTENSION );
1698
1699 if ( 'json' === $ext ) {
1700 $widget1 = WDKIT_BUILDER_PATH . "/{$value['builder']}/{$value['data']}/{$sub_dir_value}";
1701 $filedata = wp_json_file_decode( $widget1 );
1702 $decode_data = json_decode( wp_json_encode( $filedata ), true );
1703 array_push( $j_s_o_n_array, $decode_data['widget_data'] );
1704 }
1705 }
1706 }
1707 }
1708
1709 wp_send_json( $j_s_o_n_array );
1710 wp_die();
1711 }
1712
1713 /**
1714 *
1715 * It is Use for manage widget category.
1716 *
1717 * @since 1.0.0
1718 */
1719 protected function wdkit_manage_widget_category() {
1720 $data = isset( $_POST['info'] ) ? sanitize_text_field( wp_unslash( $_POST['info'] ) ) : '';
1721 $data = json_decode( stripslashes( $data ) );
1722
1723 $type = isset( $data->manage_type ) ? sanitize_text_field( wp_unslash( $data->manage_type ) ) : '';
1724 $wkit_builder_option = get_option( 'wkit_builder' );
1725
1726 if ( empty( $wkit_builder_option ) ) {
1727 add_option( 'wkit_builder', array( 'WDesignKit' ), '', 'yes' );
1728 }
1729
1730 if ( 'get' === $type ) {
1731 if ( ! in_array( 'WDesignKit', $wkit_builder_option ) ) {
1732 update_option( 'wkit_builder', array( 'WDesignKit' ) );
1733 }
1734 } elseif ( 'update' === $type ) {
1735 $list = isset( $data->category_list ) ? $data->category_list : array();
1736 $list = array_unique( $list );
1737 $list = array_values( $list );
1738
1739 if ( ! empty( $list ) ) {
1740 update_option( 'wkit_builder', $list );
1741 }
1742 }
1743
1744 wp_send_json( get_option( 'wkit_builder' ) );
1745 }
1746
1747 /**
1748 *
1749 * Custom_upload_dir
1750 *
1751 * @since 1.0.0
1752 *
1753 * @param array $upload store data.
1754 */
1755 public function custom_upload_dir( $upload ) {
1756 // Specify the path to your custom upload directory.
1757 if ( isset( $this->widget_folder_u_r_l ) && ! empty( $this->widget_folder_u_r_l ) ) {
1758
1759 // Set the custom directory as the upload path.
1760 $upload['path'] = $this->widget_folder_u_r_l;
1761 // Set the URL for the uploaded file.
1762 $upload['url'] = $upload['baseurl'] . $upload['subdir'];
1763 }
1764
1765 return $upload;
1766 }
1767
1768 /**
1769 *
1770 * It is Use for create widget for local
1771 *
1772 * @since 1.0.0
1773 */
1774 protected function wdkit_create_widget() {
1775 $image = '';
1776 if ( isset( $_FILES ) && ! empty( $_FILES ) && isset( $_FILES['image'] ) && ! empty( $_FILES['image'] ) ) {
1777 $image = Wdkit_Data_Hooks::get_super_global_value( $_FILES, 'image' );
1778 }
1779
1780 $icon = '';
1781 if ( isset( $_FILES ) && ! empty( $_FILES ) && isset( $_FILES['icon'] ) && ! empty( $_FILES['icon'] ) ) {
1782 $icon = Wdkit_Data_Hooks::get_super_global_value( $_FILES, 'icon' );
1783 }
1784
1785 $data = ! empty( $_POST['value'] ) ? $this->wdkit_sanitizer_bypass( $_POST, 'value', 'cr_widget' ) : '';
1786 $data = ! empty( $data ) ? stripslashes( $data ) : '';
1787 $return = ! empty( $data ) ? json_decode( $data ) : '';
1788
1789 $all_val = ! empty( $return ) ? $return : '';
1790 if ( empty( $all_val ) ) {
1791
1792 $responce = array(
1793 'message' => esc_html__( 'Data Not Found', 'wdesignkit' ),
1794 'description' => esc_html__( 'something went wrong! please try again later.', 'wdesignkit' ),
1795 'success' => false,
1796 );
1797
1798 wp_send_json( $responce );
1799 wp_die();
1800 }
1801
1802 $file_name = ! empty( $all_val->file_name ) ? sanitize_text_field( $all_val->file_name ) : '';
1803 $folder_name = ! empty( $all_val->folder_name ) ? sanitize_text_field( $all_val->folder_name ) : '';
1804 $old_widget = ! empty( $all_val->old_folder ) ? sanitize_text_field( $all_val->old_folder ) : '';
1805 $description = ! empty( $all_val->description ) ? sanitize_text_field( $all_val->description ) : '';
1806 $json_file = ! empty( $all_val->json_file ) ? $all_val->json_file : '';
1807 $function_call = ! empty( $all_val->call ) ? sanitize_text_field( $all_val->call ) : '';
1808 $plugin = ! empty( $all_val->plugin ) ? $all_val->plugin : '';
1809 $d_image = ! empty( $all_val->d_image ) ? $all_val->d_image : '';
1810 $data = json_decode( $json_file );
1811
1812 $elementor_php_file = ! empty( $all_val->elementor_php_file ) ? $all_val->elementor_php_file : '';
1813 $elementor_js = ! empty( $all_val->elementor_js ) ? $all_val->elementor_js : '';
1814 $elementor_css = ! empty( $all_val->elementor_css ) ? $all_val->elementor_css : '';
1815
1816 $gutenberg_php_file = ! empty( $all_val->gutenberg_php_file ) ? $all_val->gutenberg_php_file : '';
1817 $gutenberg_js = ! empty( $all_val->gutenberg_js ) ? $all_val->gutenberg_js : '';
1818 $gutenberg_css = ! empty( $all_val->gutenberg_css ) ? $all_val->gutenberg_css : '';
1819 $external_js_file = ! empty( $all_val->external_js_file ) ? $all_val->external_js_file : '';
1820 $style_file = ! empty( $all_val->style_file ) ? $all_val->style_file : '';
1821
1822 $bricks_php_file = ! empty( $all_val->bricks_php_file ) ? $all_val->bricks_php_file : '';
1823 $bricks_js = ! empty( $all_val->bricks_js ) ? $all_val->bricks_js : '';
1824 $bricks_css = ! empty( $all_val->bricks_css ) ? $all_val->bricks_css : '';
1825
1826 $old_folder = ! empty( $old_widget ) ? str_replace( ' ', '-', $old_widget ) : '';
1827 $widget_type = ! empty( $data->widget_data->widgetdata->type ) ? sanitize_text_field( $data->widget_data->widgetdata->type ) : '';
1828
1829 if ( empty( $widget_type ) ) {
1830 $responce = array(
1831 'message' => esc_html__( 'Builder Type not found', 'wdesignkit' ),
1832 'description' => esc_html__( 'something went wrong! please try again later.', 'wdesignkit' ),
1833 'success' => false,
1834 );
1835
1836 wp_send_json( $responce );
1837 wp_die();
1838 }
1839
1840 $builder_type_path = trailingslashit( WDKIT_BUILDER_PATH ) . trailingslashit( $widget_type );
1841 $widget_file_url = $builder_type_path . $folder_name;
1842
1843 if ( ! is_dir( $widget_file_url ) ) {
1844 wp_mkdir_p( $widget_file_url );
1845 }
1846
1847 include_once ABSPATH . 'wp-admin/includes/file.php';
1848 \WP_Filesystem();
1849 global $wp_filesystem;
1850 $widget_folder_u_r_l = trailingslashit( $widget_file_url ) . $file_name;
1851 $this->widget_folder_u_r_l = $widget_file_url;
1852
1853 if ( 'elementor' === $plugin ) {
1854 $widget_file_list = scandir( $widget_file_url );
1855 $widget_file_list = array_diff( $widget_file_list, array( '.', '..' ) );
1856
1857 foreach ( $widget_file_list as $sub_dir_value ) {
1858 $file = new SplFileInfo( $sub_dir_value );
1859 $check_ext = $file->getExtension();
1860 $extiona = pathinfo( $sub_dir_value, PATHINFO_EXTENSION );
1861
1862 if ( 'js' === $extiona || 'css' === $extiona || 'json' === $extiona || 'php' === $extiona ) {
1863 $wp_filesystem->rmdir( "$widget_file_url/$sub_dir_value", true );
1864 }
1865 }
1866
1867 if ( ! empty( $elementor_php_file ) ) {
1868 $wp_filesystem->put_contents( "$widget_folder_u_r_l.php", $elementor_php_file );
1869 }
1870 if ( ! empty( $json_file ) ) {
1871 $wp_filesystem->put_contents( "$widget_folder_u_r_l.json", $json_file );
1872 }
1873 if ( ! empty( $elementor_css ) ) {
1874 $wp_filesystem->put_contents( "$widget_folder_u_r_l.css", $elementor_css );
1875 }
1876 if ( ! empty( $elementor_js ) ) {
1877 $wp_filesystem->put_contents( "$widget_folder_u_r_l.js", $elementor_js );
1878 }
1879 } elseif ( 'bricks' === $plugin ) {
1880
1881 $widget_file_list = scandir( $widget_file_url );
1882 $widget_file_list = array_diff( $widget_file_list, array( '.', '..' ) );
1883
1884 foreach ( $widget_file_list as $sub_dir_value ) {
1885 $file = new SplFileInfo( $sub_dir_value );
1886 $check_ext = $file->getExtension();
1887 $extiona = pathinfo( $sub_dir_value, PATHINFO_EXTENSION );
1888
1889 if ( 'js' === $extiona || 'css' === $extiona || 'json' === $extiona || 'php' === $extiona ) {
1890 $wp_filesystem->rmdir( "$widget_file_url/$sub_dir_value", true );
1891 }
1892 }
1893
1894 $wp_filesystem->put_contents( "$widget_folder_u_r_l.php", $bricks_php_file );
1895 $wp_filesystem->put_contents( "$widget_folder_u_r_l.json", $json_file );
1896
1897 if ( ! empty( $bricks_css ) ) {
1898 $wp_filesystem->put_contents( "$widget_folder_u_r_l.css", $bricks_css );
1899 }
1900
1901 if ( ! empty( $bricks_js ) ) {
1902 $wp_filesystem->put_contents( "$widget_folder_u_r_l.js", $bricks_js );
1903 }
1904 } elseif ( 'gutenberg' === $plugin ) {
1905
1906 $widget_file_list = scandir( $widget_file_url );
1907 $widget_file_list = array_diff( $widget_file_list, array( '.', '..' ) );
1908
1909 foreach ( $widget_file_list as $sub_dir_value ) {
1910 $file = new SplFileInfo( $sub_dir_value );
1911 $check_ext = $file->getExtension();
1912 $extiona = pathinfo( $sub_dir_value, PATHINFO_EXTENSION );
1913
1914 if ( 'js' === $extiona || 'css' === $extiona || 'json' === $extiona || 'php' === $extiona ) {
1915 $wp_filesystem->rmdir( "$widget_file_url/$sub_dir_value", true );
1916 }
1917 }
1918
1919 if ( ! empty( $external_js_file ) ) {
1920 $wp_filesystem->put_contents( "$widget_file_url/index.js", $external_js_file );
1921 }
1922 $wp_filesystem->put_contents( "$widget_folder_u_r_l.php", $gutenberg_php_file );
1923 $wp_filesystem->put_contents( "$widget_folder_u_r_l.json", $json_file );
1924 if ( ! empty( $gutenberg_css ) ) {
1925 $wp_filesystem->put_contents( "$widget_folder_u_r_l.css", $gutenberg_css );
1926 }
1927 $wp_filesystem->put_contents( "$widget_folder_u_r_l.js", $gutenberg_js );
1928 }
1929
1930 if ( ! empty( $image ) && ! empty( $image['tmp_name'] ) ) {
1931
1932 $img_type = array( 'jpg', 'png' );
1933
1934 foreach ( $img_type as $imgext ) {
1935 $wp_filesystem->rmdir( "$widget_folder_u_r_l . $imgext", true );
1936 }
1937
1938 $ext = $image['type'];
1939 $img_ext = '';
1940 if ( strpos( $ext, 'jpeg' ) ) {
1941 $img_ext = 'jpg';
1942 } elseif ( strpos( $ext, 'png' ) ) {
1943 $img_ext = 'png';
1944 }
1945 if ( ! empty( $img_ext ) ) {
1946 add_filter( 'upload_dir', array( $this, 'custom_upload_dir' ) );
1947
1948 $uploaded_file = wp_handle_upload( $image, array( 'test_form' => false ) );
1949
1950 rename( $uploaded_file['file'], $widget_folder_u_r_l . '.' . $img_ext );
1951
1952 remove_filter( 'upload_dir', array( $this, 'custom_upload_dir' ) );
1953
1954 }
1955 } elseif ( ! empty( $old_widget ) ) {
1956 $img_url = $data->widget_data->widgetdata->w_image;
1957 $img_ext = ! empty( pathinfo( $img_url )['extension'] ) ? pathinfo( $img_url )['extension'] : '';
1958
1959 if ( ! empty( $img_ext ) ) {
1960 $old_widget_folder = str_replace( ' ', '-', $old_widget );
1961 $old_widget_file = str_replace( ' ', '_', $old_widget );
1962 $img_path = "$builder_type_path$old_widget_folder/$old_widget_file.$img_ext";
1963 $img_path = str_replace( '\\', '/', $img_path );
1964
1965 if ( file_exists( $img_path ) ) {
1966 $get_img = $img_path;
1967 $put_img = "$widget_folder_u_r_l.$img_ext";
1968
1969 if ( ! empty( $get_img ) && ! empty( $put_img ) ) {
1970 rename( $get_img, $put_img );
1971 }
1972 }
1973 }
1974 }
1975
1976 if ( ! empty( $d_image ) ) {
1977 $d_image = str_replace( '\\', '', $d_image );
1978 $d_img_url = $d_image;
1979 $img_body = wp_remote_get( $d_img_url );
1980 $img_ext = pathinfo( $d_img_url )['extension'];
1981 $wp_filesystem->put_contents( WDKIT_BUILDER_PATH . "/$widget_type/$folder_name/$file_name.$img_ext", $img_body['body'] );
1982 }
1983
1984 if ( ! empty( $function_call ) && 'import' !== $function_call && ! empty( $old_folder ) && strtolower( $old_folder ) !== strtolower( $folder_name ) && is_dir( $builder_type_path . $old_folder ) ) {
1985 require_once ABSPATH . 'wp-admin/includes/file.php';
1986 global $wp_filesystem;
1987 WP_Filesystem();
1988 $wp_filesystem->rmdir( $builder_type_path . $old_folder, true );
1989 } elseif ( $old_folder !== $folder_name ) {
1990 rename( WDKIT_BUILDER_PATH . "/$widget_type/$old_folder", WDKIT_BUILDER_PATH . "/$widget_type/$folder_name" );
1991 }
1992
1993 $responce = array(
1994 'message' => esc_html__( 'Update Saved Successfully', 'wdesignkit' ),
1995 'description' => esc_html__( 'Success! Update Saved', 'wdesignkit' ),
1996 'success' => true,
1997 );
1998
1999 wp_send_json( $responce );
2000 wp_die();
2001 }
2002
2003 /**
2004 *
2005 * It is Use for delete widget from server
2006 *
2007 * @since 1.0.0
2008 */
2009 protected function wdkit_import_widget() {
2010 $filename = '';
2011 if ( isset( $_FILES ) && ! empty( $_FILES ) && isset( $_FILES['zipName'] ) && ! empty( $_FILES['zipName'] ) ) {
2012 $filename = ! empty( $_FILES['zipName']['name'] ) ? sanitize_file_name( $_FILES['zipName']['name'] ) : '';
2013 }
2014
2015 $name = rtrim( $filename, '.zip' );
2016 $ext = WDKIT_BUILDER_PATH . '/elementor/dump/';
2017
2018 if ( ! is_dir( $ext ) ) {
2019 wp_mkdir_p( $ext );
2020 } else {
2021 require_once ABSPATH . 'wp-admin/includes/file.php';
2022 global $wp_filesystem;
2023 WP_Filesystem();
2024 $wp_filesystem->rmdir( $ext, true );
2025 }
2026
2027 $dir = WDKIT_BUILDER_PATH . '/elementor/dump';
2028 $getall_json = array();
2029 $zip = new ZipArchive();
2030
2031 $zipname = '';
2032 if ( ! empty( $_FILES['zipName']['tmp_name'] ) ) {
2033 $zipname = $this->wdkit_file_sanitizer_bypass( $_FILES, 'zipName', 'name' );
2034 }
2035
2036 $res = $zip->open( $zipname );
2037 if ( true === $res ) {
2038 $zip->extractTo( $ext );
2039 $zip->close();
2040 $widget_name = $image = $json_file = '';
2041 $list = scandir( $dir );
2042 $list = array_diff( $list, array( '.', '..' ) );
2043 foreach ( $list as $sub_dir_value ) {
2044 $file = new SplFileInfo( $sub_dir_value );
2045 $check_ext = $file->getExtension();
2046 $extiona = pathinfo( $sub_dir_value, PATHINFO_EXTENSION );
2047 if ( 'json' === $extiona ) {
2048 $json_file = $sub_dir_value;
2049 $u_r_l = wp_json_file_decode( $ext . $sub_dir_value );
2050 if ( ! empty( $u_r_l->widget_data->widgetdata->name ) && ! empty( $u_r_l->widget_data->widgetdata->widget_id ) ) {
2051 $widget_name = $u_r_l->widget_data->widgetdata->name;
2052 $widget_id = $u_r_l->widget_data->widgetdata->widget_id;
2053 $widget_type = ! empty( $u_r_l->widget_data->widgetdata->type ) ? $u_r_l->widget_data->widgetdata->type : '';
2054 }
2055 } elseif ( 'jpg' === $extiona || 'png' === $extiona || 'jpeg' === $extiona ) {
2056 $img_ext = $extiona;
2057 $image = $sub_dir_value;
2058 }
2059 }
2060
2061 if ( ! empty( $widget_name ) && ! empty( $json_file ) ) {
2062 $folder_name = str_replace( ' ', '-', $widget_name );
2063 $file_name = str_replace( ' ', '_', $widget_name );
2064 $file_path = WDKIT_BUILDER_PATH . "/{$widget_type}/{$folder_name}_{$widget_id}";
2065 $dummy_path = WDKIT_BUILDER_PATH . '/elementor/dump';
2066
2067 if ( is_dir( $dummy_path ) ) {
2068 if ( ! rename( $dummy_path, $file_path ) ) {
2069
2070 require_once ABSPATH . 'wp-admin/includes/file.php';
2071 global $wp_filesystem;
2072 WP_Filesystem();
2073 $wp_filesystem->rmdir( $dummy_path, true );
2074
2075 $responce = (object) array(
2076 'success' => fasle,
2077 'message' => esc_html__( 'Widget Not imported', 'wdesignkit' ),
2078 'description' => esc_html__( 'Widget alreday exist!', 'wdesignkit' ),
2079 );
2080
2081 wp_send_json( $responce );
2082 wp_die();
2083 }
2084 }
2085
2086 rename( "{$file_path}/{$json_file}", "{$file_path}/{$file_name}_{$widget_id}.json" );
2087
2088 $get_img_file = "{$file_path}/{$image}";
2089
2090 if ( file_exists( $get_img_file ) ) {
2091 rename( $get_img_file, "{$file_path}/{$file_name}_{$widget_id}.{$img_ext}" );
2092 }
2093 }
2094
2095 $responce = (object) array(
2096 'success' => true,
2097 'message' => esc_html__( 'Widget imported', 'wdesignkit' ),
2098 'description' => esc_html__( 'Widget imported successfully', 'wdesignkit' ),
2099 'json' => $u_r_l,
2100 );
2101
2102 wp_send_json( $responce );
2103 wp_die();
2104
2105 } else {
2106 $responce = (object) array(
2107 'success' => false,
2108 'message' => esc_html__( 'Operation Fial!', 'wdesignkit' ),
2109 'description' => esc_html__( 'Widget imported successfully', 'wdesignkit' ),
2110 );
2111
2112 wp_send_json( $responce );
2113 wp_die();
2114 }
2115 }
2116
2117 /**
2118 *
2119 * Create Uniq name
2120 *
2121 * @since 1.0.0
2122 */
2123 protected function generate_unique_id() {
2124 $now = new DateTime();
2125 $unique_i_d = $now->format( 'YmdHis' );
2126 $hashed_i_d = (int) $unique_i_d % 10000;
2127 return str_pad( $hashed_i_d, 4, '0', STR_PAD_LEFT );
2128 }
2129
2130 /**
2131 *
2132 * It is Use for delete widget from server
2133 *
2134 * @since 1.0.0
2135 */
2136 protected function wdkit_export_widget() {
2137 $data = isset( $_POST['info'] ) ? sanitize_text_field( wp_unslash( $_POST['info'] ) ) : '';
2138 $data = json_decode( stripslashes( $data ) );
2139
2140 $widget_name_temp = isset( $data->widget_name ) ? sanitize_text_field( $data->widget_name ) : '';
2141 $widget_type = isset( $data->widget_type ) ? sanitize_text_field( $data->widget_type ) : '';
2142
2143 $widget_name = str_replace( ' ', '_', $widget_name_temp );
2144 $folder = str_replace( ' ', '-', $widget_name_temp );
2145 $unique_version = $this->generate_unique_id();
2146
2147 if ( empty( $widget_type ) ) {
2148 $result = (object) array(
2149 'success' => false,
2150 'url' => '',
2151 'message' => esc_html__( 'Widget Type Fail', 'wdesignkit' ),
2152 'description' => esc_html__( 'Widget Type Not Exists', 'wdesignkit' ),
2153 );
2154
2155 wp_send_json( $result );
2156 wp_die();
2157 }
2158
2159 $downlod_path = WDKIT_BUILDER_PATH . "/{$widget_type}/";
2160 $new_path = "{$downlod_path}/{$folder}/{$widget_name}";
2161
2162 $download_url = WDKIT_SERVER_PATH . "/{$widget_type}/{$widget_name}.zip";
2163 $zip = new ZipArchive();
2164 $tmp_file = "{$downlod_path}{$widget_name}.zip";
2165
2166 $json_data = wp_json_file_decode( "$new_path.json" );
2167 $img_ext = $json_data->widget_data->widgetdata->img_ext;
2168
2169 if ( true === $zip->open( $tmp_file, ZipArchive::CREATE ) ) {
2170 $widget_wb = str_replace( '-', '_', $folder );
2171 $zip->addFile( "$new_path.json", "$widget_wb.json" );
2172 if ( ! empty( $img_ext ) ) {
2173 $zip->addFile( "$new_path.$img_ext", "$widget_wb.$img_ext" );
2174 }
2175 $zip->close();
2176
2177 $result = (object) array(
2178 'success' => true,
2179 'url' => $download_url,
2180 'message' => esc_html__( 'Widget Exported', 'wdesignkit' ),
2181 'description' => esc_html__( 'Widget Exported successfully', 'wdesignkit' ),
2182 );
2183
2184 wp_send_json( $result );
2185 wp_die();
2186 } else {
2187 $result = (object) array(
2188 'success' => false,
2189 'url' => '',
2190 'message' => esc_html__( 'Widget Exported Fail', 'wdesignkit' ),
2191 'description' => esc_html__( 'something went wrong! please try again later.', 'wdesignkit' ),
2192 );
2193
2194 wp_send_json( $result );
2195 wp_die();
2196 }
2197 }
2198
2199 /**
2200 *
2201 * It is Use for delete widget from server
2202 *
2203 * @since 1.0.0
2204 */
2205 protected function wdkit_delete_widget() {
2206 $data = isset( $_POST['info'] ) ? sanitize_text_field( wp_unslash( $_POST['info'] ) ) : '';
2207 $data = json_decode( stripslashes( $data ) );
2208
2209 $delete_type = isset( $data->delete_type ) ? sanitize_text_field( $data->delete_type ) : '';
2210
2211 if ( 'plugin_server' === $delete_type ) {
2212 $array_data = array(
2213 'token' => isset( $data->token ) ? sanitize_text_field( $data->token ) : '',
2214 'type' => isset( $data->type ) ? sanitize_text_field( $data->type ) : '',
2215 'w_unique' => isset( $data->w_unique ) ? sanitize_text_field( $data->w_unique ) : '',
2216 'id' => isset( $data->id ) ? sanitize_text_field( $data->id ) : '',
2217 );
2218
2219 $response = $this->wkit_api_call( $array_data, 'save_widget' );
2220 $success = ! empty( $response['success'] ) ? $response['success'] : false;
2221
2222 if ( empty( $success ) ) {
2223 $massage = ! empty( $response['massage'] ) ? $response['massage'] : esc_html__( 'server error', 'wdesignkit' );
2224
2225 $result = (object) array(
2226 'success' => false,
2227 'message' => esc_html__( 'Widget Not Deleted', 'wdesignkit' ),
2228 'description' => esc_html__( 'Widget Not Deleted', 'wdesignkit' ),
2229 );
2230
2231 wp_send_json( $result );
2232 wp_die();
2233 }
2234 }
2235
2236 $dir_name = isset( $data->name ) ? sanitize_text_field( $data->name ) : '';
2237 $widget_type = isset( $data->builder ) ? sanitize_text_field( $data->builder ) : '';
2238 $dir = WDKIT_BUILDER_PATH . "/{$widget_type}/{$dir_name}";
2239
2240 require_once ABSPATH . 'wp-admin/includes/file.php';
2241 global $wp_filesystem;
2242 WP_Filesystem();
2243 $wp_filesystem->rmdir( $dir, true );
2244
2245 if ( 'plugin_server' === $delete_type ) {
2246 wp_send_json( $response['data'] );
2247 wp_die();
2248 } else {
2249 $result = (object) array(
2250 'success' => true,
2251 'message' => esc_html__( 'widget deleted', 'wdesignkit' ),
2252 'description' => esc_html__( 'Widget deleted successfully', 'wdesignkit' ),
2253 );
2254
2255 wp_send_json( $result );
2256 wp_die();
2257 }
2258 }
2259
2260 /**
2261 *
2262 * It is Use for download widget from widget listing.
2263 *
2264 * @since 1.0.0
2265 */
2266 protected function wdkit_download_widget() {
2267 $data = ! empty( $_POST['widget_info'] ) ? $this->wdkit_sanitizer_bypass( $_POST, 'widget_info', 'none' ) : '';
2268 $data = json_decode( stripslashes( $data ) );
2269
2270 $array_data = array(
2271 'token' => isset( $data->token ) ? sanitize_text_field( $data->token ) : '',
2272 'type' => isset( $data->type ) ? sanitize_text_field( $data->type ) : '',
2273 'w_unique' => isset( $data->w_uniq ) ? sanitize_text_field( $data->w_uniq ) : '',
2274 );
2275
2276 $response = $this->wkit_api_call( $array_data, 'save_widget' );
2277 $success = ! empty( $response['success'] ) ? $response['success'] : false;
2278
2279 if ( empty( $success ) ) {
2280 $massage = ! empty( $response['massage'] ) ? $response['massage'] : esc_html__( 'server error', 'wdesignkit' );
2281 $result = (object) array(
2282 'success' => false,
2283 'message' => $massage,
2284 'description' => esc_html__( ' Widget not Downloaded', 'wdesignkit' ),
2285 );
2286
2287 wp_send_json( $result );
2288 wp_die();
2289 }
2290
2291 $response = json_decode( wp_json_encode( $response['data'] ), true );
2292
2293 if ( empty( $response ) || empty( $response['data'] ) ) {
2294 $message = ! empty( $response['message'] ) ? $response['message'] : 'No Response Found';
2295 $description = ! empty( $response['description'] ) ? $response['description'] : 'Widget not Downloaded';
2296
2297 $result = (object) array(
2298 'success' => false,
2299 'message' => esc_html( $message ),
2300 'description' => esc_html( $description ),
2301 );
2302
2303 wp_send_json( $result );
2304 wp_die();
2305 }
2306
2307 $img_url = ! empty( $response['data']['image'] ) ? $response['data']['image'] : '';
2308 $json_data = ! empty( $response['data']['json'] ) ? json_decode( $response['data']['json'] ) : '';
2309
2310 if ( empty( $img_url ) && empty( $json_data ) ) {
2311 $responce = (object) array(
2312 'success' => false,
2313 'message' => esc_html__( 'No Response Found', 'wdesignkit' ),
2314 'description' => esc_html__( 'Widget not Downloaded', 'wdesignkit' ),
2315 );
2316
2317 wp_send_json( $responce );
2318 wp_die();
2319 }
2320
2321 include_once ABSPATH . 'wp-admin/includes/file.php';
2322 \WP_Filesystem();
2323 global $wp_filesystem;
2324
2325 $title = ! empty( $json_data->widget_data->widgetdata->name ) ? sanitize_text_field( $json_data->widget_data->widgetdata->name ) : '';
2326 $builder = ! empty( $json_data->widget_data->widgetdata->type ) ? sanitize_text_field( $json_data->widget_data->widgetdata->type ) : '';
2327 $w_uniq = ! empty( $json_data->widget_data->widgetdata->widget_id ) ? sanitize_text_field( $json_data->widget_data->widgetdata->widget_id ) : '';
2328
2329 $folder_name = str_replace( ' ', '-', $title ) . '_' . $w_uniq;
2330 $file_name = str_replace( ' ', '_', $title ) . '_' . $w_uniq;
2331 $builder_type_path = WDKIT_BUILDER_PATH . "/{$builder}/";
2332
2333 if ( ! is_dir( $builder_type_path ) ) {
2334 wp_mkdir_p( $builder_type_path );
2335 }
2336
2337 if ( ! is_dir( $builder_type_path . $folder_name ) ) {
2338 wp_mkdir_p( $builder_type_path . $folder_name );
2339 }
2340
2341 if ( ! empty( $img_url ) ) {
2342 $img_body = wp_remote_get( $img_url );
2343 $img_ext = pathinfo( $img_url )['extension'];
2344
2345 $wp_filesystem->put_contents( WDKIT_BUILDER_PATH . "/$builder/$folder_name/$file_name.$img_ext", $img_body['body'] );
2346 $json_data->widget_data->widgetdata->w_image = WDKIT_SERVER_PATH . "/$builder/$folder_name/$file_name.$img_ext";
2347 }
2348
2349 $result = (object) array(
2350 'success' => false,
2351 'message' => ! empty( $response['message'] ) ? $response['message'] : esc_html__( 'no message', 'wdesignkit' ),
2352 'description' => '',
2353 'json' => wp_json_encode( $json_data ),
2354 );
2355
2356 wp_send_json( $result );
2357 wp_die();
2358 }
2359
2360 /**
2361 *
2362 * It is Use for download widget from browse page.
2363 *
2364 * @since 1.0.0
2365 */
2366 protected function wdkit_public_download_widget() {
2367 $data = ! empty( $_POST['widget_info'] ) ? $this->wdkit_sanitizer_bypass( $_POST, 'widget_info', 'none' ) : '';
2368 $data = json_decode( stripslashes( $data ) );
2369
2370 $array_data = array(
2371 'id' => isset( $data->w_uniq ) ? sanitize_text_field( $data->w_uniq ) : '',
2372 'u_id' => isset( $data->u_id ) ? sanitize_text_field( $data->u_id ) : '',
2373 'type' => isset( $data->d_type ) ? sanitize_text_field( $data->d_type ) : '',
2374 );
2375
2376 $response = $this->wkit_api_call( $array_data, 'widget/download' );
2377 $success = ! empty( $response['success'] ) ? $response['success'] : false;
2378
2379 if ( empty( $success ) ) {
2380 $massage = ! empty( $response['massage'] ) ? $response['massage'] : esc_html__( 'server error', 'wdesignkit' );
2381
2382 $result = (object) array(
2383 'success' => false,
2384 'message' => $massage,
2385 'description' => esc_html__( 'Widget not Downloaded', 'wdesignkit' ),
2386 );
2387
2388 wp_send_json( $result );
2389 wp_die();
2390 }
2391
2392 $response = json_decode( wp_json_encode( $response['data'] ), true );
2393 if ( ! empty( $response ) && ! empty( $response['data'] ) ) {
2394 $img_url = ! empty( $response['data']['image'] ) ? esc_url_raw( $response['data']['image'] ) : '';
2395 $json = ! empty( $response['data']['json'] ) ? wp_json_encode( $response['data']['json'] ) : '';
2396
2397 if ( ! empty( $json ) ) {
2398 include_once ABSPATH . 'wp-admin/includes/file.php';
2399 \WP_Filesystem();
2400 global $wp_filesystem;
2401
2402 $json_data = json_decode( $json );
2403 $json_data = json_decode( $json_data );
2404 $title = ! empty( $json_data->widget_data->widgetdata->name ) ? sanitize_text_field( $json_data->widget_data->widgetdata->name ) : '';
2405 $builder = ! empty( $json_data->widget_data->widgetdata->type ) ? sanitize_text_field( $json_data->widget_data->widgetdata->type ) : '';
2406 $widget_id = ! empty( $json_data->widget_data->widgetdata->widget_id ) ? sanitize_text_field( $json_data->widget_data->widgetdata->widget_id ) : '';
2407
2408 $folder_name = str_replace( ' ', '-', $title ) . '_' . $widget_id;
2409 $file_name = str_replace( ' ', '_', $title ) . '_' . $widget_id;
2410
2411 $builder_type_path = WDKIT_BUILDER_PATH . "/{$builder}/";
2412
2413 if ( ! is_dir( $builder_type_path . $folder_name ) ) {
2414 wp_mkdir_p( $builder_type_path . $folder_name );
2415 }
2416
2417 if ( ! empty( $img_url ) ) {
2418 $img_body = wp_remote_get( $img_url );
2419 $img_ext = pathinfo( $img_url )['extension'];
2420 $wp_filesystem->put_contents( WDKIT_BUILDER_PATH . "/$builder/$folder_name/$file_name.$img_ext", $img_body['body'] );
2421
2422 $json_data->widget_data->widgetdata->w_image = WDKIT_SERVER_PATH . "/$builder/$folder_name/$file_name.$img_ext";
2423 }
2424
2425 $response = (object) array(
2426 'message' => ! empty( $response['message'] ) ? $response['message'] : '',
2427 'description' => ! empty( $response['description'] ) ? $response['description'] : '',
2428 'success' => ! empty( $response['success'] ) ? $response['success'] : false,
2429 'r_id' => ! empty( $response['data']['rid'] ) ? $response['data']['rid'] : 0,
2430 'json' => wp_json_encode( $json_data ),
2431 );
2432 }
2433 }
2434
2435 wp_send_json( $response );
2436 wp_die();
2437 }
2438
2439 /**
2440 *
2441 * It is Use for sync widget to server
2442 *
2443 * @since 1.0.0
2444 */
2445 protected function wdkit_add_widget() {
2446 $data = ! empty( $_POST['widget_info'] ) ? $this->wdkit_sanitizer_bypass( $_POST, 'widget_info', 'none' ) : '';
2447 $data = json_decode( stripslashes( $data ) );
2448
2449 $title = isset( $data->title ) ? sanitize_text_field( $data->title ) : '';
2450 $builder = isset( $data->builder ) ? sanitize_text_field( $data->builder ) : '';
2451 $w_uniq = isset( $data->w_uniq ) ? sanitize_text_field( $data->w_uniq ) : '';
2452 $w_image = isset( $data->w_image ) ? esc_url_raw( $data->w_image ) : '';
2453
2454 if ( ! empty( $w_image ) ) {
2455 $w_image = str_replace( '\\', '', $w_image );
2456 $w_image = wp_remote_get( $w_image )['body'];
2457 }
2458
2459 $array_data = array(
2460 'token' => isset( $data->token ) ? sanitize_text_field( $data->token ) : '',
2461 'type' => isset( $data->type ) ? sanitize_text_field( $data->type ) : '',
2462 'title' => isset( $data->title ) ? sanitize_text_field( $data->title ) : '',
2463 'content' => isset( $data->content ) ? sanitize_text_field( $data->content ) : '',
2464 'builder' => isset( $data->builder ) ? sanitize_text_field( $data->builder ) : '',
2465 'w_data' => isset( $data->w_data ) ? $data->w_data : '',
2466 'w_unique' => isset( $data->w_uniq ) ? sanitize_text_field( $data->w_uniq ) : '',
2467 'w_image' => $w_image,
2468 'w_imgext' => isset( $data->w_imgext ) ? sanitize_text_field( $data->w_imgext ) : '',
2469 'w_version' => isset( $data->w_version ) ? $data->w_version : '',
2470 'w_updates' => ! empty( $data->w_updates ) ? serialize( $data->w_updates ) : serialize( array() ),
2471 'r_id' => isset( $data->r_id ) ? $data->r_id : 0,
2472 );
2473
2474 $response = $this->wkit_api_call( $array_data, 'save_widget' );
2475 $success = ! empty( $response['success'] ) ? $response['success'] : false;
2476
2477 if ( empty( $success ) ) {
2478 $massage = ! empty( $response['massage'] ) ? $response['massage'] : esc_html__( 'server error', 'wdesignkit' );
2479
2480 $result = (object) array(
2481 'success' => false,
2482 'message' => $massage,
2483 'description' => esc_html__( 'Widget Not Added', 'wdesignkit' ),
2484 );
2485
2486 wp_send_json( $result );
2487 wp_die();
2488 }
2489
2490 $res = ! empty( $response['data'] ) ? $response['data'] : array();
2491
2492 $response = json_decode( wp_json_encode( $res ), true );
2493 $img_url = ! empty( $response['data']['imgurl'] ) ? $response['data']['imgurl'] : '';
2494
2495 if ( ! empty( $img_url ) && 'error' !== $res ) {
2496 $img_body = wp_remote_get( $img_url );
2497 $img_ext = pathinfo( $img_url )['extension'];
2498 include_once ABSPATH . 'wp-admin/includes/file.php';
2499 \WP_Filesystem();
2500 global $wp_filesystem;
2501 $folder_name = str_replace( ' ', '-', $title ) . '_' . $w_uniq;
2502 $file_name = str_replace( ' ', '_', $title ) . '_' . $w_uniq;
2503 $file_path = WDKIT_BUILDER_PATH . "/$builder/$folder_name/$file_name";
2504
2505 $u_r_l = wp_json_file_decode( "$file_path.json" );
2506 $u_r_l->widget_data->widgetdata->w_image = WDKIT_SERVER_PATH . "/$builder/$folder_name/$file_name.$img_ext";
2507
2508 $wp_filesystem->put_contents( "$file_path.json", wp_json_encode( $u_r_l ) );
2509 $wp_filesystem->put_contents( "$file_path.$img_ext", $img_body['body'] );
2510 }
2511
2512 wp_send_json( $response );
2513 wp_die();
2514 }
2515
2516 /**
2517 *
2518 * It is Use for manage favourite widget
2519 *
2520 * @since 1.0.0
2521 */
2522 protected function wdkit_favourite_widget() {
2523 $data = isset( $_POST['widget_info'] ) ? json_decode( stripslashes( sanitize_text_field( wp_unslash( $_POST['widget_info'] ) ) ) ) : '';
2524 $array_data = array(
2525 'token' => isset( $data->token ) ? sanitize_text_field( $data->token ) : '',
2526 'type' => isset( $data->type ) ? sanitize_text_field( $data->type ) : '',
2527 'w_unique' => isset( $data->w_uniq ) ? sanitize_text_field( $data->w_uniq ) : '',
2528 );
2529
2530 $response = $this->wkit_api_call( $array_data, 'save_widget' );
2531 $success = ! empty( $response['success'] ) ? $response['success'] : false;
2532
2533 if ( empty( $success ) ) {
2534 $massage = ! empty( $response['massage'] ) ? $response['massage'] : esc_html__( 'server error', 'wdesignkit' );
2535
2536 $result = (object) array(
2537 'success' => false,
2538 'message' => $massage,
2539 'description' => '',
2540 );
2541
2542 wp_send_json( $result );
2543 wp_die();
2544 }
2545
2546 wp_send_json( $response );
2547 wp_die();
2548 }
2549
2550 /**
2551 * It is Used Setting Panel Defalut Data Get.
2552 *
2553 * @since 1.0.0
2554 */
2555 protected function wdkit_setting_panel() {
2556 $event = ! empty( $_POST['event'] ) ? sanitize_text_field( wp_unslash( $_POST['event'] ) ) : 'get';
2557
2558 if ( 'get' === $event ) {
2559 return self::wkit_get_settings_panel();
2560 } elseif ( 'set' === $event ) {
2561 $data = ! empty( $_POST['data'] ) ? stripslashes( sanitize_text_field( wp_unslash( $_POST['data'] ) ) ) : array();
2562
2563 $data = json_decode( $data, true );
2564
2565 update_option( 'wkit_settings_panel', $data );
2566 return self::wkit_get_settings_panel();
2567 } else {
2568 return false;
2569 }
2570 }
2571
2572 /**
2573 * Get Setting Panal Data
2574 *
2575 * @since 1.0.0
2576 */
2577 protected static function wkit_get_settings_panel() {
2578 $get_setting = get_option( 'wkit_settings_panel', false );
2579
2580 return array(
2581 'builder' => isset( $get_setting['builder'] ) ? $get_setting['builder'] : true,
2582 'template' => isset( $get_setting['template'] ) ? $get_setting['template'] : true,
2583 'gutenberg_builder' => isset( $get_setting['gutenberg_builder'] ) ? $get_setting['gutenberg_builder'] : true,
2584 'elementor_builder' => isset( $get_setting['elementor_builder'] ) ? $get_setting['elementor_builder'] : true,
2585 'bricks_builder' => isset( $get_setting['bricks_builder'] ) ? $get_setting['bricks_builder'] : false,
2586 'debugger_mode' => isset( $get_setting['debugger_mode'] ) ? $get_setting['debugger_mode'] : false,
2587 );
2588 }
2589
2590 /**
2591 *
2592 * Use for Add new licence key.
2593 *
2594 * @since 1.0.0
2595 */
2596 protected function wdkit_activate_licence() {
2597 $args = array(
2598 'token' => ! empty( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '',
2599 'licencekey' => ! empty( $_POST['licencekey'] ) ? sanitize_text_field( wp_unslash( $_POST['licencekey'] ) ) : '',
2600 'licencename' => ! empty( $_POST['licencename'] ) ? sanitize_text_field( wp_unslash( $_POST['licencename'] ) ) : '',
2601 );
2602
2603 $response = $this->wkit_api_call( $args, 'wkit_activate_key' );
2604
2605 wp_send_json( $response['data'] );
2606 wp_die();
2607 }
2608
2609 /**
2610 *
2611 * Use for Delete licence key.
2612 *
2613 * @since 1.0.0
2614 */
2615 protected function wdkit_delete_licence_key() {
2616 $token = ! empty( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '';
2617 $licencename = ! empty( $_POST['licencename'] ) ? sanitize_text_field( wp_unslash( $_POST['licencename'] ) ) : '';
2618
2619 $args = array(
2620 'token' => $token,
2621 'licencename' => $licencename,
2622 );
2623
2624 $response = $this->wkit_api_call( $args, 'licence_delete' );
2625
2626 wp_send_json( $response['data'] );
2627 wp_die();
2628 }
2629
2630 /**
2631 *
2632 * Use for Sync licence key.
2633 *
2634 * @since 1.0.0
2635 */
2636 protected function wdkit_sync_licence_key() {
2637 $token = ! empty( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '';
2638 $licencename = ! empty( $_POST['licencename'] ) ? sanitize_text_field( wp_unslash( $_POST['licencename'] ) ) : '';
2639
2640 $args = array(
2641 'token' => $token,
2642 'licencename' => $licencename,
2643 );
2644
2645 $response = $this->wkit_api_call( $args, 'licence_sync' );
2646
2647 wp_send_json( $response['data'] );
2648 wp_die();
2649 }
2650
2651 /**
2652 *
2653 * It is Use for logout.
2654 *
2655 * @since 1.0.0
2656 */
2657 protected function wdkit_logout() {
2658 $email = isset( $_POST['email'] ) ? strtolower( sanitize_email( wp_unslash( $_POST['email'] ) ) ) : false;
2659 $response = '';
2660
2661 if ( ! empty( $email ) ) {
2662 $token = $this->wdkit_login_user_token( $email );
2663 $args = array( 'token' => $token );
2664
2665 delete_transient( 'wdkit_auth_' . $email );
2666 $response = WDesignKit_Data_Query::get_data( 'logout', $args );
2667 }
2668
2669 wp_send_json( $response );
2670 wp_die();
2671 }
2672
2673 /**
2674 *
2675 * It is Use for get token of login user.
2676 *
2677 * @since 1.0.0
2678 *
2679 * @param string $email check user email.
2680 */
2681 protected function wdkit_login_user_token( $email = '' ) {
2682
2683 if ( ! empty( $email ) ) {
2684 $user_key = strstr( $email, '@', true );
2685 $get_login = get_transient( 'wdkit_auth_' . $user_key );
2686
2687 if ( ! empty( $get_login ) && ! empty( $get_login['token'] ) ) {
2688 return $get_login['token'];
2689 }
2690 }
2691
2692 return false;
2693 }
2694
2695 /**
2696 * Parse args $_POST
2697 *
2698 * @since 1.0.0
2699 *
2700 * @param string $data send all post data.
2701 * @param string $type store text data.
2702 * @param string $condition store text data.
2703 */
2704 protected function wdkit_sanitizer_bypass( $data, $type, $condition = 'none' ) {
2705
2706 if ( 'none' === $condition ) {
2707 return $data[ $type ];
2708 } elseif ( 'cr_widget' === $condition ) {
2709 return $data[ $type ];
2710 }
2711 }
2712
2713 /**
2714 * Parse args $_POST
2715 *
2716 * @since 1.0.0
2717 *
2718 * @param string $data send all post data.
2719 * @param string $type store text data.
2720 * @param string $condition store text data.
2721 */
2722 protected function wdkit_file_sanitizer_bypass( $data, $type, $condition = 'none' ) {
2723
2724 if ( 'name' === $condition ) {
2725 return wp_normalize_path( $data[ $type ]['tmp_name'] );
2726 }
2727 }
2728
2729 /**
2730 * Parse args $_POST
2731 *
2732 * @since 1.0.0
2733 *
2734 * @param string $data send all post data.
2735 */
2736 protected function wdkit_parse_args( $data = array() ) {
2737 if ( empty( $data ) ) {
2738 return array();
2739 }
2740
2741 $args = array();
2742 if ( isset( $data['email'] ) ) {
2743 $args['email'] = isset( $data['email'] ) ? sanitize_email( $data['email'] ) : '';
2744 }
2745
2746 if ( isset( $data['data'] ) ) {
2747 $args['data'] = isset( $data['data'] ) ? wp_unslash( $data['data'] ) : array();
2748 }
2749
2750 if ( isset( $data['plugins'] ) ) {
2751 $args['plugins'] = isset( $data['plugins'] ) ? wp_unslash( $data['plugins'] ) : array();
2752 }
2753
2754 if ( isset( $data['template_id'] ) ) {
2755 $args['template_id'] = isset( $data['template_id'] ) ? intval( strtolower( sanitize_text_field( $data['template_id'] ) ) ) : '';
2756 }
2757
2758 if ( isset( $data['builder'] ) ) {
2759 $args['builder'] = isset( $data['builder'] ) ? sanitize_text_field( $data['builder'] ) : '';
2760 }
2761
2762 if ( isset( $data['editor'] ) ) {
2763 $args['editor'] = isset( $data['editor'] ) ? sanitize_text_field( $data['editor'] ) : '';
2764 }
2765
2766 if ( isset( $data['type_upload'] ) ) {
2767 $args['type_upload'] = isset( $data['type_upload'] ) ? sanitize_text_field( $data['type_upload'] ) : '';
2768 }
2769
2770 if ( isset( $data['title'] ) ) {
2771 $args['title'] = isset( $data['title'] ) ? wp_strip_all_tags( $data['title'] ) : '';
2772 }
2773
2774 if ( isset( $data['template_type'] ) ) {
2775 $args['template_type'] = isset( $data['template_type'] ) ? sanitize_text_field( $data['template_type'] ) : '';
2776 }
2777
2778 if ( isset( $data['wstype'] ) ) {
2779 $args['wstype'] = isset( $data['wstype'] ) ? wp_strip_all_tags( $data['wstype'] ) : '';
2780 }
2781
2782 if ( isset( $data['wid'] ) ) {
2783 $args['wid'] = isset( $data['wid'] ) ? intval( strtolower( sanitize_text_field( $data['wid'] ) ) ) : '';
2784 }
2785
2786 if ( isset( $data['perpage'] ) ) {
2787 $args['perpage'] = isset( $data['perpage'] ) ? intval( strtolower( sanitize_text_field( $data['perpage'] ) ) ) : 12;
2788 }
2789
2790 if ( isset( $data['page'] ) ) {
2791 $args['page'] = isset( $data['page'] ) ? intval( strtolower( sanitize_text_field( $data['page'] ) ) ) : 1;
2792 }
2793
2794 if ( isset( $data['buildertype'] ) ) {
2795 $args['buildertype'] = isset( $data['buildertype'] ) ? sanitize_text_field( $data['buildertype'] ) : '';
2796 }
2797
2798 if ( isset( $data['search'] ) ) {
2799 $args['search'] = isset( $data['search'] ) ? sanitize_text_field( $data['search'] ) : '';
2800 }
2801
2802 if ( isset( $data['plugin'] ) ) {
2803 $args['plugin'] = isset( $data['plugin'] ) ? wp_unslash( $data['plugin'] ) : array();
2804 }
2805
2806 if ( isset( $data['tag'] ) ) {
2807 $args['tag'] = isset( $data['tag'] ) ? wp_unslash( $data['tag'] ) : array();
2808 }
2809
2810 if ( isset( $data['category'] ) ) {
2811 $args['category'] = isset( $data['category'] ) ? wp_unslash( $data['category'] ) : array();
2812 }
2813
2814 if ( isset( $data['free_pro'] ) ) {
2815 $args['free_pro'] = isset( $data['free_pro'] ) ? sanitize_text_field( wp_unslash( $data['free_pro'] ) ) : '';
2816 }
2817
2818 if ( isset( $data['wp_post_type'] ) ) {
2819 $args['wp_post_type'] = isset( $data['wp_post_type'] ) ? sanitize_text_field( wp_unslash( $data['wp_post_type'] ) ) : '';
2820 }
2821
2822 if ( isset( $data['favorite'] ) ) {
2823 $args['favorite'] = isset( $data['favorite'] ) ? sanitize_text_field( wp_unslash( $data['favorite'] ) ) : '';
2824 }
2825
2826 if ( isset( $data['content'] ) ) {
2827 $args['content'] = isset( $data['content'] ) ? wp_unslash( $data['content'] ) : '';
2828 }
2829
2830 return $args;
2831 }
2832 }
2833
2834 Wdkit_Api_Call::get_instance();
2835 }
2836