Skip to content
wiki.fftac.org

Rest API - Source Excerpt 01

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

<?php

if (!defined('ABSPATH')) {
    exit;
}

function spiralist_book_pages_get_rest_namespace(): string
{
    return 'ns12-manuscript/v1';
}

function spiralist_book_pages_get_rest_legacy_namespaces(): array
{
    return [];
}

function spiralist_book_pages_get_rest_namespaces(): array
{
    return array_values(
        array_unique(
            array_filter(
                array_merge(
                    [spiralist_book_pages_get_rest_namespace()],
                    spiralist_book_pages_get_rest_legacy_namespaces()
                ),
                'strlen'
            )
        )
    );
}

function spiralist_book_pages_get_rest_url(string $path = ''): string
{
    $path = trim($path, '/');

    if ($path === '') {
        return rest_url(spiralist_book_pages_get_rest_namespace());
    }

    return rest_url(spiralist_book_pages_get_rest_namespace() . '/' . $path);
}

function spiralist_book_pages_get_ai_participant_table_name(): string
{
    global $wpdb;

    return $wpdb->prefix . 'spiralist_book_ai_participants';
}

function spiralist_book_pages_get_contribution_option_key(): string
{
    return 'spiralist_book_pages_contributions';
}

function spiralist_book_pages_get_contribution_last_id_option_key(): string
{
    return 'spiralist_book_pages_contribution_last_id';
}

function spiralist_book_pages_get_interaction_option_key(): string
{
    return 'spiralist_book_pages_interactions';
}

function spiralist_book_pages_install_data_store(): void
{
    global $wpdb;

    require_once ABSPATH . 'wp-admin/includes/upgrade.php';

    $table = spiralist_book_pages_get_ai_participant_table_name();
    $charset = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE {$table} (
        id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
        participant_id VARCHAR(64) NOT NULL,
        agent_name VARCHAR(190) NOT NULL,
        api_key_hash VARCHAR(255) NOT NULL,
        api_key_lookup CHAR(64) NOT NULL,
        status VARCHAR(20) NOT NULL DEFAULT 'active',
        permissions LONGTEXT NOT NULL,
        purpose TEXT NOT NULL,
        contact VARCHAR(190) NOT NULL DEFAULT '',
        notes LONGTEXT NULL,
        created_at DATETIME NOT NULL,
        updated_at DATETIME NOT NULL,
        key_issued_at DATETIME NOT NULL,
        last_seen_at DATETIME NULL DEFAULT NULL,
        PRIMARY KEY (id),
        UNIQUE KEY participant_id (participant_id),
        UNIQUE KEY api_key_lookup (api_key_lookup),
        KEY agent_name (agent_name),
        KEY status (status),
        KEY last_seen_at (last_seen_at)
    ) {$charset};";

    dbDelta($sql);
}

function spiralist_book_pages_register_rest_routes(): void
{
    foreach (spiralist_book_pages_get_rest_namespaces() as $namespace) {
        register_rest_route(
            $namespace,
            '/site',
            [
                'methods' => WP_REST_Server::READABLE,
                'callback' => 'spiralist_book_pages_rest_get_site',
                'permission_callback' => '__return_true',
            ]
        );

        register_rest_route(
            $namespace,
            '/symbols',
            [
                'methods' => WP_REST_Server::READABLE,
                'callback' => 'spiralist_book_pages_rest_get_symbols',
                'permission_callback' => '__return_true',
            ]
        );

        register_rest_route(
            $namespace,
            '/symbols/registry',
            [
                'methods' => WP_REST_Server::READABLE,
                'callback' => 'spiralist_book_pages_rest_get_symbol_registry',
                'permission_callback' => '__return_true',
            ]
        );

        register_rest_route(
            $namespace,
            '/axioms',
            [
                'methods' => WP_REST_Server::READABLE,
                'callback' => 'spiralist_book_pages_rest_get_axioms',
                'permission_callback' => '__return_true',
            ]
        );

        register_rest_route(
            $namespace,
            '/kernel',
            [
                'methods' => WP_REST_Server::READABLE,
                'callback' => 'spiralist_book_pages_rest_get_kernel',
                'permission_callback' => '__return_true',
            ]
        );

        register_rest_route(
            $namespace,
            '/prompts',
            [
                'methods' => WP_REST_Server::READABLE,
                'callback' => 'spiralist_book_pages_rest_get_prompts',
                'permission_callback' => '__return_true',
            ]
        );

        register_rest_route(
            $namespace,
            '/manuscript/search',
            [
                'methods' => WP_REST_Server::READABLE,
                'callback' => 'spiralist_book_pages_rest_search_manuscript',
                'permission_callback' => '__return_true',
            ]
        );

        register_rest_route(
            $namespace,
            '/manuscript/manifest',
            [
                'methods' => WP_REST_Server::READABLE,
                'callback' => 'spiralist_book_pages_rest_get_manuscript_manifest',
                'permission_callback' => '__return_true',
            ]
        );

        register_rest_route(
            $namespace,
            '/manuscript/iiif-search',
            [
                'methods' => WP_REST_Server::READABLE,
                'callback' => 'spiralist_book_pages_rest_get_manuscript_iiif_search',
                'permission_callback' => '__return_true',
            ]
        );

        register_rest_route(
            $namespace,
            '/contribute',
            [
                'methods' => WP_REST_Server::CREATABLE,
                'callback' => 'spiralist_book_pages_rest_submit_contribution',
                'permission_callback' => '__return_true',
            ]
        );

        register_rest_route(
            $namespace,
            '/interactions',
            [
                'methods' => WP_REST_Server::CREATABLE,
                'callback' => 'spiralist_book_pages_rest_record_interaction',
                'permission_callback' => static function (): bool {
                    return is_user_logged_in();
                },
            ]
        );

        register_rest_route(
            $namespace,
            '/spec',
            [
                'methods' => WP_REST_Server::READABLE,
                'callback' => 'spiralist_book_pages_rest_get_spec',
                'permission_callback' => '__return_true',
            ]
        );

        register_rest_route(
            $namespace,
            '/examples',
            [
                'methods' => WP_REST_Server::READABLE,
                'callback' => 'spiralist_book_pages_rest_get_examples',
                'permission_callback' => '__return_true',
            ]
        );

        register_rest_route(
            $namespace,
            '/subscribe',
            [
                [
                    'methods' => WP_REST_Server::READABLE,
                    'callback' => 'spiralist_book_pages_rest_describe_subscription',
                    'permission_callback' => '__return_true',
                ],
                [
                    'methods' => WP_REST_Server::CREATABLE,
                    'callback' => 'spiralist_book_pages_rest_subscribe',
                    'permission_callback' => '__return_true',
                ],
            ]
        );

        register_rest_route(
            $namespace,
            '/participant',
            [
                'methods' => WP_REST_Server::READABLE,
                'callback' => 'spiralist_book_pages_rest_get_participant',
                'permission_callback' => 'spiralist_book_pages_rest_participant_permission_callback',
            ]
        );

        register_rest_route(
            $namespace,
            '/participant/resources',
            [
                'methods' => WP_REST_Server::READABLE,
                'callback' => 'spiralist_book_pages_rest_get_participant_resources',
                'permission_callback' => static function (WP_REST_Request $request) {
                    return spiralist_book_pages_rest_participant_permission_callback($request, 'access_participant_resources');
                },
            ]
        );
    }
}
add_action('rest_api_init', 'spiralist_book_pages_register_rest_routes');

function spiralist_book_pages_rest_response(array $payload, int $status = 200, bool $no_store = false): WP_REST_Response
{
    $response = new WP_REST_Response($payload, $status);
    $response->header('Content-Type', 'application/json; charset=utf-8');

    if ($no_store) {
        $response->header('Cache-Control', 'no-store');
    }

    return $response;
}