fix: 修复多 API 管理配置获取的错误处理

- 在 argon_get_ai_provider_config() 中添加数组和键存在性检查
- 在 argon_get_provider_apis() 中确保始终返回数组类型
- 添加 isset() 检查防止访问不存在的数组键
- 修复可能导致 Fatal Error 的空配置问题
- 改进向后兼容性逻辑的健壮性
This commit is contained in:
2026-01-26 11:39:44 +08:00
parent f696d2e117
commit 950d1dcbde

View File

@@ -6467,6 +6467,11 @@ function argon_get_ai_summary($post_id) {
function argon_get_provider_apis($provider) { function argon_get_provider_apis($provider) {
$apis = get_option("argon_ai_{$provider}_apis", []); $apis = get_option("argon_ai_{$provider}_apis", []);
// 确保返回的是数组
if (!is_array($apis)) {
$apis = [];
}
// 向后兼容:如果没有多 API 配置,尝试从旧配置迁移 // 向后兼容:如果没有多 API 配置,尝试从旧配置迁移
if (empty($apis)) { if (empty($apis)) {
$old_api_key = get_option("argon_ai_{$provider}_api_key", ''); $old_api_key = get_option("argon_ai_{$provider}_api_key", '');
@@ -6504,24 +6509,26 @@ function argon_get_ai_provider_config($provider = '') {
$active_api_id = get_option("argon_ai_{$provider}_active_api", ''); $active_api_id = get_option("argon_ai_{$provider}_active_api", '');
// 查找激活的 API // 查找激活的 API
if (!empty($apis) && is_array($apis)) {
foreach ($apis as $api) { foreach ($apis as $api) {
if ($api['id'] === $active_api_id || (!empty($api['is_active']) && $api['is_active'])) { if (isset($api['id']) && ($api['id'] === $active_api_id || (!empty($api['is_active']) && $api['is_active']))) {
return [ return [
'api_key' => $api['api_key'], 'api_key' => isset($api['api_key']) ? $api['api_key'] : '',
'api_endpoint' => $api['api_endpoint'], 'api_endpoint' => isset($api['api_endpoint']) ? $api['api_endpoint'] : '',
'model' => $api['model'] 'model' => isset($api['model']) ? $api['model'] : ''
]; ];
} }
} }
// 如果没有找到激活的 API返回第一个 // 如果没有找到激活的 API返回第一个
if (!empty($apis)) { if (isset($apis[0])) {
return [ return [
'api_key' => $apis[0]['api_key'], 'api_key' => isset($apis[0]['api_key']) ? $apis[0]['api_key'] : '',
'api_endpoint' => $apis[0]['api_endpoint'], 'api_endpoint' => isset($apis[0]['api_endpoint']) ? $apis[0]['api_endpoint'] : '',
'model' => $apis[0]['model'] 'model' => isset($apis[0]['model']) ? $apis[0]['model'] : ''
]; ];
} }
}
// 向后兼容:如果没有多 API 配置,使用旧配置 // 向后兼容:如果没有多 API 配置,使用旧配置
return [ return [