Skip to content

Commit 2d292f0

Browse files
committed
adds new api endpoints
1 parent 8499ad4 commit 2d292f0

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

src/Loader.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,28 @@ public static function init() {
7575
}
7676
}
7777

78+
/**
79+
* Get cache token used in API requests.
80+
*
81+
* @return string Cache token.
82+
*/
83+
public static function get_cache_token() {
84+
$cache_token = get_transient( 'themeisle_sdk_cache_token' );
85+
if ( false === $cache_token ) {
86+
$cache_token = wp_generate_password( 6, false );
87+
set_transient( $cache_token, WEEK_IN_SECONDS );
88+
}
89+
90+
return $cache_token;
91+
}
92+
93+
/**
94+
* Clear cache token.
95+
*/
96+
public static function clear_cache_token() {
97+
delete_transient( 'themeisle_sdk_cache_token' );
98+
}
99+
78100
/**
79101
* Register product into SDK.
80102
*

src/Modules/Licenser.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
// Exit if accessed directly.
1515
use ThemeisleSDK\Common\Abstract_Module;
16+
use ThemeisleSDK\Loader;
1617
use ThemeisleSDK\Product;
1718

1819
if ( ! defined( 'ABSPATH' ) ) {
@@ -435,7 +436,9 @@ public function get_error() {
435436
* @return bool|\WP_Error
436437
*/
437438
private function do_license_process( $license, $action = 'toggle' ) {
438-
439+
if ( strlen( $license ) < 10 ) {
440+
return new \WP_Error( 'themeisle-license-invalid-format', 'Invalid license.' );
441+
}
439442
$status = $this->get_license_status();
440443

441444
if ( 'valid' === $status && 'activate' === $action ) {
@@ -451,25 +454,27 @@ private function do_license_process( $license, $action = 'toggle' ) {
451454
'item_name' => rawurlencode( $this->product->get_name() ),
452455
'url' => rawurlencode( home_url() ),
453456
);
454-
455-
switch ( $action ) {
456-
case 'activate':
457-
$api_params['edd_action'] = 'activate_license';
458-
break;
459-
case 'deactivate':
460-
$api_params['edd_action'] = 'deactivate_license';
461-
break;
462-
case 'check':
463-
$api_params['edd_action'] = 'check_license';
464-
break;
465-
case 'toggle':
466-
case 'default':
467-
$api_params['edd_action'] = ( 'valid' !== $status ? ( 'activate_license' ) : ( 'deactivate_license' ) );
468-
break;
457+
if ( 'toggle' === $action ) {
458+
$action = ( 'valid' !== $status ? ( 'activate' ) : ( 'deactivate' ) );
469459
}
470460

471461
// Call the custom API.
472-
$response = wp_remote_get( add_query_arg( $api_params, $this->get_api_url() ) );
462+
if ( 'check' === $action ) {
463+
$response = wp_remote_get( sprintf( '%slicense/check/%s/%s/%s/%s', Product::API_URL, rawurlencode( $this->product->get_name() ), $license, rawurlencode( home_url() ), Loader::get_cache_token() ) );
464+
} else {
465+
$response = wp_remote_post(
466+
sprintf( '%slicense/%s/%s/%s/%s/%s', Product::API_URL, $action, rawurlencode( $this->product->get_name() ), $license ),
467+
array(
468+
'body' => array(
469+
'url' => rawurlencode( home_url() ),
470+
),
471+
'headers' => array(
472+
'Content-Type' => 'application/json',
473+
),
474+
)
475+
);
476+
}
477+
473478
// make sure the response came back okay.
474479
if ( is_wp_error( $response ) ) {
475480
return new \WP_Error( 'themeisle-license-500', sprintf( 'ERROR: Failed to connect to the license service. Please try again later. Reason: %s', $response->get_error_message() ) );
@@ -483,6 +488,9 @@ private function do_license_process( $license, $action = 'toggle' ) {
483488
if ( 'check' === $action ) {
484489
return $license_data;
485490
}
491+
492+
Loader::clear_cache_token();
493+
486494
if ( ! isset( $license_data->license ) ) {
487495
$license_data->license = 'invalid';
488496
}

src/Modules/Rollback.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private function get_versions_api_url() {
167167
return '';
168168
}
169169

170-
return sprintf( '%s?edd_action=get_versions&name=%s&url=%s&license=%s', $this->product->get_store_url(), urlencode( $this->product->get_name() ), urlencode( get_site_url() ), $license );
170+
return sprintf( '%slicense/versions/%s/%s/%s/%s', Product::API_URL, urlencode( $this->product->get_name() ), $license, urlencode( get_site_url() ), $this->product->get_version() );
171171
}
172172

173173
/**

src/Product.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ class Product {
106106
* @var string $version The product version.
107107
*/
108108
private $version;
109-
109+
/**
110+
* Root api endpoint.
111+
*/
112+
const API_URL = 'https://api.themeisle.com/';
110113
/**
111114
* ThemeIsle_SDK_Product constructor.
112115
*

0 commit comments

Comments
 (0)