Skip to content

Commit

Permalink
Fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
JurriaanK committed Mar 16, 2023
1 parent 1f4ba54 commit 645bb80
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
72 changes: 57 additions & 15 deletions src/LicenseHandler.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?php
/**
* GravityWP License handler.
*
* @package gravitywp-license-handler
* @license MIT
*/

namespace GravityWP\LicenseHandler;

use GFCommon;
Expand All @@ -8,7 +15,7 @@
/**
* Handles GWP Licenses.
*
* @version 1.0.20
* @version 1.0.21
*/
class LicenseHandler {

Expand Down Expand Up @@ -57,6 +64,15 @@ class LicenseHandler {
*/
private $_addon_license = '';

/**
* Store the GravityWP GF Addon license hash.
*
* @since 1.0
* @access private
* @var string $_addon_license_hash the GravityWP GF Addon license hash.
*/
private $_addon_license_hash = '';

/**
* Store the GravityWP GF Addon title
*
Expand Down Expand Up @@ -87,24 +103,44 @@ class LicenseHandler {
* @return void
*/
public function __construct( $gwp_addon_class, $license_hash, $plugin_file_path ) {
$this->_addon_class = $gwp_addon_class;
$this->_addon_slug = $gwp_addon_class::get_instance()->get_slug();
$this->_addon_license = $gwp_addon_class::get_instance()->get_plugin_setting( $this->_addon_slug . '_license_key' );
$this->_addon_title = $gwp_addon_class::get_instance()->plugin_page_title();
$this->_addon_class = $gwp_addon_class;
$this->_addon_slug = $gwp_addon_class::get_instance()->get_slug();
$this->_addon_license = $gwp_addon_class::get_instance()->get_plugin_setting( $this->_addon_slug . '_license_key' );
$this->_addon_license_hash = $license_hash;
$this->_addon_title = $gwp_addon_class::get_instance()->plugin_page_title();
$this->_addon_file_path = $plugin_file_path;

$this->_appsero_client = new \Appsero\Client( $license_hash, $this->_addon_title, $plugin_file_path );
if ( $this->initialize_appsero_client() ) {

$this->_license_handler = $this->_appsero_client->license();
if ( $this->_license_handler->is_valid() ) {
$this->_appsero_client->updater();

if ( $this->_license_handler->is_valid() ) {
$this->_appsero_client->updater();

} else {
add_action( 'admin_notices', array( $this, 'action_admin_notices' ) );
} else {
add_action( 'admin_notices', array( $this, 'action_admin_notices' ) );
}
}
}

/**
* Initialize or reinitialize the Appsero client.
*
* @return bool
*/
public function initialize_appsero_client() {
try {
unset( $this->_appsero_client );
unset( $this->_license_handler );

$this->_appsero_client = new \Appsero\Client( $this->_addon_license_hash, $this->_addon_title, $this->_addon_file_path );

$this->_license_handler = $this->_appsero_client->license();
} catch ( \Exception $e ) {
$this->_addon_class::get_instance()->log_error( __CLASS__ . '::' . __METHOD__ . '(): License client failed to initialize: ' . $e->getMessage() );
return false;
}

return true;
}
/**
* Display an admin notice.
*
Expand All @@ -121,6 +157,7 @@ public function action_admin_notices() {
$url = 'https://gravitywp.com/add-ons/?utm_source=admin_notice&utm_medium=admin&utm_content=inactive&utm_campaign=Admin%20Notice';
}

/* translators: button tags */
$message = esc_html__( 'Your %1$s license has not been actived. This means you are missing out on security fixes, updates and support.%2$sActivate your license%3$s or %4$sget a license here%5$s', 'gravitywp-license-handler' );
$message = sprintf( $message, $this->_addon_title, '<br /><br /><a href="' . esc_url( $primary_button_link ) . '" class="button button-primary">', '</a>', '<a href="' . esc_url( $url ) . '" class="button button-secondary">', '</a>' );

Expand Down Expand Up @@ -187,7 +224,11 @@ public function license_feedback( $value, $field ) {
GFCommon::remove_dismissible_message( $this->_addon_slug . '_license_notice' );
return true;
}

$message = 'Failed to activate this license key.';
if ( ! empty( $this->_license_handler->error ) ) {
$message = $this->_license_handler->error;
}
$this->_addon_class::get_instance()->set_field_error( $field, $message );
return false;
}

Expand All @@ -214,6 +255,9 @@ public function license_validation( $field, $field_setting ) {
}

if ( ! empty( $field_setting ) ) {
// Reset the license handler, to reset the $_license_handler->is_valid_license value.
$this->initialize_appsero_client();

// Send the remote request to activate the new license.
$this->_license_handler->license_form_submit(
array(
Expand All @@ -222,8 +266,6 @@ public function license_validation( $field, $field_setting ) {
'license_key' => $field_setting,
)
);
// Reset the license handler, to unset the $_license_handler->is_valid_license value.
$this->_license_handler = $this->_appsero_client->license();
}
}
}
4 changes: 4 additions & 0 deletions src/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
= 1.0.21 =
- Show an error message when license activation fails.
- Fix error icon being shown directly after saving a correct license.

= 1.0.20 =
- Update comment to reflect changes in appsero 1.2.2.
- Added changelog.

0 comments on commit 645bb80

Please sign in to comment.