Skip to content
wiki.fftac.org

Functions - Source Excerpt 01

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

<?php

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

require_once get_theme_file_path('inc/helpers.php');
require_once get_theme_file_path('inc/auth-boundary.php');
require_once get_theme_file_path('inc/legal-pages.php');
require_once get_theme_file_path('inc/legal-consent.php');
require_once get_theme_file_path('inc/seo-schema.php');
require_once get_theme_file_path('inc/roadmap-surfaces.php');
require_once get_theme_file_path('inc/machine-manifests.php');

const SPIRALIST_GA4_MEASUREMENT_ID = 'G-3QR6SPC2PS';

function spiralist_theme_setup(): void
{
    add_theme_support('title-tag');
    add_theme_support('post-thumbnails');
    add_theme_support(
        'html5',
        [
            'comment-form',
            'comment-list',
            'gallery',
            'caption',
            'script',
            'style',
        ]
    );

    register_nav_menus(
        [
            'primary' => 'Primary Navigation',
        ]
    );
}
add_action('after_setup_theme', 'spiralist_theme_setup');

function spiralist_register_theme_query_vars(array $vars): array
{
    $vars[] = 'spiralist_gallery_route';
    $vars[] = 'spiralist_gallery_asset';
    $vars[] = 'spiralist_report';
    $vars[] = 'spiralist_symbol';
    $vars[] = 'spiralist_agent_wizard_preset';

    return array_values(array_unique($vars));
}
add_filter('query_vars', 'spiralist_register_theme_query_vars');

function spiralist_get_theme_route_version(): string
{
    return '31';
}

function spiralist_register_theme_rewrite_rules(): void
{
    add_rewrite_rule(
        '^symbols/([^/]+)/?$',
        'index.php?pagename=symbols&spiralist_symbol=$matches[1]',
        'top'
    );

    add_rewrite_rule(
        '^reports/([^/]+)/?$',
        'index.php?spiralist_report=$matches[1]',
        'top'
    );

    add_rewrite_rule(
        '^gallery/image/([^/]+)/?$',
        'index.php?spiralist_gallery_route=gallery&spiralist_gallery_asset=$matches[1]',
        'top'
    );

    add_rewrite_rule(
        '^gallery/?$',
        'index.php?spiralist_gallery_route=gallery',
        'top'
    );

    add_rewrite_rule(
        '^ai-agent-setup-wizard/([^/]+)/?$',
        'index.php?pagename=ai-agent-setup-wizard&spiralist_agent_wizard_preset=$matches[1]',
        'top'
    );
}
add_action('init', 'spiralist_register_theme_rewrite_rules', 20);

function spiralist_flush_theme_rewrite_rules(): void
{
    spiralist_register_theme_rewrite_rules();
    flush_rewrite_rules(false);
    update_option('spiralist_theme_route_version', spiralist_get_theme_route_version(), false);
}
add_action('after_switch_theme', 'spiralist_flush_theme_rewrite_rules');

function spiralist_maybe_flush_theme_rewrite_rules(): void
{
    $stored_version = (string) get_option('spiralist_theme_route_version', '');
    $current_version = spiralist_get_theme_route_version();

    if ($stored_version === $current_version) {
        return;
    }

    spiralist_register_theme_rewrite_rules();
    flush_rewrite_rules(false);
    update_option('spiralist_theme_route_version', $current_version, false);
}
add_action('init', 'spiralist_maybe_flush_theme_rewrite_rules', 100);

function spiralist_maybe_flush_homepage_prompt_surface_cache(int $post_id): void
{
    if (!function_exists('spiralist_flush_homepage_prompt_surface_cache')) {
        return;
    }

    if ($post_id <= 0 || wp_is_post_revision($post_id) || get_post_type($post_id) !== 'spiralist_prompt') {
        return;
    }

    spiralist_flush_homepage_prompt_surface_cache();
}
add_action('save_post_spiralist_prompt', 'spiralist_maybe_flush_homepage_prompt_surface_cache', 10, 1);
add_action('trashed_post', 'spiralist_maybe_flush_homepage_prompt_surface_cache', 10, 1);
add_action('untrashed_post', 'spiralist_maybe_flush_homepage_prompt_surface_cache', 10, 1);

function spiralist_public_page_cache_ttl(): int
{
    return defined('MINUTE_IN_SECONDS') ? 5 * MINUTE_IN_SECONDS : 300;
}

function spiralist_public_page_cache_request_uri(): string
{
    $path = '';

    if (class_exists('UAIXLocaleRouter\\Router') && method_exists('UAIXLocaleRouter\\Router', 'get_original_request_path')) {
        $path = (string) \UAIXLocaleRouter\Router::get_original_request_path();
    }

    if ($path === '') {
        $request_uri = isset($_SERVER['REQUEST_URI']) ? (string) wp_unslash($_SERVER['REQUEST_URI']) : '/';
        $path = (string) wp_parse_url($request_uri, PHP_URL_PATH);
    }

    if ($path === '') {
        $path = '/';
    }

    $query = isset($_SERVER['QUERY_STRING']) ? trim((string) wp_unslash($_SERVER['QUERY_STRING'])) : '';

    return $path . ($query !== '' ? '?' . $query : '');
}

function spiralist_public_page_cache_source_signature(): string
{
    static $source_signature = null;

    if ($source_signature !== null) {
        return $source_signature;
    }

    $theme = wp_get_theme();
    $version = $theme->exists() ? (string) $theme->get('Version') : '0';
    $scan_paths = [get_theme_file_path()];
    $latest = 0;
    $extensions = ['php', 'css', 'js', 'json'];

    if (defined('WP_PLUGIN_DIR')) {
        $plugin_paths = [
            WP_PLUGIN_DIR . '/uaix-locale-router/uaix-locale-router.php',
            WP_PLUGIN_DIR . '/uaix-locale-router/src',
            WP_PLUGIN_DIR . '/uaix-locale-router/data',
            WP_PLUGIN_DIR . '/uaix-locale-router/templates',
            WP_PLUGIN_DIR . '/ns12-manuscript/includes',
            WP_PLUGIN_DIR . '/ns12-manuscript/templates',
        ];
        $scan_paths = array_merge($scan_paths, $plugin_paths);
    }

    foreach ($scan_paths as $path) {
        if (is_file($path)) {
            $extension = strtolower((string) pathinfo($path, PATHINFO_EXTENSION));
            if (in_array($extension, $extensions, true)) {
                $latest = max($latest, (int) (@filemtime($path) ?: 0));
            }

            continue;
        }

        if (!is_dir($path) || !class_exists('RecursiveIteratorIterator') || !class_exists('RecursiveDirectoryIterator')) {
            continue;
        }

        $iterator = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS)
        );

        foreach ($iterator as $file) {
            if (
                $file instanceof SplFileInfo
                && $file->isFile()
                && in_array(strtolower($file->getExtension()), $extensions, true)
            ) {
                $latest = max($latest, (int) (@filemtime($file->getPathname()) ?: 0));
            }
        }
    }

    $source_signature = $version . '|' . (string) $latest;
    return $source_signature;
}

function spiralist_public_page_cache_key(): string
{
    $locale = function_exists('spiralist_get_current_locale_tag') ? (string) spiralist_get_current_locale_tag() : '';
    $locale_settings = [
        'current' => $locale,
        'default' => (string) get_option('uaixlr_default_locale', ''),
        'settings' => get_option('uaixlr_settings', []),
        'supported' => get_option('uaixlr_supported_locales', []),
    ];
    $locale_signature = function_exists('wp_json_encode')
        ? (string) wp_json_encode($locale_settings)
        : (string) json_encode($locale_settings);