Extract shared comment helpers (content/item/form/geo/actions), port GIF [g=] smilies + FixedInput/moveForm UX, wire close_comments/city/quick settings, REST comment get, and align product review chrome with zibll.
148 lines
4.5 KiB
PHP
148 lines
4.5 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) . ']';
|
|
}
|
|
return sprintf(
|
|
'<img class="smilie-icon" src="%1$s" alt="%2$s" title="%2$s" width="30" height="30" loading="lazy" decoding="async" data-smilie="%3$s" />',
|
|
esc_url($map[$lookup]),
|
|
esc_attr('[' . $lookup . ']'),
|
|
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(
|
|
'/\[img=(https?:\/\/[^\s\[\]]+)\]/i',
|
|
'<img class="box-img comment-inline-img" src="$1" alt="' . esc_attr__('评论图片', 'aksrt') . '" loading="lazy" decoding="async" />',
|
|
$content
|
|
);
|
|
// [code]...[/code]
|
|
$rendered = str_replace(
|
|
['[code]', '[/code]'],
|
|
['<pre class="comment-code"><code>', '</code></pre>'],
|
|
(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,
|
|
'loading' => 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;
|