Skip to content
wiki.fftac.org

Rest API - Source Excerpt 02

Back to Rest API

Summary

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

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

function spiralist_book_pages_rest_error_response(string $code, string $message, int $status = 400, array $extra = []): WP_REST_Response
{
    return spiralist_book_pages_rest_response(
        array_merge(
            [
                'error' => $code,
                'message' => $message,
            ],
            $extra
        ),
        $status,
        true
    );
}

function spiralist_book_pages_rest_search_manuscript(WP_REST_Request $request): WP_REST_Response
{
    $query = trim((string) $request->get_param('q'));
    $limit = max(1, min(24, (int) $request->get_param('limit')));
    $payload = function_exists('spiralist_book_pages_get_manuscript_search_payload')
        ? spiralist_book_pages_get_manuscript_search_payload($query, $limit)
        : [
            'query' => $query,
            'total' => 0,
            'returned' => 0,
            'results' => [],
        ];

    return spiralist_book_pages_rest_response($payload, 200);
}

function spiralist_book_pages_rest_get_manuscript_manifest(WP_REST_Request $request): WP_REST_Response
{
    $payload = function_exists('spiralist_book_pages_get_manuscript_manifest_payload')
        ? spiralist_book_pages_get_manuscript_manifest_payload()
        : [];
    $response = new WP_REST_Response($payload, 200);
    $response->header('Content-Type', 'application/ld+json; charset=utf-8');

    return $response;
}

function spiralist_book_pages_rest_get_manuscript_iiif_search(WP_REST_Request $request): WP_REST_Response
{
    $query = trim((string) $request->get_param('q'));
    $payload = function_exists('spiralist_book_pages_get_manuscript_iiif_search_payload')
        ? spiralist_book_pages_get_manuscript_iiif_search_payload($query)
        : [
            '@context' => 'http://iiif.io/api/search/2/context.json',
            'id' => '',
            'type' => 'AnnotationPage',
            'items' => [],
        ];
    $response = new WP_REST_Response($payload, 200);
    $response->header('Content-Type', 'application/ld+json; charset=utf-8');

    return $response;
}

function spiralist_book_pages_rest_get_json_payload(WP_REST_Request $request): array
{
    $payload = $request->get_json_params();
    if (is_array($payload)) {
        return $payload;
    }

    $body = trim((string) $request->get_body());
    if ($body === '') {
        return $request->get_params();
    }

    $decoded = json_decode($body, true);

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

function spiralist_book_pages_rest_normalize_identifier_list($value): array
{
    if (is_string($value)) {
        $value = preg_split('/[\s,]+/', $value) ?: [];
    }

    if (!is_array($value)) {
        return [];
    }

    $items = array_map(
        static function ($item): string {
            return trim((string) $item);
        },
        $value
    );

    return array_values(array_filter($items, 'strlen'));
}

function spiralist_book_pages_rest_get_system_label(string $system): string
{
    $labels = [
        'spiralist' => 'Spiralist',
        'protocol5' => 'Protocol5',
        'uai' => 'Spiral Script',
    ];

    return $labels[$system] ?? ucwords(str_replace(['_', '-'], ' ', $system));
}

function spiralist_book_pages_rest_get_role_label(string $role): string
{
    $labels = [
        'observer' => 'Observer',
        'participant' => 'Participant',
        'contributor' => 'Contributor',
        'ai' => 'AI',
    ];

    return $labels[$role] ?? ucwords(str_replace(['_', '-'], ' ', $role));
}

function spiralist_book_pages_rest_build_site_payload(string $language = ''): array
{
    if (!in_array($language, spiralist_book_pages_get_supported_language_slugs(), true)) {
        $language = spiralist_book_pages_get_current_language_code();
    }

    $publication = spiralist_book_pages_get_publication_context(
        null,
        [
            'home' => true,
            'allow_admin' => true,
            'language' => $language !== '' ? $language : 'en',
        ]
    );
    $pages = [
        [
            'id' => 'manuscript',
            'title' => 'Manuscript',
            'url' => spiralist_book_pages_get_manuscript_page_url(),
        ],
        [
            'id' => 'symbols',
            'title' => 'Symbols',
            'url' => spiralist_book_pages_get_symbols_page_url(),
        ],
        [
            'id' => 'prompts',
            'title' => 'Prompts',
            'url' => spiralist_book_pages_get_prompts_page_url(),
        ],
    ];

    return [
        'spec' => 'ns12-manuscript-site',
        'version' => '1.0.0',
        'documentId' => 'ns12.manuscript.site',
        'source' => [
            'uri' => spiralist_book_pages_get_rest_url('site'),
            'title' => get_bloginfo('name'),
            'retrievedAt' => gmdate('c'),
        ],
        'metadata' => [
            'language' => (string) ($publication['language'] ?? ($language !== '' ? $language : 'en')),
            'homeUrl' => (string) ($publication['humanUrl'] ?? home_url('/')),
            'definition' => spiralist_book_pages_get_spiralist_definition(),
        ],
        'structure' => [
            [
                'id' => 'book-system',
                'type' => 'collection',
                'label' => spiralist_book_pages_get_public_name(),
                'pages' => $pages,
            ],
        ],
        'semantics' => [
            ['type' => 'surface', 'value' => 'book-system'],
            ['type' => 'audience', 'value' => 'human'],
            ['type' => 'audience', 'value' => 'ai'],
        ],
        'symbols' => array_slice(spiralist_book_pages_get_symbol_detail_records(), 0, 3),
        'assets' => [
            [
                'id' => 'manuscript-preview',
                'type' => 'image',
                'url' => spiralist_book_pages_get_manuscript_preview_url(),
            ],
        ],
        'relationships' => [
            [
                'type' => 'machine-surface',
                'value' => spiralist_book_pages_get_primary_machine_endpoints(),
            ],
        ],
        'annotations' => [
            [
                'type' => 'note',
                'value' => 'This endpoint describes the standalone Spiralist Manuscript surface.',
            ],
        ],
        'provenance' => [
            'generatedBy' => 'ns12-manuscript',
            'generatedAt' => gmdate('c'),
            'mode' => 'plugin-runtime',
        ],
        'extensions' => [
            'publication' => $publication,
            'machineLinks' => spiralist_book_pages_get_machine_links(),
        ],
    ];
}

function spiralist_book_pages_rest_build_symbol_registry_payload(): array
{
    $symbols = array_map(
        static function (array $symbol): array {
            return [
                'glyph' => (string) ($symbol['glyph'] ?? ''),
                'label' => (string) ($symbol['label'] ?? ''),
                'canonicalId' => (string) ($symbol['canonical_id'] ?? ''),
                'type' => (string) ($symbol['type'] ?? ''),
                'meaning' => (string) ($symbol['meaning'] ?? ''),
                'definition' => (string) ($symbol['definition'] ?? ''),
                'derivation' => (string) ($symbol['derivation'] ?? ''),
                'provenance' => (string) ($symbol['provenance'] ?? ''),
                'related' => array_values(array_filter((array) ($symbol['related'] ?? []), 'is_string')),
                'axioms' => array_values(array_filter((array) ($symbol['axioms'] ?? []), 'is_string')),
            ];
        },
        spiralist_book_pages_get_symbols()
    );

    return [
        'spec' => 'ns12-manuscript-symbol-registry',
        'version' => '1.0.0',
        'registryId' => 'ns12.manuscript.symbols',
        'source' => [
            'uri' => spiralist_book_pages_get_symbol_registry_url(),
            'title' => 'Spiralist Symbol Registry',
            'retrievedAt' => gmdate('c'),
            'contentHash' => 'sha256:' . hash('sha256', wp_json_encode($symbols, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)),
        ],
        'count' => count($symbols),
        'symbols' => $symbols,
        'provenance' => [
            'generatedBy' => 'ns12-manuscript',
            'generatedAt' => gmdate('c'),
            'mode' => 'plugin-registry',
        ],
    ];
}

function spiralist_book_pages_rest_build_symbols_payload(): array
{
    return [
        'count' => count(spiralist_book_pages_get_symbols()),
        'axioms' => spiralist_book_pages_get_axioms(),
        'symbols' => spiralist_book_pages_get_symbol_detail_records(),
        'transformations' => spiralist_book_pages_get_symbol_transformations(),
    ];
}

function spiralist_book_pages_rest_build_kernel_payload(): array
{
    return [