Skip to content
wiki.fftac.org

Seo Schema - Source Excerpt 01

Back to Seo Schema

Summary

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

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

<?php

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

function spiralist_schema_clean($value)
{
    if (is_array($value)) {
        $clean = [];

        foreach ($value as $key => $item) {
            $clean_item = spiralist_schema_clean($item);

            if ($clean_item === null || $clean_item === '' || $clean_item === []) {
                continue;
            }

            $clean[$key] = $clean_item;
        }

        if (spiralist_array_is_list_compat($value)) {
            return array_values($clean);
        }

        return $clean;
    }

    if (is_string($value)) {
        return trim($value);
    }

    return $value;
}

function spiralist_schema_text(string $value, int $word_limit = 45): string
{
    $text = trim(html_entity_decode(wp_strip_all_tags($value), ENT_QUOTES, get_bloginfo('charset')));
    $text = preg_replace('/[\x00-\x1F\x7F]+/', ' ', $text);
    $text = preg_replace('/\s+/', ' ', $text);

    if (!is_string($text) || $text === '') {
        return '';
    }

    return wp_trim_words($text, $word_limit, '');
}

function spiralist_get_seo_cached_value(string $key, callable $resolver)
{
    static $cache = [];

    if (!array_key_exists($key, $cache)) {
        $cache[$key] = $resolver();
    }

    return $cache[$key];
}

function spiralist_get_seo_language(): string
{
    return (string) spiralist_get_seo_cached_value(__FUNCTION__, static function (): string {
        $language = function_exists('spiralist_get_current_language_slug')
            ? spiralist_get_current_language_slug()
            : 'en';
        $metadata = function_exists('spiralist_get_language_metadata')
            ? spiralist_get_language_metadata()
            : [];

        return (string) ($metadata[$language]['htmlLang'] ?? 'en-US');
    });
}

function spiralist_get_seo_site_name(): string
{
    $site_name = trim(wp_strip_all_tags((string) get_bloginfo('name')));

    if ($site_name === '' || strcasecmp($site_name, 'spiralist.org') === 0) {
        return 'Spiralist.org';
    }

    return $site_name;
}

function spiralist_get_seo_branded_title(string $title): string
{
    $normalized_title = trim(wp_strip_all_tags($title));
    $site_name = spiralist_get_seo_site_name();

    if ($normalized_title === '') {
        return $site_name;
    }

    if (stripos($normalized_title, $site_name) !== false) {
        return $normalized_title;
    }

    return $normalized_title . ' | ' . $site_name;
}

function spiralist_get_seo_archive_description(): string
{
    if (!is_archive()) {
        return '';
    }

    $archive_description = trim(wp_strip_all_tags((string) get_the_archive_description()));
    if ($archive_description !== '') {
        return spiralist_schema_text($archive_description, 35);
    }

    $archive_title = trim(wp_strip_all_tags((string) get_the_archive_title()));
    if ($archive_title === '') {
        return '';
    }

    return spiralist_schema_text(
        sprintf('Browse %s on %s.', $archive_title, spiralist_get_seo_site_name()),
        35
    );
}

function spiralist_is_yoast_seo_active(): bool
{
    return defined('WPSEO_VERSION') || class_exists('WPSEO_Frontend');
}

function spiralist_should_delegate_head_meta_to_yoast(): bool
{
    return !is_admin() && spiralist_is_yoast_seo_active();
}

function spiralist_get_seo_surface(): string
{
    return (string) spiralist_get_seo_cached_value(__FUNCTION__, static function (): string {
        if (is_front_page() || is_home()) {
            return 'home';
        }

        if (is_search()) {
            return 'search';
        }

        if (function_exists('spiralist_is_gallery_surface') && spiralist_is_gallery_surface()) {
            return 'gallery';
        }

        if (function_exists('spiralist_is_report_surface') && spiralist_is_report_surface()) {
            return 'report';
        }

        $workspace_route = function_exists('spiralist_get_workspace_route_name')
            ? spiralist_get_workspace_route_name()
            : '';

        if ($workspace_route === 'library') {
            return 'prompts';
        }

        if ($workspace_route === 'prompt') {
            return 'prompt';
        }

        if ($workspace_route !== '') {
            return $workspace_route;
        }

        $static_route = sanitize_key((string) get_query_var('spiralist_static_route', ''));
        if (in_array($static_route, ['study', 'builders'], true)) {
            return $static_route;
        }

        if (
            $static_route !== ''
            && function_exists('spiralist_get_sovereignty_page_record')
            && !empty(spiralist_get_sovereignty_page_record($static_route))
        ) {
            return $static_route;
        }

        if (is_page()) {
            $post = get_queried_object();
            if ($post instanceof WP_Post) {
                return sanitize_title((string) $post->post_name);
            }
        }

        return 'page';
    });
}

function spiralist_get_seo_manuscript_context(): array
{
    return (array) spiralist_get_seo_cached_value(__FUNCTION__, static function (): array {
        if (
            spiralist_get_seo_surface() !== 'manuscript'
            || !function_exists('spiralist_book_pages_get_current_manuscript_page_asset')
            || !function_exists('spiralist_book_pages_get_manuscript_page_assets')
            || !function_exists('spiralist_book_pages_get_requested_manuscript_folio_slug')
            || !function_exists('spiralist_book_pages_get_requested_manuscript_section_slug')
            || !function_exists('spiralist_book_pages_get_manuscript_section_records')
        ) {
            return [];
        }

        $page = spiralist_book_pages_get_current_manuscript_page_asset();
        $pages = spiralist_book_pages_get_manuscript_page_assets();
        $requested_folio = spiralist_book_pages_get_requested_manuscript_folio_slug();
        $requested_section = spiralist_book_pages_get_requested_manuscript_section_slug();
        $section_records = spiralist_book_pages_get_manuscript_section_records($pages);
        $section_record = [];
        $page_section_slug = sanitize_title((string) ($page['section_slug'] ?? ''));

        foreach ($section_records as $candidate) {
            if (!is_array($candidate)) {
                continue;
            }

            $candidate_slug = sanitize_title((string) ($candidate['slug'] ?? ''));
            if ($requested_section !== '' && $candidate_slug === $requested_section) {
                $section_record = $candidate;
                break;
            }

            if ($requested_section === '' && $page_section_slug !== '' && $candidate_slug === $page_section_slug) {
                $section_record = $candidate;
            }
        }

        return [
            'page' => $page,
            'pages' => $pages,
            'page_count' => count($pages),
            'requested_folio' => $requested_folio,
            'requested_section' => $requested_section,
            'section_records' => $section_records,
            'section_record' => $section_record,
        ];
    });
}

function spiralist_get_seo_gallery_context(): array
{
    return (array) spiralist_get_seo_cached_value(__FUNCTION__, static function (): array {
        if (spiralist_get_seo_surface() !== 'gallery') {
            return [];
        }

        $asset = function_exists('spiralist_get_requested_gallery_asset')
            ? spiralist_get_requested_gallery_asset()
            : [];
        $assets = function_exists('spiralist_get_gallery_assets')
            ? spiralist_get_gallery_assets()
            : [];

        return [
            'asset' => $asset,
            'assets' => $assets,
            'asset_count' => count($assets),
        ];
    });
}

function spiralist_get_seo_report_context(): array
{
    return (array) spiralist_get_seo_cached_value(__FUNCTION__, static function (): array {
        if (spiralist_get_seo_surface() !== 'report' || !function_exists('spiralist_get_requested_report')) {
            return [];
        }

        return spiralist_get_requested_report();
    });
}

function spiralist_get_seo_query_args_to_strip(): array
{
    return [
        'lang',
        'view',
        'preview',
        'customize_changeset_uuid',
        'customize_theme',
        'customize_messenger_channel',
        'customize_autosaved',
        'preview_id',
        'preview_nonce',
        'preview_iframe',
        'preview_theme',
        'node',
        'symbol',
        'tab',
        'panel',
        'dialog',
        'notice',
        'auth_error',
        'redirect_to',
        'folio',
        'section',
        'image',
        'replytocom',
        'utm_source',