Completed
Push — master ( 2cfa6f...8927a4 )
by Zack
10:00 queued 06:05
created

includes/class-gv-license-handler.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
class GV_License_Handler {
4
5
	/**
6
	 * @var GravityView_Settings
7
	 */
8
	private $Addon;
9
10
	const name = 'GravityView';
11
12
	const author = 'Katz Web Services, Inc.';
13
	
14
	const url = 'https://gravityview.co';
15
	
16
	const version = GravityView_Plugin::version;
17
18
	/**
19
	 * Post ID on gravityview.co
20
	 * @since 1.15
21
	 */
22
	const item_id = 17;
23
24
	/**
25
	 * Name of the transient used to store license status for GV
26
	 * @since 1.17
27
	 */
28
	const status_transient_key = 'gravityview_edd-activate_valid';
29
30
	private $EDD_SL_Plugin_Updater;
31
32
	/**
33
	 * @var GV_License_Handler
34
	 */
35
	public static $instance;
36
37
	/**
38
	 * @param GravityView_Settings $GFAddOn
39
	 *
40
	 * @return GV_License_Handler
41
	 */
42
	public static function get_instance( GravityView_Settings $GFAddOn ) {
43
		if( empty( self::$instance ) ) {
44
			self::$instance = new self( $GFAddOn );
45
		}
46
		return self::$instance;
47
	}
48
	
49
	private function __construct( GravityView_Settings $GFAddOn ) {
50
51
		$this->Addon = $GFAddOn;
52
53
		$this->setup_edd();
54
		
55
		$this->add_hooks();
56
	}
57
58
	private function add_hooks() {
59
		add_action( 'wp_ajax_gravityview_license', array( $this, 'license_call' ) );
60
		add_action( 'admin_init', array( $this, 'refresh_license_status' ) );
61
	}
62
63
	/**
64
	 * When the status transient expires (or is deleted on activation), re-check the status
65
	 *
66
	 * @since 1.17
67
	 *
68
	 * @return void
69
	 */
70
	public function refresh_license_status() {
71
72
		// Only perform on GravityView pages
73
		if( ! gravityview_is_admin_page() ) {
74
			return;
75
		}
76
77
		// The transient is fresh; don't fetch.
78
		if( $status = get_transient( self::status_transient_key ) ) {
79
			return;
80
		}
81
82
		$data = array(
83
			'edd_action' => 'check_license',
84
			'license' => trim( $this->Addon->get_app_setting( 'license_key' ) ),
85
			'update' => true,
86
			'format' => 'object',
87
			'field_id' => 'refresh_license_status', // Required to set the `status_transient_key` transient
88
		);
89
90
		$license_call = GravityView_Settings::get_instance()->get_license_handler()->license_call( $data );
91
92
		do_action( 'gravityview_log_debug', __METHOD__ . ': Refreshed the license.', $license_call );
93
	}
94
95
	function settings_edd_license_activation( $field, $echo ) {
96
97
		$script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
98
99
		wp_enqueue_script( 'gv-admin-edd-license', GRAVITYVIEW_URL . 'assets/js/admin-edd-license' . $script_debug . '.js', array( 'jquery' ) );
100
101
		$status = trim( $this->Addon->get_app_setting( 'license_key_status' ) );
102
		$key = trim( $this->Addon->get_app_setting( 'license_key' ) );
103
104
		if( !empty( $key ) ) {
105
			$response = $this->Addon->get_app_setting( 'license_key_response' );
106
			$response = is_array( $response ) ? (object) $response : json_decode( $response );
107
		} else {
108
			$response = array();
109
		}
110
111
		wp_localize_script( 'gv-admin-edd-license', 'GVGlobals', array(
112
			'license_box' => $this->get_license_message( $response )
113
		));
114
115
116
		$fields = array(
117
			array(
118
				'name'  => 'edd-activate',
119
				'value' => __('Activate License', 'gravityview'),
120
				'data-pending_text' => __('Verifying license&hellip;', 'gravityview'),
121
				'data-edd_action' => 'activate_license',
122
				'class' => 'button-primary',
123
			),
124
			array(
125
				'name'  => 'edd-deactivate',
126
				'value' => __('Deactivate License', 'gravityview'),
127
				'data-pending_text' => __('Deactivating license&hellip;', 'gravityview'),
128
				'data-edd_action' => 'deactivate_license',
129
				'class' => ( empty( $status ) ? 'button-primary hide' : 'button-primary' ),
130
			),
131
			array(
132
				'name'  => 'edd-check',
133
				'value' => __('Check License', 'gravityview'),
134
				'data-pending_text' => __('Verifying license&hellip;', 'gravityview'),
135
				'title' => 'Check the license before saving it',
136
				'data-edd_action' => 'check_license',
137
				'class' => 'button-secondary',
138
			),
139
		);
140
141
142
		$class = 'button gv-edd-action';
143
144
		$class .= ( !empty( $key ) && $status !== 'valid' ) ? '' : ' hide';
145
146
		$disabled_attribute = GVCommon::has_cap( 'gravityview_edit_settings' ) ? false : 'disabled';
147
148
		$submit = '<div class="gv-edd-button-wrapper">';
149
		foreach ( $fields as $field ) {
150
			$field['type'] = 'button';
151
			$field['class'] = isset( $field['class'] ) ? $field['class'] . ' '. $class : $class;
152
			$field['style'] = 'margin-left: 10px;';
153
			if( $disabled_attribute ) {
154
				$field['disabled'] = $disabled_attribute;
155
			}
156
			$submit .= $this->Addon->settings_submit( $field, $echo );
157
		}
158
		$submit .= '</div>';
159
160
		return $submit;
161
	}
162
163
	/**
164
	 * Include the EDD plugin updater class, if not exists
165
	 * @since 1.7.4
166
	 * @return void
167
	 */
168
	private function setup_edd() {
169
170
		if( !class_exists('EDD_SL_Plugin_Updater') ) {
171
			require_once( GRAVITYVIEW_DIR . 'includes/lib/EDD_SL_Plugin_Updater.php');
172
		}
173
174
		// setup the updater
175
		$this->EDD_SL_Plugin_Updater = new EDD_SL_Plugin_Updater(
176
			self::url,
177
			GRAVITYVIEW_FILE,
178
			$this->_get_edd_settings()
179
		);
180
181
	}
182
183
	/**
184
	 * Generate the array of settings passed to the EDD license call
185
	 *
186
	 * @since 1.7.4
187
	 *
188
	 * @param string $action The action to send to edd, such as `check_license`
189
	 * @param string $license The license key to have passed to EDD
190
	 *
191
	 * @return array
192
	 */
193
	function _get_edd_settings( $action = '', $license = '' ) {
194
195
		// retrieve our license key from the DB
196
		$license_key = empty( $license ) ? trim( $this->Addon->get_app_setting( 'license_key' ) ) : $license;
197
198
		$settings = array(
199
			'version'   => self::version,
200
			'license'   => $license_key,
201
			'item_name' => self::name,
202
			'item_id'   => self::item_id,
203
			'author'    => self::author,
204
			'language'  => get_locale(),
205
			'url'       => home_url(),
206
		);
207
208
		if( !empty( $action ) ) {
209
			$settings['edd_action'] = esc_attr( $action );
210
		}
211
212
		$settings = array_map( 'urlencode', $settings );
213
214
		return $settings;
215
	}
216
217
	/**
218
	 * Perform the call
219
	 * @return array|WP_Error
220
	 */
221
	private function _license_get_remote_response( $data, $license = '' ) {
222
223
		$api_params = $this->_get_edd_settings( $data['edd_action'], $license );
224
225
		$url = add_query_arg( $api_params, self::url );
226
227
		$response = wp_remote_get( $url, array(
228
			'timeout'   => 15,
229
			'sslverify' => false,
230
		));
231
232
		if ( is_wp_error( $response ) ) {
233
			return array();
234
		}
235
236
		$license_data = json_decode( wp_remote_retrieve_body( $response ) );
237
238
		// Not JSON
239
		if ( empty( $license_data ) ) {
240
241
			delete_transient( self::status_transient_key );
242
243
			// Change status
244
			return array();
245
		}
246
247
		// Store the license key inside the data array
248
		$license_data->license_key = $license;
249
250
		return $license_data;
251
	}
252
253
	/**
254
	 * Generate the status message displayed in the license field
255
	 *
256
	 * @since 1.7.4
257
	 * @param $license_data
258
	 *
259
	 * @return string
260
	 */
261
	function get_license_message( $license_data ) {
262
263
		if( empty( $license_data ) ) {
264
			$message = '';
265
		} else {
266
267
			if( ! empty( $license_data->error ) ) {
268
				$class = 'error';
269
				$string_key = $license_data->error;
270
			} else {
271
				$class = $license_data->license;
272
				$string_key = $license_data->license;
273
			}
274
275
			$message = sprintf( '<p><strong>%s: %s</strong></p>', $this->strings('status'), $this->strings( $string_key, $license_data ) );
276
277
			$message = $this->generate_license_box( $message, $class );
278
		}
279
280
		return $message;
281
	}
282
283
	/**
284
	 * Generate the status message box HTML based on the current status
285
	 *
286
	 * @since 1.7.4
287
	 * @param $message
288
	 * @param string $class
289
	 *
290
	 * @return string
291
	 */
292
	private function generate_license_box( $message, $class = '' ) {
293
294
		$template = '<div id="gv-edd-status" aria-live="polite" aria-busy="false" class="gv-edd-message inline %s">%s</div>';
295
296
		$output = sprintf( $template, esc_attr( $class ), $message );
297
298
		return $output;
299
	}
300
301
	/**
302
	 * Allow pure HTML in settings fields
303
	 *
304
	 * @since 1.17
305
	 *
306
	 * @param array $response License response
307
	 *
308
	 * @return string `html` key of the $field
309
	 */
310
	public function license_details( $response = array() ) {
311
312
		$response = (array) $response;
313
314
		$return = '';
315
		$wrapper = '<span class="gv-license-details" aria-live="polite" aria-busy="false">%s</span>';
316
317
		if( ! empty( $response['license_key'] ) ) {
318
319
			$return .= '<h3>' . esc_html__( 'License Details:', 'gravityview' ) . '</h3>';
320
321
			if ( in_array( rgar( $response, 'license' ), array( 'invalid', 'deactivated' ) ) ) {
322
				$return .= $this->strings( $response['license'], $response );
323
			} elseif ( ! empty( $response['license_name'] ) ) {
324
325
				$response_keys = array(
326
					'license_name'   => '',
327
					'license_limit'  => '',
328
					'customer_name'  => '',
329
					'customer_email' => '',
330
					'site_count'     => '',
331
					'expires'        => '',
332
					'upgrades'       => ''
333
				);
334
335
				// Make sure all the keys are set
336
				$response = wp_parse_args( $response, $response_keys );
337
338
				$login_link = sprintf( '<a href="%s" class="howto" rel="external">%s</a>', esc_url( sprintf( 'https://gravityview.co/wp-login.php?username=%s', $response['customer_email'] ) ), esc_html__( 'Access your GravityView account', 'gravityview' ) );
339
				$local_text = ( ! empty( $response['is_local'] ) ? '<span class="howto">' . __( 'This development site does not count toward license activation limits', 'gravityview' ) . '</span>' : '' );
340
				$details    = array(
341
					'license'     => sprintf( esc_html__( 'License level: %s', 'gravityview' ), esc_html( $response['license_name'] ), esc_html( $response['license_limit'] ) ),
342
					'licensed_to' => sprintf( esc_html_x( 'Licensed to: %1$s (%2$s)', '1: Customer name; 2: Customer email', 'gravityview' ), esc_html__( $response['customer_name'], 'gravityview' ), esc_html__( $response['customer_email'], 'gravityview' ) ) . $login_link,
343
					'activations' => sprintf( esc_html__( 'Activations: %d of %s sites', 'gravityview' ), intval( $response['site_count'] ), esc_html( $response['license_limit'] ) ) . $local_text,
344
					'expires'     => sprintf( esc_html__( 'Renew on: %s', 'gravityview' ), date_i18n( get_option( 'date_format' ), strtotime( $response['expires'] ) - DAY_IN_SECONDS ) ),
345
					'upgrade'     => $this->get_upgrade_html( $response['upgrades'] ),
346
				);
347
348
				if ( ! empty( $response['error'] ) && 'expired' === $response['error'] ) {
349
					unset( $details['upgrade'] );
350
					$details['expires'] = '<div class="error inline"><p>' . $this->strings( 'expired', $response ) . '</p></div>';
351
				}
352
353
				$return .= '<ul><li>' . implode( '</li><li>', array_filter( $details ) ) . '</li></ul>';
354
			}
355
		}
356
357
		return sprintf( $wrapper, $return );
358
	}
359
360
	/**
361
	 * Display possible upgrades for a license
362
	 *
363
	 * @since 1.17
364
	 *
365
	 * @param array $upgrades Array of upgrade paths, returned from the GV website
366
	 *
367
	 * @return string HTML list of upgrades available for the current license
368
	 */
369
	function get_upgrade_html( $upgrades ) {
370
371
		$output = '';
372
373
		if( ! empty( $upgrades ) ) {
374
375
			$locale_parts = explode( '_', get_locale() );
376
377
			$is_english = ( 'en' === $locale_parts[0] );
378
379
			$output .= '<h4>' . esc_html__( 'Upgrades available:', 'gravityview' ) . '</h4>';
380
381
			$output .= '<ul class="ul-disc">';
382
383
			foreach ( $upgrades as $upgrade_id => $upgrade ) {
384
385
				$upgrade = (object) $upgrade;
386
387
				$anchor_text = sprintf( esc_html_x( 'Upgrade to %1$s for %2$s', '1: GravityView upgrade name, 2: Cost of upgrade', 'gravityview' ), esc_attr( $upgrade->name ), esc_attr( $upgrade->price ) );
388
389
				if( $is_english && isset( $upgrade->description ) ) {
390
					$message = esc_html( $upgrade->description );
391
				} else {
392
					switch( $upgrade->price_id ) {
393
						// Interstellar
394
						case 1:
395
						default:
396
							$message = esc_html__( 'Get access to Extensions', 'gravityview' );
397
							break;
398
						// Galactic
399
						case 2:
400
							$message = esc_html__( 'Get access to Entry Importer and other Premium plugins', 'gravityview' );
401
							break;
402
					}
403
				}
404
405
				$output .= sprintf( '<li><a href="%s">%s</a><span class="howto">%s</span></li>', esc_url( add_query_arg( array( 'utm_source' => 'settings', 'utm_medium' => 'admin', 'utm_content' => 'license-details', 'utm_campaign' => 'Upgrades' ), $upgrade->url ) ), $anchor_text, $message );
406
			}
407
			$output .= '</ul>';
408
		}
409
410
		return $output;
411
	}
412
413
	/**
414
	 * Perform the call to EDD based on the AJAX call or passed data
415
	 *
416
	 * @since 1.7.4
417
	 *
418
	 * @param array $array {
419
	 * @type string $license The license key
420
	 * @type string $edd_action The EDD action to perform, like `check_license`
421
	 * @type string $field_id The ID of the field to check
422
	 * @type boolean $update Whether to update plugin settings. Prevent updating the data by setting an `update` key to false
423
	 * @type string $format If `object`, return the object of the license data. Else, return the JSON-encoded object
424
	 * }
425
	 *
426
	 * @return mixed|string|void
427
	 */
428
	public function license_call( $array = array() ) {
429
430
		$is_ajax = ( defined('DOING_AJAX') && DOING_AJAX );
431
		$data = empty( $array ) ? $_POST['data'] : $array;
432
		$has_cap = GVCommon::has_cap( 'gravityview_edit_settings' );
433
434
		if ( $is_ajax && empty( $data['license'] ) ) {
435
			die( - 1 );
436
		}
437
438
		// If the user isn't allowed to edit settings, show an error message
439
		if( ! $has_cap ) {
440
			$license_data = new stdClass();
441
			$license_data->error = 'capability';
442
			$license_data->message = $this->get_license_message( $license_data );
443
			$json = json_encode( $license_data );
444
		} else {
445
446
			$license      = esc_attr( rgget( 'license', $data ) );
447
			$license_data = $this->_license_get_remote_response( $data, $license );
448
449
			// Empty is returned when there's an error.
450
			if ( empty( $license_data ) ) {
451
				if ( $is_ajax ) {
452
					exit( json_encode( array() ) );
453
				} else { // Non-ajax call
454
					return json_encode( array() );
455
				}
456
			}
457
458
			$license_data->details = $this->license_details( $license_data );
459
			$license_data->message = $this->get_license_message( $license_data );
460
461
			$json = json_encode( $license_data );
462
463
			$update_license = ( ! isset( $data['update'] ) || ! empty( $data['update'] ) );
464
465
			$is_check_action_button = ( 'check_license' === $data['edd_action'] && defined( 'DOING_AJAX' ) && DOING_AJAX );
466
467
			// Failed is the response from trying to de-activate a license and it didn't work.
468
			// This likely happened because people entered in a different key and clicked "Deactivate",
469
			// meaning to deactivate the original key. We don't want to save this response, since it is
470
			// most likely a mistake.
471
			if ( $license_data->license !== 'failed' && ! $is_check_action_button && $update_license ) {
472
473
				if ( ! empty( $data['field_id'] ) ) {
474
					set_transient( self::status_transient_key, $license_data, DAY_IN_SECONDS );
475
				}
476
477
				$this->license_call_update_settings( $license_data, $data );
478
			}
479
		} // End $has_cap
480
481
		if ( $is_ajax ) {
482
			exit( $json );
483
		} else { // Non-ajax call
484
			return ( rgget('format', $data ) === 'object' ) ? $license_data : $json;
485
		}
486
	}
487
488
	/**
489
	 * Update the license after fetching it
490
	 * @param object $license_data
491
	 * @return void
492
	 */
493
	private function license_call_update_settings( $license_data, $data ) {
494
495
		// Update option with passed data license
496
		$settings = $this->Addon->get_app_settings();
497
498
        $settings['license_key'] = $license_data->license_key = trim( $data['license'] );
499
		$settings['license_key_status'] = $license_data->license;
500
		$settings['license_key_response'] = (array)$license_data;
501
502
		$this->Addon->update_app_settings( $settings );
503
	}
504
505
	/**
506
	 * URL to direct license renewal, or if license key is not set, then just the account page
507
	 * @since 1.13.1
508
	 * @param  object|null $license_data Object with license data
509
	 * @return string Renewal or account URL
510
	 */
511
	private function get_license_renewal_url( $license_data ) {
512
		$license_data = is_array( $license_data ) ? (object)$license_data : $license_data;
513
		$renew_license_url = ( ! empty( $license_data ) && !empty( $license_data->license_key ) ) ? sprintf( 'https://gravityview.co/checkout/?download_id=17&edd_license_key=%s&utm_source=admin_notice&utm_medium=admin&utm_content=expired&utm_campaign=Activation&force_login=1', $license_data->license_key ) : 'https://gravityview.co/account/';
514
		return $renew_license_url;
515
	}
516
517
	/**
518
	 * Override the text used in the GravityView EDD license Javascript
519
	 *
520
	 * @param  array|null $status Status to get. If empty, get all strings.
521
	 * @param  object|null $license_data Object with license data
522
	 * @return array          Modified array of content
523
	 */
524
	public function strings( $status = NULL, $license_data = null ) {
525
0 ignored issues
show
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
526
527
		$strings = array(
528
			'status' => esc_html__('Status', 'gravityview'),
529
			'error' => esc_html__('There was an error processing the request.', 'gravityview'),
530
			'failed'  => esc_html__('Could not deactivate the license. The license key you attempted to deactivate may not be active or valid.', 'gravityview'),
531
			'site_inactive' => esc_html__('The license key is valid, but it has not been activated for this site.', 'gravityview'),
532
			'inactive' => esc_html__('The license key is valid, but it has not been activated for this site.', 'gravityview'),
533
			'no_activations_left' => esc_html__('Invalid: this license has reached its activation limit.', 'gravityview') . ' ' . sprintf( esc_html__('You can manage license activations %son your GravityView account page%s.', 'gravityview'), '<a href="https://gravityview.co/account/#licenses">', '</a>' ),
534
			'deactivated' => esc_html__('The license has been deactivated.', 'gravityview'),
535
			'valid' => esc_html__('The license key is valid and active.', 'gravityview'),
536
			'invalid' => esc_html__('The license key entered is invalid.', 'gravityview'),
537
			'missing' => esc_html__('Invalid license key.', 'gravityview'),
538
			'revoked' => esc_html__('This license key has been revoked.', 'gravityview'),
539
			'expired' => sprintf( esc_html__('This license key has expired. %sRenew your license on the GravityView website%s to receive updates and support.', 'gravityview'), '<a href="'. esc_url( $this->get_license_renewal_url( $license_data ) ) .'">', '</a>' ),
540
			'capability' => esc_html__( 'You don\'t have the ability to edit plugin settings.', 'gravityview' ),
541
542
			'verifying_license' => esc_html__('Verifying license&hellip;', 'gravityview'),
543
			'activate_license' => esc_html__('Activate License', 'gravityview'),
544
			'deactivate_license' => esc_html__('Deactivate License', 'gravityview'),
545
			'check_license' => esc_html__('Verify License', 'gravityview'),
546
		);
547
548
		if( empty( $status ) ) {
549
			return $strings;
550
		}
551
552
		if( isset( $strings[ $status ] ) ) {
553
			return $strings[ $status ];
554
		}
555
556
		return NULL;
557
	}
558
559
}