= ($expiry_time - 60)); } public static function refresh_token() { $settings = Options::get_site_settings(); $username = $settings['alertwise_setting_username'] ?? ''; $password = $settings['alertwise_setting_password'] ?? ''; if (empty($username) || empty($password)) { error_log('[Alertwise] Username or password missing for token refresh.'); return ''; } $response = AlertwiseAPI::instance()->api_login([ 'username' => $username, 'password' => $password, ]); if (is_wp_error($response)) { error_log('[Alertwise] Token request failed: ' . $response->get_error_message()); return ''; } // Defensive: check for access_token key if (!empty($response['access_token'])) { $settings['alertwise_setting_username'] = $username; $settings['alertwise_setting_password'] = $password; $settings['alertwise_setting_access_token'] = $response['access_token']; $settings['alertwise_setting_token_type'] = $response['token_type'] ?? 'Bearer'; $settings['alertwise_setting_expires_in'] = $response['expires_in'] ?? 3600; $settings['alertwise_setting_created_at'] = gmdate('Y-m-d H:i:s'); $settings['alertwise_setting_owner_id'] = (Helpers::decode($response['access_token'])['id'] ?? ''); Options::update_site_settings($settings); error_log('[Alertwise] Token refreshed successfully.'); return $response['access_token']; } else { error_log('[Alertwise] Token not found in login response: ' . print_r($response, true)); return ''; } } }