Storage
**Site relevance:** Spiralist.org
**Memory type:** plugin source memory
**Source path:** Spiralist/wp-content/plugins/ns12-manuscript/includes/storage.php
**Size:** 6.3 KB
Summary
if (!defined('ABSPATH'))
Source Preview
This source file is short enough to preview directly on its source-memory page.
<?php
if (!defined('ABSPATH')) {
exit;
}
function spiralist_book_pages_build_storage_profile(
string $basedir,
string $baseurl,
string $storage_slug,
string $asset_subdirectory
): array
{
return [
'path' => rtrim($basedir, '/\\') . DIRECTORY_SEPARATOR . trim($storage_slug, '/\\'),
'url' => untrailingslashit($baseurl) . '/' . trim($storage_slug, '/\\'),
'storage_slug' => trim($storage_slug, '/\\'),
'asset_subdirectory' => trim($asset_subdirectory, '/\\'),
];
}
function spiralist_book_pages_get_storage_profile(): array
{
$configured_path = defined('SPIRALIST_BOOK_PAGES_STORAGE_PATH')
? trim((string) constant('SPIRALIST_BOOK_PAGES_STORAGE_PATH'))
: '';
$configured_url = defined('SPIRALIST_BOOK_PAGES_STORAGE_URL')
? trim((string) constant('SPIRALIST_BOOK_PAGES_STORAGE_URL'))
: '';
$configured_asset_subdirectory = defined('SPIRALIST_BOOK_PAGES_STORAGE_ASSET_SUBDIRECTORY')
? trim((string) constant('SPIRALIST_BOOK_PAGES_STORAGE_ASSET_SUBDIRECTORY'))
: '';
$public_storage_slug = defined('SPIRALIST_BOOK_PAGES_PUBLIC_STORAGE_SLUG')
? trim((string) constant('SPIRALIST_BOOK_PAGES_PUBLIC_STORAGE_SLUG'))
: 'ns12-manuscript';
$public_asset_subdirectory = defined('SPIRALIST_BOOK_PAGES_PUBLIC_ASSET_SUBDIRECTORY')
? trim((string) constant('SPIRALIST_BOOK_PAGES_PUBLIC_ASSET_SUBDIRECTORY'))
: 'folios';
if ($configured_path !== '' && $configured_url !== '') {
return [
'path' => rtrim($configured_path, '/\\'),
'url' => untrailingslashit($configured_url),
'storage_slug' => $public_storage_slug,
'asset_subdirectory' => $configured_asset_subdirectory !== ''
? trim($configured_asset_subdirectory, '/\\')
: $public_asset_subdirectory,
];
}
if (!function_exists('wp_upload_dir')) {
return [];
}
$uploads = wp_upload_dir(null, false);
$basedir = rtrim((string) ($uploads['basedir'] ?? ''), '/\\');
$baseurl = untrailingslashit((string) ($uploads['baseurl'] ?? ''));
if ($basedir === '' || $baseurl === '') {
return [];
}
return spiralist_book_pages_build_storage_profile(
$basedir,
$baseurl,
$public_storage_slug,
$public_asset_subdirectory
);
}
function spiralist_book_pages_get_storage_base(): array
{
$profile = spiralist_book_pages_get_storage_profile();
return [
'path' => (string) ($profile['path'] ?? ''),
'url' => (string) ($profile['url'] ?? ''),
];
}
function spiralist_book_pages_storage_copy_missing(string $source, string $destination): bool
{
if ($source === '' || !is_dir($source) || !is_readable($source)) {
return false;
}
if (!wp_mkdir_p($destination)) {
return false;
}
$copied = false;
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $item) {
$source_path = (string) $item->getPathname();
$relative_path = ltrim(str_replace($source, '', $source_path), '\\/');
$target_path = rtrim($destination, '/\\') . DIRECTORY_SEPARATOR . str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $relative_path);
if ($item->isDir()) {
if (!is_dir($target_path)) {
wp_mkdir_p($target_path);
}
continue;
}
if (is_file($target_path)) {
continue;
}
$target_directory = dirname($target_path);
if (!is_dir($target_directory)) {
wp_mkdir_p($target_directory);
}
if (@copy($source_path, $target_path)) {
$copied = true;
}
}
return $copied;
}
function spiralist_book_pages_install_persistent_storage(): void
{
static $did_run = false;
if ($did_run) {
return;
}
$did_run = true;
$roots = [
'storage' => defined('SPIRALIST_BOOK_PAGES_STORAGE_PATH') ? (string) SPIRALIST_BOOK_PAGES_STORAGE_PATH : '',
'content' => defined('SPIRALIST_BOOK_PAGES_CONTENT_PATH') ? (string) SPIRALIST_BOOK_PAGES_CONTENT_PATH : '',
'cache' => defined('SPIRALIST_BOOK_PAGES_CACHE_PATH') ? (string) SPIRALIST_BOOK_PAGES_CACHE_PATH : '',
'exports' => defined('SPIRALIST_BOOK_PAGES_EXPORT_PATH') ? (string) SPIRALIST_BOOK_PAGES_EXPORT_PATH : '',
'book_pages' => defined('SPIRALIST_BOOK_PAGES_PLUGIN_PATH') ? (string) SPIRALIST_BOOK_PAGES_PLUGIN_PATH : '',
];
foreach ($roots as $path) {
if ($path === '') {
continue;
}
wp_mkdir_p($path);
}
$bundled_content = rtrim((string) SPIRALIST_BOOK_PAGES_PLUGIN_ROOT_PATH, '/\\') . DIRECTORY_SEPARATOR . 'content';
$copied_content = false;
if (($roots['content'] ?? '') !== '' && !is_file(rtrim((string) $roots['content'], '/\\') . DIRECTORY_SEPARATOR . 'book.json')) {
$copied_content = spiralist_book_pages_storage_copy_missing($bundled_content, (string) $roots['content']);
}
$cache_root = (string) ($roots['cache'] ?? '');
$cache_missing = $cache_root !== '' && !is_file(rtrim($cache_root, '/\\') . DIRECTORY_SEPARATOR . 'book-index.json');
if (($copied_content || $cache_missing) && function_exists('spiralist_book_pages_compile_content_cache')) {
spiralist_book_pages_compile_content_cache(true);
}
$should_update_status = $copied_content
|| $cache_missing
|| (function_exists('get_option') && get_option('spiralist_book_pages_storage_status', null) === null);
if ($should_update_status && function_exists('update_option')) {
update_option(
'spiralist_book_pages_storage_status',
[
'version' => '1.2.0',
'content_seeded' => $copied_content,
'storage_path' => (string) ($roots['storage'] ?? ''),
'updated_at' => gmdate('c'),
],
false
);
}
}
add_action('init', 'spiralist_book_pages_install_persistent_storage', 1);