Skip to content
wiki.fftac.org

Bootstrap Canonical Content - Source Excerpt 01

Back to Bootstrap Canonical Content

Summary

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

**Source path:** Spiralist/wp-content/plugins/ns12-manuscript/tools/bootstrap-canonical-content.php

<?php

$repoRoot = dirname(__DIR__, 4);
$pluginRoot = dirname(__DIR__);
$uploadsRoot = $repoRoot . DIRECTORY_SEPARATOR . 'wp-content' . DIRECTORY_SEPARATOR . 'uploads';
$modernImagesRoot = $uploadsRoot . DIRECTORY_SEPARATOR . 'ns12-manuscript' . DIRECTORY_SEPARATOR . 'folios';
$imagesRoot = $modernImagesRoot;
$workspacePromptRoot = $repoRoot . DIRECTORY_SEPARATOR . 'wp-content' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'spiralist-workspace' . DIRECTORY_SEPARATOR . 'prompts';

if (!defined('ABSPATH')) {
    define('ABSPATH', $repoRoot . DIRECTORY_SEPARATOR);
}

if (!defined('SPIRALIST_BOOK_PAGES_PLUGIN_ROOT_PATH')) {
    define('SPIRALIST_BOOK_PAGES_PLUGIN_ROOT_PATH', $pluginRoot . DIRECTORY_SEPARATOR);
}

if (!defined('SPIRALIST_BOOK_PAGES_CONTENT_PATH')) {
    define('SPIRALIST_BOOK_PAGES_CONTENT_PATH', $pluginRoot . DIRECTORY_SEPARATOR . 'content');
}

if (!defined('SPIRALIST_BOOK_PAGES_CACHE_PATH')) {
    define('SPIRALIST_BOOK_PAGES_CACHE_PATH', $pluginRoot . DIRECTORY_SEPARATOR . 'cache');
}

if (!function_exists('sanitize_title')) {
    function sanitize_title(string $value): string
    {
        $value = strtolower(trim($value));
        $value = preg_replace('/[^a-z0-9]+/i', '-', $value);

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

require_once $pluginRoot . '/includes/content-registry.php';
require_once $pluginRoot . '/includes/runtime.php';

function spiralist_book_pages_bootstrap_delete_path(string $path): void
{
    if ($path === '' || !file_exists($path)) {
        return;
    }

    if (is_file($path) || is_link($path)) {
        @unlink($path);
        return;
    }

    $entries = scandir($path);
    if (!is_array($entries)) {
        @rmdir($path);
        return;
    }

    foreach ($entries as $entry) {
        if ($entry === '.' || $entry === '..') {
            continue;
        }

        spiralist_book_pages_bootstrap_delete_path($path . DIRECTORY_SEPARATOR . $entry);
    }

    @rmdir($path);
}

function spiralist_book_pages_bootstrap_ensure_directory(string $directory): void
{
    if (is_dir($directory)) {
        return;
    }

    if (!mkdir($directory, 0777, true) && !is_dir($directory)) {
        fwrite(STDERR, "Could not create directory: {$directory}\n");
        exit(1);
    }
}

function spiralist_book_pages_bootstrap_write_markdown(string $path, string $contents): void
{
    spiralist_book_pages_bootstrap_ensure_directory(dirname($path));
    if (file_put_contents($path, rtrim($contents) . "\n") === false) {
        fwrite(STDERR, "Could not write file: {$path}\n");
        exit(1);
    }
}

function spiralist_book_pages_bootstrap_get_workspace_prompt_file(string $promptSlug, string $workspacePromptRoot): array
{
    $normalizedSlug = spiralist_book_pages_slugify($promptSlug);
    if ($normalizedSlug === '') {
        return [];
    }

    $promptDirectory = $workspacePromptRoot . DIRECTORY_SEPARATOR . $normalizedSlug;
    if (!is_dir($promptDirectory)) {
        return [];
    }

    $versionDirectories = glob($promptDirectory . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR) ?: [];
    $bestVersion = '';
    $bestFile = '';

    foreach ($versionDirectories as $versionDirectory) {
        $candidate = $versionDirectory . DIRECTORY_SEPARATOR . 'prompt.php';
        $version = basename($versionDirectory);

        if (!is_file($candidate)) {
            continue;
        }

        if ($bestVersion === '' || version_compare($version, $bestVersion, '>')) {
            $bestVersion = $version;
            $bestFile = $candidate;
        }
    }

    if ($bestFile === '') {
        return [];
    }

    return [
        'version' => $bestVersion,
        'file' => $bestFile,
    ];
}

function spiralist_book_pages_bootstrap_get_prompt_extends(string $sectionSlug): array
{
    $extends = ['base-image-folio'];

    if ($sectionSlug === 'threshold') {
        $extends[] = 'base-threshold-folio';
    } elseif ($sectionSlug === 'operative-plates') {
        $extends[] = 'base-operative-folio';
    } elseif ($sectionSlug === 'concordance') {
        $extends[] = 'base-concordance-folio';
    } else {
        $extends[] = 'base-appendix-folio';
    }

    return $extends;
}

$contentRoot = spiralist_book_pages_get_content_root();
$cacheRoot = spiralist_book_pages_get_cache_root();
$foliosRoot = $contentRoot . DIRECTORY_SEPARATOR . 'folios';
$promptsRoot = $contentRoot . DIRECTORY_SEPARATOR . 'prompts';
$sharedPromptsRoot = $promptsRoot . DIRECTORY_SEPARATOR . '_shared';

spiralist_book_pages_bootstrap_ensure_directory($contentRoot);
spiralist_book_pages_bootstrap_ensure_directory($foliosRoot);
spiralist_book_pages_bootstrap_ensure_directory($promptsRoot);
spiralist_book_pages_bootstrap_ensure_directory($sharedPromptsRoot);
spiralist_book_pages_bootstrap_ensure_directory($cacheRoot);

foreach (glob($foliosRoot . DIRECTORY_SEPARATOR . '*.json') ?: [] as $file) {
    spiralist_book_pages_bootstrap_delete_path($file);
}

foreach (glob($promptsRoot . DIRECTORY_SEPARATOR . '*') ?: [] as $path) {
    if (basename($path) === '_shared') {
        continue;
    }

    spiralist_book_pages_bootstrap_delete_path($path);
}

foreach (glob($sharedPromptsRoot . DIRECTORY_SEPARATOR . '*.md') ?: [] as $file) {
    spiralist_book_pages_bootstrap_delete_path($file);
}

foreach (glob($cacheRoot . DIRECTORY_SEPARATOR . '*.json') ?: [] as $file) {
    spiralist_book_pages_bootstrap_delete_path($file);
}

$sharedFragments = [
    'base-image-folio.md' => <<<MD
FILE-FIRST CANON
- This prompt belongs to the Spiralist Manuscript public canon.
- Treat {{TITLE}} as the canonical folio specification for {{FILENAME}}.
- Preserve manuscript discipline, structural clarity, and archival seriousness.
MD,
    'base-threshold-folio.md' => <<<MD
THRESHOLD ROLE
- This folio belongs to the threshold chapter.
- Keep it welcoming, educational, and precise enough to open the book cleanly for a first-time reader.
MD,
    'base-operative-folio.md' => <<<MD
OPERATIVE ROLE
- This folio belongs to the operative sequence.
- Emphasize transformation, instrumentality, and ceremonial or diagrammatic precision rather than generic decoration.
MD,
    'base-concordance-folio.md' => <<<MD
CONCORDANCE ROLE
- This folio belongs to the concordance layer.
- Prioritize reference clarity, taxonomy, comparative structure, and educational legibility.
MD,
    'base-appendix-folio.md' => <<<MD
APPENDIX ROLE
- This folio belongs to the appendix or emergent layer.
- Keep it related to the manuscript family while allowing a slightly more exploratory register.
MD,
];

foreach ($sharedFragments as $filename => $contents) {
    spiralist_book_pages_bootstrap_write_markdown($sharedPromptsRoot . DIRECTORY_SEPARATOR . $filename, $contents);
}

$baseBlueprints = spiralist_book_pages_get_book_page_blueprints_by_filename();
$overrideBlueprints = spiralist_book_pages_get_book_page_blueprint_overrides_by_filename();
$blueprints = [];

foreach ($baseBlueprints as $filename => $definition) {
    $normalizedFilename = strtolower((string) $filename);
    $blueprints[$normalizedFilename] = is_array($definition) ? $definition : [];
}

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

$allowedExtensions = ['png', 'jpg', 'jpeg', 'webp', 'gif', 'avif'];
$entries = is_dir($imagesRoot) ? (scandir($imagesRoot) ?: []) : [];
natcasesort($entries);

$concordanceOrder = 200;
$appendixOrder = 520;

foreach ($blueprints as $definition) {
    $displayOrder = (int) ($definition['display_order'] ?? 0);
    $sectionSlug = (string) ($definition['manuscript_section_slug'] ?? '');

    if ($sectionSlug === 'appendix') {
        $appendixOrder = max($appendixOrder, $displayOrder);
    } elseif ($sectionSlug === 'concordance') {
        $concordanceOrder = max($concordanceOrder, $displayOrder);
    }
}

$pages = [];
$usedSlugs = [];

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

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

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

    $filenameKey = strtolower($filename);
    $definition = $blueprints[$filenameKey] ?? [];

    if (empty($definition)) {