Functions - Source Excerpt 07
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
function spiralist_get_theme_surface_page_blueprint_version(): string
{
return '1:' . md5((string) wp_json_encode(spiralist_get_theme_surface_page_blueprints()));
}
function spiralist_ensure_theme_surface_pages(): void
{
$current_version = spiralist_get_theme_surface_page_blueprint_version();
$stored_version = (string) get_option('spiralist_theme_surface_page_blueprint_version', '');
if ($stored_version === $current_version) {
return;
}
foreach (spiralist_get_theme_surface_page_blueprints() as $slug => $blueprint) {
$normalized = sanitize_title((string) $slug);
if ($normalized === '') {
continue;
}
$existing = get_page_by_path($normalized);
if ($existing instanceof WP_Post) {
continue;
}
wp_insert_post(
[
'post_type' => 'page',
'post_status' => 'publish',
'post_title' => (string) ($blueprint['title'] ?? ucwords(str_replace('-', ' ', $normalized))),
'post_name' => $normalized,
'post_content' => (string) ($blueprint['content'] ?? ''),
'comment_status' => 'closed',
'ping_status' => 'closed',
],
true
);
}
update_option('spiralist_theme_surface_page_blueprint_version', $current_version, false);
}
add_action('init', 'spiralist_ensure_theme_surface_pages', 15);
function spiralist_register_report_sitemap_provider(): void
{
if (!function_exists('spiralist_book_pages_register_sitemap_provider')) {
return;
}
spiralist_book_pages_register_sitemap_provider(
'spiralist_static_reports',
'spiralist_get_static_report_sitemap_entries',
['label' => 'Static reports', 'priority' => 22]
);
}
add_action('spiralist_book_pages_register_sitemap_providers', 'spiralist_register_report_sitemap_provider');
function spiralist_get_static_report_sitemap_entries(): array
{
if (
!function_exists('spiralist_get_static_reports')
|| !function_exists('spiralist_book_pages_sitemap_entry')
|| !function_exists('spiralist_book_pages_sitemap_timestamp')
) {
return [];
}
$entries = [];
foreach (spiralist_get_static_reports() as $report) {
$slug = sanitize_title((string) ($report['slug'] ?? ''));
$url = $slug !== '' ? home_url('/reports/' . rawurlencode($slug) . '/') : '';
if ($slug === '' || $url === '') {
continue;
}
$html_path = (string) ($report['html_path'] ?? '');
$source_path = (string) ($report['source_path'] ?? '');
$last_modified = max(
(int) ($html_path !== '' && is_readable($html_path) ? @filemtime($html_path) : 0),
(int) ($source_path !== '' && is_readable($source_path) ? @filemtime($source_path) : 0)
);
$entries[] = spiralist_book_pages_sitemap_entry(
'report-' . $slug,
'Reports',
(string) ($report['title'] ?? 'Research Report'),
$url,
[
'description' => (string) ($report['summary'] ?? ''),
'lastmod' => spiralist_book_pages_sitemap_timestamp($last_modified > 0 ? $last_modified : time()),
'priority' => '0.70',
]
);
}
return $entries;
}
function spiralist_get_manuscript_source_signature(string $source_file): string
{
if ($source_file === '' || !is_file($source_file) || !is_readable($source_file)) {
return '';
}
$filesize = (int) (filesize($source_file) ?: 0);
$modified_at = (int) (filemtime($source_file) ?: 0);
return basename($source_file) . '|' . $filesize . '|' . $modified_at;
}
function spiralist_ensure_manuscript_attachment(): void
{
if (defined('SPIRALIST_BOOK_PAGES_PLUGIN_FILE')) {
return;
}
$source_file = spiralist_get_manuscript_source_file();
if ($source_file === '') {
return;
}
$source_signature = spiralist_get_manuscript_source_signature($source_file);
if ($source_signature === '') {
return;
}
$stored_attachment_id = (int) get_option('spiralist_manuscript_attachment_id', 0);
$stored_signature = (string) get_option('spiralist_manuscript_attachment_signature', '');
if (
$stored_attachment_id > 0
&& $stored_signature !== ''
&& hash_equals($stored_signature, $source_signature)
) {
$attachment = get_post($stored_attachment_id);
if ($attachment instanceof WP_Post && $attachment->post_type === 'attachment') {
return;
}
}
$source_hash = md5_file($source_file);
if (!is_string($source_hash) || $source_hash === '') {
return;
}
$stored_hash = (string) get_option('spiralist_manuscript_attachment_hash', '');
if (
$stored_attachment_id > 0
&& $stored_signature !== ''
&& hash_equals($stored_signature, $source_signature)
&& hash_equals($stored_hash, $source_hash)
) {
return;
}
$existing = get_posts(
[
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => 1,
'meta_key' => '_spiralist_canonical_manuscript',
'meta_value' => '1',
'fields' => 'ids',
]
);
if (!empty($existing[0]) && $stored_hash !== '' && hash_equals($stored_hash, $source_hash)) {
update_option('spiralist_manuscript_attachment_id', (int) $existing[0], false);
update_option('spiralist_manuscript_attachment_signature', $source_signature, false);
return;
}
$image_contents = file_get_contents($source_file);
if (!is_string($image_contents) || $image_contents === '') {
return;
}
$upload = wp_upload_bits(basename($source_file), null, $image_contents);
if (!empty($upload['error']) || empty($upload['file']) || empty($upload['url'])) {
return;
}
$filetype = wp_check_filetype($upload['file']);
$attachment_id = wp_insert_attachment(
[
'guid' => $upload['url'],
'post_mime_type' => $filetype['type'] ?? 'image/png',
'post_title' => 'Spiralist Manuscript',
'post_content' => '',
'post_status' => 'inherit',
],
$upload['file']
);
if (is_wp_error($attachment_id) || !$attachment_id) {
return;
}
require_once ABSPATH . 'wp-admin/includes/image.php';
$metadata = wp_generate_attachment_metadata($attachment_id, $upload['file']);
if (is_array($metadata)) {
wp_update_attachment_metadata($attachment_id, $metadata);
}
update_post_meta($attachment_id, '_spiralist_canonical_manuscript', '1');
update_post_meta($attachment_id, '_spiralist_canonical_manuscript_hash', $source_hash);
update_option('spiralist_manuscript_attachment_id', (int) $attachment_id, false);
update_option('spiralist_manuscript_attachment_hash', $source_hash, false);
update_option('spiralist_manuscript_attachment_signature', $source_signature, false);
}
if (!defined('SPIRALIST_BOOK_PAGES_PLUGIN_FILE')) {
add_action('after_switch_theme', 'spiralist_ensure_manuscript_attachment');
add_action('init', 'spiralist_ensure_manuscript_attachment');
}
function spiralist_theme_template_include(string $template): string
{
if (function_exists('spiralist_is_report_surface') && spiralist_is_report_surface()) {
$report_template = get_theme_file_path('page-report.php');
return file_exists($report_template) ? $report_template : $template;
}
if (function_exists('spiralist_is_gallery_surface') && spiralist_is_gallery_surface()) {
$gallery_template = get_theme_file_path('page-gallery.php');
return file_exists($gallery_template) ? $gallery_template : $template;
}
return $template;
}
add_filter('template_include', 'spiralist_theme_template_include', 20);
function spiralist_filter_document_title(string $title): string
{
if (is_admin() || spiralist_should_delegate_head_meta_to_yoast() || !function_exists('spiralist_get_seo_title')) {
return $title;
}
$seo_title = trim(spiralist_get_seo_title());
if ($seo_title !== '') {
return $seo_title;
}
return $title;
}
add_filter('pre_get_document_title', 'spiralist_filter_document_title');
function spiralist_filter_wpseo_title($title): string
{
if (!function_exists('spiralist_get_seo_title')) {
return (string) $title;
}
$seo_title = trim(spiralist_get_seo_title());
return $seo_title !== '' ? $seo_title : (string) $title;
}