fix(vip): preserve equity HTML instead of re-splitting into list items
This commit is contained in:
1 parent
ee5a16f9a0
commit
fce5ab3088
5 files changed
+110
-102
No files matched your search
@@ -415,3 +415,80 @@ if (!function_exists('aksrt_get_vip_purchase_url')) {
|
||||
return home_url('/');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Format VIP equity HTML for front-end display.
|
||||
*
|
||||
* Admin field is free-form HTML (typically one or more <li>). Do NOT
|
||||
* re-parse / re-split the content into synthetic list items by punctuation
|
||||
* or Chinese keyword heuristics — that mangles intentional HTML.
|
||||
*
|
||||
* Rules:
|
||||
* - Sanitize with a small allowlist.
|
||||
* - If markup already has <li>, wrap the items in <ul class="v-equity …">.
|
||||
* - Otherwise output the sanitized HTML inside <div class="v-equity …">.
|
||||
* - Plain multi-line text: each non-empty line becomes one <li> (only \n split).
|
||||
* - Empty input → empty string.
|
||||
*
|
||||
* @param string $raw Equity HTML/plain from settings.
|
||||
* @param string $class Extra classes on the wrapper (e.g. "mb10 relative").
|
||||
*/
|
||||
if (!function_exists('aksrt_format_vip_equity_html')) {
|
||||
function aksrt_format_vip_equity_html(string $raw, string $class = ''): string {
|
||||
$raw = trim($raw);
|
||||
if ($raw === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
$allowed = [
|
||||
'li' => ['class' => true],
|
||||
'ul' => ['class' => true],
|
||||
'ol' => ['class' => true],
|
||||
'strong' => [],
|
||||
'b' => [],
|
||||
'em' => [],
|
||||
'i' => ['class' => true],
|
||||
'span' => ['class' => true],
|
||||
'br' => [],
|
||||
'p' => ['class' => true],
|
||||
'div' => ['class' => true],
|
||||
'a' => ['href' => true, 'target' => true, 'rel' => true, 'class' => true],
|
||||
];
|
||||
$clean = trim(wp_kses($raw, $allowed));
|
||||
if ($clean === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
$class = trim('v-equity ' . $class);
|
||||
$class_attr = esc_attr($class);
|
||||
|
||||
// Already list items — preserve them, only re-wrap.
|
||||
if (preg_match('/<li[\s>]/i', $clean)) {
|
||||
$inner = preg_replace('#</?(ul|ol)[^>]*>#i', '', $clean) ?? $clean;
|
||||
$inner = trim($inner);
|
||||
if ($inner === '') {
|
||||
return '';
|
||||
}
|
||||
return '<ul class="' . $class_attr . '">' . $inner . '</ul>';
|
||||
}
|
||||
|
||||
// Other HTML (p/br/a/…) — keep as-is, no list fabrication.
|
||||
if (preg_match('/<[^>]+>/', $clean)) {
|
||||
return '<div class="' . $class_attr . '">' . $clean . '</div>';
|
||||
}
|
||||
|
||||
// Plain text only: split by newlines (author-authored line breaks), never
|
||||
// by commas / Chinese keywords.
|
||||
$lines = preg_split('/\R+/u', $clean) ?: [];
|
||||
$lines = array_values(array_filter(array_map('trim', $lines), static fn($s) => $s !== ''));
|
||||
if ($lines === []) {
|
||||
return '';
|
||||
}
|
||||
$html = '<ul class="' . $class_attr . '">';
|
||||
foreach ($lines as $line) {
|
||||
$html .= '<li>' . esc_html($line) . '</li>';
|
||||
}
|
||||
$html .= '</ul>';
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
@@ -18,72 +18,6 @@ if (empty($vip_settings['enabled'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize equity HTML/plain text into a proper <ul> of <li>.
|
||||
*/
|
||||
$normalize_equity = static function (string $raw): string {
|
||||
$raw = trim($raw);
|
||||
if ($raw === '') {
|
||||
return '';
|
||||
}
|
||||
$allowed = [
|
||||
'li' => ['class' => true],
|
||||
'ul' => ['class' => true],
|
||||
'ol' => ['class' => true],
|
||||
'strong' => [],
|
||||
'b' => [],
|
||||
'em' => [],
|
||||
'i' => ['class' => true],
|
||||
'span' => ['class' => true],
|
||||
'br' => [],
|
||||
];
|
||||
$clean = wp_kses($raw, $allowed);
|
||||
|
||||
// Already has list items.
|
||||
if (preg_match('/<li[\s>]/i', $clean)) {
|
||||
// Strip wrapping ul/ol; we re-wrap.
|
||||
$inner = preg_replace('#</?(ul|ol)[^>]*>#i', '', $clean) ?? $clean;
|
||||
return '<ul class="v-equity">' . $inner . '</ul>';
|
||||
}
|
||||
|
||||
// Plain text: split by newlines / Chinese separators / commas.
|
||||
$plain = trim(wp_strip_all_tags($clean));
|
||||
if ($plain === '') {
|
||||
return '';
|
||||
}
|
||||
$parts = preg_split('/[\r\n]+|[;;,,||]+/u', $plain) ?: [];
|
||||
$items = [];
|
||||
foreach ($parts as $part) {
|
||||
$part = trim((string) $part);
|
||||
// Also split jammed Chinese phrases if still one long blob with known tokens.
|
||||
if ($part === '') {
|
||||
continue;
|
||||
}
|
||||
$items[] = $part;
|
||||
}
|
||||
|
||||
// Heuristic: if still a single long string without separators, try common benefit chunks.
|
||||
if (count($items) === 1 && mb_strlen($items[0]) > 16) {
|
||||
$blob = $items[0];
|
||||
$chunks = preg_split('/(?=(?:免费|会员|更高|专属|付费|下载|身份|权益|折扣|无限))/u', $blob, -1, PREG_SPLIT_NO_EMPTY) ?: [];
|
||||
$chunks = array_values(array_filter(array_map('trim', $chunks)));
|
||||
if (count($chunks) > 1) {
|
||||
$items = $chunks;
|
||||
}
|
||||
}
|
||||
|
||||
if ($items === []) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$html = '<ul class="v-equity">';
|
||||
foreach ($items as $item) {
|
||||
$html .= '<li>' . esc_html($item) . '</li>';
|
||||
}
|
||||
$html .= '</ul>';
|
||||
return $html;
|
||||
};
|
||||
|
||||
$user_id = (int) ($user_id ?? get_current_user_id());
|
||||
$level = function_exists('aksrt_get_user_vip_level') ? aksrt_get_user_vip_level($user_id) : 0;
|
||||
$expires = (int) get_user_meta($user_id, 'aksrt_vip_expires', true);
|
||||
@@ -142,14 +76,11 @@ foreach ([1, 2] as $lv) {
|
||||
}
|
||||
$lv_name = (string) ($levels[$lv]['name'] ?? ('VIP' . $lv));
|
||||
$lv_desc = (string) ($levels[$lv]['desc'] ?? '');
|
||||
// Preserve admin equity HTML as-is (sanitize/wrap only; never re-split into items).
|
||||
$equity_raw = (string) ($levels[$lv]['equity'] ?? '');
|
||||
if ($equity_raw === '') {
|
||||
// Fallback defaults when option empty / stripped.
|
||||
$equity_raw = $lv === 1
|
||||
? "免费下载部分资源\n会员专属身份标识\n付费内容会员价"
|
||||
: "更高免费下载额度\n更高等级身份标识\n更多付费内容会员价";
|
||||
}
|
||||
$equity = $normalize_equity($equity_raw);
|
||||
$equity = function_exists('aksrt_format_vip_equity_html')
|
||||
? aksrt_format_vip_equity_html($equity_raw)
|
||||
: '';
|
||||
$cta = sprintf(__('开通%s', 'aksrt'), $lv_name);
|
||||
$vip_attr = $lv;
|
||||
if (str_starts_with($btn_type, 'renew_')) {
|
||||
|
||||
Reference in new issue
Block a user