PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 1.2.0
JetFormBuilder — Dynamic Blocks Form Builder v1.2.0
3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 All 130 releases
jetformbuilder / includes / license / manager.php
manager.php
766 lines 19.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Jet_Form_Builder\License;
4
5 use Jet_Form_Builder\Plugin;
6
7 // If this file is called directly, abort.
8 if ( ! defined( 'WPINC' ) ) {
9 die;
10 }
11
12 /**
13 * Define Manager class
14 */
15 class Manager {
16
17 /**
18 * [$api_url description]
19 * @var string
20 */
21 public $api_url = 'https://account.jetformbuilder.com';
22
23 /**
24 * [$license_data_key description]
25 * @var string
26 */
27 public $license_data_key = 'jfb-license-data';
28
29 /**
30 * [$settings description]
31 * @var null
32 */
33 public $license_data = null;
34
35 /**
36 * [$user_plugins description]
37 * @var boolean
38 */
39 public $user_installed_plugins = false;
40
41 /**
42 * [$update_plugins description]
43 * @var boolean
44 */
45 public $update_plugins = false;
46
47 /**
48 * Proccesing subscribe form ajax
49 *
50 * @return void
51 */
52 public function license_action() {
53
54 $data = ( ! empty( $_POST['data'] ) ) ? $_POST['data'] : false;
55
56 if ( ! $data ) {
57 wp_send_json( [
58 'success' => false,
59 'message' => __( 'Server error. Please, try again later', 'jet-form-builder' ),
60 'data' => [],
61 ] );
62 }
63
64 $license_action = $data['action'];
65 $license_key = $data['license'];
66
67 if ( empty( $license_key ) ) {
68 $license_key = $this->get_license_key();
69 }
70
71 $responce_data = $this->license_action_query( $license_action, $license_key );
72
73 if ( ! $responce_data['success'] ) {
74 wp_send_json( [
75 'success' => false,
76 'message' => __( 'Server error. Please, try again later', 'jet-form-builder' ),
77 'data' => [],
78 ] );
79 }
80
81 set_site_transient( 'update_plugins', null );
82
83 switch ( $license_action ) {
84 case 'activate_license':
85 $this->add_license_data( $license_key, $responce_data );
86
87 $responce_data['license_key'] = $license_key;
88
89 wp_send_json( [
90 'success' => true,
91 'message' => __( 'The license has been successfully activated', 'jet-form-builder' ),
92 'data' => $responce_data,
93 ] );
94
95 break;
96
97 case 'deactivate_license':
98 $license_list = $this->get_license_data();
99
100 $license_list = array_filter( $license_list, function ( $license_data ) use ( $license_key ) {
101 return $license_data['license_key'] !== $license_key;
102 } );
103
104 update_option( $this->license_data_key, $license_list );
105
106 wp_send_json( [
107 'success' => true,
108 'message' => __( 'The license has been successfully deactivated', 'jet-form-builder' ),
109 'data' => $responce_data,
110 ] );
111
112 break;
113 }
114
115 wp_send_json( [
116 'success' => false,
117 'message' => __( 'Server error. Please, try again later', 'jet-form-builder' ),
118 'data' => [],
119 ] );
120 }
121
122 /**
123 * Remote request to updater API.
124 *
125 * @since 1.0.0
126 * @return array|bool
127 */
128 public function license_action_query( $action = '', $license = '' ) {
129
130 $query_url = add_query_arg(
131 array(
132 'edd_action' => $action,
133 'item_id' => 9,
134 'license' => $license,
135 'url' => urlencode( $this->get_site_url() ),
136 ),
137 $this->api_url
138 );
139
140 $response = wp_remote_get( $query_url, array(
141 'timeout' => 60,
142 ) );
143
144 if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != '200' ) {
145 return false;
146 }
147
148 return json_decode( $response['body'], true );
149 }
150
151 /**
152 * [get description]
153 * @param [type] $setting [description]
154 * @param boolean $default [description]
155 * @return [type] [description]
156 */
157 public function get_license_data() {
158
159 if ( null === $this->license_data ) {
160 $this->license_data = get_option( $this->license_data_key, [] );
161 }
162
163 return $this->license_data;
164 }
165
166 /**
167 * [set_license_data description]
168 * @param [type] $setting [description]
169 * @param boolean $value [description]
170 */
171 public function add_license_data( $license_key = false, $license_data = [] ) {
172
173 if ( ! $license_key ) {
174 return false;
175 }
176
177 $current_license_data = get_option( $this->license_data_key, [] );
178
179 $license_data['license_key'] = $license_key;
180
181 $current_license_data[] = $license_data;
182
183 update_option( $this->license_data_key, $current_license_data );
184 }
185
186 /**
187 * [get_site_url description]
188 * @return [type] [description]
189 */
190 public function get_site_url() {
191 $urlParts = parse_url( site_url( '/' ) );
192 $site_url = $urlParts['host'] . $urlParts['path'];
193 $site_url = preg_replace( '#^https?://#', '', rtrim( $site_url ) );
194 $site_url = str_replace( 'www.', '', $site_url );
195
196 return $site_url;
197 }
198
199 /**
200 * @param false $plugin_slug
201 *
202 * @return false|string
203 */
204 public function package_url( $plugin_slug = false ) {
205
206 $license_key = $this->get_license_key();
207
208 if ( ! $license_key ) {
209 return false;
210 }
211
212 $plugin_slug = explode('/', $plugin_slug )[0];
213
214 return add_query_arg(
215 array(
216 'ct_api_action' => 'get_plugin',
217 'license' => $license_key,
218 'slug' => $plugin_slug,
219 'url' => urlencode( $this->get_site_url() ),
220 ),
221 $this->api_url
222 );
223 }
224
225 /**
226 * [get_plugin_license_key description]
227 * @param boolean $setting [description]
228 * @param boolean $value [description]
229 * @return [type] [description]
230 */
231 public function get_license_key() {
232
233 $license_list = $this->get_license_data();
234
235 if ( ! empty( $license_list ) ) {
236
237 foreach ( $license_list as $key => $license_data ) {
238
239 if ( 'expired' === $license_data['license'] ) {
240 continue;
241 }
242
243 $is_expired = $this->license_expired_check( $license_data['expires'] );
244
245 if ( $is_expired ) {
246 $license_list[ $key ]['license'] = 'expired';
247
248 continue;
249 }
250
251 if ( 'valid' === $license_data['license'] ) {
252 return $license_data['license_key'];
253 }
254 }
255 }
256
257 return false;
258 }
259
260 /**
261 * [if_license_expire_check description]
262 * @param boolean $expire_date [description]
263 * @return [type] [description]
264 */
265 public function license_expired_check( $expire_date = false, $day_to_expire = 0 ) {
266
267 if ( '0000-00-00 00:00:00' === $expire_date
268 || '1000-01-01 00:00:00' === $expire_date
269 || 'lifetime' === $expire_date
270 ) {
271 return false;
272 }
273
274 $current_time = time();
275
276 $current_time = strtotime( sprintf( '+%s day', $day_to_expire ), $current_time );
277
278 $expire_time = strtotime( $expire_date );
279
280 if ( $current_time > $expire_time ) {
281 return true;
282 }
283
284 return false;
285 }
286
287 /**
288 * @return array
289 */
290 public function get_plugin_data_list() {
291
292 $jet_plugin_list = $this->get_jfb_remote_plugin_list();
293 $user_plugin_list = $this->get_user_plugins();
294
295 $plugins_list = [];
296
297 if ( ! empty( $jet_plugin_list ) ) {
298 foreach ( $jet_plugin_list as $key => $plugin_data ) {
299
300 $plugin_slug = $plugin_data['slug'];
301
302 if ( array_key_exists( $plugin_slug, $user_plugin_list ) ) {
303 $plugin_data = wp_parse_args( $plugin_data, $user_plugin_list[ $plugin_slug ] );
304 } else {
305 $plugin_data = wp_parse_args( $plugin_data, array(
306 'version' => $plugin_data['version'],
307 'currentVersion' => $plugin_data['version'],
308 'updateAvaliable' => false,
309 'isActivated' => false,
310 'isInstalled' => false,
311 ) );
312 }
313
314 $plugins_list[ $plugin_data['slug'] ] = $plugin_data;
315 }
316 }
317
318 return $plugins_list;
319 }
320
321 /**
322 * Remote request to updater API.
323 *
324 * @since 1.0.0
325 * @return array|bool
326 */
327 public function get_jfb_remote_plugin_list() {
328
329 $remote_jfb_addons_info = get_site_transient( 'jfb_remote_addons_list' );
330
331 if ( $remote_jfb_addons_info ) {
332 return $remote_jfb_addons_info;
333 }
334
335 $response = wp_remote_get( $this->api_url . '/wp-json/croco/v1/plugins/', array(
336 'timeout' => 30,
337 ) );
338
339 if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != '200' ) {
340 return false;
341 }
342
343 $response = json_decode( $response['body'], true );
344
345 if ( ! $response['success'] ) {
346 return false;
347 }
348
349 if ( ! isset( $response['plugins'] ) ) {
350 return false;
351 }
352
353 $jfb_remote_addons_list = $response['plugins'];
354
355 set_site_transient( 'jfb_remote_addons_list', $jfb_remote_addons_list, HOUR_IN_SECONDS * 24 );
356
357 return $jfb_remote_addons_list;
358 }
359
360 /**
361 * [get_plugin_data description]
362 * @return [type] [description]
363 */
364 public function get_user_plugins() {
365
366 if ( ! $this->user_installed_plugins ) {
367
368 if ( ! function_exists( 'get_plugins' ) ) {
369 require_once ABSPATH . 'wp-admin/includes/plugin.php';
370 }
371
372 $this->user_installed_plugins = get_plugins();
373 }
374
375 $plugin_list = array();
376
377 if ( $this->user_installed_plugins ) {
378
379 foreach ( $this->user_installed_plugins as $plugin_file => $plugin_data ) {
380
381 $current_version = $plugin_data['Version'];
382 $latest_version = $this->get_latest_version( $plugin_file );
383
384 $plugin_list[ $plugin_file ] = array(
385 'version' => $latest_version,
386 'currentVersion' => $current_version,
387 'updateAvaliable' => version_compare( $latest_version, $current_version, '>' ),
388 'isActivated' => is_plugin_active( $plugin_file ),
389 'isInstalled' => true,
390 );
391
392 }
393 }
394
395 return $plugin_list;
396 }
397
398 /**
399 * Get latest version for passed plugin
400 *
401 * @param [type] $remote_plugin_data [description]
402 * @return [type] [description]
403 */
404 public function get_latest_version( $plugin_file ) {
405
406 if ( ! $this->update_plugins ) {
407 $this->update_plugins = get_site_transient( 'update_plugins' );
408 }
409
410 $no_update = isset( $this->update_plugins->no_update ) ? $this->update_plugins->no_update : false;
411 $to_update = isset( $this->update_plugins->response ) ? $this->update_plugins->response : false;
412
413 if ( $to_update && ! empty( $to_update ) && array_key_exists( $plugin_file, $to_update ) ) {
414 $version = $to_update[ $plugin_file ]->new_version;
415 } elseif ( ! empty( $no_update ) && array_key_exists( $plugin_file, $no_update ) ) {
416 $version = $no_update[ $plugin_file ]->new_version;
417 } elseif ( array_key_exists( $plugin_file, $this->user_installed_plugins ) ) {
418 $version = $this->user_installed_plugins[ $plugin_file ]['Version'];
419 } else {
420 $version = '1.0.0';
421 }
422
423 return $version;
424 }
425
426 /**
427 * Plugin Action Handler
428 */
429 public function plugin_action() {
430
431 $data = ( ! empty( $_POST['data'] ) ) ? $_POST['data'] : false;
432
433 if ( ! $data ) {
434 wp_send_json( [
435 'success' => false,
436 'message' => __( 'Server error. Please, try again later', 'jet-form-builder' ),
437 'data' => [],
438 ] );
439 }
440
441 $action = $data['action'];
442 $plugin = $data['plugin'];
443
444 switch ( $action ) {
445
446 case 'activate':
447 $this->activate_plugin( $plugin );
448 break;
449
450 case 'deactivate':
451 $this->deactivate_plugin( $plugin );
452 break;
453
454 case 'install':
455 $this->install_plugin( $plugin );
456 break;
457
458 case 'update':
459 //$this->update_plugin( $plugin );
460 break;
461 }
462
463 wp_send_json( [
464 'success' => true,
465 'message' => __( 'Success', 'jet-form-builder' ),
466 'data' => [],
467 ] );
468 }
469
470 /**
471 * @param $plugin_file
472 *
473 * @return array
474 */
475 public function get_plugin_data( $plugin_file ) {
476
477 if ( ! $this->user_installed_plugins ) {
478
479 if ( ! function_exists( 'get_plugins' ) ) {
480 require_once ABSPATH . 'wp-admin/includes/plugin.php';
481 }
482
483 $this->user_installed_plugins = get_plugins();
484 }
485
486 $plugin_data = $this->user_installed_plugins[ $plugin_file ];
487
488 $current_version = $plugin_data['Version'];
489
490 $latest_version = $this->get_latest_version( $plugin_file );
491
492 return [
493 'version' => $latest_version,
494 'currentVersion' => $current_version,
495 'updateAvaliable' => version_compare( $latest_version, $current_version, '>' ),
496 'isActivated' => is_plugin_active( $plugin_file ),
497 'isInstalled' => true,
498 ];
499 }
500
501 /**
502 * @param $plugin_file
503 */
504 public function activate_plugin( $plugin_file ) {
505
506 $status = [];
507
508 if ( ! current_user_can( 'activate_plugins' ) ) {
509 wp_send_json( [
510 'success' => false,
511 'message' => __( 'Sorry, you are not allowed to install plugins on this site.', 'jet-form-builder' ),
512 'data' => [],
513 ] );
514 }
515
516 $activate = null;
517
518 if ( ! is_plugin_active( $plugin_file ) ) {
519 $activate = activate_plugin( $plugin_file );
520 }
521
522 if ( is_wp_error( $activate ) ) {
523 wp_send_json( [
524 'success' => false,
525 'message' => $activate->get_error_message(),
526 'data' => [],
527 ] );
528 }
529
530 wp_send_json( [
531 'success' => true,
532 'message' => __( 'The plugin has been activated', 'jet-form-builder' ),
533 'data' => $this->get_plugin_data( $plugin_file ),
534 ] );
535 }
536
537 /**
538 * @param $plugin_file
539 */
540 public function deactivate_plugin( $plugin_file ) {
541
542 $status = [];
543
544 if ( ! current_user_can( 'activate_plugins' ) ) {
545 wp_send_json( [
546 'success' => false,
547 'message' => __( 'Sorry, you are not allowed to install plugins on this site.', 'jet-form-builder' ),
548 'data' => [],
549 ] );
550 }
551
552 $deactivate_handler = null;
553
554 if ( is_plugin_active( $plugin_file ) ) {
555 $deactivate_handler = deactivate_plugins( $plugin_file );
556 }
557
558 if ( is_wp_error( $deactivate_handler ) ) {
559 wp_send_json( [
560 'success' => false,
561 'message' => $deactivate_handler->get_error_message(),
562 'data' => [],
563 ] );
564 }
565
566 wp_send_json( [
567 'success' => true,
568 'message' => __( 'The plugin has been deactivated', 'jet-form-builder' ),
569 'data' => $this->get_plugin_data( $plugin_file ),
570 ] );
571 }
572
573 /**
574 * @param $plugin_file
575 * @param false $plugin_url
576 */
577 public function install_plugin( $plugin_file, $plugin_url = false ) {
578
579 $status = [];
580
581 if ( ! current_user_can( 'install_plugins' ) ) {
582 wp_send_json( [
583 'success' => false,
584 'message' => __( 'Sorry, you are not allowed to install plugins on this site.', 'jet-form-builder' ),
585 'data' => [],
586 ] );
587 }
588
589 if ( ! $plugin_url ) {
590 $package = $this->package_url( $plugin_file );
591 } else {
592 $package = $plugin_url;
593 }
594
595 include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
596
597 $skin = new \WP_Ajax_Upgrader_Skin();
598 $upgrader = new \Plugin_Upgrader( $skin );
599 $result = $upgrader->install( $package );
600
601 if ( is_wp_error( $result ) ) {
602 $status['errorCode'] = $result->get_error_code();
603 $status['errorMessage'] = $result->get_error_message();
604
605 wp_send_json( [
606 'success' => false,
607 'message' => $result->get_error_message(),
608 'data' => [],
609 ] );
610 } elseif ( is_wp_error( $skin->result ) ) {
611 $status['errorCode'] = $skin->result->get_error_code();
612 $status['errorMessage'] = $skin->result->get_error_message();
613
614 wp_send_json( [
615 'success' => false,
616 'message' => $skin->result->get_error_message(),
617 'data' => [],
618 ] );
619 } elseif ( $skin->get_errors()->get_error_code() ) {
620 $status['errorMessage'] = $skin->get_error_messages();
621
622 wp_send_json( [
623 'success' => false,
624 'message' => $skin->get_error_messages(),
625 'data' => [],
626 ] );
627 } elseif ( is_null( $result ) ) {
628 global $wp_filesystem;
629
630 $status['errorMessage'] = 'Unable to connect to the filesystem. Please confirm your credentials.';
631
632 // Pass through the error from WP_Filesystem if one was raised.
633 if ( $wp_filesystem instanceof \WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
634 $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
635 }
636
637 wp_send_json( [
638 'success' => false,
639 'message' => $status['errorMessage'],
640 'data' => [],
641 ] );
642 }
643
644 wp_send_json( [
645 'success' => true,
646 'message' => __( 'The plugin has been Installed', 'jet-form-builder' ),
647 'data' => $this->get_plugin_data( $plugin_file ),
648 ] );
649 }
650
651 /**
652 * @param $plugin_file
653 */
654 public function update_plugin( $plugin_file ) {
655
656 $plugin = plugin_basename( sanitize_text_field( wp_unslash( $plugin_file ) ) );
657 $slug = dirname( $plugin );
658
659 $status = [
660 'update' => 'plugin',
661 'slug' => $slug,
662 'oldVersion' => '',
663 'newVersion' => '',
664 ];
665
666 if ( ! current_user_can( 'update_plugins' ) || 0 !== validate_file( $plugin ) ) {
667 wp_send_json( [
668 'success' => false,
669 'message' => __( 'Sorry, you are not allowed to update plugins on this site.', 'jet-form-builder' ),
670 'data' => [],
671 ] );
672 }
673
674 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
675
676 include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
677
678 wp_update_plugins();
679
680 $skin = new \WP_Ajax_Upgrader_Skin();
681 $upgrader = new \Plugin_Upgrader( $skin );
682 $result = $upgrader->bulk_upgrade( [ $plugin ] );
683
684 $upgrade_messages = [];
685
686 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
687 $upgrade_messages = $skin->get_upgrade_messages();
688 }
689
690 if ( is_wp_error( $skin->result ) ) {
691 wp_send_json( [
692 'success' => false,
693 'message' => $skin->result->get_error_message(),
694 'data' => [],
695 'debug' => $upgrade_messages,
696 ] );
697 } elseif ( $skin->get_errors()->get_error_code() ) {
698 wp_send_json( [
699 'success' => false,
700 'message' => $skin->get_error_message(),
701 'data' => [],
702 'debug' => $upgrade_messages,
703 ] );
704
705 } elseif ( is_array( $result ) && ! empty( $result[ $plugin ] ) ) {
706 $plugin_update_data = current( $result );
707
708 /*
709 * If the `update_plugins` site transient is empty (e.g. when you update
710 * two plugins in quick succession before the transient repopulates),
711 * this may be the return.
712 *
713 * Preferably something can be done to ensure `update_plugins` isn't empty.
714 * For now, surface some sort of error here.
715 */
716 if ( true === $plugin_update_data ) {
717 wp_send_json( [
718 'success' => false,
719 'message' => __( 'Plugin update failed.', 'jet-form-builder' ),
720 'data' => [],
721 'debug' => $upgrade_messages,
722 ] );
723 }
724
725 $plugin_data = get_plugins( '/' . $result[ $plugin ]['destination_name'] );
726 $plugin_data = reset( $plugin_data );
727
728 wp_send_json( [
729 'success' => true,
730 'message' => __( 'The plugin has been updated', 'jet-form-builder' ),
731 'data' => $this->get_plugin_data( $plugin_file ),
732 ] );
733
734 } elseif ( false === $result ) {
735 global $wp_filesystem;
736
737 $errorMessage = 'Unable to connect to the filesystem. Please confirm your credentials.';
738
739 // Pass through the error from WP_Filesystem if one was raised.
740 if ( $wp_filesystem instanceof \WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
741 $errorMessage = esc_html( $wp_filesystem->errors->get_error_message() );
742 }
743
744 wp_send_json( [
745 'success' => false,
746 'message' => $errorMessage,
747 'data' => [],
748 ] );
749 }
750
751 wp_send_json( [
752 'success' => false,
753 'message' => __( 'Plugin update failed.', 'jet-form-builder' ),
754 'data' => [],
755 ] );
756 }
757
758 /**
759 * Manager constructor.
760 */
761 public function __construct() {
762 add_action( 'wp_ajax_jfb_license_action', array( $this, 'license_action' ) );
763 add_action( 'wp_ajax_jet_fb_addon_action', array( $this, 'plugin_action' ) );
764 }
765 }
766