Completed
Push — master ( 6b21ff...339518 )
by Stephanie
15s queued 10s
created

FrmAddonsController::is_license_expiring()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 4
nop 0
dl 0
loc 20
rs 8.9777
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	die( 'You are not allowed to call this page directly.' );
4
}
5
6
class FrmAddonsController {
7
8
	public static function menu() {
9
		if ( ! current_user_can( 'activate_plugins' ) ) {
10
			return;
11
		}
12
13
		$label = __( 'Add-Ons', 'formidable' );
14
		if ( FrmAppHelper::pro_is_installed() ) {
15
			$label = '<span style="color:#fe5a1d">' . $label . '</span>';
16
		}
17
		add_submenu_page( 'formidable', 'Formidable | ' . __( 'Add-Ons', 'formidable' ), $label, 'frm_view_forms', 'formidable-addons', 'FrmAddonsController::list_addons' );
18
19
		if ( ! FrmAppHelper::pro_is_installed() ) {
20
			add_submenu_page(
21
				'formidable',
22
				'Formidable | ' . __( 'Upgrade', 'formidable' ),
23
				'<span style="color:#fe5a1d">' . __( 'Upgrade', 'formidable' ) . '</span>',
24
				'frm_view_forms',
25
				'formidable-pro-upgrade',
26
				'FrmAddonsController::upgrade_to_pro'
27
			);
28
		} elseif ( 'formidable-pro-upgrade' === FrmAppHelper::get_param( 'page' ) ) {
29
			wp_safe_redirect( admin_url( 'admin.php?page=formidable' ) );
30
			exit;
31
		}
32
	}
33
34
	public static function list_addons() {
35
		FrmAppHelper::include_svg();
36
		$installed_addons = apply_filters( 'frm_installed_addons', array() );
37
		$license_type     = '';
38
39
		$addons = self::get_api_addons();
40
		$errors = array();
41
42
		if ( isset( $addons['error'] ) ) {
43
			$api    = new FrmFormApi();
44
			$errors = $api->get_error_from_response( $addons );
45
			$license_type = isset( $addons['error']['type'] ) ? $addons['error']['type'] : '';
46
			unset( $addons['error'] );
47
		}
48
		self::prepare_addons( $addons );
49
50
		$pricing = FrmAppHelper::admin_upgrade_link( 'addons' );
51
52
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/list.php' );
53
	}
54
55
	public static function license_settings() {
56
		$plugins = apply_filters( 'frm_installed_addons', array() );
57
		if ( empty( $plugins ) ) {
58
			esc_html_e( 'There are no plugins on your site that require a license', 'formidable' );
59
60
			return;
61
		}
62
63
		ksort( $plugins );
64
65
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/settings.php' );
66
	}
67
68
	private static function get_api_addons() {
69
		$api    = new FrmFormApi();
70
		$addons = $api->get_api_info();
71
72
		if ( empty( $addons ) ) {
73
			$addons = self::fallback_plugin_list();
74
		} else {
75
			foreach ( $addons as $k => $addon ) {
76
				if ( empty( $addon['excerpt'] ) && $k !== 'error' ) {
77
					unset( $addons[ $k ] );
78
				}
79
			}
80
		}
81
82
		return $addons;
83
	}
84
85
	/**
86
	 * If the API is unable to connect, show something on the addons page
87
	 *
88
	 * @since 3.04.03
89
	 * @return array
90
	 */
91
	private static function fallback_plugin_list() {
92
		return array(
93
			'formidable-pro' => array(
94
				'title'   => 'Formidable Forms',
95
				'link'    => 'pricing/',
96
				'docs'    => '',
97
				'excerpt' => 'Enhance your basic Formidable forms with a plethora of Pro field types and features. Create advanced forms and data-driven applications in minutes.',
98
			),
99
			'mailchimp'      => array(
100
				'title'   => 'MailChimp Forms',
101
				'excerpt' => 'Get on the path to more sales and leads in a matter of minutes. Add leads to a MailChimp mailing list when they submit forms and update their information along with the entry.',
102
			),
103
			'registration'   => array(
104
				'title'   => 'User Registration Forms',
105
				'link'    => 'downloads/user-registration/',
106
				'excerpt' => 'Give new users access to your site as quickly and painlessly as possible. Allow users to register, edit and be able to login to their profiles on your site from the front end in a clean, customized registration form.',
107
			),
108
			'paypal'         => array(
109
				'title'   => 'PayPal Standard Forms',
110
				'link'    => 'downloads/paypal-standard/',
111
				'excerpt' => 'Automate your business by collecting instant payments from your clients. Collect information, calculate a total, and send them on to PayPal. Require a payment before publishing content on your site.',
112
			),
113
			'stripe'         => array(
114
				'title'   => 'Stripe Forms',
115
				'docs'    => 'knowledgebase/stripe/',
116
				'excerpt' => 'Any Formidable forms on your site can accept credit card payments without users ever leaving your site.',
117
			),
118
			'authorize-net'  => array(
119
				'title'   => 'Authorize.net AIM Forms',
120
				'link'    => 'downloads/authorize-net-aim/',
121
				'docs'    => 'knowledgebase/authorize-net-aim/',
122
				'excerpt' => 'Accept one-time payments directly on your site, using Authorize.net AIM.',
123
			),
124
			'woocommerce'    => array(
125
				'title'   => 'WooCommerce Forms',
126
				'excerpt' => 'Use a Formidable form on your WooCommerce product pages.',
127
			),
128
			'autoresponder'  => array(
129
				'title'   => 'Form Action Automation',
130
				'docs'    => 'knowledgebase/schedule-autoresponder/',
131
				'excerpt' => 'Schedule email notifications, SMS messages, and API actions.',
132
			),
133
			'modal'          => array(
134
				'title'   => 'Bootstrap Modal Forms',
135
				'link'    => 'downloads/bootstrap-modal/',
136
				'docs'    => 'knowledgebase/bootstrap-modal/',
137
				'excerpt' => 'Open a view or form in a Bootstrap popup.',
138
			),
139
			'bootstrap'      => array(
140
				'title'   => 'Bootstrap Style Forms',
141
				'excerpt' => 'Instantly add Bootstrap styling to all your Formidable forms.',
142
			),
143
			'zapier'         => array(
144
				'title'   => 'Zapier Forms',
145
				'excerpt' => 'Connect with hundreds of different applications through Zapier. Insert a new row in a Google docs spreadsheet, post on Twitter, or add a new Dropbox file with your form.',
146
			),
147
			'signature'      => array(
148
				'title'   => 'Digital Signature Forms',
149
				'excerpt' => 'Add a signature field to your form. The user may write their signature with a trackpad/mouse or just type it.',
150
			),
151
			'api'            => array(
152
				'title'   => 'Formidable Forms API',
153
				'link'    => 'downloads/formidable-api/',
154
				'excerpt' => 'Send entry results to any other site that has a Rest API. This includes the option of sending entries from one Formidable site to another.',
155
			),
156
			'twilio'         => array(
157
				'title'   => 'Twilio SMS Forms',
158
				'docs'    => 'knowledgebase/twilio-add-on/',
159
				'excerpt' => 'Allow users to text their votes for polls created by Formidable Forms, or send SMS notifications when entries are submitted or updated.',
160
			),
161
		);
162
	}
163
164
	/**
165
	 * If Pro is missing but has been authenticated, include a download URL
166
	 *
167
	 * @since 3.04.03
168
	 * @return string
169
	 */
170
	public static function get_pro_download_url() {
171
		$pro_cred_store = 'frmpro-credentials';
172
		$pro_wpmu_store = 'frmpro-wpmu-sitewide';
173
		if ( is_multisite() && get_site_option( $pro_wpmu_store ) ) {
174
			$creds = get_site_option( $pro_cred_store );
175
		} else {
176
			$creds = get_option( $pro_cred_store );
177
		}
178
179
		if ( empty( $creds ) || ! is_array( $creds ) || ! isset( $creds['license'] ) ) {
180
			return '';
181
		}
182
183
		$license = $creds['license'];
184
		if ( empty( $license ) ) {
185
			return '';
186
		}
187
188
		if ( strpos( $license, '-' ) ) {
189
			// this is a fix for licenses saved in the past
190
			$license = strtoupper( $license );
191
		}
192
193
		$api       = new FrmFormApi( $license );
194
		$downloads = $api->get_api_info();
195
		$pro       = self::get_pro_from_addons( $downloads );
196
197
		return isset( $pro['url'] ) ? $pro['url'] : '';
198
	}
199
200
	/**
201
	 * @since 4.08
202
	 */
203
	private static function get_pro_from_addons( $addons ) {
204
		return isset( $addons['93790'] ) ? $addons['93790'] : array();
205
	}
206
207
	/**
208
	 * @since 4.06
209
	 */
210
	public static function license_type() {
211
		$api     = new FrmFormApi();
212
		$addons  = $api->get_api_info();
213
		$type    = 'free';
214
215
		if ( isset( $addons['error'] ) ) {
216
			if ( isset( $addons['error']['code'] ) && $addons['error']['code'] === 'expired' ) {
217
				return $addons['error']['code'];
218
			}
219
			$type = isset( $addons['error']['type'] ) ? $addons['error']['type'] : $type;
220
		}
221
222
		$pro = self::get_pro_from_addons( $addons );
223
		if ( $type === 'free' ) {
224
			$type = isset( $pro['type'] ) ? $pro['type'] : $type;
225
			if ( $type === 'free' ) {
226
				return $type;
227
			}
228
		}
229
230
		if ( isset( $pro['code'] ) && $pro['code'] === 'grandfathered' ) {
231
			return $pro['code'];
232
		}
233
234
		$expires = isset( $pro['expires'] ) ? $pro['expires'] : '';
235
		$expired = $expires ? $expires < time() : false;
236
		return $expired ? 'expired' : strtolower( $type );
237
	}
238
239
	/**
240
	 * @since 4.0.01
241
	 */
242
	public static function is_license_expired() {
243
		$version_info = self::get_primary_license_info();
244
		if ( ! isset( $version_info['error'] ) ) {
245
			return false;
246
		}
247
248
		return $version_info['error'];
249
	}
250
251
	/**
252
	 * @since 4.08
253
	 *
254
	 * @return boolean|int false or the number of days until expiration.
255
	 */
256
	public static function is_license_expiring() {
257
		$version_info = self::get_primary_license_info();
258
		if ( ! isset( $version_info['active_sub'] ) || $version_info['active_sub'] !== 'no' ) {
259
			// Check for a subscription first.
260
			return false;
261
		}
262
263
		if ( ! isset( $version_info['error'] ) || empty( $version_info['expires'] ) ) {
264
			// It's either invalid or already expired.
265
			return false;
266
		}
267
268
		$expiration = $version_info['expires'];
269
		$days_left  = ( $expiration - time() ) / DAY_IN_SECONDS;
270
		if ( $days_left > 30 ) {
271
			return false;
272
		}
273
274
		return $days_left;
275
	}
276
277
	/**
278
	 * @since 4.08
279
	 */
280
	private static function get_primary_license_info() {
281
		$installed_addons = apply_filters( 'frm_installed_addons', array() );
282
		if ( empty( $installed_addons ) || ! isset( $installed_addons['formidable_pro'] ) ) {
283
			return false;
284
		}
285
		$installed_addons = array(
286
			'formidable_pro' => $installed_addons['formidable_pro'],
287
		);
288
289
		return self::fill_update_addon_info( $installed_addons );
290
	}
291
292
	/**
293
	 * @since 3.04.03
294
	 */
295
	public static function check_update( $transient ) {
296
		if ( ! is_object( $transient ) ) {
297
			$transient = new stdClass();
298
		}
299
300
		$installed_addons = apply_filters( 'frm_installed_addons', array() );
301
		if ( empty( $installed_addons ) ) {
302
			return $transient;
303
		}
304
305
		$version_info = self::fill_update_addon_info( $installed_addons );
306
307
		$transient->last_checked = time();
308
309
		if ( ! function_exists( 'get_plugins' ) ) {
310
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
311
		}
312
		$wp_plugins = get_plugins();
313
314
		foreach ( $version_info as $id => $plugin ) {
315
			$plugin = (object) $plugin;
316
317
			if ( ! isset( $plugin->new_version ) || ! isset( $plugin->package ) ) {
318
				continue;
319
			}
320
321
			$folder = $plugin->plugin;
322
			if ( empty( $folder ) ) {
323
				continue;
324
			}
325
326
			if ( ! self::is_installed( $folder ) ) {
327
				// don't show an update if the plugin isn't installed
328
				continue;
329
			}
330
331
			$wp_plugin  = isset( $wp_plugins[ $folder ] ) ? $wp_plugins[ $folder ] : array();
332
			$wp_version = isset( $wp_plugin['Version'] ) ? $wp_plugin['Version'] : '1.0';
333
334
			if ( version_compare( $wp_version, $plugin->new_version, '<' ) ) {
335
				$slug                           = explode( '/', $folder );
336
				$plugin->slug                   = $slug[0];
337
				$transient->response[ $folder ] = $plugin;
338
			}
339
340
			$transient->checked[ $folder ] = $wp_version;
341
342
		}
343
344
		return $transient;
345
	}
346
347
	/**
348
	 * Check if a plugin is installed before showing an update for it
349
	 *
350
	 * @since 3.05
351
	 *
352
	 * @param string $plugin - the folder/filename.php for a plugin
353
	 *
354
	 * @return bool - True if installed
355
	 */
356
	private static function is_installed( $plugin ) {
357
		if ( ! function_exists( 'get_plugins' ) ) {
358
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
359
		}
360
361
		$all_plugins = get_plugins();
362
363
		return isset( $all_plugins[ $plugin ] );
364
	}
365
366
	/**
367
	 * @since 3.04.03
368
	 *
369
	 * @param array $installed_addons
370
	 *
371
	 * @return array
372
	 */
373
	private static function fill_update_addon_info( $installed_addons ) {
374
		$checked_licenses = array();
375
		$version_info     = array();
376
377
		foreach ( $installed_addons as $addon ) {
378
			if ( $addon->store_url !== 'https://formidableforms.com' ) {
379
				// check if this is a third-party addon
380
				continue;
381
			}
382
383
			$new_license = $addon->license;
384
			if ( empty( $new_license ) || in_array( $new_license, $checked_licenses ) ) {
385
				continue;
386
			}
387
388
			$checked_licenses[] = $new_license;
389
390
			$api = new FrmFormApi( $new_license );
391
			if ( empty( $version_info ) ) {
392
				$version_info = $api->get_api_info();
393
				continue;
394
			}
395
396
			$plugin = $api->get_addon_for_license( $addon, $version_info );
397
			if ( empty( $plugin ) ) {
398
				continue;
399
			}
400
401
			$download_id = isset( $plugin['id'] ) ? $plugin['id'] : 0;
402
			if ( ! empty( $download_id ) && ! isset( $version_info[ $download_id ]['package'] ) ) {
403
				// if this addon is using its own license, get the update url
404
				$addon_info = $api->get_api_info();
405
406
				$version_info[ $download_id ] = $addon_info[ $download_id ];
407
				if ( isset( $addon_info['error'] ) ) {
408
					$version_info[ $download_id ]['error'] = array(
409
						'message' => $addon_info['error']['message'],
410
						'code'    => $addon_info['error']['code'],
411
					);
412
				}
413
			}
414
		}
415
416
		return $version_info;
417
	}
418
419
	/**
420
	 * Get the action link for an addon that isn't active.
421
	 *
422
	 * @since 3.06.03
423
	 * @param string $addon The plugin slug
0 ignored issues
show
Bug introduced by
There is no parameter named $addon. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
424
	 * @return array
425
	 */
426
	public static function install_link( $plugin ) {
427
		$link    = array();
428
		$addons = self::get_api_addons();
429
		self::prepare_addons( $addons );
430
431
		foreach ( $addons as $addon ) {
432
			$slug = explode( '/', $addon['plugin'] );
433
			if ( $slug[0] !== 'formidable-' . $plugin ) {
434
				continue;
435
			}
436
437
			if ( $addon['status']['type'] === 'installed' && ! empty( $addon['activate_url'] ) ) {
438
				$link = array(
439
					'url'   => $addon['plugin'],
440
					'class' => 'frm-activate-addon',
441
				);
442
			} elseif ( isset( $addon['url'] ) && ! empty( $addon['url'] ) ) {
443
				$link = array(
444
					'url'   => $addon['url'],
445
					'class' => 'frm-install-addon',
446
				);
447
			} elseif ( isset( $addon['categories'] ) && ! empty( $addon['categories'] ) ) {
448
				$link = array(
449
					'categories' => $addon['categories'],
450
				);
451
			}
452
453
			if ( ! empty( $link ) ) {
454
				$link['status'] = $addon['status']['type'];
455
			}
456
457
			return $link;
458
		}
459
	}
460
461
	/**
462
	 * @since 3.04.03
463
	 *
464
	 * @param array $addons
465
	 * @param object $license The FrmAddon object
466
	 *
467
	 * @return array
468
	 */
469
	public static function get_addon_for_license( $addons, $license ) {
470
		$download_id = $license->download_id;
471
		$plugin      = array();
472 View Code Duplication
		if ( empty( $download_id ) && ! empty( $addons ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
473
			foreach ( $addons as $addon ) {
474
				if ( strtolower( $license->plugin_name ) == strtolower( $addon['title'] ) ) {
475
					return $addon;
476
				}
477
			}
478
		} elseif ( isset( $addons[ $download_id ] ) ) {
479
			$plugin = $addons[ $download_id ];
480
		}
481
482
		return $plugin;
483
	}
484
485
	private static function prepare_addons( &$addons ) {
486
		$activate_url = '';
487
		if ( current_user_can( 'activate_plugins' ) ) {
488
			$activate_url = add_query_arg( array( 'action' => 'activate' ), admin_url( 'plugins.php' ) );
489
		}
490
491
		$loop_addons = $addons;
492
		foreach ( $loop_addons as $id => $addon ) {
493
			if ( is_numeric( $id ) ) {
494
				$slug      = str_replace( array( '-wordpress-plugin', '-wordpress' ), '', $addon['slug'] );
495
				$file_name = $addon['plugin'];
496
			} else {
497
				$slug = $id;
498
				if ( isset( $addon['file'] ) ) {
499
					$base_file = $addon['file'];
500
				} else {
501
					$base_file = 'formidable-' . $slug;
502
				}
503
				$file_name = $base_file . '/' . $base_file . '.php';
504
			}
505
506
			$addon['installed']    = self::is_installed( $file_name );
507
			$addon['activate_url'] = '';
508
509
			if ( $addon['installed'] && ! empty( $activate_url ) && ! is_plugin_active( $file_name ) ) {
510
				$addon['activate_url'] = add_query_arg(
511
					array(
512
						'_wpnonce' => wp_create_nonce( 'activate-plugin_' . $file_name ),
513
						'plugin'   => $file_name,
514
					),
515
					$activate_url
516
				);
517
			}
518
519
			if ( ! isset( $addon['docs'] ) ) {
520
				$addon['docs'] = 'knowledgebase/formidable-' . $slug . '/';
521
			}
522
			self::prepare_addon_link( $addon['docs'] );
523
524
			if ( ! isset( $addon['link'] ) ) {
525
				$addon['link'] = 'downloads/' . $slug . '/';
526
			}
527
			self::prepare_addon_link( $addon['link'] );
528
529
			self::set_addon_status( $addon );
530
			$addons[ $id ] = $addon;
531
		}
532
	}
533
534
	/**
535
	 * @since 3.04.02
536
	 */
537
	private static function prepare_addon_link( &$link ) {
538
		$site_url = 'https://formidableforms.com/';
539
		if ( strpos( $link, 'http' ) !== 0 ) {
540
			$link = $site_url . $link;
541
		}
542
		$link       = FrmAppHelper::make_affiliate_url( $link );
543
		$query_args = array(
544
			'utm_source'   => 'WordPress',
545
			'utm_medium'   => 'addons',
546
			'utm_campaign' => 'liteplugin',
547
		);
548
		$link       = add_query_arg( $query_args, $link );
549
	}
550
551
	/**
552
	 * Add the status to the addon array. Status options are:
553
	 * installed, active, not installed
554
	 *
555
	 * @since 3.04.02
556
	 */
557
	private static function set_addon_status( &$addon ) {
558
		if ( ! empty( $addon['activate_url'] ) ) {
559
			$addon['status'] = array(
560
				'type'  => 'installed',
561
				'label' => __( 'Installed', 'formidable' ),
562
			);
563
		} elseif ( $addon['installed'] ) {
564
			$addon['status'] = array(
565
				'type'  => 'active',
566
				'label' => __( 'Active', 'formidable' ),
567
			);
568
		} else {
569
			$addon['status'] = array(
570
				'type'  => 'not-installed',
571
				'label' => __( 'Not Installed', 'formidable' ),
572
			);
573
		}
574
	}
575
576
	public static function upgrade_to_pro() {
577
		FrmAppHelper::include_svg();
578
579
		$link_parts = array(
580
			'medium'  => 'upgrade',
581
			'content' => 'button',
582
		);
583
584
		$features = array(
585
			'Display Entries' => array(
586
				array(
587
					'label' => 'Display form data with virtually limitless views',
588
					'link'  => array(
589
						'content' => 'views',
590
						'param'   => 'views-display-form-data',
591
					),
592
					'lite'  => false,
593
				),
594
				array(
595
					'label' => 'Generate graphs and stats based on your submitted data',
596
					'link'  => array(
597
						'content' => 'graphs',
598
						'param'   => 'statistics-graphs-wordpress-forms',
599
					),
600
					'lite'  => false,
601
				),
602
			),
603
			'Entry Management' => array(
604
				array(
605
					'label' => 'Import entries from a CSV',
606
					'link'  => array(
607
						'content' => 'import-entries',
608
						'param'   => 'importing-exporting-wordpress-forms',
609
					),
610
					'lite'  => false,
611
				),
612
				array(
613
					'label' => 'Logged-in users can save drafts and return later',
614
					'link'  => array(
615
						'content' => 'save-drafts',
616
						'param'   => 'save-drafts-wordpress-form',
617
					),
618
					'lite'  => false,
619
				),
620
				array(
621
					'label' => 'Flexibly and powerfully view, edit, and delete entries from anywhere on your site',
622
					'link'  => array(
623
						'content' => 'front-edit',
624
						'param'   => 'wordpress-front-end-editing',
625
					),
626
					'lite'  => false,
627
				),
628
				array(
629
					'label' => 'View form submissions from the back-end',
630
					'lite'  => true,
631
				),
632
				array(
633
					'label' => 'Export your entries to a CSV',
634
					'lite'  => true,
635
				),
636
			),
637
			'Form Building' => array(
638
				array(
639
					'label' => 'Save a calculated value into a field',
640
					'link'  => array(
641
						'content' => 'calculations',
642
						'param'   => 'field-calculations-wordpress-form',
643
					),
644
					'lite'  => false,
645
				),
646
				array(
647
					'label' => 'Allow multiple file uploads',
648
					'link'  => array(
649
						'content' => 'file-uploads',
650
						'param'   => 'wordpress-multi-file-upload-fields',
651
					),
652
					'lite'  => false,
653
				),
654
				array(
655
					'label' => 'Repeat sections of fields',
656
					'link'  => array(
657
						'content' => 'repeaters',
658
						'param'   => 'repeatable-sections-forms',
659
					),
660
					'lite'  => false,
661
				),
662
				array(
663
					'label' => 'Hide and show fields conditionally based on other fields or the user\'s role',
664
					'link'  => array(
665
						'content' => 'conditional-logic',
666
						'param'   => 'conditional-logic-wordpress-forms',
667
					),
668
					'lite'  => false,
669
				),
670
				array(
671
					'label' => 'Confirmation fields',
672
					'link'  => array(
673
						'content' => 'confirmation-fields',
674
						'param'   => 'confirmation-fields-wordpress-forms',
675
					),
676
					'lite'  => false,
677
				),
678
				array(
679
					'label' => 'Multi-paged forms',
680
					'link'  => array(
681
						'content' => 'page-breaks',
682
						'param'   => 'wordpress-multi-page-forms',
683
					),
684
					'lite'  => false,
685
				),
686
				array(
687
					'label' => 'Include section headings, page breaks, rich text, dates, times, scales, star ratings, sliders, toggles, dynamic fields populated from other forms, passwords, and tags in advanced forms.',
688
					'lite'  => false,
689
				),
690
				array(
691
					'label' => 'Include text, email, url, paragraph text, radio, checkbox, dropdown fields, hidden fields, user ID fields, and HTML blocks in your form.',
692
					'lite'  => true,
693
				),
694
				array(
695
					'label' => 'Drag & Drop Form building',
696
					'link'  => array(
697
						'content' => 'drag-drop',
698
						'param'   => 'drag-drop-forms',
699
					),
700
					'lite'  => true,
701
				),
702
				array(
703
					'label' => 'Create forms from Templates',
704
					'link'  => array(
705
						'content' => 'form-templates',
706
						'param'   => 'wordpress-form-templates',
707
					),
708
					'lite'  => true,
709
				),
710
				array(
711
					'label' => 'Import and export forms with XML',
712
					'link'  => array(
713
						'content' => 'import',
714
						'param'   => 'importing-exporting-wordpress-forms',
715
					),
716
					'lite'  => true,
717
				),
718
				array(
719
					'label' => 'Use input placeholder text in your fields that clear when typing starts.',
720
					'lite'  => true,
721
				),
722
			),
723
			'Form Actions' => array(
724
				array(
725
					'label' => 'Conditionally send your email notifications based on values in your form',
726
					'link'  => array(
727
						'content' => 'conditional-emails',
728
					),
729
					'lite'  => false,
730
				),
731
				array(
732
					'label' => 'Create and edit WordPress posts or custom posts from the front-end',
733
					'link'  => array(
734
						'content' => 'create-posts',
735
						'param'   => 'create-posts-pages-wordpress-forms',
736
					),
737
					'lite'  => false,
738
				),
739
				array(
740
					'label' => 'Send multiple emails and autoresponders',
741
					'link'  => array(
742
						'content' => 'multiple-emails',
743
						'param'   => 'virtually-unlimited-emails',
744
					),
745
					'lite'  => true,
746
				),
747
			),
748
			'Form Appearance' => array(
749
				array(
750
					'label' => 'Create Multiple styles for different forms',
751
					'link'  => array(
752
						'content' => 'multiple-styles',
753
						'param'   => 'wordpress-visual-form-styler',
754
					),
755
					'lite'  => false,
756
				),
757
				array(
758
					'label' => 'Customizable layout with CSS classes',
759
					'link'  => array(
760
						'content' => 'form-layout',
761
						'param'   => 'wordpress-mobile-friendly-forms',
762
					),
763
					'lite'  => true,
764
				),
765
				array(
766
					'label' => 'Customize the HTML for your forms',
767
					'link'  => array(
768
						'content' => 'custom-html',
769
						'param'   => 'customizable-html-wordpress-form',
770
					),
771
					'lite'  => true,
772
				),
773
				array(
774
					'label' => 'Style your form with the Visual Form Styler',
775
					'lite'  => true,
776
				),
777
			),
778
		);
779
780
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/upgrade_to_pro.php' );
781
	}
782
783
	/**
784
	 * Install Pro after connection with Formidable.
785
	 *
786
	 * @since 4.02.05
787
	 */
788
	public static function connect_pro() {
789
		FrmAppHelper::permission_check( 'install_plugins' );
790
		check_ajax_referer( 'frm_ajax', 'nonce' );
791
792
		$url = FrmAppHelper::get_post_param( 'plugin', '', 'sanitize_text_field' );
793
		if ( FrmAppHelper::pro_is_installed() || empty( $url ) ) {
794
			wp_die();
795
		}
796
797
		$response = array();
798
799
		// It's already installed and active.
800
		$active = activate_plugin( 'formidable-pro/formidable-pro.php', false, false, true );
801
		if ( is_wp_error( $active ) ) {
802
			// The plugin was installed, but not active. Download it now.
803
			self::ajax_install_addon();
804
		} else {
805
			$response['active'] = true;
806
		}
807
808
		echo json_encode( $response );
809
		wp_die();
810
	}
811
812
	/**
813
	 * @since 3.04.02
814
	 */
815
	public static function ajax_install_addon() {
816
817
		self::install_addon_permissions();
818
819
		// Set the current screen to avoid undefined notices.
820
		global $hook_suffix;
821
		set_current_screen();
822
823
		self::maybe_show_cred_form();
824
825
		$installed = self::install_addon();
826
		self::maybe_activate_addon( $installed );
827
828
		// Send back a response.
829
		echo json_encode( __( 'Your plugin has been installed. Please reload the page to see more options.', 'formidable' ) );
830
		wp_die();
831
	}
832
833
	/**
834
	 * @since 3.04.02
835
	 */
836
	private static function maybe_show_cred_form() {
837
		// Start output bufferring to catch the filesystem form if credentials are needed.
838
		ob_start();
839
840
		$show_form = false;
841
		$method    = '';
842
		$url       = add_query_arg( array( 'page' => 'formidable-settings' ), admin_url( 'admin.php' ) );
843
		$url       = esc_url_raw( $url );
844
		$creds     = request_filesystem_credentials( $url, $method, false, false, null );
845
846
		if ( false === $creds ) {
847
			$show_form = true;
848
		} elseif ( ! WP_Filesystem( $creds ) ) {
849
			request_filesystem_credentials( $url, $method, true, false, null );
850
			$show_form = true;
851
		}
852
853
		if ( $show_form ) {
854
			//$form = ob_get_clean();
855
			//TODO: test this: echo json_encode( array( 'form' => $form ) );
856
			echo json_encode( array( 'form' => __( 'Sorry, you\'re site requires FTP authentication. Please install plugins manaully.', 'formidable' ) ) );
857
			wp_die();
858
		}
859
860
		ob_end_clean();
861
	}
862
863
	/**
864
	 * We do not need any extra credentials if we have gotten this far,
865
	 * so let's install the plugin.
866
	 *
867
	 * @since 3.04.02
868
	 */
869
	private static function install_addon() {
870
		require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
871
872
		$download_url = FrmAppHelper::get_param( 'plugin', '', 'post', 'esc_url_raw' );
873
874
		// Create the plugin upgrader with our custom skin.
875
		$installer = new Plugin_Upgrader( new FrmInstallerSkin() );
876
		$installer->install( $download_url );
877
878
		// Flush the cache and return the newly installed plugin basename.
879
		wp_cache_flush();
880
881
		return $installer->plugin_info();
882
	}
883
884
	/**
885
	 * @since 3.06.03
886
	 */
887
	public static function ajax_activate_addon() {
888
889
		self::install_addon_permissions();
890
891
		// Set the current screen to avoid undefined notices.
892
		global $hook_suffix;
893
		set_current_screen();
894
895
		$plugin = FrmAppHelper::get_param( 'plugin', '', 'post', 'sanitize_text_field' );
896
		self::maybe_activate_addon( $plugin );
897
898
		// Send back a response.
899
		echo json_encode( __( 'Your plugin has been activated. Please reload the page to see more options.', 'formidable' ) );
900
		wp_die();
901
	}
902
903
	/**
904
	 * @since 4.06.02
905
	 */
906
	public static function ajax_multiple_addons() {
907
		self::install_addon_permissions();
908
909
		// Set the current screen to avoid undefined notices.
910
		global $hook_suffix;
911
		set_current_screen();
912
913
		$download_urls = FrmAppHelper::get_param( 'plugin', '', 'post' );
914
		$download_urls = explode( ',', $download_urls );
915
		FrmAppHelper::sanitize_value( 'esc_url_raw', $download_urls );
916
917
		foreach ( $download_urls as $download_url ) {
918
			$_POST['plugin'] = $download_url;
919
			if ( strpos( $download_url, 'http' ) !== false ) {
920
				// Installing.
921
				self::maybe_show_cred_form();
922
923
				$installed = self::install_addon();
924
				self::maybe_activate_addon( $installed );
925
			} else {
926
				// Activating.
927
				self::maybe_activate_addon( $download_url );
928
			}
929
		}
930
931
		echo json_encode( __( 'Your plugins have been installed and activated.', 'formidable' ) );
932
933
		wp_die();
934
	}
935
936
	/**
937
	 * @since 3.04.02
938
	 * @param string $installed The plugin folder name with file name
939
	 */
940
	private static function maybe_activate_addon( $installed ) {
941
		if ( ! $installed ) {
942
			return;
943
		}
944
945
		$activate = activate_plugin( $installed );
946
		if ( is_wp_error( $activate ) ) {
947
			// Ignore the invalid header message that shows with nested plugins.
948
			if ( $activate->get_error_code() !== 'no_plugin_header' ) {
949
				echo json_encode( array( 'error' => $activate->get_error_message() ) );
950
				wp_die();
951
			}
952
		}
953
	}
954
955
	/**
956
	 * Run security checks before installing
957
	 *
958
	 * @since 3.04.02
959
	 */
960
	private static function install_addon_permissions() {
961
		check_ajax_referer( 'frm_ajax', 'nonce' );
962
963
		if ( ! current_user_can( 'activate_plugins' ) || ! isset( $_POST['plugin'] ) ) {
964
			echo json_encode( true );
965
			wp_die();
966
		}
967
	}
968
969
	/**
970
	 * @since 3.04.03
971
	 * @deprecated 3.06
972
	 * @codeCoverageIgnore
973
	 * @return array
974
	 */
975
	public static function error_for_license( $license ) {
976
		return FrmDeprecated::error_for_license( $license );
977
	}
978
979
	/**
980
	 * @since 3.04.03
981
	 * @deprecated 3.06
982
	 * @codeCoverageIgnore
983
	 */
984
	public static function get_pro_updater() {
985
		return FrmDeprecated::get_pro_updater();
986
	}
987
988
	/**
989
	 * @since 3.04.03
990
	 * @deprecated 3.06
991
	 * @codeCoverageIgnore
992
	 *
993
	 * @return array
994
	 */
995
	public static function get_addon_info( $license = '' ) {
996
		return FrmDeprecated::get_addon_info( $license );
997
	}
998
999
	/**
1000
	 * @since 3.04.03
1001
	 * @deprecated 3.06
1002
	 * @codeCoverageIgnore
1003
	 *
1004
	 * @return string
1005
	 */
1006
	public static function get_cache_key( $license ) {
1007
		return FrmDeprecated::get_cache_key( $license );
1008
	}
1009
1010
	/**
1011
	 * @since 3.04.03
1012
	 * @deprecated 3.06
1013
	 * @codeCoverageIgnore
1014
	 */
1015
	public static function reset_cached_addons( $license = '' ) {
1016
		FrmDeprecated::reset_cached_addons( $license );
1017
	}
1018
1019
	/**
1020
	 * @since 2.03.08
1021
	 * @deprecated 3.04.03
1022
	 * @codeCoverageIgnore
1023
	 *
1024
	 * @param boolean $return
1025
	 * @param string $package
1026
	 *
1027
	 * @return boolean
1028
	 */
1029
	public static function add_shorten_edd_filename_filter( $return, $package ) {
1030
		return FrmDeprecated::add_shorten_edd_filename_filter( $return, $package );
1031
	}
1032
1033
	/**
1034
	 * @since 2.03.08
1035
	 * @deprecated 3.04.03
1036
	 * @codeCoverageIgnore
1037
	 *
1038
	 * @param string $filename
1039
	 * @param string $ext
1040
	 *
1041
	 * @return string
1042
	 */
1043
	public static function shorten_edd_filename( $filename, $ext ) {
1044
		return FrmDeprecated::shorten_edd_filename( $filename, $ext );
1045
	}
1046
1047
	/**
1048
	 * @deprecated 3.04.03
1049
	 * @codeCoverageIgnore
1050
	 */
1051
	public static function get_licenses() {
1052
		FrmDeprecated::get_licenses();
1053
	}
1054
}
1055