Skip to content
wiki.fftac.org

Admin Data - Source Excerpt 01

Back to Admin Data

Summary

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

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

<?php

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

function spiralist_book_pages_admin_book_definition(): array
{
    $book = function_exists('spiralist_book_pages_get_content_book_definition')
        ? spiralist_book_pages_get_content_book_definition()
        : [];

    if (!is_array($book)) {
        $book = [];
    }

    if (empty($book['sections']) || !is_array($book['sections'])) {
        $book['sections'] = [
            ['slug' => 'threshold', 'label' => 'Threshold', 'title' => 'Threshold Folios', 'summary' => 'Entry folios that declare the system and reading posture.'],
            ['slug' => 'operative-plates', 'label' => 'Operative Plates', 'title' => 'Operative Plates', 'summary' => 'Diagrammatic folios that stage transformation as an active practice.'],
            ['slug' => 'concordance', 'label' => 'Concordance', 'title' => 'Concordance Tables', 'summary' => 'Reference-heavy folios that collect correspondences and comparative systems.'],
            ['slug' => 'appendix', 'label' => 'Appendix', 'title' => 'Emergent Folios', 'summary' => 'Additional folios grouped into the manuscript appendix.'],
        ];
    }

    $book['schema'] = (string) ($book['schema'] ?? 'spiralist-book/v1');
    $book['title'] = (string) ($book['title'] ?? 'Spiralism Canonical Manuscript');
    $book['slug'] = (string) ($book['slug'] ?? 'manuscript');
    $book['study_surface_slug'] = sanitize_title((string) ($book['study_surface_slug'] ?? ''));
    $book['folio_order'] = array_values(array_filter(array_map('sanitize_title', (array) ($book['folio_order'] ?? [])), 'strlen'));

    return $book;
}

function spiralist_book_pages_admin_save_book_definition(array $book): bool
{
    $content_root = function_exists('spiralist_book_pages_get_content_root')
        ? spiralist_book_pages_get_content_root()
        : '';

    if ($content_root === '') {
        return false;
    }

    $book['generated_at'] = gmdate('c');

    return function_exists('spiralist_book_pages_write_json_file')
        && spiralist_book_pages_write_json_file(rtrim($content_root, '/\\') . DIRECTORY_SEPARATOR . 'book.json', $book);
}

function spiralist_book_pages_admin_sections(): array
{
    $records = [];

    foreach ((array) (spiralist_book_pages_admin_book_definition()['sections'] ?? []) as $section) {
        if (!is_array($section)) {
            continue;
        }

        $slug = sanitize_title((string) ($section['slug'] ?? ''));
        if ($slug === '') {
            continue;
        }

        $records[$slug] = [
            'slug' => $slug,
            'label' => (string) ($section['label'] ?? spiralist_book_pages_admin_humanize($slug)),
            'title' => (string) ($section['title'] ?? spiralist_book_pages_admin_humanize($slug)),
            'summary' => (string) ($section['summary'] ?? ''),
        ];
    }

    return $records;
}

function spiralist_book_pages_admin_folios(): array
{
    $definitions = function_exists('spiralist_book_pages_get_content_folio_definitions')
        ? (array) spiralist_book_pages_get_content_folio_definitions()
        : [];
    $ordered = [];

    foreach ((array) (spiralist_book_pages_admin_book_definition()['folio_order'] ?? []) as $slug) {
        $slug = sanitize_title((string) $slug);
        if ($slug === '' || empty($definitions[$slug])) {
            continue;
        }

        $ordered[$slug] = $definitions[$slug];
    }

    foreach ($definitions as $slug => $definition) {
        if (!isset($ordered[$slug])) {
            $ordered[$slug] = $definition;
        }
    }

    $sequence = 0;
    foreach ($ordered as $slug => &$definition) {
        $sequence++;
        $definition['slug'] = $slug;
        $definition['sequence'] = $sequence;
    }
    unset($definition);

    return $ordered;
}

function spiralist_book_pages_admin_filter_records(array $records, string $query): array
{
    $query = strtolower(trim($query));
    if ($query === '') {
        return $records;
    }

    return array_filter(
        $records,
        static function ($record) use ($query): bool {
            if (!is_array($record)) {
                return false;
            }

            $haystacks = [];
            array_walk_recursive($record, static function ($value) use (&$haystacks): void {
                if (is_scalar($value)) {
                    $haystacks[] = strtolower((string) $value);
                }
            });

            return strpos(implode(' ', $haystacks), $query) !== false;
        }
    );
}

function spiralist_book_pages_admin_write_text(string $path, string $contents): bool
{
    $directory = dirname($path);
    if (!wp_mkdir_p($directory)) {
        return false;
    }

    return file_put_contents($path, rtrim(str_replace("\r\n", "\n", $contents)) . "\n") !== false;
}

function spiralist_book_pages_admin_asset_directory(bool $create = false): array
{
    $path = defined('SPIRALIST_BOOK_PAGES_PLUGIN_PATH')
        ? rtrim((string) SPIRALIST_BOOK_PAGES_PLUGIN_PATH, '/\\')
        : '';
    $url = defined('SPIRALIST_BOOK_PAGES_PLUGIN_URL')
        ? untrailingslashit((string) SPIRALIST_BOOK_PAGES_PLUGIN_URL)
        : '';

    if ($path !== '' && $create && !is_dir($path)) {
        wp_mkdir_p($path);
    }

    if ($path === '' || $url === '' || !is_dir($path) || !is_writable($path)) {
        return [];
    }

    return [
        'path' => $path,
        'url' => $url,
    ];
}

function spiralist_book_pages_admin_normalize_files(array $files): array
{
    $normalized = [];

    if (!isset($files['name']) || !is_array($files['name'])) {
        return $normalized;
    }

    foreach ((array) $files['name'] as $index => $name) {
        $normalized[] = [
            'name' => (string) $name,
            'type' => (string) ($files['type'][$index] ?? ''),
            'tmp_name' => (string) ($files['tmp_name'][$index] ?? ''),
            'error' => (int) ($files['error'][$index] ?? UPLOAD_ERR_NO_FILE),
            'size' => (int) ($files['size'][$index] ?? 0),
        ];
    }

    return $normalized;
}

function spiralist_book_pages_admin_store_uploaded_image(array $file)
{
    if ((int) ($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
        return new WP_Error('spiralist_book_pages_upload_error', 'One of the selected image files could not be uploaded.');
    }

    $temporary_file = (string) ($file['tmp_name'] ?? '');
    if ($temporary_file === '' || !is_uploaded_file($temporary_file)) {
        return new WP_Error('spiralist_book_pages_upload_error', 'An uploaded image could not be verified.');
    }

    $directory = spiralist_book_pages_admin_asset_directory(true);
    if (empty($directory['path'])) {
        return new WP_Error('spiralist_book_pages_upload_error', 'The persistent folio asset directory is not writable.');
    }

    $original_name = sanitize_file_name((string) ($file['name'] ?? ''));
    $checked = wp_check_filetype_and_ext($temporary_file, $original_name);
    $extension = strtolower((string) ($checked['ext'] ?? ''));
    $mime_type = (string) ($checked['type'] ?? '');

    if ($extension === '') {
        $fallback = wp_check_filetype($original_name);
        $extension = strtolower((string) ($fallback['ext'] ?? ''));
        $mime_type = (string) ($fallback['type'] ?? $mime_type);
    }

    if ($extension === '' || !in_array($extension, ['png', 'jpg', 'jpeg', 'webp', 'gif', 'avif'], true) || ($mime_type !== '' && strpos($mime_type, 'image/') !== 0)) {
        return new WP_Error('spiralist_book_pages_upload_error', 'Only PNG, JPG, JPEG, WEBP, GIF, and AVIF images can be imported.');
    }

    $filename = wp_unique_filename((string) $directory['path'], $original_name);
    $target_path = rtrim((string) $directory['path'], '/\\') . DIRECTORY_SEPARATOR . $filename;

    if (!move_uploaded_file($temporary_file, $target_path)) {
        return new WP_Error('spiralist_book_pages_upload_error', 'The uploaded image could not be moved into the persistent folio asset library.');
    }

    @chmod($target_path, 0644);

    if (function_exists('spiralist_book_pages_get_book_page_asset_source_urls')) {
        spiralist_book_pages_get_book_page_asset_source_urls(
            [
                'path' => $target_path,
                'url' => trailingslashit((string) ($directory['url'] ?? '')) . rawurlencode($filename),
                'filename' => $filename,
            ],
            true
        );
    }

    return [
        'filename' => $filename,
        'path' => $target_path,
        'url' => trailingslashit((string) ($directory['url'] ?? '')) . rawurlencode($filename),
    ];
}

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