Files
CodePress/cms/core/class/ContentAPI.php
T
E.Noorlander 90253673ba ContentAPI voor PHP content bestanden + handleiding in admin
- Nieuwe ContentAPI class beschikbaar als $api in PHP content bestanden
  met methodes: getAllPages, getPage, getMenu, getConfig, buildUrl, etc.
- Admin handleiding pagina op /admin/guide met taalwisselaar
- Zijbalk link naar handleiding in admin menu
- Dubbele alert in config pagina verwijderd
- Handleidingen (nl/en) uitgebreid met Content API referentie
2026-07-28 13:59:32 +02:00

155 lines
4.0 KiB
PHP

<?php
class ContentAPI
{
private CodePressCMS $cms;
public function __construct(CodePressCMS $cms)
{
$this->cms = $cms;
}
public function getAllPages(): array
{
return $this->cms->getAllPageTitles();
}
public function getPage(string $path): ?array
{
$contentDir = $this->cms->config['content_dir'];
$path = preg_replace('/\.(md|php|html)$/', '', $path);
$filePath = $contentDir . '/' . $path;
$extensions = ['md', 'php', 'html'];
$actualPath = null;
foreach ($extensions as $ext) {
if (file_exists($filePath . '.' . $ext)) {
$actualPath = $filePath . '.' . $ext;
break;
}
}
if (!$actualPath || !file_exists($actualPath)) {
return null;
}
$content = file_get_contents($actualPath);
$extension = pathinfo($actualPath, PATHINFO_EXTENSION);
switch ($extension) {
case 'md':
$result = $this->cms->parseMarkdown($content, $actualPath);
break;
case 'php':
$result = $this->cms->parsePHP($actualPath);
$result['content'] = $this->cms->processContent($result['content']);
break;
case 'html':
$result = $this->cms->parseHTML($content, $actualPath);
break;
default:
return null;
}
return [
'title' => $result['title'] ?? '',
'content' => $result['content'] ?? '',
'path' => $path,
'layout' => $result['layout'] ?? 'sidebar-content',
'metadata' => $result['metadata'] ?? [],
];
}
public function getMenu(): array
{
return $this->cms->getMenu();
}
public function getConfig(string $key, $default = null)
{
$keys = explode('.', $key);
$value = $this->cms->config;
foreach ($keys as $k) {
if (!isset($value[$k])) {
return $default;
}
$value = $value[$k];
}
return $value;
}
public function getCurrentLanguage(): string
{
return $this->cms->currentLanguage;
}
public function buildUrl(string $page = 'index', ?string $lang = null, array $params = []): string
{
$lang = $lang ?? $this->getCurrentLanguage();
$query = 'page=' . urlencode($page) . '&lang=' . urlencode($lang);
if (!empty($params)) {
$query .= '&' . http_build_query($params);
}
return '?' . $query;
}
public function pageExists(string $path): bool
{
$contentDir = $this->cms->config['content_dir'];
$path = preg_replace('/\.(md|php|html)$/', '', $path);
$basePath = $contentDir . '/' . $path;
return file_exists($basePath . '.md')
|| file_exists($basePath . '.php')
|| file_exists($basePath . '.html');
}
public function getCurrentPageTitle(): string
{
$page = $this->cms->getPage();
return $page['title'] ?? '';
}
public function getCurrentPagePath(): string
{
return $_GET['page'] ?? $this->cms->config['default_page'];
}
public function isHomepage(): bool
{
$defaultPage = $this->cms->config['default_page'] ?? 'index';
$currentPage = $_GET['page'] ?? $defaultPage;
return $currentPage === $defaultPage;
}
public function t(string $key): string
{
return $this->cms->t($key);
}
public function getSiteTitle(): string
{
return $this->cms->config['site_title'] ?? 'CodePress';
}
public function getAvailableLanguages(): array
{
return $this->cms->getAvailableLanguages();
}
public function getSearchResults(): array
{
if (isset($_GET['search'])) {
return $this->cms->searchResults;
}
return [];
}
public function isSearching(): bool
{
return isset($_GET['search']);
}
}