fix(comments): align image upload with Zibll

Unify comment image upload settings, REST permissions, preview, progress,
and response handling while removing obsolete compatibility paths.
This commit is contained in:
2026-09-04 19:52:56 +08:00
parent 129432ae45
commit 44e165d34e
14 changed files with 513 additions and 216 deletions
+28 -12
View File
@@ -50,11 +50,11 @@ class UserController extends ControllerBase {
],
]);
// Comment inline image upload (logged-in users)
// Comment inline image upload (logged-in users with the comment upload setting enabled).
register_rest_route(self::API_NAMESPACE, '/comment/upload-image', [
'methods' => 'POST',
'callback' => [$this, 'rest_upload_comment_image'],
'permission_callback' => function () { return is_user_logged_in(); },
'permission_callback' => [$this, 'check_comment_image_upload_permission'],
]);
// Notification settings
@@ -476,22 +476,38 @@ class UserController extends ControllerBase {
return $this->success(['message' => __('消息清理任务已受理', 'aksrt')]);
}
public function check_comment_image_upload_permission(?\WP_REST_Request $request = null) {
if (!is_user_logged_in()) {
return new \WP_Error('aksrt_auth_required', __('请先登录', 'aksrt'), ['status' => 401]);
}
if ($request !== null) {
$nonce = (string) $request->get_header('X-WP-Nonce');
if ($nonce === '' || !wp_verify_nonce($nonce, 'wp_rest')) {
return new \WP_Error('aksrt_rest_nonce', __('安全验证失败,请刷新页面后重试', 'aksrt'), ['status' => 403]);
}
}
$settings = class_exists('\\AKSRT\\Settings\\Settings') ? \AKSRT\Settings\Settings::get_all() : [];
$image_enabled = !isset($settings['comment_img']) || !empty($settings['comment_img']);
if (!$image_enabled || empty($settings['comment_upload_img'])) {
return new \WP_Error('aksrt_comment_upload_disabled', __('评论图片上传功能未开启', 'aksrt'), ['status' => 403]);
}
$user_id = get_current_user_id();
if (!function_exists('aksrt_user_can') || !aksrt_user_can('upload_comment_image', $user_id)) {
return new \WP_Error('aksrt_comment_upload_forbidden', __('当前账号没有上传评论图片的权限', 'aksrt'), ['status' => 403]);
}
return true;
}
public function rest_upload_comment_image(\WP_REST_Request $request): \WP_REST_Response {
$user_id = get_current_user_id();
if ($user_id <= 0) {
return $this->error(__('请先登录', 'aksrt'), 401);
}
if (!function_exists('aksrt_user_can') || !aksrt_user_can('upload_comment_image', $user_id)) {
return $this->error(__('当前账号没有上传评论图片的权限', 'aksrt'), 403);
}
if (empty($_FILES['image']) && empty($_FILES['file'])) {
if (empty($_FILES['image'])) {
return $this->error(__('请选择要上传的图片', 'aksrt'), 400);
}
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
$file_key = !empty($_FILES['image']) ? 'image' : 'file';
$file_key = 'image';
$file = $_FILES[$file_key];
$upload_error = isset($file['error']) ? (int) $file['error'] : UPLOAD_ERR_NO_FILE;
if ($upload_error !== UPLOAD_ERR_OK) {
@@ -537,13 +553,13 @@ class UserController extends ControllerBase {
}
$url = wp_get_attachment_url((int) $attachment_id);
if (!$url) {
wp_delete_attachment((int) $attachment_id, true);
return $this->error(__('上传失败', 'aksrt'), 500);
}
return $this->success([
'message' => __('图片上传成功', 'aksrt'),
'data' => [
'url' => $url,
'attachment_id' => (int) $attachment_id,
'url' => $url,
],
]);
}