Runtime - Source Excerpt 07
Summary
This source excerpt preserves a bounded section of Spiralist/wp-content/plugins/ns12-manuscript/includes/runtime.php so readers can inspect the evidence without opening the full source file.
**Source path:** Spiralist/wp-content/plugins/ns12-manuscript/includes/runtime.php
? rtrim((string) SPIRALIST_BOOK_PAGES_STORAGE_PATH, '/\\') . DIRECTORY_SEPARATOR . 'image-variants'
: '';
$url = defined('SPIRALIST_BOOK_PAGES_STORAGE_URL')
? untrailingslashit((string) SPIRALIST_BOOK_PAGES_STORAGE_URL) . '/image-variants'
: '';
if ($path === '' || $url === '') {
return [];
}
if (!is_dir($path) && !wp_mkdir_p($path)) {
return [];
}
return [
'path' => $path,
'url' => $url,
];
}
function spiralist_book_pages_get_book_page_variant_location(array $asset, string $variant): array
{
$root = spiralist_book_pages_get_book_page_variant_storage_root();
$source_path = trim((string) ($asset['path'] ?? ''));
$source_filename = trim((string) ($asset['filename'] ?? basename($source_path)));
$extension = strtolower((string) pathinfo($source_filename, PATHINFO_EXTENSION));
if (empty($root) || $source_path === '' || $source_filename === '' || $extension === '') {
return [];
}
$variant_path = rtrim((string) ($root['path'] ?? ''), '/\\') . DIRECTORY_SEPARATOR . $variant;
$variant_url = untrailingslashit((string) ($root['url'] ?? '')) . '/' . rawurlencode($variant);
if ($variant_path === '' || $variant_url === '') {
return [];
}
if (!is_dir($variant_path) && !wp_mkdir_p($variant_path)) {
return [];
}
$basename = sanitize_title((string) pathinfo($source_filename, PATHINFO_FILENAME));
if ($basename === '') {
$basename = 'folio';
}
// Keep variant filenames portable across environments so pre-generated previews
// can be shipped in content-layer deploys without depending on local absolute paths.
$path_signature = substr(md5(strtolower($source_filename)), 0, 12);
$variant_filename = $basename . '-' . $path_signature . '.' . $extension;
return [
'path' => $variant_path . DIRECTORY_SEPARATOR . $variant_filename,
'url' => $variant_url . '/' . rawurlencode($variant_filename),
'filename' => $variant_filename,
];
}
function spiralist_book_pages_generate_book_page_asset_variant(array $asset, string $variant): array
{
static $cache = [];
$normalized_variant = sanitize_key($variant);
$source_path = trim((string) ($asset['path'] ?? ''));
$source_url = trim((string) ($asset['url'] ?? ''));
$source_filename = trim((string) ($asset['filename'] ?? basename($source_path)));
$specs = spiralist_book_pages_get_book_page_asset_variant_specs();
$original = [
'path' => $source_path,
'url' => $source_url,
'filename' => $source_filename,
];
if (
$normalized_variant === ''
|| !isset($specs[$normalized_variant])
|| $source_path === ''
|| !is_file($source_path)
|| !is_readable($source_path)
) {
return $original;
}
$cache_key = implode(
'|',
[
$normalized_variant,
strtolower(wp_normalize_path($source_path)),
(string) (@filemtime($source_path) ?: 0),
(string) (filesize($source_path) ?: 0),
]
);
if (isset($cache[$cache_key])) {
return $cache[$cache_key];
}
$location = spiralist_book_pages_get_book_page_variant_location($asset, $normalized_variant);
if (empty($location['path']) || empty($location['url'])) {
$cache[$cache_key] = $original;
return $cache[$cache_key];
}
$target_path = (string) $location['path'];
$source_mtime = (int) (@filemtime($source_path) ?: 0);
$target_mtime = (int) (@filemtime($target_path) ?: 0);
if (is_file($target_path) && $target_mtime >= $source_mtime) {
$cache[$cache_key] = $location;
return $cache[$cache_key];
}
if (!function_exists('wp_get_image_editor')) {
require_once ABSPATH . 'wp-admin/includes/image.php';
}
if (!function_exists('wp_get_image_editor')) {
$cache[$cache_key] = $original;
return $cache[$cache_key];
}
$editor = wp_get_image_editor($source_path);
if (is_wp_error($editor)) {
$cache[$cache_key] = $original;
return $cache[$cache_key];
}
$spec = $specs[$normalized_variant];
$size = method_exists($editor, 'get_size') ? (array) $editor->get_size() : [];
$width = (int) ($size['width'] ?? 0);
$height = (int) ($size['height'] ?? 0);
$copied = false;
if ($width > 0 && $height > 0 && $width <= (int) $spec['max_width'] && $height <= (int) $spec['max_height']) {
$copied = @copy($source_path, $target_path);
} else {
$resized = $editor->resize((int) $spec['max_width'], (int) $spec['max_height'], false);
if (is_wp_error($resized)) {
$cache[$cache_key] = $original;
return $cache[$cache_key];
}
$saved = $editor->save($target_path);
if (is_wp_error($saved) || empty($saved['path']) || !is_file((string) $saved['path'])) {
$cache[$cache_key] = $original;
return $cache[$cache_key];
}
}
if ($copied && !is_file($target_path)) {
$cache[$cache_key] = $original;
return $cache[$cache_key];
}
@chmod($target_path, 0644);
$cache[$cache_key] = $location;
return $cache[$cache_key];
}
function spiralist_book_pages_get_book_page_asset_source_urls(array $asset, bool $include_interactive = true): array
{
$original_url = trim((string) ($asset['url'] ?? ''));
$thumbnail_asset = spiralist_book_pages_generate_book_page_asset_variant($asset, 'thumbnail');
$thumbnail_url = trim((string) ($thumbnail_asset['url'] ?? ''));
$large_asset = spiralist_book_pages_generate_book_page_asset_variant($asset, 'large');
$large_url = trim((string) ($large_asset['url'] ?? $original_url));
$interactive_url = '';
if ($include_interactive) {
$interactive_asset = spiralist_book_pages_generate_book_page_asset_variant($asset, 'interactive');
$interactive_url = trim((string) ($interactive_asset['url'] ?? ''));
}
if ($interactive_url === '') {
$interactive_url = $large_url !== '' ? $large_url : $original_url;
}
if ($large_url === '') {
$large_url = $original_url !== '' ? $original_url : $interactive_url;
}
if ($thumbnail_url === '') {
$thumbnail_url = $large_url !== '' ? $large_url : ($original_url !== '' ? $original_url : $interactive_url);
}
return [
'original_url' => $original_url,
'large_url' => $large_url,
'thumbnail_url' => $thumbnail_url,
'interactive_url' => $interactive_url,
];
}
function spiralist_book_pages_get_public_book_pages_directory(): array
{
return spiralist_book_pages_get_plugin_book_pages_directory();
}
function spiralist_book_pages_get_case_insensitive_asset_from_directory(array $directory, string $basename): array
{
$base_path = rtrim((string) ($directory['path'] ?? ''), '/\\');
$base_url = trailingslashit((string) ($directory['url'] ?? ''));
$target = strtolower(trim($basename));
if ($base_path === '' || $base_url === '' || $target === '' || !is_dir($base_path) || !is_readable($base_path)) {
return [];
}
$matches = glob($base_path . DIRECTORY_SEPARATOR . '*');
if (!is_array($matches)) {
return [];
}
foreach ($matches as $match) {
if (!is_string($match) || $match === '' || !is_file($match) || !is_readable($match)) {
continue;
}
$resolved_name = basename($match);
if (strtolower($resolved_name) !== $target) {
continue;
}
return [
'path' => $match,
'url' => $base_url . rawurlencode($resolved_name),
'filename' => $resolved_name,
];
}
return [];
}
function spiralist_book_pages_get_requested_gallery_asset_slug(): string
{
$slug = sanitize_title((string) get_query_var('spiralist_book_pages_gallery_asset', ''));
if ($slug !== '') {
return $slug;
}
return isset($_GET['image']) ? sanitize_title((string) wp_unslash($_GET['image'])) : '';
}
function spiralist_book_pages_get_requested_gallery_asset(): array
{
$slug = spiralist_book_pages_get_requested_gallery_asset_slug();
return $slug !== '' ? spiralist_book_pages_get_gallery_asset_by_slug($slug) : [];
}
function spiralist_book_pages_get_featured_gallery_assets(int $limit = 6): array
{
if ($limit < 1) {
return [];
}
$assets = spiralist_book_pages_get_gallery_assets();
$prominent = array_values(
array_filter(
$assets,
static fn(array $asset): bool => !empty($asset['is_prominent'])