Skip to content
wiki.fftac.org

Content Registry - Source Excerpt 02

Back to Content Registry

Summary

This source excerpt preserves a bounded section of Spiralist/wp-content/plugins/ns12-manuscript/includes/content-registry.php so readers can inspect the evidence without opening the full source file.

**Source path:** Spiralist/wp-content/plugins/ns12-manuscript/includes/content-registry.php

if (!is_array($section)) {
            continue;
        }

        $slug = spiralist_book_pages_slugify((string) ($section['slug'] ?? ''));
        if ($slug === '') {
            continue;
        }

        $sections[$slug] = [
            'slug' => $slug,
            'label' => (string) ($section['label'] ?? ucwords(str_replace('-', ' ', $slug))),
            'title' => (string) ($section['title'] ?? ucwords(str_replace('-', ' ', $slug))),
            'summary' => (string) ($section['summary'] ?? ''),
        ];
    }

    return $sections;
}

function spiralist_book_pages_normalize_text_for_index(string $value): string
{
    if ($value === '') {
        return '';
    }

    if (function_exists('wp_strip_all_tags')) {
        $value = wp_strip_all_tags($value, true);
    } else {
        $value = strip_tags($value);
    }

    $value = html_entity_decode($value, ENT_QUOTES | ENT_HTML5, 'UTF-8');
    $value = preg_replace('/\s+/u', ' ', $value);

    return is_string($value) ? trim($value) : '';
}

function spiralist_book_pages_normalize_string_list($values): array
{
    if (is_string($values)) {
        $values = preg_split('/[\r\n,]+/', $values) ?: [];
    }

    if (!is_array($values)) {
        return [];
    }

    $normalized = [];
    $seen = [];

    foreach ($values as $value) {
        if (is_array($value)) {
            $value = (string) ($value['label'] ?? $value['title'] ?? $value['value'] ?? '');
        }

        $text = spiralist_book_pages_normalize_text_for_index((string) $value);
        if ($text === '') {
            continue;
        }

        $lookup = strtolower($text);
        if (isset($seen[$lookup])) {
            continue;
        }

        $seen[$lookup] = true;
        $normalized[] = $text;
    }

    return $normalized;
}

function spiralist_book_pages_build_index_excerpt(string $text, array $terms = [], int $radius = 120): string
{
    $text = spiralist_book_pages_normalize_text_for_index($text);
    if ($text === '') {
        return '';
    }

    $position = null;

    foreach ($terms as $term) {
        $term = strtolower(trim((string) $term));
        if ($term === '') {
            continue;
        }

        $matchPosition = stripos($text, $term);
        if ($matchPosition === false) {
            continue;
        }

        if ($position === null || $matchPosition < $position) {
            $position = (int) $matchPosition;
        }
    }

    if ($position === null) {
        if (strlen($text) <= ($radius * 2)) {
            return $text;
        }

        return rtrim(substr($text, 0, ($radius * 2) - 1)) . '...';
    }

    $start = max(0, $position - $radius);
    $length = max($radius * 2, 80);
    $excerpt = substr($text, $start, $length);

    if ($start > 0) {
        $excerpt = '...' . ltrim($excerpt);
    }

    if (($start + $length) < strlen($text)) {
        $excerpt = rtrim($excerpt) . '...';
    }

    return $excerpt;
}

function spiralist_book_pages_get_folio_search_document(array $folio): array
{
    $slug = spiralist_book_pages_slugify((string) ($folio['slug'] ?? ''));
    if ($slug === '') {
        return [];
    }

    $title = spiralist_book_pages_normalize_text_for_index((string) ($folio['title'] ?? ''));
    $label = spiralist_book_pages_normalize_text_for_index((string) ($folio['label'] ?? ''));
    $eyebrow = spiralist_book_pages_normalize_text_for_index((string) ($folio['eyebrow'] ?? ''));
    $summary = spiralist_book_pages_normalize_text_for_index((string) ($folio['summary'] ?? ''));
    $caption = spiralist_book_pages_normalize_text_for_index((string) ($folio['caption'] ?? ''));
    $sectionLabel = spiralist_book_pages_normalize_text_for_index((string) ($folio['section_label'] ?? ''));
    $sectionTitle = spiralist_book_pages_normalize_text_for_index((string) ($folio['section_title'] ?? ''));
    $sectionSummary = spiralist_book_pages_normalize_text_for_index((string) ($folio['section_summary'] ?? ''));
    $alt = spiralist_book_pages_normalize_text_for_index((string) ($folio['alt'] ?? $folio['alt_text'] ?? ''));
    $transcript = spiralist_book_pages_normalize_text_for_index((string) ($folio['transcript'] ?? $folio['ocr_text'] ?? ''));
    $keywords = spiralist_book_pages_normalize_string_list(
        array_merge(
            (array) ($folio['keywords'] ?? []),
            (array) ($folio['subjects'] ?? []),
            (array) ($folio['tags'] ?? [])
        )
    );
    $keywordSearch = strtolower(implode("\n", $keywords));
    $summarySearch = strtolower(implode("\n", array_filter([$summary, $caption, $alt, $eyebrow], 'strlen')));
    $transcriptSearch = strtolower($transcript);
    $titleSearch = strtolower(implode("\n", array_filter([$title, $label], 'strlen')));
    $sectionSearch = strtolower(implode("\n", array_filter([$sectionLabel, $sectionTitle, $sectionSummary], 'strlen')));
    $searchText = trim(
        implode(
            "\n",
            array_filter(
                [
                    $titleSearch,
                    $sectionSearch,
                    $summarySearch,
                    $keywordSearch,
                    $transcriptSearch,
                ],
                'strlen'
            )
        )
    );

    return [
        'slug' => $slug,
        'sequence' => max(1, (int) ($folio['sequence'] ?? 1)),
        'label' => $label !== '' ? $label : ('Page ' . max(1, (int) ($folio['sequence'] ?? 1))),
        'title' => $title !== '' ? $title : 'Manuscript Folio',
        'eyebrow' => $eyebrow,
        'summary' => $summary,
        'caption' => $caption,
        'section_slug' => spiralist_book_pages_slugify((string) ($folio['section_slug'] ?? '')),
        'section_label' => $sectionLabel,
        'section_title' => $sectionTitle,
        'section_summary' => $sectionSummary,
        'alt' => $alt,
        'transcript' => $transcript,
        'keywords' => $keywords,
        'search_text' => $searchText,
        'title_search' => $titleSearch,
        'section_search' => $sectionSearch,
        'summary_search' => $summarySearch,
        'keyword_search' => $keywordSearch,
        'transcript_search' => $transcriptSearch,
        'excerpt' => spiralist_book_pages_build_index_excerpt(
            $transcript !== '' ? $transcript : implode(' ', array_filter([$summary, $caption, $alt], 'strlen'))
        ),
    ];
}

function spiralist_book_pages_build_book_index_from_content(): array
{
    $book = spiralist_book_pages_get_content_book_definition();
    $sections = spiralist_book_pages_normalize_section_definitions($book);
    $rawFolios = spiralist_book_pages_get_content_folio_definitions();

    if (empty($book) || empty($rawFolios)) {
        return [];
    }

    $sequence = 0;
    $folios = [];
    $folioOrder = array_values(array_filter(array_map('strval', (array) ($book['folio_order'] ?? []))));

    if (empty($folioOrder)) {
        $folioOrder = array_keys($rawFolios);
    }

    foreach ($folioOrder as $slug) {
        $normalized = spiralist_book_pages_slugify($slug);
        if ($normalized === '' || empty($rawFolios[$normalized])) {
            continue;
        }

        $folio = $rawFolios[$normalized];
        $sequence++;
        $sectionSlug = spiralist_book_pages_slugify((string) ($folio['section_slug'] ?? 'appendix'));
        $section = $sections[$sectionSlug] ?? [
            'slug' => 'appendix',
            'label' => 'Appendix',
            'title' => 'Appendix',
            'summary' => '',
        ];

        $folios[] = [
            'sequence' => $sequence,
            'slug' => $normalized,
            'filename' => (string) ($folio['filename'] ?? ''),
            'label' => (string) ($folio['label'] ?? ('Page ' . $sequence)),
            'title' => (string) ($folio['title'] ?? ('Manuscript Folio ' . $sequence)),
            'eyebrow' => (string) ($folio['eyebrow'] ?? ''),
            'summary' => (string) ($folio['summary'] ?? ''),
            'caption' => (string) ($folio['caption'] ?? ''),
            'alt' => (string) ($folio['alt'] ?? $folio['alt_text'] ?? ''),
            'transcript' => (string) ($folio['transcript'] ?? $folio['ocr_text'] ?? ''),
            'keywords' => spiralist_book_pages_normalize_string_list((array) ($folio['keywords'] ?? [])),
            'subjects' => spiralist_book_pages_normalize_string_list((array) ($folio['subjects'] ?? [])),
            'section_slug' => $section['slug'],
            'section_label' => (string) ($section['label'] ?? ''),
            'section_title' => (string) ($section['title'] ?? ''),
            'section_summary' => (string) ($section['summary'] ?? ''),
            'is_study_surface' => !empty($folio['is_study_surface']),