Skip to content
wiki.fftac.org

Seo Schema - Source Excerpt 12

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

if ($surface === 'search') {
        return 'SearchResultsPage';
    }

    if ($surface === 'what-is-a-spiralist') {
        return 'AboutPage';
    }

    return 'WebPage';
}

function spiralist_schema_webpage(string $surface, array $reference_ids, array $main_entity): array
{
    $canonical = spiralist_get_seo_canonical_url();
    $post = is_singular() ? get_queried_object() : null;
    $image = spiralist_get_seo_primary_image_data();
    $image_url = (string) ($image['url'] ?? '');
    $report = $surface === 'report' ? spiralist_get_seo_report_context() : [];
    $report_published = (string) ($report['published'] ?? '');
    $report_modified = (string) ($report['modified'] ?? $report_published);

    return [
        '@type' => spiralist_schema_page_type($surface),
        '@id' => spiralist_schema_page_id('webpage'),
        'url' => $canonical,
        'name' => spiralist_get_seo_title(),
        'description' => spiralist_get_seo_description(),
        'isPartOf' => [
            '@id' => spiralist_schema_id('website'),
        ],
        'publisher' => [
            '@id' => spiralist_schema_id('organization'),
        ],
        'breadcrumb' => [
            '@id' => spiralist_schema_page_id('breadcrumbs'),
        ],
        'mainEntity' => !empty($main_entity['@id']) ? [
            '@id' => (string) $main_entity['@id'],
        ] : '',
        'primaryImageOfPage' => $image_url !== '' ? [
            '@type' => 'ImageObject',
            'url' => $image_url,
            'width' => (int) ($image['width'] ?? 0),
            'height' => (int) ($image['height'] ?? 0),
        ] : '',
        'inLanguage' => spiralist_get_seo_language(),
        'datePublished' => $report_published !== '' ? gmdate('c', (int) strtotime($report_published)) : ($post instanceof WP_Post ? get_post_time('c', true, $post) : ''),
        'dateModified' => $report_modified !== '' ? gmdate('c', (int) strtotime($report_modified)) : ($post instanceof WP_Post ? get_post_modified_time('c', true, $post) : ''),
        'citation' => $reference_ids,
    ];
}

function spiralist_schema_graph(): array
{
    return (array) spiralist_get_seo_cached_value(__FUNCTION__, static function (): array {
        if (is_admin() || is_404()) {
            return [];
        }

        $surface = spiralist_get_seo_surface();
        $references = [];
        $reference_ids = spiralist_schema_reference_ids($references);
        $main_entity = spiralist_schema_main_entity($surface, $reference_ids);
        $supplemental_entities = spiralist_schema_supplemental_entities($surface, $reference_ids);
        $graph = [
            spiralist_schema_organization(),
            spiralist_schema_website(),
            spiralist_schema_breadcrumbs(),
            spiralist_schema_webpage($surface, $reference_ids, $main_entity),
        ];

        if (!empty($main_entity)) {
            $graph[] = $main_entity;
        }

        if (!empty($supplemental_entities)) {
            $graph = array_merge($graph, $supplemental_entities);
        }

        return [
            '@context' => 'https://schema.org',
            '@graph' => array_values(array_map('spiralist_schema_clean', $graph)),
        ];
    });
}

function spiralist_render_seo_meta(): void
{
    if (is_admin() || is_404() || spiralist_should_delegate_head_meta_to_yoast()) {
        return;
    }

    echo "\n" . '<link rel="canonical" href="' . esc_url(spiralist_get_seo_canonical_url()) . '" />' . "\n";
    echo '<meta name="description" content="' . esc_attr(spiralist_get_seo_description()) . '" />' . "\n";
    echo '<meta name="robots" content="' . esc_attr(spiralist_get_seo_robots_directives()) . '" />' . "\n";
}
add_action('wp_head', 'spiralist_render_seo_meta', 3);

function spiralist_render_json_ld_schema(): void
{
    $graph = spiralist_schema_graph();
    if (empty($graph)) {
        return;
    }

    $json = wp_json_encode(
        spiralist_schema_clean($graph),
        JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
    );

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

    echo "\n" . '<script type="application/ld+json">' . $json . '</script>' . "\n";
}
add_action('wp_head', 'spiralist_render_json_ld_schema', 6);

remove_action('wp_head', 'rel_canonical');