Skip to content

Commit b802937

Browse files
authored
Merge pull request #268 from namithj/V0.6-Prep
Change the API URL and add more debugging options
2 parents f2d3005 + 854f6ac commit b802937

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

includes/class-admin-settings.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,14 @@ public function register_settings() {
415415
'description' => esc_html__( 'Your new API Host.', 'aspireupdate' ),
416416
'options' => [
417417
[
418-
'value' => 'api.aspirecloud.org',
419-
'label' => 'AspireCloud (api.aspirecloud.org)',
418+
'value' => 'api.aspirecloud.net',
419+
'label' => sprintf(
420+
/* translators: 1: The name of the API Service */
421+
__( 'AspireCloud (%1$s)', 'aspireupdate' ),
422+
'api.aspirecloud.net'
423+
),
420424
'require-api-key' => 'false',
421-
'api-key-url' => 'api.aspirecloud.org/v1/apitoken',
425+
'api-key-url' => 'api.aspirecloud.net/v1/apitoken',
422426
],
423427
[
424428
'value' => 'other',

includes/class-api-rewrite.php

+26-5
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,27 @@ class API_Rewrite {
3232
*/
3333
private $disable_ssl;
3434

35+
/**
36+
* API Key.
37+
*
38+
* @var string
39+
*/
40+
private $api_key;
41+
3542
/**
3643
* The Constructor.
3744
*
3845
* @param string $redirected_host The host to redirect to.
3946
* @param boolean $disable_ssl Disable SSL.
4047
*/
41-
public function __construct( $redirected_host, $disable_ssl ) {
48+
public function __construct( $redirected_host, $disable_ssl, $api_key ) {
4249
if ( 'debug' === $redirected_host ) {
4350
$this->redirected_host = $this->default_host;
4451
} else {
4552
$this->redirected_host = strtolower( $redirected_host );
4653
}
4754
$this->disable_ssl = $disable_ssl;
55+
$this->api_key = $api_key;
4856
add_filter( 'pre_http_request', [ $this, 'pre_http_request' ], 10, 3 );
4957
}
5058

@@ -65,17 +73,30 @@ public function pre_http_request( $response, $parsed_args, $url ) {
6573
( '' !== $this->redirected_host )
6674
) {
6775
if ( false !== strpos( $url, $this->default_host ) ) {
68-
Debug::log_string( 'Default API Found: ' . $url );
69-
Debug::log_request( $parsed_args );
76+
Debug::log_string( __( 'Default API Found: ', 'aspireupdate' ) . $url );
7077

7178
if ( $this->default_host !== $this->redirected_host ) {
7279
if ( $this->disable_ssl ) {
73-
Debug::log_string( 'SSL Verification Disabled' );
80+
Debug::log_string( __( 'SSL Verification Disabled', 'aspireupdate' ) );
7481
$parsed_args['sslverify'] = false;
7582
}
7683

84+
if ( '' !== $this->api_key ) {
85+
Debug::log_string( __( 'API Key Authorization header added.', 'aspireupdate' ) );
86+
$parsed_args['headers']['Authorization'] = 'Bearer ' . $this->api_key;
87+
}
88+
7789
$updated_url = str_replace( $this->default_host, $this->redirected_host, $url );
78-
Debug::log_string( 'API Rerouted to: ' . $updated_url );
90+
91+
/**
92+
* Adding cache buster parameter for AC beta test. Will remove this after Beta.
93+
*/
94+
Debug::log_string( __( 'Cache Buster Added to URL', 'aspireupdate' ) );
95+
$updated_url = add_query_arg( 'cache_buster', time(), $updated_url );
96+
97+
Debug::log_string( __( 'API Rerouted to: ', 'aspireupdate' ) . $updated_url );
98+
99+
Debug::log_request( $parsed_args );
79100

80101
/**
81102
* Temporarily Unhook Filter to prevent recursion.

includes/class-controller.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ private function api_rewrite() {
4343
if ( isset( $api_host ) && ( '' !== $api_host ) ) {
4444
$enable_debug = $admin_settings->get_setting( 'enable_debug', false );
4545
$disable_ssl = $admin_settings->get_setting( 'disable_ssl_verification', false );
46+
$api_key = $admin_settings->get_setting( 'api_key', '' );
4647
if ( $enable_debug && $disable_ssl ) {
47-
new API_Rewrite( $api_host, true );
48+
new API_Rewrite( $api_host, true, $api_key );
4849
} else {
49-
new API_Rewrite( $api_host, false );
50+
new API_Rewrite( $api_host, false, $api_key );
5051
}
5152
}
5253
}

0 commit comments

Comments
 (0)