feat(users): govern profiles and access

Normalize fixed profile fields, profile actions, and the five-column capability matrix for comments, uploads, and downloads.
This commit is contained in:
kate522 committed 2026-08-13 00:24:54 +08:00
1 parent 0b06b2c7df
commit 8241570ca2
67 files changed
+780 -1127

No files matched your search

+1 -2
View File
@@ -149,8 +149,7 @@ if (!function_exists('aksrt_is_avatar_upload_enabled')) {
* Whether local avatar uploads are enabled by attachment policy.
*/
function aksrt_is_avatar_upload_enabled(): bool {
$settings = class_exists('\\AKSRT\\Settings\\Settings') ? \AKSRT\Settings\Settings::get_all() : [];
return !isset($settings['allow_avatar_upload']) || !empty($settings['allow_avatar_upload']);
return function_exists('aksrt_user_can') && aksrt_user_can('upload_avatar', get_current_user_id());
}
}
-2
View File
@@ -76,7 +76,6 @@ if (!function_exists('aksrt_comments_build_context')) :
'order' => 'desc',
'nested_depth' => 3,
'require_login' => false,
'allow_reply' => true,
'avatar_size' => 48,
'empty_text' => __('暂无评论,来做第一个评论的人吧~', 'aksrt'),
];
@@ -109,7 +108,6 @@ if (!function_exists('aksrt_comments_build_context')) :
'post_author_id' => (int) get_the_author_meta('ID'),
'avatar_size' => $avatar_size,
'max_depth' => max(1, (int) ($settings['nested_depth'] ?? 3)),
'allow_reply' => !empty($settings['allow_reply']),
'empty_text' => (string) ($settings['empty_text'] ?? __('暂无评论,来做第一个评论的人吧~', 'aksrt')),
'toolbar' => $toolbar,
'upload_enabled' => !empty($toolbar['upload_img']),
+1 -6
View File
@@ -775,13 +775,8 @@ if (!function_exists('aksrt_can_download')) {
return new WP_Error('aksrt_not_downloadable', __('该内容不是可下载资源。', 'aksrt'));
}
$paid_settings = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_paid_content_settings()
: ['download_login' => true];
$uid = $user_id > 0 ? $user_id : get_current_user_id();
if (!empty($paid_settings['download_login']) && $uid <= 0) {
if (!function_exists('aksrt_user_can') || !aksrt_user_can('resource_download', $uid, (int) $p->ID)) {
$login_url = wp_login_url(get_permalink($p));
return new WP_Error('aksrt_need_login', __('请先登录。', 'aksrt'), ['login_url' => $login_url]);
}
+1 -5
View File
@@ -403,11 +403,7 @@ if (!function_exists('aksrt_can_download')) {
}
$uid = $user_id > 0 ? $user_id : get_current_user_id();
$paid_settings = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_paid_content_settings()
: ['download_login' => true];
if (!empty($paid_settings['download_login']) && $uid <= 0) {
if (!function_exists('aksrt_user_can') || !aksrt_user_can('resource_download', $uid, (int) $p->ID)) {
return new WP_Error('aksrt_need_login', __('请先登录后再下载。', 'aksrt'), [
'status' => 401,
'login_url' => wp_login_url(get_permalink($p)),
+70 -43
View File
@@ -7,41 +7,48 @@
defined('ABSPATH') || exit;
if (!function_exists('aksrt_normalize_user_contacts')) {
if (!function_exists('aksrt_get_user_profile_details')) {
/**
* Sanitize the editable public contact list.
* Read the fixed profile details. Social fields are deliberately kept in one
* structured meta value so public rendering can filter them server-side.
*
* @param mixed $raw_contacts Submitted contact rows.
* @return array<int, array{name:string,url:string,icon:string}>
* @return array{privacy:string,gender:string,website:string,qq:string,wechat:string,github:string}
*/
function aksrt_normalize_user_contacts($raw_contacts): array {
$contacts = [];
if (!is_array($raw_contacts)) {
return $contacts;
function aksrt_get_user_profile_details(int $user_id): array {
$raw = get_user_meta($user_id, 'aksrt_profile_details', true);
$raw = is_array($raw) ? $raw : [];
$privacy = (string) ($raw['privacy'] ?? 'private');
if (!in_array($privacy, ['private', 'public', 'members'], true)) {
$privacy = 'private';
}
$gender = (string) ($raw['gender'] ?? 'secret');
if (!in_array($gender, ['secret', 'male', 'female'], true)) {
$gender = 'secret';
}
foreach ($raw_contacts as $contact) {
if (!is_array($contact)) {
continue;
}
return [
'privacy' => $privacy,
'gender' => $gender,
'website' => esc_url_raw((string) ($raw['website'] ?? '')),
'qq' => sanitize_text_field((string) ($raw['qq'] ?? '')),
'wechat' => sanitize_text_field((string) ($raw['wechat'] ?? '')),
'github' => esc_url_raw((string) ($raw['github'] ?? '')),
];
}
}
$url = isset($contact['url']) ? esc_url_raw(wp_unslash($contact['url'])) : '';
$icon = isset($contact['icon']) ? sanitize_key(wp_unslash($contact['icon'])) : '';
$icon = preg_replace('/^(fab|fas|far)[\s-]*/', '', $icon);
$icon = preg_replace('/^fa[\s-]*/', '', (string) $icon);
$icon = trim(preg_replace('/^[\s-]+/', '', (string) $icon), '- ');
if (!$url || !$icon) {
continue;
}
$contacts[] = [
'name' => aksrt_icon_key_to_name($icon),
'url' => $url,
'icon' => $icon,
];
}
return $contacts;
if (!function_exists('aksrt_get_visible_user_profile_details')) {
/** @return array{gender:string,website:string,qq:string,wechat:string,github:string} */
function aksrt_get_visible_user_profile_details(int $user_id, int $viewer_id = 0): array {
$details = aksrt_get_user_profile_details($user_id);
$visible = $viewer_id === $user_id || $details['privacy'] === 'public' || ($details['privacy'] === 'members' && $viewer_id > 0);
return [
'gender' => $details['gender'] === 'secret' ? '' : $details['gender'],
'website' => $visible ? $details['website'] : '',
'qq' => $visible ? $details['qq'] : '',
'wechat' => $visible ? $details['wechat'] : '',
'github' => $visible ? $details['github'] : '',
];
}
}
@@ -79,6 +86,30 @@ if (!function_exists('aksrt_save_user_center_profile')) {
}
}
$privacy = isset($input['privacy']) ? sanitize_key(wp_unslash($input['privacy'])) : 'private';
if (!in_array($privacy, ['private', 'public', 'members'], true)) {
$errors[] = __('无效的隐私设置。', 'aksrt');
}
$gender = isset($input['gender']) ? sanitize_key(wp_unslash($input['gender'])) : 'secret';
if (!in_array($gender, ['secret', 'male', 'female'], true)) {
$errors[] = __('无效的性别设置。', 'aksrt');
}
$details = [
'privacy' => $privacy,
'gender' => $gender,
'website' => isset($input['website']) ? esc_url_raw(wp_unslash($input['website'])) : '',
'qq' => isset($input['qq']) ? sanitize_text_field(wp_unslash($input['qq'])) : '',
'wechat' => isset($input['wechat']) ? sanitize_text_field(wp_unslash($input['wechat'])) : '',
'github' => isset($input['github']) ? esc_url_raw(wp_unslash($input['github'])) : '',
];
foreach (['website', 'github'] as $field) {
if (!empty($input[$field]) && $details[$field] === '') {
$errors[] = sprintf(__('无效的%s链接。', 'aksrt'), $field === 'website' ? __('个人网站', 'aksrt') : 'GitHub');
}
}
if (mb_strlen($details['qq']) > 50 || mb_strlen($details['wechat']) > 100) {
$errors[] = __('社交资料长度超过限制。', 'aksrt');
}
if ($errors) {
return new WP_Error('aksrt_profile_invalid', implode('<br>', $errors));
}
@@ -89,8 +120,7 @@ if (!function_exists('aksrt_save_user_center_profile')) {
return $result;
}
}
update_user_meta($user_id, 'aksrt_user_contacts', aksrt_normalize_user_contacts($input['contacts'] ?? []));
update_user_meta($user_id, 'aksrt_profile_details', $details);
return true;
@@ -388,6 +418,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;
return [
'enabled' => true,
'restUrl' => rest_url('aksrt/v1/user/background'),
@@ -396,11 +428,11 @@ if (!function_exists('aksrt_user_center_cover_upload_config')) {
'inputId' => 'aksrt-bg-file-input',
'imageId' => 'aksrt-cover-img',
'fieldName' => 'background',
'maxSize' => 2097152,
'maxSize' => $max_size,
'fallbackImage' => AKSRT_URI . '/assets/img/person_background.png',
'idleText' => __('编辑', 'aksrt'),
'idleText' => __('修改背景图', 'aksrt'),
'loadingText' => __('上传中…', 'aksrt'),
'tooLargeText' => __('图片大小不能超过 2MB', 'aksrt'),
'tooLargeText' => sprintf(__('图片大小不能超过 %dMB', 'aksrt'), (int) ($max_size / MB_IN_BYTES)),
'successText' => __('背景图已更新', 'aksrt'),
'errorText' => __('上传失败', 'aksrt'),
'networkErrorText' => __('网络错误,请重试', 'aksrt'),
@@ -418,10 +450,7 @@ if (!function_exists('aksrt_get_user_center_render_context')) {
$user_center_settings = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_user_center_settings()
: [
'tabs' => ['profile', 'comments', 'favorites'],
'avatar_upload' => true,
'cover_upload' => true,
'social_links' => true,
'tabs' => ['profile', 'comments', 'favorites'],
];
$tabs = aksrt_user_center_resolve_tabs();
@@ -439,11 +468,9 @@ if (!function_exists('aksrt_get_user_center_render_context')) {
$comment_count = (int) ($dashboard['comment_count'] ?? get_comments(['user_id' => $user_id, 'count' => true, 'status' => 'approve']));
$favorite_post_ids = $dashboard['favorite_post_ids'] ?? [];
$favorite_count = (int) ($dashboard['favorite_count'] ?? count($favorite_post_ids));
$user_contacts = $dashboard['user_contacts'] ?? get_user_meta($user_id, 'aksrt_user_contacts', true);
$user_contacts = is_array($user_contacts) ? $user_contacts : [];
$user_background_url = (string) ($dashboard['background_url'] ?? get_user_meta($user_id, 'aksrt_background_image', true));
$allow_avatar_upload = !empty($user_center_settings['avatar_upload']);
$allow_bg_upload = !empty($user_center_settings['cover_upload']) && class_exists('\AKSRT\Settings\Settings') && aksrt_get_setting('allow_background_upload', false);
$allow_avatar_upload = function_exists('aksrt_user_can') && aksrt_user_can('upload_avatar', $user_id);
$allow_bg_upload = function_exists('aksrt_user_can') && aksrt_user_can('upload_profile_background', $user_id);
$messages_enabled = !class_exists('\AKSRT\Settings\Settings') || \AKSRT\Settings\Settings::message_enabled();
$follower_count = class_exists('\AKSRT\User\Follow')
? \AKSRT\User\Follow::get_instance()->get_follower_count($user_id)
@@ -501,7 +528,7 @@ if (!function_exists('aksrt_get_user_center_render_context')) {
'favorite_count' => $favorite_count,
'follower_count' => $follower_count,
'following_count' => $following_count,
'user_contacts' => $user_contacts,
'profile_details' => aksrt_get_user_profile_details($user_id),
'profile_signals' => $dashboard['profile_signals'] ?? [],
'user_background_url' => $user_background_url,
'allow_bg_upload' => $allow_bg_upload,
+5 -11
View File
@@ -23,7 +23,7 @@ defined('ABSPATH') || exit;
* @type int $comment_count Approved comment count.
* @type int $favorite_count Favorited post count.
* @type array $favorite_post_ids Array of favorited post IDs.
* @type array $user_contacts Social/contact links from user meta.
* @type array $profile_details Server-filtered fixed public profile fields.
* @type array $profile_signals Identity chips (verified, level, badges).
* @type array $profile_notice Ban/restriction notice, if any.
* @type string $background_url Cover image URL.
@@ -62,14 +62,9 @@ function aksrt_get_user_dashboard_view(int $user_id, array $args = []): array {
])
: [];
$user_contacts = get_user_meta($user_id, 'aksrt_user_contacts', true) ?: [];
if (!is_array($user_contacts)) {
$user_contacts = [];
}
$user_center_settings = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_user_center_settings()
: ['social_links' => true];
$profile_details = function_exists('aksrt_get_visible_user_profile_details')
? aksrt_get_visible_user_profile_details($user_id, $viewer_id)
: ['gender' => '', 'website' => '', 'qq' => '', 'wechat' => '', 'github' => ''];
$background_url = get_user_meta($user_id, 'aksrt_background_image', true) ?: '';
@@ -98,8 +93,7 @@ function aksrt_get_user_dashboard_view(int $user_id, array $args = []): array {
'favorite_count' => $favorite_count,
'favorite_post_ids'=> $favorite_post_ids,
'recent_posts' => $recent_posts,
'user_contacts' => $user_contacts,
'social_links_on' => !empty($user_center_settings['social_links']),
'profile_details' => $profile_details,
'profile_signals' => $profile_signals,
'profile_notice' => $profile_notice,
'show_report' => $show_report,
+2 -7
View File
@@ -34,15 +34,11 @@ if (!function_exists('aksrt_get_user_home_page_context')) {
'posts_per_page' => 10,
];
$user_center_settings = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_user_center_settings()
: ['social_links' => true];
$post_count = (int) ($dashboard['post_count'] ?? 0);
$comment_count = (int) ($dashboard['comment_count'] ?? 0);
$favorite_post_ids = $dashboard['favorite_post_ids'] ?? [];
$favorite_count = (int) ($dashboard['favorite_count'] ?? 0);
$user_contacts = $dashboard['user_contacts'] ?? [];
$profile_details = $dashboard['profile_details'] ?? [];
$per_page = max(1, (int) ($user_home_settings['posts_per_page'] ?? 10));
$credits_enabled = !empty($dashboard['credits_enabled']);
$credit_balance = $dashboard['credit_balance'] ?? 0.0;
@@ -118,8 +114,7 @@ if (!function_exists('aksrt_get_user_home_page_context')) {
'dashboard' => $dashboard,
'is_own_profile' => $is_own_profile,
'user_home_settings' => $user_home_settings,
'user_center_settings' => $user_center_settings,
'user_contacts' => $user_contacts,
'profile_details' => $profile_details,
'post_count' => $post_count,
'comment_count' => $comment_count,
'favorite_post_ids' => $favorite_post_ids,
+1 -2
View File
@@ -151,7 +151,6 @@ if (!function_exists('aksrt_get_comment_footer_html')) :
*/
function aksrt_get_comment_footer_html(WP_Comment $comment, array $args, int $depth, array $settings = []): string {
$is_pending = '0' === (string) $comment->comment_approved;
$allow_reply = !empty($settings['allow_reply']);
// Time ago.
$absolute = date_i18n('Y-m-d H:i:s', strtotime((string) $comment->comment_date));
@@ -181,7 +180,7 @@ if (!function_exists('aksrt_get_comment_footer_html')) :
// Reply.
$reply_html = '';
if ($allow_reply && !$is_pending && $depth > 0) {
if (!$is_pending && $depth > 0) {
$max_depth = (int) ($args['max_depth'] ?? get_option('thread_comments_depth', 5));
$link = get_comment_reply_link(
array_merge(
@@ -1,34 +0,0 @@
<?php
/**
* User center profile form: contact rows block.
*
* Expects extracted $context vars.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
if (empty($user_center_settings['social_links'])) {
return;
}
?>
<div class="widget-card">
<h3 class="widget-title"><?php esc_html_e('联系方式', 'aksrt'); ?></h3>
<p class="profile-hint profile-hint--top"><?php esc_html_e('添加你的社交链接,显示在公开用户主页', 'aksrt'); ?></p>
<div id="contacts-list">
<?php foreach ((array) $user_contacts as $i => $contact) : ?>
<div class="contact-row" data-index="<?php echo (int) $i; ?>">
<select name="contacts[<?php echo (int) $i; ?>][icon]" class="profile-input contact-icon">
<?php echo aksrt_contact_icon_options($contact['icon'] ?? ''); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</select>
<input type="hidden" name="contacts[<?php echo (int) $i; ?>][name]" value="">
<input type="url" name="contacts[<?php echo (int) $i; ?>][url]"
value="<?php echo esc_url($contact['url'] ?? ''); ?>"
class="profile-input contact-url" placeholder="<?php esc_attr_e('链接 URL', 'aksrt'); ?>">
<button type="button" class="contact-remove" onclick="this.parentElement.remove()" title="<?php esc_attr_e('删除', 'aksrt'); ?>">&times;</button>
</div>
<?php endforeach; ?>
</div>
<button type="button" class="btn-ghost btn-sm profile-add-contact" id="add-contact-btn">+ <?php esc_html_e('添加联系方式', 'aksrt'); ?></button>
</div>
@@ -9,6 +9,7 @@ defined('ABSPATH') || exit;
$context = is_array($args['context'] ?? null) ? $args['context'] : [];
extract($context, EXTR_SKIP); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
$profile_details = isset($profile_details) && is_array($profile_details) ? $profile_details : aksrt_get_user_profile_details((int) $user_id);
?>
<form class="profile-form" id="profile-form" method="post">
<?php wp_nonce_field('aksrt_profile_save', '_profile_nonce'); ?>
@@ -24,13 +25,39 @@ extract($context, EXTR_SKIP); // phpcs:ignore WordPress.PHP.DontExtract.extract_
</div>
<div class="widget-card">
<h3 class="widget-title"><?php esc_html_e('个人简介', 'aksrt'); ?></h3>
<h3 class="widget-title"><?php esc_html_e('个人签名', 'aksrt'); ?></h3>
<textarea name="description" id="profile-bio" class="profile-input profile-textarea"
rows="4" maxlength="500" placeholder="<?php esc_attr_e('介绍一下自己…', 'aksrt'); ?>"><?php echo esc_textarea($target_user->description); ?></textarea>
<p class="profile-hint"><?php esc_html_e('显示在你的公开用户主页上,最多 500 字', 'aksrt'); ?></p>
</div>
<?php require __DIR__ . '/form-contacts.php'; ?>
<div class="widget-card">
<h3 class="widget-title"><?php esc_html_e('隐私设置', 'aksrt'); ?></h3>
<select name="privacy" class="profile-input">
<option value="private" <?php selected($profile_details['privacy'], 'private'); ?>><?php esc_html_e('社交资料 所有人不可见', 'aksrt'); ?></option>
<option value="public" <?php selected($profile_details['privacy'], 'public'); ?>><?php esc_html_e('社交资料 所有人可见', 'aksrt'); ?></option>
<option value="members" <?php selected($profile_details['privacy'], 'members'); ?>><?php esc_html_e('社交资料 仅注册用户可见', 'aksrt'); ?></option>
</select>
</div>
<div class="widget-card">
<h3 class="widget-title"><?php esc_html_e('性别', 'aksrt'); ?></h3>
<select name="gender" class="profile-input">
<option value="secret" <?php selected($profile_details['gender'], 'secret'); ?>><?php esc_html_e('保密', 'aksrt'); ?></option>
<option value="male" <?php selected($profile_details['gender'], 'male'); ?>><?php esc_html_e('男', 'aksrt'); ?></option>
<option value="female" <?php selected($profile_details['gender'], 'female'); ?>><?php esc_html_e('女', 'aksrt'); ?></option>
</select>
</div>
<div class="widget-card">
<h3 class="widget-title"><?php esc_html_e('社交资料', 'aksrt'); ?></h3>
<div class="profile-form__fields">
<label><span><?php esc_html_e('个人网站', 'aksrt'); ?></span><input type="url" name="website" class="profile-input" value="<?php echo esc_attr($profile_details['website']); ?>" placeholder="https://"></label>
<label><span>QQ</span><input type="text" name="qq" class="profile-input" maxlength="50" value="<?php echo esc_attr($profile_details['qq']); ?>"></label>
<label><span><?php esc_html_e('微信', 'aksrt'); ?></span><input type="text" name="wechat" class="profile-input" maxlength="100" value="<?php echo esc_attr($profile_details['wechat']); ?>"></label>
<label><span>GitHub</span><input type="url" name="github" class="profile-input" value="<?php echo esc_attr($profile_details['github']); ?>" placeholder="https://github.com/"></label>
</div>
</div>
<div class="profile-form__submit">
@@ -14,13 +14,18 @@ extract($context, EXTR_SKIP); // phpcs:ignore WordPress.PHP.DontExtract.extract_
<div class="uc-header__cover">
<img id="aksrt-cover-img" class="uc-header__cover-img" src="<?php echo esc_url($user_background_url ? $user_background_url : get_theme_file_uri('assets/img/person_background.png')); ?>" alt="">
<div class="uc-header__cover-mask"></div>
<?php if ($allow_bg_upload) : ?>
<button type="button" class="uc-header__cover-upload" id="aksrt-upload-bg-btn" title="<?php esc_attr_e('编辑背景图', 'aksrt'); ?>">
<i class="fas fa-pen" aria-hidden="true"></i>
<span><?php esc_html_e('编辑', 'aksrt'); ?></span>
</button>
<input type="file" id="aksrt-bg-file-input" accept="image/jpeg,image/png,image/webp,image/gif" style="display:none">
<?php endif; ?>
<div class="aksrt-cover-menu" data-aksrt-menu>
<button type="button" class="aksrt-cover-menu__trigger" aria-haspopup="true" aria-expanded="false" aria-label="<?php esc_attr_e('个人主页操作', 'aksrt'); ?>">
<i class="fas fa-ellipsis-h" aria-hidden="true"></i>
</button>
<div class="aksrt-cover-menu__panel" role="menu" hidden>
<a role="menuitem" href="<?php echo esc_url(aksrt_get_user_home_url()); ?>"><i class="fas fa-home" aria-hidden="true"></i><?php esc_html_e('我的主页', 'aksrt'); ?></a>
<?php if ($allow_bg_upload) : ?>
<button type="button" role="menuitem" id="aksrt-upload-bg-btn"><i class="fas fa-image" aria-hidden="true"></i><span><?php esc_html_e('修改背景图', 'aksrt'); ?></span></button>
<?php endif; ?>
</div>
</div>
<?php if ($allow_bg_upload) : ?><input type="file" id="aksrt-bg-file-input" accept="image/jpeg,image/png,image/webp,image/gif" hidden><?php endif; ?>
</div>
<div class="uc-header__content">
<div class="uc-header__avatar">
@@ -72,9 +77,6 @@ extract($context, EXTR_SKIP); // phpcs:ignore WordPress.PHP.DontExtract.extract_
<?php endif; ?>
</a>
<?php endif; ?>
<a href="<?php echo esc_url(aksrt_get_user_home_url()); ?>" class="btn-ghost btn-sm">
<i class="fas fa-map-marker-alt" aria-hidden="true"></i> <?php esc_html_e('我的主页', 'aksrt'); ?>
</a>
</div>
</div>
</div>