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

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