Completed
Push — master ( 0c1501...0c5440 )
by Stephanie
02:47
created

FrmAddonsController::get_licenses()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 8
nop 0
dl 0
loc 20
rs 8.2222
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
		$site_url = 'https://formidableforms.com/';
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
			_e( 'There are no plugins on your site that require a license', 'formidable' );
28
			return;
29
		}
30
31
		$allow_autofill = self::allow_autofill();
32
33
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/settings.php' );
34
	}
35
36
	/**
37
	 * Don't allow subsite addon licenses to be fetched
38
	 * unless the current user has super admin permissions
39
	 *
40
	 * @since 2.03.10
41
	 */
42
	private static function allow_autofill() {
43
		$allow_autofill = FrmAppHelper::pro_is_installed();
44
		if ( $allow_autofill && is_multisite() ) {
45
			$sitewide_activated = get_site_option( 'frmpro-wpmu-sitewide' );
46
			if ( $sitewide_activated ) {
47
				$allow_autofill = current_user_can( 'setup_network' );
48
			}
49
		}
50
		return $allow_autofill;
51
	}
52
53
	private static function get_api_addons() {
54
		$addons = array(
55
			'formidable-pro' => array(
56
				'title'   => 'Formidable Pro',
57
				'link'    => 'pricing/',
58
				'docs'    => '',
59
				'file'    => 'formidable/pro',
60
				'excerpt' => 'Enhance your basic Formidable forms with a plethora of Pro field types and features. Create advanced forms and data-driven applications in minutes.',
61
			),
62
			'mailchimp' => array(
63
				'title'   => 'MailChimp',
64
				'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.',
65
			),
66
			'registration' => array(
67
				'title'   => 'User Registration',
68
				'link'    => 'downloads/user-registration/',
69
				'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.',
70
			),
71
			'paypal' => array(
72
				'title'   => 'PayPal Standard',
73
				'link'    => 'downloads/paypal-standard/',
74
				'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.',
75
			),
76
			'stripe' => array(
77
				'title'   => 'Stripe',
78
				'docs'    => 'stripe/',
79
				'excerpt' => 'Any Formidable forms on your site can accept credit card payments without users ever leaving your site.',
80
			),
81
			'authorize-net' => array(
82
				'title'   => 'Authorize.net AIM',
83
				'link'    => 'downloads/authorize-net-aim/',
84
				'docs'    => 'authorize-net-aim/',
85
				'excerpt' => 'Accept one-time payments directly on your site, using Authorize.net AIM.',
86
			),
87
			'woocommerce' => array(
88
				'title'   => 'WooCommerce',
89
				'excerpt' => 'Use a Formidable form on your WooCommerce product pages.',
90
			),
91
			'autoresponder' => array(
92
				'title'   => 'Form Action Automation',
93
				'docs'    => 'schedule-autoresponder/',
94
				'excerpt' => 'Schedule email notifications, SMS messages, and API actions.',
95
			),
96
			'modal' => array(
97
				'title'   => 'Bootstrap Modal',
98
				'link'    => 'downloads/bootstrap-modal/',
99
				'docs'    => 'bootstrap-modal/',
100
				'excerpt' => 'Open a view or form in a Bootstrap popup.',
101
			),
102
			'bootstrap' => array(
103
				'title'   => 'Bootstrap',
104
				'excerpt' => 'Instantly add Bootstrap styling to all your Formidable forms.',
105
			),
106
			'zapier' => array(
107
				'title'   => 'Zapier',
108
				'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.',
109
			),
110
			'signature' => array(
111
				'title'   => 'Signature',
112
				'excerpt' => 'Add a signature field to your form. The user may write their signature with a trackpad/mouse or just type it.',
113
			),
114
			'api' => array(
115
				'title'   => 'Formidable API',
116
				'link'    => 'downloads/formidable-api/',
117
				'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.',
118
			),
119
			'twilio' => array(
120
				'title'   => 'Twilio',
121
				'docs'    => 'twilio-add-on/',
122
				'excerpt' => 'Allow users to text their votes for polls created by Formidable Forms, or send SMS notifications when entries are submitted or updated.',
123
			),
124
			'aweber' => array(
125
				'title'   => 'AWeber',
126
				'excerpt' => 'Subscribe users to an AWeber mailing list when they submit a form. AWeber is a powerful email marketing service.',
127
			),
128
			'highrise' => array(
129
				'title'   => 'Highrise',
130
				'excerpt' => 'Add your leads to your Highrise CRM account any time a Formidable form is submitted.',
131
			),
132
			'wpml' => array(
133
				'title'   => 'WP Multilingual',
134
				'link'    => 'downloads/wp-multilingual/',
135
				'docs'    => 'formidable-multi-language/',
136
				'excerpt' => 'Translate your forms into multiple languages using the Formidable-integrated WPML plugin.',
137
			),
138
			'polylang' => array(
139
				'title'   => 'Polylang',
140
				'excerpt' => 'Create bilingual or multilingual forms with help from Polylang.',
141
			),
142
			'math-captcha' => array(
143
				'title'   => 'Math Captcha',
144
				'excerpt' => 'Require users to perform a simple calculation before submitting a form to prevent spam. This add-on extends BestWebSoft\'s Captcha plugin.',
145
			),
146
			'locations' => array(
147
				'title'   => 'Locations',
148
				'excerpt' => 'Populate fields with Countries, States/Provinces, U.S. Counties, and U.S. Cities. This data can then be used in dependent Data from Entries fields.',
149
			),
150
			'user-tracking' => array(
151
				'title'   => 'User Tracking',
152
				'excerpt' => 'Track which pages a user visits prior to submitting a form.',
153
			),
154
		);
155
156
		return $addons;
157
	}
158
159
	private static function prepare_addons( &$addons ) {
160
		$activate_url = '';
161
		if ( current_user_can( 'activate_plugins' ) ) {
162
			$activate_url = add_query_arg( array( 'action' => 'activate' ), admin_url( 'plugins.php' ) );
163
		}
164
165
		$loop_addons = $addons;
166
		foreach ( $loop_addons as $slug => $addon ) {
167
			if ( isset( $addon['file'] ) ) {
168
				$base_file = $addon['file'];
169
			} else {
170
				$base_file = 'formidable-' . $slug;
171
			}
172
			$file = WP_PLUGIN_DIR . '/' . $base_file;
173
174
			$addon['installed'] = is_dir( $file );
175
			$addon['activate_url'] = '';
176
			if ( $addon['installed'] && ! empty( $activate_url ) ) {
177
				if ( file_exists( $file . '/' . $base_file . '.php' ) ) {
178
					$file_name = $base_file . '/' . $base_file . '.php';
179
					if ( ! is_plugin_active( $file_name ) ) {
180
						$addon['activate_url'] = add_query_arg( array(
181
							'_wpnonce'    => wp_create_nonce( 'activate-plugin_' . $file_name ),
182
							'plugin'      => $file_name,
183
						), $activate_url );
184
					}
185
				}
186
			}
187
188
			if ( ! isset( $addon['docs'] ) ) {
189
				$addon['docs'] = 'formidable-' . $slug . '/';
190
			}
191
192
			if ( ! isset( $addon['link'] ) ) {
193
				$addon['link'] = 'downloads/' . $slug . '/';
194
			}
195
			$addon['link'] = FrmAppHelper::make_affiliate_url( $addon['link'] );
196
197
			$addons[ $slug ] = $addon;
198
		}
199
	}
200
201
	public static function get_licenses() {
202
		$allow_autofill = self::allow_autofill();
203
		$required_role = $allow_autofill ? 'setup_network' : 'frm_change_settings';
204
		FrmAppHelper::permission_check( $required_role );
205
		check_ajax_referer( 'frm_ajax', 'nonce' );
206
207
		if ( is_multisite() && get_site_option( 'frmpro-wpmu-sitewide' ) ) {
208
			$license = get_site_option( 'frmpro-credentials' );
209
		} else {
210
			$license = get_option( 'frmpro-credentials' );
211
		}
212
213
		if ( $license && is_array( $license ) && isset( $license['license'] ) ) {
214
			$url = 'https://formidableforms.com/frm-edd-api/licenses?l=' . urlencode( base64_encode( $license['license'] ) );
215
			$licenses = self::send_api_request( $url, array( 'name' => 'frm_api_licence', 'expires' => 60 * 60 * 5 ) );
216
			echo json_encode( $licenses );
217
		}
218
219
		wp_die();
220
	}
221
222
	private static function send_api_request( $url, $transient = array() ) {
223
		$data = get_transient( $transient['name'] );
224
		if ( $data !== false ) {
225
			return $data;
226
		}
227
228
		$arg_array = array(
229
			'body'      => array(
230
				'url'   => home_url(),
231
			),
232
			'timeout'   => 15,
233
			'sslverify' => false,
234
			'user-agent' => 'Formidable/' . FrmAppHelper::$plug_version . '; ' . home_url(),
235
		);
236
237
		$response = wp_remote_post( $url, $arg_array );
238
		$body = wp_remote_retrieve_body( $response );
239
		$data = false;
240
		if ( ! is_wp_error( $response ) && ! is_wp_error( $body ) ) {
241
			$data = json_decode( $body, true );
242
			set_transient( $transient['name'], $data, $transient['expires'] );
243
		}
244
245
		return $data;
246
	}
247
248
	public static function upgrade_to_pro() {
249
		$pro_pricing = self::prepare_pro_info();
250
251
		include( FrmAppHelper::plugin_path() . '/classes/views/addons/upgrade_to_pro.php' );
252
	}
253
254
	private static function prepare_pro_info() {
255
		$pro_pricing = array(
256
			'personal'      => array( 'id' => 5, 'price' => '49.00', 'name' => 'Personal' ),
257
			'professional'  => array( 'id' => 6, 'price' => '99.00', 'name' => 'Professional' ),
258
			'smallbusiness' => array( 'id' => 3, 'price' => '199.00', 'name' => 'Small Business' ),
259
			'enterprise'    => array( 'id' => 4, 'price' => '399.00', 'name' => 'Enterprise' ),
260
		);
261
262
		return $pro_pricing;
263
	}
264
265
	/**
266
	 * Add a filter to shorten the EDD filename for Formidable plugin, and add-on, updates
267
	 *
268
	 * @since 2.03.08
269
	 *
270
	 * @param boolean $return
271
	 * @param string $package
272
	 *
273
	 * @return boolean
274
	 */
275
	public static function add_shorten_edd_filename_filter( $return, $package ) {
276
		if ( strpos( $package, '/edd-sl/package_download/' ) !== false && strpos( $package, 'formidableforms.com' ) !== false ) {
277
			add_filter( 'wp_unique_filename', 'FrmAddonsController::shorten_edd_filename', 10, 2 );
278
		}
279
280
		return $return;
281
	}
282
283
	/**
284
	 * Shorten the EDD filename for automatic updates
285
	 * Decreases size of file path so file path limit is not hit on Windows servers
286
	 *
287
	 * @since 2.03.08
288
	 *
289
	 * @param string $filename
290
	 * @param string $ext
291
	 *
292
	 * @return string
293
	 */
294
	public static function shorten_edd_filename( $filename, $ext ) {
295
		$filename = substr( $filename, 0, 50 ) . $ext;
296
		remove_filter( 'wp_unique_filename', 'FrmAddonsController::shorten_edd_filename', 10 );
297
298
		return $filename;
299
	}
300
}
301