Files
2026-08-26 14:21:32 +08:00

64 lines
1.8 KiB
PHP

<?php
/**
* Comment content filters — legacy-compatible shortcodes.
*
* Supports [img=url] and [code]...[/code].
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
if (!function_exists('aksrt_render_comment_content')) :
/**
* Filter comment body: images and code blocks (legacy 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
);
$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-src' => true,
]);
$allowed['pre'] = array_merge($allowed['pre'] ?? [], ['class' => true]);
$allowed['code'] = $allowed['code'] ?? [];
return wp_kses((string) $rendered, $allowed);
}
endif;