Skip to content
wiki.fftac.org

Helpers - Source Excerpt 19

Back to Helpers

Summary

This source excerpt preserves a bounded section of Spiralist/wp-content/themes/spiralist/inc/helpers.php so readers can inspect the evidence without opening the full source file.

**Source path:** Spiralist/wp-content/themes/spiralist/inc/helpers.php

$directories = [];
    $public_images = spiralist_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'] ?? ''));
    });

    if ($cache_key !== '') {
        $pattern_cache[$cache_key] = $assets;
    }

    return $assets;
}

function spiralist_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_get_gallery_page_url(): string
{
    return home_url('/gallery/');
}

function spiralist_get_gallery_overview_url(): string
{
    $url = spiralist_get_gallery_page_url();
    $language = function_exists('spiralist_get_current_language_slug')
        ? spiralist_get_current_language_slug()
        : '';

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

    return $url;
}

function spiralist_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_get_gallery_asset_blueprints(): array
{
    $records = [];

    foreach (spiralist_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_get_gallery_assets(): array
{
    static $assets = null;

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

    $public_images = spiralist_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'] ?? ''));
    if ($base_path === '' || !is_dir($base_path)) {
        $assets = [];

        return $assets;
    }

    $allowed_extensions = ['png', 'jpg', 'jpeg', 'webp', 'gif', 'avif'];
    $section_blueprints = spiralist_get_gallery_section_blueprints();
    $asset_blueprints = spiralist_get_gallery_asset_blueprints();
    $manifest = spiralist_get_directory_asset_manifest(
        [
            [
                'path' => $base_path,
                'url' => $base_url,
            ],
        ],
        $allowed_extensions
    );
    $manifest_signature = (string) ($manifest['signature'] ?? '');
    $blueprint_signature = hash(
        'sha256',
        (string) wp_json_encode($section_blueprints) . '|' . (string) wp_json_encode($asset_blueprints)
    );
    $cache_signature = $manifest_signature !== ''
        ? hash('sha256', $manifest_signature . '|' . $blueprint_signature)
        : '';
    $cached = spiralist_read_runtime_cache('gallery_assets_v2');

    if (
        $cache_signature !== ''
        && ($cached['signature'] ?? '') === $cache_signature
        && is_array($cached['records'] ?? null)
    ) {
        $records = $cached['records'];
    } else {
        $records = [];
        $used_slugs = [];

        foreach ((array) ($manifest['files'] ?? []) as $asset_file) {
            if (!is_array($asset_file)) {
                continue;
            }

            $filename = trim((string) ($asset_file['filename'] ?? ''));
            $path = (string) ($asset_file['path'] ?? '');
            if ($filename === '' || $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) ($asset_file['filesize'] ?? 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_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