Skip to content

Commit

Permalink
Merge pull request #78 from Codeinwp/dev
Browse files Browse the repository at this point in the history
fix: license activation workflow, show error message when failed to a…
  • Loading branch information
selul committed Sep 11, 2019
2 parents ede4dbd + 2a2559a commit ade795c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/Modules/Dashboard_widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public function can_load( $product ) {
* @return Dashboard_Widget Module instance.
*/
public function load( $product ) {

if ( apply_filters( 'themeisle_sdk_hide_dashboard_widget', false ) ) {
return;
}
$this->product = $product;
$this->dashboard_name = apply_filters( 'themeisle_sdk_dashboard_widget_name', 'WordPress Guides/Tutorials' );
$this->feeds = apply_filters(
Expand Down
60 changes: 46 additions & 14 deletions src/Modules/Licenser.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public function register_settings() {
if ( ! is_admin() ) {
return false;
}
if ( apply_filters( $this->product->get_key() . '_hide_license_field', false ) ) {
return;
}
add_settings_field(
$this->product->get_key() . '_license',
$this->product->get_name() . ' license',
Expand All @@ -109,6 +112,7 @@ public function license_view() {
$valid_string = apply_filters( $this->product->get_key() . '_lc_valid_string', 'Valid' );
$invalid_string = apply_filters( $this->product->get_key() . '_lc_invalid_string', 'Invalid' );
$license_message = apply_filters( $this->product->get_key() . '_lc_license_message', 'Enter your license from %s purchase history in order to get %s updates' );
$error_message = $this->get_error();
?>
<style type="text/css">
input.themeisle-sdk-text-input-valid {
Expand Down Expand Up @@ -157,7 +161,7 @@ public function license_view() {
</style>
<?php
echo sprintf(
'<p>%s<input class="themeisle-sdk-license-input %s" type="text" id="%s_license" name="%s_license" value="%s" /><a class="%s">%s</a>&nbsp;&nbsp;&nbsp;<button name="%s_btn_trigger" class="button button-primary themeisle-sdk-licenser-button-cta" value="yes" type="submit" >%s</button></p><p class="description">%s</p>',
'<p>%s<input class="themeisle-sdk-license-input %s" type="text" id="%s_license" name="%s_license" value="%s" /><a class="%s">%s</a>&nbsp;&nbsp;&nbsp;<button name="%s_btn_trigger" class="button button-primary themeisle-sdk-licenser-button-cta" value="yes" type="submit" >%s</button></p><p class="description">%s</p>%s',
( ( 'valid' === $status ) ? sprintf( '<input type="hidden" value="%s" name="%s_license" />', $value, $this->product->get_key() ) : '' ),
( ( 'valid' === $status ) ? 'themeisle-sdk-text-input-valid' : '' ),
$this->product->get_key(),
Expand All @@ -167,7 +171,8 @@ public function license_view() {
( 'valid' === $status ? $valid_string : $invalid_string ),
$this->product->get_key(),
( 'valid' === $status ? $deactivate_string : $activate_string ),
sprintf( $license_message, '<a href="' . $this->get_api_url() . '">' . $this->get_distributor_name() . '</a> ', $this->product->get_type() )
sprintf( $license_message, '<a href="' . $this->get_api_url() . '">' . $this->get_distributor_name() . '</a> ', $this->product->get_type() ),
empty( $error_message ) ? '' : sprintf( '<p style="color:#dd3d36">%s</p>', $error_message )
);

}
Expand Down Expand Up @@ -224,6 +229,10 @@ function show_notice() {
if ( ! is_admin() ) {
return false;
}

if ( apply_filters( $this->product->get_key() . '_hide_license_notices', false ) ) {
return;
}
$status = $this->get_license_status();
$no_activations_string = apply_filters( $this->product->get_key() . '_lc_no_activations_string', 'No activations left for %s !!!. You need to upgrade your plan in order to use %s on more websites. Please ask the %s Staff for more details.' );
$no_valid_string = apply_filters( $this->product->get_key() . '_lc_no_valid_string', 'In order to benefit from updates and support for %s, please add your license code from your <a href="%s" target="_blank">purchase history</a> and validate it <a href="%s">here</a>. ' );
Expand Down Expand Up @@ -413,6 +422,26 @@ private function reset_failed_checks() {
update_option( $this->product->get_key() . '_failed_checks', $this->failed_checks );
}

/**
* Set license validation error message.
*
* @param string $message Error message.
*/
public function set_error( $message = '' ) {
set_transient( $this->product->get_key() . 'act_err', $message, MINUTE_IN_SECONDS );

return;
}

/**
* Return the last error message.
*
* @return mixed Error message.
*/
public function get_error() {
return get_transient( $this->product->get_key() . 'act_err' );
}

/**
* Activate the license remotely.
*/
Expand All @@ -439,19 +468,22 @@ function activate_license() {
$response = wp_remote_get( add_query_arg( $api_params, $this->get_api_url() ) );
// make sure the response came back okay.
if ( is_wp_error( $response ) ) {
$license_data = new \stdClass();
$license_data->license = ( 'valid' != $status ) ? 'valid' : 'invalid';
$this->set_error( sprintf( 'ERROR: Failed to connect to the license service. Please try again later. Reason: %s', $response->get_error_message() ) );

} else {
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
if ( ! is_object( $license_data ) ) {
$license_data = new \stdClass();
$license_data->license = ( 'valid' != $status ) ? 'valid' : 'invalid';
}
if ( ! isset( $license_data->license ) ) {
$license_data->license = 'invalid';
}
return;
}

$license_data = json_decode( wp_remote_retrieve_body( $response ) );

if ( ! is_object( $license_data ) ) {
$this->set_error( 'ERROR: Failed to validate license. Please try again in one minute.' );

return;
}
if ( ! isset( $license_data->license ) ) {
$license_data->license = 'invalid';
}

if ( ! isset( $license_data->key ) ) {
$license_data->key = $license;
}
Expand All @@ -462,7 +494,7 @@ function activate_license() {
if ( isset( $license_data->plan ) ) {
update_option( $this->product->get_key() . '_license_plan', $license_data->plan );
}

$this->set_error( '' );
update_option( $this->product->get_key() . '_license_data', $license_data );
set_transient( $this->product->get_key() . '_license_data', $license_data, 12 * HOUR_IN_SECONDS );

Expand Down
3 changes: 3 additions & 0 deletions src/Modules/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ function ( $value ) {
* @return Notification Module instance.
*/
public function load( $product ) {
if ( apply_filters( 'themeisle_sdk_hide_notifications', false ) ) {
return;
}
$this->product = $product;

$notifications = apply_filters( 'themeisle_sdk_registered_notifications', [] );
Expand Down
5 changes: 5 additions & 0 deletions src/Modules/Uninstall_feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,11 @@ public function can_load( $product ) {
* @return Uninstall_Feedback Current module instance.
*/
public function load( $product ) {

if ( apply_filters( $product->get_key() . '_hide_uninstall_feedback', false ) ) {
return;
}

$this->product = $product;
add_action( 'admin_head', array( $this, 'load_resources' ) );
add_action( 'wp_ajax_' . $this->product->get_key() . '_uninstall_feedback', array( $this, 'post_deactivate' ) );
Expand Down

0 comments on commit ade795c

Please sign in to comment.