修改qkua主题评论表情包添加方式

前言:用过这个功能的都知道,对于批量添加表情包有点不友好,那就改的像缩略图呗。当然,以下的方法只是为了维持原有框架而修改,表情包添加方案是很多的。

效果图:

一、覆盖原有filter

在主题目录functions.php最后一行插入代码:

add_filter('qk_comment_filters', function ($comment_text) {
    // 获取缓存的表情包列表
    $cache_key = 'emoji_list_cache';
    $emoticon_packs = qk_get_option("comment_can_cache")? get_transient($cache_key):false;

    // 如果缓存不存在,重新生成表情包列表
    if (!$emoticon_packs) {
        $emoticon_packs = Qk\Modules\Common\RestApi::getEmojiList(); // 替换为获取表情包列表的方法
        if (is_wp_error($emoticon_packs)) {
            return $comment_text; // 如果获取失败,返回原始评论
        }
        $emoticon_packs = $emoticon_packs->get_data();
    }

    // 检查是否有表情包数据
    if (empty($emoticon_packs['list'])) {
        return $comment_text;
    }
    // 构建表情符号和图标的映射数组
    $emoticon_map = [];
    foreach ($emoticon_packs['list'] as $pack) {
        if (!isset($pack['list']) || !is_array($pack['list'])) {
            continue;
        }
        foreach ($pack['list'] as $emoticon) {
            if (isset($emoticon['name']) && isset($emoticon['icon'])) {
                $emoticon_map[$emoticon['name']] = $emoticon['icon'];
                $emoticon_map['size'] = $pack['size'];
                
            }
        }
    }

    // 替换评论中的表情标记为图片标签
    $comment_text = preg_replace_callback('/\[(.*?)\]/', function ($matches) use ($emoticon_map) {
        $name = $matches[1]; // 提取表情的 name
        if (isset($emoticon_map[$name])) {
            $icon_url = esc_url($emoticon_map[$name]);
            $alt_text = esc_attr($name);
            return '<img src="' . $icon_url . '" alt="' . $alt_text . '" class="emoticon-image '.$emoticon_map['size'].'">';
        }
        return $matches[0]; // 如果未找到匹配,保留原始文本
    }, $comment_text);

    // 返回处理后的评论文本
    return $comment_text;
},20);

注:优先级20,亲测可行

二、修改Modules\Common\RestApi.php

进入文件RestApi.php,搜索getEmojiList,删除这个函数,然后在原来位置添加代码:

    public static function getEmojiList() {
        $cache_key = 'emoji_list_cache';
        if(qk_get_option("comment_can_cache") == 1){
        // 检查是否已有缓存
        $cached_data = get_transient($cache_key);
    
        // 如果缓存存在且有效,直接返回缓存数据
        if ($cached_data) {
            return new \WP_REST_Response($cached_data, 200);
        }
       }
    
        // 获取配置项
        $smilies = qk_get_option('comment_smilies_arg');
    
        // 初始化结果数组
        $res = [];
    
        if (is_array($smilies) && $smilies) {
            $res['list'] = [];
    
            foreach ($smilies as $smiley) {
                // 初始化当前表情组
                $group = [
                    'name' => $smiley['name'] ?? '未命名表情组',
                    'size' => $smiley['size'] ?? 'default',
                    'list' => [],
                ];
    
                // 确保 `gallery` 存在
                if (isset($smiley['gallery'])) {
                    // 将字符串拆分成数组
                    $gallery_ids = explode(',', $smiley['gallery']);
    
                    // 批量获取图片 URL 和名称
                    $attachments = get_posts([
                        'post_type'      => 'attachment',
                        'post__in'       => $gallery_ids,
                        'post_status'    => 'inherit',
                        'fields'         => 'ids',
                        'numberposts'    => -1,
                    ]);
    
                    // 创建 ID => URL 的映射
                    $url_map = [];
                    foreach ($attachments as $attachment_id) {
                        $url_map[$attachment_id] = wp_get_attachment_url($attachment_id);
                    }
    
                    // 遍历图片 ID 列表
                    foreach ($gallery_ids as $id) {
                        if (isset($url_map[$id])) {
                            $img_url = $url_map[$id];
                            $img_name = pathinfo(basename($img_url), PATHINFO_FILENAME);
    
                            $group['list'][] = [
                                'name' => $img_name,
                                'icon' => $img_url,
                            ];
                        }
                    }
                }
    
                // 将当前表情组添加到结果列表
                $res['list'][] = $group;
            }
            // 设置缓存,1 小时(3600 秒)
            set_transient($cache_key, $res, 3600);
        } else {
            $res['error'] = "暂未有表情设置";
        }
    
        // 错误处理或返回结果
        if (isset($res['error'])) {
            return new \WP_Error('comment_error', $res['error'], ['status' => 403]);
        } else {
            return new \WP_REST_Response($res, 200);
        }
    }
    

三、修改Modules\Settings\Template.php

进入Template.php,搜索comment_smilies_arg,把整个数组删掉,然后在原有位置添加代码:

                
                   array(
                    'id'         => 'comment_can_cache',
                    'title'      => '开启缓存列表',
                    'type'       => 'switcher',
                    'help'       => '如果表情包很多,体验不好?',
                    'default'    => true,
                    'dependency' => array('comment_close|comment_use_smiles', '==|!=', '', '', 'visible'),
                ),
                   array(
                    'id'     => 'comment_smilies_arg',
                    'type'   => 'group',
                    'title'  => ' ',
                    'subtitle'   => '表情组',
                    'class'      => 'compact',
                    'button_title' => '新增表情组',
                    'fields' => array(
                        array(
                            'id'    => 'name',
                            'type'  => 'text',
                            'title' => '表情组名'
                        ),
                        array(
                            'id'         => 'size',
                            'type'       => 'radio',
                            'title'      => '表情大小',
                            'options'    => array(
                                'small' => '小(24 x 24)px',
                                'normal' => '中(50 x 50)px',
                                'large' => '大(80 x 80)px',
                            ),
                            'default'    => 'normal'
                        ),
                        array(
                            'id'          => 'gallery',
                            'type'        => 'gallery',
                            'title'       => '表情相册',
                            'add_title'   => '新增表情',
                            'edit_title'  => '编辑表情',
                            'clear_title' => '清空表情',
                            'default'     => false,
                        ),
                    ),
                    'default'   => array(
                        array(
                            'name' => '',
                        ),
                    ),
                    'dependency' => array('comment_close|comment_use_smiles', '==|!=', '', '', 'visible'),
                ),

结束

以上代码均来自ChatGPT,亲测可行,如果不行有没有可能在复制过程又出了什么问题?关于这个主题,只要你能想到的想实现的美化,都可以和我交流。(希望新版本快点到来!)

上一篇

PHP 如何进行二维数组搜索和判断值是否存在

下一篇

【置顶测试】分类数据只做主题功能演示使用,不做其他任何用途
相关推荐
WordPress 自定义查询 WP_Query 所有参数详细注释及使用方法详解
wordpress实现不同分类设置不同的每页显示文章数量
WordPress 自定义文章类型终极教程
WordPress中实现分词搜索,可以使用插件或自定义代码来实现
WordPress 深入了解WP_User_Query:属性、方法、所有参数及使用示例
WordPress 发布文章百度收录自动提交代码
评论(0)
游客的头像
表情
全部评论 只看作者
最新热门
  1. 暂时还没有评论哦