PluginProbe
WDesignKit – AI Templates, Widget Builder & MCP Workflow / 1.0.16
WDesignKit – AI Templates, Widget Builder & MCP Workflow v1.0.16
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.16, at includes/admin/class-api.php

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