PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.2.6
JetFormBuilder — Dynamic Blocks Form Builder v1.2.6
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 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / license / manager.php
jetformbuilder / includes / license Last commit date
manager.php 4 years ago
manager.php
1109 lines
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 * [$jet_changelog_url description]
25 * @var string
26 */
27 public $jet_changelog_url = 'https://account.jetformbuilder.com/wp-content/uploads/jet-changelog/%s.json';
28
29 /**
30 * [$license_data_key description]
31 * @var string
32 */
33 public $license_data_key = 'jfb-license-data';
34
35 /**
36 * [$settings description]
37 * @var null
38 */
39 public $license_data = null;
40
41 /**
42 * [$user_plugins description]
43 * @var boolean
44 */
45 public $user_installed_plugins = false;
46
47 /**
48 * [$update_plugins description]
49 * @var boolean
50 */
51 public $update_plugins = false;
52
53 /**
54 * Proccesing subscribe form ajax
55 *
56 * @return void
57 */
58 public function license_action() {
59
60 $data = ( ! empty( $_POST['data'] ) ) ? $_POST['data'] : false;
61
62 if ( ! $data ) {
63 wp_send_json( [
64 'success' => false,
65 'message' => __( 'Server error. Please, try again later', 'jet-form-builder' ),
66 'data' => [],
67 ] );
68 }
69
70 $license_action = $data['action'];
71 $license_key = $data['license'];
72
73 if ( empty( $license_key ) ) {
74 $license_key = $this->get_license_key();
75 }
76
77 $responce_data = $this->license_action_query( $license_action, $license_key );
78
79 if ( ! $responce_data['success'] ) {
80
81 if ( 'invalid' === $responce_data['license'] ) {
82 wp_send_json( [
83 'success' => false,
84 'message' => __( 'Invalid license key', 'jet-form-builder' ),
85 'data' => [],
86 ] );
87 }
88
89 if ( 'failed' === $responce_data['license'] ) {
90
91 $license_list = $this->get_license_data();
92
93 $license_list = array_filter( $license_list, function ( $license_data ) use ( $license_key ) {
94 return $license_data['license_key'] !== $license_key;
95 } );
96
97 update_option( $this->license_data_key, $license_list );
98
99 wp_send_json( [
100 'success' => true,
101 'message' => __( 'The license for this site is already activated', 'jet-form-builder' ),
102 'data' => [],
103 ] );
104 }
105
106 wp_send_json( [
107 'success' => false,
108 'message' => __( 'Server error. Please, try again later', 'jet-form-builder' ),
109 'data' => [],
110 ] );
111 }
112
113 switch ( $license_action ) {
114 case 'activate_license':
115 $this->add_license_data( $license_key, $responce_data );
116
117 $responce_data['license_key'] = $license_key;
118
119 wp_send_json( [
120 'success' => true,
121 'message' => __( 'The license has been successfully activated', 'jet-form-builder' ),
122 'data' => $responce_data,
123 ] );
124
125 break;
126
127 case 'deactivate_license':
128 $license_list = $this->get_license_data();
129
130 $license_list = array_filter( $license_list, function ( $license_data ) use ( $license_key ) {
131 return $license_data['license_key'] !== $license_key;
132 } );
133
134 update_option( $this->license_data_key, $license_list );
135
136 wp_send_json( [
137 'success' => true,
138 'message' => __( 'The license has been successfully deactivated', 'jet-form-builder' ),
139 'data' => $responce_data,
140 ] );
141
142 break;
143 }
144
145 wp_send_json( [
146 'success' => false,
147 'message' => __( 'Server error. Please, try again later', 'jet-form-builder' ),
148 'data' => [],
149 ] );
150 }
151
152 /**
153 * Remote request to updater API.
154 *
155 * @since 1.0.0
156 * @return array|bool
157 */
158 public function license_action_query( $action = '', $license = '' ) {
159
160 $query_url = add_query_arg(
161 array(
162 'edd_action' => $action,
163 'item_id' => 9,
164 'license' => $license,
165 'url' => urlencode( $this->get_site_url() ),
166 ),
167 $this->api_url
168 );
169
170 $response = wp_remote_get( $query_url, array(
171 'timeout' => 60,
172 ) );
173
174 if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != '200' ) {
175 return false;
176 }
177
178 return json_decode( $response['body'], true );
179 }
180
181 /**
182 * [get description]
183 * @param [type] $setting [description]
184 * @param boolean $default [description]
185 * @return [type] [description]
186 */
187 public function get_license_data() {
188
189 if ( null === $this->license_data ) {
190 $this->license_data = get_option( $this->license_data_key, [] );
191 }
192
193 return $this->license_data;
194 }
195
196 /**
197 * [set_license_data description]
198 * @param [type] $setting [description]
199 * @param boolean $value [description]
200 */
201 public function add_license_data( $license_key = false, $license_data = [] ) {
202
203 if ( ! $license_key ) {
204 return false;
205 }
206
207 $current_license_data = get_option( $this->license_data_key, [] );
208
209 $license_data['license_key'] = $license_key;
210
211 $current_license_data[] = $license_data;
212
213 update_option( $this->license_data_key, $current_license_data );
214 }
215
216 /**
217 * [get_site_url description]
218 * @return [type] [description]
219 */
220 public function get_site_url() {
221 $urlParts = parse_url( site_url( '/' ) );
222 $site_url = $urlParts['host'] . $urlParts['path'];
223 $site_url = preg_replace( '#^https?://#', '', rtrim( $site_url ) );
224 $site_url = str_replace( 'www.', '', $site_url );
225
226 return $site_url;
227 }
228
229 /**
230 * @param false $plugin_slug
231 *
232 * @return false|string
233 */
234 public function package_url( $plugin_slug = false ) {
235
236 $license_key = $this->get_license_key();
237
238 if ( ! $license_key ) {
239 return false;
240 }
241
242 $plugin_slug = $this->get_addon_slug_by_filename( $plugin_slug );
243
244 return add_query_arg(
245 array(
246 'ct_api_action' => 'get_plugin',
247 'license' => $license_key,
248 'slug' => $plugin_slug,
249 'url' => urlencode( $this->get_site_url() ),
250 ),
251 $this->api_url
252 );
253 }
254
255 /**
256 * @param false $addon_filename
257 *
258 * @return string
259 */
260 public function get_addon_slug_by_filename( $addon_filename = false ) {
261 return explode('/', $addon_filename )[0];
262 }
263
264 /**
265 * [get_plugin_license_key description]
266 * @param boolean $setting [description]
267 * @param boolean $value [description]
268 * @return [type] [description]
269 */
270 public function get_license_key() {
271
272 $license_list = $this->get_license_data();
273
274 if ( ! empty( $license_list ) ) {
275
276 foreach ( $license_list as $key => $license_data ) {
277
278 if ( 'expired' === $license_data['license'] ) {
279 continue;
280 }
281
282 $is_expired = $this->license_expired_check( $license_data['expires'] );
283
284 if ( $is_expired ) {
285 $license_list[ $key ]['license'] = 'expired';
286
287 continue;
288 }
289
290 if ( 'valid' === $license_data['license'] ) {
291 return $license_data['license_key'];
292 }
293 }
294 }
295
296 return false;
297 }
298
299 /**
300 * [if_license_expire_check description]
301 * @param boolean $expire_date [description]
302 * @return [type] [description]
303 */
304 public function license_expired_check( $expire_date = false, $day_to_expire = 0 ) {
305
306 if ( '0000-00-00 00:00:00' === $expire_date
307 || '1000-01-01 00:00:00' === $expire_date
308 || 'lifetime' === $expire_date
309 ) {
310 return false;
311 }
312
313 $current_time = time();
314
315 $current_time = strtotime( sprintf( '+%s day', $day_to_expire ), $current_time );
316
317 $expire_time = strtotime( $expire_date );
318
319 if ( $current_time > $expire_time ) {
320 return true;
321 }
322
323 return false;
324 }
325
326 /**
327 * @return array
328 */
329 public function get_plugin_data_list() {
330
331 $jet_plugin_list = $this->get_jfb_remote_plugin_list();
332 $user_plugin_list = $this->get_user_plugins();
333
334 $plugins_list = [];
335
336 if ( ! empty( $jet_plugin_list ) ) {
337 foreach ( $jet_plugin_list as $key => $plugin_data ) {
338
339 $plugin_slug = $plugin_data['slug'];
340
341 if ( array_key_exists( $plugin_slug, $user_plugin_list ) ) {
342 $plugin_data = wp_parse_args( $plugin_data, $user_plugin_list[ $plugin_slug ] );
343 } else {
344 $plugin_data = wp_parse_args( $plugin_data, array(
345 'version' => $plugin_data['version'],
346 'currentVersion' => $plugin_data['version'],
347 'updateAvaliable' => false,
348 'isActivated' => false,
349 'isInstalled' => false,
350 ) );
351 }
352
353 $plugins_list[ $plugin_data['slug'] ] = $plugin_data;
354 }
355 }
356
357
358 return $plugins_list;
359 }
360
361 /**
362 * Remote request to updater API.
363 *
364 * @since 1.0.0
365 * @return array|bool
366 */
367 public function get_jfb_remote_plugin_list() {
368
369 $remote_jfb_addons_info = get_site_transient( 'jfb_remote_addons_list' );
370
371 if ( $remote_jfb_addons_info ) {
372 return $remote_jfb_addons_info;
373 }
374
375 $response = wp_remote_get( $this->api_url . '/wp-json/croco/v1/plugins/', array(
376 'timeout' => 30,
377 ) );
378
379 if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != '200' ) {
380 return false;
381 }
382
383 $response = json_decode( $response['body'], true );
384
385 if ( ! $response['success'] ) {
386 return false;
387 }
388
389 if ( ! isset( $response['plugins'] ) ) {
390 return false;
391 }
392
393 $jfb_remote_addons_list = $response['plugins'];
394
395 set_site_transient( 'jfb_remote_addons_list', $jfb_remote_addons_list, HOUR_IN_SECONDS * 24 );
396
397 return $jfb_remote_addons_list;
398 }
399
400 /**
401 * @param false $plugin_file
402 *
403 * @return false|mixed
404 */
405 public function get_jfb_remote_plugin_data( $plugin_file = false ) {
406
407 if ( ! $plugin_file ) {
408 return false;
409 }
410
411 $remote_plugin_list = $this->get_jfb_remote_plugin_list();
412
413 if ( $remote_plugin_list ) {
414 foreach ( $remote_plugin_list as $key => $plugin_data ) {
415 if ( $plugin_file === $plugin_data['slug'] ) {
416 return $plugin_data;
417 }
418 }
419 }
420
421 return false;
422 }
423
424 /**
425 * @return array[]|bool
426 */
427 public function get_user_installed_plugins() {
428
429 if ( ! $this->user_installed_plugins ) {
430
431 if ( ! function_exists( 'get_plugins' ) ) {
432 require_once ABSPATH . 'wp-admin/includes/plugin.php';
433 }
434
435 $this->user_installed_plugins = get_plugins();
436 }
437
438 return $this->user_installed_plugins;
439 }
440
441 /**
442 * [get_addon_data description]
443 * @return [type] [description]
444 */
445 public function get_user_plugins() {
446
447 $user_installed_plugins = $this->get_user_installed_plugins();
448
449 $plugin_list = [];
450
451 if ( $user_installed_plugins ) {
452
453 foreach ( $user_installed_plugins as $plugin_file => $plugin_data ) {
454
455 $current_version = $plugin_data['Version'];
456 $latest_version = $this->get_latest_version( $plugin_file );
457
458 $plugin_list[ $plugin_file ] = array(
459 'version' => $latest_version,
460 'currentVersion' => $current_version,
461 'updateAvaliable' => version_compare( $latest_version, $current_version, '>' ),
462 'isActivated' => is_plugin_active( $plugin_file ),
463 'isInstalled' => true,
464 );
465
466 }
467 }
468
469 return $plugin_list;
470 }
471
472 /**
473 * Get latest version for passed plugin
474 *
475 * @param [type] $remote_plugin_data [description]
476 * @return [type] [description]
477 */
478 public function get_latest_version( $plugin_file ) {
479
480 if ( ! $this->update_plugins ) {
481 $this->update_plugins = get_site_transient( 'update_plugins' );
482 }
483
484 $available_addons_data = $this->get_jfb_remote_plugin_list();
485
486 if ( ! empty( $available_addons_data ) && array_key_exists( $plugin_file, $this->user_installed_plugins ) ) {
487
488 foreach ( $available_addons_data as $key => $addon_info ) {
489
490 if ( $plugin_file === $addon_info['slug'] && version_compare( $this->user_installed_plugins[ $plugin_file ]['Version'], $addon_info['version'], '<' ) ) {
491 return $addon_info['version'];
492 }
493 }
494 }
495
496 $no_update = isset( $this->update_plugins->no_update ) ? $this->update_plugins->no_update : false;
497 $to_update = isset( $this->update_plugins->response ) ? $this->update_plugins->response : false;
498
499 if ( $to_update && ! empty( $to_update ) && array_key_exists( $plugin_file, $to_update ) ) {
500 return $to_update[ $plugin_file ]->new_version;
501 } elseif ( ! empty( $no_update ) && array_key_exists( $plugin_file, $no_update ) ) {
502 return $no_update[ $plugin_file ]->new_version;
503 } elseif ( array_key_exists( $plugin_file, $this->user_installed_plugins ) ) {
504 $current_version = $this->user_installed_plugins[ $plugin_file ]['Version'];
505
506 return $current_version;
507 } else {
508 return '1.0.0';
509 }
510
511 return false;
512 }
513
514 /**
515 * Plugin Action Handler
516 */
517 public function plugin_action() {
518
519 $data = ( ! empty( $_POST['data'] ) ) ? $_POST['data'] : false;
520
521 if ( ! $data ) {
522 wp_send_json( [
523 'success' => false,
524 'message' => __( 'Server error. Please, try again later', 'jet-form-builder' ),
525 'data' => [],
526 ] );
527 }
528
529 $action = $data['action'];
530 $plugin = $data['plugin'];
531
532 switch ( $action ) {
533
534 case 'activate':
535 $this->activate_plugin( $plugin );
536 break;
537
538 case 'deactivate':
539 $this->deactivate_plugin( $plugin );
540 break;
541
542 case 'install':
543 $this->install_plugin( $plugin );
544 break;
545
546 case 'update':
547 $this->update_plugin( $plugin );
548 break;
549 }
550
551 wp_send_json( [
552 'success' => true,
553 'message' => __( 'Success', 'jet-form-builder' ),
554 'data' => [],
555 ] );
556 }
557
558 /**
559 * [allow_unsafe_urls description]
560 * @param [type] $args [description]
561 * @return [type] [description]
562 */
563 public function allow_unsafe_urls( $args ) {
564
565 if ( isset( $_REQUEST['action'] ) && 'jfb_addon_action' === $_REQUEST['action'] ) {
566 $args['reject_unsafe_urls'] = false;
567 }
568
569 return $args;
570 }
571
572 /**
573 * @param $plugin_file
574 *
575 * @return array
576 */
577 public function get_addon_data( $plugin_file ) {
578
579 $user_installed_plugins = $this->get_user_installed_plugins();
580
581 if ( empty( $user_installed_plugins[ $plugin_file ] )) {
582 return false;
583 }
584
585 $plugin_data = $user_installed_plugins[ $plugin_file ];
586 $current_version = $plugin_data['Version'];
587 $latest_version = $this->get_latest_version( $plugin_file );
588
589 return [
590 'version' => $latest_version,
591 'currentVersion' => $current_version,
592 'updateAvaliable' => version_compare( $latest_version, $current_version, '>' ),
593 'isActivated' => is_plugin_active( $plugin_file ),
594 'isInstalled' => true,
595 ];
596 }
597
598 /**
599 * @param $plugin_file
600 */
601 public function activate_plugin( $plugin_file ) {
602
603 $status = [];
604
605 if ( ! current_user_can( 'activate_plugins' ) ) {
606 wp_send_json( [
607 'success' => false,
608 'message' => __( 'Sorry, you are not allowed to install plugins on this site.', 'jet-form-builder' ),
609 'data' => [],
610 ] );
611 }
612
613 $activate = null;
614
615 if ( ! is_plugin_active( $plugin_file ) ) {
616 $activate = activate_plugin( $plugin_file );
617 }
618
619 if ( is_wp_error( $activate ) ) {
620 wp_send_json( [
621 'success' => false,
622 'message' => $activate->get_error_message(),
623 'data' => [],
624 ] );
625 }
626
627 wp_send_json( [
628 'success' => true,
629 'message' => __( 'The addon has been activated', 'jet-form-builder' ),
630 'data' => $this->get_addon_data( $plugin_file ),
631 ] );
632 }
633
634 /**
635 * @param $plugin_file
636 */
637 public function deactivate_plugin( $plugin_file ) {
638
639 $status = [];
640
641 if ( ! current_user_can( 'activate_plugins' ) ) {
642 wp_send_json( [
643 'success' => false,
644 'message' => __( 'Sorry, you are not allowed to install plugins on this site.', 'jet-form-builder' ),
645 'data' => [],
646 ] );
647 }
648
649 $deactivate_handler = null;
650
651 if ( is_plugin_active( $plugin_file ) ) {
652 $deactivate_handler = deactivate_plugins( $plugin_file );
653 }
654
655 if ( is_wp_error( $deactivate_handler ) ) {
656 wp_send_json( [
657 'success' => false,
658 'message' => $deactivate_handler->get_error_message(),
659 'data' => [],
660 ] );
661 }
662
663 wp_send_json( [
664 'success' => true,
665 'message' => __( 'The addon has been deactivated', 'jet-form-builder' ),
666 'data' => $this->get_addon_data( $plugin_file ),
667 ] );
668 }
669
670 /**
671 * @param $plugin_file
672 * @param false $plugin_url
673 */
674 public function install_plugin( $plugin_file, $plugin_url = false ) {
675
676 $status = [];
677
678 if ( ! current_user_can( 'install_plugins' ) ) {
679 wp_send_json( [
680 'success' => false,
681 'message' => __( 'Sorry, you are not allowed to install plugins on this site.', 'jet-form-builder' ),
682 'data' => [],
683 ] );
684 }
685
686 if ( ! $plugin_url ) {
687 $package = $this->package_url( $plugin_file );
688 } else {
689 $package = $plugin_url;
690 }
691
692 include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
693
694 $skin = new \WP_Ajax_Upgrader_Skin();
695 $upgrader = new \Plugin_Upgrader( $skin );
696 $result = $upgrader->install( $package );
697
698 if ( is_wp_error( $result ) ) {
699 $status['errorCode'] = $result->get_error_code();
700 $status['errorMessage'] = $result->get_error_message();
701
702 wp_send_json( [
703 'success' => false,
704 'message' => $result->get_error_message(),
705 'data' => [],
706 ] );
707 } elseif ( is_wp_error( $skin->result ) ) {
708 $status['errorCode'] = $skin->result->get_error_code();
709 $status['errorMessage'] = $skin->result->get_error_message();
710
711 wp_send_json( [
712 'success' => false,
713 'message' => $skin->result->get_error_message(),
714 'data' => [],
715 ] );
716 } elseif ( $skin->get_errors()->get_error_code() ) {
717 $status['errorMessage'] = $skin->get_error_messages();
718
719 wp_send_json( [
720 'success' => false,
721 'message' => $skin->get_error_messages(),
722 'data' => [],
723 ] );
724 } elseif ( is_null( $result ) ) {
725 global $wp_filesystem;
726
727 $status['errorMessage'] = 'Unable to connect to the filesystem. Please confirm your credentials.';
728
729 // Pass through the error from WP_Filesystem if one was raised.
730 if ( $wp_filesystem instanceof \WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
731 $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
732 }
733
734 wp_send_json( [
735 'success' => false,
736 'message' => $status['errorMessage'],
737 'data' => [],
738 ] );
739 }
740
741 wp_send_json( [
742 'success' => true,
743 'message' => __( 'The addon has been Installed', 'jet-form-builder' ),
744 'data' => $this->get_addon_data( $plugin_file ),
745 ] );
746 }
747
748 /**
749 * @param $plugin_file
750 */
751 public function update_plugin( $plugin_file ) {
752
753 $plugin = plugin_basename( sanitize_text_field( wp_unslash( $plugin_file ) ) );
754 $slug = dirname( $plugin );
755
756 $status = [
757 'update' => 'plugin',
758 'slug' => $slug,
759 'oldVersion' => '',
760 'newVersion' => '',
761 ];
762
763 if ( ! current_user_can( 'update_plugins' ) || 0 !== validate_file( $plugin ) ) {
764 wp_send_json( [
765 'success' => false,
766 'message' => __( 'Sorry, you are not allowed to update plugins on this site.', 'jet-form-builder' ),
767 'data' => [],
768 ] );
769 }
770
771 include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
772
773 wp_update_plugins();
774
775 $skin = new \WP_Ajax_Upgrader_Skin();
776 $upgrader = new \Plugin_Upgrader( $skin );
777 $result = $upgrader->bulk_upgrade( [ $plugin ] );
778
779 $upgrade_messages = [];
780
781 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
782 $upgrade_messages = $skin->get_upgrade_messages();
783 }
784
785 if ( is_wp_error( $skin->result ) ) {
786 wp_send_json( [
787 'success' => false,
788 'message' => $skin->result->get_error_message(),
789 'data' => [],
790 'debug' => $upgrade_messages,
791 ] );
792 } elseif ( $skin->get_errors()->get_error_code() ) {
793 wp_send_json( [
794 'success' => false,
795 'message' => $skin->get_error_message(),
796 'data' => [],
797 'debug' => $upgrade_messages,
798 ] );
799
800 } elseif ( is_array( $result ) && ! empty( $result[ $plugin ] ) ) {
801 $plugin_update_data = current( $result );
802
803 /*
804 * If the `update_plugins` site transient is empty (e.g. when you update
805 * two plugins in quick succession before the transient repopulates),
806 * this may be the return.
807 *
808 * Preferably something can be done to ensure `update_plugins` isn't empty.
809 * For now, surface some sort of error here.
810 */
811 if ( true === $plugin_update_data ) {
812 wp_send_json( [
813 'success' => false,
814 'message' => __( 'Addon update failed.', 'jet-form-builder' ),
815 'data' => [],
816 'debug' => $upgrade_messages,
817 ] );
818 }
819
820 wp_send_json( [
821 'success' => true,
822 'message' => __( 'The addon has been updated', 'jet-form-builder' ),
823 'data' => $this->get_addon_data( $plugin_file ),
824 ] );
825
826 } elseif ( false === $result ) {
827 global $wp_filesystem;
828
829 $errorMessage = 'Unable to connect to the filesystem. Please confirm your credentials.';
830
831 // Pass through the error from WP_Filesystem if one was raised.
832 if ( $wp_filesystem instanceof \WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
833 $errorMessage = esc_html( $wp_filesystem->errors->get_error_message() );
834 }
835
836 wp_send_json( [
837 'success' => false,
838 'message' => $errorMessage,
839 'data' => [],
840 ] );
841 }
842
843 wp_send_json( [
844 'success' => false,
845 'message' => __( 'Plugin update failed.', 'jet-form-builder' ),
846 'data' => [],
847 ] );
848 }
849
850 /**
851 * License service action
852 */
853 public function license_service_action() {
854
855 $data = ( ! empty( $_POST['data'] ) ) ? $_POST['data'] : false;
856
857 if ( ! $data || ! isset( $data['action'] ) ) {
858 wp_send_json( [
859 'success' => false,
860 'message' => __( 'Server error. Please, try again later', 'jet-form-builder' ),
861 'data' => [],
862 ] );
863 }
864
865 $action = $data['action'];
866
867 switch ( $action ) {
868
869 case 'check-plugin-update':
870 //set_site_transient( 'update_plugins', null );
871 wp_clean_update_cache();
872
873 wp_send_json( [
874 'success' => true,
875 'message' => __( 'Addons Update Checked', 'jet-form-builder' ),
876 'data' => [],
877 ] );
878
879 break;
880
881 default:
882 wp_send_json( [
883 'success' => false,
884 'message' => __( 'Service action Not Found', 'jet-form-builder' ),
885 'data' => [],
886 ] );
887 break;
888 }
889
890 exit;
891 }
892
893 /**
894 * @param $data
895 *
896 * @return mixed
897 */
898 public function check_addons_update( $data ) {
899
900 delete_site_transient( 'jfb_remote_addons_list' );
901
902 $available_addons_data = $this->get_jfb_remote_plugin_list();
903
904 foreach ( $available_addons_data as $key => $addon_info ) {
905
906 $addon_slug = $addon_info['slug'];
907 $installed_user_plugins = $this->get_user_plugins();
908
909 if ( ! isset( $installed_user_plugins[ $addon_slug ] ) ) {
910 continue;
911 }
912
913 $current_version = $installed_user_plugins[ $addon_slug ]['currentVersion'];
914
915 if ( version_compare( $current_version, $addon_info['version'], '<' ) ) {
916
917 $plugin_file = $addon_info['slug'];
918 $plugin_slug = $this->get_addon_slug_by_filename( $plugin_file );
919
920 delete_site_transient( $plugin_slug . '_addon_info_data' );
921
922 $update = new \stdClass();
923 $update->slug = $plugin_slug;
924 $update->plugin = $plugin_file;
925 $update->new_version = true;
926 $update->url = false;
927 $update->package = $this->package_url( $plugin_file );
928
929 $data->response[ $plugin_file ] = $update;
930 }
931 }
932
933 return $data;
934 }
935
936 /**
937 * @param $plugin_meta
938 * @param $plugin_file
939 * @param $plugin_data
940 *
941 * @return mixed
942 */
943 public function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data ) {
944
945 $available_addons_data = $this->get_jfb_remote_plugin_list();
946
947 $is_jfb_addon = false;
948 $plugin_data = false;
949
950 foreach ( $available_addons_data as $key => $addon_data ) {
951
952 if ( $plugin_file === $addon_data['slug'] ) {
953 $is_jfb_addon = true;
954 $plugin_data = $addon_data;
955 }
956 }
957
958 if ( $is_jfb_addon && empty( $plugin_data['update'] ) ) {
959
960 $plugin_slug = $this->get_addon_slug_by_filename( $plugin_data['slug'] );
961
962 $plugin_meta['view-details'] = sprintf( '<a href="%s" class="thickbox open-plugin-details-modal" aria-label="%s" data-title="%s">%s</a>',
963 esc_url( network_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $plugin_slug . '&TB_iframe=true&width=600&height=550' ) ),
964 esc_attr( sprintf( __( 'More information about %s', 'jet-form-builder' ), $plugin_data['name'] ) ),
965 esc_attr( $plugin_data['name'] ),
966 __( 'View details', 'jet-form-builder' )
967 );
968 }
969
970 return $plugin_meta;
971 }
972
973 /**
974 * @param $_data
975 * @param string $_action
976 * @param null $_args
977 *
978 * @return mixed
979 */
980 public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
981
982 if ( 'plugin_information' !== $_action ) {
983 return $_data;
984 }
985
986 if ( ! isset( $_args->slug ) ) {
987 return $_data;
988 }
989
990 $available_addons_data = $this->get_jfb_remote_plugin_list();
991 $user_installed_plugins = $this->get_user_installed_plugins();
992
993 $registered_plugin_data = false;
994
995 foreach ( $available_addons_data as $key => $addon_data ) {
996
997 $addon_slug = $this->get_addon_slug_by_filename( $addon_data['slug'] );
998
999 if ( $addon_slug === $_args->slug ) {
1000 $registered_plugin_data = $addon_data;
1001 $registered_plugin_data['plugin_slug'] = $addon_slug;
1002 $registered_plugin_data['transient_key'] = $addon_slug . '_addon_info_data';
1003 $registered_plugin_data['banners'] = [
1004 'high' => sprintf( 'https://account.jetformbuilder.com/free-download/images/addon-banners/%s.png', $addon_slug ),
1005 'low' => sprintf( 'https://account.jetformbuilder.com/free-download/images/addon-banners/%s.png', $addon_slug ),
1006 ];
1007
1008 if ( ! empty( $user_installed_plugins[ $addon_data['slug'] ] ) ) {
1009 $installed_plugin_data = $user_installed_plugins[ $addon_data['slug'] ];
1010
1011 $registered_plugin_data['author'] = $installed_plugin_data['Author'];
1012 $registered_plugin_data['plugin_url'] = $installed_plugin_data['PluginURI'];
1013 }
1014
1015 break;
1016 }
1017 }
1018
1019 if ( ! $registered_plugin_data ) {
1020 return $_data;
1021 }
1022
1023 $addon_api_data = get_site_transient( $registered_plugin_data['transient_key'] );
1024
1025 if ( empty( $addon_api_data ) ) {
1026 $changelog_remote_response = $this->changelog_remote_query( $registered_plugin_data['plugin_slug'] );
1027
1028 if ( ! $changelog_remote_response ) {
1029 return $_data;
1030 }
1031
1032 $plugin_api_data = new \stdClass();
1033
1034 $plugin_api_data->name = $registered_plugin_data['name'];
1035 $plugin_api_data->slug = $registered_plugin_data['slug'];
1036 $plugin_api_data->author = $registered_plugin_data['author'];
1037 $plugin_api_data->homepage = $registered_plugin_data['plugin_url'];
1038 $plugin_api_data->requires = $registered_plugin_data['requires'];
1039 $plugin_api_data->tested = $registered_plugin_data['tested'];
1040 $plugin_api_data->banners = $registered_plugin_data['banners'];
1041 $plugin_api_data->version = $changelog_remote_response->current_version;
1042 $plugin_api_data->sections = [
1043 'changelog' => $changelog_remote_response->changelog,
1044 ];
1045
1046 // Expires in 1 day
1047 set_site_transient( $registered_plugin_data['transient_key'], $plugin_api_data, DAY_IN_SECONDS );
1048 }
1049
1050 $_data = $addon_api_data;
1051
1052 return $_data;
1053 }
1054
1055 /**
1056 * @param $slug
1057 *
1058 * @return false|mixed
1059 */
1060 public function changelog_remote_query( $slug ) {
1061
1062 $response = wp_remote_get( sprintf( $this->jet_changelog_url, $slug ) );
1063
1064 if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != '200' ) {
1065 return false;
1066 }
1067
1068 $response = json_decode( $response['body'] );
1069
1070 return $response;
1071 }
1072
1073 /**
1074 * [get_theme_info description]
1075 * @return [type] [description]
1076 */
1077 public function get_theme_info() {
1078 $style_parent_theme = wp_get_theme( get_template() );
1079
1080 return apply_filters( 'jfb-addons-page/theme-info', array(
1081 'name' => $style_parent_theme->get('Name'),
1082 'theme' => strtolower( preg_replace('/\s+/', '', $style_parent_theme->get('Name') ) ),
1083 'version' => $style_parent_theme->get('Version'),
1084 'author' => $style_parent_theme->get('Author'),
1085 'authorSlug' => strtolower( preg_replace('/\s+/', '', $style_parent_theme->get('Author') ) ),
1086 ) );
1087 }
1088
1089 /**
1090 * Manager constructor.
1091 */
1092 public function __construct() {
1093 add_action( 'wp_ajax_jfb_license_action', array( $this, 'license_action' ) );
1094
1095 add_action( 'wp_ajax_jfb_license_service_action', array( $this, 'license_service_action' ) );
1096
1097 add_action( 'wp_ajax_jfb_addon_action', array( $this, 'plugin_action' ) );
1098
1099 add_filter( 'http_request_args', array( $this, 'allow_unsafe_urls' ) );
1100
1101 add_action( 'pre_set_site_transient_update_plugins', array( $this, 'check_addons_update' ) );
1102
1103 add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 3 );
1104
1105 add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
1106
1107 }
1108 }
1109