用户功能设置收口与废弃后台链路清理
This commit is contained in:
1 parent
081eb0870d
commit
86692ff3f9
59 files changed
+506
-1767
No files matched your search
@@ -37,18 +37,98 @@ if (!function_exists('aksrt_resolve_avatar_user_id')) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_resolve_avatar_email')) {
|
||||
/**
|
||||
* Resolve an email address from an avatar subject.
|
||||
*
|
||||
* @param mixed $subject User ID, WP_User, comment, post, or email.
|
||||
*/
|
||||
function aksrt_resolve_avatar_email($subject): string {
|
||||
if ($subject instanceof WP_User) {
|
||||
return sanitize_email((string) $subject->user_email);
|
||||
}
|
||||
if ($subject instanceof WP_Comment && is_email((string) $subject->comment_author_email)) {
|
||||
return sanitize_email((string) $subject->comment_author_email);
|
||||
}
|
||||
if (is_object($subject) && isset($subject->comment_author_email) && is_email((string) $subject->comment_author_email)) {
|
||||
return sanitize_email((string) $subject->comment_author_email);
|
||||
}
|
||||
if (is_string($subject) && is_email($subject)) {
|
||||
return sanitize_email($subject);
|
||||
}
|
||||
|
||||
$user_id = aksrt_resolve_avatar_user_id($subject);
|
||||
$user = $user_id > 0 ? get_userdata($user_id) : false;
|
||||
return $user instanceof WP_User ? sanitize_email((string) $user->user_email) : '';
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_get_user_feature_settings')) {
|
||||
/**
|
||||
* Return the canonical user media and signature settings.
|
||||
*
|
||||
* @return array{avatar_upload_s:bool,avatar_external:string,avatar_default_img:string,user_cover_upload_s:bool,user_cover_img:string,user_desc_std:string}
|
||||
*/
|
||||
function aksrt_get_user_feature_settings(): array {
|
||||
if (class_exists('\AKSRT\Settings\Settings')) {
|
||||
return \AKSRT\Settings\Settings::get_user_feature_settings();
|
||||
}
|
||||
|
||||
return [
|
||||
'avatar_upload_s' => true,
|
||||
'avatar_external' => '',
|
||||
'avatar_default_img' => get_theme_file_uri('assets/img/avatar-default.png'),
|
||||
'user_cover_upload_s' => true,
|
||||
'user_cover_img' => get_theme_file_uri('assets/img/user_t.jpg'),
|
||||
'user_desc_std' => '这家伙很懒,什么都没有写...',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
$settings = aksrt_get_user_feature_settings();
|
||||
return (string) apply_filters(
|
||||
'aksrt_default_avatar_url',
|
||||
get_theme_file_uri('assets/img/user/default-avatar.svg')
|
||||
(string) $settings['avatar_default_img']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_get_external_avatar_url')) {
|
||||
/**
|
||||
* Return the configured external avatar for a subject.
|
||||
*
|
||||
* @param mixed $subject User subject.
|
||||
*/
|
||||
function aksrt_get_external_avatar_url($subject, int $size = 96): string {
|
||||
$settings = aksrt_get_user_feature_settings();
|
||||
$hosts = [
|
||||
'gravatar' => 'https://www.gravatar.com/avatar/',
|
||||
'weavatar' => 'https://weavatar.com/avatar/',
|
||||
'cravatar' => 'https://cn.cravatar.com/avatar/',
|
||||
'geekzu' => 'https://sdn.geekzu.org/avatar/',
|
||||
'loli' => 'https://gravatar.loli.net/avatar/',
|
||||
'sep' => 'https://cdn.sep.cc/avatar/',
|
||||
'v2ex' => 'https://cdn.v2ex.com/gravatar/',
|
||||
];
|
||||
$host = $hosts[(string) $settings['avatar_external']] ?? '';
|
||||
$email = aksrt_resolve_avatar_email($subject);
|
||||
if ($host === '' || $email === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
$size = max(1, min(2048, $size));
|
||||
return add_query_arg([
|
||||
's' => $size,
|
||||
'd' => aksrt_get_default_avatar_url(),
|
||||
], $host . hash('sha256', strtolower(trim($email))));
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_get_custom_avatar_url')) {
|
||||
/**
|
||||
* Return the locally uploaded avatar URL.
|
||||
@@ -91,6 +171,10 @@ if (!function_exists('aksrt_get_avatar_url')) {
|
||||
if ($custom !== '') {
|
||||
return $custom;
|
||||
}
|
||||
$external = aksrt_get_external_avatar_url($subject, $size);
|
||||
if ($external !== '') {
|
||||
return $external;
|
||||
}
|
||||
return aksrt_get_default_avatar_url();
|
||||
}
|
||||
}
|
||||
@@ -133,9 +217,7 @@ 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));
|
||||
$args['url'] = $custom !== '' ? $custom : aksrt_get_default_avatar_url();
|
||||
$args['url'] = aksrt_get_avatar_url($subject, ['size' => (int) ($args['size'] ?? 96)]);
|
||||
$args['found_avatar'] = true;
|
||||
return $args;
|
||||
}, 20, 2);
|
||||
@@ -151,7 +233,37 @@ if (!function_exists('aksrt_is_avatar_upload_enabled')) {
|
||||
* Whether local avatar uploads are enabled by attachment policy.
|
||||
*/
|
||||
function aksrt_is_avatar_upload_enabled(): bool {
|
||||
return function_exists('aksrt_user_can') && aksrt_user_can('upload_avatar', get_current_user_id());
|
||||
$settings = aksrt_get_user_feature_settings();
|
||||
return !empty($settings['avatar_upload_s']);
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_is_user_cover_upload_enabled')) {
|
||||
function aksrt_is_user_cover_upload_enabled(): bool {
|
||||
$settings = aksrt_get_user_feature_settings();
|
||||
return !empty($settings['user_cover_upload_s']);
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_get_user_cover_url')) {
|
||||
function aksrt_get_user_cover_url(int $user_id): string {
|
||||
$custom = $user_id > 0 ? esc_url_raw((string) get_user_meta($user_id, 'aksrt_background_image', true)) : '';
|
||||
if ($custom !== '') {
|
||||
return $custom;
|
||||
}
|
||||
$settings = aksrt_get_user_feature_settings();
|
||||
return (string) $settings['user_cover_img'];
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_get_user_description')) {
|
||||
function aksrt_get_user_description(int $user_id): string {
|
||||
$description = $user_id > 0 ? trim((string) get_user_meta($user_id, 'description', true)) : '';
|
||||
if ($description !== '') {
|
||||
return $description;
|
||||
}
|
||||
$settings = aksrt_get_user_feature_settings();
|
||||
return (string) $settings['user_desc_std'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user