Helpers - Source Excerpt 12
Summary
This source excerpt preserves a bounded section of Spiralist/wp-content/themes/spiralist/inc/helpers.php so readers can inspect the evidence without opening the full source file.
**Source path:** Spiralist/wp-content/themes/spiralist/inc/helpers.php
$slug = sanitize_title((string) ($report['slug'] ?? ''));
if ($slug === '') {
return spiralist_get_reports_url();
}
return trailingslashit(spiralist_get_reports_url()) . rawurlencode($slug) . '/';
}
function spiralist_prepare_static_report(array $report): array
{
$slug = sanitize_title((string) ($report['slug'] ?? ''));
if ($slug === '') {
return [];
}
$source = trim((string) ($report['source'] ?? ''));
$html = trim((string) ($report['html'] ?? ''));
$source_path = $source !== '' ? spiralist_get_report_source_path($source) : '';
$html_path = $html !== '' ? get_theme_file_path($html) : '';
if ($html_path === '' || !is_readable($html_path)) {
return [];
}
$report['slug'] = $slug;
$report['url'] = spiralist_get_report_url($report);
$report['source_path'] = $source_path;
$report['html_path'] = $html_path;
$report['modified'] = gmdate('Y-m-d', (int) (@filemtime($html_path) ?: time()));
return $report;
}
function spiralist_get_static_reports(): array
{
static $reports = null;
if (is_array($reports)) {
return $reports;
}
$reports = array_values(array_filter(array_map(
'spiralist_prepare_static_report',
spiralist_get_static_report_blueprints()
)));
usort(
$reports,
static function (array $left, array $right): int {
return strcmp((string) ($right['published'] ?? ''), (string) ($left['published'] ?? ''))
?: strcmp((string) ($left['title'] ?? ''), (string) ($right['title'] ?? ''));
}
);
return $reports;
}
function spiralist_get_static_report_by_slug(string $slug): array
{
$normalized = sanitize_title($slug);
if ($normalized === '') {
return [];
}
foreach (spiralist_get_static_reports() as $report) {
if (sanitize_title((string) ($report['slug'] ?? '')) === $normalized) {
return $report;
}
$aliases = array_values(array_map(
static function ($alias): string {
return sanitize_title((string) $alias);
},
(array) ($report['aliases'] ?? [])
));
if (in_array($normalized, $aliases, true)) {
return $report;
}
}
return [];
}
function spiralist_get_requested_report_slug(): string
{
return sanitize_title((string) get_query_var('spiralist_report', ''));
}
function spiralist_get_requested_report(): array
{
return spiralist_get_static_report_by_slug(spiralist_get_requested_report_slug());
}
function spiralist_is_report_surface(): bool
{
return spiralist_get_requested_report_slug() !== '';
}
function spiralist_get_report_html(array $report): string
{
static $cache = [];
$html_path = (string) ($report['html_path'] ?? '');
if ($html_path === '' || !is_readable($html_path)) {
return '';
}
$cache_key = $html_path . '|' . (string) (filemtime($html_path) ?: 0);
if (isset($cache[$cache_key])) {
return $cache[$cache_key];
}
$html = file_get_contents($html_path);
$cache[$cache_key] = is_string($html) ? spiralist_resolve_report_url_tokens($html) : '';
return $cache[$cache_key];
}
function spiralist_resolve_report_url_tokens(string $html): string
{
if ($html === '' || strpos($html, '{{spiralist-url:') === false) {
return $html;
}
return preg_replace_callback(
'/{{spiralist-url:([A-Za-z0-9_-]+)(?::([A-Za-z0-9_.-]+))?}}/',
static function (array $matches): string {
$type = (string) ($matches[1] ?? '');
$value = (string) ($matches[2] ?? '');
switch ($type) {
case 'prompts':
return function_exists('spiralist_get_prompts_page_url') ? spiralist_get_prompts_page_url() : home_url('/prompts/');
case 'system':
return function_exists('spiralist_get_system_url') ? spiralist_get_system_url() : spiralist_get_page_url('system');
case 'reports':
return spiralist_get_reports_url();
case 'contribute':
return function_exists('spiralist_get_contribute_page_url') ? spiralist_get_contribute_page_url() : spiralist_get_page_url('contribute');
case 'prompt':
if ($value !== '' && function_exists('spiralist_get_workspace_prompt_url')) {
return spiralist_get_workspace_prompt_url($value);
}
return $value !== '' ? home_url('/prompt/' . rawurlencode($value) . '/') : spiralist_get_prompts_page_url();
default:
return '#';
}
},
$html
) ?? $html;
}
function spiralist_format_report_date(string $date): string
{
$timestamp = strtotime($date);
if (!$timestamp) {
return $date;
}
return date_i18n('F j, Y', $timestamp);
}
function spiralist_get_workspace_route_name(): string
{
return sanitize_key((string) get_query_var('spiralist_workspace_route'));
}
function spiralist_is_workspace_public_surface(): bool
{
return in_array(
spiralist_get_workspace_route_name(),
['library', 'prompt', 'workspace', 'members', 'run', 'conversation'],
true
);
}
function spiralist_get_workspace_prompt_url(string $slug): string
{
$normalized = sanitize_title($slug);
if ($normalized === '') {
return spiralist_get_prompts_page_url();
}
$url = home_url('/prompt/' . rawurlencode($normalized) . '/');
$language = function_exists('spiralist_get_current_language_slug')
? spiralist_get_current_language_slug()
: '';
if ($language !== '' && function_exists('spiralist_get_language_prefixed_url')) {
return spiralist_get_language_prefixed_url($url, $language);
}
return $url;
}
function spiralist_get_workspace_prompt_record_by_slug(string $slug): array
{
$normalized = sanitize_title($slug);
if ($normalized === '' || !function_exists('spiralist_workspace')) {
return [];
}
$plugin = spiralist_workspace();
if (!is_object($plugin) || !method_exists($plugin, 'boot') || !method_exists($plugin, 'prompts') || !method_exists($plugin, 'permissions')) {
return [];
}
$plugin->boot();
$viewer_user_id = function_exists('get_current_user_id') ? get_current_user_id() : 0;
$prompt = $plugin->prompts()->find_by_slug($normalized, $viewer_user_id);
if (empty($prompt) || !$plugin->permissions()->can_view_prompt($prompt, $viewer_user_id)) {
return [];
}
return $prompt;
}
function spiralist_get_current_workspace_prompt_record(): array
{
return spiralist_get_workspace_prompt_record_by_slug((string) get_query_var('spiralist_workspace_prompt_slug'));
}
function spiralist_get_workspace_library_prompt_records(int $limit = 24): array
{
if ($limit < 1 || !function_exists('spiralist_workspace')) {
return [];
}
$plugin = spiralist_workspace();
if (!is_object($plugin) || !method_exists($plugin, 'boot') || !method_exists($plugin, 'frontend')) {
return [];
}
$plugin->boot();
return array_slice((array) $plugin->frontend()->library_prompts(), 0, $limit);
}
function spiralist_get_homepage_prompt_surface_cache_key(): string
{
return 'homepage_prompt_surface_v1';
}
function spiralist_flush_homepage_prompt_surface_cache(): void
{
spiralist_delete_runtime_cache(spiralist_get_homepage_prompt_surface_cache_key());
}
function spiralist_get_homepage_prompt_surface(): array
{
static $cache = null;
if (is_array($cache)) {
return $cache;
}
$empty = [
'public_prompts' => [],
'official_prompts' => [],
'archive_prompts' => [],
'run_prompt' => [],
'prompt_systems' => [],
'popular_categories' => [],
'top_prompts' => [],
];
if (!function_exists('spiralist_workspace')) {
$cache = $empty;
return $cache;
}
$plugin = spiralist_workspace();
if (!is_object($plugin) || !method_exists($plugin, 'boot') || !method_exists($plugin, 'frontend')) {
$cache = $empty;
return $cache;
}
$ttl = defined('HOUR_IN_SECONDS') ? HOUR_IN_SECONDS : 3600;
$cached = spiralist_read_runtime_cache(spiralist_get_homepage_prompt_surface_cache_key());
$generated_at = (int) ($cached['generated_at'] ?? 0);
$payload = $cached['payload'] ?? null;
if ($generated_at > 0 && ($generated_at + $ttl) >= time() && is_array($payload)) {
$cache = array_merge($empty, $payload);
return $cache;
}