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

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

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