fix(user): repair phone binding and profile fields
This commit is contained in:
1 parent
0cf4639d95
commit
f646fa8573
15 files changed
+627
-162
No files matched your search
@@ -267,8 +267,6 @@ if (!function_exists('aksrt_get_avatar_box')) {
|
||||
}
|
||||
}
|
||||
|
||||
add_action('show_user_profile', 'aksrt_render_admin_avatar_field');
|
||||
add_action('edit_user_profile', 'aksrt_render_admin_avatar_field');
|
||||
add_action('user_edit_form_tag', static function (): void {
|
||||
echo ' enctype="multipart/form-data"';
|
||||
});
|
||||
@@ -312,137 +310,6 @@ if (!function_exists('aksrt_get_user_description')) {
|
||||
}
|
||||
}
|
||||
|
||||
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) || !aksrt_is_avatar_upload_enabled()) {
|
||||
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 get_avatar($user->ID, 96, '', $user->display_name, ['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_filter('user_profile_update_errors', static function (WP_Error $errors, bool $update): void {
|
||||
if (empty($_FILES['aksrt_avatar_file']['name']) || !aksrt_is_avatar_upload_enabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$file = $_FILES['aksrt_avatar_file'];
|
||||
// 头像上限并入「前端图片上传限制」统一治理(参考主题同源)。
|
||||
$max_size = class_exists('\AKSRT\Attachment\UploadLimits') ? \AKSRT\Attachment\UploadLimits::limit_bytes('image') : (int) wp_max_upload_size();
|
||||
$actual_size = !empty($file['tmp_name']) && is_readable($file['tmp_name']) ? (int) filesize($file['tmp_name']) : 0;
|
||||
if ((int) ($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK || $actual_size <= 0 || $actual_size > $max_size) {
|
||||
$errors->add('aksrt_avatar_size', sprintf(__('头像文件无效或超过 %s', 'aksrt'), size_format($max_size)));
|
||||
return;
|
||||
}
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/file.php';
|
||||
$checked = wp_check_filetype_and_ext($file['tmp_name'], $file['name'], [
|
||||
'jpg|jpeg|jpe' => 'image/jpeg',
|
||||
'png' => 'image/png',
|
||||
'gif' => 'image/gif',
|
||||
'webp' => 'image/webp',
|
||||
]);
|
||||
$image_size = wp_getimagesize($file['tmp_name']);
|
||||
if (empty($checked['type']) || empty($checked['ext']) || !wp_match_mime_types('image', $checked['type']) || !$image_size || ((int) $image_size[0] * (int) $image_size[1]) > 40000000) {
|
||||
$errors->add('aksrt_avatar_type', __('头像必须是有效的 JPG、PNG、GIF 或 WebP 图片,且总像素不能超过 4000 万。', 'aksrt'));
|
||||
}
|
||||
}, 10, 2);
|
||||
|
||||
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) || !aksrt_is_avatar_upload_enabled()) {
|
||||
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;
|
||||
}
|
||||
|
||||
// 头像上限并入「前端图片上传限制」统一治理(参考主题同源)。
|
||||
$max_size = class_exists('\AKSRT\Attachment\UploadLimits') ? \AKSRT\Attachment\UploadLimits::limit_bytes('image') : (int) wp_max_upload_size();
|
||||
$file = $_FILES['aksrt_avatar_file'];
|
||||
$actual_size = !empty($file['tmp_name']) && is_readable($file['tmp_name']) ? (int) filesize($file['tmp_name']) : 0;
|
||||
if ((int) ($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK || $actual_size <= 0 || $actual_size > $max_size) {
|
||||
return;
|
||||
}
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/file.php';
|
||||
$checked = wp_check_filetype_and_ext($file['tmp_name'], $file['name'], [
|
||||
'jpg|jpeg|jpe' => 'image/jpeg',
|
||||
'png' => 'image/png',
|
||||
'gif' => 'image/gif',
|
||||
'webp' => 'image/webp',
|
||||
]);
|
||||
$image_size = wp_getimagesize($file['tmp_name']);
|
||||
if (empty($checked['type']) || empty($checked['ext']) || !wp_match_mime_types('image', $checked['type']) || !$image_size || ((int) $image_size[0] * (int) $image_size[1]) > 40000000) {
|
||||
return;
|
||||
}
|
||||
|
||||
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.
|
||||
|
||||
Reference in new issue
Block a user