Skip to content

Commit 2920503

Browse files
authored
Fix settings and connect issues (#170)
* fix: token fails to refresh after expiry * update: add 12 hour time for plan data refresh and fix missing subscription id * update: add check to refresh plan data * fix: decoding errors and alignment and add logging for errors * update: refresh logic and formatting
1 parent c8c7e11 commit 2920503

File tree

2 files changed

+72
-30
lines changed

2 files changed

+72
-30
lines changed

classes/services/client.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ protected function request( $method, $endpoint, $args = [] ) {
163163
return $body;
164164
}
165165

166+
$body = json_decode( $body );
167+
166168
if ( false === $body ) {
167169
return new WP_Error( 422, 'Wrong Server Response' );
168170
}

modules/settings/module.php

Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -100,28 +100,6 @@ public function enqueue_scripts( $hook ) : void {
100100
);
101101
}
102102

103-
public static function refresh_plan_data() {
104-
$plan_data = Settings::get( Settings::PLAN_DATA );
105-
106-
$response = Utils::get_api_client()->make_request(
107-
'GET',
108-
'site/info',
109-
[ 'api_key' => $plan_data->public_api_key ]
110-
);
111-
112-
if ( ! empty( $response->site_url ) && Data::get_home_url() !== $response->site_url ) {
113-
Data::set_home_url( $response->site_url );
114-
}
115-
116-
if ( ! is_wp_error( $response ) ) {
117-
Settings::set( Settings::PLAN_DATA, json_decode( $response ) );
118-
Settings::set( Settings::IS_VALID_PLAN_DATA, true );
119-
} else {
120-
Logger::error( esc_html( $response->get_error_message() ) );
121-
Settings::set( Settings::IS_VALID_PLAN_DATA, false );
122-
}
123-
}
124-
125103
/**
126104
* Get Mixpanel project Token
127105
* @return string
@@ -156,12 +134,24 @@ public function on_connect(): void {
156134
return;
157135
}
158136

137+
self::register_site_with_data();
138+
}
139+
140+
/**
141+
* Register the website and save the plan data.
142+
* @return void
143+
*/
144+
public static function register_site_with_data() : void {
159145
$register_response = Utils::get_api_client()->make_request(
160146
'POST',
161147
'site/register'
162148
);
163149

164-
$this->save_plan_data( $register_response );
150+
if ( is_wp_error( $register_response ) ) {
151+
Logger::error( esc_html( $register_response->get_error_message() ) );
152+
} else {
153+
self::save_plan_data( $register_response );
154+
}
165155
}
166156

167157
/**
@@ -170,24 +160,65 @@ public function on_connect(): void {
170160
*
171161
* @return void
172162
*/
173-
public function save_plan_data( $register_response ) : void {
163+
public static function save_plan_data( $register_response ) : void {
174164
if ( $register_response && ! is_wp_error( $register_response ) ) {
175-
$decoded_response = json_decode( $register_response );
176-
Data::set_subscription_id( $decoded_response->id );
165+
$decoded_response = $register_response;
166+
Data::set_subscription_id( $decoded_response->plan->subscription_id );
177167
update_option( Settings::PLAN_DATA, $decoded_response );
178168
update_option( Settings::IS_VALID_PLAN_DATA, true );
179-
$this->set_default_settings();
169+
self::set_default_settings();
170+
self::set_plan_data_refresh_transient();
180171
} else {
181172
Logger::error( esc_html( $register_response->get_error_message() ) );
182173
update_option( Settings::IS_VALID_PLAN_DATA, false );
183174
}
184175
}
185176

177+
/**
178+
* Refresh the plan data after 12 hours
179+
* @return void
180+
*/
181+
public static function refresh_plan_data() : void {
182+
183+
if ( ! Connect::is_connected() ) {
184+
return;
185+
}
186+
187+
// Refresh only if refresh transient is expired
188+
if ( self::get_plan_data_refresh_transient() ) {
189+
return;
190+
}
191+
192+
$plan_data = Settings::get( Settings::PLAN_DATA );
193+
194+
// Return if plan data does not have public_api_key
195+
if ( ! $plan_data->public_api_key ) {
196+
Logger::error( 'Cannot refresh the plan data. No public API key found.' );
197+
self::register_site_with_data();
198+
return;
199+
}
200+
201+
$response = Utils::get_api_client()->make_request(
202+
'GET',
203+
'site/info',
204+
[ 'api_key' => $plan_data->public_api_key ]
205+
);
206+
207+
if ( ! is_wp_error( $response ) ) {
208+
Settings::set( Settings::PLAN_DATA, $response );
209+
Settings::set( Settings::IS_VALID_PLAN_DATA, true );
210+
self::set_plan_data_refresh_transient();
211+
} else {
212+
Logger::error( esc_html( $response->get_error_message() ) );
213+
Settings::set( Settings::IS_VALID_PLAN_DATA, false );
214+
}
215+
}
216+
186217
/**
187218
* Set default values after successful registration.
188219
* @return void
189220
*/
190-
private function set_default_settings() : void {
221+
private static function set_default_settings() : void {
191222
$widget_menu_settings = [
192223
'bigger-text' => [
193224
'enabled' => true,
@@ -242,7 +273,7 @@ private function set_default_settings() : void {
242273
'unit' => 'px',
243274
],
244275
'vertical' => [
245-
'direction' => 'top',
276+
'direction' => 'bottom',
246277
'value' => 10,
247278
'unit' => 'px',
248279
],
@@ -303,7 +334,8 @@ public function check_plan_data( $current_screen ) : void {
303334
'site/register'
304335
);
305336

306-
$this->save_plan_data( $register_response );
337+
self::save_plan_data( $register_response );
338+
self::set_plan_data_refresh_transient();
307339
}
308340
}
309341

@@ -392,6 +424,14 @@ public function register_settings(): void {
392424
}
393425
}
394426

427+
public static function set_plan_data_refresh_transient(): void {
428+
set_transient( Settings::PLAN_DATA . '_refresh', true, HOUR_IN_SECONDS * 12 );
429+
}
430+
431+
public static function get_plan_data_refresh_transient(): bool {
432+
return get_transient( Settings::PLAN_DATA . '_refresh' );
433+
}
434+
395435
/**
396436
* Module constructor.
397437
*/

0 commit comments

Comments
 (0)