Completed
Push — master ( 923363...55c46a )
by Stephanie
17s queued 11s
created

FrmAddonsController::prepare_addons()   C

Complexity

Conditions 11
Paths 82

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 82
nop 1
dl 0
loc 51
rs 6.9224
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
		$list = 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
		$defaults = array(
164
			'released' => '',
165
		);
166
167
		foreach ( $list as $k => $info ) {
168
			$info['slug'] = $k;
169
			$list[ $k ]   = array_merge( $defaults, $info );
170
		}
171
		return $list;
172
	}
173
174
	/**
175
	 * If Pro is missing but has been authenticated, include a download URL
176
	 *
177
	 * @since 3.04.03
178
	 * @return string
179
	 */
180
	public static function get_pro_download_url() {
181
		$license   = self::get_pro_license();
182
		$api       = new FrmFormApi( $license );
183
		$downloads = $api->get_api_info();
184
		$pro       = self::get_pro_from_addons( $downloads );
185
186
		return isset( $pro['url'] ) ? $pro['url'] : '';
187
	}
188
189
	/**
190
	 * @since 4.08
191
	 */
192
	public static function get_pro_license() {
193
		$pro_cred_store = 'frmpro-credentials';
194
		$pro_wpmu_store = 'frmpro-wpmu-sitewide';
195
		if ( is_multisite() && get_site_option( $pro_wpmu_store ) ) {
196
			$creds = get_site_option( $pro_cred_store );
197
		} else {
198
			$creds = get_option( $pro_cred_store );
199
		}
200
201
		if ( empty( $creds ) || ! is_array( $creds ) || ! isset( $creds['license'] ) ) {
202
			return '';
203
		}
204
205
		$license = $creds['license'];
206
		if ( empty( $license ) ) {
207
			return '';
208
		}
209
210
		if ( strpos( $license, '-' ) ) {
211
			// this is a fix for licenses saved in the past
212
			$license = strtoupper( $license );
213
		}
214
		return $license;
215
	}
216
217
	/**
218
	 * @since 4.08
219
	 */
220
	private static function get_pro_from_addons( $addons ) {
221
		return isset( $addons['93790'] ) ? $addons['93790'] : array();
222
	}
223
224
	/**
225
	 * @since 4.06
226
	 */
227
	public static function license_type() {
228
		$api     = new FrmFormApi();
229
		$addons  = $api->get_api_info();
230
		$type    = 'free';
231
232
		if ( isset( $addons['error'] ) ) {
233
			if ( isset( $addons['error']['code'] ) && $addons['error']['code'] === 'expired' ) {
234
				return $addons['error']['code'];
235
			}
236
			$type = isset( $addons['error']['type'] ) ? $addons['error']['type'] : $type;
237
		}
238
239
		$pro = self::get_pro_from_addons( $addons );
240
		if ( $type === 'free' ) {
241
			$type = isset( $pro['type'] ) ? $pro['type'] : $type;
242
			if ( $type === 'free' ) {
243
				return $type;
244
			}
245
		}
246
247
		if ( isset( $pro['code'] ) && $pro['code'] === 'grandfathered' ) {
248
			return $pro['code'];
249
		}
250
251
		$expires = isset( $pro['expires'] ) ? $pro['expires'] : '';
252
		$expired = $expires ? $expires < time() : false;
253
		return $expired ? 'expired' : strtolower( $type );
254
	}
255
256
	/**
257
	 * @since 4.0.01
258
	 */
259
	public static function is_license_expired() {
260
		$version_info = self::get_primary_license_info();
261
		if ( ! isset( $version_info['error'] ) ) {
262
			return false;
263
		}
264
265
		return $version_info['error'];
266
	}
267
268
	/**
269
	 * @since 4.08
270
	 *
271
	 * @return boolean|int false or the number of days until expiration.
272
	 */
273
	public static function is_license_expiring() {
274
		$version_info = self::get_primary_license_info();
275
		if ( ! isset( $version_info['active_sub'] ) || $version_info['active_sub'] !== 'no' ) {
276
			// Check for a subscription first.
277
			return false;
278
		}
279
280
		if ( isset( $version_info['error'] ) || empty( $version_info['expires'] ) ) {
281
			// It's either invalid or already expired.
282
			return false;
283
		}
284
285
		$expiration = $version_info['expires'];
286
		$days_left  = ( $expiration - time() ) / DAY_IN_SECONDS;
287
		if ( $days_left > 30 ) {
288
			return false;
289
		}
290
291
		return $days_left;
292
	}
293
294
	/**
295
	 * @since 4.08
296
	 */
297
	private static function get_primary_license_info() {
298
		$installed_addons = apply_filters( 'frm_installed_addons', array() );
299
		if ( empty( $installed_addons ) || ! isset( $installed_addons['formidable_pro'] ) ) {
300
			return false;
301
		}
302
		$installed_addons = array(
303
			'formidable_pro' => $installed_addons['formidable_pro'],
304
		);
305
306
		return self::fill_update_addon_info( $installed_addons );
307
	}
308
309
	/**
310
	 * @since 3.04.03
311
	 */
312
	public static function check_update( $transient ) {
313
		if ( ! is_object( $transient ) ) {
314
			$transient = new stdClass();
315
		}
316
317
		$installed_addons = apply_filters( 'frm_installed_addons', array() );
318
		if ( empty( $installed_addons ) ) {
319
			return $transient;
320
		}
321
322
		$version_info = self::fill_update_addon_info( $installed_addons );
323
324
		$transient->last_checked = time();
325
326
		if ( ! function_exists( 'get_plugins' ) ) {
327
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
328
		}
329
		$wp_plugins = get_plugins();
330
331
		foreach ( $version_info as $id => $plugin ) {
332
			$plugin = (object) $plugin;
333
334
			if ( ! isset( $plugin->new_version ) || ! isset( $plugin->package ) ) {
335
				continue;
336
			}
337
338
			$folder = $plugin->plugin;
339
			if ( empty( $folder ) ) {
340
				continue;
341
			}
342
343
			if ( ! self::is_installed( $folder ) ) {
344
				// don't show an update if the plugin isn't installed
345
				continue;
346
			}
347
348
			$wp_plugin  = isset( $wp_plugins[ $folder ] ) ? $wp_plugins[ $folder ] : array();
349
			$wp_version = isset( $wp_plugin['Version'] ) ? $wp_plugin['Version'] : '1.0';
350
351
			if ( version_compare( $wp_version, $plugin->new_version, '<' ) ) {
352
				$slug                           = explode( '/', $folder );
353
				$plugin->slug                   = $slug[0];
354
				$transient->response[ $folder ] = $plugin;
355
			}
356
357
			$transient->checked[ $folder ] = $wp_version;
358
359
		}
360
361
		return $transient;
362
	}
363
364
	/**
365
	 * Check if a plugin is installed before showing an update for it
366
	 *
367
	 * @since 3.05
368
	 *
369
	 * @param string $plugin - the folder/filename.php for a plugin
370
	 *
371
	 * @return bool - True if installed
372
	 */
373
	private static function is_installed( $plugin ) {
374
		if ( ! function_exists( 'get_plugins' ) ) {
375
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
376
		}
377
378
		$all_plugins = get_plugins();
379
380
		return isset( $all_plugins[ $plugin ] );
381
	}
382
383
	/**
384
	 * @since 3.04.03
385
	 *
386
	 * @param array $installed_addons
387
	 *
388
	 * @return array
389
	 */
390
	private static function fill_update_addon_info( $installed_addons ) {
391
		$checked_licenses = array();
392
		$version_info     = array();
393
394
		foreach ( $installed_addons as $addon ) {
395
			if ( $addon->store_url !== 'https://formidableforms.com' ) {
396
				// check if this is a third-party addon
397
				continue;
398
			}
399
400
			$new_license = $addon->license;
401
			if ( empty( $new_license ) || in_array( $new_license, $checked_licenses ) ) {
402
				continue;
403
			}
404
405
			$checked_licenses[] = $new_license;
406
407
			$api = new FrmFormApi( $new_license );
408
			if ( empty( $version_info ) ) {
409
				$version_info = $api->get_api_info();
410
				continue;
411
			}
412
413
			$plugin = $api->get_addon_for_license( $addon, $version_info );
414
			if ( empty( $plugin ) ) {
415
				continue;
416
			}
417
418
			$download_id = isset( $plugin['id'] ) ? $plugin['id'] : 0;
419
			if ( ! empty( $download_id ) && ! isset( $version_info[ $download_id ]['package'] ) ) {
420
				// if this addon is using its own license, get the update url
421
				$addon_info = $api->get_api_info();
422
423
				$version_info[ $download_id ] = $addon_info[ $download_id ];
424
				if ( isset( $addon_info['error'] ) ) {
425
					$version_info[ $download_id ]['error'] = array(
426
						'message' => $addon_info['error']['message'],
427
						'code'    => $addon_info['error']['code'],
428
					);
429
				}
430
			}
431
		}
432
433
		return $version_info;
434
	}
435
436
	/**
437
	 * Get the action link for an addon that isn't active.
438
	 *
439
	 * @since 3.06.03
440
	 * @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...
441
	 * @return array
442
	 */
443
	public static function install_link( $plugin ) {
444
		$link    = array();
445
		$addons = self::get_api_addons();
446
		self::prepare_addons( $addons );
447
448
		foreach ( $addons as $addon ) {
449
			$slug = explode( '/', $addon['plugin'] );
450
			if ( $slug[0] !== 'formidable-' . $plugin ) {
451
				continue;
452
			}
453
454
			if ( $addon['status']['type'] === 'installed' && ! empty( $addon['activate_url'] ) ) {
455
				$link = array(
456
					'url'   => $addon['plugin'],
457
					'class' => 'frm-activate-addon',
458
				);
459
			} elseif ( isset( $addon['url'] ) && ! empty( $addon['url'] ) ) {
460
				$link = array(
461
					'url'   => $addon['url'],
462
					'class' => 'frm-install-addon',
463
				);
464
			} elseif ( isset( $addon['categories'] ) && ! empty( $addon['categories'] ) ) {
465
				$link = array(
466
					'categories' => $addon['categories'],
467
				);
468
			}
469
470
			if ( ! empty( $link ) ) {
471
				$link['status'] = $addon['status']['type'];
472
			}
473
474
			return $link;
475
		}
476
	}
477
478
	/**
479
	 * @since 3.04.03
480
	 *
481
	 * @param array $addons
482
	 * @param object $license The FrmAddon object
483
	 *
484
	 * @return array
485
	 */
486
	public static function get_addon_for_license( $addons, $license ) {
487
		$download_id = $license->download_id;
488
		$plugin      = array();
489 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...
490
			foreach ( $addons as $addon ) {
491
				if ( strtolower( $license->plugin_name ) == strtolower( $addon['title'] ) ) {
492
					return $addon;
493
				}
494
			}
495
		} elseif ( isset( $addons[ $download_id ] ) ) {
496
			$plugin = $addons[ $download_id ];
497
		}
498
499
		return $plugin;
500
	}
501
502
	private static function prepare_addons( &$addons ) {
503
		$activate_url = '';
504
		if ( current_user_can( 'activate_plugins' ) ) {
505
			$activate_url = add_query_arg( array( 'action' => 'activate' ), admin_url( 'plugins.php' ) );
506
		}
507
508
		$loop_addons = $addons;
509
		foreach ( $loop_addons as $id => $addon ) {
510
			if ( is_numeric( $id ) ) {
511
				$slug      = str_replace( array( '-wordpress-plugin', '-wordpress' ), '', $addon['slug'] );
512
				$file_name = $addon['plugin'];
513
			} else {
514
				$slug = $id;
515
				if ( isset( $addon['file'] ) ) {
516
					$base_file = $addon['file'];
517
				} else {
518
					$base_file = 'formidable-' . $slug;
519
				}
520
				$file_name = $base_file . '/' . $base_file . '.php';
521
				if ( ! isset( $addon['plugin'] ) ) {
522
					$addon['plugin'] = $file_name;
523
				}
524
			}
525
526
			$addon['installed']    = self::is_installed( $file_name );
527
			$addon['activate_url'] = '';
528
529
			if ( $addon['installed'] && ! empty( $activate_url ) && ! is_plugin_active( $file_name ) ) {
530
				$addon['activate_url'] = add_query_arg(
531
					array(
532
						'_wpnonce' => wp_create_nonce( 'activate-plugin_' . $file_name ),
533
						'plugin'   => $file_name,
534
					),
535
					$activate_url
536
				);
537
			}
538
539
			if ( ! isset( $addon['docs'] ) ) {
540
				$addon['docs'] = 'knowledgebase/formidable-' . $slug . '/';
541
			}
542
			self::prepare_addon_link( $addon['docs'] );
543
544
			if ( ! isset( $addon['link'] ) ) {
545
				$addon['link'] = 'downloads/' . $slug . '/';
546
			}
547
			self::prepare_addon_link( $addon['link'] );
548
549
			self::set_addon_status( $addon );
550
			$addons[ $id ] = $addon;
551
		}
552
	}
553
554
	/**
555
	 * @since 3.04.02
556
	 */
557
	private static function prepare_addon_link( &$link ) {
558
		$site_url = 'https://formidableforms.com/';
559
		if ( strpos( $link, 'http' ) !== 0 ) {
560
			$link = $site_url . $link;
561
		}
562
		$link       = FrmAppHelper::make_affiliate_url( $link );
563
		$query_args = array(
564
			'utm_source'   => 'WordPress',
565
			'utm_medium'   => 'addons',
566
			'utm_campaign' => 'liteplugin',
567
		);
568
		$link       = add_query_arg( $query_args, $link );
569
	}
570
571
	/**
572
	 * Add the status to the addon array. Status options are:
573
	 * installed, active, not installed
574
	 *
575
	 * @since 3.04.02
576
	 */
577
	private static function set_addon_status( &$addon ) {
578
		if ( ! empty( $addon['activate_url'] ) ) {
579
			$addon['status'] = array(
580
				'type'  => 'installed',
581
				'label' => __( 'Installed', 'formidable' ),
582
			);
583
		} elseif ( $addon['installed'] ) {
584
			$addon['status'] = array(
585
				'type'  => 'active',
586
				'label' => __( 'Active', 'formidable' ),
587
			);
588
		} else {
589
			$addon['status'] = array(
590
				'type'  => 'not-installed',
591
				'label' => __( 'Not Installed', 'formidable' ),
592
			);
593
		}
594
	}
595
596
	public static function upgrade_to_pro() {
597
		FrmAppHelper::include_svg();
598
599
		$link_parts = array(
600
			'medium'  => 'upgrade',
601
			'content' => 'button',
602
		);
603
604
		$features = array(
605
			'Display Entries' => array(
606
				array(
607
					'label' => 'Display form data with virtually limitless views',
608
					'link'  => array(
609
						'content' => 'views',
610
						'param'   => 'views-display-form-data',
611
					),
612
					'lite'  => false,
613
				),
614
				array(
615
					'label' => 'Generate graphs and stats based on your submitted data',
616
					'link'  => array(
617
						'content' => 'graphs',
618
						'param'   => 'statistics-graphs-wordpress-forms',
619
					),
620
					'lite'  => false,
621
				),
622
			),
623
			'Entry Management' => array(
624
				array(
625
					'label' => 'Import entries from a CSV',
626
					'link'  => array(
627
						'content' => 'import-entries',
628
						'param'   => 'importing-exporting-wordpress-forms',
629
					),
630
					'lite'  => false,
631
				),
632
				array(
633
					'label' => 'Logged-in users can save drafts and return later',
634
					'link'  => array(
635
						'content' => 'save-drafts',
636
						'param'   => 'save-drafts-wordpress-form',
637
					),
638
					'lite'  => false,
639
				),
640
				array(
641
					'label' => 'Flexibly and powerfully view, edit, and delete entries from anywhere on your site',
642
					'link'  => array(
643
						'content' => 'front-edit',
644
						'param'   => 'wordpress-front-end-editing',
645
					),
646
					'lite'  => false,
647
				),
648
				array(
649
					'label' => 'View form submissions from the back-end',
650
					'lite'  => true,
651
				),
652
				array(
653
					'label' => 'Export your entries to a CSV',
654
					'lite'  => true,
655
				),
656
			),
657
			'Form Building' => array(
658
				array(
659
					'label' => 'Save a calculated value into a field',
660
					'link'  => array(
661
						'content' => 'calculations',
662
						'param'   => 'field-calculations-wordpress-form',
663
					),
664
					'lite'  => false,
665
				),
666
				array(
667
					'label' => 'Allow multiple file uploads',
668
					'link'  => array(
669
						'content' => 'file-uploads',
670
						'param'   => 'wordpress-multi-file-upload-fields',
671
					),
672
					'lite'  => false,
673
				),
674
				array(
675
					'label' => 'Repeat sections of fields',
676
					'link'  => array(
677
						'content' => 'repeaters',
678
						'param'   => 'repeatable-sections-forms',
679
					),
680
					'lite'  => false,
681
				),
682
				array(
683
					'label' => 'Hide and show fields conditionally based on other fields or the user\'s role',
684
					'link'  => array(
685
						'content' => 'conditional-logic',
686
						'param'   => 'conditional-logic-wordpress-forms',
687
					),
688
					'lite'  => false,
689
				),
690
				array(
691
					'label' => 'Confirmation fields',
692
					'link'  => array(
693
						'content' => 'confirmation-fields',
694
						'param'   => 'confirmation-fields-wordpress-forms',
695
					),
696
					'lite'  => false,
697
				),
698
				array(
699
					'label' => 'Multi-paged forms',
700
					'link'  => array(
701
						'content' => 'page-breaks',
702
						'param'   => 'wordpress-multi-page-forms',
703
					),
704
					'lite'  => false,
705
				),
706
				array(
707
					'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.',
708
					'lite'  => false,
709
				),
710
				array(
711
					'label' => 'Include text, email, url, paragraph text, radio, checkbox, dropdown fields, hidden fields, user ID fields, and HTML blocks in your form.',
712
					'lite'  => true,
713
				),
714
				array(
715
					'label' => 'Drag & Drop Form building',
716
					'link'  => array(
717
						'content' => 'drag-drop',
718
						'param'   => 'drag-drop-forms',
719
					),
720
					'lite'  => true,
721
				),
722
				array(
723
					'label' => 'Create forms from Templates',
724
					'link'  => array(
725
						'content' => 'form-templates',
726
						'param'   => 'wordpress-form-templates',
727
					),
728
					'lite'  => true,
729
				),
730
				array(
731
					'label' => 'Import and export forms with XML',
732
					'link'  => array(
733
						'content' => 'import',
734
						'param'   => 'importing-exporting-wordpress-forms',
735
					),
736
					'lite'  => true,
737
				),
738
				array(
739
					'label' => 'Use input placeholder text in your fields that clear when typing starts.',
740
					'lite'  => true,
741
				),
742
			),
743
			'Form Actions' => array(
744
				array(
745
					'label' => 'Conditionally send your email notifications based on values in your form',
746
					'link'  => array(
747
						'content' => 'conditional-emails',
748
					),
749
					'lite'  => false,
750
				),
751
				array(
752
					'label' => 'Create and edit WordPress posts or custom posts from the front-end',
753
					'link'  => array(
754
						'content' => 'create-posts',
755
						'param'   => 'create-posts-pages-wordpress-forms',
756
					),
757
					'lite'  => false,
758
				),
759
				array(
760
					'label' => 'Send multiple emails and autoresponders',
761
					'link'  => array(
762
						'content' => 'multiple-emails',
763
						'param'   => 'virtually-unlimited-emails',
764
					),
765
					'lite'  => true,
766
				),
767
			),
768
			'Form Appearance' => array(
769
				array(
770
					'label' => 'Create Multiple styles for different forms',
771
					'link'  => array(
772
						'content' => 'multiple-styles',
773
						'param'   => 'wordpress-visual-form-styler',
774
					),
775
					'lite'  => false,
776
				),
777
				array(
778
					'label' => 'Customizable layout with CSS classes',
779
					'link'  => array(
780
						'content' => 'form-layout',
781
						'param'   => 'wordpress-mobile-friendly-forms',
782
					),
783
					'lite'  => true,
784
				),
785
				array(
786
					'label' => 'Customize the HTML for your forms',
787
					'link'  => array(
788
						'content' => 'custom-html',
789
						'param'   => 'customizable-html-wordpress-form',
790
					),
791
					'lite'  => true,
792
				),
793
				array(
794
					'label' => 'Style your form with the Visual Form Styler',
795
					'lite'  => true,
796
				),
797
			),
798
		);
799
800
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/upgrade_to_pro.php' );
801
	}
802
803
	/**
804
	 * Install Pro after connection with Formidable.
805
	 *
806
	 * @since 4.02.05
807
	 */
808
	public static function connect_pro() {
809
		FrmAppHelper::permission_check( 'install_plugins' );
810
		check_ajax_referer( 'frm_ajax', 'nonce' );
811
812
		$url = FrmAppHelper::get_post_param( 'plugin', '', 'sanitize_text_field' );
813
		if ( FrmAppHelper::pro_is_installed() || empty( $url ) ) {
814
			wp_die();
815
		}
816
817
		$response = array();
818
819
		// It's already installed and active.
820
		$active = activate_plugin( 'formidable-pro/formidable-pro.php', false, false, true );
821
		if ( is_wp_error( $active ) ) {
822
			// The plugin was installed, but not active. Download it now.
823
			self::ajax_install_addon();
824
		} else {
825
			$response['active']  = true;
826
			$response['success'] = true;
827
		}
828
829
		echo json_encode( $response );
830
		wp_die();
831
	}
832
833
	/**
834
	 * @since 3.04.02
835
	 */
836
	public static function ajax_install_addon() {
837
838
		self::install_addon_permissions();
839
840
		self::download_and_activate();
841
842
		// Send back a response.
843
		echo json_encode( __( 'Your plugin has been installed. Please reload the page to see more options.', 'formidable' ) );
844
		wp_die();
845
	}
846
847
	/**
848
	 * @since 4.08
849
	 */
850
	private static function download_and_activate() {
851
		// Set the current screen to avoid undefined notices.
852
		if ( is_admin() ) {
853
			global $hook_suffix;
854
			set_current_screen();
855
		}
856
857
		self::maybe_show_cred_form();
858
859
		$installed = self::install_addon();
860
		if ( is_array( $installed ) && isset( $installed['message'] ) ) {
861
			return $installed;
862
		}
863
		self::maybe_activate_addon( $installed );
864
	}
865
866
	/**
867
	 * @since 3.04.02
868
	 */
869
	private static function maybe_show_cred_form() {
870
		if ( ! function_exists( 'request_filesystem_credentials' ) ) {
871
			include_once( ABSPATH . 'wp-admin/includes/file.php' );
872
		}
873
874
		// Start output bufferring to catch the filesystem form if credentials are needed.
875
		ob_start();
876
877
		$show_form = false;
878
		$method    = '';
879
		$url       = add_query_arg( array( 'page' => 'formidable-settings' ), admin_url( 'admin.php' ) );
880
		$url       = esc_url_raw( $url );
881
		$creds     = request_filesystem_credentials( $url, $method, false, false, null );
882
883
		if ( false === $creds ) {
884
			$show_form = true;
885
		} elseif ( ! WP_Filesystem( $creds ) ) {
886
			request_filesystem_credentials( $url, $method, true, false, null );
887
			$show_form = true;
888
		}
889
890
		if ( $show_form ) {
891
			$form     = ob_get_clean();
892
			$message  = __( 'Sorry, your site requires FTP authentication. Please download plugins from FormidableForms.com and install them manually.', 'formidable' );
893
			$data     = $form;
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
894
			$response = array(
895
				'success' => false,
896
				'message' => $message,
897
				'form'    => $form,
898
			);
899
			wp_send_json( $response );
900
		}
901
902
		ob_end_clean();
903
	}
904
905
	/**
906
	 * We do not need any extra credentials if we have gotten this far,
907
	 * so let's install the plugin.
908
	 *
909
	 * @since 3.04.02
910
	 */
911
	private static function install_addon() {
912
		require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
913
914
		$download_url = FrmAppHelper::get_param( 'plugin', '', 'post', 'esc_url_raw' );
915
916
		// Create the plugin upgrader with our custom skin.
917
		$installer = new Plugin_Upgrader( new FrmInstallerSkin() );
918
		$installer->install( $download_url );
919
920
		// Flush the cache and return the newly installed plugin basename.
921
		wp_cache_flush();
922
923
		$plugin = $installer->plugin_info();
924
		if ( empty( $plugin ) ) {
925
			return array(
926
				'message' => 'Plugin was not installed. ' . $installer->result,
927
				'success' => false,
928
			);
929
		}
930
		return $plugin;
931
	}
932
933
	/**
934
	 * @since 3.06.03
935
	 */
936
	public static function ajax_activate_addon() {
937
938
		self::install_addon_permissions();
939
940
		// Set the current screen to avoid undefined notices.
941
		global $hook_suffix;
942
		set_current_screen();
943
944
		$plugin = FrmAppHelper::get_param( 'plugin', '', 'post', 'sanitize_text_field' );
945
		self::maybe_activate_addon( $plugin );
946
947
		// Send back a response.
948
		echo json_encode( __( 'Your plugin has been activated. Please reload the page to see more options.', 'formidable' ) );
949
		wp_die();
950
	}
951
952
	/**
953
	 * @since 4.06.02
954
	 */
955
	public static function ajax_multiple_addons() {
956
		self::install_addon_permissions();
957
958
		// Set the current screen to avoid undefined notices.
959
		global $hook_suffix;
960
		set_current_screen();
961
962
		$download_urls = FrmAppHelper::get_param( 'plugin', '', 'post' );
963
		$download_urls = explode( ',', $download_urls );
964
		FrmAppHelper::sanitize_value( 'esc_url_raw', $download_urls );
965
966
		foreach ( $download_urls as $download_url ) {
967
			$_POST['plugin'] = $download_url;
968
			if ( strpos( $download_url, 'http' ) !== false ) {
969
				// Installing.
970
				self::maybe_show_cred_form();
971
972
				$installed = self::install_addon();
973
				self::maybe_activate_addon( $installed );
974
			} else {
975
				// Activating.
976
				self::maybe_activate_addon( $download_url );
977
			}
978
		}
979
980
		echo json_encode( __( 'Your plugins have been installed and activated.', 'formidable' ) );
981
982
		wp_die();
983
	}
984
985
	/**
986
	 * @since 3.04.02
987
	 * @param string $installed The plugin folder name with file name
988
	 */
989
	private static function maybe_activate_addon( $installed ) {
990
		if ( ! $installed ) {
991
			return;
992
		}
993
994
		$activate = activate_plugin( $installed );
995
		if ( is_wp_error( $activate ) ) {
996
			// Ignore the invalid header message that shows with nested plugins.
997
			if ( $activate->get_error_code() !== 'no_plugin_header' ) {
998
				if ( wp_doing_ajax() ) {
999
					echo json_encode( array( 'error' => $activate->get_error_message() ) );
1000
					wp_die();
1001
				}
1002
				return array(
1003
					'message' => $activate->get_error_message(),
1004
					'success' => false,
1005
				);
1006
			}
1007
		}
1008
	}
1009
1010
	/**
1011
	 * Run security checks before installing
1012
	 *
1013
	 * @since 3.04.02
1014
	 */
1015
	private static function install_addon_permissions() {
1016
		check_ajax_referer( 'frm_ajax', 'nonce' );
1017
1018
		if ( ! current_user_can( 'activate_plugins' ) || ! isset( $_POST['plugin'] ) ) {
1019
			echo json_encode( true );
1020
			wp_die();
1021
		}
1022
	}
1023
1024
	/**
1025
	 * @since 4.08
1026
	 */
1027
	public static function connect_link() {
1028
		$auth = get_option( 'frm_connect_token' );
1029
		if ( empty( $auth ) ) {
1030
			$auth = hash( 'sha512', wp_rand() );
1031
			update_option( 'frm_connect_token', $auth );
1032
		}
1033
		$link = FrmAppHelper::admin_upgrade_link( 'connect', 'api-connect' );
1034
		$args = array(
1035
			'v'       => 2,
1036
			'siteurl' => FrmAppHelper::site_url(),
1037
			'url'     => get_rest_url(),
1038
			'token'   => $auth,
1039
			'l'       => self::get_pro_license(),
1040
		);
1041
1042
		return add_query_arg( $args, $link );
1043
	}
1044
1045
	/**
1046
	 * Check the auth value for install permission.
1047
	 *
1048
	 * @since 4.08
1049
	 *
1050
	 * @return bool
1051
	 */
1052
	public static function can_install_addon_api() {
1053
		// Verify params present (auth & download link).
1054
		$post_auth = FrmAppHelper::get_param( 'token', '', 'request', 'sanitize_text_field' );
1055
		$post_url  = FrmAppHelper::get_param( 'file_url', '', 'request', 'sanitize_text_field' );
1056
1057
		if ( empty( $post_auth ) || empty( $post_url ) ) {
1058
			return false;
1059
		}
1060
1061
		// Verify auth.
1062
		$auth = get_option( 'frm_connect_token' );
1063
		if ( empty( $auth ) || ! hash_equals( $auth, $post_auth ) ) {
1064
			return false;
1065
		}
1066
1067
		return true;
1068
	}
1069
1070
	/**
1071
	 * Install and/or activate the add-on file.
1072
	 *
1073
	 * @since 4.08
1074
	 */
1075
	public static function install_addon_api() {
1076
		// Sanitize when it's used to prevent double sanitizing.
1077
		// phpcs:ignore WordPress.Security.ValidatedSanitizedInput
1078
		$_POST['plugin'] = isset( $_REQUEST['file_url'] ) ? $_REQUEST['file_url'] : ''; // Set for later use
1079
1080
		$error = esc_html__( 'Could not install an upgrade. Please download from formidableforms.com and install manually.', 'formidable' );
0 ignored issues
show
Unused Code introduced by
$error is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1081
1082
		// Delete so cannot replay.
1083
		delete_option( 'frm_connect_token' );
1084
1085
		// It's already installed and active.
1086
		$active = activate_plugin( 'formidable-pro/formidable-pro.php', false, false, true );
1087
		if ( is_wp_error( $active ) ) {
1088
			// Download plugin now.
1089
			$response = self::download_and_activate();
1090
			if ( is_array( $response ) && isset( $response['success'] ) ) {
1091
				return $response;
1092
			}
1093
		}
1094
1095
		// If empty license, save it now.
1096
		if ( empty( self::get_pro_license() ) && function_exists( 'load_formidable_pro' ) ) {
1097
			load_formidable_pro();
1098
			$license = stripslashes( FrmAppHelper::get_param( 'key', '', 'request', 'sanitize_text_field' ) );
1099
			if ( empty( $license ) ) {
1100
				return array(
1101
					'success' => false,
1102
					'error'   => 'That site does not have a valid license key.',
1103
				);
1104
			}
1105
1106
			$response = FrmAddon::activate_license_for_plugin( $license, 'formidable_pro' );
1107
			if ( ! $response['success'] ) {
1108
				// Could not activate license.
1109
				return $response;
1110
			}
1111
		}
1112
1113
		return array(
1114
			'success' => true,
1115
		);
1116
	}
1117
1118
	/**
1119
	 * @since 3.04.03
1120
	 * @deprecated 3.06
1121
	 * @codeCoverageIgnore
1122
	 * @return array
1123
	 */
1124
	public static function error_for_license( $license ) {
1125
		return FrmDeprecated::error_for_license( $license );
1126
	}
1127
1128
	/**
1129
	 * @since 3.04.03
1130
	 * @deprecated 3.06
1131
	 * @codeCoverageIgnore
1132
	 */
1133
	public static function get_pro_updater() {
1134
		return FrmDeprecated::get_pro_updater();
1135
	}
1136
1137
	/**
1138
	 * @since 3.04.03
1139
	 * @deprecated 3.06
1140
	 * @codeCoverageIgnore
1141
	 *
1142
	 * @return array
1143
	 */
1144
	public static function get_addon_info( $license = '' ) {
1145
		return FrmDeprecated::get_addon_info( $license );
1146
	}
1147
1148
	/**
1149
	 * @since 3.04.03
1150
	 * @deprecated 3.06
1151
	 * @codeCoverageIgnore
1152
	 *
1153
	 * @return string
1154
	 */
1155
	public static function get_cache_key( $license ) {
1156
		return FrmDeprecated::get_cache_key( $license );
1157
	}
1158
1159
	/**
1160
	 * @since 3.04.03
1161
	 * @deprecated 3.06
1162
	 * @codeCoverageIgnore
1163
	 */
1164
	public static function reset_cached_addons( $license = '' ) {
1165
		FrmDeprecated::reset_cached_addons( $license );
1166
	}
1167
1168
	/**
1169
	 * @since 2.03.08
1170
	 * @deprecated 3.04.03
1171
	 * @codeCoverageIgnore
1172
	 *
1173
	 * @param boolean $return
1174
	 * @param string $package
1175
	 *
1176
	 * @return boolean
1177
	 */
1178
	public static function add_shorten_edd_filename_filter( $return, $package ) {
1179
		return FrmDeprecated::add_shorten_edd_filename_filter( $return, $package );
1180
	}
1181
1182
	/**
1183
	 * @since 2.03.08
1184
	 * @deprecated 3.04.03
1185
	 * @codeCoverageIgnore
1186
	 *
1187
	 * @param string $filename
1188
	 * @param string $ext
1189
	 *
1190
	 * @return string
1191
	 */
1192
	public static function shorten_edd_filename( $filename, $ext ) {
1193
		return FrmDeprecated::shorten_edd_filename( $filename, $ext );
1194
	}
1195
1196
	/**
1197
	 * @deprecated 3.04.03
1198
	 * @codeCoverageIgnore
1199
	 */
1200
	public static function get_licenses() {
1201
		FrmDeprecated::get_licenses();
1202
	}
1203
}
1204