v1.0.0.1
StocketBase
- <?php
-
- declare(strict_types=1);
-
- require_once __DIR__ . '/social-icons.php';
-
- final class FooterLinks
- {
- public static function sanitizeColumns(string $rawJson): string
- {
- $decoded = json_decode($rawJson, true);
- if (!is_array($decoded)) {
- $decoded = [];
- }
-
- $clean = [];
- foreach ($decoded as $column) {
- if (!is_array($column)) {
- continue;
- }
- $title = strip_tags((string) ($column['title'] ?? ''));
- if ($title === '') {
- continue;
- }
-
- $links = [];
- foreach (is_array($column['links'] ?? null) ? $column['links'] : [] as $link) {
- if (!is_array($link)) {
- continue;
- }
- $label = strip_tags((string) ($link['label'] ?? ''));
- $url = self::cleanUrl((string) ($link['url'] ?? ''));
- if ($label === '' || $url === '') {
- continue;
- }
- $links[] = ['label' => $label, 'url' => $url];
- }
-
- if (empty($links)) {
- continue;
- }
-
- $clean[] = ['title' => $title, 'links' => $links];
- }
-
- return json_encode($clean, JSON_UNESCAPED_UNICODE) ?: '[]';
- }
-
- public static function decodeColumns(string $rawJson): array
- {
- $decoded = json_decode($rawJson, true);
- return is_array($decoded) ? $decoded : [];
- }
-
- public static function sanitizeSocialLinks(string $rawJson): string
- {
- $decoded = json_decode($rawJson, true);
- if (!is_array($decoded)) {
- $decoded = [];
- }
-
- $clean = [];
- foreach ($decoded as $item) {
- if (!is_array($item)) {
- continue;
- }
- $url = self::cleanUrl((string) ($item['url'] ?? ''));
- if ($url === '') {
- continue;
- }
-
- $key = strip_tags((string) ($item['key'] ?? ''));
- $label = strip_tags((string) ($item['label'] ?? ''));
-
- if ($key !== '' && SocialIcons::isBuiltin($key)) {
- $clean[] = ['key' => $key, 'label' => $label !== '' ? $label : $key, 'url' => $url];
- continue;
- }
-
- $svg = SocialIcons::sanitizeCustomSvg((string) ($item['svg'] ?? ''));
- if ($svg === '' || $label === '') {
- continue;
- }
-
- $clean[] = ['key' => 'custom', 'label' => $label, 'url' => $url, 'svg' => $svg];
- }
-
- return json_encode($clean, JSON_UNESCAPED_UNICODE) ?: '[]';
- }
-
- public static function decodeSocialLinks(string $rawJson): array
- {
- $decoded = json_decode($rawJson, true);
- return is_array($decoded) ? $decoded : [];
- }
-
- private static function cleanUrl(string $url): string
- {
- $url = trim($url);
- if ($url === '') {
- return '';
- }
- if (str_starts_with($url, '/') || str_starts_with($url, 'https://') || str_starts_with($url, 'http://') || str_starts_with($url, 'mailto:')) {
- return filter_var($url, FILTER_SANITIZE_URL) ?: '';
- }
- return '';
- }
- }
-