[1 => true, 3 => true, 4 => true, 5 => true], limited to [$first, $last]. Mirrors parseLineRanges(). * * @return array */ function zadex_code_highlight_lines(string $spec, int $first, int $last): array { $out = []; foreach (explode(',', $spec) as $part) { if (!preg_match('/^\s*(\d+)\s*(?:-\s*(\d+)\s*)?$/', $part, $zx_m)) { continue; } $a = (int) $zx_m[1]; $b = isset($zx_m[2]) && $zx_m[2] !== '' ? (int) $zx_m[2] : $a; if ($a > $b) { [$a, $b] = [$b, $a]; } for ($n = max($a, $first); $n <= min($b, $last); $n++) { $out[$n] = true; } } return $out; } } $languages = ['html', 'css', 'js', 'php', 'json', 'bash', 'python', 'text']; $language = in_array($settings['language'] ?? 'js', $languages, true) ? $settings['language'] : 'text'; $code = is_scalar($settings['code'] ?? null) ? (string) $settings['code'] : ''; $code = rtrim((string) preg_replace('/\r\n?/', "\n", $code), "\n"); if (trim($code) === '') { return; } $theme = ($settings['theme'] ?? 'dark') === 'light' ? 'light' : 'dark'; $numbers = !empty($settings['line_numbers']); $wrap = !empty($settings['word_wrap']); $copy = !empty($settings['copy_button']); $start = max(1, (int) ($settings['start_line'] ?? 1)); $lines = explode("\n", $code); $last = $start + count($lines) - 1; $highlighted = zadex_code_highlight_lines((string) ($settings['highlight_lines'] ?? ''), $start, $last); $classes = 'zx-code-highlight--' . $theme . ($numbers ? ' zx-code-highlight--numbers' : '') . ($wrap ? ' zx-code-highlight--wrap' : ''); $attrs = preg_replace('/class="/', 'class="' . esc_attr($classes) . ' ', $attrs, 1); $body = ''; foreach ($lines as $i => $line) { $n = $start + $i; // htmlspecialchars (double encoding on) instead of esc_html: code like "&" must stay visible as written. $body .= '' . htmlspecialchars($line, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . ''; } $copy_html = ''; if ($copy) { $copy_label = trim((string) ($settings['copy_label'] ?? '')) !== '' ? (string) $settings['copy_label'] : __('Copy', 'zadex'); $copied_label = trim((string) ($settings['copied_label'] ?? '')) !== '' ? (string) $settings['copied_label'] : __('Copied!', 'zadex'); $copy_html = '' . ''; } $digits = strlen((string) $last); $zx_html = '
' . $copy_html . '
'
    . '' . $body . "
\n"; echo wp_kses($zx_html, \Zadex\Frontend\Kses::allowed_html());