Skip to content

Commit

Permalink
update: enhanced error handling method for license server API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
nurul-umbhiya authored Mar 27, 2024
1 parent a8e02e8 commit 421e43c
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions src/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Appsero;

use WP_Error;

/**
* Appsero License Checker
*
Expand Down Expand Up @@ -144,7 +146,7 @@ public function deactivate( $license_key ) {
/**
* Send common request
*
* @return array
* @return array|WP_Error
*/
protected function send_request( $license_key, $route ) {
$params = [
Expand All @@ -156,19 +158,13 @@ protected function send_request( $license_key, $route ) {
$response = $this->client->send_request( $params, $route, true );

if ( is_wp_error( $response ) ) {
return [
'success' => false,
'error' => $response->get_error_message(),
];
return $response;
}

$response = json_decode( wp_remote_retrieve_body( $response ), true );

if ( empty( $response ) || isset( $response['exception'] ) ) {
return [
'success' => false,
'error' => $this->client->__trans( 'Unknown error occurred, Please try again.' ),
];
return new WP_Error( 'invalid-json-error', $this->client->__trans( 'Unknown error occurred, Please try again.' ) );
}

if ( isset( $response['errors'] ) && isset( $response['errors']['license_key'] ) ) {
Expand All @@ -187,6 +183,15 @@ protected function send_request( $license_key, $route ) {
public function refresh_license_api() {
$this->check_license_status();

if ( ! empty( $this->error ) ) {
wp_send_json_error(
[
'message' => $this->error,
],
400
);
}

wp_send_json_success(
[
'message' => 'License refreshed successfully.',
Expand Down Expand Up @@ -353,7 +358,9 @@ public function check_license_status() {
if ( isset( $license['key'] ) && ! empty( $license['key'] ) ) {
$response = $this->check( $license['key'] );

if ( isset( $response['success'] ) && $response['success'] ) {
if ( is_wp_error( $response ) ) {
$this->error = $response->get_error_message();
} elseif ( isset( $response['success'] ) && $response['success'] ) {
$license['status'] = 'activate';
$license['remaining'] = $response['remaining'];
$license['activation_limit'] = $response['activation_limit'];
Expand Down Expand Up @@ -633,7 +640,11 @@ private function active_client_license( $license_key ) {

$response = $this->activate( $license_key );

if ( ! $response['success'] ) {
if ( is_wp_error( $response ) ) {
$this->error = $response->get_error_message();

return;
} elseif ( ! $response['success'] ) {
$this->error = $response['error'] ? $response['error'] : $this->client->__trans( 'Unknown error occurred.' );

return;
Expand Down Expand Up @@ -668,6 +679,15 @@ private function deactive_client_license() {
}

$response = $this->deactivate( $license['key'] );
if ( is_wp_error( $response ) ) {
$this->error = $response->get_error_message();

return;
} elseif ( ! $response['success'] ) {
$this->error = $response['error'] ? $response['error'] : $this->client->__trans( 'Unknown error occurred.' );

return;
}

$data = [
'key' => '',
Expand All @@ -676,12 +696,6 @@ private function deactive_client_license() {

update_option( $this->option_key, $data, false );

if ( ! $response['success'] ) {
$this->error = $response['error'] ? $response['error'] : $this->client->__trans( 'Unknown error occurred.' );

return;
}

$this->success = $this->client->__trans( 'License deactivated successfully.' );
}

Expand All @@ -699,7 +713,9 @@ private function refresh_client_license() {

$this->check_license_status();

$this->success = $this->client->__trans( 'License refreshed successfully.' );
if ( empty( $this->error ) ) {
$this->success = $this->client->__trans( 'License refreshed successfully.' );
}
}

/**
Expand Down

0 comments on commit 421e43c

Please sign in to comment.