refactor(color): unify color sanitizing and picking behind shared components
- Add AKSRT\Core\Color::sanitize($value, $allow_alpha) as the single PHP color sanitizer; replace sanitize_hex_color calls and per-class regexes (FloatButtons, FieldSchema, ThemeSearch, icon cards, TicketStatusService) - SchemaProvider / ControllerBase sanitize `color` fields as colors; alpha kept only when the schema sets showAlpha - Split SchemaProvider OAuth validation and option matching into sibling traits to stay under the 1000-line limit - Add AksColorPicker.vue + utils/color.js mirroring the PHP rules; AksField, FloatBtnCommonFields and TicketStatusPanel use it - Widget background color input now uses wpColorPicker - Remove dead AksCompositeField dimensions/spacing/background branches and the unused Gutenberg ColorPicker var - Add color-unification contract test; update harness requires and tests Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
74021d26bb
commit
57ff405691
32 files changed
+522
-456
No files matched your search
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
/**
|
||||
* 全站唯一的颜色值净化器(SPA 设置、小工具、悬浮按钮、工单状态、前台渲染共用)。
|
||||
*
|
||||
* 前端对应实现:assets/admin/src/utils/color.js(normalizeColor),两端规则保持一致。
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
namespace AKSRT\Core;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
final class Color {
|
||||
|
||||
private const HEX = '/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i';
|
||||
private const HEX_ALPHA = '/^#?([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i';
|
||||
private const RGB = '/^rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(?:,\s*(0(?:\.\d+)?|1(?:\.0+)?|\.\d+)\s*)?\)$/i';
|
||||
|
||||
/**
|
||||
* 返回规范化颜色;非法值返回空串。
|
||||
*
|
||||
* - 默认只接受 #rgb / #rrggbb(可省略 #),输出小写并补 #。
|
||||
* - $allow_alpha 时额外接受 #rgba / #rrggbbaa 与 rgb()/rgba(),
|
||||
* rgb 形态统一输出为 `rgb(r, g, b)` / `rgba(r, g, b, a)`。
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public static function sanitize($value, bool $allow_alpha = false): string {
|
||||
if (!is_string($value)) {
|
||||
return '';
|
||||
}
|
||||
$value = trim($value);
|
||||
if ($value === '') {
|
||||
return '';
|
||||
}
|
||||
if (preg_match($allow_alpha ? self::HEX_ALPHA : self::HEX, $value, $m) === 1) {
|
||||
return '#' . strtolower($m[1]);
|
||||
}
|
||||
if (!$allow_alpha || preg_match(self::RGB, $value, $m) !== 1) {
|
||||
return '';
|
||||
}
|
||||
$channels = [(int) $m[1], (int) $m[2], (int) $m[3]];
|
||||
foreach ($channels as $channel) {
|
||||
if ($channel > 255) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
if (!isset($m[4]) || $m[4] === '') {
|
||||
return 'rgb(' . implode(', ', $channels) . ')';
|
||||
}
|
||||
$alpha = rtrim(rtrim(number_format((float) $m[4], 3, '.', ''), '0'), '.');
|
||||
return 'rgba(' . implode(', ', $channels) . ', ' . ($alpha === '' ? '0' : $alpha) . ')';
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user