'star', 'label' => __('Star', 'fluentform'), ], [ 'value' => 'heart', 'label' => __('Heart', 'fluentform'), ], [ 'value' => 'thumb', 'label' => __('Thumb', 'fluentform'), ], [ 'value' => 'smile', 'label' => __('Smile', 'fluentform'), ], [ 'value' => 'bolt', 'label' => __('Bolt', 'fluentform'), ], ]; } public static function getPresetSvgs() { return [ 'star' => '', 'heart' => '', 'thumb' => '', 'smile' => '', 'bolt' => '', ]; } public static function resolveSettings($field) { $settings = ArrayHelper::get($field, 'settings'); if (!is_array($settings)) { $settings = ArrayHelper::get($field, 'raw.settings', []); } if (!is_array($settings)) { $settings = is_array($field) ? $field : []; } return [ 'icon_source' => static::normalizeIconSource(ArrayHelper::get($settings, 'icon_source')), 'icon_type' => static::normalizeIconType(ArrayHelper::get($settings, 'icon_type')), 'custom_icon_svg' => static::sanitizeCustomSvg(ArrayHelper::get($settings, 'custom_icon_svg')), 'inactive_color' => static::sanitizeColor(ArrayHelper::get($settings, 'inactive_color'), static::DEFAULT_INACTIVE_COLOR), 'active_color' => static::sanitizeColor(ArrayHelper::get($settings, 'active_color'), static::DEFAULT_ACTIVE_COLOR), ]; } public static function sanitizeColor($color, $fallback = '') { $sanitized = sanitize_hex_color((string) $color); if ($sanitized) { return $sanitized; } return $fallback ? sanitize_hex_color($fallback) : ''; } public static function normalizeIconSource($iconSource) { return sanitize_key($iconSource) === 'custom_svg' ? 'custom_svg' : 'preset'; } public static function normalizeIconType($iconType) { $iconType = sanitize_key($iconType); $presetSvgs = static::getPresetSvgs(); return isset($presetSvgs[$iconType]) ? $iconType : static::DEFAULT_ICON; } public static function sanitizeCustomSvg($svg) { if (!is_string($svg)) { return ''; } $svg = trim(preg_replace('/<\?xml.*?\?>/i', '', $svg)); if (!$svg || stripos($svg, ']*>/i', $svg, $match)) { return ''; } $existingTag = $match[0]; $existingClass = ''; if (preg_match('/\sclass=("|\')(.*?)\1/i', $existingTag, $classMatch)) { $existingClass = $classMatch[2]; } if (isset($attributes['class'])) { $attributes['class'] = trim($existingClass . ' ' . $attributes['class']); } elseif ($existingClass) { $attributes['class'] = $existingClass; } $attributes = array_merge([ 'focusable' => 'false', 'aria-hidden' => 'true', 'preserveAspectRatio' => 'xMidYMid meet', 'preserveaspectratio' => 'xMidYMid meet', ], $attributes); $attributePairs = []; foreach ($attributes as $key => $value) { if ($value === null || $value === '') { continue; } $attributePairs[] = sprintf('%s="%s"', esc_attr($key), esc_attr($value)); } $tag = preg_replace('/\sclass=("|\')(.*?)\1/i', '', $existingTag); $tag = rtrim(substr($tag, 0, -1)) . ' ' . implode(' ', $attributePairs) . '>'; return preg_replace('/]*>/i', $tag, $svg, 1); } protected static function getAllowedSvgTags() { $attributes = [ 'class' => true, 'd' => true, 'cx' => true, 'cy' => true, 'r' => true, 'rx' => true, 'ry' => true, 'x' => true, 'y' => true, 'x1' => true, 'x2' => true, 'y1' => true, 'y2' => true, 'width' => true, 'height' => true, 'viewbox' => true, 'fill' => true, 'fill-rule' => true, 'clip-rule' => true, 'stroke' => true, 'stroke-width' => true, 'stroke-linecap' => true, 'stroke-linejoin' => true, 'stroke-miterlimit' => true, 'opacity' => true, 'points' => true, 'transform' => true, 'xmlns' => true, 'xmlns:xlink' => true, 'aria-hidden' => true, 'focusable' => true, 'role' => true, 'preserveaspectratio' => true, ]; return [ 'svg' => $attributes, 'g' => $attributes, 'path' => $attributes, 'circle' => $attributes, 'rect' => $attributes, 'polygon' => $attributes, 'polyline' => $attributes, 'line' => $attributes, 'ellipse' => $attributes, 'title' => [], 'desc' => [], ]; } }