[], 'b' => [], 'em' => [], 'i' => [], 'mark' => [], 'small' => [], 'br' => [], 'a' => ['href' => true, 'target' => true, 'rel' => true], ]; public static function sanitize_graph(array $graph): array { $clean = self::sanitize_tree($graph, 0); return [ 'nodes' => array_values(array_filter( (array) ($clean['nodes'] ?? []), static fn ($node): bool => is_array($node) && isset($node['id'], $node['type']) && is_string($node['id']) && is_string($node['type']) )), 'edges' => array_values(array_filter( (array) ($clean['edges'] ?? []), static fn ($edge): bool => is_array($edge) && isset($edge['source'], $edge['target']) )), ] + $clean; } public static function sanitize_settings(array $settings): array { return self::sanitize_tree($settings, 0); } private static function sanitize_tree(array $tree, int $depth): array { if ($depth > self::MAX_DEPTH) { return []; } $clean = []; $count = 0; foreach ($tree as $key => $value) { if (++$count > self::MAX_ITEMS) { break; } if (is_string($key) && ! preg_match('/^[\w.-]{1,64}$/', $key)) { continue; } if (is_array($value)) { $clean[$key] = self::sanitize_tree($value, $depth + 1); } elseif (is_string($value)) { $clean[$key] = self::sanitize_string((string) $key, $value); } elseif (is_bool($value) || is_int($value) || is_float($value) || null === $value) { $clean[$key] = $value; } } return $clean; } private static function sanitize_string(string $key, string $value): string { $value = substr($value, 0, self::MAX_STRING); if ('url' === $key || str_ends_with($key, '_url')) { $value = trim($value); if ('' === $value || preg_match('/^\{[\w.-]+\}$/', $value)) { return $value; } return esc_url_raw($value, ['http', 'https', 'mailto', 'tel']); } if (str_contains($value, '<')) { return wp_kses($value, self::RICH_TAGS, ['http', 'https', 'mailto', 'tel']); } return $value; } }