'integer', 'default' => 15]); register_setting('wpavatar_marketing', 'wpavatar_commenters_size', ['type' => 'integer', 'default' => 45]); register_setting('wpavatar_marketing', 'wpavatar_users_count', ['type' => 'integer', 'default' => 15]); register_setting('wpavatar_marketing', 'wpavatar_users_size', ['type' => 'integer', 'default' => 40]); } /** * 添加前端样式 */ public static function add_frontend_styles() { echo ' '; } /** * 渲染最近评论者头像 */ public static function render_latest_commenters($atts) { $default_count = wpavatar_get_option('wpavatar_commenters_count', 15); $default_size = wpavatar_get_option('wpavatar_commenters_size', 45); $atts = shortcode_atts([ 'size' => $default_size, 'number' => $default_count, 'show_names' => 'false' // 新增参数,默认不显示评论者名称 ], $atts, 'wpavatar_latest_commenters'); // 获取全部文章的最新评论,限制为指定的评论者数量 $comments = get_comments(array( 'number' => $atts['number'] * 3, // 获取更多评论以确保有足够不重复的评论者 'status' => 'approve', )); if (empty($comments)) { return '
暂无评论用户头像
'; } // 初始化一个数组用于存储已显示的评论者 $seen_emails = array(); $output = '
'; foreach ($comments as $comment) { // 检查评论者的电子邮件地址 $comment_email = $comment->comment_author_email; // 如果该评论者已经显示过,则跳过 if (in_array($comment_email, $seen_emails) || empty($comment_email)) { continue; } // 获取评论的链接 $comment_link = get_comment_link($comment); // 创建带链接的头像 $avatar_with_link = ''; $avatar_with_link .= get_avatar($comment, $atts['size'], '', $comment->comment_author); $avatar_with_link .= ''; // 添加头像到输出 $output .= '
'; $output .= $avatar_with_link; // 根据参数决定是否显示评论者名称 if ($atts['show_names'] === 'true') { $output .= '' . esc_html($comment->comment_author) . ''; } $output .= '
'; // 记录该评论者,防止重复显示 $seen_emails[] = $comment_email; // 如果已经达到了显示数量限制,跳出循环 if (count($seen_emails) >= $atts['number']) { break; } } $output .= '
'; return $output; } /** * 渲染最新用户头像 */ public static function render_latest_users($atts) { $default_count = wpavatar_get_option('wpavatar_users_count', 15); $default_size = wpavatar_get_option('wpavatar_users_size', 40); $atts = shortcode_atts([ 'number' => $default_count, 'size' => $default_size, 'show_names' => 'true' // 新增参数,控制是否显示用户名 ], $atts); $users = get_users([ 'orderby' => 'registered', 'order' => 'DESC', 'number' => $atts['number'] ]); if (empty($users)) { return '
暂无用户
'; } $output = '
'; foreach ($users as $user) { $output .= '
'; $output .= get_avatar($user->ID, $atts['size'], '', $user->display_name); // 根据参数决定是否显示用户名 if ($atts['show_names'] === 'true') { $output .= '
' . esc_html($user->display_name) . '
'; } $output .= '
'; } $output .= '
'; return $output; } /** * 渲染随机用户头像 */ public static function render_random_users($atts) { $default_count = wpavatar_get_option('wpavatar_users_count', 15); $default_size = wpavatar_get_option('wpavatar_users_size', 40); $atts = shortcode_atts([ 'number' => $default_count, 'size' => $default_size, 'show_names' => 'true' // 新增参数,控制是否显示用户名 ], $atts); $args = [ 'orderby' => 'registered', 'order' => 'DESC', 'number' => intval($atts['number']) * 2 // 获取更多用户以便随机选择 ]; $users = get_users($args); if (empty($users)) { return '
暂无用户
'; } // 打乱用户数组以随机化顺序 shuffle($users); // 限制显示的用户数量 $users = array_slice($users, 0, intval($atts['number'])); $output = '
'; foreach ($users as $user) { $output .= '
'; $output .= get_avatar($user->ID, $atts['size'], '', $user->display_name); // 根据参数决定是否显示用户名 if ($atts['show_names'] === 'true') { $output .= '
' . esc_html($user->display_name) . '
'; } $output .= '
'; } $output .= '
'; return $output; } /** * 渲染文章作者头像 */ public static function render_author_avatar($atts) { $atts = shortcode_atts([ 'size' => '96', ], $atts); $post_id = get_the_ID(); if (!$post_id) { return ''; } $author_id = get_post_field('post_author', $post_id); if (!$author_id) { return ''; } $avatar = get_avatar($author_id, $atts['size']); return $avatar; } /** * 生成预览头像 */ public static function generate_preview($shape = 'square', $size = 96, $count = 6) { global $wpdb; // 获取最近的评论者 $sql = "SELECT DISTINCT comment_author_email, comment_author FROM {$wpdb->comments} WHERE comment_approved = '1' AND comment_author_email != '' ORDER BY comment_date DESC LIMIT %d"; $commenters = $wpdb->get_results($wpdb->prepare($sql, $count)); $output = '
'; if (!empty($commenters)) { $output .= '
'; $output .= '

最近评论者预览

'; $output .= '
'; foreach ($commenters as $commenter) { if (empty($commenter->comment_author_email)) continue; $output .= '
'; $avatar_args = [ 'class' => 'preview-avatar', 'size' => $size ]; if ($shape === 'circle') { $avatar_args['extra_attr'] = 'style="border-radius: 50%; overflow: hidden;"'; } elseif ($shape === 'rounded') { $avatar_args['extra_attr'] = 'style="border-radius: 8px; overflow: hidden;"'; } $output .= get_avatar($commenter->comment_author_email, $size, '', $commenter->comment_author, $avatar_args); $output .= '
'; } $output .= '
'; $output .= '
'; } // 获取最新用户 $recent_users = get_users([ 'orderby' => 'registered', 'order' => 'DESC', 'number' => $count ]); if (!empty($recent_users)) { $output .= '
'; $output .= '

最新用户预览

'; $output .= '
'; foreach ($recent_users as $user) { $output .= '
'; $avatar_args = [ 'class' => 'preview-avatar', 'size' => $size ]; if ($shape === 'circle') { $avatar_args['extra_attr'] = 'style="border-radius: 50%; overflow: hidden;"'; } elseif ($shape === 'rounded') { $avatar_args['extra_attr'] = 'style="border-radius: 8px; overflow: hidden;"'; } $output .= get_avatar($user->ID, $size, '', $user->display_name, $avatar_args); $output .= '' . esc_html($user->display_name) . ''; $output .= '
'; } $output .= '
'; $output .= '
'; } $output .= '
'; return $output; } /** * 渲染管理界面 */ public static function render_admin_page() { // 使用wpavatar_get_option而不是get_option获取设置 $commenters_count = wpavatar_get_option('wpavatar_commenters_count', 15); $commenters_size = wpavatar_get_option('wpavatar_commenters_size', 45); $users_count = wpavatar_get_option('wpavatar_users_count', 15); $users_size = wpavatar_get_option('wpavatar_users_size', 40); // 检查多站点网络控制 $is_option_disabled = false; if (is_multisite() && get_site_option('wpavatar_network_enabled', 1)) { $network_controlled_options = get_site_option('wpavatar_network_controlled_options', array()); if (!is_array($network_controlled_options)) { $network_controlled_options = explode(',', $network_controlled_options); } // 检查营销组件选项是否由网络控制 $is_commenters_count_disabled = in_array('wpavatar_commenters_count', $network_controlled_options) ? 'disabled' : ''; $is_commenters_size_disabled = in_array('wpavatar_commenters_size', $network_controlled_options) ? 'disabled' : ''; $is_users_count_disabled = in_array('wpavatar_users_count', $network_controlled_options) ? 'disabled' : ''; $is_users_size_disabled = in_array('wpavatar_users_size', $network_controlled_options) ? 'disabled' : ''; // 检查是否强制使用网络设置 if (get_site_option('wpavatar_network_enforce', 0)) { $is_commenters_count_disabled = 'disabled'; $is_commenters_size_disabled = 'disabled'; $is_users_count_disabled = 'disabled'; $is_users_size_disabled = 'disabled'; } } else { $is_commenters_count_disabled = ''; $is_commenters_size_disabled = ''; $is_users_count_disabled = ''; $is_users_size_disabled = ''; } ?>

>

>

>

>

[wpavatar_latest_commenters]
  • number -
  • size -
  • show_names -
[wpavatar_latest_commenters number="10" size="50" show_names="true"]
[wpavatar_latest_users]
  • number -
  • size -
  • show_names -
[wpavatar_latest_users number="12" size="40" show_names="true"]
[wpavatar_random_users]
  • number -
  • size -
  • show_names -
[wpavatar_random_users number="12" size="40" show_names="false"]
[wpavatar_author]
  • size -
[wpavatar_author size="96"]