Files
wordpress-theme-aksrt/inc/helpers/comments/content.php
T
kate522 e1f176429b feat(comments): 评论区按 Zibll 1:1 全量复刻
- 评论项/表单/工具栏/筛选/分页/空状态 DOM 全部替换为 zibll 原始类名
  (li.comment、comt-main、comt-meta、list-inline、dropup 工具栏、
  theme-pagination/pagenav、post_ajax_loader 骨架等)
- 移植 zibll main.css L9412–9751 桌面 + 移动端评论样式,
  缺失基础组件(title-theme/dropdown-menu/line-form/tab-nav-theme 等)
  以 #comments 作用域补齐
- 删除全部 aksrt-comment__* 旧类名与旧样式,无兼容层
- JS 选择器对齐 zibll DOM:片段整块替换 #postcomments、骨架加载、
  跳页输入、prepend 插入、addComment onclick 剥离,
  移除 WP comment-reply enqueue 避免双 moveForm
- 表情资源替换为 zibll 全套 gif(内容逐字节一致,git 无差异)
- REST 后端 (CommentActions) 不变

验证:php -l 全部通过;5 个改动 JS 语法校验通过;
node --test tests/widget-system/post-visual-contract.test.cjs 12/12 通过
2026-08-22 13:29:51 +08:00

159 lines
5.1 KiB
PHP

<?php
/**
* Comment content filters — zibll-compatible shortcodes.
*
* Supports [g=key] GIF smilies, [img=url], [code]...[/code].
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
if (!function_exists('aksrt_comment_smilie_keys')) :
/**
* Canonical zibll smilie keys (filename without .gif).
*
* @return array<int,string>
*/
function aksrt_comment_smilie_keys(): array {
return [
'aoman', 'baiyan', 'bishi', 'bizui', 'cahan', 'ciya', 'dabing', 'daku', 'deyi', 'doge',
'fadai', 'fanu', 'fendou', 'ganga', 'guzhang', 'haixiu', 'hanxiao', 'zuohengheng', 'zhuakuang',
'zhouma', 'zhemo', 'zhayanjian', 'zaijian', 'yun', 'youhengheng', 'yiwen', 'yinxian', 'xu',
'xieyanxiao', 'xiaoku', 'xiaojiujie', 'xia', 'wunai', 'wozuimei', 'weixiao', 'weiqu', 'tuosai',
'tu', 'touxiao', 'tiaopi', 'shui', 'se', 'saorao', 'qiudale', 'qinqin', 'qiaoda', 'piezui',
'penxue', 'nanguo', 'liulei', 'liuhan', 'lenghan', 'leiben', 'kun', 'kuaikule', 'ku', 'koubi',
'kelian', 'keai', 'jingya', 'jingxi', 'jingkong', 'jie', 'huaixiao', 'haqian', 'aini', 'OK',
'qiang', 'quantou', 'shengli', 'woshou', 'gouyin', 'baoquan', 'aixin', 'bangbangtang',
'xiaoyanger', 'xigua', 'hexie', 'pijiu', 'lanqiu', 'juhua', 'hecai', 'haobang', 'caidao',
'baojin', 'chi', 'dan', 'kulou', 'shuai', 'shouqiang', 'yangtuo', 'youling',
];
}
endif;
if (!function_exists('aksrt_comment_smilie_map')) :
/**
* Smilie key => URL map (GIF under assets/img/smilies).
*
* @return array<string,string>
*/
function aksrt_comment_smilie_map(): array {
static $map = null;
if (is_array($map)) {
return $map;
}
$base = trailingslashit(get_template_directory_uri()) . 'assets/img/smilies/';
$map = [];
foreach (aksrt_comment_smilie_keys() as $key) {
$map[$key] = $base . rawurlencode($key) . '.gif';
}
return $map;
}
endif;
if (!function_exists('aksrt_comment_smilies_base_uri')) :
/**
* Base URI for smilie GIFs (localized to JS).
*/
function aksrt_comment_smilies_base_uri(): string {
return trailingslashit(get_template_directory_uri()) . 'assets/img/smilies/';
}
endif;
if (!function_exists('aksrt_render_comment_smilie')) :
/**
* Render a single [g=key] smilie as <img class="smilie-icon">.
*/
function aksrt_render_comment_smilie(string $key): string {
// Preserve case for OK.gif; sanitize other keys.
$raw = trim($key);
if ($raw === '') {
return '';
}
$lookup = $raw === 'OK' ? 'OK' : sanitize_key($raw);
// Allow original casing match for files like OK.gif.
$map = aksrt_comment_smilie_map();
if (!isset($map[$lookup]) && isset($map[$raw])) {
$lookup = $raw;
}
if (!isset($map[$lookup])) {
// Fallback: try case-insensitive.
foreach ($map as $k => $url) {
if (strcasecmp((string) $k, $raw) === 0) {
$lookup = (string) $k;
break;
}
}
}
if (!isset($map[$lookup])) {
return '[g=' . esc_html($raw) . ']';
}
$label = esc_attr('[' . $lookup . ']');
return '<img' . aksrt_get_lazy_attr('lazy_comment', (string) $map[$lookup], 'smilie-icon', aksrt_get_lazy_thumb('sm'))
. ' alt="' . $label . '" title="' . $label . '" width="30" height="30" decoding="async" data-smilie="' . esc_attr($lookup) . '">';
}
endif;
if (!function_exists('aksrt_render_comment_content')) :
/**
* Filter comment body: smilies, images, code blocks (zibll parity).
*/
function aksrt_render_comment_content(string $content): string {
// [img=url]
$rendered = preg_replace_callback(
'/\[img=(https?:\/\/[^\s\[\]]+)\]/i',
static function (array $matches): string {
return '<img' . aksrt_get_lazy_attr('lazy_comment', (string) $matches[1], 'box-img comment-inline-img', aksrt_get_lazy_thumb('lg'))
. ' alt="' . esc_attr__('评论图片', 'aksrt') . '" decoding="async">';
},
$content
);
// [code]...[/code]
$rendered = str_replace(
['[code]', '[/code]'],
['<pre class="comment-code"><code>', '</code></pre>'],
(string) $rendered
);
// [link=url target=_blank]text[/link] / [link=url]text[/link]
$rendered = preg_replace(
'/\[link=(https?:\/\/[^\s\[\]]+) target=_blank\]([^\[]*)\[\/link\]/i',
'<a href="$1" target="_blank" rel="nofollow">$2</a>',
(string) $rendered
);
$rendered = preg_replace(
'/\[link=(https?:\/\/[^\s\[\]]+)\]([^\[]*)\[\/link\]/i',
'<a href="$1" rel="nofollow">$2</a>',
(string) $rendered
);
// [g=key]
$rendered = preg_replace_callback(
'/\[g=([^\]]+)\]/i',
static function (array $matches): string {
return aksrt_render_comment_smilie(trim((string) $matches[1]));
},
(string) $rendered
);
$allowed = wp_kses_allowed_html('post');
if (!isset($allowed['img'])) {
$allowed['img'] = [];
}
$allowed['img'] = array_merge($allowed['img'], [
'class' => true,
'src' => true,
'alt' => true,
'title' => true,
'width' => true,
'height' => true,
'data-src' => true,
'decoding' => true,
'data-smilie' => true,
'data-src' => true,
]);
$allowed['pre'] = array_merge($allowed['pre'] ?? [], ['class' => true]);
$allowed['code'] = $allowed['code'] ?? [];
return wp_kses((string) $rendered, $allowed);
}
endif;