'success', 'text' => __('Embed channel cleared.', 'agenized-for-woocommerce')];
} else {
$ch = AgenizedApi::get_channel($channel_id);
if (!$ch['ok']) {
$notice = ['type' => 'error', 'text' => __('Failed to fetch channel:', 'agenized-for-woocommerce') . ' ' . ($ch['message'] ?? '')];
} else {
$channel = $ch['channel'] ?? [];
$public_key = is_array($channel) && isset($channel['public_key']) ? (string) $channel['public_key'] : '';
if ($public_key === '') {
$notice = ['type' => 'error', 'text' => __('Channel has no public_key.', 'agenized-for-woocommerce')];
} else {
Options::update_string(Options::OPT_CHANNEL_ID, $channel_id);
Options::update_string(Options::OPT_CHANNEL_PUBLIC_KEY, $public_key);
$notice = ['type' => 'success', 'text' => __('Embed channel saved.', 'agenized-for-woocommerce')];
}
}
}
}
if ($action === 'use_channel_for_widget') {
$channel_id = isset($_POST['channel_id']) ? sanitize_text_field(wp_unslash($_POST['channel_id'])) : '';
if ($channel_id === '') {
$notice = ['type' => 'error', 'text' => __('Missing channel id.', 'agenized-for-woocommerce')];
} else {
$ch = AgenizedApi::get_channel($channel_id);
if (!$ch['ok']) {
$notice = ['type' => 'error', 'text' => __('Failed to fetch channel:', 'agenized-for-woocommerce') . ' ' . ($ch['message'] ?? '')];
} else {
$channel = $ch['channel'] ?? [];
$public_key = is_array($channel) && isset($channel['public_key']) ? (string) $channel['public_key'] : '';
if ($public_key === '') {
$notice = ['type' => 'error', 'text' => __('This channel has no public_key yet.', 'agenized-for-woocommerce')];
} else {
Options::update_string(Options::OPT_CHANNEL_ID, $channel_id);
Options::update_string(Options::OPT_CHANNEL_PUBLIC_KEY, $public_key);
$notice = ['type' => 'success', 'text' => __('Widget will now use this channel.', 'agenized-for-woocommerce')];
}
}
}
}
}
$channels_res = AgenizedApi::list_chat_widget_channels();
$channels = $channels_res['ok'] ? ($channels_res['channels'] ?? []) : [];
$selected_channel_id = Options::get_string(Options::OPT_CHANNEL_ID, '');
$selected_public_key = Options::get_string(Options::OPT_CHANNEL_PUBLIC_KEY, '');
$dashboard_url = Defaults::merchant_portal_base_url();
?>