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

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

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