Skip to content
wiki.fftac.org

Content Registry - Source Excerpt 01

Back to Content Registry

Summary

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

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

<?php

if (!defined('SPIRALIST_BOOK_PAGES_PLUGIN_ROOT_PATH')) {
    define('SPIRALIST_BOOK_PAGES_PLUGIN_ROOT_PATH', dirname(__DIR__) . DIRECTORY_SEPARATOR);
}

if (!defined('SPIRALIST_BOOK_PAGES_CONTENT_PATH')) {
    define('SPIRALIST_BOOK_PAGES_CONTENT_PATH', SPIRALIST_BOOK_PAGES_PLUGIN_ROOT_PATH . 'content');
}

if (!defined('SPIRALIST_BOOK_PAGES_CACHE_PATH')) {
    define('SPIRALIST_BOOK_PAGES_CACHE_PATH', SPIRALIST_BOOK_PAGES_PLUGIN_ROOT_PATH . 'cache');
}

if (!defined('SPIRALIST_BOOK_PAGES_EXPORT_PATH')) {
    define('SPIRALIST_BOOK_PAGES_EXPORT_PATH', SPIRALIST_BOOK_PAGES_PLUGIN_ROOT_PATH . 'exports');
}

function spiralist_book_pages_slugify(string $value): string
{
    if (function_exists('sanitize_title')) {
        return sanitize_title($value);
    }

    $value = strtolower(trim($value));
    $value = preg_replace('/[^a-z0-9]+/i', '-', $value);
    $value = is_string($value) ? trim($value, '-') : '';

    return $value;
}

function spiralist_book_pages_json_encode($value): string
{
    if (function_exists('wp_json_encode')) {
        $encoded = wp_json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        return is_string($encoded) ? $encoded : '';
    }

    $encoded = json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

    return is_string($encoded) ? $encoded : '';
}

function spiralist_book_pages_get_content_root(): string
{
    return defined('SPIRALIST_BOOK_PAGES_CONTENT_PATH')
        ? (string) SPIRALIST_BOOK_PAGES_CONTENT_PATH
        : '';
}

function spiralist_book_pages_get_cache_root(): string
{
    return defined('SPIRALIST_BOOK_PAGES_CACHE_PATH')
        ? (string) SPIRALIST_BOOK_PAGES_CACHE_PATH
        : '';
}

function spiralist_book_pages_get_export_root(): string
{
    return defined('SPIRALIST_BOOK_PAGES_EXPORT_PATH')
        ? (string) SPIRALIST_BOOK_PAGES_EXPORT_PATH
        : '';
}

function spiralist_book_pages_read_json_file(string $path): array
{
    if ($path === '' || !is_file($path) || !is_readable($path)) {
        return [];
    }

    $raw = file_get_contents($path);
    if (!is_string($raw) || trim($raw) === '') {
        return [];
    }

    $decoded = json_decode($raw, true);

    return is_array($decoded) ? $decoded : [];
}

function spiralist_book_pages_read_text_file(string $path): string
{
    if ($path === '' || !is_file($path) || !is_readable($path)) {
        return '';
    }

    $raw = file_get_contents($path);

    return is_string($raw) ? str_replace("\r\n", "\n", $raw) : '';
}

function spiralist_book_pages_write_json_file(string $path, array $payload): bool
{
    $directory = dirname($path);
    if (!is_dir($directory) && !mkdir($directory, 0777, true) && !is_dir($directory)) {
        return false;
    }

    $encoded = spiralist_book_pages_json_encode($payload);
    if ($encoded === '') {
        return false;
    }

    return file_put_contents($path, $encoded . "\n") !== false;
}

function spiralist_book_pages_get_cached_index(string $filename): array
{
    $path = spiralist_book_pages_get_cache_root();
    if ($path === '') {
        return [];
    }

    return spiralist_book_pages_read_json_file(rtrim($path, '/\\') . DIRECTORY_SEPARATOR . $filename);
}

function spiralist_book_pages_get_content_book_definition(): array
{
    $path = spiralist_book_pages_get_content_root();
    if ($path === '') {
        return [];
    }

    return spiralist_book_pages_read_json_file(rtrim($path, '/\\') . DIRECTORY_SEPARATOR . 'book.json');
}

function spiralist_book_pages_get_content_folio_definitions(): array
{
    static $folios = null;

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

    $folios = [];
    $directory = spiralist_book_pages_get_content_root();
    if ($directory === '') {
        return $folios;
    }

    $directory = rtrim($directory, '/\\') . DIRECTORY_SEPARATOR . 'folios';
    if (!is_dir($directory) || !is_readable($directory)) {
        return $folios;
    }

    $files = glob($directory . DIRECTORY_SEPARATOR . '*.json') ?: [];

    foreach ($files as $file) {
        if (!is_string($file) || $file === '') {
            continue;
        }

        $definition = spiralist_book_pages_read_json_file($file);
        $slug = spiralist_book_pages_slugify((string) ($definition['slug'] ?? pathinfo($file, PATHINFO_FILENAME)));
        if ($slug === '') {
            continue;
        }

        $definition['slug'] = $slug;
        $folios[$slug] = $definition;
    }

    ksort($folios, SORT_NATURAL | SORT_FLAG_CASE);

    return $folios;
}

function spiralist_book_pages_get_content_prompt_meta_map(): array
{
    static $prompts = null;

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

    $prompts = [];
    $directory = spiralist_book_pages_get_content_root();
    if ($directory === '') {
        return $prompts;
    }

    $directory = rtrim($directory, '/\\') . DIRECTORY_SEPARATOR . 'prompts';
    if (!is_dir($directory) || !is_readable($directory)) {
        return $prompts;
    }

    $promptDirectories = glob($directory . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR) ?: [];

    foreach ($promptDirectories as $promptDirectory) {
        if (!is_string($promptDirectory) || basename($promptDirectory) === '_shared') {
            continue;
        }

        $meta = spiralist_book_pages_read_json_file($promptDirectory . DIRECTORY_SEPARATOR . 'meta.json');
        $slug = spiralist_book_pages_slugify((string) ($meta['slug'] ?? basename($promptDirectory)));
        if ($slug === '') {
            continue;
        }

        $meta['slug'] = $slug;
        $prompts[$slug] = $meta;
    }

    ksort($prompts, SORT_NATURAL | SORT_FLAG_CASE);

    return $prompts;
}

function spiralist_book_pages_interpolate_prompt_text(string $text, array $tokens): string
{
    if ($text === '' || empty($tokens)) {
        return $text;
    }

    $replacements = [];

    foreach ($tokens as $key => $value) {
        if (!is_scalar($value)) {
            continue;
        }

        $normalizedKey = strtoupper(trim((string) $key));
        if ($normalizedKey === '') {
            continue;
        }

        $replacements['{{' . $normalizedKey . '}}'] = (string) $value;
    }

    return strtr($text, $replacements);
}

function spiralist_book_pages_get_shared_prompt_fragment(string $name, array $tokens = []): string
{
    $normalized = spiralist_book_pages_slugify($name);
    if ($normalized === '') {
        return '';
    }

    $path = spiralist_book_pages_get_content_root();
    if ($path === '') {
        return '';
    }

    $fragment = spiralist_book_pages_read_text_file(
        rtrim($path, '/\\') . DIRECTORY_SEPARATOR . 'prompts' . DIRECTORY_SEPARATOR . '_shared' . DIRECTORY_SEPARATOR . $normalized . '.md'
    );

    return spiralist_book_pages_interpolate_prompt_text($fragment, $tokens);
}

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

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

    $directory = rtrim($directory, '/\\') . DIRECTORY_SEPARATOR . 'prompts' . DIRECTORY_SEPARATOR . $normalized;
    $meta = spiralist_book_pages_read_json_file($directory . DIRECTORY_SEPARATOR . 'meta.json');

    if (empty($meta)) {
        return [];
    }

    $meta['slug'] = $normalized;
    $body = spiralist_book_pages_read_text_file($directory . DIRECTORY_SEPARATOR . 'body.md');
    $tokens = array_merge(
        [
            'SLUG' => $normalized,
            'TITLE' => (string) ($meta['title'] ?? $normalized),
            'FILENAME' => (string) ($meta['filename'] ?? ''),
            'SECTION' => (string) ($meta['section_slug'] ?? ''),
        ],
        is_array($meta['placeholders'] ?? null) ? (array) $meta['placeholders'] : []
    );

    $fragments = [];
    foreach ((array) ($meta['extends'] ?? []) as $fragmentName) {
        $fragmentText = spiralist_book_pages_get_shared_prompt_fragment((string) $fragmentName, $tokens);
        if ($fragmentText !== '') {
            $fragments[] = trim($fragmentText);
        }
    }

    $composedBody = trim(spiralist_book_pages_interpolate_prompt_text($body, $tokens));
    if ($composedBody !== '') {
        $fragments[] = $composedBody;
    }

    $meta['text'] = trim(implode("\n\n", array_filter($fragments, 'strlen')));

    return $meta;
}

function spiralist_book_pages_normalize_section_definitions(array $book): array
{
    $sections = [];

    foreach ((array) ($book['sections'] ?? []) as $section) {