Completed
Push — master ( 411acc...0c1501 )
by Stephanie
08:24
created

FrmAddonsController   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 277
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 277
rs 9.6
c 0
b 0
f 0
wmc 32
lcom 0
cbo 1

11 Methods

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