Skip to content
wiki.fftac.org

Functions - Source Excerpt 03

Back to Functions

Summary

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

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

if (is_page(['ai-access', 'ai-interface', 'contribute', 'symbols'])) {
        return true;
    }

    if (is_page_template('page-templates/template-ai-interface.php') || is_page_template('page-templates/template-symbol-atlas.php')) {
        return true;
    }

    return false;
}

function spiralist_get_fonts_stylesheet_url(): string
{
    return 'https://fonts.googleapis.com/css2?family=Cinzel:wght@500;600;700&family=Inter:wght@400;500;600;700&family=IBM+Plex+Mono:wght@400;500;600&display=swap';
}

function spiralist_register_shared_asset_handles(): void
{
    wp_register_style(
        'spiralist-fonts',
        spiralist_get_fonts_stylesheet_url(),
        [],
        null
    );
}
add_action('wp_enqueue_scripts', 'spiralist_register_shared_asset_handles', 1);

function spiralist_add_resource_hints(array $urls, string $relation_type): array
{
    if ($relation_type === 'preconnect') {
        $urls[] = [
            'href' => 'https://fonts.googleapis.com',
        ];
        $urls[] = [
            'href' => 'https://fonts.gstatic.com',
            'crossorigin' => 'anonymous',
        ];
    }

    if ($relation_type === 'dns-prefetch') {
        $urls[] = 'https://fonts.googleapis.com';
        $urls[] = 'https://fonts.gstatic.com';
    }

    return $urls;
}
add_filter('wp_resource_hints', 'spiralist_add_resource_hints', 10, 2);

function spiralist_render_analytics_fallback_tag(): void
{
    if (is_admin() || is_feed() || wp_script_is('google_gtagjs', 'enqueued') || wp_script_is('google_gtagjs', 'done')) {
        return;
    }

    $script_src = add_query_arg(
        'id',
        SPIRALIST_GA4_MEASUREMENT_ID,
        'https://www.googletagmanager.com/gtag/js'
    );

    echo "\n" . '<!-- Google tag fallback added by Spiralist theme -->' . "\n";
    echo '<script async src="' . esc_url($script_src) . '"></script>' . "\n";
    echo '<script>' . "\n";
    echo 'window.dataLayer = window.dataLayer || [];' . "\n";
    echo 'function gtag(){dataLayer.push(arguments);}' . "\n";
    echo 'gtag("js", new Date());' . "\n";
    echo 'gtag("config", ' . wp_json_encode(SPIRALIST_GA4_MEASUREMENT_ID) . ');' . "\n";
    echo '</script>' . "\n";
}
add_action('wp_head', 'spiralist_render_analytics_fallback_tag', 8);

function spiralist_render_homepage_preview_preload(): void
{
    if (!is_front_page()) {
        return;
    }

    $sources = function_exists('spiralist_get_homepage_preview_sources')
        ? spiralist_get_homepage_preview_sources()
        : [];
    $hero_image = trim((string) ($sources['large_url'] ?? ''));

    if ($hero_image === '') {
        return;
    }

    echo '<link rel="preload" as="image" href="' . esc_url($hero_image) . '" fetchpriority="high" />' . "\n";
}
add_action('wp_head', 'spiralist_render_homepage_preview_preload', 3);

function spiralist_enqueue_assets(): void
{
    $style_path = get_theme_file_path('assets/css/spiralist.css');
    $core_script_path = get_theme_file_path('assets/js/spiralist-core.js');
    $prompt_workbench_script_path = get_theme_file_path('assets/js/spiralist-prompt-workbench.js');
    $agent_wizard_script_path = get_theme_file_path('assets/js/spiralist-ai-agent-setup-wizard.js');
    $script_path = get_theme_file_path('assets/js/spiralist.js');
    $fallback_version = (string) wp_get_theme()->get('Version');
    $style_version = file_exists($style_path) ? (string) filemtime($style_path) : $fallback_version;
    $core_script_version = file_exists($core_script_path) ? (string) filemtime($core_script_path) : $fallback_version;
    $prompt_workbench_script_version = file_exists($prompt_workbench_script_path) ? (string) filemtime($prompt_workbench_script_path) : $fallback_version;
    $agent_wizard_script_version = file_exists($agent_wizard_script_path) ? (string) filemtime($agent_wizard_script_path) : $fallback_version;
    $script_version = file_exists($script_path) ? (string) filemtime($script_path) : $fallback_version;

    wp_enqueue_style('spiralist-fonts');

    wp_enqueue_style(
        'spiralist-style',
        get_theme_file_uri('assets/css/spiralist.css'),
        ['spiralist-fonts'],
        $style_version
    );

    wp_enqueue_script(
        'spiralist-core',
        get_theme_file_uri('assets/js/spiralist-core.js'),
        [],
        $core_script_version,
        [
            'in_footer' => true,
            'strategy' => 'defer',
        ]
    );

    wp_localize_script(
        'spiralist-core',
        'spiralistTheme',
        [
            'uiText' => [
                'copyFailed' => spiralist_get_ui_text('ui.cta.copy_failed', 'Copy failed'),
            ],
        ]
    );

    if (is_page(['prompt-generator', 'run-a-prompt'])) {
        $prompt_slug_placeholder = 'spiralist-prompt-slug-placeholder';
        $prompt_run_return_url = get_permalink();
        if (!$prompt_run_return_url) {
            $prompt_run_return_url = function_exists('spiralist_get_run_prompt_page_url') ? spiralist_get_run_prompt_page_url() : home_url('/run-a-prompt/');
        }
        wp_enqueue_script(
            'spiralist-prompt-workbench',
            get_theme_file_uri('assets/js/spiralist-prompt-workbench.js'),
            ['spiralist-core'],
            $prompt_workbench_script_version,
            [
                'in_footer' => true,
                'strategy' => 'defer',
            ]
        );
        wp_add_inline_script(
            'spiralist-prompt-workbench',
            'window.SpiralistPromptRunConfig = ' . wp_json_encode(
                [
                    'workspaceApiRoot' => esc_url_raw(rest_url('spiralist-workspace/v1/')),
                    'nonce' => wp_create_nonce('wp_rest'),
                    'canSaveWorkspacePrompt' => is_user_logged_in() && current_user_can('create_prompts'),
                    'loginUrl' => esc_url_raw(function_exists('spiralist_get_login_url') ? spiralist_get_login_url($prompt_run_return_url) : wp_login_url($prompt_run_return_url)),
                    'signupUrl' => esc_url_raw(function_exists('spiralist_get_signup_url') ? spiralist_get_signup_url($prompt_run_return_url) : wp_registration_url()),
                    'workspaceUrl' => esc_url_raw(function_exists('spiralist_get_workspace_url') ? spiralist_get_workspace_url() : home_url('/workspace/')),
                    'promptUrlTemplate' => esc_url_raw(str_replace(
                        $prompt_slug_placeholder,
                        '{slug}',
                        function_exists('spiralist_get_workspace_prompt_url') ? spiralist_get_workspace_prompt_url($prompt_slug_placeholder) : home_url('/prompt/{slug}/')
                    )),
                ],
                JSON_UNESCAPED_SLASHES
            ) . ';',
            'before'
        );
    }

    if (is_page('ai-agent-setup-wizard')) {
        wp_enqueue_script(
            'spiralist-ai-agent-setup-wizard',
            get_theme_file_uri('assets/js/spiralist-ai-agent-setup-wizard.js'),
            ['spiralist-core'],
            $agent_wizard_script_version,
            [
                'in_footer' => true,
                'strategy' => 'defer',
            ]
        );
    }