Add docblocks to ContentAPI and handleGuide

This commit is contained in:
2026-07-28 14:40:55 +02:00
parent 890510c4c6
commit 3bb16ff116
3 changed files with 101 additions and 0 deletions
+1
View File
@@ -540,6 +540,7 @@ class CodePressCMS {
* Parse Markdown content to HTML using League CommonMark * Parse Markdown content to HTML using League CommonMark
* *
* @param string $content Raw Markdown content * @param string $content Raw Markdown content
* @param string $actualFilePath Path to the source file (used for display name fallback)
* @return array Parsed content with title and body * @return array Parsed content with title and body
*/ */
public function parseMarkdown($content, $actualFilePath = '') { public function parseMarkdown($content, $actualFilePath = '') {
+90
View File
@@ -1,5 +1,12 @@
<?php <?php
/**
* Content API for PHP content files
*
* Provides a safe, read-only interface for PHP content files to access
* CMS data (pages, menu, config, navigation, translations, search).
* Only instantiated inside parsePHP() — never exposed via URL.
*/
class ContentAPI class ContentAPI
{ {
private CodePressCMS $cms; private CodePressCMS $cms;
@@ -9,11 +16,22 @@ class ContentAPI
$this->cms = $cms; $this->cms = $cms;
} }
/**
* Get all pages as a flat array of path => title pairs
*
* @return array Associative array like ['index' => 'Home', 'over-ons' => 'Over ons']
*/
public function getAllPages(): array public function getAllPages(): array
{ {
return $this->cms->getAllPageTitles(); return $this->cms->getAllPageTitles();
} }
/**
* Get a single page by path, including title, content, layout, and metadata
*
* @param string $path Page path without extension (e.g. 'over-ons' or 'blog/post-1')
* @return array|null Page data or null if not found
*/
public function getPage(string $path): ?array public function getPage(string $path): ?array
{ {
$contentDir = $this->cms->config['content_dir']; $contentDir = $this->cms->config['content_dir'];
@@ -60,11 +78,23 @@ class ContentAPI
]; ];
} }
/**
* Get the navigation menu structure
*
* @return array Hierarchical menu array with 'title', 'path', 'children', 'active' keys
*/
public function getMenu(): array public function getMenu(): array
{ {
return $this->cms->getMenu(); return $this->cms->getMenu();
} }
/**
* Get a config value using dot notation
*
* @param string $key Config key, e.g. 'site_title' or 'features.search'
* @param mixed $default Default value if key is not found
* @return mixed Config value or default
*/
public function getConfig(string $key, $default = null) public function getConfig(string $key, $default = null)
{ {
$keys = explode('.', $key); $keys = explode('.', $key);
@@ -80,11 +110,24 @@ class ContentAPI
return $value; return $value;
} }
/**
* Get the current language code (e.g. 'nl' or 'en')
*
* @return string
*/
public function getCurrentLanguage(): string public function getCurrentLanguage(): string
{ {
return $this->cms->currentLanguage; return $this->cms->currentLanguage;
} }
/**
* Build a URL for a page, optionally with a specific language and extra params
*
* @param string $page Page path (default 'index')
* @param string|null $lang Language code (defaults to current language)
* @param array $params Additional query parameters
* @return string URL string starting with '?'
*/
public function buildUrl(string $page = 'index', ?string $lang = null, array $params = []): string public function buildUrl(string $page = 'index', ?string $lang = null, array $params = []): string
{ {
$lang = $lang ?? $this->getCurrentLanguage(); $lang = $lang ?? $this->getCurrentLanguage();
@@ -95,6 +138,12 @@ class ContentAPI
return '?' . $query; return '?' . $query;
} }
/**
* Check if a page exists at the given path
*
* @param string $path Page path without extension
* @return bool
*/
public function pageExists(string $path): bool public function pageExists(string $path): bool
{ {
$contentDir = $this->cms->config['content_dir']; $contentDir = $this->cms->config['content_dir'];
@@ -106,17 +155,32 @@ class ContentAPI
|| file_exists($basePath . '.html'); || file_exists($basePath . '.html');
} }
/**
* Get the title of the currently viewed page
*
* @return string
*/
public function getCurrentPageTitle(): string public function getCurrentPageTitle(): string
{ {
$page = $this->cms->getPage(); $page = $this->cms->getPage();
return $page['title'] ?? ''; return $page['title'] ?? '';
} }
/**
* Get the path of the currently viewed page
*
* @return string
*/
public function getCurrentPagePath(): string public function getCurrentPagePath(): string
{ {
return $_GET['page'] ?? $this->cms->config['default_page']; return $_GET['page'] ?? $this->cms->config['default_page'];
} }
/**
* Check if the current page is the homepage
*
* @return bool
*/
public function isHomepage(): bool public function isHomepage(): bool
{ {
$defaultPage = $this->cms->config['default_page'] ?? 'index'; $defaultPage = $this->cms->config['default_page'] ?? 'index';
@@ -124,21 +188,42 @@ class ContentAPI
return $currentPage === $defaultPage; return $currentPage === $defaultPage;
} }
/**
* Translate a language key using the current language
*
* @param string $key Language key
* @return string Translated text
*/
public function t(string $key): string public function t(string $key): string
{ {
return $this->cms->t($key); return $this->cms->t($key);
} }
/**
* Get the site title from config
*
* @return string
*/
public function getSiteTitle(): string public function getSiteTitle(): string
{ {
return $this->cms->config['site_title'] ?? 'CodePress'; return $this->cms->config['site_title'] ?? 'CodePress';
} }
/**
* Get all available language codes
*
* @return array Language codes like ['nl', 'en']
*/
public function getAvailableLanguages(): array public function getAvailableLanguages(): array
{ {
return $this->cms->getAvailableLanguages(); return $this->cms->getAvailableLanguages();
} }
/**
* Get search results for the current search query
*
* @return array Search results, or empty array if not searching
*/
public function getSearchResults(): array public function getSearchResults(): array
{ {
if (isset($_GET['search'])) { if (isset($_GET['search'])) {
@@ -147,6 +232,11 @@ class ContentAPI
return []; return [];
} }
/**
* Check if a search is currently active
*
* @return bool
*/
public function isSearching(): bool public function isSearching(): bool
{ {
return isset($_GET['search']); return isset($_GET['search']);
+10
View File
@@ -1374,6 +1374,16 @@ function handleUsers(AdminAuth $auth, array $config): void
require __DIR__ . '/../admin/templates/layout.php'; require __DIR__ . '/../admin/templates/layout.php';
} }
/**
* Render the admin guide page from a markdown file
*
* Reads the guide file for the selected language, renders it through
* CommonMark (with HeadingPermalink IDs moved to heading elements for
* working deep links), and outputs it via the admin layout.
*
* @param AdminAuth $auth Admin authentication instance
* @param array $config Site configuration
*/
function handleGuide(AdminAuth $auth, array $config): void function handleGuide(AdminAuth $auth, array $config): void
{ {
$user = $auth->getCurrentUser(); $user = $auth->getCurrentUser();