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
@@ -79,7 +79,7 @@ final class FieldSchema {
|
||||
case 'upload':
|
||||
return esc_url_raw((string) $value);
|
||||
case 'color':
|
||||
return self::color((string) $value);
|
||||
return \AKSRT\Core\Color::sanitize($value, true);
|
||||
case 'icon':
|
||||
return function_exists('aksrt_sanitize_icon_value')
|
||||
? aksrt_sanitize_icon_value((string) $value)
|
||||
@@ -192,7 +192,7 @@ final class FieldSchema {
|
||||
private static function background($value): array {
|
||||
$value = is_array($value) ? $value : [];
|
||||
return [
|
||||
'color' => self::color((string) ($value['color'] ?? '')),
|
||||
'color' => \AKSRT\Core\Color::sanitize($value['color'] ?? '', true),
|
||||
'image' => esc_url_raw((string) ($value['image'] ?? '')),
|
||||
'position' => self::allow((string) ($value['position'] ?? 'center center'), ['center center', 'top center', 'bottom center', 'center left', 'center right'], 'center center'),
|
||||
'repeat' => self::allow((string) ($value['repeat'] ?? 'no-repeat'), ['no-repeat', 'repeat', 'repeat-x', 'repeat-y'], 'no-repeat'),
|
||||
@@ -213,18 +213,6 @@ final class FieldSchema {
|
||||
return self::normalize($children, is_array($value) ? $value : []);
|
||||
}
|
||||
|
||||
private static function color(string $value): string {
|
||||
$value = trim($value);
|
||||
if ($value === '') {
|
||||
return '';
|
||||
}
|
||||
$hex = sanitize_hex_color($value);
|
||||
if ($hex) {
|
||||
return $hex;
|
||||
}
|
||||
return preg_match('/^rgba?\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}(?:\s*,\s*(?:0|1|0?\.\d+))?\s*\)$/', $value) ? $value : '';
|
||||
}
|
||||
|
||||
private static function allow(string $value, array $allowed, string $fallback): string {
|
||||
return in_array($value, $allowed, true) ? $value : $fallback;
|
||||
}
|
||||
|
||||
@@ -221,7 +221,7 @@ final class FormRenderer {
|
||||
|
||||
private static function background(SchemaWidget $widget, string $key, array $value): void {
|
||||
echo '<div class="aksrt-widget-background">';
|
||||
self::plain_input($widget, $key . '[color]', (string) ($value['color'] ?? ''), 'text', '背景颜色');
|
||||
self::plain_input($widget, $key . '[color]', (string) ($value['color'] ?? ''), 'text', '背景颜色', 'widefat aksrt-widget-color');
|
||||
self::plain_input($widget, $key . '[image]', (string) ($value['image'] ?? ''), 'url', '背景图片 URL');
|
||||
foreach ([
|
||||
'position' => ['center center' => '居中', 'top center' => '顶部', 'bottom center' => '底部', 'center left' => '左侧', 'center right' => '右侧'],
|
||||
@@ -273,9 +273,9 @@ final class FormRenderer {
|
||||
echo '</div></details>';
|
||||
}
|
||||
|
||||
private static function plain_input(SchemaWidget $widget, string $key, string $value, string $type, string $placeholder): void {
|
||||
private static function plain_input(SchemaWidget $widget, string $key, string $value, string $type, string $placeholder, string $class = 'widefat'): void {
|
||||
$name = $widget->get_field_name($key);
|
||||
$id = $widget->get_field_id(str_replace(['[', ']'], ['-', ''], $key));
|
||||
echo '<input class="widefat" type="' . esc_attr($type) . '" id="' . esc_attr($id) . '" name="' . esc_attr($name) . '" value="' . esc_attr($value) . '" placeholder="' . esc_attr($placeholder) . '">';
|
||||
echo '<input class="' . esc_attr($class) . '" type="' . esc_attr($type) . '" id="' . esc_attr($id) . '" name="' . esc_attr($name) . '" value="' . esc_attr($value) . '" placeholder="' . esc_attr($placeholder) . '">';
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user