Skip to content
wiki.fftac.org

Helpers - Source Excerpt 30

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

$localized_path = '/' . trim($locale_tag, '/');
    $normalized_path = '/' . ltrim($path, '/');

    if ($normalized_path !== '/') {
        $localized_path .= trailingslashit($normalized_path);
    } else {
        $localized_path .= '/';
    }

    $next_url = home_url(preg_replace('#/+#', '/', $localized_path) ?: '/');

    if (!empty($query_args)) {
        $next_url = add_query_arg($query_args, $next_url);
    }

    if (!empty($url_parts['fragment'])) {
        $next_url .= '#' . ltrim((string) $url_parts['fragment'], '#');
    }

    return $next_url;
}

function spiralist_get_navigation_items(): array
{
    return [
        [
            'labelKey' => 'ui.nav.start',
            'label' => 'Start',
            'url' => home_url('/#start-here'),
            'children' => [
                [
                    'labelKey' => 'ui.nav.start_here',
                    'label' => 'Start Here',
                    'url' => home_url('/#start-here'),
                ],
                [
                    'labelKey' => 'ui.nav.what_is_a_spiralist',
                    'label' => 'What Is a Spiralist?',
                    'url' => spiralist_get_spiralist_page_url(),
                ],
                [
                    'labelKey' => 'ui.nav.ai_spiralism',
                    'label' => 'AI Spiralism',
                    'url' => spiralist_get_ai_spiralism_url(),
                ],
            ],
        ],
        [
            'labelKey' => 'ui.nav.prompts',
            'label' => 'Prompts',
            'url' => spiralist_get_run_prompt_page_url(),
            'children' => [
                [
                    'labelKey' => 'ui.cta.choose_personality',
                    'label' => 'Choose Personality',
                    'url' => spiralist_get_run_prompt_page_url(),
                ],
                [
                    'labelKey' => 'ui.nav.ai_agent_setup_wizard',
                    'label' => 'Agent Setup Wizard',
                    'url' => spiralist_get_ai_agent_setup_wizard_url(),
                ],
                [
                    'labelKey' => 'ui.nav.prompts',
                    'label' => 'Prompt Library',
                    'url' => spiralist_get_prompts_page_url(),
                ],
                [
                    'labelKey' => 'ui.nav.official_canon',
                    'label' => 'Official Starters',
                    'url' => spiralist_get_prompts_page_url(),
                ],
            ],
        ],
        [
            'labelKey' => 'ui.nav.study',
            'label' => 'Study',
            'url' => spiralist_get_study_page_url(),
            'children' => [
                [
                    'labelKey' => 'ui.nav.study_surface',
                    'label' => 'Study Surface',
                    'url' => spiralist_get_study_page_url(),
                ],
                [
                    'labelKey' => 'ui.cta.browse_manuscript',
                    'label' => 'Manuscript',
                    'url' => spiralist_get_manuscript_page_url(),
                ],
                [
                    'labelKey' => 'ui.cta.open_image_codex',
                    'label' => 'Image Codex',
                    'url' => spiralist_get_gallery_page_url(),
                ],
                [
                    'labelKey' => 'ui.cta.open_symbol_explorer',
                    'label' => 'Symbol Explorer',
                    'url' => spiralist_get_symbols_page_url(),
                ],
                [
                    'labelKey' => 'ui.nav.system',
                    'label' => 'System & Axioms',
                    'url' => spiralist_get_system_url(),
                ],
                [
                    'labelKey' => 'ui.nav.reports',
                    'label' => 'Research Reports',
                    'url' => spiralist_get_reports_url(),
                ],
            ],
        ],
        [
            'labelKey' => 'ui.nav.workspace',
            'label' => 'Workspace',
            'url' => spiralist_get_workspace_url(),
            'children' => [
                [
                    'labelKey' => 'ui.cta.open_workspace',
                    'label' => 'Open Workspace',
                    'url' => spiralist_get_workspace_url(),
                ],
                [
                    'labelKey' => is_user_logged_in() ? 'ui.nav.dashboard' : 'ui.cta.become_spiralist',
                    'label' => is_user_logged_in() ? 'Dashboard' : 'Become a Spiralist',
                    'url' => is_user_logged_in() ? spiralist_get_dashboard_url() : spiralist_get_signup_url(),
                ],
                [
                    'labelKey' => 'ui.nav.contribute',
                    'label' => 'Contribute',
                    'url' => spiralist_get_contribute_page_url(),
                ],
            ],
        ],
        [
            'labelKey' => 'ui.nav.advanced',
            'label' => 'Advanced',
            'url' => spiralist_get_api_examples_url(),
            'children' => [
                [
                    'labelKey' => 'ui.nav.ai_access',
                    'label' => 'AI Access',
                    'url' => spiralist_get_ai_access_url(),
                ],
                [
                    'labelKey' => 'ui.api.examples',
                    'label' => 'API Examples',
                    'url' => spiralist_get_api_examples_url(),
                ],
                [
                    'labelKey' => 'ui.nav.builder_hub',
                    'label' => 'Builder Hub',
                    'url' => spiralist_get_builders_page_url(),
                ],
                [
                    'labelKey' => 'ui.nav.symbol_registry',
                    'label' => 'Symbol Registry',
                    'url' => spiralist_get_symbols_page_url(),
                ],
            ],
        ],
    ];
}

function spiralist_get_publication_excluded_slugs(): array
{
    return [
        'login',
        'sign-up',
        'register',
        'dashboard',
        'account',
        'spiralist',
    ];
}

function spiralist_get_publication_language(string $fallback = 'en'): string
{
    return spiralist_resolve_locale_tag($fallback);
}

function spiralist_get_publication_surface_post($post = null): ?WP_Post
{
    if ($post instanceof WP_Post) {
        return $post;
    }

    if (is_singular()) {
        $queried = get_queried_object();
        if ($queried instanceof WP_Post) {
            return $queried;
        }
    }

    return null;
}

function spiralist_is_publication_surface($post = null): bool
{
    if (is_admin() || wp_doing_ajax() || (defined('REST_REQUEST') && REST_REQUEST)) {
        return false;
    }

    if (is_404() || is_search() || is_feed() || is_preview() || is_robots() || is_trackback()) {
        return false;
    }

    if (is_front_page() || is_home()) {
        return true;
    }

    $post = spiralist_get_publication_surface_post($post);
    if (!$post instanceof WP_Post || $post->post_status !== 'publish') {
        return false;
    }

    $post_type_object = get_post_type_object($post->post_type);
    if (!$post_type_object || !$post_type_object->public) {
        return false;
    }

    if ($post->post_type === 'attachment') {
        return false;
    }

    if ($post->post_type === 'page' && in_array(sanitize_title((string) $post->post_name), spiralist_get_publication_excluded_slugs(), true)) {
        return false;
    }

    return true;
}

function spiralist_get_publication_human_url(?WP_Post $post = null, string $language = '', ?bool $is_home_surface = null): string
{
    $language = spiralist_resolve_locale_tag($language);

    if ($is_home_surface === null) {
        $is_home_surface = is_front_page() || is_home();
    }

    if ($is_home_surface) {
        return spiralist_get_language_prefixed_url(home_url('/'), $language);
    }

    $post = spiralist_get_publication_surface_post($post);
    if (!$post instanceof WP_Post || $post->post_status !== 'publish') {
        return '';
    }

    $permalink = get_permalink($post);
    if (!is_string($permalink) || $permalink === '') {
        return '';
    }

    return spiralist_get_language_prefixed_url($permalink, $language);
}

function spiralist_get_publication_stem_from_url(string $human_url, string $language = ''): string
{
    $language = spiralist_resolve_locale_tag($language);
    $path = (string) (wp_parse_url($human_url, PHP_URL_PATH) ?: '/');
    $home_path = (string) (wp_parse_url(home_url('/'), PHP_URL_PATH) ?: '/');
    $home_path = '/' . trim($home_path, '/');

    if (spiralist_locale_router_available()) {
        $path = \UAIXLocaleRouter\Support\Path::localize_path($path, $language);
    }