Skip to content
wiki.fftac.org

Runtime - Source Excerpt 15

Back to Runtime

Summary

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

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

function spiralist_book_pages_get_manuscript_page_asset_by_filename(string $filename): array
{
    $normalized = strtolower(trim($filename));
    if ($normalized === '') {
        return [];
    }

    foreach (spiralist_book_pages_get_manuscript_page_assets() as $page) {
        if ($normalized === strtolower((string) ($page['filename'] ?? ''))) {
            return $page;
        }
    }

    return [];
}

function spiralist_book_pages_get_manuscript_page_asset_by_slug(string $slug): array
{
    $normalized = sanitize_title($slug);
    if ($normalized === '') {
        return [];
    }

    foreach (spiralist_book_pages_get_manuscript_page_assets() as $page) {
        if ($normalized === (string) ($page['slug'] ?? '')) {
            return $page;
        }
    }

    return [];
}

function spiralist_book_pages_get_manuscript_page_asset_by_sequence(int $sequence): array
{
    if ($sequence < 1) {
        return [];
    }

    foreach (spiralist_book_pages_get_manuscript_page_assets() as $page) {
        if ((int) ($page['sequence'] ?? 0) === $sequence) {
            return $page;
        }
    }

    return [];
}

function spiralist_book_pages_get_manuscript_search_api_url(): string
{
    return function_exists('spiralist_book_pages_get_rest_url')
        ? spiralist_book_pages_get_rest_url('manuscript/search')
        : rest_url(spiralist_book_pages_get_rest_namespace() . '/manuscript/search');
}

function spiralist_book_pages_get_manuscript_manifest_url(): string
{
    return function_exists('spiralist_book_pages_get_rest_url')
        ? spiralist_book_pages_get_rest_url('manuscript/manifest')
        : rest_url(spiralist_book_pages_get_rest_namespace() . '/manuscript/manifest');
}

function spiralist_book_pages_get_manuscript_iiif_search_url(): string
{
    return function_exists('spiralist_book_pages_get_rest_url')
        ? spiralist_book_pages_get_rest_url('manuscript/iiif-search')
        : rest_url(spiralist_book_pages_get_rest_namespace() . '/manuscript/iiif-search');
}

function spiralist_book_pages_get_manuscript_iiif_label_map(string $value): array
{
    $value = trim($value);

    return [
        'en' => [$value !== '' ? $value : 'Untitled'],
    ];
}

function spiralist_book_pages_get_manuscript_iiif_metadata_entry(string $label, string $value): array
{
    return [
        'label' => spiralist_book_pages_get_manuscript_iiif_label_map($label),
        'value' => spiralist_book_pages_get_manuscript_iiif_label_map($value),
    ];
}

function spiralist_book_pages_get_manuscript_iiif_canvas_id(array $page): string
{
    $slug = sanitize_title((string) ($page['slug'] ?? 'folio'));
    $base = untrailingslashit(spiralist_book_pages_get_manuscript_manifest_url());

    return $base . '/canvas/' . rawurlencode($slug);
}

function spiralist_book_pages_get_manuscript_iiif_image_format(array $page): string
{
    $filename = (string) ($page['filename'] ?? wp_parse_url((string) ($page['url'] ?? ''), PHP_URL_PATH));
    $extension = strtolower((string) pathinfo($filename, PATHINFO_EXTENSION));

    switch ($extension) {
        case 'jpg':
        case 'jpeg':
            return 'image/jpeg';
        case 'webp':
            return 'image/webp';
        case 'gif':
            return 'image/gif';
        case 'tif':
        case 'tiff':
            return 'image/tiff';
        case 'png':
        default:
            return 'image/png';
    }
}

function spiralist_book_pages_get_manuscript_manifest_payload(): array
{
    $pages = spiralist_book_pages_get_manuscript_page_assets();
    $book = function_exists('spiralist_book_pages_get_book_content_index')
        ? spiralist_book_pages_get_book_content_index()
        : [];
    $manifestUrl = spiralist_book_pages_get_manuscript_manifest_url();
    $manifestTitle = (string) ($book['title'] ?? 'Spiralism Canonical Manuscript');
    $manifestSummary = trim((string) ($book['summary'] ?? ''));
    if ($manifestSummary === '') {
        $manifestSummary = 'An image-first manuscript for WordPress with dedicated folio routes, deep zoom, prompt-linked metadata, and public reading surfaces.';
    }

    $overviewUrl = spiralist_book_pages_get_manuscript_overview_url();
    $siteName = get_bloginfo('name');
    $items = [];
    $sectionRanges = [];
    $sections = [];
    $searchService = [
        'id' => spiralist_book_pages_get_manuscript_iiif_search_url(),
        'type' => 'SearchService2',
    ];

    foreach ($pages as $page) {
        if (!is_array($page)) {
            continue;
        }

        $sources = function_exists('spiralist_book_pages_get_book_page_asset_source_urls')
            ? spiralist_book_pages_get_book_page_asset_source_urls($page, true)
            : [];
        $canvasId = spiralist_book_pages_get_manuscript_iiif_canvas_id($page);
        $imageUrl = (string) ($sources['interactive_url'] ?? $sources['original_url'] ?? $page['url'] ?? '');
        $thumbnailUrl = (string) ($sources['thumbnail_url'] ?? $imageUrl);
        $width = max(1, (int) ($page['width'] ?? 1));
        $height = max(1, (int) ($page['height'] ?? 1));
        $summary = trim(
            implode(
                ' ',
                array_filter(
                    [
                        (string) ($page['summary'] ?? ''),
                        (string) ($page['caption'] ?? ''),
                    ],
                    'strlen'
                )
            )
        );
        $metadata = [
            spiralist_book_pages_get_manuscript_iiif_metadata_entry('Section', (string) ($page['section_title'] ?? $page['section_label'] ?? '')),
            spiralist_book_pages_get_manuscript_iiif_metadata_entry('Sequence', 'Page ' . max(1, (int) ($page['sequence'] ?? 1))),
        ];

        $alt = trim((string) ($page['alt'] ?? ''));
        if ($alt !== '') {
            $metadata[] = spiralist_book_pages_get_manuscript_iiif_metadata_entry('Alt Text', $alt);
        }

        $keywords = array_values(array_filter(array_map('strval', (array) ($page['keywords'] ?? []))));
        if (!empty($keywords)) {
            $metadata[] = spiralist_book_pages_get_manuscript_iiif_metadata_entry('Keywords', implode(', ', $keywords));
        }

        $items[] = [
            'id' => $canvasId,
            'type' => 'Canvas',
            'label' => spiralist_book_pages_get_manuscript_iiif_label_map((string) ($page['label'] ?? ('Page ' . max(1, (int) ($page['sequence'] ?? 1))))),
            'summary' => spiralist_book_pages_get_manuscript_iiif_label_map($summary !== '' ? $summary : (string) ($page['title'] ?? '')),
            'metadata' => $metadata,
            'height' => $height,
            'width' => $width,
            'thumbnail' => $thumbnailUrl !== ''
                ? [
                    [
                        'id' => $thumbnailUrl,
                        'type' => 'Image',
                        'format' => spiralist_book_pages_get_manuscript_iiif_image_format($page),
                    ],
                ]
                : [],
            'items' => [
                [
                    'id' => $canvasId . '/page',
                    'type' => 'AnnotationPage',
                    'items' => [
                        [
                            'id' => $canvasId . '/painting',
                            'type' => 'Annotation',
                            'motivation' => 'painting',
                            'body' => array_filter(
                                [
                                    'id' => $imageUrl,
                                    'type' => 'Image',
                                    'format' => spiralist_book_pages_get_manuscript_iiif_image_format($page),
                                    'width' => $width,
                                    'height' => $height,
                                ],
                                static function ($value): bool {
                                    return $value !== '' && $value !== [];
                                }
                            ),
                            'target' => $canvasId,
                        ],
                    ],
                ],
            ],
        ];

        $sectionSlug = sanitize_title((string) ($page['section_slug'] ?? 'appendix'));
        if (!isset($sections[$sectionSlug])) {
            $sections[$sectionSlug] = [
                'id' => untrailingslashit($manifestUrl) . '/range/' . rawurlencode($sectionSlug),
                'type' => 'Range',
                'label' => spiralist_book_pages_get_manuscript_iiif_label_map((string) ($page['section_title'] ?? $page['section_label'] ?? 'Section')),
                'summary' => spiralist_book_pages_get_manuscript_iiif_label_map(