diff --git a/src/Modules/Licenser.php b/src/Modules/Licenser.php index 71ba24e3..ec4f1ce6 100644 --- a/src/Modules/Licenser.php +++ b/src/Modules/Licenser.php @@ -109,6 +109,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(); ?> %s%s   

%s

', + '

%s%s   

%s

%s', ( ( 'valid' === $status ) ? sprintf( '', $value, $this->product->get_key() ) : '' ), ( ( 'valid' === $status ) ? 'themeisle-sdk-text-input-valid' : '' ), $this->product->get_key(), @@ -167,7 +168,8 @@ public function license_view() { ( 'valid' === $status ? $valid_string : $invalid_string ), $this->product->get_key(), ( 'valid' === $status ? $deactivate_string : $activate_string ), - sprintf( $license_message, '' . $this->get_distributor_name() . ' ', $this->product->get_type() ) + sprintf( $license_message, '' . $this->get_distributor_name() . ' ', $this->product->get_type() ), + empty( $error_message ) ? '' : sprintf( '

%s

', $error_message ) ); } @@ -413,6 +415,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. */ @@ -439,19 +461,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; } @@ -462,7 +487,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 );