From 2f9384f3681519bb9e0a807437fc5d9a8c342c96 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 6 Aug 2026 16:24:59 +0800 Subject: [PATCH] feat(user-center): upgrade posts panel to creator center --- assets/css/routes/profile/layout.css | 23 +++++++++++ assets/js/routes/user-center.js | 35 +++++++++++++++- inc/helpers/aksrt-user-center.php | 1 + .../user-center-renderers/posts-list.php | 40 ++++++++++++------- .../user-center-renderers/profile-posts.php | 29 ++++++++++---- src/User/Center.php | 2 +- templates/parts/content-profile-list-item.php | 29 +++++++++----- 7 files changed, 123 insertions(+), 36 deletions(-) diff --git a/assets/css/routes/profile/layout.css b/assets/css/routes/profile/layout.css index 2480a6b1..68a12f67 100644 --- a/assets/css/routes/profile/layout.css +++ b/assets/css/routes/profile/layout.css @@ -479,6 +479,29 @@ padding-bottom: 12px; border-bottom: 2px solid var(--color-primary-50); } + +.creator-center-header { + align-items: flex-start; +} +.creator-center-heading { + min-width: 220px; +} +.creator-center-heading__summary { + margin: 4px 0 0; + color: var(--color-ink-4); + font-size: 12px; +} +.creator-center-actions { + display: flex; + flex-wrap: wrap; + align-items: center; + justify-content: flex-end; + gap: 10px; +} +.creator-center-actions .profile-status-filter { + justify-content: flex-end; +} + .widget-title--inline { margin: 0; padding: 0; diff --git a/assets/js/routes/user-center.js b/assets/js/routes/user-center.js index 5037f184..191d71d9 100644 --- a/assets/js/routes/user-center.js +++ b/assets/js/routes/user-center.js @@ -16,6 +16,7 @@ var ajaxUrl = cfg.ajaxUrl || ''; var publicNonce = cfg.publicNonce || ''; var settingsNonce = cfg.settingsNonce || ''; + var submitNonce = cfg.submitNonce || ''; var userId = cfg.userId || 0; var i18n = cfg.i18n || {}; var tabAliases = cfg.tabAliases || {}; @@ -698,13 +699,44 @@ }); } - /* ==================== 初始化 ==================== */ + /* ==================== 创作中心文章操作 ==================== */ + function initCreatorPostActions() { + document.addEventListener('click', function(e) { + var btn = e.target.closest('[data-action="delete-post"]'); + if (!btn || btn.disabled) return; + if (!confirm(i18n.confirmDeletePost || '确定删除这篇文章?')) return; + var postId = btn.getAttribute('data-post-id'); + var nonce = btn.getAttribute('data-nonce') || submitNonce; + if (!postId || !nonce) return; + btn.disabled = true; + var body = new URLSearchParams(); + body.set('action', 'aksrt_delete_post'); + body.set('id', postId); + body.set('_nonce', nonce); + fetch(ajaxUrl, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }, credentials: 'same-origin', body: body.toString() }) + .then(function(r) { return r.json(); }) + .then(function(res) { + if (res && res.success) { + var item = btn.closest('.profile-list-item'); + if (item) item.remove(); + showToast((res.data && res.data.message) || '文章已经删除'); + } else { + btn.disabled = false; + showToast((res && res.data && res.data.message) || i18n.operationFailed || '操作失败'); + } + }) + .catch(function() { btn.disabled = false; showToast(i18n.networkError || '网络错误'); }); + }); + } + + if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function() { initDashboardNavigation(); initProfileForm(); initAuthForm(); initUnfavorite(); + initCreatorPostActions(); initNotifications(); initSettings(); }); @@ -713,6 +745,7 @@ initProfileForm(); initAuthForm(); initUnfavorite(); + initCreatorPostActions(); initNotifications(); initSettings(); } diff --git a/inc/helpers/aksrt-user-center.php b/inc/helpers/aksrt-user-center.php index 5fabd13a..86eab3d6 100644 --- a/inc/helpers/aksrt-user-center.php +++ b/inc/helpers/aksrt-user-center.php @@ -344,6 +344,7 @@ if (!function_exists('aksrt_user_center_runtime_config')) { 'userId' => $user_id, 'restUrl' => rest_url('aksrt/v1'), 'restNonce' => wp_create_nonce('wp_rest'), + 'submitNonce' => wp_create_nonce('aksrt_submit'), 'defaultTab' => $default_tab, 'activeTab' => $active_tab, 'tabAliases' => [ diff --git a/inc/helpers/user-center-renderers/posts-list.php b/inc/helpers/user-center-renderers/posts-list.php index 7493e5b5..455ae3df 100644 --- a/inc/helpers/user-center-renderers/posts-list.php +++ b/inc/helpers/user-center-renderers/posts-list.php @@ -21,27 +21,37 @@ $ps_args = [ 'update_post_term_cache' => true, ]; $q = new WP_Query($ps_args); +$front_submit = class_exists('\\AKSRT\\Core\\FrontSubmit') ? \AKSRT\Core\FrontSubmit::get_instance() : null; if ($q->have_posts()) : ?> -
+
have_posts()) : $q->the_post(); $st = get_post_status(); - $status_label = ''; - if ('publish' !== $st) { - $st_map = [ - 'draft' => __('草稿', 'aksrt'), - 'pending' => __('待审', 'aksrt'), - 'private' => __('私密', 'aksrt'), - 'future' => __('定时', 'aksrt'), - ]; - $status_label = $st_map[$st] ?? $st; - } - $edit_link = get_edit_post_link(); + $status_map = [ + 'publish' => __('已发布', 'aksrt'), + 'draft' => __('草稿', 'aksrt'), + 'pending' => __('待审', 'aksrt'), + 'private' => __('私密', 'aksrt'), + 'future' => __('定时', 'aksrt'), + ]; + $status_label = $status_map[$st] ?? $st; + $can_delete = $front_submit && $front_submit->can_delete_post(get_the_ID(), (int) $user_id); + $edit_link = $front_submit && $front_submit->can_edit_post(get_the_ID(), (int) $user_id) + ? aksrt_get_new_post_url(get_the_ID()) + : ''; + $view_link = get_permalink(); + $action = $edit_link + ? ['type' => 'edit', 'url' => $edit_link, 'label' => __('编辑', 'aksrt')] + : ('publish' === $st && $view_link ? ['type' => 'view', 'url' => $view_link, 'label' => __('预览', 'aksrt')] : []); get_template_part('templates/parts/content', 'profile-list-item', [ - 'action' => $edit_link ? ['type' => 'edit', 'url' => $edit_link, 'label' => __('编辑', 'aksrt')] : [], - 'status' => $status_label, + 'action' => $action, + 'status' => $status_label, + 'delete_action' => [ + 'enabled' => $can_delete, + 'nonce' => wp_create_nonce('aksrt_submit'), + ], ]); endwhile; ?> @@ -54,7 +64,7 @@ else : $svg_empty_post, __('还没有文章', 'aksrt'), __('试试发布你的第一篇文章吧。', 'aksrt'), - admin_url('post-new.php'), + !empty($new_post_url) ? $new_post_url : '', __('写文章', 'aksrt') ); endif; diff --git a/inc/helpers/user-center-renderers/profile-posts.php b/inc/helpers/user-center-renderers/profile-posts.php index e7ebbe8c..1242e08d 100644 --- a/inc/helpers/user-center-renderers/profile-posts.php +++ b/inc/helpers/user-center-renderers/profile-posts.php @@ -9,16 +9,29 @@ defined('ABSPATH') || exit; $context = is_array($args['context'] ?? null) ? $args['context'] : []; extract($context, EXTR_SKIP); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract +$can_submit_posts = class_exists('\\AKSRT\\Core\\FrontSubmit') + && \AKSRT\Core\FrontSubmit::get_instance()->can_submit((int) $user_id); ?>
-
-

- -

- +
+
+

+

+ +

+
+
+ + + + + + + +
diff --git a/src/User/Center.php b/src/User/Center.php index ef4920ce..d3c23808 100644 --- a/src/User/Center.php +++ b/src/User/Center.php @@ -128,7 +128,7 @@ class Center { 'type' => 'panel', ], 'posts' => [ - 'label' => __('我的文章', 'aksrt'), + 'label' => __('创作中心', 'aksrt'), 'icon' => 'file-alt', 'priority' => 20, 'type' => 'panel', diff --git a/templates/parts/content-profile-list-item.php b/templates/parts/content-profile-list-item.php index fd999ec1..1dc12183 100644 --- a/templates/parts/content-profile-list-item.php +++ b/templates/parts/content-profile-list-item.php @@ -21,20 +21,21 @@ if (!is_array($action)) { $action_type = isset($action['type']) ? (string) $action['type'] : ''; $action_url = isset($action['url']) ? (string) $action['url'] : ''; $action_label = isset($action['label']) ? (string) $action['label'] : ''; +$delete_action = isset($args['delete_action']) && is_array($args['delete_action']) ? $args['delete_action'] : []; $status_label = isset($args['status']) ? (string) $args['status'] : (string) get_query_var('aksrt_profile_item_status', ''); -$post_id = get_the_ID(); -$permalink = get_permalink(); -$title = get_the_title(); -$date_iso = get_the_date('c'); -$date_label = get_the_date(); -$excerpt = wp_trim_words(get_the_excerpt(), 30, '…'); -$comment_count = (int) get_comments_number(); -$categories = get_the_category() ?: []; -$tags = get_the_tags() ?: []; +$post_id = get_the_ID(); +$permalink = get_permalink(); +$title = get_the_title(); +$date_iso = get_the_date('c'); +$date_label = get_the_date(); +$excerpt = wp_trim_words(get_the_excerpt(), 30, '…'); +$comment_count = (int) get_comments_number(); +$categories = get_the_category() ?: []; +$tags = get_the_tags() ?: []; ?> -
> +
data-post-id="">

@@ -89,7 +90,7 @@ $tags = get_the_tags() ?: []; break; } if ('unfavorite' === $action_type) : - ?> + ?> +