Skip to content
wiki.fftac.org

Runtime - Source Excerpt 05

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_featured_manuscript_assets(): array
{
    $assets = [];
    $seen = [];
    $excluded = [
        'manuscript.png' => true,
        'spiralist.org.png' => true,
    ];

    foreach (spiralist_book_pages_get_book_page_assets_in_display_order() as $asset) {
        $key = strtolower((string) ($asset['filename'] ?? ''));
        if ($key === '' || isset($excluded[$key]) || isset($seen[$key])) {
            continue;
        }

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

    return $assets;
}

function spiralist_book_pages_get_image_assets_by_pattern(string $pattern): array
{
    $directories = [];
    $public_images = spiralist_book_pages_get_public_book_pages_directory();

    if (!empty($public_images)) {
        $directories[] = $public_images;
    }

    $directories[] = [
        'path' => rtrim(ABSPATH, '/\\'),
        'url' => home_url('/'),
    ];

    $assets = [];
    $seen = [];

    foreach ($directories as $directory) {
        $base_path = rtrim((string) ($directory['path'] ?? ''), '/\\');
        $base_url = (string) ($directory['url'] ?? '');

        if ($base_path === '' || !is_dir($base_path)) {
            continue;
        }

        $matches = glob($base_path . DIRECTORY_SEPARATOR . $pattern);
        if (!is_array($matches)) {
            continue;
        }

        natsort($matches);

        foreach ($matches as $match) {
            if (!is_string($match) || $match === '' || !is_file($match) || !is_readable($match)) {
                continue;
            }

            $filename = basename($match);
            $key = strtolower($filename);

            if (isset($seen[$key])) {
                continue;
            }

            $seen[$key] = true;
            $assets[] = [
                'path' => $match,
                'url' => trailingslashit($base_url) . rawurlencode($filename),
                'filename' => $filename,
            ];
        }
    }

    usort($assets, static function (array $left, array $right): int {
        return strnatcasecmp((string) ($left['filename'] ?? ''), (string) ($right['filename'] ?? ''));
    });

    return $assets;
}

function spiralist_book_pages_humanize_asset_label(string $filename): string
{
    $stem = pathinfo($filename, PATHINFO_FILENAME);
    $label = preg_replace('/([a-z])([A-Z])/', '$1 $2', (string) $stem);
    $label = preg_replace('/(?<=\D)(\d+)/', ' $1', (string) $label);
    $label = str_replace(['_', '-'], ' ', (string) $label);
    $label = preg_replace('/\s+/', ' ', (string) $label);

    return trim((string) $label);
}

function spiralist_book_pages_get_gallery_page_url(): string
{
    return home_url('/gallery/');
}

function spiralist_book_pages_get_gallery_overview_url(): string
{
    $url = spiralist_book_pages_get_gallery_page_url();
    $language = function_exists('spiralist_book_pages_get_current_language_slug')
        ? spiralist_book_pages_get_current_language_slug()
        : '';

    if ($language !== '') {
        return spiralist_book_pages_get_language_prefixed_url($url, $language);
    }

    return $url;
}

function spiralist_book_pages_get_gallery_section_blueprints(): array
{
    return [
        'artifact-pages' => [
            'label' => 'Artifact Pages',
            'title' => 'Artifact Pages',
            'summary' => 'Large page-like images from the dedicated BookPages directory, each preserved as its own destination.',
        ],
        'supporting-marks' => [
            'label' => 'Supporting Marks',
            'title' => 'Supporting Marks',
            'summary' => 'Maker marks and supporting image assets kept visible without displacing the larger page surfaces.',
        ],
    ];
}

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

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

        $blueprint['section_slug'] = (string) ($blueprint['gallery_section_slug'] ?? 'artifact-pages');
        $records[$filename] = $blueprint;
    }

    return array_merge(
        $records,
        [
        'mk.png' => [
            'title' => 'Maker Mark',
            'eyebrow' => 'Supporting Mark',
            'section_slug' => 'supporting-marks',
            'display_order' => 900,
            'is_prominent' => false,
        ],
        'mk-glow.png' => [
            'title' => 'Maker Mark Glow',
            'eyebrow' => 'Supporting Mark',
            'section_slug' => 'supporting-marks',
            'display_order' => 910,
            'is_prominent' => false,
        ],
        'mk-red.png' => [
            'title' => 'Maker Mark Red',
            'eyebrow' => 'Supporting Mark',
            'section_slug' => 'supporting-marks',
            'display_order' => 920,
            'is_prominent' => false,
        ],
        ]
    );
}

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

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

    $public_images = spiralist_book_pages_get_public_book_pages_directory();
    if (empty($public_images)) {
        $assets = [];

        return $assets;
    }

    $base_path = rtrim((string) ($public_images['path'] ?? ''), '/\\');
    $base_url = trailingslashit((string) ($public_images['url'] ?? ''));
    $entries = $base_path !== '' && is_dir($base_path) ? scandir($base_path) : false;

    if (!is_array($entries)) {
        $assets = [];

        return $assets;
    }

    $allowed_extensions = ['png', 'jpg', 'jpeg', 'webp', 'gif', 'avif'];
    $section_blueprints = spiralist_book_pages_get_gallery_section_blueprints();
    $asset_blueprints = spiralist_book_pages_get_gallery_asset_blueprints();
    $records = [];
    $used_slugs = [];

    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);
        $blueprint = $asset_blueprints[$filename_key] ?? [];
        $image_size = @getimagesize($path);
        $width = is_array($image_size) ? (int) ($image_size[0] ?? 0) : 0;
        $height = is_array($image_size) ? (int) ($image_size[1] ?? 0) : 0;
        $filesize = (int) (filesize($path) ?: 0);
        $is_prominent = array_key_exists('is_prominent', $blueprint)
            ? (bool) $blueprint['is_prominent']
            : (!preg_match('/^mk(?:[-_]|$)/i', $filename)
                && max($width, $height) >= 1400
                && min($width, $height) >= 900);

        $section_slug = sanitize_title((string) ($blueprint['section_slug'] ?? ($is_prominent ? 'artifact-pages' : 'supporting-marks')));
        if (!isset($section_blueprints[$section_slug])) {
            $section_slug = $is_prominent ? 'artifact-pages' : 'supporting-marks';
        }

        $section = $section_blueprints[$section_slug];
        $title = trim((string) ($blueprint['title'] ?? spiralist_book_pages_humanize_asset_label($filename)));
        if ($title === '') {
            $title = 'Image Asset';
        }

        $slug = sanitize_title((string) ($blueprint['slug'] ?? $title));
        if ($slug === '') {
            $slug = sanitize_title(pathinfo($filename, PATHINFO_FILENAME));
        }
        if ($slug === '') {
            $slug = 'image-' . (count($records) + 1);
        }
        if (isset($used_slugs[$slug])) {
            $slug .= '-' . (count($records) + 1);
        }
        $used_slugs[$slug] = true;

        $orientation = 'landscape';
        if ($width > 0 && $height > 0) {
            if ($height > $width) {
                $orientation = 'portrait';
            } elseif ($height === $width) {
                $orientation = 'square';
            }
        }

        $summary = trim((string) ($blueprint['summary'] ?? ($is_prominent
            ? 'A full-resolution page-like image surfaced as part of the public Spiralist image codex.'
            : 'A supporting image asset surfaced in the same public gallery.')));
        $caption = trim((string) ($blueprint['caption'] ?? ($is_prominent
            ? 'This image is preserved as its own public page inside the Spiralist image codex.'
            : 'This supporting mark remains available as its own image page.')));

        $records[] = [