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