Skip to content

Commit

Permalink
add: retry registering in there is any error after connect
Browse files Browse the repository at this point in the history
  • Loading branch information
nirbhayel committed Dec 13, 2024
1 parent c295651 commit a2bc37a
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions modules/settings/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function register_page() : void {
* Enqueue Scripts and Styles
*/
public function enqueue_scripts( $hook ) : void {
//TODO: Update page name
if ( 'toplevel_page_accessibility-settings-2' !== $hook ) {
return;
}
Expand Down Expand Up @@ -122,6 +123,35 @@ public function on_connect(): void {
}
}

/**
* Retry registering the site if it fails during connect.
*
* @param $current_screen
* @return void
*/
public function check_plan_data( $current_screen ) : void {
//TODO: Update page name
if ( 'toplevel_page_accessibility-settings-2' !== $current_screen->base ) {
return;
}

if ( Connect::is_connected() && get_option( Settings::PLAN_DATA ) === false ) {
$register_response = Utils::get_api_client()->make_request(
'POST',
'site/register'
);

if ( $register_response && ! is_wp_error( $register_response ) ) {
Data::set_subscription_id( $register_response->id );
update_option( Settings::PLAN_DATA, $register_response );
update_option( Settings::IS_VALID_PLAN_DATA, true );
} else {
Logger::error( esc_html( $register_response->get_error_message() ) );
update_option( Settings::IS_VALID_PLAN_DATA, false );
}
}
}

/**
* Register settings.
*
Expand Down Expand Up @@ -176,5 +206,6 @@ public function __construct() {
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
add_action( 'rest_api_init', [ $this, 'register_settings' ] );
add_action( 'on_connect_' . Config::APP_PREFIX . '_connected', [ $this, 'on_connect' ] );
add_action( 'current_screen', [ $this, 'check_plan_data' ] );
}
}

0 comments on commit a2bc37a

Please sign in to comment.