PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.0.7
JetFormBuilder — Dynamic Blocks Form Builder v3.0.7
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 / addons / manager.php
jetformbuilder / includes / addons Last commit date
manager.php 3 years ago
manager.php
658 lines
1 <?php
2 namespace Jet_Form_Builder\Addons;
3
4 use Jet_Form_Builder\Classes\Tools;
5
6 /**
7 * This class required to get actual JetFormBuilder addons list and changelog for these addons from account.jetformbuilder.com.
8 * The data retrieved from the account.jetformbuilder.com contains only information about addons and required to show Addons admin page.
9 * This class don't send any sensetive data from client website to account.jetformbuilder.com, just technical information required webservers to communicate between each other
10 * like IP or server URI, there is no user personal data send with this requests
11 */
12
13 // If this file is called directly, abort.
14 if ( ! defined( 'WPINC' ) ) {
15 die;
16 }
17
18 class Manager {
19
20 const NOT_ACTIVE = 'license-not-activated';
21
22 /**
23 * [$api_url description]
24 *
25 * @var string
26 */
27 public $api_url = 'https://account.jetformbuilder.com';
28
29 /**
30 * [$jet_changelog_url description]
31 *
32 * @var string
33 */
34 public $jet_changelog_url = 'https://account.jetformbuilder.com/wp-content/uploads/jet-changelog/%s.json';
35
36 /**
37 * [$user_plugins description]
38 *
39 * @var boolean
40 */
41 public $user_installed_plugins = false;
42
43 /**
44 * [$update_plugins description]
45 *
46 * @var boolean
47 */
48 public $update_plugins = false;
49
50 private $addons_info;
51
52 /**
53 * @param false $addon_filename
54 *
55 * @return string
56 */
57 public function get_addon_slug_by_filename( $addon_filename = false ) {
58 return explode( '/', $addon_filename )[0];
59 }
60
61 /**
62 * @return array
63 */
64 public function get_plugin_data_list() {
65
66 $jet_plugin_list = $this->get_jfb_remote_plugin_list();
67 $user_plugin_list = $this->get_user_plugins();
68
69 $plugins_list = array();
70
71 if ( ! empty( $jet_plugin_list ) ) {
72 foreach ( $jet_plugin_list as $key => $plugin_data ) {
73
74 $plugin_slug = $plugin_data['slug'];
75
76 if ( array_key_exists( $plugin_slug, $user_plugin_list ) ) {
77 $plugin_data = wp_parse_args( $plugin_data, $user_plugin_list[ $plugin_slug ] );
78 } else {
79 $plugin_data = wp_parse_args(
80 $plugin_data,
81 array(
82 'version' => $plugin_data['version'],
83 'currentVersion' => $plugin_data['version'],
84 'updateAvaliable' => false,
85 'isActivated' => false,
86 'isInstalled' => false,
87 )
88 );
89 }
90
91 $plugins_list[ $plugin_data['slug'] ] = $plugin_data;
92 }
93 }
94
95 return $plugins_list;
96 }
97
98 /**
99 * Remote request to updater API.
100 *
101 * @since 1.0.0
102 * @return array|bool
103 */
104 public function get_jfb_remote_plugin_list() {
105
106 if ( ! is_null( $this->addons_info ) ) {
107 return $this->addons_info;
108 }
109
110 $remote_jfb_addons_info = get_site_transient( 'jfb_remote_addons_list' );
111
112 if ( $remote_jfb_addons_info ) {
113 $this->addons_info = $remote_jfb_addons_info;
114
115 return $remote_jfb_addons_info;
116 }
117
118 $response = wp_remote_get(
119 $this->api_url . '/wp-json/croco/v1/plugins/',
120 array(
121 'timeout' => 30,
122 )
123 );
124
125 if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != '200' ) {
126 return false;
127 }
128
129 $response = json_decode( $response['body'], true );
130
131 if ( ! $response['success'] ) {
132 return false;
133 }
134
135 if ( ! isset( $response['plugins'] ) ) {
136 return false;
137 }
138
139 $jfb_remote_addons_list = $response['plugins'];
140
141 set_site_transient( 'jfb_remote_addons_list', $jfb_remote_addons_list, HOUR_IN_SECONDS * 24 );
142
143 return $jfb_remote_addons_list;
144 }
145
146 /**
147 * @param false $plugin_file
148 *
149 * @return false|mixed
150 */
151 public function get_jfb_remote_plugin_data( $plugin_file = false ) {
152
153 if ( ! $plugin_file ) {
154 return false;
155 }
156
157 $remote_plugin_list = $this->get_jfb_remote_plugin_list();
158
159 if ( $remote_plugin_list ) {
160 foreach ( $remote_plugin_list as $key => $plugin_data ) {
161 if ( $plugin_file === $plugin_data['slug'] ) {
162 return $plugin_data;
163 }
164 }
165 }
166
167 return false;
168 }
169
170 /**
171 * @return array[]|bool
172 */
173 public function get_user_installed_plugins() {
174
175 if ( ! $this->user_installed_plugins ) {
176
177 if ( ! function_exists( 'get_plugins' ) ) {
178 require_once ABSPATH . 'wp-admin/includes/plugin.php';
179 }
180
181 $this->user_installed_plugins = get_plugins();
182 }
183
184 return $this->user_installed_plugins;
185 }
186
187 /**
188 * [get_addon_data description]
189 *
190 * @return [type] [description]
191 */
192 public function get_user_plugins() {
193
194 $user_installed_plugins = $this->get_user_installed_plugins();
195
196 $plugin_list = array();
197
198 if ( $user_installed_plugins ) {
199
200 foreach ( $user_installed_plugins as $plugin_file => $plugin_data ) {
201
202 $current_version = $plugin_data['Version'];
203 $latest_version = $this->get_latest_version( $plugin_file );
204
205 $plugin_list[ $plugin_file ] = array(
206 'version' => $latest_version,
207 'currentVersion' => $current_version,
208 'updateAvaliable' => version_compare( $latest_version, $current_version, '>' ),
209 'isActivated' => is_plugin_active( $plugin_file ),
210 'isInstalled' => true,
211 );
212
213 }
214 }
215
216 return $plugin_list;
217 }
218
219 /**
220 * Get latest version for passed plugin
221 *
222 * @param [type] $remote_plugin_data [description]
223 * @return [type] [description]
224 */
225 public function get_latest_version( $plugin_file ) {
226
227 if ( ! $this->update_plugins ) {
228 $this->update_plugins = get_site_transient( 'update_plugins' );
229 }
230
231 $available_addons_data = $this->get_jfb_remote_plugin_list();
232
233 if ( ! empty( $available_addons_data ) && array_key_exists( $plugin_file, $this->user_installed_plugins ) ) {
234
235 foreach ( $available_addons_data as $key => $addon_info ) {
236
237 if ( $plugin_file === $addon_info['slug'] && version_compare( $this->user_installed_plugins[ $plugin_file ]['Version'], $addon_info['version'], '<' ) ) {
238 return $addon_info['version'];
239 }
240 }
241 }
242
243 $no_update = isset( $this->update_plugins->no_update ) ? $this->update_plugins->no_update : false;
244 $to_update = isset( $this->update_plugins->response ) ? $this->update_plugins->response : false;
245
246 if ( $to_update && ! empty( $to_update ) && array_key_exists( $plugin_file, $to_update ) ) {
247 return $to_update[ $plugin_file ]->new_version;
248 } elseif ( ! empty( $no_update ) && array_key_exists( $plugin_file, $no_update ) ) {
249 return $no_update[ $plugin_file ]->new_version;
250 } elseif ( array_key_exists( $plugin_file, $this->user_installed_plugins ) ) {
251 $current_version = $this->user_installed_plugins[ $plugin_file ]['Version'];
252
253 return $current_version;
254 } else {
255 return '1.0.0';
256 }
257
258 return false;
259 }
260
261 /**
262 * @param $plugin_file
263 *
264 * @return array
265 */
266 public function get_addon_data( $plugin_file ) {
267
268 $user_installed_plugins = $this->get_user_installed_plugins();
269
270 if ( empty( $user_installed_plugins[ $plugin_file ] ) ) {
271 return false;
272 }
273
274 $plugin_data = $user_installed_plugins[ $plugin_file ];
275 $current_version = $plugin_data['Version'];
276 $latest_version = $this->get_latest_version( $plugin_file );
277
278 return array(
279 'version' => $latest_version,
280 'currentVersion' => $current_version,
281 'updateAvaliable' => version_compare( $latest_version, $current_version, '>' ),
282 'isActivated' => is_plugin_active( $plugin_file ),
283 'isInstalled' => true,
284 );
285 }
286
287 /**
288 * Plugin Action Handler
289 */
290 public function addon_activate_action() {
291
292 $data = ( ! empty( $_POST['data'] ) ) ? Tools::sanitize_recursive( $_POST['data'] ) : false;
293
294 if ( ! $data ) {
295 wp_send_json( [
296 'success' => false,
297 'message' => __( 'Server error. Please, try again later', 'jet-form-builder' ),
298 'data' => [],
299 ] );
300 }
301
302 $plugin = $data['plugin'];
303
304 $this->activate_plugin( $plugin );
305
306 wp_send_json( [
307 'success' => true,
308 'message' => __( 'Success', 'jet-form-builder' ),
309 'data' => [],
310 ] );
311 }
312
313 /**
314 * Plugin Action Handler
315 */
316 public function addon_deactivate_action() {
317
318 $data = ( ! empty( $_POST['data'] ) ) ? Tools::sanitize_recursive( $_POST['data'] ) : false;
319
320 if ( ! $data ) {
321 wp_send_json( [
322 'success' => false,
323 'message' => __( 'Server error. Please, try again later', 'jet-form-builder' ),
324 'data' => [],
325 ] );
326 }
327
328 $plugin = $data['plugin'];
329
330 $this->deactivate_plugin( $plugin );
331
332 wp_send_json( [
333 'success' => true,
334 'message' => __( 'Success', 'jet-form-builder' ),
335 'data' => [],
336 ] );
337 }
338
339 /**
340 * @param $plugin_file
341 */
342 public function activate_plugin( $plugin_file ) {
343
344 $status = array();
345
346 if ( ! current_user_can( 'activate_plugins' ) ) {
347 wp_send_json( [
348 'success' => false,
349 'message' => __( 'Sorry, you are not allowed to install plugins on this site.', 'jet-form-builder' ),
350 'data' => [],
351 ] );
352 }
353
354 $activate = null;
355
356 if ( ! is_plugin_active( $plugin_file ) ) {
357 $activate = activate_plugin( $plugin_file );
358 }
359
360 if ( is_wp_error( $activate ) ) {
361 wp_send_json( [
362 'success' => false,
363 'message' => $activate->get_error_message(),
364 'data' => [],
365 ] );
366 }
367
368 wp_send_json( [
369 'success' => true,
370 'message' => __( 'The addon has been activated', 'jet-form-builder' ),
371 'data' => $this->get_addon_data( $plugin_file ),
372 ] );
373 }
374
375 /**
376 * @param $plugin_file
377 */
378 public function deactivate_plugin( $plugin_file ) {
379
380 $status = [];
381
382 if ( ! current_user_can( 'activate_plugins' ) ) {
383 wp_send_json( [
384 'success' => false,
385 'message' => __( 'Sorry, you are not allowed to install plugins on this site.', 'jet-form-builder' ),
386 'data' => [],
387 ] );
388 }
389
390 $deactivate_handler = null;
391
392 if ( is_plugin_active( $plugin_file ) ) {
393 $deactivate_handler = deactivate_plugins( $plugin_file );
394 }
395
396 if ( is_wp_error( $deactivate_handler ) ) {
397 wp_send_json( [
398 'success' => false,
399 'message' => $deactivate_handler->get_error_message(),
400 'data' => [],
401 ] );
402 }
403
404 wp_send_json( [
405 'success' => true,
406 'message' => __( 'The addon has been deactivated', 'jet-form-builder' ),
407 'data' => $this->get_addon_data( $plugin_file ),
408 ] );
409 }
410
411 /**
412 * License service action
413 */
414 public function service_action() {
415
416 $data = ( ! empty( $_POST['data'] ) ) ? Tools::sanitize_recursive( $_POST['data'] ) : false;
417
418 if ( ! $data || ! isset( $data['action'] ) ) {
419 wp_send_json(
420 array(
421 'success' => false,
422 'message' => __( 'Server error. Please, try again later', 'jet-form-builder' ),
423 'data' => array(),
424 )
425 );
426 }
427
428 $action = $data['action'];
429
430 switch ( $action ) {
431
432 case 'check-plugin-update':
433 wp_clean_update_cache();
434
435 wp_send_json(
436 array(
437 'success' => true,
438 'message' => __( 'Addons Update Checked', 'jet-form-builder' ),
439 'data' => array(),
440 )
441 );
442
443 break;
444
445 default:
446 wp_send_json(
447 array(
448 'success' => false,
449 'message' => __( 'Service action Not Found', 'jet-form-builder' ),
450 'data' => array(),
451 )
452 );
453 break;
454 }
455
456 exit;
457 }
458
459 /**
460 * @param $plugin_meta
461 * @param $plugin_file
462 * @param $plugin_data
463 *
464 * @return mixed
465 */
466 public function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data ) {
467
468 $available_addons_data = $this->get_jfb_remote_plugin_list();
469
470 $is_jfb_addon = false;
471 $plugin_data = false;
472
473 foreach ( $available_addons_data as $key => $addon_data ) {
474
475 if ( $plugin_file === $addon_data['slug'] ) {
476 $is_jfb_addon = true;
477 $plugin_data = $addon_data;
478 }
479 }
480
481 if ( $is_jfb_addon && empty( $plugin_data['update'] ) ) {
482
483 $plugin_slug = $this->get_addon_slug_by_filename( $plugin_data['slug'] );
484
485 $plugin_meta['view-details'] = sprintf( '<a href="%s" class="thickbox open-plugin-details-modal" aria-label="%s" data-title="%s">%s</a>',
486 esc_url_raw( network_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $plugin_slug . '&TB_iframe=true&width=600&height=550' ) ),
487 esc_attr( sprintf( __( 'More information about %s', 'jet-form-builder' ), $plugin_data['name'] ) ),
488 esc_attr( $plugin_data['name'] ),
489 __( 'View details', 'jet-form-builder' )
490 );
491 }
492
493 return $plugin_meta;
494 }
495
496 /**
497 * @param $_data
498 * @param string $_action
499 * @param null $_args
500 *
501 * @return mixed
502 */
503 public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
504
505 if ( 'plugin_information' !== $_action ) {
506 return $_data;
507 }
508
509 if ( ! isset( $_args->slug ) ) {
510 return $_data;
511 }
512
513 $available_addons_data = $this->get_jfb_remote_plugin_list();
514 $user_installed_plugins = $this->get_user_installed_plugins();
515
516 $registered_plugin_data = false;
517
518 foreach ( $available_addons_data as $key => $addon_data ) {
519
520 $addon_slug = $this->get_addon_slug_by_filename( $addon_data['slug'] );
521
522 if ( $addon_slug === $_args->slug ) {
523 $registered_plugin_data = $addon_data;
524 $registered_plugin_data['plugin_slug'] = $addon_slug;
525 $registered_plugin_data['transient_key'] = $addon_slug . '_addon_info_data';
526 $registered_plugin_data['banners'] = [];
527
528 if ( ! empty( $user_installed_plugins[ $addon_data['slug'] ] ) ) {
529 $installed_plugin_data = $user_installed_plugins[ $addon_data['slug'] ];
530
531 $registered_plugin_data['author'] = $installed_plugin_data['Author'];
532 $registered_plugin_data['plugin_url'] = $installed_plugin_data['PluginURI'];
533 $registered_plugin_data['requires'] = $installed_plugin_data['RequiresWP'];
534 $registered_plugin_data['tested'] = $installed_plugin_data['RequiresPHP'];
535 }
536
537 break;
538 }
539 }
540
541 if ( ! $registered_plugin_data ) {
542 return $_data;
543 }
544
545 $addon_api_data = get_site_transient( $registered_plugin_data['transient_key'] );
546
547 if ( empty( $addon_api_data ) ) {
548 $changelog_remote_response = $this->changelog_remote_query( $registered_plugin_data['plugin_slug'] );
549
550 if ( ! $changelog_remote_response ) {
551 return $_data;
552 }
553
554 $plugin_api_data = new \stdClass();
555
556 $plugin_api_data->name = $registered_plugin_data['name'];
557 $plugin_api_data->slug = $registered_plugin_data['slug'];
558 $plugin_api_data->author = $registered_plugin_data['author'];
559 $plugin_api_data->homepage = $registered_plugin_data['plugin_url'];
560 $plugin_api_data->requires = $registered_plugin_data['requires'];
561 $plugin_api_data->tested = $registered_plugin_data['tested'];
562 $plugin_api_data->banners = $registered_plugin_data['banners'];
563 $plugin_api_data->version = $changelog_remote_response->current_version;
564 $plugin_api_data->sections = array(
565 'changelog' => $changelog_remote_response->changelog,
566 );
567
568 // Expires in 1 day
569 set_site_transient( $registered_plugin_data['transient_key'], $plugin_api_data, DAY_IN_SECONDS );
570 }
571
572 $_data = $addon_api_data;
573
574 return $_data;
575 }
576
577 /**
578 * @param $slug
579 *
580 * @return false|mixed
581 */
582 public function changelog_remote_query( $slug ) {
583
584 $response = wp_remote_get( sprintf( $this->jet_changelog_url, $slug ) );
585
586 if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != '200' ) {
587 return false;
588 }
589
590 $response = json_decode( $response['body'] );
591
592 return $response;
593 }
594
595 /**
596 * [get_theme_info description]
597 *
598 * @return [type] [description]
599 */
600 public function get_theme_info() {
601 $style_parent_theme = wp_get_theme( get_template() );
602
603 return apply_filters(
604 'jfb-addons-page/theme-info',
605 array(
606 'name' => $style_parent_theme->get( 'Name' ),
607 'theme' => strtolower( preg_replace( '/\s+/', '', $style_parent_theme->get( 'Name' ) ) ),
608 'version' => $style_parent_theme->get( 'Version' ),
609 'author' => $style_parent_theme->get( 'Author' ),
610 'authorSlug' => strtolower( preg_replace( '/\s+/', '', $style_parent_theme->get( 'Author' ) ) ),
611 )
612 );
613 }
614
615 /**
616 * [allow_unsafe_urls description]
617 * @param [type] $args [description]
618 * @return [type] [description]
619 */
620 public function allow_unsafe_urls( $args ) {
621
622 if ( isset( $_REQUEST['action'] ) && 'jfb_addon_action' === $_REQUEST['action'] ) {
623 $args['reject_unsafe_urls'] = false;
624 }
625
626 return $args;
627 }
628
629 public function is_active() {
630 return ! empty( get_option( 'jfb-license-data', false ) );
631 }
632
633 public function get_slug(): string {
634 return $this->is_active() ? 'jetformbuilder-license' : self::NOT_ACTIVE;
635 }
636
637 /**
638 * Manager constructor.
639 */
640 public function __construct() {
641
642 add_action( 'wp_ajax_jfb_license_service_action', array( $this, 'service_action' ) );
643
644 add_action( 'wp_ajax_jfb_addon_action', array( $this, 'plugin_action' ) );
645
646 add_action( 'wp_ajax_jfb_addon_activate_action', array( $this, 'addon_activate_action' ) );
647
648 add_action( 'wp_ajax_jfb_addon_deactivate_action', array( $this, 'addon_deactivate_action' ) );
649
650 add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 3 );
651
652 add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
653
654 add_filter( 'http_request_args', array( $this, 'allow_unsafe_urls' ) );
655
656 }
657 }
658