feat(user): add local avatar management flow

This commit is contained in:
2026-07-29 13:35:17 +08:00
parent 1b4ba8938a
commit e94437ef60
20 changed files with 289 additions and 186 deletions
+133 -13
View File
@@ -37,9 +37,21 @@ if (!function_exists('aksrt_resolve_avatar_user_id')) {
}
}
if (!function_exists('aksrt_get_default_avatar_url')) {
/**
* Return the bundled avatar used when a user has no custom image.
*/
function aksrt_get_default_avatar_url(): string {
return (string) apply_filters(
'aksrt_default_avatar_url',
get_theme_file_uri('assets/img/user/default avatar.svg')
);
}
}
if (!function_exists('aksrt_get_custom_avatar_url')) {
/**
* Return the locally uploaded or OAuth avatar URL.
* Return the locally uploaded avatar URL.
*/
function aksrt_get_custom_avatar_url(int $user_id, int $size = 96): string {
if ($user_id <= 0) {
@@ -47,15 +59,17 @@ if (!function_exists('aksrt_get_custom_avatar_url')) {
}
$attachment_id = (int) get_user_meta($user_id, 'aksrt_avatar_attachment_id', true);
if ($attachment_id > 0) {
$image = wp_get_attachment_image_url($attachment_id, [$size, $size]);
if (is_string($image) && $image !== '') {
return $image;
}
if ($attachment_id <= 0) {
return '';
}
$oauth_avatar = esc_url_raw((string) get_user_meta($user_id, 'aksrt_oauth_avatar', true));
return $oauth_avatar;
$attachment = get_post($attachment_id);
if (!$attachment instanceof WP_Post || $attachment->post_type !== 'attachment' || (int) $attachment->post_author !== $user_id || !wp_attachment_is_image($attachment_id)) {
return '';
}
$image = wp_get_attachment_image_url($attachment_id, [$size, $size]);
return is_string($image) ? $image : '';
}
}
@@ -77,7 +91,7 @@ if (!function_exists('aksrt_get_avatar_url')) {
if ($custom !== '') {
return $custom;
}
return (string) get_avatar_url($subject, array_merge($args, ['size' => $size]));
return aksrt_get_default_avatar_url();
}
}
@@ -119,9 +133,115 @@ if (!function_exists('aksrt_get_avatar_html')) {
add_filter('get_avatar_data', static function (array $args, $subject): array {
$user_id = aksrt_resolve_avatar_user_id($subject);
$custom = aksrt_get_custom_avatar_url($user_id, (int) ($args['size'] ?? 96));
if ($custom !== '') {
$args['url'] = $custom;
$args['found_avatar'] = true;
}
$args['url'] = $custom !== '' ? $custom : aksrt_get_default_avatar_url();
$args['found_avatar'] = true;
return $args;
}, 20, 2);
add_action('show_user_profile', 'aksrt_render_admin_avatar_field');
add_action('edit_user_profile', 'aksrt_render_admin_avatar_field');
if (!function_exists('aksrt_render_admin_avatar_field')) {
/**
* Render local avatar controls on the native WordPress user profile screen.
*/
function aksrt_render_admin_avatar_field(WP_User $user): void {
if (!current_user_can('edit_user', $user->ID)) {
return;
}
?>
<h2><?php esc_html_e('AKSRT 头像', 'aksrt'); ?></h2>
<table class="form-table" role="presentation">
<tr>
<th scope="row"><?php esc_html_e('当前头像', 'aksrt'); ?></th>
<td>
<?php echo aksrt_get_avatar_html($user->ID, 96, ['loading' => 'eager']); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<p><input type="file" name="aksrt_avatar_file" accept="image/jpeg,image/png,image/webp,image/gif"></p>
<?php if (aksrt_get_custom_avatar_url((int) $user->ID, 96) !== '') : ?>
<label><input type="checkbox" name="aksrt_avatar_reset" value="1"> <?php esc_html_e('恢复项目默认头像并删除当前头像文件', 'aksrt'); ?></label>
<?php endif; ?>
<p class="description"><?php esc_html_e('支持 JPG、PNG、GIF、WebP;大小上限由“权限 → 附件权限”控制。', 'aksrt'); ?></p>
<?php wp_nonce_field('aksrt_save_admin_avatar_' . $user->ID, 'aksrt_admin_avatar_nonce'); ?>
</td>
</tr>
</table>
<?php
}
}
add_action('personal_options_update', 'aksrt_save_admin_avatar_field');
add_action('edit_user_profile_update', 'aksrt_save_admin_avatar_field');
if (!function_exists('aksrt_save_admin_avatar_field')) {
/**
* Save local avatar changes from the native WordPress profile screen.
*/
function aksrt_save_admin_avatar_field(int $user_id): void {
if (!current_user_can('edit_user', $user_id)) {
return;
}
$nonce = isset($_POST['aksrt_admin_avatar_nonce']) ? sanitize_text_field(wp_unslash($_POST['aksrt_admin_avatar_nonce'])) : '';
if (!wp_verify_nonce($nonce, 'aksrt_save_admin_avatar_' . $user_id)) {
return;
}
$previous_id = (int) get_user_meta($user_id, 'aksrt_avatar_attachment_id', true);
if (!empty($_POST['aksrt_avatar_reset'])) {
delete_user_meta($user_id, 'aksrt_avatar_attachment_id');
aksrt_delete_owned_avatar_attachment($previous_id, $user_id);
do_action('aksrt_user_avatar_changed', $user_id, 0, '');
return;
}
if (empty($_FILES['aksrt_avatar_file']['name'])) {
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;
$file = $_FILES['aksrt_avatar_file'];
if ((int) ($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK || (int) ($file['size'] ?? 0) <= 0 || (int) $file['size'] > $max_size) {
return;
}
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
$attachment_id = media_handle_upload('aksrt_avatar_file', 0, [], [
'test_form' => false,
'mimes' => [
'jpg|jpeg|jpe' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'webp' => 'image/webp',
],
]);
if (is_wp_error($attachment_id) || !wp_attachment_is_image((int) $attachment_id)) {
if (!is_wp_error($attachment_id)) {
wp_delete_attachment((int) $attachment_id, true);
}
return;
}
$attachment_id = (int) $attachment_id;
wp_update_post(['ID' => $attachment_id, 'post_author' => $user_id]);
update_post_meta($attachment_id, '_aksrt_profile_media_user_id', $user_id);
update_post_meta($attachment_id, '_aksrt_profile_media_type', 'avatar');
update_user_meta($user_id, 'aksrt_avatar_attachment_id', $attachment_id);
aksrt_delete_owned_avatar_attachment($previous_id, $user_id);
do_action('aksrt_user_avatar_changed', $user_id, $attachment_id, (string) wp_get_attachment_image_url($attachment_id, 'thumbnail'));
}
}
if (!function_exists('aksrt_delete_owned_avatar_attachment')) {
/**
* Delete an avatar attachment only when it belongs to the target user.
*/
function aksrt_delete_owned_avatar_attachment(int $attachment_id, int $user_id): bool {
$attachment = $attachment_id > 0 ? get_post($attachment_id) : null;
if (!$attachment instanceof WP_Post || $attachment->post_type !== 'attachment' || (int) $attachment->post_author !== $user_id) {
return false;
}
return (bool) wp_delete_attachment($attachment_id, true);
}
}