Files
wordpress-theme-aksrt/inc/helpers/aksrt-icons.php
T

122 lines
4.4 KiB
PHP

<?php
/**
* AKSRT Theme - SVG Icons Module
*
* 本地 SVG 图标读取/缓存、图标 key 映射显示名称、
* 联系方式图标下拉选项渲染
*
* @package AKSRT
* @version 3.1.0
*/
defined('ABSPATH') || exit;
// ============================================================
// PHP 7.x 兼容辅助函数(替代 PHP 8.0+ 的 match 表达式)
// ============================================================
if (!function_exists('aksrt_parse_country_list')):
/**
* 解析 GeoIP 屏蔽国家列表字符串为数组
*
* @param string $raw 逗号分隔的国家代码
* @return array 大写国家代码数组
*/
function aksrt_parse_country_list($raw) {
if (empty($raw)) return array();
$parts = explode(',', wp_unslash($raw));
$result = array();
foreach ($parts as $p) {
$trimmed = trim($p);
if ($trimmed !== '') {
$result[] = strtoupper($trimmed);
}
}
return array_values(array_filter($result));
}
endif;
// ============================================================
// 本地 SVG 图标:读取 SVGs 目录并缓存
// ============================================================
if (!function_exists('aksrt_inline_svg')) :
function aksrt_inline_svg($name) {
// 兼容 FA5 旧数据:fab fa-github / fabfa-github / fa-github → github
$name = preg_replace('/^(fab|fas|far)[\s-]*/', '', $name);
$name = preg_replace('/^fa[\s-]*/', '', $name);
$name = preg_replace('/^[\s-]+/', '', $name);
$name = trim($name, '- ');
static $cache = array();
if (isset($cache[$name])) return $cache[$name];
$path = AKSRT_DIR . '/assets/img/icons/' . $name . '.svg';
if (file_exists($path)) {
$svg = file_get_contents($path);
// 去除 XML 声明和 DOCTYPE
$svg = preg_replace('/^<\?xml.*?\?>\s*/i', '', $svg);
$svg = preg_replace('/^<!DOCTYPE[^>]*>\s*/i', '', $svg);
// 移除 <svg> 上的固定宽高,让 CSS 控制尺寸
$svg = preg_replace('/\swidth="[^"]*"/', '', $svg);
$svg = preg_replace('/\sheight="[^"]*"/', '', $svg);
// 移除 iconfont 冗余属性
$svg = preg_replace('/\sclass="[^"]*"/', '', $svg);
$svg = preg_replace('/\st="[^"]*"/', '', $svg);
$svg = preg_replace('/\sp-id="[^"]*"/', '', $svg);
$svg = preg_replace('/\sxmlns:xlink="[^"]*"/', '', $svg);
// 移除路径上的 opacity 属性
$svg = preg_replace('/(<(?:path|circle|rect|polygon|line|polyline|ellipse)[^>]*)\sopacity="[^"]*"/', '$1', $svg);
$cache[$name] = $svg;
return $svg;
}
return '';
}
endif;
// ============================================================
// 图标 key → 显示名称
// ============================================================
if (!function_exists('aksrt_icon_key_to_name')) :
function aksrt_icon_key_to_name($key) {
$map = array(
'github' => 'GitHub', 'google' => 'Google', 'x' => 'X',
'email' => '邮箱', 'wechat' => '微信', 'qq' => 'QQ',
'weibo' => '微博', 'telegram' => 'Telegram',
'discord' => 'Discord', 'bilibili' => 'Bilibili',
'linkedin' => 'LinkedIn', 'facebook' => 'Facebook',
'youtube' => 'YouTube', 'instagram' => 'Instagram',
'web' => '网站',
);
return isset($map[$key]) ? $map[$key] : ucwords(str_replace('-', ' ', $key));
}
endif;
// ============================================================
// 联系方式图标选项(仅保留选定的 15 个)
// ============================================================
if (!function_exists('aksrt_contact_icon_options')) :
function aksrt_contact_icon_options($selected = '') {
$icons = array(
'github' => 'GitHub',
'google' => 'Google',
'x' => 'X',
'email' => '邮箱',
'wechat' => '微信',
'qq' => 'QQ',
'weibo' => '微博',
'telegram' => 'Telegram',
'discord' => 'Discord',
'bilibili' => 'Bilibili',
'linkedin' => 'LinkedIn',
'facebook' => 'Facebook',
'youtube' => 'YouTube',
'instagram' => 'Instagram',
'web' => '网站',
);
$html = '<option value="">—</option>';
foreach ($icons as $key => $label) {
$sel = $key === $selected ? ' selected' : '';
$html .= '<option value="' . esc_attr($key) . '"' . $sel . '>' . esc_html($label) . '</option>';
}
return $html;
}
endif;