get_credential( \Airalo\Admin\Settings\Credential::CLIENT_ID );
//$sandbox_client_id = $credentials->get_credential( \Airalo\Admin\Settings\Credential::CLIENT_ID_SANDBOX );
$options = new \Airalo\Admin\Settings\Option();
$client_secret_encrypted = $options->fetch_option(\Airalo\Admin\Settings\Credential::CLIENT_SECRET);
//$client_sandbox_secret_encrypted = $options->fetch_option(\Airalo\Admin\Settings\Credential::CLIENT_SECRET_SANDBOX);
$auto_publish = $options->fetch_option_for_settings_page( \Airalo\Admin\Settings\Option::AUTO_PUBLISH );
$auto_publish_after_update = $options->fetch_option_for_settings_page( \Airalo\Admin\Settings\Option::AUTO_PUBLISH_AFTER_UPDATE );
//$use_sandbox = $options->fetch_option_for_settings_page(\Airalo\Admin\Settings\Option::USE_SANDBOX);
$airalo_sim_name = $options->fetch_option_for_settings_page( \Airalo\Admin\Settings\Option::USE_AIRALO_SIM_NAME );
$airalo_esim_cloud_share = $options->fetch_option_for_settings_page( \Airalo\Admin\Settings\Option::USE_ESIM_CLOUD_SHARE );
$airalo_update_product_title = $options->fetch_option( \Airalo\Admin\Settings\Option::UPDATE_PRODUCT_TITLE ) !== 'off' ? 'checked' : '';
$airalo_update_product_description = $options->fetch_option( \Airalo\Admin\Settings\Option::UPDATE_PRODUCT_DESCRIPTION ) !== 'off' ? 'checked' : '';
$airalo_flush_products = $options->fetch_option( \Airalo\Admin\Settings\Option::FLUSH_PRODUCTS ) !== 'off' ? 'checked' : '';
$sync_images = $options->fetch_option( \Airalo\Admin\Settings\Option::SYNC_IMAGES ) !== 'off' ? 'checked' : '';
$last_sync = $options->fetch_option(\Airalo\Admin\Settings\Option::LAST_SYNC);
$last_successful_sync = $options->fetch_option(\Airalo\Admin\Settings\Option::LAST_SUCCESSFUL_SYNC);
$error = $options->fetch_option( \Airalo\Admin\Settings\Option::SYNC_ERROR );
$show_error = $error ? 'airaloShow': 'airaloHide';
$language = $options->fetch_option( \Airalo\Admin\Settings\Option::LANGUAGE );
wp_enqueue_style('airalo-admin-style', 'https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;1,100;1,200;1,300;1,400;1,500;1,600;1,700&display=swap');
$nonce = wp_create_nonce( 'airalo-admin' );
?>
General
Sync Products
Last Sync:
Last Successful Sync:
Sync Error:
fetch_option( \Airalo\Admin\Settings\Credential::CLIENT_SECRET );
$stored_client_id = ( new \Airalo\Admin\Settings\Credential() )->get_credential( \Airalo\Admin\Settings\Credential::CLIENT_ID );
// The secret field is pre-filled with the stored encrypted blob; if the
// admin did not edit it the submission echoes that blob back. Treat it
// as "unchanged" so we don't try to use the encrypted value as a secret.
if ( $client_secret == $encrypted_secret && null != $encrypted_secret ) {
$client_secret = null;
}
// Skip verification entirely when neither credential field was changed
// (admin saved the page without touching credentials). Without this,
// every settings save would re-verify stored credentials and surface
// an error if those happen to be invalid, even though the admin made
// no change to them.
$id_unchanged = ( null === $client_id ) || ( $client_id === $stored_client_id );
$secret_unchanged = ( null === $client_secret );
if ( $id_unchanged && $secret_unchanged ) {
return;
}
airalo_verify_and_save_credentials( $client_id, $client_secret );
//$sandbox_client_id = isset( $_POST['airalo_client_id_sandbox'] ) ? sanitize_text_field( $_POST['airalo_client_id_sandbox'] ): null;
//$sandbox_client_secret = isset( $_POST['airalo_client_secret_sandbox'] ) ? sanitize_text_field( $_POST['airalo_client_secret_sandbox'] ): null;
//$encrypted_sandbox_secret = $options->fetch_option( \Airalo\Admin\Settings\Credential::CLIENT_SECRET_SANDBOX );
// if ( $sandbox_client_secret == $encrypted_sandbox_secret && null != $encrypted_sandbox_secret ) {
// $sandbox_client_secret = null;
// }
//airalo_save_credentials( $sandbox_client_id, $sandbox_client_secret, true );
}
}
function airalo_save_settings(): void {
$auto_publish = isset( $_POST['airalo_auto_publish'] ) ? sanitize_text_field( $_POST['airalo_auto_publish'] ) : 'off';
$auto_publish_after_update = isset( $_POST['airalo_auto_publish_update'] ) ? sanitize_text_field( $_POST['airalo_auto_publish_update'] ) : 'off';
// @IMPORTANT: enforce sandbox is stored as disabled in DB, some partners may have it enabled and if we hide option it will remain stuck
$use_sandbox = 'off';
$language = isset( $_POST['airalo_language'] ) ? sanitize_text_field( $_POST['airalo_language'] ) : 'en';
$sync_images = isset( $_POST['airalo_sync_images'] ) ? sanitize_text_field( $_POST['airalo_sync_images'] ) : 'off';
$airalo_sim_name = isset( $_POST['airalo_sim_name'] ) ? sanitize_text_field( $_POST['airalo_sim_name'] ): 'off';
$airalo_esim_cloud_share = isset( $_POST['airalo_esim_cloud_share'] ) ? sanitize_text_field( $_POST['airalo_esim_cloud_share'] ): 'off';
$airalo_update_product_title = isset( $_POST['airalo_update_product_title'] ) ? sanitize_text_field( $_POST['airalo_update_product_title'] ): 'off';
$airalo_update_product_description = isset( $_POST['airalo_update_product_description'] ) ? sanitize_text_field( $_POST['airalo_update_product_description'] ): 'off';
$airalo_flush_products = isset( $_POST['airalo_flush_products'] ) ? sanitize_text_field( $_POST['airalo_flush_products'] ) : 'off';
$options = new \Airalo\Admin\Settings\Option();
// $use_sandbox_old = $options->fetch_option( \Airalo\Admin\Settings\Option::USE_SANDBOX );
// if ( $use_sandbox_old != $use_sandbox ) {
// $options->insert_option( \Airalo\Admin\Settings\Option::ENVIRONMENT_SWITCHED, 'true' );
// }
$options->insert_option( \Airalo\Admin\Settings\Option::AUTO_PUBLISH, $auto_publish );
$options->insert_option( \Airalo\Admin\Settings\Option::AUTO_PUBLISH_AFTER_UPDATE, $auto_publish_after_update );
$options->insert_option( \Airalo\Admin\Settings\Option::USE_SANDBOX, $use_sandbox );
$options->insert_option( \Airalo\Admin\Settings\Option::LANGUAGE, $language );
$options->insert_option( \Airalo\Admin\Settings\Option::SYNC_IMAGES, $sync_images );
$options->insert_option( \Airalo\Admin\Settings\Option::USE_AIRALO_SIM_NAME, $airalo_sim_name );
$options->insert_option( \Airalo\Admin\Settings\Option::USE_ESIM_CLOUD_SHARE, $airalo_esim_cloud_share );
$options->insert_option( \Airalo\Admin\Settings\Option::UPDATE_PRODUCT_TITLE, $airalo_update_product_title );
$options->insert_option( \Airalo\Admin\Settings\Option::UPDATE_PRODUCT_DESCRIPTION, $airalo_update_product_description );
$options->insert_option( \Airalo\Admin\Settings\Option::FLUSH_PRODUCTS, $airalo_flush_products );
}
function airalo_save_credentials( $clientId, $clientSecret, $isSandbox = false ): void {
$clientIdCredential = \Airalo\Admin\Settings\Credential::CLIENT_ID;
$clientSecretCredential = \Airalo\Admin\Settings\Credential::CLIENT_SECRET;
// if ( $isSandbox ) {
// $clientIdCredential = \Airalo\Admin\Settings\Credential::CLIENT_ID_SANDBOX;
// $clientSecretCredential = \Airalo\Admin\Settings\Credential::CLIENT_SECRET_SANDBOX;
// }
$credentials = new \Airalo\Admin\Settings\Credential();
if ( $clientId ) {
$credentials->insert_credential( sanitize_text_field( $clientId ), $clientIdCredential );
}
if ( $clientSecret ) {
$credentials->insert_credential( sanitize_text_field( $clientSecret ), $clientSecretCredential );
}
}
/**
* Verifies submitted Airalo API credentials by attempting to obtain an
* access token, and only persists them when verification succeeds.
*
* The submitted client_secret may be null when the admin left the field
* unchanged; in that case the previously stored secret is used for the
* verification call so a partial update (e.g. only client_id) still works.
*
* Result is stored in a transient and rendered as a one-shot admin notice
* by airalo_credentials_verification_notice().
*
* @param string|null $clientId
* @param string|null $clientSecret
* @return void
*/
function airalo_verify_and_save_credentials( $clientId, $clientSecret ): void {
// Nothing submitted at all -> nothing to do, do not show a notice.
if ( ! $clientId && ! $clientSecret ) {
return;
}
$credentials = new \Airalo\Admin\Settings\Credential();
$option = new \Airalo\Admin\Settings\Option();
$effective_id = $clientId ?: $credentials->get_credential( \Airalo\Admin\Settings\Credential::CLIENT_ID );
$effective_secret = $clientSecret ?: $credentials->get_credential( \Airalo\Admin\Settings\Credential::CLIENT_SECRET );
// Without both pieces we cannot verify; refuse to save and warn.
if ( ! $effective_id || ! $effective_secret ) {
set_transient( 'airalo_credentials_verification_result', [
'ok' => false,
'message' => 'Airalo credentials were not saved: both Client ID and Client Secret are required.',
], 60 );
return;
}
$language = $option->fetch_option( \Airalo\Admin\Settings\Option::LANGUAGE ) ?: 'en';
$result = AiraloClient::verify( (string) $effective_id, (string) $effective_secret, $option->get_environment(), (string) $language );
if ( ! $result['ok'] ) {
error_log( '[Airalo] Credential verification failed: ' . $result['error'] );
set_transient( 'airalo_credentials_verification_result', [
'ok' => false,
'message' => 'Airalo credentials were not saved. Please provide a valid Client ID and Client Secret.',
], 60 );
return;
}
// Verified -> persist.
airalo_save_credentials( $clientId, $clientSecret );
set_transient( 'airalo_credentials_verification_result', [
'ok' => true,
'message' => 'Airalo credentials verified and saved successfully.',
], 60 );
}
add_action( 'admin_notices', 'airalo_credentials_verification_notice' );
function airalo_credentials_verification_notice() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$result = get_transient( 'airalo_credentials_verification_result' );
if ( ! $result || ! is_array( $result ) ) {
return;
}
delete_transient( 'airalo_credentials_verification_result' );
$class = ! empty( $result['ok'] ) ? 'notice-success' : 'notice-error';
echo 'Airalo: '
. esc_html( (string) ( $result['message'] ?? '' ) )
. '
';
}
function airalo_main_section_db() {
echo 'Settings
';
}
function airalo_settings_field_cb() {
$options = get_options( 'airalo-options' );
echo 'Settings
';
}
add_action( 'airalo_sync_products', 'airalo_sync_products_function', 10, 2 );
function airalo_sync_products_function() {
$product_syncer = new \Airalo\Admin\Syncers\ProductSyncer();
$product_syncer->handle();
}
add_filter( 'woocommerce_add_to_cart_validation', 'airalo_validate_cart_item_quantity', 10, 3 );
function airalo_validate_cart_item_quantity( $passed, $product_id, $quantity ) {
return ( new OrderValidator() )->handle( $passed, $product_id, $quantity );
}
add_action( 'woocommerce_thankyou', 'airalo_submit_order', 10, 1 );
function airalo_submit_order( $order ) {
try {
$order = wc_get_order( $order );
if ( ! $order ) {
return;
}
$items = $order->get_items();
$order_items = new \Airalo\Admin\OrderItem( $items );
$airalo_order_items = $order_items->get_airalo_order_items();
// skip if no airalo order items
if ( empty( $airalo_order_items ) ) {
return;
}
( new AiraloOrder() )->handle( $order );
} catch ( \Throwable $ex ) {
if ( $order instanceof \WC_Order ) {
try {
$order->update_status( 'on-hold', 'Error while submitting order to Airalo. Error: ' . $ex->getMessage() );
} catch ( \Throwable $inner ) {
error_log( '[Airalo] Failed to update order status in thankyou handler: ' . $inner->getMessage() );
}
}
error_log( '[Airalo] woocommerce_thankyou handler failed: ' . $ex->getMessage() );
}
}
add_action( 'woocommerce_order_status_completed', 'airalo_admin_order_on_status' );
add_action( 'woocommerce_order_status_processing', 'airalo_admin_order_on_status' );
function airalo_admin_order_on_status( $order_id ) {
try {
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
}
$created_via = $order->get_created_via();
if ( $created_via != 'admin' ) {
return;
}
if ( Cached::get( function () {
// do nothing, just check if cache for this order exists
}, $order->get_id() )
) {
return;
}
( new AiraloOrder() )->handle( $order );
} catch ( \Throwable $ex ) {
error_log( '[Airalo] order status handler failed: ' . $ex->getMessage() );
}
}
/**
* Show an admin notice when Airalo API credentials are missing so the
* misconfiguration is surfaced before it impacts checkout.
*/
add_action( 'admin_notices', 'airalo_missing_credentials_notice' );
function airalo_missing_credentials_notice() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
try {
$client = new AiraloClient( new Option() );
if ( $client->has_credentials() ) {
return;
}
} catch ( \Throwable $ex ) {
error_log( '[Airalo] Failed to check credentials for admin notice: ' . $ex->getMessage() );
return;
}
echo 'Airalo: '
. esc_html__( 'API credentials (Client ID and/or Client Secret) are not configured. Airalo orders will not be processed until they are set in the plugin settings.', 'airalo' )
. '
';
}