feat(user-center): deepen zibll parity and profile media
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
/**
|
||||
* Unified frontend avatar helpers.
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
if (!function_exists('aksrt_resolve_avatar_user_id')) {
|
||||
/**
|
||||
* Resolve a WordPress user ID from an avatar subject.
|
||||
*
|
||||
* @param mixed $subject User ID, WP_User, comment, post, or email.
|
||||
*/
|
||||
function aksrt_resolve_avatar_user_id($subject): int {
|
||||
if ($subject instanceof WP_User) {
|
||||
return (int) $subject->ID;
|
||||
}
|
||||
if ($subject instanceof WP_Post) {
|
||||
return (int) $subject->post_author;
|
||||
}
|
||||
if ($subject instanceof WP_Comment) {
|
||||
return (int) $subject->user_id;
|
||||
}
|
||||
if (is_object($subject) && isset($subject->user_id)) {
|
||||
return (int) $subject->user_id;
|
||||
}
|
||||
if (is_numeric($subject)) {
|
||||
return max(0, (int) $subject);
|
||||
}
|
||||
if (is_string($subject) && is_email($subject)) {
|
||||
$user = get_user_by('email', $subject);
|
||||
return $user instanceof WP_User ? (int) $user->ID : 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_get_custom_avatar_url')) {
|
||||
/**
|
||||
* Return the locally uploaded or OAuth avatar URL.
|
||||
*/
|
||||
function aksrt_get_custom_avatar_url(int $user_id, int $size = 96): string {
|
||||
if ($user_id <= 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
$oauth_avatar = esc_url_raw((string) get_user_meta($user_id, 'aksrt_oauth_avatar', true));
|
||||
return $oauth_avatar;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_get_avatar_url')) {
|
||||
/**
|
||||
* Resolve the canonical avatar URL used by all AKSRT frontend surfaces.
|
||||
*
|
||||
* @param mixed $subject User subject accepted by get_avatar_url().
|
||||
* @param array|int $args WordPress avatar args or a numeric size.
|
||||
*/
|
||||
function aksrt_get_avatar_url($subject, $args = []): string {
|
||||
if (is_numeric($args)) {
|
||||
$args = ['size' => (int) $args];
|
||||
}
|
||||
$args = is_array($args) ? $args : [];
|
||||
$size = max(16, min(512, (int) ($args['size'] ?? 96)));
|
||||
$user_id = aksrt_resolve_avatar_user_id($subject);
|
||||
$custom = aksrt_get_custom_avatar_url($user_id, $size);
|
||||
if ($custom !== '') {
|
||||
return $custom;
|
||||
}
|
||||
return (string) get_avatar_url($subject, array_merge($args, ['size' => $size]));
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_get_avatar_html')) {
|
||||
/**
|
||||
* Render a canonical avatar image.
|
||||
*
|
||||
* @param mixed $subject User subject.
|
||||
* @param int $size Pixel size.
|
||||
* @param array<string,mixed> $attrs Additional safe image attributes.
|
||||
*/
|
||||
function aksrt_get_avatar_html($subject, int $size = 96, array $attrs = []): string {
|
||||
$size = max(16, min(512, $size));
|
||||
$url = aksrt_get_avatar_url($subject, ['size' => $size]);
|
||||
if ($url === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
$defaults = [
|
||||
'class' => 'avatar avatar-' . $size . ' photo',
|
||||
'alt' => '',
|
||||
'width' => (string) $size,
|
||||
'height' => (string) $size,
|
||||
'loading' => 'lazy',
|
||||
];
|
||||
$attrs = array_merge($defaults, $attrs);
|
||||
$html_attrs = '';
|
||||
foreach ($attrs as $name => $value) {
|
||||
$name = sanitize_key((string) $name);
|
||||
if ($name === '' || $value === false || $value === null) {
|
||||
continue;
|
||||
}
|
||||
$html_attrs .= ' ' . $name . '="' . esc_attr((string) $value) . '"';
|
||||
}
|
||||
return '<img src="' . esc_url($url) . '"' . $html_attrs . ' />';
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
return $args;
|
||||
}, 20, 2);
|
||||
Reference in New Issue
Block a user