ID; } elseif (is_object($id_or_email) && !empty($id_or_email->user_id)) { $user_id = (int) $id_or_email->user_id; } if (empty($user_id)) { return $avatar; } $local_avatars = get_user_meta($user_id, 'simple_local_avatar', true); if (empty($local_avatars) || empty($local_avatars['full'])) { return $avatar; } $size = (int) $size; if (empty($alt)) { $alt = get_the_author_meta('display_name', $user_id); } if (empty($local_avatars[$size])) { $upload_path = wp_upload_dir(); $avatar_full_path = str_replace($upload_path['baseurl'], $upload_path['basedir'], $local_avatars['full']); $image = wp_get_image_editor($avatar_full_path); if (!is_wp_error($image)) { $image->resize($size, $size, true); $image_sized = $image->save(); } $local_avatars[$size] = is_wp_error($image_sized) ? $local_avatars[$size] = $local_avatars['full'] : str_replace($upload_path['basedir'], $upload_path['baseurl'], $image_sized['path']); update_user_meta($user_id, 'simple_local_avatar', $local_avatars); } elseif ('http' != substr($local_avatars[$size], 0, 4)) { $local_avatars[$size] = home_url($local_avatars[$size]); } $author_class = is_author($user_id) ? ' current-author' : ''; $avatar = "".esc_attr($alt).""; return apply_filters('simple_local_avatar', $avatar); } public function edit_user_profile($profileuser) { ?>

头像

ID); ?>
'; if (empty($profileuser->simple_local_avatar)) { echo '尚未设置本地头像,请点击“浏览”按钮上传本地头像'; } else { echo '移除本地头像
'; echo '如需要修改本地头像,请重新上传新头像。如需要移除本地头像,请选中上方的“移除本地头像”复选框并更新个人资料即可。
移除本地头像后,将恢复使用 Gravatar 头像
'; } } else { if (empty($profileuser->simple_local_avatar)) { echo '尚未设置本地头像,请在 Gravatar.com 网站设置头像'; } else { echo '你没有头像上传权限,如需要修改本地头像,请联系站点管理员'; } } ?>
'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', ]; if (!function_exists('wp_handle_upload')) { require_once ABSPATH.'wp-admin/includes/file.php'; } $this->avatar_delete($user_id); if (strstr($_FILES['basic-user-avatar']['name'], '.php')) { wp_die('基于安全考虑 ".php" 格式文件禁止上传'); } $this->user_id_being_edited = $user_id; $avatar = wp_handle_upload($_FILES['basic-user-avatar'], [ 'mimes' => $mimes, 'test_form' => false, 'unique_filename_callback' => [ $this, 'unique_filename_callback', ], ]); update_user_meta($user_id, 'simple_local_avatar', [ 'full' => $avatar['url'], ]); } elseif (!empty($_POST['basic-user-avatar-erase'])) { $this->avatar_delete($user_id); } } public function avatar_defaults($avatar_defaults) { remove_action('get_avatar', [ $this, 'get_avatar', ]); return $avatar_defaults; } public function avatar_delete($user_id) { $old_avatars = get_user_meta($user_id, 'simple_local_avatar', true); $upload_path = wp_upload_dir(); if (is_array($old_avatars)) { foreach ($old_avatars as $old_avatar) { $old_avatar_path = str_replace($upload_path['baseurl'], $upload_path['basedir'], $old_avatar); unlink($old_avatar_path); } } delete_user_meta($user_id, 'simple_local_avatar'); } public function unique_filename_callback($dir, $name, $ext) { $user = get_user_by('id', (int) $this->user_id_being_edited); $name = $base_name = sanitize_file_name($user->ID.'_avatar'); $number = 1; while (file_exists($dir."/{$name}{$ext}")) { $name = $base_name.'_'.$number; ++$number; } return $name.$ext; } } $gdk_local_avatars = new gdk_local_avatars();