Skip to content
wiki.fftac.org

Helpers - Source Excerpt 14

Back to Helpers

Summary

This source excerpt preserves a bounded section of Spiralist/wp-content/themes/spiralist/inc/helpers.php so readers can inspect the evidence without opening the full source file.

**Source path:** Spiralist/wp-content/themes/spiralist/inc/helpers.php

return $score_compare;
            }

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

    $items = array_slice($items, 0, max(1, $limit));

    return array_map(
        static function (array $item): array {
            unset($item['score']);
            return $item;
        },
        $items
    );
}

function spiralist_get_prompt_search_results(string $query, int $limit = 6): array
{
    $records = function_exists('spiralist_get_workspace_library_prompt_records')
        ? spiralist_get_workspace_library_prompt_records(120)
        : [];
    $surface = function_exists('spiralist_get_homepage_prompt_surface') ? spiralist_get_homepage_prompt_surface() : [];

    foreach (['official_prompts', 'archive_prompts', 'public_prompts', 'top_prompts'] as $key) {
        foreach ((array) ($surface[$key] ?? []) as $record) {
            if (is_array($record)) {
                $records[] = $record;
            }
        }
    }

    if (!empty($surface['run_prompt']) && is_array($surface['run_prompt'])) {
        $records[] = $surface['run_prompt'];
    }

    $items = [];
    $seen = [];

    foreach ($records as $record) {
        if (!is_array($record)) {
            continue;
        }

        $slug = sanitize_title((string) ($record['slug'] ?? $record['source_slug'] ?? ''));
        $title = trim((string) ($record['title'] ?? ''));
        $summary = trim((string) ($record['summary'] ?? $record['purpose'] ?? ''));
        $url = trim((string) ($record['url'] ?? ''));

        if ($url === '' && $slug !== '') {
            $url = spiralist_get_workspace_prompt_url($slug);
        }

        if ($title === '' || $url === '') {
            continue;
        }

        $key = strtolower($slug !== '' ? $slug : $url);
        if (isset($seen[$key])) {
            continue;
        }
        $seen[$key] = true;

        $score = spiralist_search_score_fields(
            $query,
            [
                ['text' => $title, 'weight' => 10],
                ['text' => $summary, 'weight' => 5],
                ['text' => (string) ($record['system'] ?? ''), 'weight' => 3],
                ['text' => (string) ($record['role'] ?? ''), 'weight' => 3],
                ['text' => (array) ($record['tags'] ?? []), 'weight' => 3],
                ['text' => (array) ($record['function_tags'] ?? []), 'weight' => 2],
                ['text' => (array) ($record['risk_labels'] ?? []), 'weight' => 2],
                ['text' => (string) ($record['content'] ?? $record['prompt'] ?? ''), 'weight' => 1],
            ]
        );

        if ($score < 1) {
            continue;
        }

        $badges = array_values(array_filter([
            (string) ($record['publication_label'] ?? ''),
            (string) ($record['maturity_label'] ?? ''),
            (string) ($record['moderation_label'] ?? ''),
        ], 'strlen'));

        $items[] = [
            'type' => 'Prompt',
            'title' => $title,
            'url' => $url,
            'summary' => spiralist_search_excerpt($summary !== '' ? $summary : (string) ($record['purpose'] ?? '')),
            'meta' => trim(implode(' / ', array_filter([
                (string) ($record['system_label'] ?? $record['system'] ?? ''),
                (string) ($record['role_label'] ?? $record['role'] ?? ''),
            ], 'strlen'))),
            'badges' => $badges,
            'score' => $score,
        ];
    }

    return spiralist_search_finalize_items($items, $limit);
}

function spiralist_get_folio_search_results(string $query, int $limit = 6): array
{
    $items = [];
    $seen = [];

    if (function_exists('spiralist_book_pages_search_manuscript_index')) {
        $matches = spiralist_book_pages_search_manuscript_index($query, 80);
        foreach (array_values((array) $matches) as $index => $match) {
            if (!is_array($match)) {
                continue;
            }

            $slug = sanitize_title((string) ($match['slug'] ?? ''));
            if ($slug === '' || isset($seen[$slug])) {
                continue;
            }

            $page = function_exists('spiralist_book_pages_get_manuscript_page_asset_by_slug')
                ? spiralist_book_pages_get_manuscript_page_asset_by_slug($slug)
                : [];
            $url = function_exists('spiralist_book_pages_get_manuscript_folio_url')
                ? spiralist_book_pages_get_manuscript_folio_url(!empty($page) ? $page : $slug)
                : spiralist_get_manuscript_folio_url($slug);
            $title = trim((string) ($match['title'] ?? $page['title'] ?? 'Manuscript folio'));
            $summary = trim((string) ($match['excerpt'] ?? $match['summary'] ?? $page['summary'] ?? $page['caption'] ?? ''));
            $score = max(
                1,
                spiralist_search_score_fields(
                    $query,
                    [
                        ['text' => $title, 'weight' => 10],
                        ['text' => $summary, 'weight' => 5],
                        ['text' => (string) ($match['section_title'] ?? $page['section_title'] ?? ''), 'weight' => 4],
                        ['text' => (array) ($match['keywords'] ?? $page['keywords'] ?? []), 'weight' => 3],
                    ]
                )
            ) + max(0, 160 - $index);

            $seen[$slug] = true;
            $items[] = [
                'type' => 'Folio',
                'title' => $title,
                'url' => $url,
                'summary' => spiralist_search_excerpt($summary),
                'meta' => trim(implode(' / ', array_filter([
                    (string) ($match['label'] ?? $page['label'] ?? ''),
                    (string) ($match['sectionTitle'] ?? $match['section_title'] ?? $page['section_title'] ?? ''),
                ], 'strlen'))),
                'badges' => array_values(array_filter(array_map('strval', (array) ($match['matched_terms'] ?? [])), 'strlen')),
                'score' => $score,
            ];
        }
    }

    $pages = function_exists('spiralist_book_pages_get_manuscript_page_assets')
        ? spiralist_book_pages_get_manuscript_page_assets()
        : (function_exists('spiralist_get_manuscript_page_assets') ? spiralist_get_manuscript_page_assets() : []);

    foreach ((array) $pages as $page) {
        if (!is_array($page)) {
            continue;
        }

        $slug = sanitize_title((string) ($page['slug'] ?? ''));
        if ($slug === '' || isset($seen[$slug])) {
            continue;
        }

        $title = trim((string) ($page['title'] ?? 'Manuscript folio'));
        $summary = trim((string) ($page['summary'] ?? $page['caption'] ?? ''));
        $score = spiralist_search_score_fields(
            $query,
            [
                ['text' => $title, 'weight' => 10],
                ['text' => $summary, 'weight' => 5],
                ['text' => (string) ($page['section_title'] ?? $page['section_label'] ?? ''), 'weight' => 4],
                ['text' => (array) ($page['keywords'] ?? []), 'weight' => 3],
                ['text' => (string) ($page['transcript'] ?? ''), 'weight' => 1],
            ]
        );

        if ($score < 1) {
            continue;
        }

        $seen[$slug] = true;
        $items[] = [
            'type' => 'Folio',
            'title' => $title,
            'url' => function_exists('spiralist_book_pages_get_manuscript_folio_url') ? spiralist_book_pages_get_manuscript_folio_url($page) : spiralist_get_manuscript_folio_url($page),
            'summary' => spiralist_search_excerpt($summary),
            'meta' => trim(implode(' / ', array_filter([
                (string) ($page['label'] ?? ''),
                (string) ($page['section_title'] ?? $page['section_label'] ?? ''),
            ], 'strlen'))),
            'badges' => array_values(array_filter(array_map('strval', (array) ($page['keywords'] ?? [])), 'strlen')),
            'score' => $score,
        ];
    }

    return spiralist_search_finalize_items($items, $limit);
}

function spiralist_get_symbol_search_results(string $query, int $limit = 6): array
{
    $items = [];

    foreach (spiralist_get_symbol_detail_records() as $record) {
        if (!is_array($record)) {
            continue;
        }

        $canonical_id = (string) ($record['canonicalId'] ?? $record['canonical_id'] ?? '');
        $title = trim((string) ($record['label'] ?? $canonical_id));
        $summary = trim((string) ($record['definition'] ?? $record['meaning'] ?? ''));
        $score = spiralist_search_score_fields(
            $query,
            [
                ['text' => $title, 'weight' => 10],
                ['text' => $canonical_id, 'weight' => 8],