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