feat(submit): add user media library

This commit is contained in:
2026-09-04 22:53:33 +08:00
parent 44e165d34e
commit f3fcfe45da
23 changed files with 1321 additions and 945 deletions
+47 -13
View File
@@ -16,6 +16,7 @@ class FrontSubmit {
use FrontSubmitSave;
use FrontSubmitPay;
use FrontSubmitFeatured;
use FrontSubmitMedia;
private static $instance = null;
@@ -37,6 +38,8 @@ class FrontSubmit {
add_action('wp_ajax_nopriv_aksrt_submit_post', [$this, 'ajax_submit_post']);
add_action('rest_api_init', [$this, 'register_rest_routes']);
add_action('aksrt_new_add_posts', [$this, 'sync_post_attachments']);
add_action('aksrt_new_edit_posts', [$this, 'sync_post_attachments']);
add_filter('body_class', [$this, 'body_class']);
add_filter('wp_robots', [$this, 'robots_noindex']);
@@ -45,6 +48,38 @@ class FrontSubmit {
}
public function register_rest_routes(): void {
register_rest_route('aksrt/v1', '/front-submit/media/(?P<type>image|video|file)', [
'methods' => \WP_REST_Server::READABLE,
'callback' => [$this, 'rest_list_attachments'],
'permission_callback' => [$this, 'can_list_attachments'],
'args' => [
'type' => [
'validate_callback' => static function ($value): bool {
return in_array((string) $value, ['image', 'video', 'file'], true);
},
],
'post_id' => [
'default' => 0,
'sanitize_callback' => 'absint',
],
'paged' => [
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => static function ($value): bool {
return absint($value) > 0;
},
],
'search' => [
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
],
'exclude' => [
'default' => [],
'sanitize_callback' => [$this, 'sanitize_attachment_ids'],
],
],
]);
register_rest_route('aksrt/v1', '/front-submit/upload/(?P<kind>image|video|resource)', [
'methods' => 'POST',
'callback' => [$this, 'rest_upload_attachment'],
@@ -55,6 +90,10 @@ class FrontSubmit {
return in_array((string) $value, ['image', 'video', 'resource'], true);
},
],
'post_id' => [
'default' => 0,
'sanitize_callback' => 'absint',
],
],
]);
}
@@ -64,7 +103,7 @@ class FrontSubmit {
if ($user_id <= 0) {
return false;
}
if (!$this->can_submit($user_id)) {
if (!$this->can_use_media($request, $user_id)) {
return false;
}
@@ -173,23 +212,18 @@ class FrontSubmit {
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]);
$attachment_id = (int) $attachment_id;
wp_update_post(['ID' => $attachment_id, 'post_author' => $user_id]);
update_post_meta($attachment_id, '_aksrt_front_submit_media', $kind);
$url = wp_get_attachment_url($attachment_id);
if (!$url) {
$attachment = $this->prepare_attachment_response($attachment_id);
if ($attachment === null) {
wp_delete_attachment($attachment_id, true);
return new \WP_Error('aksrt_upload_failed', __('上传后无法读取文件。', 'aksrt'), ['status' => 500]);
}
return new \WP_REST_Response([
'success' => true,
'attachment_id' => $attachment_id,
'url' => $url,
'name' => get_the_title($attachment_id),
'filename' => wp_basename((string) get_attached_file($attachment_id)),
'mime' => $mime,
], 201);
$attachment['success'] = true;
return new \WP_REST_Response($attachment, 201);
}
/**