refactor(attachment): 头像与背景图上限并入前端图片上传限制

- 移除独立的「用户头像与资料卡图片」分组与 avatar_max_size 字段,
  与参考主题保持一致:头像/资料卡背景图与投稿评论图片共用
  upload_img_size 同一套用户类型 × 大小配置
- 5 处消费方(REST 头像/背景上传、后台头像字段、用户中心头像与
  封面配置)全部改读 UploadLimits::limit_bytes('image'),提示语
  改用 size_format 与实际上限对齐
- 附件配置仅剩上传大小/上传格式/上传行为三组;管理端 SPA 已重建,
  421 项契约测试通过
This commit is contained in:
kate522 committed 2026-09-24 11:19:47 +08:00
1 parent 95cdad5c5d
commit bd7586a3b7
5 files changed
+24 -35

No files matched your search

+10 -10
View File
@@ -345,14 +345,14 @@ add_filter('user_profile_update_errors', static function (WP_Error $errors, bool
return;
}
$file = $_FILES['aksrt_avatar_file'];
$settings = class_exists('\\AKSRT\\Settings\\Settings') ? \AKSRT\Settings\Settings::get_all() : [];
$max_size_mb = max(1, min(10, (int) ($settings['avatar_max_size'] ?? 2)));
$actual_size = !empty($file['tmp_name']) && is_readable($file['tmp_name']) ? (int) filesize($file['tmp_name']) : 0;
if ((int) ($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK || $actual_size <= 0 || $actual_size > $max_size_mb * MB_IN_BYTES) {
$errors->add('aksrt_avatar_size', sprintf(__('头像文件无效或超过 %dMB', 'aksrt'), $max_size_mb));
return;
}
$file = $_FILES['aksrt_avatar_file'];
// 头像上限并入「前端图片上传限制」统一治理(参考主题同源)。
$max_size = class_exists('\AKSRT\Attachment\UploadLimits') ? \AKSRT\Attachment\UploadLimits::limit_bytes('image') : (int) wp_max_upload_size();
$actual_size = !empty($file['tmp_name']) && is_readable($file['tmp_name']) ? (int) filesize($file['tmp_name']) : 0;
if ((int) ($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK || $actual_size <= 0 || $actual_size > $max_size) {
$errors->add('aksrt_avatar_size', sprintf(__('头像文件无效或超过 %s', 'aksrt'), size_format($max_size)));
return;
}
require_once ABSPATH . 'wp-admin/includes/file.php';
$checked = wp_check_filetype_and_ext($file['tmp_name'], $file['name'], [
@@ -395,8 +395,8 @@ if (!function_exists('aksrt_save_admin_avatar_field')) {
return;
}
$settings = class_exists('\\AKSRT\\Settings\\Settings') ? \AKSRT\Settings\Settings::get_all() : [];
$max_size = max(1, min(10, (int) ($settings['avatar_max_size'] ?? 2))) * MB_IN_BYTES;
// 头像上限并入「前端图片上传限制」统一治理(参考主题同源)。
$max_size = class_exists('\AKSRT\Attachment\UploadLimits') ? \AKSRT\Attachment\UploadLimits::limit_bytes('image') : (int) wp_max_upload_size();
$file = $_FILES['aksrt_avatar_file'];
$actual_size = !empty($file['tmp_name']) && is_readable($file['tmp_name']) ? (int) filesize($file['tmp_name']) : 0;
if ((int) ($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK || $actual_size <= 0 || $actual_size > $max_size) {
+6 -6
View File
@@ -383,8 +383,8 @@ if (!function_exists('aksrt_user_center_avatar_upload_config')) {
if (!$enabled) {
return [];
}
$settings = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_all() : [];
$max_size = max(1, min(10, (int) ($settings['avatar_max_size'] ?? 2))) * MB_IN_BYTES;
// 头像上限并入「前端图片上传限制」统一治理(参考主题同源)。
$max_size = class_exists('\AKSRT\Attachment\UploadLimits') ? \AKSRT\Attachment\UploadLimits::limit_bytes('image') : (int) wp_max_upload_size();
return [
'enabled' => true,
'restUrl' => rest_url('aksrt/v1/user/avatar'),
@@ -396,7 +396,7 @@ if (!function_exists('aksrt_user_center_avatar_upload_config')) {
'maxSize' => $max_size,
'idleText' => __('更换头像', 'aksrt'),
'loadingText' => __('上传中…', 'aksrt'),
'tooLargeText' => sprintf(__('图片大小不能超过 %dMB', 'aksrt'), (int) ($max_size / MB_IN_BYTES)),
'tooLargeText' => sprintf(__('图片大小不能超过 %s', 'aksrt'), size_format($max_size)),
'successText' => __('头像已更新', 'aksrt'),
'errorText' => __('头像上传失败', 'aksrt'),
];
@@ -411,8 +411,8 @@ if (!function_exists('aksrt_user_center_cover_upload_config')) {
if (!$allow_bg_upload) {
return [];
}
$settings = class_exists('\\AKSRT\\Settings\\Settings') ? \AKSRT\Settings\Settings::get_all() : [];
$max_size = max(1, min(10, (int) ($settings['avatar_max_size'] ?? 2))) * MB_IN_BYTES;
// 背景图上限并入「前端图片上传限制」统一治理(参考主题同源)。
$max_size = class_exists('\AKSRT\Attachment\UploadLimits') ? \AKSRT\Attachment\UploadLimits::limit_bytes('image') : (int) wp_max_upload_size();
return [
'enabled' => true,
'restUrl' => rest_url('aksrt/v1/user/background'),
@@ -425,7 +425,7 @@ if (!function_exists('aksrt_user_center_cover_upload_config')) {
'fallbackImage' => function_exists('aksrt_get_user_cover_url') ? aksrt_get_user_cover_url(get_current_user_id()) : get_theme_file_uri('assets/img/user_t.jpg'),
'idleText' => __('修改背景图', 'aksrt'),
'loadingText' => __('上传中…', 'aksrt'),
'tooLargeText' => sprintf(__('图片大小不能超过 %dMB', 'aksrt'), (int) ($max_size / MB_IN_BYTES)),
'tooLargeText' => sprintf(__('图片大小不能超过 %s', 'aksrt'), size_format($max_size)),
'successText' => __('背景图已更新', 'aksrt'),
'errorText' => __('上传失败', 'aksrt'),
'networkErrorText' => __('网络错误,请重试', 'aksrt'),