Skip to content
wiki.fftac.org

Runtime - Source Excerpt 12

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

$normalized = strtolower((string) $filename);
        if ($normalized !== '') {
            $configured_filenames[$normalized] = true;
        }

        $blueprint_section = (string) ($blueprint['manuscript_section_slug'] ?? '');
        $blueprint_order = (int) ($blueprint['display_order'] ?? 0);

        if ($blueprint_section === 'appendix') {
            $appendix_order = max($appendix_order, $blueprint_order);
            continue;
        }

        if ($blueprint_section === 'concordance') {
            $concordance_order = max($concordance_order, $blueprint_order);
        }
    }

    $allowed_extensions = ['png', 'jpg', 'jpeg', 'webp', 'gif', 'avif'];
    $generated = [];

    foreach ($entries as $entry) {
        $filename = trim((string) $entry);
        if ($filename === '' || $filename[0] === '.') {
            continue;
        }

        $extension = strtolower((string) pathinfo($filename, PATHINFO_EXTENSION));
        if (!in_array($extension, $allowed_extensions, true)) {
            continue;
        }

        $path = $base_path . DIRECTORY_SEPARATOR . $filename;
        if (!is_file($path) || !is_readable($path)) {
            continue;
        }

        $filename_key = strtolower($filename);
        if ($filename_key === '' || isset($configured_filenames[$filename_key])) {
            continue;
        }

        $title = spiralist_book_pages_get_auto_book_page_title($filename);
        if ($title === '') {
            $title = 'Appendix Folio';
        }

        $slug = sanitize_title($title);
        $section_slug = spiralist_book_pages_get_auto_book_page_section_slug($filename);
        if ($section_slug === 'appendix') {
            $appendix_order++;
            $display_order = $appendix_order;
        } else {
            $concordance_order++;
            $display_order = $concordance_order;
        }

        $generated[$filename_key] = [
            'title' => $title,
            'slug' => $slug,
            'eyebrow' => spiralist_book_pages_get_auto_book_page_eyebrow($section_slug),
            'summary' => spiralist_book_pages_get_auto_book_page_summary($title, $section_slug),
            'caption' => spiralist_book_pages_get_auto_book_page_caption($title, $section_slug),
            'manuscript_section_slug' => $section_slug,
            'gallery_section_slug' => 'artifact-pages',
            'display_order' => $display_order,
            'is_prominent' => true,
            'creation_prompt_status' => 'Inline prompt attached',
            'creation_prompt_summary' => 'This newly added folio now includes an inline manuscript prompt so it can ship with the public book immediately.',
            'creation_prompt_text' => spiralist_book_pages_get_auto_book_page_prompt_text($title, $section_slug),
            'creation_prompt_source_slug' => $slug,
            'related_workspace_prompt_slugs' => [$slug],
        ];
    }

    return $generated;
}

function spiralist_book_pages_get_all_book_page_blueprints_by_filename(): array
{
    $contentIndex = function_exists('spiralist_book_pages_get_book_content_index')
        ? spiralist_book_pages_get_book_content_index()
        : [];
    if (!empty($contentIndex['folios'])) {
        $blueprints = [];

        foreach ((array) $contentIndex['folios'] as $folio) {
            if (!is_array($folio)) {
                continue;
            }

            $filename = strtolower(trim((string) ($folio['filename'] ?? '')));
            if ($filename === '') {
                continue;
            }

            $blueprints[$filename] = [
                'label' => (string) ($folio['label'] ?? ''),
                'title' => (string) ($folio['title'] ?? ''),
                'slug' => (string) ($folio['slug'] ?? ''),
                'eyebrow' => (string) ($folio['eyebrow'] ?? ''),
                'summary' => (string) ($folio['summary'] ?? ''),
                'caption' => (string) ($folio['caption'] ?? ''),
                'manuscript_section_slug' => (string) ($folio['section_slug'] ?? 'appendix'),
                'gallery_section_slug' => 'artifact-pages',
                'display_order' => (int) ($folio['display_order'] ?? 0),
                'is_study_surface' => !empty($folio['is_study_surface']),
                'is_prominent' => !empty($folio['is_prominent']),
                'creation_prompt_title' => (string) ($folio['creation_prompt_title'] ?? 'Prompt Used'),
                'creation_prompt_status' => (string) ($folio['creation_prompt_status'] ?? 'Canonical prompt'),
                'creation_prompt_summary' => (string) ($folio['creation_prompt_summary'] ?? ''),
                'creation_prompt_source_slug' => (string) ($folio['creation_prompt_source_slug'] ?? ''),
                'related_workspace_prompt_slugs' => array_values(
                    array_filter(
                        array_map('strval', (array) ($folio['related_workspace_prompt_slugs'] ?? []))
                    )
                ),
            ];
        }

        if (!empty($blueprints)) {
            return $blueprints;
        }
    }

    $blueprints = spiralist_book_pages_get_book_page_blueprints_by_filename();

    foreach (spiralist_book_pages_get_book_page_blueprint_overrides_by_filename() as $filename => $override) {
        $normalized = strtolower((string) $filename);
        $existing = isset($blueprints[$normalized]) && is_array($blueprints[$normalized]) ? $blueprints[$normalized] : [];
        $blueprints[$normalized] = array_merge($existing, $override);
    }

    foreach (spiralist_book_pages_get_generated_book_page_blueprints_by_filename($blueprints) as $filename => $generated) {
        $normalized = strtolower((string) $filename);
        if ($normalized === '') {
            continue;
        }

        $existing = isset($blueprints[$normalized]) && is_array($blueprints[$normalized]) ? $blueprints[$normalized] : [];
        $blueprints[$normalized] = array_merge($existing, $generated);
    }

    return $blueprints;
}

function spiralist_book_pages_get_book_page_blueprints_in_display_order(): array
{
    $records = [];

    foreach (spiralist_book_pages_get_all_book_page_blueprints_by_filename() as $filename => $blueprint) {
        if (!is_array($blueprint)) {
            continue;
        }

        $records[] = [
            'filename' => strtolower((string) $filename),
            'blueprint' => $blueprint,
        ];
    }

    usort(
        $records,
        static function (array $left, array $right): int {
            $order_compare = ((int) ($left['blueprint']['display_order'] ?? 0)) <=> ((int) ($right['blueprint']['display_order'] ?? 0));
            if ($order_compare !== 0) {
                return $order_compare;
            }

            return strnatcasecmp((string) ($left['filename'] ?? ''), (string) ($right['filename'] ?? ''));
        }
    );

    return $records;
}

function spiralist_book_pages_get_book_page_assets_in_display_order(): array
{
    static $assets = null;

    if (is_array($assets)) {
        return $assets;
    }

    $assets = [];
    $seen = [];
    $available_assets = spiralist_book_pages_get_available_book_page_assets_by_filename();

    foreach (spiralist_book_pages_get_book_page_blueprints_in_display_order() as $record) {
        $filename = (string) ($record['filename'] ?? '');
        if ($filename === '') {
            continue;
        }

        $key = strtolower(trim(basename($filename)));
        $asset = $key !== '' && isset($available_assets[$key])
            ? $available_assets[$key]
            : spiralist_book_pages_get_book_page_asset_by_filename($filename);
        if (empty($asset)) {
            continue;
        }

        $asset_key = strtolower((string) ($asset['filename'] ?? $filename));
        if ($asset_key === '' || isset($seen[$asset_key])) {
            continue;
        }

        $seen[$asset_key] = true;
        $assets[] = $asset;
    }

    $remaining_assets = array_filter(
        $available_assets,
        static function (string $key) use ($seen): bool {
            return !isset($seen[$key]);
        },
        ARRAY_FILTER_USE_KEY
    );

    if (!empty($remaining_assets)) {
        uksort(
            $remaining_assets,
            static function (string $left, string $right): int {
                return strnatcasecmp($left, $right);
            }
        );

        foreach ($remaining_assets as $key => $asset) {
            if ($key === '' || isset($seen[$key])) {
                continue;
            }

            $seen[$key] = true;
            $assets[] = $asset;
        }
    }

    return $assets;
}

function spiralist_book_pages_get_manuscript_section_blueprints(): array
{