Completed
Push — master ( d52ec6...f57973 )
by Stephanie
02:32
created

FrmAddon::edd_plugin_updater()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 5
nop 0
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	die( 'You are not allowed to call this page directly.' );
5
}
6
7
class FrmAddon {
8
	public $store_url = 'https://formidableforms.com';
9
	public $download_id;
10
	public $plugin_file;
11
	public $plugin_folder;
12
	public $plugin_name;
13
	public $plugin_slug;
14
	public $option_name;
15
	public $version;
16
	public $author = 'Strategy11';
17
	private $license;
18
19
	public function __construct() {
20
21
		if ( empty( $this->plugin_slug ) ) {
22
			$this->plugin_slug = preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->plugin_name ) ) );
23
		}
24
		if ( empty( $this->option_name ) ) {
25
			$this->option_name = 'edd_' . $this->plugin_slug . '_license_';
26
		}
27
28
		$this->plugin_folder = plugin_basename( $this->plugin_file );
29
		$this->license = $this->get_license();
30
31
		add_filter( 'frm_installed_addons', array( &$this, 'insert_installed_addon' ) );
32
		$this->edd_plugin_updater();
33
	}
34
35
	public static function load_hooks() {
36
		add_filter( 'frm_include_addon_page', '__return_true' );
37
		//new static();
38
	}
39
40
	public function insert_installed_addon( $plugins ) {
41
		$plugins[ $this->plugin_slug ] = $this;
42
		return $plugins;
43
	}
44
45
	public static function get_addon( $plugin_slug ) {
46
		$plugins = apply_filters( 'frm_installed_addons', array() );
47
		$plugin = false;
48
		if ( isset( $plugins[ $plugin_slug ] ) ) {
49
			$plugin = $plugins[ $plugin_slug ];
50
		}
51
		return $plugin;
52
	}
53
54
	public function edd_plugin_updater() {
55
56
		$this->is_license_revoked();
57
		$license = $this->license;
58
59
		if ( empty( $license ) ) {
60
			add_action( 'after_plugin_row_' . plugin_basename( $this->plugin_file ), array( $this, 'show_license_message' ), 10, 2 );
61
		} else {
62
63
			// setup the updater
64
			$api_data = array(
65
				'version' 	=> $this->version,
66
				'license' 	=> $license,
67
				'author' 	=> $this->author,
68
			);
69
			if ( is_numeric( $this->download_id ) ) {
70
				$api_data['item_id'] = $this->download_id;
71
			}
72
73
			$edd = new FrmEDD_SL_Plugin_Updater( $this->store_url, $this->plugin_file, $api_data );
74
			if ( $this->plugin_folder == 'formidable/formidable.php' ) {
75
				remove_filter( 'plugins_api', array( $edd, 'plugins_api_filter' ), 10, 3 );
76
			}
77
78
			add_filter( 'site_transient_update_plugins', array( &$this, 'clear_expired_download' ) );
79
		}
80
	}
81
82
	public function get_license() {
83
		$license = trim( get_option( $this->option_name . 'key' ) );
84
		if ( empty( $license ) ) {
85
			$license = $this->activate_defined_license();
86
		}
87
		return $license;
88
	}
89
90
	/**
91
	 * Activate the license in wp-config.php
92
	 * @since 2.04
93
	 */
94
	public function activate_defined_license() {
95
		$license = $this->get_defined_license();
96
		if ( ! empty( $license ) && ! $this->is_active() && $this->is_time_to_auto_activate() ) {
97
			$response = $this->activate_license( $license );
98
			$this->set_auto_activate_time();
99
			if ( ! $response['success'] ) {
100
				$license = '';
101
			}
102
		}
103
		return $license;
104
	}
105
106
	/**
107
	 * Check the wp-config.php for the license key
108
	 * @since 2.04
109
	 */
110
	public function get_defined_license() {
111
		$consant_name = 'FRM_' . strtoupper( $this->plugin_slug ) . '_LICENSE';
112
		return defined( $consant_name ) ? constant( $consant_name ) : false;
113
	}
114
115
	public function set_license( $license ) {
116
		update_option( $this->option_name . 'key', $license );
117
	}
118
119
	/**
120
	 * If the license is in the config, limit the frequency of checks.
121
	 * The license may be entered incorrectly, so we don't want to check on every page load.
122
	 * @since 2.04
123
	 */
124
	private function is_time_to_auto_activate() {
125
		$last_try = get_option( $this->option_name .'last_activate' );
126
		return ( ! $last_try || $last_try < strtotime('-1 day') );
127
	}
128
129
	private function set_auto_activate_time() {
130
		update_option( $this->option_name . 'last_activate', time() );
131
	}
132
133
	public function is_active() {
134
		return get_option( $this->option_name . 'active' );
135
	}
136
137
	public function clear_license() {
138
		delete_option( $this->option_name . 'active' );
139
		delete_option( $this->option_name . 'key' );
140
		delete_site_transient( $this->transient_key() );
141
		delete_transient('frm_api_licence');
142
	}
143
144
	public function set_active( $is_active ) {
145
		update_option( $this->option_name . 'active', $is_active );
146
		delete_transient('frm_api_licence');
147
	}
148
149
	public function show_license_message( $file, $plugin ) {
150
		$wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
151
		echo '<tr class="plugin-update-tr active"><td colspan="' . esc_attr( $wp_list_table->get_column_count() ) . '" class="plugin-update colspanchange"><div class="update-message">';
152
		echo sprintf( __( 'Your %1$s license key is missing. Please add it on the %2$slicenses page%3$s.', 'formidable' ), $this->plugin_name, '<a href="' . esc_url( admin_url('admin.php?page=formidable-settings&t=licenses_settings' ) ) . '">', '</a>' );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
153
		$id = sanitize_title( $plugin['Name'] );
154
		echo '<script type="text/javascript">var d = document.getElementById("' . esc_attr( $id ) . '");if ( d !== null ){ d.className = d.className + " update"; }</script>';
155
		echo '</div></td></tr>';
156
	}
157
158
	public function clear_expired_download( $transient ) {
159
		if ( ! is_object( $transient ) ) {
160
			return $transient;
161
		}
162
163
		if ( $this->is_current_version( $transient ) ) {
164
			//make sure it doesn't show there is an update if plugin is up-to-date
165
			if ( isset( $transient->response[ $this->plugin_folder ] ) ) {
166
				unset( $transient->response[ $this->plugin_folder ] );
167
			}
168
		} else if ( isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) ) {
169
			$cache_key = $this->version_cache_key();
170
			$version_info = get_transient( $cache_key );
171
172
			$this->clear_old_plugin_version( $version_info );
173
174
			if ( $version_info !== false && version_compare( $version_info->new_version, $this->version, '>' ) ) {
175
				$transient->response[ $this->plugin_folder ] = $version_info;
176
			} else {
177
				delete_transient( $cache_key );
178
				if ( ! $this->has_been_cleared() ) {
179
					// if the transient has expired, clear the update and trigger it again
180
					$this->cleared_plugins();
181
					$this->manually_queue_update();
182
				}
183
184
				unset( $transient->response[ $this->plugin_folder ] );
185
			}
186
		}
187
188
		return $transient;
189
	}
190
191
192
	/**
193
	 * @since 2.05.05
194
	 */
195
	private function version_cache_key() {
196
		return 'edd_plugin_' . md5( sanitize_key( $this->license . $this->version ) . '_get_version' );
197
	}
198
199
	/**
200
	 * make sure transients don't stick around on sites that
201
	 * don't save the transient expiration
202
	 *
203
	 * @since 2.05.05
204
	 */
205
	private function clear_old_plugin_version( &$version_info ) {
206
		if ( $version_info !== false ) {
207
208
			$cache_key = $this->version_cache_key();
209
			$expiration = get_option( '_transient_timeout_' . $cache_key );
210
211
			if ( $expiration === false ) {
212
				$last_checked = ( is_array( $version_info->sections ) && isset( $version_info->sections['last_checked'] ) ) ? $version_info->sections['last_checked'] : 0;
213
214
				if ( $last_checked < strtotime('-48 hours') ) {
215
					$version_info = false;
216
				}
217
			}
218
		}
219
	}
220
221
	private function is_current_version( $transient ) {
222
		if ( empty( $transient->checked ) || ! isset( $transient->checked[ $this->plugin_folder ] ) ) {
223
			return false;
224
		}
225
226
		$response = ! isset( $transient->response ) || empty( $transient->response );
227
		if ( $response ) {
228
			return true;
229
		}
230
231
		return isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) && $transient->checked[ $this->plugin_folder ] == $transient->response[ $this->plugin_folder ]->new_version;
232
	}
233
234
	private function has_been_cleared() {
235
		$last_cleared = get_option( 'frm_last_cleared' );
236
		return ( $last_cleared && $last_cleared > date( 'Y-m-d H:i:s', strtotime('-5 minutes') ) );
237
	}
238
239
	private function cleared_plugins() {
240
		update_option( 'frm_last_cleared', date('Y-m-d H:i:s') );
241
	}
242
243
	private function is_license_revoked() {
244
		if ( empty( $this->license ) || empty( $this->plugin_slug ) || isset( $_POST['license'] ) ) {
245
			return;
246
		}
247
248
		$last_checked = get_site_option( $this->transient_key() );
249
		$seven_days_ago = date( 'Y-m-d H:i:s', strtotime('-7 days') );
250
251
		if ( ! $last_checked || $last_checked < $seven_days_ago ) {
252
			update_site_option( $this->transient_key(), date( 'Y-m-d H:i:s' ) ); // check weekly
253
			$response = $this->get_license_status();
254
			if ( $response['status'] == 'revoked' ) {
255
				$this->clear_license();
256
			}
257
		}
258
	}
259
260
	private function transient_key() {
261
		return 'frm_' . md5( sanitize_key( $this->license . '_' . $this->plugin_slug ) );
262
	}
263
264
	public static function activate() {
265
		FrmAppHelper::permission_check('frm_change_settings');
266
	 	check_ajax_referer( 'frm_ajax', 'nonce' );
267
268
		if ( ! isset( $_POST['license'] ) || empty( $_POST['license'] ) ) {
269
			wp_die( json_encode( array(
270
				'message' => __( 'Oops! You forgot to enter your license number.', 'formidable' ),
271
				'success' => false,
272
			) ) );
273
		}
274
275
		$license = stripslashes( sanitize_text_field( $_POST['license'] ) );
276
		$plugin_slug = sanitize_text_field( $_POST['plugin'] );
277
		$this_plugin = self::get_addon( $plugin_slug );
278
		$response = $this_plugin->activate_license( $license );
279
280
		echo json_encode( $response );
281
		wp_die();
282
	}
283
284
	private function activate_license( $license ) {
285
		$this->set_license( $license );
286
		$this->license = $license;
287
288
		$response = $this->get_license_status();
289
		$response['message'] = '';
290
		$response['success'] = false;
291
292
		if ( $response['error'] ) {
293
			$response['message'] = $response['status'];
294
		} else {
295
			$messages = $this->get_messages();
296
			if ( is_string( $response['status'] ) && isset( $messages[ $response['status'] ] ) ) {
297
				$response['message'] = $messages[ $response['status'] ];
298
			} else {
299
				$response['message'] = FrmAppHelper::kses( $response['status'], array( 'a' ) );
300
			}
301
302
			$is_valid = false;
303
			if ( $response['status'] == 'valid' ) {
304
				$is_valid = 'valid';
305
				$response['success'] = true;
306
			}
307
			$this->set_active( $is_valid );
308
		}
309
310
		return $response;
311
	}
312
313
	private function get_license_status() {
314
		$response = array( 'status' => 'missing', 'error' => true );
315
		if ( empty( $this->license ) ) {
316
			$response['error'] = false;
317
			return $response;
318
		}
319
320
		try {
321
			$response['error'] = false;
322
			$license_data = $this->send_mothership_request( 'activate_license' );
323
324
			// $license_data->license will be either "valid" or "invalid"
325
			if ( is_array( $license_data ) ) {
326
				if ( in_array( $license_data['license'], array( 'valid', 'invalid' ) ) ) {
327
					$response['status'] = $license_data['license'];
328
				}
329
			} else {
330
				$response['status'] = $license_data;
331
			}
332
		} catch ( Exception $e ) {
333
			$response['status'] = $e->getMessage();
334
		}
335
336
		return $response;
337
	}
338
339
	private function get_messages() {
340
		return array(
341
			'valid'   => __( 'Your license has been activated. Enjoy!', 'formidable' ),
342
			'invalid' => __( 'That license key is invalid', 'formidable' ),
343
			'expired' => __( 'That license is expired', 'formidable' ),
344
			'revoked' => __( 'That license has been refunded', 'formidable' ),
345
			'no_activations_left' => __( 'That license has been used on too many sites', 'formidable' ),
346
			'invalid_item_id' => __( 'Oops! That is the wrong license key for this plugin.', 'formidable' ),
347
			'missing' => __( 'That license key is invalid', 'formidable' ),
348
		);
349
	}
350
351
	public static function deactivate() {
352
		FrmAppHelper::permission_check('frm_change_settings');
353
		check_ajax_referer( 'frm_ajax', 'nonce' );
354
355
		$plugin_slug = sanitize_text_field( $_POST['plugin'] );
356
		$this_plugin = self::get_addon( $plugin_slug );
357
		$license = $this_plugin->get_license();
358
		$this_plugin->license = $license;
359
360
		$response = array( 'success' => false, 'message' => '' );
361
		try {
362
			// $license_data->license will be either "deactivated" or "failed"
363
			$license_data = $this_plugin->send_mothership_request( 'deactivate_license' );
364
			if ( is_array( $license_data ) && $license_data['license'] == 'deactivated' ) {
365
				$response['success'] = true;
366
				$response['message'] = __( 'That license was removed successfully', 'formidable' );
367
			} else {
368
				$response['message'] = __( 'There was an error deactivating your license.', 'formidable' );
369
			}
370
		} catch ( Exception $e ) {
371
			$response['message'] = $e->getMessage();
372
		}
373
374
		$this_plugin->clear_license();
375
376
		echo json_encode( $response );
377
		wp_die();
378
	}
379
380
	public function send_mothership_request( $action ) {
381
		$api_params = array(
382
			'edd_action' => $action,
383
			'license'    => $this->license,
384
			'url'        => home_url(),
385
		);
386
		if ( is_numeric( $this->download_id ) ) {
387
			$api_params['item_id'] = absint( $this->download_id );
388
		} else {
389
			$api_params['item_name'] = urlencode( $this->plugin_name );
390
		}
391
392
		$arg_array = array(
393
			'body'      => $api_params,
394
			'timeout'   => 25,
395
			'sslverify' => false,
396
			'user-agent' => $this->plugin_slug . '/' . $this->version . '; ' . get_bloginfo( 'url' ),
397
		);
398
399
		$resp = wp_remote_post( $this->store_url, $arg_array );
400
		$body = wp_remote_retrieve_body( $resp );
401
402
		$message = __( 'Your License Key was invalid', 'formidable' );
403
		if ( is_wp_error( $resp ) ) {
404
			$message = sprintf( __( 'You had an error communicating with the Formidable API. %1$sClick here%2$s for more information.', 'formidable' ), '<a href="https://formidableforms.com/knowledgebase/why-cant-i-activate-formidable-pro/" target="_blank">', '</a>');
405
			$message .= ' ' . $resp->get_error_message();
406
		} else if ( $body == 'error' || is_wp_error( $body ) ) {
407
			$message = __( 'You had an HTTP error connecting to the Formidable API', 'formidable' );
408
		} else {
409
			$json_res = json_decode( $body, true );
410
			if ( null !== $json_res ) {
411
				if ( is_array( $json_res ) && isset( $json_res['error'] ) ) {
412
					$message = $json_res['error'];
413
				} else {
414
					$message = $json_res;
415
				}
416
			} else if ( isset( $resp['response'] ) && isset( $resp['response']['code'] ) ) {
417
				$message = sprintf( __( 'There was a %1$s error: %2$s', 'formidable' ), $resp['response']['code'], $resp['response']['message'] . ' ' . $resp['body'] );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$resp'
Loading history...
418
			}
419
		}
420
421
		return $message;
422
	}
423
424
    public function manually_queue_update() {
425
        set_site_transient( 'update_plugins', null );
426
    }
427
}
428