Skip to content
wiki.fftac.org

Runtime - Source Excerpt 03

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

'slug' => (string) ($prompt['slug'] ?? ''),
        'canonical_id' => '',
        'title' => (string) ($prompt['title'] ?? 'Related prompt'),
        'purpose' => (string) ($prompt['summary'] ?? ''),
        'summary' => (string) ($prompt['summary'] ?? ''),
        'status' => $status,
        'statusLabel' => $statusLabel,
        'tags' => array_values(array_filter(array_map('strval', (array) ($prompt['tags'] ?? [])))),
        'author' => (string) ($prompt['author_label'] ?? 'Spiralist'),
        'url' => (string) ($prompt['url'] ?? ''),
        'source' => 'canon-file',
        'featured' => !empty($prompt['featured']),
    ];
}

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

    $filePrompt = spiralist_book_pages_get_file_prompt_record_by_slug($normalized);
    if (!empty($filePrompt)) {
        return $filePrompt;
    }

    if (!function_exists('spiralist_book_pages_workspace')) {
        return [];
    }

    $plugin = spiralist_book_pages_workspace();
    if (!is_object($plugin) || !method_exists($plugin, 'boot') || !method_exists($plugin, 'prompts') || !method_exists($plugin, 'permissions')) {
        return [];
    }

    $plugin->boot();
    $viewer_user_id = function_exists('get_current_user_id') ? get_current_user_id() : 0;
    $prompt = $plugin->prompts()->find_by_slug($normalized, $viewer_user_id);

    if (empty($prompt) || !$plugin->permissions()->can_view_prompt($prompt, $viewer_user_id)) {
        return [];
    }

    return $prompt;
}

function spiralist_book_pages_get_current_workspace_prompt_record(): array
{
    return spiralist_book_pages_get_workspace_prompt_record_by_slug((string) get_query_var('spiralist_book_pages_workspace_prompt_slug'));
}

function spiralist_book_pages_get_workspace_library_prompt_records(int $limit = 24): array
{
    if ($limit < 1) {
        return [];
    }

    $records = [];
    $seen = [];
    $register_record = static function (array $record) use (&$records, &$seen): void {
        $key = strtolower(
            (string) (
                $record['source_slug']
                ?? $record['slug']
                ?? $record['id']
                ?? $record['url']
                ?? ''
            )
        );

        if ($key === '' || isset($seen[$key])) {
            return;
        }

        $seen[$key] = true;
        $records[] = $record;
    };

    $contentPromptIndex = function_exists('spiralist_book_pages_get_prompt_content_index')
        ? spiralist_book_pages_get_prompt_content_index()
        : [];
    if (!empty($contentPromptIndex)) {
        foreach ($contentPromptIndex as $slug => $prompt) {
            $record = spiralist_book_pages_get_file_prompt_summary_by_slug((string) $slug);
            if (empty($record)) {
                continue;
            }

            $register_record(array_merge(
                $record,
                [
                    'featured' => !empty($prompt['featured']),
                ]
            ));
        }
    }

    if (function_exists('spiralist_book_pages_workspace')) {
        $plugin = spiralist_book_pages_workspace();
        if (is_object($plugin) && method_exists($plugin, 'boot') && method_exists($plugin, 'frontend')) {
            $plugin->boot();

            foreach ((array) $plugin->frontend()->library_prompts() as $record) {
                if (!is_array($record)) {
                    continue;
                }

                $register_record($record);
            }
        }
    }

    usort(
        $records,
        static function (array $left, array $right): int {
            $featuredCompare = ((int) !empty($right['featured'])) <=> ((int) !empty($left['featured']));
            if ($featuredCompare !== 0) {
                return $featuredCompare;
            }

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

    return array_slice($records, 0, $limit);
}

function spiralist_book_pages_get_workspace_prompt_record_by_source_slug(string $source_slug): array
{
    $normalized = sanitize_title($source_slug);
    if ($normalized === '') {
        return [];
    }

    $filePrompt = spiralist_book_pages_get_file_prompt_summary_by_slug($normalized);
    if (!empty($filePrompt)) {
        return $filePrompt;
    }

    if (!function_exists('spiralist_book_pages_workspace')) {
        return [];
    }

    $plugin = spiralist_book_pages_workspace();
    if (!is_object($plugin) || !method_exists($plugin, 'boot') || !method_exists($plugin, 'prompts')) {
        return [];
    }

    $plugin->boot();
    $viewer_user_id = function_exists('get_current_user_id') ? get_current_user_id() : 0;
    $prompt = $plugin->prompts()->find_by_source_slug($normalized, $viewer_user_id);

    if (empty($prompt)) {
        $prompt = spiralist_book_pages_get_workspace_prompt_record_by_slug($normalized);
    }

    if (
        empty($prompt)
        || (string) ($prompt['visibility'] ?? '') !== 'public'
        || (string) ($prompt['prompt_status'] ?? '') !== 'published'
        || !empty($prompt['soft_deleted_at'])
        || (string) ($prompt['moderation_status'] ?? 'approved') === 'restricted'
    ) {
        return [];
    }

    $tags = array_values(array_filter(array_map('strval', (array) ($prompt['tags'] ?? []))));
    $status = (string) ($prompt['moderation_status'] ?? 'approved');
    $status_label = $status !== ''
        ? ucwords(str_replace('_', ' ', $status))
        : 'Approved';

    return [
        'id' => (string) ($prompt['source_slug'] ?? $prompt['slug'] ?? $normalized),
        'slug' => (string) ($prompt['slug'] ?? $normalized),
        'canonical_id' => '',
        'title' => (string) ($prompt['title'] ?? 'Related prompt'),
        'purpose' => (string) ($prompt['summary'] ?? ''),
        'status' => $status,
        'statusLabel' => $status_label,
        'tags' => $tags,
        'author' => (string) ($prompt['author_label'] ?? 'Spiralist'),
        'url' => spiralist_book_pages_get_workspace_prompt_url((string) ($prompt['slug'] ?? $normalized)),
        'source' => 'workspace',
    ];
}

function spiralist_book_pages_get_workspace_prompt_records_by_source_slugs(array $source_slugs): array
{
    $records = [];
    $seen = [];

    foreach ($source_slugs as $source_slug) {
        $record = spiralist_book_pages_get_workspace_prompt_record_by_source_slug((string) $source_slug);
        if (empty($record)) {
            continue;
        }

        $key = strtolower((string) ($record['id'] ?? $record['url'] ?? ''));
        if ($key !== '' && isset($seen[$key])) {
            continue;
        }

        if ($key !== '') {
            $seen[$key] = true;
        }

        $records[] = $record;
    }

    return $records;
}

function spiralist_book_pages_get_manuscript_related_prompt_records(array $page): array
{
    return spiralist_book_pages_get_workspace_prompt_records_by_source_slugs((array) ($page['related_workspace_prompt_slugs'] ?? []));
}

function spiralist_book_pages_get_book_page_dossier_tabs(): array
{
    return [
        'overview' => 'Page Notes',
        'contents' => 'Table of Contents',
        'prompt' => 'Prompt Used',
        'related' => 'Related Prompts',
        'textlayer' => 'Text Layer',
        'metadata' => 'Metadata',
    ];
}

function spiralist_book_pages_get_book_page_creation_prompt_payload(array $page): array
{
    $default_title = 'Prompt Used';
    $default_status = 'Prompt pending';
    $default_summary = 'The creation prompt for this page will be attached here as the online-first book dossier grows.';

    $title = trim((string) ($page['creation_prompt_title'] ?? $default_title));
    $status = trim((string) ($page['creation_prompt_status'] ?? $default_status));
    $summary = trim((string) ($page['creation_prompt_summary'] ?? $default_summary));
    $text = trim((string) ($page['creation_prompt_text'] ?? ''));
    $source_slug = sanitize_title((string) ($page['creation_prompt_source_slug'] ?? ''));
    $fallback_related = array_values(
        array_filter(
            array_map(
                static function ($slug): string {
                    return sanitize_title((string) $slug);
                },
                (array) ($page['related_workspace_prompt_slugs'] ?? [])
            ),
            'strlen'
        )
    );

    if ($source_slug === '' && !empty($fallback_related[0])) {
        $source_slug = (string) $fallback_related[0];
    }

    if ($source_slug !== '') {
        $prompt_summary = spiralist_book_pages_get_workspace_prompt_record_by_source_slug($source_slug);