Version 1.7.1 — auto default_page detection

- config.json now has default_page: auto for fresh installs
- CodePressCMS::detectDefaultPage() scans content/ for first available file
- getHomepageTitle() also respects auto mode
- Admin config form preserves auto as selectable option
- Save handler falls back to auto instead of index
This commit is contained in:
2026-07-28 15:39:12 +02:00
parent 8b454cd038
commit 2faea90872
6 changed files with 54 additions and 7 deletions
+39 -1
View File
@@ -354,6 +354,9 @@ class CodePressCMS {
}
$page = $_GET['page'] ?? $this->config['default_page'];
if ($page === 'auto') {
$page = $this->detectDefaultPage();
}
// Limit length
$page = substr($page, 0, 255);
// Only remove file extension at the end, not all dots
@@ -1323,6 +1326,37 @@ class CodePressCMS {
return 'markdown';
}
/**
* Auto-detect the first available content page
*
* Scans the content directory for the first .md/.php/.html file
* (preferring non-language-prefixed files) and returns its page key.
*
* @return string Detected page key, or 'index' as fallback
*/
private function detectDefaultPage(): string
{
$contentDir = $this->config['content_dir'];
if (!is_dir($contentDir)) return 'index';
$files = scandir($contentDir);
$candidates = [];
foreach ($files as $f) {
if (preg_match('/^((?:nl|en)\.)?(.+?)\.(md|php|html)$/', $f, $m)) {
$candidates[] = ['prefix' => $m[1], 'name' => $m[2], 'file' => $f];
}
}
if (empty($candidates)) return 'index';
// Prefer non-language-prefixed files (e.g. index.md over nl.index.md)
foreach ($candidates as $c) {
if (empty($c['prefix'])) return $c['name'];
}
return $candidates[0]['name'];
}
/**
* Get homepage title
*
@@ -1330,7 +1364,11 @@ class CodePressCMS {
*/
private function getHomepageTitle() {
// Use formatted filename for homepage title in navigation
return $this->formatDisplayName($this->config['default_page']);
$page = $this->config['default_page'];
if ($page === 'auto') {
$page = $this->detectDefaultPage();
}
return $this->formatDisplayName($page);
}
/**