Skip to content

Commit

Permalink
Improve license activation messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jakejackson1 committed Feb 20, 2024
1 parent 8026bb4 commit b33e1d2
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 37 deletions.
61 changes: 50 additions & 11 deletions src/Helper/Helper_Abstract_Addon.php
Original file line number Diff line number Diff line change
Expand Up @@ -649,10 +649,12 @@ public function schedule_license_check() {
'timeout' => 15,
'sslverify' => false,
'body' => [
'edd_action' => 'check_license',
'license' => $license_info['license'],
'item_name' => rawurlencode( $this->get_short_name() ),
'url' => home_url(),
'edd_action' => 'check_license',
'license' => $license_info['license'],
'item_id' => $this->get_edd_download_id(),
'item_name' => rawurlencode( $this->get_short_name() ),
'url' => home_url(),
'environment' => function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production',
],
]
);
Expand Down Expand Up @@ -687,13 +689,50 @@ public function schedule_license_check() {
$license_info['status'] = $license_check->license;
$license_info['message'] = $possible_responses[ $license_check->license ];

/* Include the expiry date if license expired */
if ( $license_check->license === 'expired' ) {
$date_format = get_option( 'date_format' );
$dt = new \DateTimeImmutable( $license_check->expires, wp_timezone() );
$date = $dt === false ? gmdate( $date_format, false ) : $dt->format( $date_format );

$license_info['message'] = sprintf( $license_info['message'], $date );
switch ( $license_check->license ) {
case 'expired':
$date_format = get_option( 'date_format' );
$dt = new \DateTimeImmutable( $license_check->expires, wp_timezone() );
$date = $dt === false ? gmdate( $date_format, false ) : $dt->format( $date_format );

$url = add_query_arg(
[
'edd_license_key' => $license_info['license'],
'download_id' => $this->get_edd_download_id(),
],
'https://gravitypdf.com/checkout/'
);

$license_info['message'] = sprintf( $license_info['message'], $date, $url );
break;

case 'revoked':
case 'disabled':
$url = add_query_arg(
[
'edd_action' => 'add_to_cart',
'download_id' => $this->get_edd_download_id(),
'edd_options[price_id]' => $license_check->price_id,
],
'https://gravitypdf.com/checkout/'
);

$license_info['message'] = sprintf( $license_info['message'], $url );
break;

case 'no_activations_left':
$url = add_query_arg(
[
'view' => 'upgrades',
'action' => 'manage_licenses',
'license_id' => $license_check->license_id,
'payment_id' => $license_check->payment_id,
],
'https://gravitypdf.com/account/'
);

$license_info['message'] = sprintf( $license_info['message'], $url );
break;
}

$this->log->notice( 'License key no longer valid', $license_info );
Expand Down
4 changes: 2 additions & 2 deletions src/Helper/Helper_Abstract_Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -1707,11 +1707,11 @@ public function license_callback( $args ) {
<?php if ( ! empty( $value['msg'] ) ): ?>
<?php if ( $is_error ): ?>
<div class="alert gforms_note_error">
<?php echo esc_html( $value['msg'] ); ?>
<?php echo wp_kses_post( $value['msg'] ); ?>
</div>
<?php else : ?>
<div id="message" class="alert gforms_note_success">
<?php echo esc_html( $value['msg'] ); ?>
<?php echo wp_kses_post( $value['msg'] ); ?>
</div>
<?php endif; ?>
<?php endif; ?>
Expand Down
26 changes: 17 additions & 9 deletions src/Helper/Helper_Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,25 @@ public function add_addon( Helper_Abstract_Addon $class ) {
$this->addon[ $class->get_slug() ] = $class;
}

/**
* Get the various responses for license key activations
*
* @param string $addon_name
*
* @return array
*/
public function addon_license_responses( $addon_name ) {
return [
'expired' => __( 'Your license key expired on %s.', 'gravity-forms-pdf-extended' ),
'revoked' => __( 'Your license key has been disabled', 'gravity-forms-pdf-extended' ),
'missing' => __( 'Invalid license key provided', 'gravity-forms-pdf-extended' ),
'invalid' => __( 'Your license is not active for this URL', 'gravity-forms-pdf-extended' ),
'site_inactive' => __( 'Your license is not active for this URL', 'gravity-forms-pdf-extended' ),
'item_name_mismatch' => sprintf( __( 'This appears to be an invalid license key for %s', 'gravity-forms-pdf-extended' ), $addon_name ),
'no_activations_left' => __( 'Your license key has reached its activation limit', 'gravity-forms-pdf-extended' ),
'default' => __( 'An error occurred, please try again', 'gravity-forms-pdf-extended' ),
'generic' => __( 'An error occurred during activation, please try again', 'gravity-forms-pdf-extended' ),
'expired' => sprintf( __( 'This license key expired on %%s. %1$sPlease renew your license to continue receiving updates and support%2$s.', 'gravity-forms-pdf-extended' ), '<a href="%s">', '</a>' ),
'revoked' => sprintf( __( 'This license key has been cancelled (most likely due to a refund request). %1$sPlease consider purchasing a new license%2$s.', 'gravity-forms-pdf-extended' ), '<a href="%s">', '</a>' ),
'disabled' => sprintf( __( 'This license key has been cancelled (most likely due to a refund request). %1$sPlease consider purchasing a new license%2$s.', 'gravity-forms-pdf-extended' ), '<a href="%s">', '</a>' ),
'missing' => __( 'This license key is invalid. Please check your key has been entered correctly.', 'gravity-forms-pdf-extended' ),
'invalid' => __( 'The license key is invalid. Please check your key has been entered correctly.', 'gravity-forms-pdf-extended' ),
'site_inactive' => __( 'Your license key is valid but does not match your current domain. This usually occurs if your domain URL changes. Please resave the settings to activate the license for this website.', 'gravity-forms-pdf-extended' ),
'item_name_mismatch' => sprintf( __( 'This license key is invalid for %s.', 'gravity-forms-pdf-extended' ), $addon_name ),
'no_activations_left' => sprintf( __( 'This license key has reached its activation limit. %1$sPlease upgrade your license to increase the site limit (you only pay the difference)$2$s.', 'gravity-forms-pdf-extended' ), '<a href="%s">', '</a>' ),
'default' => __( 'An error occurred, please try again.', 'gravity-forms-pdf-extended' ),
'generic' => __( 'An error occurred during activation, please try again.', 'gravity-forms-pdf-extended' ),
];
}

Expand Down
71 changes: 56 additions & 15 deletions src/Model/Model_Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,12 @@ protected function activate_license( Helper_Abstract_Addon $addon, $license_key
'timeout' => 15,
'sslverify' => false,
'body' => [
'edd_action' => 'activate_license',
'license' => $license_key,
'item_name' => rawurlencode( $addon->get_short_name() ), // the name of our product in EDD
'url' => home_url(),
'edd_action' => 'activate_license',
'license' => $license_key,
'item_id' => $addon->get_edd_download_id(),
'item_name' => rawurlencode( $addon->get_short_name() ), // the name of our product in EDD
'url' => home_url(),
'environment' => function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production',
],
]
);
Expand All @@ -356,7 +358,7 @@ protected function activate_license( Helper_Abstract_Addon $addon, $license_key
);
} else {
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
$message = __( 'Your support license key has been successfully validated.', 'gravityforms' );
$message = __( 'Your support license key has been activated for this domain.', 'gravity-forms-pdf-extended' );
$status = 'active';

if ( ! isset( $license_data->success ) || false === $license_data->success ) {
Expand All @@ -367,13 +369,50 @@ protected function activate_license( Helper_Abstract_Addon $addon, $license_key
$message = $possible_responses[ $license_data->error ];
$status = $license_data->error;

/* Include the expiry date if license expired */
if ( $license_data->error === 'expired' ) {
$date_format = get_option( 'date_format' );
$dt = new \DateTimeImmutable( $license_data->expires, wp_timezone() );
$date = $dt === false ? gmdate( $date_format, false ) : $dt->format( $date_format );
switch ( $license_data->error ) {
case 'expired':
$date_format = get_option( 'date_format' );
$dt = new \DateTimeImmutable( $license_data->expires, wp_timezone() );
$date = $dt === false ? gmdate( $date_format, false ) : $dt->format( $date_format );

$url = add_query_arg(
[
'edd_license_key' => $license_key,
'download_id' => $addon->get_edd_download_id(),
],
'https://gravitypdf.com/checkout/'
);

$message = sprintf( $message, $date, $url );
break;

case 'revoked':
case 'disabled':
$url = add_query_arg(
[
'edd_action' => 'add_to_cart',
'download_id' => $addon->get_edd_download_id(),
'edd_options[price_id]' => $license_data->price_id,
],
'https://gravitypdf.com/checkout/'
);

$message = sprintf( $message, $url );
break;

$message = sprintf( $message, $date );
case 'no_activations_left':
$url = add_query_arg(
[
'view' => 'upgrades',
'action' => 'manage_licenses',
'license_id' => $license_data->license_id,
'payment_id' => $license_data->payment_id,
],
'https://gravitypdf.com/account/'
);

$message = sprintf( $message, $url );
break;
}
}

Expand Down Expand Up @@ -473,10 +512,12 @@ public function deactivate_license_key( Helper_Abstract_Addon $addon, $license_k
'timeout' => 15,
'sslverify' => false,
'body' => [
'edd_action' => 'deactivate_license',
'license' => $license_key,
'item_name' => rawurlencode( $addon->get_short_name() ), // the name of our product in EDD
'url' => home_url(),
'edd_action' => 'deactivate_license',
'license' => $license_key,
'item_id' => $addon->get_edd_download_id(),
'item_name' => rawurlencode( $addon->get_short_name() ), // the name of our product in EDD
'url' => home_url(),
'environment' => function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production',
],
]
);
Expand Down

0 comments on commit b33e1d2

Please sign in to comment.