feat(extensions): 新增系统工具页面与前台投稿分片上传
This commit is contained in:
1 parent
613b8a57a3
commit
b75bf197a3
25 files changed
+1982
-58
No files matched your search
@@ -17,6 +17,7 @@ class FrontSubmit {
|
||||
use FrontSubmitPay;
|
||||
use FrontSubmitFeatured;
|
||||
use FrontSubmitMedia;
|
||||
use FrontSubmitChunk;
|
||||
|
||||
private static $instance = null;
|
||||
|
||||
@@ -93,6 +94,8 @@ class FrontSubmit {
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->register_chunk_routes();
|
||||
}
|
||||
|
||||
public function can_upload_attachment(\WP_REST_Request $request): bool {
|
||||
@@ -128,15 +131,26 @@ class FrontSubmit {
|
||||
}
|
||||
|
||||
public function rest_upload_attachment(\WP_REST_Request $request) {
|
||||
$kind = (string) $request->get_param('kind');
|
||||
$user_id = get_current_user_id();
|
||||
$files = $request->get_file_params();
|
||||
$file = $files['file'] ?? null;
|
||||
$kind = (string) $request->get_param('kind');
|
||||
$files = $request->get_file_params();
|
||||
$file = $files['file'] ?? null;
|
||||
|
||||
if (!is_array($file)) {
|
||||
return new \WP_Error('aksrt_upload_missing_file', __('请选择要上传的文件。', 'aksrt'), ['status' => 400]);
|
||||
}
|
||||
|
||||
return $this->persist_uploaded_file($kind, $file, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验并把一个上传文件落库为附件;整传与分片合并共用同一条管线。
|
||||
*
|
||||
* @param string $kind image|video|resource
|
||||
* @param array $file $_FILES 风格数组(name/tmp_name/size/error)
|
||||
* @param bool $sideload true 时文件不是 HTTP 直传(分片合并产物),用 sideload 动作保存
|
||||
* @return \WP_REST_Response|\WP_Error
|
||||
*/
|
||||
public function persist_uploaded_file(string $kind, array $file, bool $sideload = false) {
|
||||
$max_size = self::upload_limit_bytes($kind);
|
||||
$upload_error = (int) ($file['error'] ?? UPLOAD_ERR_NO_FILE);
|
||||
if ($upload_error === UPLOAD_ERR_INI_SIZE || $upload_error === UPLOAD_ERR_FORM_SIZE) {
|
||||
@@ -156,7 +170,7 @@ class FrontSubmit {
|
||||
return new \WP_Error('aksrt_upload_failed', __('服务器无法保存上传文件。', 'aksrt'), ['status' => 500]);
|
||||
}
|
||||
|
||||
$size = (int) ($file['size'] ?? 0);
|
||||
$size = (int) ($file['size'] ?? 0);
|
||||
|
||||
if ($size <= 0 || $size > $max_size) {
|
||||
return new \WP_Error(
|
||||
@@ -197,15 +211,18 @@ class FrontSubmit {
|
||||
require_once ABSPATH . 'wp-admin/includes/image.php';
|
||||
}
|
||||
|
||||
// 合并产物不是 HTTP 直传文件,走 sideload 动作(copy 而非 move_uploaded_file)。
|
||||
$overrides = $sideload ? ['test_form' => false, 'action' => 'wp_handle_sideload'] : ['test_form' => false];
|
||||
|
||||
$_FILES['aksrt_front_submit_file'] = $file;
|
||||
$attachment_id = media_handle_upload('aksrt_front_submit_file', 0, [], ['test_form' => false]);
|
||||
$attachment_id = media_handle_upload('aksrt_front_submit_file', 0, [], $overrides);
|
||||
unset($_FILES['aksrt_front_submit_file']);
|
||||
if (is_wp_error($attachment_id)) {
|
||||
return new \WP_Error('aksrt_upload_failed', $attachment_id->get_error_message(), ['status' => 400]);
|
||||
}
|
||||
|
||||
$attachment_id = (int) $attachment_id;
|
||||
wp_update_post(['ID' => $attachment_id, 'post_author' => $user_id]);
|
||||
wp_update_post(['ID' => $attachment_id, 'post_author' => get_current_user_id()]);
|
||||
update_post_meta($attachment_id, '_aksrt_front_submit_media', $kind);
|
||||
|
||||
$attachment = $this->prepare_attachment_response($attachment_id);
|
||||
|
||||
Reference in new issue
Block a user