Completed
Push — master ( 3c6179...89260f )
by Stephanie
47:59
created

FrmAddonsController::maybe_activate_addon()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
class FrmAddonsController {
4
5
	public static function menu() {
6
		add_submenu_page( 'formidable', 'Formidable | ' . __( 'AddOns', 'formidable' ), __( 'AddOns', 'formidable' ), 'frm_view_forms', 'formidable-addons', 'FrmAddonsController::list_addons' );
7
8
		if ( ! FrmAppHelper::pro_is_installed() ) {
9
			add_submenu_page( 'formidable', 'Formidable | ' . __( 'Upgrade to Pro', 'formidable' ), __( 'Upgrade to Pro', 'formidable' ), 'frm_view_forms', 'formidable-pro-upgrade', 'FrmAddonsController::upgrade_to_pro' );
10
		}
11
	}
12
13
	public static function list_addons() {
14
		$installed_addons = apply_filters( 'frm_installed_addons', array() );
15
16
		$addons = self::get_api_addons();
17
		self::prepare_addons( $addons );
18
19
		$pricing = FrmAppHelper::admin_upgrade_link( 'addons' );
20
21
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/list.php' );
22
	}
23
24
	public static function license_settings() {
25
		$plugins = apply_filters( 'frm_installed_addons', array() );
26
		if ( empty( $plugins ) ) {
27
			esc_html_e( 'There are no plugins on your site that require a license', 'formidable' );
28
			return;
29
		}
30
31
		ksort( $plugins );
32
		$allow_autofill = self::allow_autofill();
33
34
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/settings.php' );
35
	}
36
37
	/**
38
	 * Don't allow subsite addon licenses to be fetched
39
	 * unless the current user has super admin permissions
40
	 *
41
	 * @since 2.03.10
42
	 */
43
	private static function allow_autofill() {
44
		$allow_autofill = FrmAppHelper::pro_is_installed();
45
		if ( $allow_autofill && is_multisite() ) {
46
			$sitewide_activated = get_site_option( 'frmpro-wpmu-sitewide' );
47
			if ( $sitewide_activated ) {
48
				$allow_autofill = current_user_can( 'setup_network' );
49
			}
50
		}
51
		return $allow_autofill;
52
	}
53
54
	private static function get_api_addons() {
55
		$addons = array();
56
		$url = 'https://formidableforms.com/wp-json/s11edd/v1/updates/';
57
		if ( FrmAppHelper::pro_is_installed() ) {
58
			$edd_update = new FrmProEddController();
59
			$license = $edd_update->get_license();
60
			if ( ! empty( $license ) ) {
61
				$url .= '?l=' . urlencode( base64_encode( $license ) );
62
			}
63
		}
64
65
		$response = wp_remote_get( $url,
66
			array(
67
				'timeout'     => 120,
68
				'httpversion' => '1.1',
69
			)
70
		);
71
		if ( is_array( $response ) && ! is_wp_error( $response ) ) {
72
		    $addons = $response['body'];
73
		}
74
75
		if ( ! empty( $addons ) ) {
76
			$addons = json_decode( $addons, true );
77
			$skip_categories = array( 'WordPress Form Templates', 'WordPress Form Style Templates' );
78
			foreach ( $addons as $k => $addon ) {
79
				$cats = array_intersect( $skip_categories, $addon['categories'] );
80
				if ( empty( $addon['excerpt'] ) || ! empty( $cats ) ) {
81
					unset( $addons[ $k ] );
82
				}
83
			}
84
			return $addons;
85
		}
86
87
		$addons = array(
88
			'formidable-pro' => array(
89
				'title'   => 'Formidable Pro',
90
				'link'    => 'pricing/',
91
				'docs'    => '',
92
				'excerpt' => 'Enhance your basic Formidable forms with a plethora of Pro field types and features. Create advanced forms and data-driven applications in minutes.',
93
			),
94
			'mailchimp' => array(
95
				'title'   => 'MailChimp Forms',
96
				'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.',
97
			),
98
			'registration' => array(
99
				'title'   => 'User Registration Forms',
100
				'link'    => 'downloads/user-registration/',
101
				'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.',
102
			),
103
			'paypal' => array(
104
				'title'   => 'PayPal Standard Forms',
105
				'link'    => 'downloads/paypal-standard/',
106
				'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.',
107
			),
108
			'stripe' => array(
109
				'title'   => 'Stripe Forms',
110
				'docs'    => 'knowledgebase/stripe/',
111
				'excerpt' => 'Any Formidable forms on your site can accept credit card payments without users ever leaving your site.',
112
			),
113
			'authorize-net' => array(
114
				'title'   => 'Authorize.net AIM Forms',
115
				'link'    => 'downloads/authorize-net-aim/',
116
				'docs'    => 'knowledgebase/authorize-net-aim/',
117
				'excerpt' => 'Accept one-time payments directly on your site, using Authorize.net AIM.',
118
			),
119
			'woocommerce' => array(
120
				'title'   => 'WooCommerce Forms',
121
				'excerpt' => 'Use a Formidable form on your WooCommerce product pages.',
122
			),
123
			'autoresponder' => array(
124
				'title'   => 'Form Action Automation',
125
				'docs'    => 'knowledgebase/schedule-autoresponder/',
126
				'excerpt' => 'Schedule email notifications, SMS messages, and API actions.',
127
			),
128
			'modal' => array(
129
				'title'   => 'Bootstrap Modal Forms',
130
				'link'    => 'downloads/bootstrap-modal/',
131
				'docs'    => 'knowledgebase/bootstrap-modal/',
132
				'excerpt' => 'Open a view or form in a Bootstrap popup.',
133
			),
134
			'bootstrap' => array(
135
				'title'   => 'Bootstrap Style Forms',
136
				'excerpt' => 'Instantly add Bootstrap styling to all your Formidable forms.',
137
			),
138
			'zapier' => array(
139
				'title'   => 'Zapier Forms',
140
				'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.',
141
			),
142
			'signature' => array(
143
				'title'   => 'Digital Signature Forms',
144
				'excerpt' => 'Add a signature field to your form. The user may write their signature with a trackpad/mouse or just type it.',
145
			),
146
			'api' => array(
147
				'title'   => 'Formidable Forms API',
148
				'link'    => 'downloads/formidable-api/',
149
				'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.',
150
			),
151
			'twilio' => array(
152
				'title'   => 'Twilio SMS Forms',
153
				'docs'    => 'knowledgebase/twilio-add-on/',
154
				'excerpt' => 'Allow users to text their votes for polls created by Formidable Forms, or send SMS notifications when entries are submitted or updated.',
155
			),
156
		);
157
158
		return $addons;
159
	}
160
161
	private static function prepare_addons( &$addons ) {
162
		$activate_url = '';
163
		if ( current_user_can( 'activate_plugins' ) ) {
164
			$activate_url = add_query_arg( array( 'action' => 'activate' ), admin_url( 'plugins.php' ) );
165
		}
166
167
		$loop_addons = $addons;
168
		foreach ( $loop_addons as $id => $addon ) {
169
			if ( is_numeric( $id ) ) {
170
				$slug = str_replace( array( '-wordpress-plugin', '-wordpress' ), '', $addon['slug'] );
171
				self::prepare_folder_name( $addon );
172
			} else {
173
				$slug = $id;
174
			}
175
			if ( isset( $addon['file'] ) ) {
176
				$base_file = $addon['file'];
177
			} else {
178
				$base_file = 'formidable-' . $slug;
179
			}
180
			$file = WP_PLUGIN_DIR . '/' . $base_file;
181
182
			$addon['installed'] = is_dir( $file );
183
			$addon['activate_url'] = '';
184
			if ( $addon['installed'] && ! empty( $activate_url ) ) {
185
				if ( file_exists( $file . '/' . $base_file . '.php' ) ) {
186
					$file_name = $base_file . '/' . $base_file . '.php';
187
					if ( ! is_plugin_active( $file_name ) ) {
188
						$addon['activate_url'] = add_query_arg(
189
							array(
190
								'_wpnonce'    => wp_create_nonce( 'activate-plugin_' . $file_name ),
191
								'plugin'      => $file_name,
192
							),
193
							$activate_url
194
						);
195
					}
196
				}
197
			}
198
199
			if ( ! isset( $addon['docs'] ) ) {
200
				$addon['docs'] = 'knowledgebase/formidable-' . $slug . '/';
201
			}
202
			self::prepare_addon_link( $addon['docs'] );
203
204
			if ( ! isset( $addon['link'] ) ) {
205
				$addon['link'] = 'downloads/' . $slug . '/';
206
			}
207
			self::prepare_addon_link( $addon['link'] );
208
209
			self::set_addon_status( $addon );
210
			$addons[ $id ] = $addon;
211
		}
212
	}
213
214
	/**
215
	 * @since 3.04.02
216
	 */
217
	private static function prepare_folder_name( &$addon ) {
218
		if ( isset( $addon['url'] ) ) {
219
			$url  = explode( '?', $addon['url'] );
220
			$file = explode( '/', $url[0] );
221
			$file = end( $file );
222
			$addon['file'] = str_replace( '-' . $addon['version'] . '.zip', '', $file );
223
		}
224
	}
225
226
	/**
227
	 * @since 3.04.02
228
	 */
229
	private static function prepare_addon_link( &$link ) {
230
		$site_url = 'https://formidableforms.com/';
231
		if ( strpos( $link, 'http' ) !== 0 ) {
232
			$link = $site_url . $link;
233
		}
234
		$link = FrmAppHelper::make_affiliate_url( $link );
235
		$query_args = array(
236
			'utm_source'   => 'WordPress',
237
			'utm_medium'   => 'addons',
238
			'utm_campaign' => 'liteplugin',
239
		);
240
		$link = add_query_arg( $query_args, $link );
241
	}
242
243
	/**
244
	 * Add the status to the addon array. Status options are:
245
	 * installed, active, not installed
246
	 *
247
	 * @since 3.04.02
248
	 */
249
	private static function set_addon_status( &$addon ) {
250
		if ( ! empty( $addon['activate_url'] ) ) {
251
			$addon['status'] = array(
252
				'type'  => 'installed',
253
				'label' => __( 'Installed', 'formidable' )
254
			);
255
		} elseif ( $addon['installed'] ) {
256
			$addon['status'] = array(
257
				'type'  => 'active',
258
				'label' => __( 'Active', 'formidable' ),
259
			);
260
		} else {
261
			$addon['status'] = array(
262
				'type'  => 'not-installed',
263
				'label' => __( 'Not Installed', 'formidable' ),
264
			);
265
		}
266
	}
267
268
	public static function get_licenses() {
269
		$allow_autofill = self::allow_autofill();
270
		$required_role = $allow_autofill ? 'setup_network' : 'frm_change_settings';
271
		FrmAppHelper::permission_check( $required_role );
272
		check_ajax_referer( 'frm_ajax', 'nonce' );
273
274
		if ( is_multisite() && get_site_option( 'frmpro-wpmu-sitewide' ) ) {
275
			$license = get_site_option( 'frmpro-credentials' );
276
		} else {
277
			$license = get_option( 'frmpro-credentials' );
278
		}
279
280
		if ( $license && is_array( $license ) && isset( $license['license'] ) ) {
281
			$url = 'https://formidableforms.com/frm-edd-api/licenses?l=' . urlencode( base64_encode( $license['license'] ) );
282
			$licenses = self::send_api_request(
283
				$url,
284
				array(
285
					'name'    => 'frm_api_licence',
286
					'expires' => 60 * 60 * 5,
287
				)
288
			);
289
			echo json_encode( $licenses );
290
		}
291
292
		wp_die();
293
	}
294
295
	private static function send_api_request( $url, $transient = array() ) {
296
		$data = get_transient( $transient['name'] );
297
		if ( $data !== false ) {
298
			return $data;
299
		}
300
301
		$arg_array = array(
302
			'body'      => array(
303
				'url'   => home_url(),
304
			),
305
			'timeout'   => 15,
306
			'user-agent' => 'Formidable/' . FrmAppHelper::$plug_version . '; ' . home_url(),
307
		);
308
309
		$response = wp_remote_post( $url, $arg_array );
310
		$body = wp_remote_retrieve_body( $response );
311
		$data = false;
312
		if ( ! is_wp_error( $response ) && ! is_wp_error( $body ) ) {
313
			$data = json_decode( $body, true );
314
			set_transient( $transient['name'], $data, $transient['expires'] );
315
		}
316
317
		return $data;
318
	}
319
320
	public static function upgrade_to_pro() {
321
		$pro_pricing = self::prepare_pro_info();
322
323
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/upgrade_to_pro.php' );
324
	}
325
326
	private static function prepare_pro_info() {
327
		return array(
328
			'personal'     => array(
329
				'id'       => 2,
330
				'download' => 19367654,
331
				'price'    => '49.00',
332
				'name'     => 'Personal',
333
			),
334
			'professional' => array(
335
				'id'       => 0,
336
				'download' => 19367001,
337
				'price'    => '99.00',
338
				'name'     => 'Creator',
339
			),
340
			'smallbusiness' => array(
341
				'id'       => 0,
342
				'download' => 19366995,
343
				'price'    => '199.00',
344
				'name'     => 'Business',
345
			),
346
			'enterprise'   => array(
347
				'id'       => 0,
348
				'download' => 19366992,
349
				'price'    => '399.00',
350
				'name'     => 'Enterprise',
351
			),
352
		);
353
	}
354
355
	/**
356
	 * Add a filter to shorten the EDD filename for Formidable plugin, and add-on, updates
357
	 *
358
	 * @since 2.03.08
359
	 *
360
	 * @param boolean $return
361
	 * @param string $package
362
	 *
363
	 * @return boolean
364
	 */
365
	public static function add_shorten_edd_filename_filter( $return, $package ) {
366
		if ( strpos( $package, '/edd-sl/package_download/' ) !== false && strpos( $package, 'formidableforms.com' ) !== false ) {
367
			add_filter( 'wp_unique_filename', 'FrmAddonsController::shorten_edd_filename', 10, 2 );
368
		}
369
370
		return $return;
371
	}
372
373
	/**
374
	 * Shorten the EDD filename for automatic updates
375
	 * Decreases size of file path so file path limit is not hit on Windows servers
376
	 *
377
	 * @since 2.03.08
378
	 *
379
	 * @param string $filename
380
	 * @param string $ext
381
	 *
382
	 * @return string
383
	 */
384
	public static function shorten_edd_filename( $filename, $ext ) {
385
		$filename = substr( $filename, 0, 50 ) . $ext;
386
		remove_filter( 'wp_unique_filename', 'FrmAddonsController::shorten_edd_filename', 10 );
387
388
		return $filename;
389
	}
390
391
	/**
392
	 * @since 3.04.02
393
	 */
394
	public static function ajax_install_addon() {
395
396
		self::install_addon_permissions();
397
398
		// Set the current screen to avoid undefined notices.
399
		global $hook_suffix;
400
		set_current_screen();
401
402
		self::maybe_show_cred_form();
403
404
		$installed = self::install_addon();
405
		self::maybe_activate_addon( $installed );
406
407
		// Send back a response.
408
		echo json_encode( true );
409
		wp_die();
410
	}
411
412
	/**
413
	 * @since 3.04.02
414
	 */
415
	private static function maybe_show_cred_form() {
416
		// Start output bufferring to catch the filesystem form if credentials are needed.
417
		ob_start();
418
419
		$show_form = false;
420
		$method = '';
421
		$url    = add_query_arg( array( 'page' => 'formidable-settings' ), admin_url( 'admin.php' ) );
422
		$url    = esc_url_raw( $url );
423
		$creds  = request_filesystem_credentials( $url, $method, false, false, null );
424
425
		if ( false === $creds ) {
426
			$show_form = true;
427
		} elseif ( ! WP_Filesystem( $creds ) ) {
428
			request_filesystem_credentials( $url, $method, true, false, null );
429
			$show_form = true;
430
		}
431
432
		if ( $show_form ) {
433
			$form = ob_get_clean();
0 ignored issues
show
Unused Code introduced by
$form 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...
434
			//TODO: test this: echo json_encode( array( 'form' => $form ) );
435
			echo json_encode( array( 'form' => __( 'Sorry, you\'re site requires FTP authentication. Please install plugins manaully.', 'formidable' ) ) );
436
			wp_die();
437
		}
438
439
		ob_end_clean();
440
	}
441
442
	/**
443
	 * We do not need any extra credentials if we have gotten this far,
444
	 * so let's install the plugin.
445
	 *
446
	 * @since 3.04.02
447
	 */
448
	private static function install_addon() {
449
		require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
450
451
		$download_url = esc_url_raw( $_POST['plugin'] );
452
453
		// Create the plugin upgrader with our custom skin.
454
		$installer = new Plugin_Upgrader( new FrmInstallerSkin() );
455
		$installer->install( $download_url );
456
457
		// Flush the cache and return the newly installed plugin basename.
458
		wp_cache_flush();
459
		return $installer->plugin_info();
460
	}
461
462
	/**
463
	 * @since 3.04.02
464
	 */
465
	private static function maybe_activate_addon( $installed ) {
466
		if ( ! $installed ) {
467
			return;
468
		}
469
470
		$activate = activate_plugin( $installed );
471
		if ( is_wp_error( $activate ) ) {
472
			echo json_encode( array( 'error' => $activate->get_error_message() ) );
473
			wp_die();
474
		}
475
	}
476
477
	/**
478
	 * Run security checks before installing
479
	 *
480
	 * @since 3.04.02
481
	 */
482
	private static function install_addon_permissions() {
483
		check_ajax_referer( 'frm_ajax', 'nonce' );
484
485
		if ( ! current_user_can( 'activate_plugins' ) || ! isset( $_POST['plugin'] ) ) {
486
			echo json_encode( true );
487
			wp_die();
488
		}
489
	}
490
}
491