From 950d1dcbde677f46f1a7a727681803f10d10a264 Mon Sep 17 00:00:00 2001 From: nanhaoluo <3075912108@qq.com> Date: Mon, 26 Jan 2026 11:39:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=A4=9A=20API=20?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E9=85=8D=E7=BD=AE=E8=8E=B7=E5=8F=96=E7=9A=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 argon_get_ai_provider_config() 中添加数组和键存在性检查 - 在 argon_get_provider_apis() 中确保始终返回数组类型 - 添加 isset() 检查防止访问不存在的数组键 - 修复可能导致 Fatal Error 的空配置问题 - 改进向后兼容性逻辑的健壮性 --- functions.php | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/functions.php b/functions.php index 3b0e0d0..d089a59 100644 --- a/functions.php +++ b/functions.php @@ -6467,6 +6467,11 @@ function argon_get_ai_summary($post_id) { function argon_get_provider_apis($provider) { $apis = get_option("argon_ai_{$provider}_apis", []); + // 确保返回的是数组 + if (!is_array($apis)) { + $apis = []; + } + // 向后兼容:如果没有多 API 配置,尝试从旧配置迁移 if (empty($apis)) { $old_api_key = get_option("argon_ai_{$provider}_api_key", ''); @@ -6504,25 +6509,27 @@ function argon_get_ai_provider_config($provider = '') { $active_api_id = get_option("argon_ai_{$provider}_active_api", ''); // 查找激活的 API - foreach ($apis as $api) { - if ($api['id'] === $active_api_id || (!empty($api['is_active']) && $api['is_active'])) { + if (!empty($apis) && is_array($apis)) { + foreach ($apis as $api) { + if (isset($api['id']) && ($api['id'] === $active_api_id || (!empty($api['is_active']) && $api['is_active']))) { + return [ + 'api_key' => isset($api['api_key']) ? $api['api_key'] : '', + 'api_endpoint' => isset($api['api_endpoint']) ? $api['api_endpoint'] : '', + 'model' => isset($api['model']) ? $api['model'] : '' + ]; + } + } + + // 如果没有找到激活的 API,返回第一个 + if (isset($apis[0])) { return [ - 'api_key' => $api['api_key'], - 'api_endpoint' => $api['api_endpoint'], - 'model' => $api['model'] + 'api_key' => isset($apis[0]['api_key']) ? $apis[0]['api_key'] : '', + 'api_endpoint' => isset($apis[0]['api_endpoint']) ? $apis[0]['api_endpoint'] : '', + 'model' => isset($apis[0]['model']) ? $apis[0]['model'] : '' ]; } } - // 如果没有找到激活的 API,返回第一个 - if (!empty($apis)) { - return [ - 'api_key' => $apis[0]['api_key'], - 'api_endpoint' => $apis[0]['api_endpoint'], - 'model' => $apis[0]['model'] - ]; - } - // 向后兼容:如果没有多 API 配置,使用旧配置 return [ 'api_key' => get_option("argon_ai_{$provider}_api_key", ''),