Functions - Source Excerpt 02
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
return 'spiralist_public_html_' . md5(spiralist_public_page_cache_source_signature() . '|' . home_url('/') . '|' . spiralist_public_page_cache_request_uri() . '|' . $locale_signature);
}
function spiralist_can_cache_public_page(): bool
{
if (
is_admin()
|| is_user_logged_in()
|| wp_doing_ajax()
|| wp_doing_cron()
|| (defined('REST_REQUEST') && REST_REQUEST)
|| (defined('DONOTCACHEPAGE') && DONOTCACHEPAGE)
) {
return false;
}
$method = isset($_SERVER['REQUEST_METHOD']) ? strtoupper((string) wp_unslash($_SERVER['REQUEST_METHOD'])) : 'GET';
if (!in_array($method, ['GET', 'HEAD'], true) || !empty($_POST)) {
return false;
}
$query = isset($_SERVER['QUERY_STRING']) ? trim((string) wp_unslash($_SERVER['QUERY_STRING'])) : '';
if ($query !== '') {
return false;
}
if (
is_404()
|| is_search()
|| is_preview()
|| is_feed()
|| is_trackback()
|| (function_exists('is_robots') && is_robots())
) {
return false;
}
$path = strtolower((string) wp_parse_url(spiralist_public_page_cache_request_uri(), PHP_URL_PATH));
if (preg_match('#/(?:account|dashboard|login|register|sign-up|workspace|members|run|conversation|profile)(?:/|$)#', $path)) {
return false;
}
return true;
}
function spiralist_maybe_serve_public_page_cache(): void
{
if (!spiralist_can_cache_public_page()) {
return;
}
$cached = get_transient(spiralist_public_page_cache_key());
if (!is_array($cached) || !is_string($cached['html'] ?? null) || trim((string) $cached['html']) === '') {
return;
}
if (!headers_sent()) {
status_header(200);
header('Content-Type: text/html; charset=' . get_bloginfo('charset'));
header('Cache-Control: public, max-age=300, s-maxage=600, stale-while-revalidate=60');
header('X-Spiralist-Page-Cache: HIT');
}
echo (string) $cached['html'];
exit;
}
add_action('template_redirect', 'spiralist_maybe_serve_public_page_cache', 0);
function spiralist_start_public_page_cache_buffer(): void
{
if (!spiralist_can_cache_public_page()) {
return;
}
$cache_key = spiralist_public_page_cache_key();
$ttl = spiralist_public_page_cache_ttl();
if (!headers_sent()) {
header('X-Spiralist-Page-Cache: MISS');
}
ob_start(
static function (string $html) use ($cache_key, $ttl): string {
$status_code = http_response_code();
if (trim($html) === '' || $status_code < 200 || $status_code >= 300) {
return $html;
}
set_transient(
$cache_key,
[
'created_at' => time(),
'html' => $html,
],
$ttl
);
return $html;
}
);
}
add_action('template_redirect', 'spiralist_start_public_page_cache_buffer', 1);
function spiralist_filter_public_cache_headers(array $headers): array
{
if (!spiralist_can_cache_public_page()) {
return $headers;
}
$headers['Cache-Control'] = 'public, max-age=300, s-maxage=600, stale-while-revalidate=60';
$headers['Vary'] = isset($headers['Vary']) && $headers['Vary'] !== ''
? $headers['Vary'] . ', Accept-Encoding'
: 'Accept-Encoding';
return $headers;
}
add_filter('wp_headers', 'spiralist_filter_public_cache_headers');
function spiralist_get_current_request_url(): string
{
$home_parts = wp_parse_url(home_url('/'));
$scheme = (string) ($home_parts['scheme'] ?? (is_ssl() ? 'https' : 'http'));
$host = (string) ($home_parts['host'] ?? '');
$port = isset($home_parts['port']) ? ':' . (int) $home_parts['port'] : '';
$request_uri = isset($_SERVER['REQUEST_URI']) ? (string) wp_unslash($_SERVER['REQUEST_URI']) : '/';
if ($host === '') {
$host = isset($_SERVER['HTTP_HOST']) ? sanitize_text_field((string) wp_unslash($_SERVER['HTTP_HOST'])) : '';
}
if ($host === '') {
return home_url('/');
}
return $scheme . '://' . $host . $port . $request_uri;
}
function spiralist_render_language_alternates(): void
{
if (
is_admin()
|| !function_exists('spiralist_get_language_prefixed_url')
|| !function_exists('spiralist_get_enabled_locale_records')
|| !function_exists('spiralist_get_default_locale_record')
) {
return;
}
$current_url = function_exists('spiralist_get_seo_canonical_url')
? spiralist_get_seo_canonical_url()
: home_url('/');
foreach (spiralist_get_enabled_locale_records() as $record) {
$locale = trim(str_replace('_', '-', (string) ($record['tag'] ?? '')));
$url_tag = (string) ($record['urlTag'] ?? '');
if ($locale === '' || $url_tag === '') {
continue;
}
echo "\n" . '<link rel="alternate" hreflang="' . esc_attr($locale) . '" href="' . esc_url(spiralist_get_language_prefixed_url($current_url, $url_tag)) . '" />' . "\n";
}
$default_locale = (string) (spiralist_get_default_locale_record()['urlTag'] ?? '');
if ($default_locale !== '') {
echo '<link rel="alternate" hreflang="x-default" href="' . esc_url(spiralist_get_language_prefixed_url($current_url, $default_locale)) . '" />' . "\n";
}
}
add_action('wp_head', 'spiralist_render_language_alternates', 4);
function spiralist_render_manuscript_rel_links(): void
{
if (
is_admin()
|| !function_exists('spiralist_get_seo_surface')
|| spiralist_get_seo_surface() !== 'manuscript'
|| !function_exists('spiralist_get_seo_manuscript_context')
) {
return;
}
$context = spiralist_get_seo_manuscript_context();
if (empty($context)) {
return;
}
$requested_folio = (string) ($context['requested_folio'] ?? '');
$requested_section = (string) ($context['requested_section'] ?? '');
$page = (array) ($context['page'] ?? []);
$pages = array_values(array_filter((array) ($context['pages'] ?? []), 'is_array'));
$section_records = array_values(array_filter((array) ($context['section_records'] ?? []), 'is_array'));
$prev_url = '';
$next_url = '';
if ($requested_folio !== '' && !empty($page) && function_exists('spiralist_book_pages_get_manuscript_folio_url')) {
$current_slug = sanitize_title((string) ($page['slug'] ?? ''));
$current_index = -1;
foreach ($pages as $index => $candidate_page) {
if (sanitize_title((string) ($candidate_page['slug'] ?? '')) === $current_slug) {
$current_index = $index;
break;
}
}
if ($current_index > 0) {
$prev_url = (string) spiralist_book_pages_get_manuscript_folio_url($pages[$current_index - 1]);
}
if ($current_index >= 0 && isset($pages[$current_index + 1])) {
$next_url = (string) spiralist_book_pages_get_manuscript_folio_url($pages[$current_index + 1]);
}
} elseif ($requested_section !== '') {
$current_section_index = -1;
foreach ($section_records as $index => $section_record) {
if (sanitize_title((string) ($section_record['slug'] ?? '')) === $requested_section) {
$current_section_index = $index;
break;
}
}
if ($current_section_index > 0) {
$prev_url = (string) ($section_records[$current_section_index - 1]['url'] ?? '');
}
if ($current_section_index >= 0 && isset($section_records[$current_section_index + 1])) {
$next_url = (string) ($section_records[$current_section_index + 1]['url'] ?? '');
}
}
if ($prev_url !== '') {
echo '<link rel="prev" href="' . esc_url($prev_url) . '" />' . "\n";
}
if ($next_url !== '') {
echo '<link rel="next" href="' . esc_url($next_url) . '" />' . "\n";
}
}
add_action('wp_head', 'spiralist_render_manuscript_rel_links', 4);
function spiralist_theme_needs_shell_script(): bool
{
if (is_admin()) {
return false;
}
if (function_exists('spiralist_book_pages_is_book_shell_surface') && spiralist_book_pages_is_book_shell_surface()) {
return true;
}
if (function_exists('spiralist_is_gallery_surface') && spiralist_is_gallery_surface()) {
return true;
}
if (is_front_page() && is_user_logged_in()) {
return true;
}